sm501.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  1. /*
  2. * QEMU SM501 Device
  3. *
  4. * Copyright (c) 2008 Shin-ichiro KAWASAKI
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include "hw.h"
  26. #include "pc.h"
  27. #include "console.h"
  28. #include "devices.h"
  29. #include "sysbus.h"
  30. #include "qdev-addr.h"
  31. #include "range.h"
  32. /*
  33. * Status: 2010/05/07
  34. * - Minimum implementation for Linux console : mmio regs and CRT layer.
  35. * - 2D grapihcs acceleration partially supported : only fill rectangle.
  36. *
  37. * TODO:
  38. * - Panel support
  39. * - Touch panel support
  40. * - USB support
  41. * - UART support
  42. * - More 2D graphics engine support
  43. * - Performance tuning
  44. */
  45. //#define DEBUG_SM501
  46. //#define DEBUG_BITBLT
  47. #ifdef DEBUG_SM501
  48. #define SM501_DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
  49. #else
  50. #define SM501_DPRINTF(fmt, ...) do {} while(0)
  51. #endif
  52. #define MMIO_BASE_OFFSET 0x3e00000
  53. /* SM501 register definitions taken from "linux/include/linux/sm501-regs.h" */
  54. /* System Configuration area */
  55. /* System config base */
  56. #define SM501_SYS_CONFIG (0x000000)
  57. /* config 1 */
  58. #define SM501_SYSTEM_CONTROL (0x000000)
  59. #define SM501_SYSCTRL_PANEL_TRISTATE (1<<0)
  60. #define SM501_SYSCTRL_MEM_TRISTATE (1<<1)
  61. #define SM501_SYSCTRL_CRT_TRISTATE (1<<2)
  62. #define SM501_SYSCTRL_PCI_SLAVE_BURST_MASK (3<<4)
  63. #define SM501_SYSCTRL_PCI_SLAVE_BURST_1 (0<<4)
  64. #define SM501_SYSCTRL_PCI_SLAVE_BURST_2 (1<<4)
  65. #define SM501_SYSCTRL_PCI_SLAVE_BURST_4 (2<<4)
  66. #define SM501_SYSCTRL_PCI_SLAVE_BURST_8 (3<<4)
  67. #define SM501_SYSCTRL_PCI_CLOCK_RUN_EN (1<<6)
  68. #define SM501_SYSCTRL_PCI_RETRY_DISABLE (1<<7)
  69. #define SM501_SYSCTRL_PCI_SUBSYS_LOCK (1<<11)
  70. #define SM501_SYSCTRL_PCI_BURST_READ_EN (1<<15)
  71. /* miscellaneous control */
  72. #define SM501_MISC_CONTROL (0x000004)
  73. #define SM501_MISC_BUS_SH (0x0)
  74. #define SM501_MISC_BUS_PCI (0x1)
  75. #define SM501_MISC_BUS_XSCALE (0x2)
  76. #define SM501_MISC_BUS_NEC (0x6)
  77. #define SM501_MISC_BUS_MASK (0x7)
  78. #define SM501_MISC_VR_62MB (1<<3)
  79. #define SM501_MISC_CDR_RESET (1<<7)
  80. #define SM501_MISC_USB_LB (1<<8)
  81. #define SM501_MISC_USB_SLAVE (1<<9)
  82. #define SM501_MISC_BL_1 (1<<10)
  83. #define SM501_MISC_MC (1<<11)
  84. #define SM501_MISC_DAC_POWER (1<<12)
  85. #define SM501_MISC_IRQ_INVERT (1<<16)
  86. #define SM501_MISC_SH (1<<17)
  87. #define SM501_MISC_HOLD_EMPTY (0<<18)
  88. #define SM501_MISC_HOLD_8 (1<<18)
  89. #define SM501_MISC_HOLD_16 (2<<18)
  90. #define SM501_MISC_HOLD_24 (3<<18)
  91. #define SM501_MISC_HOLD_32 (4<<18)
  92. #define SM501_MISC_HOLD_MASK (7<<18)
  93. #define SM501_MISC_FREQ_12 (1<<24)
  94. #define SM501_MISC_PNL_24BIT (1<<25)
  95. #define SM501_MISC_8051_LE (1<<26)
  96. #define SM501_GPIO31_0_CONTROL (0x000008)
  97. #define SM501_GPIO63_32_CONTROL (0x00000C)
  98. #define SM501_DRAM_CONTROL (0x000010)
  99. /* command list */
  100. #define SM501_ARBTRTN_CONTROL (0x000014)
  101. /* command list */
  102. #define SM501_COMMAND_LIST_STATUS (0x000024)
  103. /* interrupt debug */
  104. #define SM501_RAW_IRQ_STATUS (0x000028)
  105. #define SM501_RAW_IRQ_CLEAR (0x000028)
  106. #define SM501_IRQ_STATUS (0x00002C)
  107. #define SM501_IRQ_MASK (0x000030)
  108. #define SM501_DEBUG_CONTROL (0x000034)
  109. /* power management */
  110. #define SM501_POWERMODE_P2X_SRC (1<<29)
  111. #define SM501_POWERMODE_V2X_SRC (1<<20)
  112. #define SM501_POWERMODE_M_SRC (1<<12)
  113. #define SM501_POWERMODE_M1_SRC (1<<4)
  114. #define SM501_CURRENT_GATE (0x000038)
  115. #define SM501_CURRENT_CLOCK (0x00003C)
  116. #define SM501_POWER_MODE_0_GATE (0x000040)
  117. #define SM501_POWER_MODE_0_CLOCK (0x000044)
  118. #define SM501_POWER_MODE_1_GATE (0x000048)
  119. #define SM501_POWER_MODE_1_CLOCK (0x00004C)
  120. #define SM501_SLEEP_MODE_GATE (0x000050)
  121. #define SM501_POWER_MODE_CONTROL (0x000054)
  122. /* power gates for units within the 501 */
  123. #define SM501_GATE_HOST (0)
  124. #define SM501_GATE_MEMORY (1)
  125. #define SM501_GATE_DISPLAY (2)
  126. #define SM501_GATE_2D_ENGINE (3)
  127. #define SM501_GATE_CSC (4)
  128. #define SM501_GATE_ZVPORT (5)
  129. #define SM501_GATE_GPIO (6)
  130. #define SM501_GATE_UART0 (7)
  131. #define SM501_GATE_UART1 (8)
  132. #define SM501_GATE_SSP (10)
  133. #define SM501_GATE_USB_HOST (11)
  134. #define SM501_GATE_USB_GADGET (12)
  135. #define SM501_GATE_UCONTROLLER (17)
  136. #define SM501_GATE_AC97 (18)
  137. /* panel clock */
  138. #define SM501_CLOCK_P2XCLK (24)
  139. /* crt clock */
  140. #define SM501_CLOCK_V2XCLK (16)
  141. /* main clock */
  142. #define SM501_CLOCK_MCLK (8)
  143. /* SDRAM controller clock */
  144. #define SM501_CLOCK_M1XCLK (0)
  145. /* config 2 */
  146. #define SM501_PCI_MASTER_BASE (0x000058)
  147. #define SM501_ENDIAN_CONTROL (0x00005C)
  148. #define SM501_DEVICEID (0x000060)
  149. /* 0x050100A0 */
  150. #define SM501_DEVICEID_SM501 (0x05010000)
  151. #define SM501_DEVICEID_IDMASK (0xffff0000)
  152. #define SM501_DEVICEID_REVMASK (0x000000ff)
  153. #define SM501_PLLCLOCK_COUNT (0x000064)
  154. #define SM501_MISC_TIMING (0x000068)
  155. #define SM501_CURRENT_SDRAM_CLOCK (0x00006C)
  156. #define SM501_PROGRAMMABLE_PLL_CONTROL (0x000074)
  157. /* GPIO base */
  158. #define SM501_GPIO (0x010000)
  159. #define SM501_GPIO_DATA_LOW (0x00)
  160. #define SM501_GPIO_DATA_HIGH (0x04)
  161. #define SM501_GPIO_DDR_LOW (0x08)
  162. #define SM501_GPIO_DDR_HIGH (0x0C)
  163. #define SM501_GPIO_IRQ_SETUP (0x10)
  164. #define SM501_GPIO_IRQ_STATUS (0x14)
  165. #define SM501_GPIO_IRQ_RESET (0x14)
  166. /* I2C controller base */
  167. #define SM501_I2C (0x010040)
  168. #define SM501_I2C_BYTE_COUNT (0x00)
  169. #define SM501_I2C_CONTROL (0x01)
  170. #define SM501_I2C_STATUS (0x02)
  171. #define SM501_I2C_RESET (0x02)
  172. #define SM501_I2C_SLAVE_ADDRESS (0x03)
  173. #define SM501_I2C_DATA (0x04)
  174. /* SSP base */
  175. #define SM501_SSP (0x020000)
  176. /* Uart 0 base */
  177. #define SM501_UART0 (0x030000)
  178. /* Uart 1 base */
  179. #define SM501_UART1 (0x030020)
  180. /* USB host port base */
  181. #define SM501_USB_HOST (0x040000)
  182. /* USB slave/gadget base */
  183. #define SM501_USB_GADGET (0x060000)
  184. /* USB slave/gadget data port base */
  185. #define SM501_USB_GADGET_DATA (0x070000)
  186. /* Display controller/video engine base */
  187. #define SM501_DC (0x080000)
  188. /* common defines for the SM501 address registers */
  189. #define SM501_ADDR_FLIP (1<<31)
  190. #define SM501_ADDR_EXT (1<<27)
  191. #define SM501_ADDR_CS1 (1<<26)
  192. #define SM501_ADDR_MASK (0x3f << 26)
  193. #define SM501_FIFO_MASK (0x3 << 16)
  194. #define SM501_FIFO_1 (0x0 << 16)
  195. #define SM501_FIFO_3 (0x1 << 16)
  196. #define SM501_FIFO_7 (0x2 << 16)
  197. #define SM501_FIFO_11 (0x3 << 16)
  198. /* common registers for panel and the crt */
  199. #define SM501_OFF_DC_H_TOT (0x000)
  200. #define SM501_OFF_DC_V_TOT (0x008)
  201. #define SM501_OFF_DC_H_SYNC (0x004)
  202. #define SM501_OFF_DC_V_SYNC (0x00C)
  203. #define SM501_DC_PANEL_CONTROL (0x000)
  204. #define SM501_DC_PANEL_CONTROL_FPEN (1<<27)
  205. #define SM501_DC_PANEL_CONTROL_BIAS (1<<26)
  206. #define SM501_DC_PANEL_CONTROL_DATA (1<<25)
  207. #define SM501_DC_PANEL_CONTROL_VDD (1<<24)
  208. #define SM501_DC_PANEL_CONTROL_DP (1<<23)
  209. #define SM501_DC_PANEL_CONTROL_TFT_888 (0<<21)
  210. #define SM501_DC_PANEL_CONTROL_TFT_333 (1<<21)
  211. #define SM501_DC_PANEL_CONTROL_TFT_444 (2<<21)
  212. #define SM501_DC_PANEL_CONTROL_DE (1<<20)
  213. #define SM501_DC_PANEL_CONTROL_LCD_TFT (0<<18)
  214. #define SM501_DC_PANEL_CONTROL_LCD_STN8 (1<<18)
  215. #define SM501_DC_PANEL_CONTROL_LCD_STN12 (2<<18)
  216. #define SM501_DC_PANEL_CONTROL_CP (1<<14)
  217. #define SM501_DC_PANEL_CONTROL_VSP (1<<13)
  218. #define SM501_DC_PANEL_CONTROL_HSP (1<<12)
  219. #define SM501_DC_PANEL_CONTROL_CK (1<<9)
  220. #define SM501_DC_PANEL_CONTROL_TE (1<<8)
  221. #define SM501_DC_PANEL_CONTROL_VPD (1<<7)
  222. #define SM501_DC_PANEL_CONTROL_VP (1<<6)
  223. #define SM501_DC_PANEL_CONTROL_HPD (1<<5)
  224. #define SM501_DC_PANEL_CONTROL_HP (1<<4)
  225. #define SM501_DC_PANEL_CONTROL_GAMMA (1<<3)
  226. #define SM501_DC_PANEL_CONTROL_EN (1<<2)
  227. #define SM501_DC_PANEL_CONTROL_8BPP (0<<0)
  228. #define SM501_DC_PANEL_CONTROL_16BPP (1<<0)
  229. #define SM501_DC_PANEL_CONTROL_32BPP (2<<0)
  230. #define SM501_DC_PANEL_PANNING_CONTROL (0x004)
  231. #define SM501_DC_PANEL_COLOR_KEY (0x008)
  232. #define SM501_DC_PANEL_FB_ADDR (0x00C)
  233. #define SM501_DC_PANEL_FB_OFFSET (0x010)
  234. #define SM501_DC_PANEL_FB_WIDTH (0x014)
  235. #define SM501_DC_PANEL_FB_HEIGHT (0x018)
  236. #define SM501_DC_PANEL_TL_LOC (0x01C)
  237. #define SM501_DC_PANEL_BR_LOC (0x020)
  238. #define SM501_DC_PANEL_H_TOT (0x024)
  239. #define SM501_DC_PANEL_H_SYNC (0x028)
  240. #define SM501_DC_PANEL_V_TOT (0x02C)
  241. #define SM501_DC_PANEL_V_SYNC (0x030)
  242. #define SM501_DC_PANEL_CUR_LINE (0x034)
  243. #define SM501_DC_VIDEO_CONTROL (0x040)
  244. #define SM501_DC_VIDEO_FB0_ADDR (0x044)
  245. #define SM501_DC_VIDEO_FB_WIDTH (0x048)
  246. #define SM501_DC_VIDEO_FB0_LAST_ADDR (0x04C)
  247. #define SM501_DC_VIDEO_TL_LOC (0x050)
  248. #define SM501_DC_VIDEO_BR_LOC (0x054)
  249. #define SM501_DC_VIDEO_SCALE (0x058)
  250. #define SM501_DC_VIDEO_INIT_SCALE (0x05C)
  251. #define SM501_DC_VIDEO_YUV_CONSTANTS (0x060)
  252. #define SM501_DC_VIDEO_FB1_ADDR (0x064)
  253. #define SM501_DC_VIDEO_FB1_LAST_ADDR (0x068)
  254. #define SM501_DC_VIDEO_ALPHA_CONTROL (0x080)
  255. #define SM501_DC_VIDEO_ALPHA_FB_ADDR (0x084)
  256. #define SM501_DC_VIDEO_ALPHA_FB_OFFSET (0x088)
  257. #define SM501_DC_VIDEO_ALPHA_FB_LAST_ADDR (0x08C)
  258. #define SM501_DC_VIDEO_ALPHA_TL_LOC (0x090)
  259. #define SM501_DC_VIDEO_ALPHA_BR_LOC (0x094)
  260. #define SM501_DC_VIDEO_ALPHA_SCALE (0x098)
  261. #define SM501_DC_VIDEO_ALPHA_INIT_SCALE (0x09C)
  262. #define SM501_DC_VIDEO_ALPHA_CHROMA_KEY (0x0A0)
  263. #define SM501_DC_VIDEO_ALPHA_COLOR_LOOKUP (0x0A4)
  264. #define SM501_DC_PANEL_HWC_BASE (0x0F0)
  265. #define SM501_DC_PANEL_HWC_ADDR (0x0F0)
  266. #define SM501_DC_PANEL_HWC_LOC (0x0F4)
  267. #define SM501_DC_PANEL_HWC_COLOR_1_2 (0x0F8)
  268. #define SM501_DC_PANEL_HWC_COLOR_3 (0x0FC)
  269. #define SM501_HWC_EN (1<<31)
  270. #define SM501_OFF_HWC_ADDR (0x00)
  271. #define SM501_OFF_HWC_LOC (0x04)
  272. #define SM501_OFF_HWC_COLOR_1_2 (0x08)
  273. #define SM501_OFF_HWC_COLOR_3 (0x0C)
  274. #define SM501_DC_ALPHA_CONTROL (0x100)
  275. #define SM501_DC_ALPHA_FB_ADDR (0x104)
  276. #define SM501_DC_ALPHA_FB_OFFSET (0x108)
  277. #define SM501_DC_ALPHA_TL_LOC (0x10C)
  278. #define SM501_DC_ALPHA_BR_LOC (0x110)
  279. #define SM501_DC_ALPHA_CHROMA_KEY (0x114)
  280. #define SM501_DC_ALPHA_COLOR_LOOKUP (0x118)
  281. #define SM501_DC_CRT_CONTROL (0x200)
  282. #define SM501_DC_CRT_CONTROL_TVP (1<<15)
  283. #define SM501_DC_CRT_CONTROL_CP (1<<14)
  284. #define SM501_DC_CRT_CONTROL_VSP (1<<13)
  285. #define SM501_DC_CRT_CONTROL_HSP (1<<12)
  286. #define SM501_DC_CRT_CONTROL_VS (1<<11)
  287. #define SM501_DC_CRT_CONTROL_BLANK (1<<10)
  288. #define SM501_DC_CRT_CONTROL_SEL (1<<9)
  289. #define SM501_DC_CRT_CONTROL_TE (1<<8)
  290. #define SM501_DC_CRT_CONTROL_PIXEL_MASK (0xF << 4)
  291. #define SM501_DC_CRT_CONTROL_GAMMA (1<<3)
  292. #define SM501_DC_CRT_CONTROL_ENABLE (1<<2)
  293. #define SM501_DC_CRT_CONTROL_8BPP (0<<0)
  294. #define SM501_DC_CRT_CONTROL_16BPP (1<<0)
  295. #define SM501_DC_CRT_CONTROL_32BPP (2<<0)
  296. #define SM501_DC_CRT_FB_ADDR (0x204)
  297. #define SM501_DC_CRT_FB_OFFSET (0x208)
  298. #define SM501_DC_CRT_H_TOT (0x20C)
  299. #define SM501_DC_CRT_H_SYNC (0x210)
  300. #define SM501_DC_CRT_V_TOT (0x214)
  301. #define SM501_DC_CRT_V_SYNC (0x218)
  302. #define SM501_DC_CRT_SIGNATURE_ANALYZER (0x21C)
  303. #define SM501_DC_CRT_CUR_LINE (0x220)
  304. #define SM501_DC_CRT_MONITOR_DETECT (0x224)
  305. #define SM501_DC_CRT_HWC_BASE (0x230)
  306. #define SM501_DC_CRT_HWC_ADDR (0x230)
  307. #define SM501_DC_CRT_HWC_LOC (0x234)
  308. #define SM501_DC_CRT_HWC_COLOR_1_2 (0x238)
  309. #define SM501_DC_CRT_HWC_COLOR_3 (0x23C)
  310. #define SM501_DC_PANEL_PALETTE (0x400)
  311. #define SM501_DC_VIDEO_PALETTE (0x800)
  312. #define SM501_DC_CRT_PALETTE (0xC00)
  313. /* Zoom Video port base */
  314. #define SM501_ZVPORT (0x090000)
  315. /* AC97/I2S base */
  316. #define SM501_AC97 (0x0A0000)
  317. /* 8051 micro controller base */
  318. #define SM501_UCONTROLLER (0x0B0000)
  319. /* 8051 micro controller SRAM base */
  320. #define SM501_UCONTROLLER_SRAM (0x0C0000)
  321. /* DMA base */
  322. #define SM501_DMA (0x0D0000)
  323. /* 2d engine base */
  324. #define SM501_2D_ENGINE (0x100000)
  325. #define SM501_2D_SOURCE (0x00)
  326. #define SM501_2D_DESTINATION (0x04)
  327. #define SM501_2D_DIMENSION (0x08)
  328. #define SM501_2D_CONTROL (0x0C)
  329. #define SM501_2D_PITCH (0x10)
  330. #define SM501_2D_FOREGROUND (0x14)
  331. #define SM501_2D_BACKGROUND (0x18)
  332. #define SM501_2D_STRETCH (0x1C)
  333. #define SM501_2D_COLOR_COMPARE (0x20)
  334. #define SM501_2D_COLOR_COMPARE_MASK (0x24)
  335. #define SM501_2D_MASK (0x28)
  336. #define SM501_2D_CLIP_TL (0x2C)
  337. #define SM501_2D_CLIP_BR (0x30)
  338. #define SM501_2D_MONO_PATTERN_LOW (0x34)
  339. #define SM501_2D_MONO_PATTERN_HIGH (0x38)
  340. #define SM501_2D_WINDOW_WIDTH (0x3C)
  341. #define SM501_2D_SOURCE_BASE (0x40)
  342. #define SM501_2D_DESTINATION_BASE (0x44)
  343. #define SM501_2D_ALPHA (0x48)
  344. #define SM501_2D_WRAP (0x4C)
  345. #define SM501_2D_STATUS (0x50)
  346. #define SM501_CSC_Y_SOURCE_BASE (0xC8)
  347. #define SM501_CSC_CONSTANTS (0xCC)
  348. #define SM501_CSC_Y_SOURCE_X (0xD0)
  349. #define SM501_CSC_Y_SOURCE_Y (0xD4)
  350. #define SM501_CSC_U_SOURCE_BASE (0xD8)
  351. #define SM501_CSC_V_SOURCE_BASE (0xDC)
  352. #define SM501_CSC_SOURCE_DIMENSION (0xE0)
  353. #define SM501_CSC_SOURCE_PITCH (0xE4)
  354. #define SM501_CSC_DESTINATION (0xE8)
  355. #define SM501_CSC_DESTINATION_DIMENSION (0xEC)
  356. #define SM501_CSC_DESTINATION_PITCH (0xF0)
  357. #define SM501_CSC_SCALE_FACTOR (0xF4)
  358. #define SM501_CSC_DESTINATION_BASE (0xF8)
  359. #define SM501_CSC_CONTROL (0xFC)
  360. /* 2d engine data port base */
  361. #define SM501_2D_ENGINE_DATA (0x110000)
  362. /* end of register definitions */
  363. #define SM501_HWC_WIDTH (64)
  364. #define SM501_HWC_HEIGHT (64)
  365. /* SM501 local memory size taken from "linux/drivers/mfd/sm501.c" */
  366. static const uint32_t sm501_mem_local_size[] = {
  367. [0] = 4*1024*1024,
  368. [1] = 8*1024*1024,
  369. [2] = 16*1024*1024,
  370. [3] = 32*1024*1024,
  371. [4] = 64*1024*1024,
  372. [5] = 2*1024*1024,
  373. };
  374. #define get_local_mem_size(s) sm501_mem_local_size[(s)->local_mem_size_index]
  375. typedef struct SM501State {
  376. /* graphic console status */
  377. DisplayState *ds;
  378. /* status & internal resources */
  379. target_phys_addr_t base;
  380. uint32_t local_mem_size_index;
  381. uint8_t * local_mem;
  382. ram_addr_t local_mem_offset;
  383. uint32_t last_width;
  384. uint32_t last_height;
  385. /* mmio registers */
  386. uint32_t system_control;
  387. uint32_t misc_control;
  388. uint32_t gpio_31_0_control;
  389. uint32_t gpio_63_32_control;
  390. uint32_t dram_control;
  391. uint32_t irq_mask;
  392. uint32_t misc_timing;
  393. uint32_t power_mode_control;
  394. uint32_t uart0_ier;
  395. uint32_t uart0_lcr;
  396. uint32_t uart0_mcr;
  397. uint32_t uart0_scr;
  398. uint8_t dc_palette[0x400 * 3];
  399. uint32_t dc_panel_control;
  400. uint32_t dc_panel_panning_control;
  401. uint32_t dc_panel_fb_addr;
  402. uint32_t dc_panel_fb_offset;
  403. uint32_t dc_panel_fb_width;
  404. uint32_t dc_panel_fb_height;
  405. uint32_t dc_panel_tl_location;
  406. uint32_t dc_panel_br_location;
  407. uint32_t dc_panel_h_total;
  408. uint32_t dc_panel_h_sync;
  409. uint32_t dc_panel_v_total;
  410. uint32_t dc_panel_v_sync;
  411. uint32_t dc_panel_hwc_addr;
  412. uint32_t dc_panel_hwc_location;
  413. uint32_t dc_panel_hwc_color_1_2;
  414. uint32_t dc_panel_hwc_color_3;
  415. uint32_t dc_crt_control;
  416. uint32_t dc_crt_fb_addr;
  417. uint32_t dc_crt_fb_offset;
  418. uint32_t dc_crt_h_total;
  419. uint32_t dc_crt_h_sync;
  420. uint32_t dc_crt_v_total;
  421. uint32_t dc_crt_v_sync;
  422. uint32_t dc_crt_hwc_addr;
  423. uint32_t dc_crt_hwc_location;
  424. uint32_t dc_crt_hwc_color_1_2;
  425. uint32_t dc_crt_hwc_color_3;
  426. uint32_t twoD_source;
  427. uint32_t twoD_destination;
  428. uint32_t twoD_dimension;
  429. uint32_t twoD_control;
  430. uint32_t twoD_pitch;
  431. uint32_t twoD_foreground;
  432. uint32_t twoD_stretch;
  433. uint32_t twoD_color_compare_mask;
  434. uint32_t twoD_mask;
  435. uint32_t twoD_window_width;
  436. uint32_t twoD_source_base;
  437. uint32_t twoD_destination_base;
  438. } SM501State;
  439. static uint32_t get_local_mem_size_index(uint32_t size)
  440. {
  441. uint32_t norm_size = 0;
  442. int i, index = 0;
  443. for (i = 0; i < ARRAY_SIZE(sm501_mem_local_size); i++) {
  444. uint32_t new_size = sm501_mem_local_size[i];
  445. if (new_size >= size) {
  446. if (norm_size == 0 || norm_size > new_size) {
  447. norm_size = new_size;
  448. index = i;
  449. }
  450. }
  451. }
  452. return index;
  453. }
  454. /**
  455. * Check the availability of hardware cursor.
  456. * @param crt 0 for PANEL, 1 for CRT.
  457. */
  458. static inline int is_hwc_enabled(SM501State *state, int crt)
  459. {
  460. uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
  461. return addr & 0x80000000;
  462. }
  463. /**
  464. * Get the address which holds cursor pattern data.
  465. * @param crt 0 for PANEL, 1 for CRT.
  466. */
  467. static inline uint32_t get_hwc_address(SM501State *state, int crt)
  468. {
  469. uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
  470. return (addr & 0x03FFFFF0)/* >> 4*/;
  471. }
  472. /**
  473. * Get the cursor position in y coordinate.
  474. * @param crt 0 for PANEL, 1 for CRT.
  475. */
  476. static inline uint32_t get_hwc_y(SM501State *state, int crt)
  477. {
  478. uint32_t location = crt ? state->dc_crt_hwc_location
  479. : state->dc_panel_hwc_location;
  480. return (location & 0x07FF0000) >> 16;
  481. }
  482. /**
  483. * Get the cursor position in x coordinate.
  484. * @param crt 0 for PANEL, 1 for CRT.
  485. */
  486. static inline uint32_t get_hwc_x(SM501State *state, int crt)
  487. {
  488. uint32_t location = crt ? state->dc_crt_hwc_location
  489. : state->dc_panel_hwc_location;
  490. return location & 0x000007FF;
  491. }
  492. /**
  493. * Get the cursor position in x coordinate.
  494. * @param crt 0 for PANEL, 1 for CRT.
  495. * @param index 0, 1, 2 or 3 which specifies color of corsor dot.
  496. */
  497. static inline uint16_t get_hwc_color(SM501State *state, int crt, int index)
  498. {
  499. uint16_t color_reg = 0;
  500. uint16_t color_565 = 0;
  501. if (index == 0) {
  502. return 0;
  503. }
  504. switch (index) {
  505. case 1:
  506. case 2:
  507. color_reg = crt ? state->dc_crt_hwc_color_1_2
  508. : state->dc_panel_hwc_color_1_2;
  509. break;
  510. case 3:
  511. color_reg = crt ? state->dc_crt_hwc_color_3
  512. : state->dc_panel_hwc_color_3;
  513. break;
  514. default:
  515. printf("invalid hw cursor color.\n");
  516. abort();
  517. }
  518. switch (index) {
  519. case 1:
  520. case 3:
  521. color_565 = (uint16_t)(color_reg & 0xFFFF);
  522. break;
  523. case 2:
  524. color_565 = (uint16_t)((color_reg >> 16) & 0xFFFF);
  525. break;
  526. }
  527. return color_565;
  528. }
  529. static int within_hwc_y_range(SM501State *state, int y, int crt)
  530. {
  531. int hwc_y = get_hwc_y(state, crt);
  532. return (hwc_y <= y && y < hwc_y + SM501_HWC_HEIGHT);
  533. }
  534. static void sm501_2d_operation(SM501State * s)
  535. {
  536. /* obtain operation parameters */
  537. int operation = (s->twoD_control >> 16) & 0x1f;
  538. int rtl = s->twoD_control & 0x8000000;
  539. int src_x = (s->twoD_source >> 16) & 0x01FFF;
  540. int src_y = s->twoD_source & 0xFFFF;
  541. int dst_x = (s->twoD_destination >> 16) & 0x01FFF;
  542. int dst_y = s->twoD_destination & 0xFFFF;
  543. int operation_width = (s->twoD_dimension >> 16) & 0x1FFF;
  544. int operation_height = s->twoD_dimension & 0xFFFF;
  545. uint32_t color = s->twoD_foreground;
  546. int format_flags = (s->twoD_stretch >> 20) & 0x3;
  547. int addressing = (s->twoD_stretch >> 16) & 0xF;
  548. /* get frame buffer info */
  549. uint8_t * src = s->local_mem + (s->twoD_source_base & 0x03FFFFFF);
  550. uint8_t * dst = s->local_mem + (s->twoD_destination_base & 0x03FFFFFF);
  551. int src_width = (s->dc_crt_h_total & 0x00000FFF) + 1;
  552. int dst_width = (s->dc_crt_h_total & 0x00000FFF) + 1;
  553. if (addressing != 0x0) {
  554. printf("%s: only XY addressing is supported.\n", __func__);
  555. abort();
  556. }
  557. if ((s->twoD_source_base & 0x08000000) ||
  558. (s->twoD_destination_base & 0x08000000)) {
  559. printf("%s: only local memory is supported.\n", __func__);
  560. abort();
  561. }
  562. switch (operation) {
  563. case 0x00: /* copy area */
  564. #define COPY_AREA(_bpp, _pixel_type, rtl) { \
  565. int y, x, index_d, index_s; \
  566. for (y = 0; y < operation_height; y++) { \
  567. for (x = 0; x < operation_width; x++) { \
  568. if (rtl) { \
  569. index_s = ((src_y - y) * src_width + src_x - x) * _bpp; \
  570. index_d = ((dst_y - y) * dst_width + dst_x - x) * _bpp; \
  571. } else { \
  572. index_s = ((src_y + y) * src_width + src_x + x) * _bpp; \
  573. index_d = ((dst_y + y) * dst_width + dst_x + x) * _bpp; \
  574. } \
  575. *(_pixel_type*)&dst[index_d] = *(_pixel_type*)&src[index_s];\
  576. } \
  577. } \
  578. }
  579. switch (format_flags) {
  580. case 0:
  581. COPY_AREA(1, uint8_t, rtl);
  582. break;
  583. case 1:
  584. COPY_AREA(2, uint16_t, rtl);
  585. break;
  586. case 2:
  587. COPY_AREA(4, uint32_t, rtl);
  588. break;
  589. }
  590. break;
  591. case 0x01: /* fill rectangle */
  592. #define FILL_RECT(_bpp, _pixel_type) { \
  593. int y, x; \
  594. for (y = 0; y < operation_height; y++) { \
  595. for (x = 0; x < operation_width; x++) { \
  596. int index = ((dst_y + y) * dst_width + dst_x + x) * _bpp; \
  597. *(_pixel_type*)&dst[index] = (_pixel_type)color; \
  598. } \
  599. } \
  600. }
  601. switch (format_flags) {
  602. case 0:
  603. FILL_RECT(1, uint8_t);
  604. break;
  605. case 1:
  606. FILL_RECT(2, uint16_t);
  607. break;
  608. case 2:
  609. FILL_RECT(4, uint32_t);
  610. break;
  611. }
  612. break;
  613. default:
  614. printf("non-implemented SM501 2D operation. %d\n", operation);
  615. abort();
  616. break;
  617. }
  618. }
  619. static uint32_t sm501_system_config_read(void *opaque, target_phys_addr_t addr)
  620. {
  621. SM501State * s = (SM501State *)opaque;
  622. uint32_t ret = 0;
  623. SM501_DPRINTF("sm501 system config regs : read addr=%x\n", (int)addr);
  624. switch(addr) {
  625. case SM501_SYSTEM_CONTROL:
  626. ret = s->system_control;
  627. break;
  628. case SM501_MISC_CONTROL:
  629. ret = s->misc_control;
  630. break;
  631. case SM501_GPIO31_0_CONTROL:
  632. ret = s->gpio_31_0_control;
  633. break;
  634. case SM501_GPIO63_32_CONTROL:
  635. ret = s->gpio_63_32_control;
  636. break;
  637. case SM501_DEVICEID:
  638. ret = 0x050100A0;
  639. break;
  640. case SM501_DRAM_CONTROL:
  641. ret = (s->dram_control & 0x07F107C0) | s->local_mem_size_index << 13;
  642. break;
  643. case SM501_IRQ_MASK:
  644. ret = s->irq_mask;
  645. break;
  646. case SM501_MISC_TIMING:
  647. /* TODO : simulate gate control */
  648. ret = s->misc_timing;
  649. break;
  650. case SM501_CURRENT_GATE:
  651. /* TODO : simulate gate control */
  652. ret = 0x00021807;
  653. break;
  654. case SM501_CURRENT_CLOCK:
  655. ret = 0x2A1A0A09;
  656. break;
  657. case SM501_POWER_MODE_CONTROL:
  658. ret = s->power_mode_control;
  659. break;
  660. default:
  661. printf("sm501 system config : not implemented register read."
  662. " addr=%x\n", (int)addr);
  663. abort();
  664. }
  665. return ret;
  666. }
  667. static void sm501_system_config_write(void *opaque,
  668. target_phys_addr_t addr, uint32_t value)
  669. {
  670. SM501State * s = (SM501State *)opaque;
  671. SM501_DPRINTF("sm501 system config regs : write addr=%x, val=%x\n",
  672. addr, value);
  673. switch(addr) {
  674. case SM501_SYSTEM_CONTROL:
  675. s->system_control = value & 0xE300B8F7;
  676. break;
  677. case SM501_MISC_CONTROL:
  678. s->misc_control = value & 0xFF7FFF20;
  679. break;
  680. case SM501_GPIO31_0_CONTROL:
  681. s->gpio_31_0_control = value;
  682. break;
  683. case SM501_GPIO63_32_CONTROL:
  684. s->gpio_63_32_control = value;
  685. break;
  686. case SM501_DRAM_CONTROL:
  687. s->local_mem_size_index = (value >> 13) & 0x7;
  688. /* rODO : check validity of size change */
  689. s->dram_control |= value & 0x7FFFFFC3;
  690. break;
  691. case SM501_IRQ_MASK:
  692. s->irq_mask = value;
  693. break;
  694. case SM501_MISC_TIMING:
  695. s->misc_timing = value & 0xF31F1FFF;
  696. break;
  697. case SM501_POWER_MODE_0_GATE:
  698. case SM501_POWER_MODE_1_GATE:
  699. case SM501_POWER_MODE_0_CLOCK:
  700. case SM501_POWER_MODE_1_CLOCK:
  701. /* TODO : simulate gate & clock control */
  702. break;
  703. case SM501_POWER_MODE_CONTROL:
  704. s->power_mode_control = value & 0x00000003;
  705. break;
  706. default:
  707. printf("sm501 system config : not implemented register write."
  708. " addr=%x, val=%x\n", (int)addr, value);
  709. abort();
  710. }
  711. }
  712. static CPUReadMemoryFunc * const sm501_system_config_readfn[] = {
  713. NULL,
  714. NULL,
  715. &sm501_system_config_read,
  716. };
  717. static CPUWriteMemoryFunc * const sm501_system_config_writefn[] = {
  718. NULL,
  719. NULL,
  720. &sm501_system_config_write,
  721. };
  722. static uint32_t sm501_palette_read(void *opaque, target_phys_addr_t addr)
  723. {
  724. SM501State * s = (SM501State *)opaque;
  725. SM501_DPRINTF("sm501 palette read addr=%x\n", (int)addr);
  726. /* TODO : consider BYTE/WORD access */
  727. /* TODO : consider endian */
  728. assert(range_covers_byte(0, 0x400 * 3, addr));
  729. return *(uint32_t*)&s->dc_palette[addr];
  730. }
  731. static void sm501_palette_write(void *opaque,
  732. target_phys_addr_t addr, uint32_t value)
  733. {
  734. SM501State * s = (SM501State *)opaque;
  735. SM501_DPRINTF("sm501 palette write addr=%x, val=%x\n",
  736. (int)addr, value);
  737. /* TODO : consider BYTE/WORD access */
  738. /* TODO : consider endian */
  739. assert(range_covers_byte(0, 0x400 * 3, addr));
  740. *(uint32_t*)&s->dc_palette[addr] = value;
  741. }
  742. static uint32_t sm501_disp_ctrl_read(void *opaque, target_phys_addr_t addr)
  743. {
  744. SM501State * s = (SM501State *)opaque;
  745. uint32_t ret = 0;
  746. SM501_DPRINTF("sm501 disp ctrl regs : read addr=%x\n", (int)addr);
  747. switch(addr) {
  748. case SM501_DC_PANEL_CONTROL:
  749. ret = s->dc_panel_control;
  750. break;
  751. case SM501_DC_PANEL_PANNING_CONTROL:
  752. ret = s->dc_panel_panning_control;
  753. break;
  754. case SM501_DC_PANEL_FB_ADDR:
  755. ret = s->dc_panel_fb_addr;
  756. break;
  757. case SM501_DC_PANEL_FB_OFFSET:
  758. ret = s->dc_panel_fb_offset;
  759. break;
  760. case SM501_DC_PANEL_FB_WIDTH:
  761. ret = s->dc_panel_fb_width;
  762. break;
  763. case SM501_DC_PANEL_FB_HEIGHT:
  764. ret = s->dc_panel_fb_height;
  765. break;
  766. case SM501_DC_PANEL_TL_LOC:
  767. ret = s->dc_panel_tl_location;
  768. break;
  769. case SM501_DC_PANEL_BR_LOC:
  770. ret = s->dc_panel_br_location;
  771. break;
  772. case SM501_DC_PANEL_H_TOT:
  773. ret = s->dc_panel_h_total;
  774. break;
  775. case SM501_DC_PANEL_H_SYNC:
  776. ret = s->dc_panel_h_sync;
  777. break;
  778. case SM501_DC_PANEL_V_TOT:
  779. ret = s->dc_panel_v_total;
  780. break;
  781. case SM501_DC_PANEL_V_SYNC:
  782. ret = s->dc_panel_v_sync;
  783. break;
  784. case SM501_DC_CRT_CONTROL:
  785. ret = s->dc_crt_control;
  786. break;
  787. case SM501_DC_CRT_FB_ADDR:
  788. ret = s->dc_crt_fb_addr;
  789. break;
  790. case SM501_DC_CRT_FB_OFFSET:
  791. ret = s->dc_crt_fb_offset;
  792. break;
  793. case SM501_DC_CRT_H_TOT:
  794. ret = s->dc_crt_h_total;
  795. break;
  796. case SM501_DC_CRT_H_SYNC:
  797. ret = s->dc_crt_h_sync;
  798. break;
  799. case SM501_DC_CRT_V_TOT:
  800. ret = s->dc_crt_v_total;
  801. break;
  802. case SM501_DC_CRT_V_SYNC:
  803. ret = s->dc_crt_v_sync;
  804. break;
  805. case SM501_DC_CRT_HWC_ADDR:
  806. ret = s->dc_crt_hwc_addr;
  807. break;
  808. case SM501_DC_CRT_HWC_LOC:
  809. ret = s->dc_crt_hwc_location;
  810. break;
  811. case SM501_DC_CRT_HWC_COLOR_1_2:
  812. ret = s->dc_crt_hwc_color_1_2;
  813. break;
  814. case SM501_DC_CRT_HWC_COLOR_3:
  815. ret = s->dc_crt_hwc_color_3;
  816. break;
  817. case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400*3 - 4:
  818. ret = sm501_palette_read(opaque, addr - SM501_DC_PANEL_PALETTE);
  819. break;
  820. default:
  821. printf("sm501 disp ctrl : not implemented register read."
  822. " addr=%x\n", (int)addr);
  823. abort();
  824. }
  825. return ret;
  826. }
  827. static void sm501_disp_ctrl_write(void *opaque,
  828. target_phys_addr_t addr,
  829. uint32_t value)
  830. {
  831. SM501State * s = (SM501State *)opaque;
  832. SM501_DPRINTF("sm501 disp ctrl regs : write addr=%x, val=%x\n",
  833. addr, value);
  834. switch(addr) {
  835. case SM501_DC_PANEL_CONTROL:
  836. s->dc_panel_control = value & 0x0FFF73FF;
  837. break;
  838. case SM501_DC_PANEL_PANNING_CONTROL:
  839. s->dc_panel_panning_control = value & 0xFF3FFF3F;
  840. break;
  841. case SM501_DC_PANEL_FB_ADDR:
  842. s->dc_panel_fb_addr = value & 0x8FFFFFF0;
  843. break;
  844. case SM501_DC_PANEL_FB_OFFSET:
  845. s->dc_panel_fb_offset = value & 0x3FF03FF0;
  846. break;
  847. case SM501_DC_PANEL_FB_WIDTH:
  848. s->dc_panel_fb_width = value & 0x0FFF0FFF;
  849. break;
  850. case SM501_DC_PANEL_FB_HEIGHT:
  851. s->dc_panel_fb_height = value & 0x0FFF0FFF;
  852. break;
  853. case SM501_DC_PANEL_TL_LOC:
  854. s->dc_panel_tl_location = value & 0x07FF07FF;
  855. break;
  856. case SM501_DC_PANEL_BR_LOC:
  857. s->dc_panel_br_location = value & 0x07FF07FF;
  858. break;
  859. case SM501_DC_PANEL_H_TOT:
  860. s->dc_panel_h_total = value & 0x0FFF0FFF;
  861. break;
  862. case SM501_DC_PANEL_H_SYNC:
  863. s->dc_panel_h_sync = value & 0x00FF0FFF;
  864. break;
  865. case SM501_DC_PANEL_V_TOT:
  866. s->dc_panel_v_total = value & 0x0FFF0FFF;
  867. break;
  868. case SM501_DC_PANEL_V_SYNC:
  869. s->dc_panel_v_sync = value & 0x003F0FFF;
  870. break;
  871. case SM501_DC_PANEL_HWC_ADDR:
  872. s->dc_panel_hwc_addr = value & 0x8FFFFFF0;
  873. break;
  874. case SM501_DC_PANEL_HWC_LOC:
  875. s->dc_panel_hwc_location = value & 0x0FFF0FFF;
  876. break;
  877. case SM501_DC_PANEL_HWC_COLOR_1_2:
  878. s->dc_panel_hwc_color_1_2 = value;
  879. break;
  880. case SM501_DC_PANEL_HWC_COLOR_3:
  881. s->dc_panel_hwc_color_3 = value & 0x0000FFFF;
  882. break;
  883. case SM501_DC_CRT_CONTROL:
  884. s->dc_crt_control = value & 0x0003FFFF;
  885. break;
  886. case SM501_DC_CRT_FB_ADDR:
  887. s->dc_crt_fb_addr = value & 0x8FFFFFF0;
  888. break;
  889. case SM501_DC_CRT_FB_OFFSET:
  890. s->dc_crt_fb_offset = value & 0x3FF03FF0;
  891. break;
  892. case SM501_DC_CRT_H_TOT:
  893. s->dc_crt_h_total = value & 0x0FFF0FFF;
  894. break;
  895. case SM501_DC_CRT_H_SYNC:
  896. s->dc_crt_h_sync = value & 0x00FF0FFF;
  897. break;
  898. case SM501_DC_CRT_V_TOT:
  899. s->dc_crt_v_total = value & 0x0FFF0FFF;
  900. break;
  901. case SM501_DC_CRT_V_SYNC:
  902. s->dc_crt_v_sync = value & 0x003F0FFF;
  903. break;
  904. case SM501_DC_CRT_HWC_ADDR:
  905. s->dc_crt_hwc_addr = value & 0x8FFFFFF0;
  906. break;
  907. case SM501_DC_CRT_HWC_LOC:
  908. s->dc_crt_hwc_location = value & 0x0FFF0FFF;
  909. break;
  910. case SM501_DC_CRT_HWC_COLOR_1_2:
  911. s->dc_crt_hwc_color_1_2 = value;
  912. break;
  913. case SM501_DC_CRT_HWC_COLOR_3:
  914. s->dc_crt_hwc_color_3 = value & 0x0000FFFF;
  915. break;
  916. case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400*3 - 4:
  917. sm501_palette_write(opaque, addr - SM501_DC_PANEL_PALETTE, value);
  918. break;
  919. default:
  920. printf("sm501 disp ctrl : not implemented register write."
  921. " addr=%x, val=%x\n", (int)addr, value);
  922. abort();
  923. }
  924. }
  925. static CPUReadMemoryFunc * const sm501_disp_ctrl_readfn[] = {
  926. NULL,
  927. NULL,
  928. &sm501_disp_ctrl_read,
  929. };
  930. static CPUWriteMemoryFunc * const sm501_disp_ctrl_writefn[] = {
  931. NULL,
  932. NULL,
  933. &sm501_disp_ctrl_write,
  934. };
  935. static uint32_t sm501_2d_engine_read(void *opaque, target_phys_addr_t addr)
  936. {
  937. SM501State * s = (SM501State *)opaque;
  938. uint32_t ret = 0;
  939. SM501_DPRINTF("sm501 2d engine regs : read addr=%x\n", (int)addr);
  940. switch(addr) {
  941. case SM501_2D_SOURCE_BASE:
  942. ret = s->twoD_source_base;
  943. break;
  944. default:
  945. printf("sm501 disp ctrl : not implemented register read."
  946. " addr=%x\n", (int)addr);
  947. abort();
  948. }
  949. return ret;
  950. }
  951. static void sm501_2d_engine_write(void *opaque,
  952. target_phys_addr_t addr, uint32_t value)
  953. {
  954. SM501State * s = (SM501State *)opaque;
  955. SM501_DPRINTF("sm501 2d engine regs : write addr=%x, val=%x\n",
  956. addr, value);
  957. switch(addr) {
  958. case SM501_2D_SOURCE:
  959. s->twoD_source = value;
  960. break;
  961. case SM501_2D_DESTINATION:
  962. s->twoD_destination = value;
  963. break;
  964. case SM501_2D_DIMENSION:
  965. s->twoD_dimension = value;
  966. break;
  967. case SM501_2D_CONTROL:
  968. s->twoD_control = value;
  969. /* do 2d operation if start flag is set. */
  970. if (value & 0x80000000) {
  971. sm501_2d_operation(s);
  972. s->twoD_control &= ~0x80000000; /* start flag down */
  973. }
  974. break;
  975. case SM501_2D_PITCH:
  976. s->twoD_pitch = value;
  977. break;
  978. case SM501_2D_FOREGROUND:
  979. s->twoD_foreground = value;
  980. break;
  981. case SM501_2D_STRETCH:
  982. s->twoD_stretch = value;
  983. break;
  984. case SM501_2D_COLOR_COMPARE_MASK:
  985. s->twoD_color_compare_mask = value;
  986. break;
  987. case SM501_2D_MASK:
  988. s->twoD_mask = value;
  989. break;
  990. case SM501_2D_WINDOW_WIDTH:
  991. s->twoD_window_width = value;
  992. break;
  993. case SM501_2D_SOURCE_BASE:
  994. s->twoD_source_base = value;
  995. break;
  996. case SM501_2D_DESTINATION_BASE:
  997. s->twoD_destination_base = value;
  998. break;
  999. default:
  1000. printf("sm501 2d engine : not implemented register write."
  1001. " addr=%x, val=%x\n", (int)addr, value);
  1002. abort();
  1003. }
  1004. }
  1005. static CPUReadMemoryFunc * const sm501_2d_engine_readfn[] = {
  1006. NULL,
  1007. NULL,
  1008. &sm501_2d_engine_read,
  1009. };
  1010. static CPUWriteMemoryFunc * const sm501_2d_engine_writefn[] = {
  1011. NULL,
  1012. NULL,
  1013. &sm501_2d_engine_write,
  1014. };
  1015. /* draw line functions for all console modes */
  1016. #include "pixel_ops.h"
  1017. typedef void draw_line_func(uint8_t *d, const uint8_t *s,
  1018. int width, const uint32_t *pal);
  1019. typedef void draw_hwc_line_func(SM501State * s, int crt, uint8_t * palette,
  1020. int c_y, uint8_t *d, int width);
  1021. #define DEPTH 8
  1022. #include "sm501_template.h"
  1023. #define DEPTH 15
  1024. #include "sm501_template.h"
  1025. #define BGR_FORMAT
  1026. #define DEPTH 15
  1027. #include "sm501_template.h"
  1028. #define DEPTH 16
  1029. #include "sm501_template.h"
  1030. #define BGR_FORMAT
  1031. #define DEPTH 16
  1032. #include "sm501_template.h"
  1033. #define DEPTH 32
  1034. #include "sm501_template.h"
  1035. #define BGR_FORMAT
  1036. #define DEPTH 32
  1037. #include "sm501_template.h"
  1038. static draw_line_func * draw_line8_funcs[] = {
  1039. draw_line8_8,
  1040. draw_line8_15,
  1041. draw_line8_16,
  1042. draw_line8_32,
  1043. draw_line8_32bgr,
  1044. draw_line8_15bgr,
  1045. draw_line8_16bgr,
  1046. };
  1047. static draw_line_func * draw_line16_funcs[] = {
  1048. draw_line16_8,
  1049. draw_line16_15,
  1050. draw_line16_16,
  1051. draw_line16_32,
  1052. draw_line16_32bgr,
  1053. draw_line16_15bgr,
  1054. draw_line16_16bgr,
  1055. };
  1056. static draw_line_func * draw_line32_funcs[] = {
  1057. draw_line32_8,
  1058. draw_line32_15,
  1059. draw_line32_16,
  1060. draw_line32_32,
  1061. draw_line32_32bgr,
  1062. draw_line32_15bgr,
  1063. draw_line32_16bgr,
  1064. };
  1065. static draw_hwc_line_func * draw_hwc_line_funcs[] = {
  1066. draw_hwc_line_8,
  1067. draw_hwc_line_15,
  1068. draw_hwc_line_16,
  1069. draw_hwc_line_32,
  1070. draw_hwc_line_32bgr,
  1071. draw_hwc_line_15bgr,
  1072. draw_hwc_line_16bgr,
  1073. };
  1074. static inline int get_depth_index(DisplayState *s)
  1075. {
  1076. switch(ds_get_bits_per_pixel(s)) {
  1077. default:
  1078. case 8:
  1079. return 0;
  1080. case 15:
  1081. return 1;
  1082. case 16:
  1083. return 2;
  1084. case 32:
  1085. if (is_surface_bgr(s->surface))
  1086. return 4;
  1087. else
  1088. return 3;
  1089. }
  1090. }
  1091. static void sm501_draw_crt(SM501State * s)
  1092. {
  1093. int y;
  1094. int width = (s->dc_crt_h_total & 0x00000FFF) + 1;
  1095. int height = (s->dc_crt_v_total & 0x00000FFF) + 1;
  1096. uint8_t * src = s->local_mem;
  1097. int src_bpp = 0;
  1098. int dst_bpp = ds_get_bytes_per_pixel(s->ds) + (ds_get_bits_per_pixel(s->ds) % 8 ? 1 : 0);
  1099. uint32_t * palette = (uint32_t *)&s->dc_palette[SM501_DC_CRT_PALETTE
  1100. - SM501_DC_PANEL_PALETTE];
  1101. uint8_t hwc_palette[3 * 3];
  1102. int ds_depth_index = get_depth_index(s->ds);
  1103. draw_line_func * draw_line = NULL;
  1104. draw_hwc_line_func * draw_hwc_line = NULL;
  1105. int full_update = 0;
  1106. int y_start = -1;
  1107. ram_addr_t page_min = ~0l;
  1108. ram_addr_t page_max = 0l;
  1109. ram_addr_t offset = s->local_mem_offset;
  1110. /* choose draw_line function */
  1111. switch (s->dc_crt_control & 3) {
  1112. case SM501_DC_CRT_CONTROL_8BPP:
  1113. src_bpp = 1;
  1114. draw_line = draw_line8_funcs[ds_depth_index];
  1115. break;
  1116. case SM501_DC_CRT_CONTROL_16BPP:
  1117. src_bpp = 2;
  1118. draw_line = draw_line16_funcs[ds_depth_index];
  1119. break;
  1120. case SM501_DC_CRT_CONTROL_32BPP:
  1121. src_bpp = 4;
  1122. draw_line = draw_line32_funcs[ds_depth_index];
  1123. break;
  1124. default:
  1125. printf("sm501 draw crt : invalid DC_CRT_CONTROL=%x.\n",
  1126. s->dc_crt_control);
  1127. abort();
  1128. break;
  1129. }
  1130. /* set up to draw hardware cursor */
  1131. if (is_hwc_enabled(s, 1)) {
  1132. int i;
  1133. /* get cursor palette */
  1134. for (i = 0; i < 3; i++) {
  1135. uint16_t rgb565 = get_hwc_color(s, 1, i + 1);
  1136. hwc_palette[i * 3 + 0] = (rgb565 & 0xf800) >> 8; /* red */
  1137. hwc_palette[i * 3 + 1] = (rgb565 & 0x07e0) >> 3; /* green */
  1138. hwc_palette[i * 3 + 2] = (rgb565 & 0x001f) << 3; /* blue */
  1139. }
  1140. /* choose cursor draw line function */
  1141. draw_hwc_line = draw_hwc_line_funcs[ds_depth_index];
  1142. }
  1143. /* adjust console size */
  1144. if (s->last_width != width || s->last_height != height) {
  1145. qemu_console_resize(s->ds, width, height);
  1146. s->last_width = width;
  1147. s->last_height = height;
  1148. full_update = 1;
  1149. }
  1150. /* draw each line according to conditions */
  1151. for (y = 0; y < height; y++) {
  1152. int update_hwc = draw_hwc_line ? within_hwc_y_range(s, y, 1) : 0;
  1153. int update = full_update || update_hwc;
  1154. ram_addr_t page0 = offset & TARGET_PAGE_MASK;
  1155. ram_addr_t page1 = (offset + width * src_bpp - 1) & TARGET_PAGE_MASK;
  1156. ram_addr_t page;
  1157. /* check dirty flags for each line */
  1158. for (page = page0; page <= page1; page += TARGET_PAGE_SIZE)
  1159. if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG))
  1160. update = 1;
  1161. /* draw line and change status */
  1162. if (update) {
  1163. uint8_t * d = &(ds_get_data(s->ds)[y * width * dst_bpp]);
  1164. /* draw graphics layer */
  1165. draw_line(d, src, width, palette);
  1166. /* draw haredware cursor */
  1167. if (update_hwc) {
  1168. draw_hwc_line(s, 1, hwc_palette, y - get_hwc_y(s, 1), d, width);
  1169. }
  1170. if (y_start < 0)
  1171. y_start = y;
  1172. if (page0 < page_min)
  1173. page_min = page0;
  1174. if (page1 > page_max)
  1175. page_max = page1;
  1176. } else {
  1177. if (y_start >= 0) {
  1178. /* flush to display */
  1179. dpy_update(s->ds, 0, y_start, width, y - y_start);
  1180. y_start = -1;
  1181. }
  1182. }
  1183. src += width * src_bpp;
  1184. offset += width * src_bpp;
  1185. }
  1186. /* complete flush to display */
  1187. if (y_start >= 0)
  1188. dpy_update(s->ds, 0, y_start, width, y - y_start);
  1189. /* clear dirty flags */
  1190. if (page_min != ~0l) {
  1191. cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
  1192. VGA_DIRTY_FLAG);
  1193. }
  1194. }
  1195. static void sm501_update_display(void *opaque)
  1196. {
  1197. SM501State * s = (SM501State *)opaque;
  1198. if (s->dc_crt_control & SM501_DC_CRT_CONTROL_ENABLE)
  1199. sm501_draw_crt(s);
  1200. }
  1201. void sm501_init(uint32_t base, uint32_t local_mem_bytes, qemu_irq irq,
  1202. CharDriverState *chr)
  1203. {
  1204. SM501State * s;
  1205. DeviceState *dev;
  1206. int sm501_system_config_index;
  1207. int sm501_disp_ctrl_index;
  1208. int sm501_2d_engine_index;
  1209. /* allocate management data region */
  1210. s = (SM501State *)qemu_mallocz(sizeof(SM501State));
  1211. s->base = base;
  1212. s->local_mem_size_index
  1213. = get_local_mem_size_index(local_mem_bytes);
  1214. SM501_DPRINTF("local mem size=%x. index=%d\n", get_local_mem_size(s),
  1215. s->local_mem_size_index);
  1216. s->system_control = 0x00100000;
  1217. s->misc_control = 0x00001000; /* assumes SH, active=low */
  1218. s->dc_panel_control = 0x00010000;
  1219. s->dc_crt_control = 0x00010000;
  1220. /* allocate local memory */
  1221. s->local_mem_offset = qemu_ram_alloc(NULL, "sm501.local", local_mem_bytes);
  1222. s->local_mem = qemu_get_ram_ptr(s->local_mem_offset);
  1223. cpu_register_physical_memory(base, local_mem_bytes, s->local_mem_offset);
  1224. /* map mmio */
  1225. sm501_system_config_index
  1226. = cpu_register_io_memory(sm501_system_config_readfn,
  1227. sm501_system_config_writefn, s,
  1228. DEVICE_NATIVE_ENDIAN);
  1229. cpu_register_physical_memory(base + MMIO_BASE_OFFSET,
  1230. 0x6c, sm501_system_config_index);
  1231. sm501_disp_ctrl_index = cpu_register_io_memory(sm501_disp_ctrl_readfn,
  1232. sm501_disp_ctrl_writefn, s,
  1233. DEVICE_NATIVE_ENDIAN);
  1234. cpu_register_physical_memory(base + MMIO_BASE_OFFSET + SM501_DC,
  1235. 0x1000, sm501_disp_ctrl_index);
  1236. sm501_2d_engine_index = cpu_register_io_memory(sm501_2d_engine_readfn,
  1237. sm501_2d_engine_writefn, s,
  1238. DEVICE_NATIVE_ENDIAN);
  1239. cpu_register_physical_memory(base + MMIO_BASE_OFFSET + SM501_2D_ENGINE,
  1240. 0x54, sm501_2d_engine_index);
  1241. /* bridge to usb host emulation module */
  1242. dev = qdev_create(NULL, "sysbus-ohci");
  1243. qdev_prop_set_uint32(dev, "num-ports", 2);
  1244. qdev_prop_set_taddr(dev, "dma-offset", base);
  1245. qdev_init_nofail(dev);
  1246. sysbus_mmio_map(sysbus_from_qdev(dev), 0,
  1247. base + MMIO_BASE_OFFSET + SM501_USB_HOST);
  1248. sysbus_connect_irq(sysbus_from_qdev(dev), 0, irq);
  1249. /* bridge to serial emulation module */
  1250. if (chr) {
  1251. #ifdef TARGET_WORDS_BIGENDIAN
  1252. serial_mm_init(base + MMIO_BASE_OFFSET + SM501_UART0, 2,
  1253. NULL, /* TODO : chain irq to IRL */
  1254. 115200, chr, 1, 1);
  1255. #else
  1256. serial_mm_init(base + MMIO_BASE_OFFSET + SM501_UART0, 2,
  1257. NULL, /* TODO : chain irq to IRL */
  1258. 115200, chr, 1, 0);
  1259. #endif
  1260. }
  1261. /* create qemu graphic console */
  1262. s->ds = graphic_console_init(sm501_update_display, NULL,
  1263. NULL, NULL, s);
  1264. }