2
0

sm501.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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/hw.h"
  26. #include "hw/char/serial.h"
  27. #include "ui/console.h"
  28. #include "hw/devices.h"
  29. #include "hw/sysbus.h"
  30. #include "qemu/range.h"
  31. #include "ui/pixel_ops.h"
  32. #include "exec/address-spaces.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. QemuConsole *con;
  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(DisplaySurface *surface)
  1074. {
  1075. switch (surface_bits_per_pixel(surface)) {
  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(surface)) {
  1085. return 4;
  1086. } else {
  1087. return 3;
  1088. }
  1089. }
  1090. }
  1091. static void sm501_draw_crt(SM501State * s)
  1092. {
  1093. DisplaySurface *surface = qemu_console_surface(s->con);
  1094. int y;
  1095. int width = (s->dc_crt_h_total & 0x00000FFF) + 1;
  1096. int height = (s->dc_crt_v_total & 0x00000FFF) + 1;
  1097. uint8_t * src = s->local_mem;
  1098. int src_bpp = 0;
  1099. int dst_bpp = surface_bytes_per_pixel(surface);
  1100. uint32_t * palette = (uint32_t *)&s->dc_palette[SM501_DC_CRT_PALETTE
  1101. - SM501_DC_PANEL_PALETTE];
  1102. uint8_t hwc_palette[3 * 3];
  1103. int ds_depth_index = get_depth_index(surface);
  1104. draw_line_func * draw_line = NULL;
  1105. draw_hwc_line_func * draw_hwc_line = NULL;
  1106. int full_update = 0;
  1107. int y_start = -1;
  1108. ram_addr_t page_min = ~0l;
  1109. ram_addr_t page_max = 0l;
  1110. ram_addr_t offset = 0;
  1111. /* choose draw_line function */
  1112. switch (s->dc_crt_control & 3) {
  1113. case SM501_DC_CRT_CONTROL_8BPP:
  1114. src_bpp = 1;
  1115. draw_line = draw_line8_funcs[ds_depth_index];
  1116. break;
  1117. case SM501_DC_CRT_CONTROL_16BPP:
  1118. src_bpp = 2;
  1119. draw_line = draw_line16_funcs[ds_depth_index];
  1120. break;
  1121. case SM501_DC_CRT_CONTROL_32BPP:
  1122. src_bpp = 4;
  1123. draw_line = draw_line32_funcs[ds_depth_index];
  1124. break;
  1125. default:
  1126. printf("sm501 draw crt : invalid DC_CRT_CONTROL=%x.\n",
  1127. s->dc_crt_control);
  1128. abort();
  1129. break;
  1130. }
  1131. /* set up to draw hardware cursor */
  1132. if (is_hwc_enabled(s, 1)) {
  1133. int i;
  1134. /* get cursor palette */
  1135. for (i = 0; i < 3; i++) {
  1136. uint16_t rgb565 = get_hwc_color(s, 1, i + 1);
  1137. hwc_palette[i * 3 + 0] = (rgb565 & 0xf800) >> 8; /* red */
  1138. hwc_palette[i * 3 + 1] = (rgb565 & 0x07e0) >> 3; /* green */
  1139. hwc_palette[i * 3 + 2] = (rgb565 & 0x001f) << 3; /* blue */
  1140. }
  1141. /* choose cursor draw line function */
  1142. draw_hwc_line = draw_hwc_line_funcs[ds_depth_index];
  1143. }
  1144. /* adjust console size */
  1145. if (s->last_width != width || s->last_height != height) {
  1146. qemu_console_resize(s->con, width, height);
  1147. surface = qemu_console_surface(s->con);
  1148. s->last_width = width;
  1149. s->last_height = height;
  1150. full_update = 1;
  1151. }
  1152. /* draw each line according to conditions */
  1153. for (y = 0; y < height; y++) {
  1154. int update_hwc = draw_hwc_line ? within_hwc_y_range(s, y, 1) : 0;
  1155. int update = full_update || update_hwc;
  1156. ram_addr_t page0 = offset;
  1157. ram_addr_t page1 = offset + width * src_bpp - 1;
  1158. /* check dirty flags for each line */
  1159. update = memory_region_get_dirty(&s->local_mem_region, page0,
  1160. page1 - page0, DIRTY_MEMORY_VGA);
  1161. /* draw line and change status */
  1162. if (update) {
  1163. uint8_t *d = surface_data(surface);
  1164. d += y * width * dst_bpp;
  1165. /* draw graphics layer */
  1166. draw_line(d, src, width, palette);
  1167. /* draw haredware cursor */
  1168. if (update_hwc) {
  1169. draw_hwc_line(s, 1, hwc_palette, y - get_hwc_y(s, 1), d, width);
  1170. }
  1171. if (y_start < 0)
  1172. y_start = y;
  1173. if (page0 < page_min)
  1174. page_min = page0;
  1175. if (page1 > page_max)
  1176. page_max = page1;
  1177. } else {
  1178. if (y_start >= 0) {
  1179. /* flush to display */
  1180. dpy_gfx_update(s->con, 0, y_start, width, y - y_start);
  1181. y_start = -1;
  1182. }
  1183. }
  1184. src += width * src_bpp;
  1185. offset += width * src_bpp;
  1186. }
  1187. /* complete flush to display */
  1188. if (y_start >= 0)
  1189. dpy_gfx_update(s->con, 0, y_start, width, y - y_start);
  1190. /* clear dirty flags */
  1191. if (page_min != ~0l) {
  1192. memory_region_reset_dirty(&s->local_mem_region,
  1193. page_min, page_max + TARGET_PAGE_SIZE,
  1194. DIRTY_MEMORY_VGA);
  1195. }
  1196. }
  1197. static void sm501_update_display(void *opaque)
  1198. {
  1199. SM501State * s = (SM501State *)opaque;
  1200. if (s->dc_crt_control & SM501_DC_CRT_CONTROL_ENABLE)
  1201. sm501_draw_crt(s);
  1202. }
  1203. static const GraphicHwOps sm501_ops = {
  1204. .gfx_update = sm501_update_display,
  1205. };
  1206. void sm501_init(MemoryRegion *address_space_mem, uint32_t base,
  1207. uint32_t local_mem_bytes, qemu_irq irq, CharDriverState *chr)
  1208. {
  1209. SM501State * s;
  1210. DeviceState *dev;
  1211. MemoryRegion *sm501_system_config = g_new(MemoryRegion, 1);
  1212. MemoryRegion *sm501_disp_ctrl = g_new(MemoryRegion, 1);
  1213. MemoryRegion *sm501_2d_engine = g_new(MemoryRegion, 1);
  1214. /* allocate management data region */
  1215. s = (SM501State *)g_malloc0(sizeof(SM501State));
  1216. s->base = base;
  1217. s->local_mem_size_index
  1218. = get_local_mem_size_index(local_mem_bytes);
  1219. SM501_DPRINTF("local mem size=%x. index=%d\n", get_local_mem_size(s),
  1220. s->local_mem_size_index);
  1221. s->system_control = 0x00100000;
  1222. s->misc_control = 0x00001000; /* assumes SH, active=low */
  1223. s->dc_panel_control = 0x00010000;
  1224. s->dc_crt_control = 0x00010000;
  1225. /* allocate local memory */
  1226. memory_region_init_ram(&s->local_mem_region, NULL, "sm501.local",
  1227. local_mem_bytes);
  1228. vmstate_register_ram_global(&s->local_mem_region);
  1229. s->local_mem = memory_region_get_ram_ptr(&s->local_mem_region);
  1230. memory_region_add_subregion(address_space_mem, base, &s->local_mem_region);
  1231. /* map mmio */
  1232. memory_region_init_io(sm501_system_config, NULL, &sm501_system_config_ops, s,
  1233. "sm501-system-config", 0x6c);
  1234. memory_region_add_subregion(address_space_mem, base + MMIO_BASE_OFFSET,
  1235. sm501_system_config);
  1236. memory_region_init_io(sm501_disp_ctrl, NULL, &sm501_disp_ctrl_ops, s,
  1237. "sm501-disp-ctrl", 0x1000);
  1238. memory_region_add_subregion(address_space_mem,
  1239. base + MMIO_BASE_OFFSET + SM501_DC,
  1240. sm501_disp_ctrl);
  1241. memory_region_init_io(sm501_2d_engine, NULL, &sm501_2d_engine_ops, s,
  1242. "sm501-2d-engine", 0x54);
  1243. memory_region_add_subregion(address_space_mem,
  1244. base + MMIO_BASE_OFFSET + SM501_2D_ENGINE,
  1245. sm501_2d_engine);
  1246. /* bridge to usb host emulation module */
  1247. dev = qdev_create(NULL, "sysbus-ohci");
  1248. qdev_prop_set_uint32(dev, "num-ports", 2);
  1249. qdev_prop_set_uint64(dev, "dma-offset", base);
  1250. qdev_init_nofail(dev);
  1251. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0,
  1252. base + MMIO_BASE_OFFSET + SM501_USB_HOST);
  1253. sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, irq);
  1254. /* bridge to serial emulation module */
  1255. if (chr) {
  1256. serial_mm_init(address_space_mem,
  1257. base + MMIO_BASE_OFFSET + SM501_UART0, 2,
  1258. NULL, /* TODO : chain irq to IRL */
  1259. 115200, chr, DEVICE_NATIVE_ENDIAN);
  1260. }
  1261. /* create qemu graphic console */
  1262. s->con = graphic_console_init(DEVICE(dev), 0, &sm501_ops, s);
  1263. }