2
0

sm501.c 41 KB

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