sm501.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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. MemoryRegion local_mem_region;
  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 uint64_t sm501_system_config_read(void *opaque, target_phys_addr_t addr,
  620. unsigned size)
  621. {
  622. SM501State * s = (SM501State *)opaque;
  623. uint32_t ret = 0;
  624. SM501_DPRINTF("sm501 system config regs : read addr=%x\n", (int)addr);
  625. switch(addr) {
  626. case SM501_SYSTEM_CONTROL:
  627. ret = s->system_control;
  628. break;
  629. case SM501_MISC_CONTROL:
  630. ret = s->misc_control;
  631. break;
  632. case SM501_GPIO31_0_CONTROL:
  633. ret = s->gpio_31_0_control;
  634. break;
  635. case SM501_GPIO63_32_CONTROL:
  636. ret = s->gpio_63_32_control;
  637. break;
  638. case SM501_DEVICEID:
  639. ret = 0x050100A0;
  640. break;
  641. case SM501_DRAM_CONTROL:
  642. ret = (s->dram_control & 0x07F107C0) | s->local_mem_size_index << 13;
  643. break;
  644. case SM501_IRQ_MASK:
  645. ret = s->irq_mask;
  646. break;
  647. case SM501_MISC_TIMING:
  648. /* TODO : simulate gate control */
  649. ret = s->misc_timing;
  650. break;
  651. case SM501_CURRENT_GATE:
  652. /* TODO : simulate gate control */
  653. ret = 0x00021807;
  654. break;
  655. case SM501_CURRENT_CLOCK:
  656. ret = 0x2A1A0A09;
  657. break;
  658. case SM501_POWER_MODE_CONTROL:
  659. ret = s->power_mode_control;
  660. break;
  661. default:
  662. printf("sm501 system config : not implemented register read."
  663. " addr=%x\n", (int)addr);
  664. abort();
  665. }
  666. return ret;
  667. }
  668. static void sm501_system_config_write(void *opaque, target_phys_addr_t addr,
  669. uint64_t value, unsigned size)
  670. {
  671. SM501State * s = (SM501State *)opaque;
  672. SM501_DPRINTF("sm501 system config regs : write addr=%x, val=%x\n",
  673. (uint32_t)addr, (uint32_t)value);
  674. switch(addr) {
  675. case SM501_SYSTEM_CONTROL:
  676. s->system_control = value & 0xE300B8F7;
  677. break;
  678. case SM501_MISC_CONTROL:
  679. s->misc_control = value & 0xFF7FFF20;
  680. break;
  681. case SM501_GPIO31_0_CONTROL:
  682. s->gpio_31_0_control = value;
  683. break;
  684. case SM501_GPIO63_32_CONTROL:
  685. s->gpio_63_32_control = value;
  686. break;
  687. case SM501_DRAM_CONTROL:
  688. s->local_mem_size_index = (value >> 13) & 0x7;
  689. /* rODO : check validity of size change */
  690. s->dram_control |= value & 0x7FFFFFC3;
  691. break;
  692. case SM501_IRQ_MASK:
  693. s->irq_mask = value;
  694. break;
  695. case SM501_MISC_TIMING:
  696. s->misc_timing = value & 0xF31F1FFF;
  697. break;
  698. case SM501_POWER_MODE_0_GATE:
  699. case SM501_POWER_MODE_1_GATE:
  700. case SM501_POWER_MODE_0_CLOCK:
  701. case SM501_POWER_MODE_1_CLOCK:
  702. /* TODO : simulate gate & clock control */
  703. break;
  704. case SM501_POWER_MODE_CONTROL:
  705. s->power_mode_control = value & 0x00000003;
  706. break;
  707. default:
  708. printf("sm501 system config : not implemented register write."
  709. " addr=%x, val=%x\n", (int)addr, (uint32_t)value);
  710. abort();
  711. }
  712. }
  713. static const MemoryRegionOps sm501_system_config_ops = {
  714. .read = sm501_system_config_read,
  715. .write = sm501_system_config_write,
  716. .valid = {
  717. .min_access_size = 4,
  718. .max_access_size = 4,
  719. },
  720. .endianness = DEVICE_NATIVE_ENDIAN,
  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 uint64_t sm501_disp_ctrl_read(void *opaque, target_phys_addr_t addr,
  743. unsigned size)
  744. {
  745. SM501State * s = (SM501State *)opaque;
  746. uint32_t ret = 0;
  747. SM501_DPRINTF("sm501 disp ctrl regs : read addr=%x\n", (int)addr);
  748. switch(addr) {
  749. case SM501_DC_PANEL_CONTROL:
  750. ret = s->dc_panel_control;
  751. break;
  752. case SM501_DC_PANEL_PANNING_CONTROL:
  753. ret = s->dc_panel_panning_control;
  754. break;
  755. case SM501_DC_PANEL_FB_ADDR:
  756. ret = s->dc_panel_fb_addr;
  757. break;
  758. case SM501_DC_PANEL_FB_OFFSET:
  759. ret = s->dc_panel_fb_offset;
  760. break;
  761. case SM501_DC_PANEL_FB_WIDTH:
  762. ret = s->dc_panel_fb_width;
  763. break;
  764. case SM501_DC_PANEL_FB_HEIGHT:
  765. ret = s->dc_panel_fb_height;
  766. break;
  767. case SM501_DC_PANEL_TL_LOC:
  768. ret = s->dc_panel_tl_location;
  769. break;
  770. case SM501_DC_PANEL_BR_LOC:
  771. ret = s->dc_panel_br_location;
  772. break;
  773. case SM501_DC_PANEL_H_TOT:
  774. ret = s->dc_panel_h_total;
  775. break;
  776. case SM501_DC_PANEL_H_SYNC:
  777. ret = s->dc_panel_h_sync;
  778. break;
  779. case SM501_DC_PANEL_V_TOT:
  780. ret = s->dc_panel_v_total;
  781. break;
  782. case SM501_DC_PANEL_V_SYNC:
  783. ret = s->dc_panel_v_sync;
  784. break;
  785. case SM501_DC_CRT_CONTROL:
  786. ret = s->dc_crt_control;
  787. break;
  788. case SM501_DC_CRT_FB_ADDR:
  789. ret = s->dc_crt_fb_addr;
  790. break;
  791. case SM501_DC_CRT_FB_OFFSET:
  792. ret = s->dc_crt_fb_offset;
  793. break;
  794. case SM501_DC_CRT_H_TOT:
  795. ret = s->dc_crt_h_total;
  796. break;
  797. case SM501_DC_CRT_H_SYNC:
  798. ret = s->dc_crt_h_sync;
  799. break;
  800. case SM501_DC_CRT_V_TOT:
  801. ret = s->dc_crt_v_total;
  802. break;
  803. case SM501_DC_CRT_V_SYNC:
  804. ret = s->dc_crt_v_sync;
  805. break;
  806. case SM501_DC_CRT_HWC_ADDR:
  807. ret = s->dc_crt_hwc_addr;
  808. break;
  809. case SM501_DC_CRT_HWC_LOC:
  810. ret = s->dc_crt_hwc_location;
  811. break;
  812. case SM501_DC_CRT_HWC_COLOR_1_2:
  813. ret = s->dc_crt_hwc_color_1_2;
  814. break;
  815. case SM501_DC_CRT_HWC_COLOR_3:
  816. ret = s->dc_crt_hwc_color_3;
  817. break;
  818. case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400*3 - 4:
  819. ret = sm501_palette_read(opaque, addr - SM501_DC_PANEL_PALETTE);
  820. break;
  821. default:
  822. printf("sm501 disp ctrl : not implemented register read."
  823. " addr=%x\n", (int)addr);
  824. abort();
  825. }
  826. return ret;
  827. }
  828. static void sm501_disp_ctrl_write(void *opaque, target_phys_addr_t addr,
  829. uint64_t value, unsigned size)
  830. {
  831. SM501State * s = (SM501State *)opaque;
  832. SM501_DPRINTF("sm501 disp ctrl regs : write addr=%x, val=%x\n",
  833. (unsigned)addr, (unsigned)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, (unsigned)value);
  922. abort();
  923. }
  924. }
  925. static const MemoryRegionOps sm501_disp_ctrl_ops = {
  926. .read = sm501_disp_ctrl_read,
  927. .write = sm501_disp_ctrl_write,
  928. .valid = {
  929. .min_access_size = 4,
  930. .max_access_size = 4,
  931. },
  932. .endianness = DEVICE_NATIVE_ENDIAN,
  933. };
  934. static uint64_t sm501_2d_engine_read(void *opaque, target_phys_addr_t addr,
  935. unsigned size)
  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, target_phys_addr_t addr,
  952. uint64_t value, unsigned size)
  953. {
  954. SM501State * s = (SM501State *)opaque;
  955. SM501_DPRINTF("sm501 2d engine regs : write addr=%x, val=%x\n",
  956. (unsigned)addr, (unsigned)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, (unsigned)value);
  1002. abort();
  1003. }
  1004. }
  1005. static const MemoryRegionOps sm501_2d_engine_ops = {
  1006. .read = sm501_2d_engine_read,
  1007. .write = sm501_2d_engine_write,
  1008. .valid = {
  1009. .min_access_size = 4,
  1010. .max_access_size = 4,
  1011. },
  1012. .endianness = DEVICE_NATIVE_ENDIAN,
  1013. };
  1014. /* draw line functions for all console modes */
  1015. #include "pixel_ops.h"
  1016. typedef void draw_line_func(uint8_t *d, const uint8_t *s,
  1017. int width, const uint32_t *pal);
  1018. typedef void draw_hwc_line_func(SM501State * s, int crt, uint8_t * palette,
  1019. int c_y, uint8_t *d, int width);
  1020. #define DEPTH 8
  1021. #include "sm501_template.h"
  1022. #define DEPTH 15
  1023. #include "sm501_template.h"
  1024. #define BGR_FORMAT
  1025. #define DEPTH 15
  1026. #include "sm501_template.h"
  1027. #define DEPTH 16
  1028. #include "sm501_template.h"
  1029. #define BGR_FORMAT
  1030. #define DEPTH 16
  1031. #include "sm501_template.h"
  1032. #define DEPTH 32
  1033. #include "sm501_template.h"
  1034. #define BGR_FORMAT
  1035. #define DEPTH 32
  1036. #include "sm501_template.h"
  1037. static draw_line_func * draw_line8_funcs[] = {
  1038. draw_line8_8,
  1039. draw_line8_15,
  1040. draw_line8_16,
  1041. draw_line8_32,
  1042. draw_line8_32bgr,
  1043. draw_line8_15bgr,
  1044. draw_line8_16bgr,
  1045. };
  1046. static draw_line_func * draw_line16_funcs[] = {
  1047. draw_line16_8,
  1048. draw_line16_15,
  1049. draw_line16_16,
  1050. draw_line16_32,
  1051. draw_line16_32bgr,
  1052. draw_line16_15bgr,
  1053. draw_line16_16bgr,
  1054. };
  1055. static draw_line_func * draw_line32_funcs[] = {
  1056. draw_line32_8,
  1057. draw_line32_15,
  1058. draw_line32_16,
  1059. draw_line32_32,
  1060. draw_line32_32bgr,
  1061. draw_line32_15bgr,
  1062. draw_line32_16bgr,
  1063. };
  1064. static draw_hwc_line_func * draw_hwc_line_funcs[] = {
  1065. draw_hwc_line_8,
  1066. draw_hwc_line_15,
  1067. draw_hwc_line_16,
  1068. draw_hwc_line_32,
  1069. draw_hwc_line_32bgr,
  1070. draw_hwc_line_15bgr,
  1071. draw_hwc_line_16bgr,
  1072. };
  1073. static inline int get_depth_index(DisplayState *s)
  1074. {
  1075. switch(ds_get_bits_per_pixel(s)) {
  1076. default:
  1077. case 8:
  1078. return 0;
  1079. case 15:
  1080. return 1;
  1081. case 16:
  1082. return 2;
  1083. case 32:
  1084. if (is_surface_bgr(s->surface))
  1085. return 4;
  1086. else
  1087. return 3;
  1088. }
  1089. }
  1090. static void sm501_draw_crt(SM501State * s)
  1091. {
  1092. int y;
  1093. int width = (s->dc_crt_h_total & 0x00000FFF) + 1;
  1094. int height = (s->dc_crt_v_total & 0x00000FFF) + 1;
  1095. uint8_t * src = s->local_mem;
  1096. int src_bpp = 0;
  1097. int dst_bpp = ds_get_bytes_per_pixel(s->ds) + (ds_get_bits_per_pixel(s->ds) % 8 ? 1 : 0);
  1098. uint32_t * palette = (uint32_t *)&s->dc_palette[SM501_DC_CRT_PALETTE
  1099. - SM501_DC_PANEL_PALETTE];
  1100. uint8_t hwc_palette[3 * 3];
  1101. int ds_depth_index = get_depth_index(s->ds);
  1102. draw_line_func * draw_line = NULL;
  1103. draw_hwc_line_func * draw_hwc_line = NULL;
  1104. int full_update = 0;
  1105. int y_start = -1;
  1106. ram_addr_t page_min = ~0l;
  1107. ram_addr_t page_max = 0l;
  1108. ram_addr_t offset = 0;
  1109. /* choose draw_line function */
  1110. switch (s->dc_crt_control & 3) {
  1111. case SM501_DC_CRT_CONTROL_8BPP:
  1112. src_bpp = 1;
  1113. draw_line = draw_line8_funcs[ds_depth_index];
  1114. break;
  1115. case SM501_DC_CRT_CONTROL_16BPP:
  1116. src_bpp = 2;
  1117. draw_line = draw_line16_funcs[ds_depth_index];
  1118. break;
  1119. case SM501_DC_CRT_CONTROL_32BPP:
  1120. src_bpp = 4;
  1121. draw_line = draw_line32_funcs[ds_depth_index];
  1122. break;
  1123. default:
  1124. printf("sm501 draw crt : invalid DC_CRT_CONTROL=%x.\n",
  1125. s->dc_crt_control);
  1126. abort();
  1127. break;
  1128. }
  1129. /* set up to draw hardware cursor */
  1130. if (is_hwc_enabled(s, 1)) {
  1131. int i;
  1132. /* get cursor palette */
  1133. for (i = 0; i < 3; i++) {
  1134. uint16_t rgb565 = get_hwc_color(s, 1, i + 1);
  1135. hwc_palette[i * 3 + 0] = (rgb565 & 0xf800) >> 8; /* red */
  1136. hwc_palette[i * 3 + 1] = (rgb565 & 0x07e0) >> 3; /* green */
  1137. hwc_palette[i * 3 + 2] = (rgb565 & 0x001f) << 3; /* blue */
  1138. }
  1139. /* choose cursor draw line function */
  1140. draw_hwc_line = draw_hwc_line_funcs[ds_depth_index];
  1141. }
  1142. /* adjust console size */
  1143. if (s->last_width != width || s->last_height != height) {
  1144. qemu_console_resize(s->ds, width, height);
  1145. s->last_width = width;
  1146. s->last_height = height;
  1147. full_update = 1;
  1148. }
  1149. /* draw each line according to conditions */
  1150. for (y = 0; y < height; y++) {
  1151. int update_hwc = draw_hwc_line ? within_hwc_y_range(s, y, 1) : 0;
  1152. int update = full_update || update_hwc;
  1153. ram_addr_t page0 = offset & TARGET_PAGE_MASK;
  1154. ram_addr_t page1 = (offset + width * src_bpp - 1) & TARGET_PAGE_MASK;
  1155. ram_addr_t page;
  1156. /* check dirty flags for each line */
  1157. for (page = page0; page <= page1; page += TARGET_PAGE_SIZE)
  1158. if (memory_region_get_dirty(&s->local_mem_region, page,
  1159. DIRTY_MEMORY_VGA))
  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. memory_region_reset_dirty(&s->local_mem_region,
  1192. page_min, page_max + TARGET_PAGE_SIZE,
  1193. DIRTY_MEMORY_VGA);
  1194. }
  1195. }
  1196. static void sm501_update_display(void *opaque)
  1197. {
  1198. SM501State * s = (SM501State *)opaque;
  1199. if (s->dc_crt_control & SM501_DC_CRT_CONTROL_ENABLE)
  1200. sm501_draw_crt(s);
  1201. }
  1202. void sm501_init(MemoryRegion *address_space_mem, uint32_t base,
  1203. uint32_t local_mem_bytes, qemu_irq irq, CharDriverState *chr)
  1204. {
  1205. SM501State * s;
  1206. DeviceState *dev;
  1207. MemoryRegion *sm501_system_config = g_new(MemoryRegion, 1);
  1208. MemoryRegion *sm501_disp_ctrl = g_new(MemoryRegion, 1);
  1209. MemoryRegion *sm501_2d_engine = g_new(MemoryRegion, 1);
  1210. /* allocate management data region */
  1211. s = (SM501State *)g_malloc0(sizeof(SM501State));
  1212. s->base = base;
  1213. s->local_mem_size_index
  1214. = get_local_mem_size_index(local_mem_bytes);
  1215. SM501_DPRINTF("local mem size=%x. index=%d\n", get_local_mem_size(s),
  1216. s->local_mem_size_index);
  1217. s->system_control = 0x00100000;
  1218. s->misc_control = 0x00001000; /* assumes SH, active=low */
  1219. s->dc_panel_control = 0x00010000;
  1220. s->dc_crt_control = 0x00010000;
  1221. /* allocate local memory */
  1222. memory_region_init_ram(&s->local_mem_region, NULL, "sm501.local",
  1223. local_mem_bytes);
  1224. s->local_mem = memory_region_get_ram_ptr(&s->local_mem_region);
  1225. memory_region_add_subregion(address_space_mem, base, &s->local_mem_region);
  1226. /* map mmio */
  1227. memory_region_init_io(sm501_system_config, &sm501_system_config_ops, s,
  1228. "sm501-system-config", 0x6c);
  1229. memory_region_add_subregion(address_space_mem, base + MMIO_BASE_OFFSET,
  1230. sm501_system_config);
  1231. memory_region_init_io(sm501_disp_ctrl, &sm501_disp_ctrl_ops, s,
  1232. "sm501-disp-ctrl", 0x1000);
  1233. memory_region_add_subregion(address_space_mem,
  1234. base + MMIO_BASE_OFFSET + SM501_DC,
  1235. sm501_disp_ctrl);
  1236. memory_region_init_io(sm501_2d_engine, &sm501_2d_engine_ops, s,
  1237. "sm501-2d-engine", 0x54);
  1238. memory_region_add_subregion(address_space_mem,
  1239. base + MMIO_BASE_OFFSET + SM501_2D_ENGINE,
  1240. sm501_2d_engine);
  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. serial_mm_init(address_space_mem,
  1252. base + MMIO_BASE_OFFSET + SM501_UART0, 2,
  1253. NULL, /* TODO : chain irq to IRL */
  1254. 115200, chr, DEVICE_NATIVE_ENDIAN);
  1255. }
  1256. /* create qemu graphic console */
  1257. s->ds = graphic_console_init(sm501_update_display, NULL,
  1258. NULL, NULL, s);
  1259. }