sm501.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  1. /*
  2. * QEMU SM501 Device
  3. *
  4. * Copyright (c) 2008 Shin-ichiro KAWASAKI
  5. * Copyright (c) 2016-2020 BALATON Zoltan
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/units.h"
  27. #include "qapi/error.h"
  28. #include "qemu/log.h"
  29. #include "qemu/module.h"
  30. #include "hw/usb/hcd-ohci.h"
  31. #include "hw/char/serial.h"
  32. #include "ui/console.h"
  33. #include "hw/sysbus.h"
  34. #include "migration/vmstate.h"
  35. #include "hw/pci/pci_device.h"
  36. #include "hw/qdev-properties.h"
  37. #include "hw/i2c/i2c.h"
  38. #include "hw/display/i2c-ddc.h"
  39. #include "qemu/range.h"
  40. #include "ui/pixel_ops.h"
  41. #include "qemu/bswap.h"
  42. #include "trace.h"
  43. #include "qom/object.h"
  44. #define MMIO_BASE_OFFSET 0x3e00000
  45. #define MMIO_SIZE 0x200000
  46. #define DC_PALETTE_ENTRIES (0x400 * 3)
  47. /* SM501 register definitions taken from "linux/include/linux/sm501-regs.h" */
  48. /* System Configuration area */
  49. /* System config base */
  50. #define SM501_SYS_CONFIG 0x000000
  51. /* config 1 */
  52. #define SM501_SYSTEM_CONTROL 0x000000
  53. #define SM501_SYSCTRL_PANEL_TRISTATE (1 << 0)
  54. #define SM501_SYSCTRL_MEM_TRISTATE (1 << 1)
  55. #define SM501_SYSCTRL_CRT_TRISTATE (1 << 2)
  56. #define SM501_SYSCTRL_PCI_SLAVE_BURST_MASK (3 << 4)
  57. #define SM501_SYSCTRL_PCI_SLAVE_BURST_1 (0 << 4)
  58. #define SM501_SYSCTRL_PCI_SLAVE_BURST_2 (1 << 4)
  59. #define SM501_SYSCTRL_PCI_SLAVE_BURST_4 (2 << 4)
  60. #define SM501_SYSCTRL_PCI_SLAVE_BURST_8 (3 << 4)
  61. #define SM501_SYSCTRL_PCI_CLOCK_RUN_EN (1 << 6)
  62. #define SM501_SYSCTRL_PCI_RETRY_DISABLE (1 << 7)
  63. #define SM501_SYSCTRL_PCI_SUBSYS_LOCK (1 << 11)
  64. #define SM501_SYSCTRL_PCI_BURST_READ_EN (1 << 15)
  65. /* miscellaneous control */
  66. #define SM501_MISC_CONTROL 0x000004
  67. #define SM501_MISC_BUS_SH 0x0
  68. #define SM501_MISC_BUS_PCI 0x1
  69. #define SM501_MISC_BUS_XSCALE 0x2
  70. #define SM501_MISC_BUS_NEC 0x6
  71. #define SM501_MISC_BUS_MASK 0x7
  72. #define SM501_MISC_VR_62MB (1 << 3)
  73. #define SM501_MISC_CDR_RESET (1 << 7)
  74. #define SM501_MISC_USB_LB (1 << 8)
  75. #define SM501_MISC_USB_SLAVE (1 << 9)
  76. #define SM501_MISC_BL_1 (1 << 10)
  77. #define SM501_MISC_MC (1 << 11)
  78. #define SM501_MISC_DAC_POWER (1 << 12)
  79. #define SM501_MISC_IRQ_INVERT (1 << 16)
  80. #define SM501_MISC_SH (1 << 17)
  81. #define SM501_MISC_HOLD_EMPTY (0 << 18)
  82. #define SM501_MISC_HOLD_8 (1 << 18)
  83. #define SM501_MISC_HOLD_16 (2 << 18)
  84. #define SM501_MISC_HOLD_24 (3 << 18)
  85. #define SM501_MISC_HOLD_32 (4 << 18)
  86. #define SM501_MISC_HOLD_MASK (7 << 18)
  87. #define SM501_MISC_FREQ_12 (1 << 24)
  88. #define SM501_MISC_PNL_24BIT (1 << 25)
  89. #define SM501_MISC_8051_LE (1 << 26)
  90. #define SM501_GPIO31_0_CONTROL 0x000008
  91. #define SM501_GPIO63_32_CONTROL 0x00000C
  92. #define SM501_DRAM_CONTROL 0x000010
  93. /* command list */
  94. #define SM501_ARBTRTN_CONTROL 0x000014
  95. /* command list */
  96. #define SM501_COMMAND_LIST_STATUS 0x000024
  97. /* interrupt debug */
  98. #define SM501_RAW_IRQ_STATUS 0x000028
  99. #define SM501_RAW_IRQ_CLEAR 0x000028
  100. #define SM501_IRQ_STATUS 0x00002C
  101. #define SM501_IRQ_MASK 0x000030
  102. #define SM501_DEBUG_CONTROL 0x000034
  103. /* power management */
  104. #define SM501_POWERMODE_P2X_SRC (1 << 29)
  105. #define SM501_POWERMODE_V2X_SRC (1 << 20)
  106. #define SM501_POWERMODE_M_SRC (1 << 12)
  107. #define SM501_POWERMODE_M1_SRC (1 << 4)
  108. #define SM501_CURRENT_GATE 0x000038
  109. #define SM501_CURRENT_CLOCK 0x00003C
  110. #define SM501_POWER_MODE_0_GATE 0x000040
  111. #define SM501_POWER_MODE_0_CLOCK 0x000044
  112. #define SM501_POWER_MODE_1_GATE 0x000048
  113. #define SM501_POWER_MODE_1_CLOCK 0x00004C
  114. #define SM501_SLEEP_MODE_GATE 0x000050
  115. #define SM501_POWER_MODE_CONTROL 0x000054
  116. /* power gates for units within the 501 */
  117. #define SM501_GATE_HOST 0
  118. #define SM501_GATE_MEMORY 1
  119. #define SM501_GATE_DISPLAY 2
  120. #define SM501_GATE_2D_ENGINE 3
  121. #define SM501_GATE_CSC 4
  122. #define SM501_GATE_ZVPORT 5
  123. #define SM501_GATE_GPIO 6
  124. #define SM501_GATE_UART0 7
  125. #define SM501_GATE_UART1 8
  126. #define SM501_GATE_SSP 10
  127. #define SM501_GATE_USB_HOST 11
  128. #define SM501_GATE_USB_GADGET 12
  129. #define SM501_GATE_UCONTROLLER 17
  130. #define SM501_GATE_AC97 18
  131. /* panel clock */
  132. #define SM501_CLOCK_P2XCLK 24
  133. /* crt clock */
  134. #define SM501_CLOCK_V2XCLK 16
  135. /* main clock */
  136. #define SM501_CLOCK_MCLK 8
  137. /* SDRAM controller clock */
  138. #define SM501_CLOCK_M1XCLK 0
  139. /* config 2 */
  140. #define SM501_PCI_MASTER_BASE 0x000058
  141. #define SM501_ENDIAN_CONTROL 0x00005C
  142. #define SM501_DEVICEID 0x000060
  143. /* 0x050100A0 */
  144. #define SM501_DEVICEID_SM501 0x05010000
  145. #define SM501_DEVICEID_IDMASK 0xffff0000
  146. #define SM501_DEVICEID_REVMASK 0x000000ff
  147. #define SM501_PLLCLOCK_COUNT 0x000064
  148. #define SM501_MISC_TIMING 0x000068
  149. #define SM501_CURRENT_SDRAM_CLOCK 0x00006C
  150. #define SM501_PROGRAMMABLE_PLL_CONTROL 0x000074
  151. /* GPIO base */
  152. #define SM501_GPIO 0x010000
  153. #define SM501_GPIO_DATA_LOW 0x00
  154. #define SM501_GPIO_DATA_HIGH 0x04
  155. #define SM501_GPIO_DDR_LOW 0x08
  156. #define SM501_GPIO_DDR_HIGH 0x0C
  157. #define SM501_GPIO_IRQ_SETUP 0x10
  158. #define SM501_GPIO_IRQ_STATUS 0x14
  159. #define SM501_GPIO_IRQ_RESET 0x14
  160. /* I2C controller base */
  161. #define SM501_I2C 0x010040
  162. #define SM501_I2C_BYTE_COUNT 0x00
  163. #define SM501_I2C_CONTROL 0x01
  164. #define SM501_I2C_STATUS 0x02
  165. #define SM501_I2C_RESET 0x02
  166. #define SM501_I2C_SLAVE_ADDRESS 0x03
  167. #define SM501_I2C_DATA 0x04
  168. #define SM501_I2C_CONTROL_START (1 << 2)
  169. #define SM501_I2C_CONTROL_ENABLE (1 << 0)
  170. #define SM501_I2C_STATUS_COMPLETE (1 << 3)
  171. #define SM501_I2C_STATUS_ERROR (1 << 2)
  172. #define SM501_I2C_RESET_ERROR (1 << 2)
  173. /* SSP base */
  174. #define SM501_SSP 0x020000
  175. /* Uart 0 base */
  176. #define SM501_UART0 0x030000
  177. /* Uart 1 base */
  178. #define SM501_UART1 0x030020
  179. /* USB host port base */
  180. #define SM501_USB_HOST 0x040000
  181. /* USB slave/gadget base */
  182. #define SM501_USB_GADGET 0x060000
  183. /* USB slave/gadget data port base */
  184. #define SM501_USB_GADGET_DATA 0x070000
  185. /* Display controller/video engine base */
  186. #define SM501_DC 0x080000
  187. /* common defines for the SM501 address registers */
  188. #define SM501_ADDR_FLIP (1 << 31)
  189. #define SM501_ADDR_EXT (1 << 27)
  190. #define SM501_ADDR_CS1 (1 << 26)
  191. #define SM501_ADDR_MASK (0x3f << 26)
  192. #define SM501_FIFO_MASK (0x3 << 16)
  193. #define SM501_FIFO_1 (0x0 << 16)
  194. #define SM501_FIFO_3 (0x1 << 16)
  195. #define SM501_FIFO_7 (0x2 << 16)
  196. #define SM501_FIFO_11 (0x3 << 16)
  197. /* common registers for panel and the crt */
  198. #define SM501_OFF_DC_H_TOT 0x000
  199. #define SM501_OFF_DC_V_TOT 0x008
  200. #define SM501_OFF_DC_H_SYNC 0x004
  201. #define SM501_OFF_DC_V_SYNC 0x00C
  202. #define SM501_DC_PANEL_CONTROL 0x000
  203. #define SM501_DC_PANEL_CONTROL_FPEN (1 << 27)
  204. #define SM501_DC_PANEL_CONTROL_BIAS (1 << 26)
  205. #define SM501_DC_PANEL_CONTROL_DATA (1 << 25)
  206. #define SM501_DC_PANEL_CONTROL_VDD (1 << 24)
  207. #define SM501_DC_PANEL_CONTROL_DP (1 << 23)
  208. #define SM501_DC_PANEL_CONTROL_TFT_888 (0 << 21)
  209. #define SM501_DC_PANEL_CONTROL_TFT_333 (1 << 21)
  210. #define SM501_DC_PANEL_CONTROL_TFT_444 (2 << 21)
  211. #define SM501_DC_PANEL_CONTROL_DE (1 << 20)
  212. #define SM501_DC_PANEL_CONTROL_LCD_TFT (0 << 18)
  213. #define SM501_DC_PANEL_CONTROL_LCD_STN8 (1 << 18)
  214. #define SM501_DC_PANEL_CONTROL_LCD_STN12 (2 << 18)
  215. #define SM501_DC_PANEL_CONTROL_CP (1 << 14)
  216. #define SM501_DC_PANEL_CONTROL_VSP (1 << 13)
  217. #define SM501_DC_PANEL_CONTROL_HSP (1 << 12)
  218. #define SM501_DC_PANEL_CONTROL_CK (1 << 9)
  219. #define SM501_DC_PANEL_CONTROL_TE (1 << 8)
  220. #define SM501_DC_PANEL_CONTROL_VPD (1 << 7)
  221. #define SM501_DC_PANEL_CONTROL_VP (1 << 6)
  222. #define SM501_DC_PANEL_CONTROL_HPD (1 << 5)
  223. #define SM501_DC_PANEL_CONTROL_HP (1 << 4)
  224. #define SM501_DC_PANEL_CONTROL_GAMMA (1 << 3)
  225. #define SM501_DC_PANEL_CONTROL_EN (1 << 2)
  226. #define SM501_DC_PANEL_CONTROL_8BPP (0 << 0)
  227. #define SM501_DC_PANEL_CONTROL_16BPP (1 << 0)
  228. #define SM501_DC_PANEL_CONTROL_32BPP (2 << 0)
  229. #define SM501_DC_PANEL_PANNING_CONTROL 0x004
  230. #define SM501_DC_PANEL_COLOR_KEY 0x008
  231. #define SM501_DC_PANEL_FB_ADDR 0x00C
  232. #define SM501_DC_PANEL_FB_OFFSET 0x010
  233. #define SM501_DC_PANEL_FB_WIDTH 0x014
  234. #define SM501_DC_PANEL_FB_HEIGHT 0x018
  235. #define SM501_DC_PANEL_TL_LOC 0x01C
  236. #define SM501_DC_PANEL_BR_LOC 0x020
  237. #define SM501_DC_PANEL_H_TOT 0x024
  238. #define SM501_DC_PANEL_H_SYNC 0x028
  239. #define SM501_DC_PANEL_V_TOT 0x02C
  240. #define SM501_DC_PANEL_V_SYNC 0x030
  241. #define SM501_DC_PANEL_CUR_LINE 0x034
  242. #define SM501_DC_VIDEO_CONTROL 0x040
  243. #define SM501_DC_VIDEO_FB0_ADDR 0x044
  244. #define SM501_DC_VIDEO_FB_WIDTH 0x048
  245. #define SM501_DC_VIDEO_FB0_LAST_ADDR 0x04C
  246. #define SM501_DC_VIDEO_TL_LOC 0x050
  247. #define SM501_DC_VIDEO_BR_LOC 0x054
  248. #define SM501_DC_VIDEO_SCALE 0x058
  249. #define SM501_DC_VIDEO_INIT_SCALE 0x05C
  250. #define SM501_DC_VIDEO_YUV_CONSTANTS 0x060
  251. #define SM501_DC_VIDEO_FB1_ADDR 0x064
  252. #define SM501_DC_VIDEO_FB1_LAST_ADDR 0x068
  253. #define SM501_DC_VIDEO_ALPHA_CONTROL 0x080
  254. #define SM501_DC_VIDEO_ALPHA_FB_ADDR 0x084
  255. #define SM501_DC_VIDEO_ALPHA_FB_OFFSET 0x088
  256. #define SM501_DC_VIDEO_ALPHA_FB_LAST_ADDR 0x08C
  257. #define SM501_DC_VIDEO_ALPHA_TL_LOC 0x090
  258. #define SM501_DC_VIDEO_ALPHA_BR_LOC 0x094
  259. #define SM501_DC_VIDEO_ALPHA_SCALE 0x098
  260. #define SM501_DC_VIDEO_ALPHA_INIT_SCALE 0x09C
  261. #define SM501_DC_VIDEO_ALPHA_CHROMA_KEY 0x0A0
  262. #define SM501_DC_VIDEO_ALPHA_COLOR_LOOKUP 0x0A4
  263. #define SM501_DC_PANEL_HWC_BASE 0x0F0
  264. #define SM501_DC_PANEL_HWC_ADDR 0x0F0
  265. #define SM501_DC_PANEL_HWC_LOC 0x0F4
  266. #define SM501_DC_PANEL_HWC_COLOR_1_2 0x0F8
  267. #define SM501_DC_PANEL_HWC_COLOR_3 0x0FC
  268. #define SM501_HWC_EN (1 << 31)
  269. #define SM501_OFF_HWC_ADDR 0x00
  270. #define SM501_OFF_HWC_LOC 0x04
  271. #define SM501_OFF_HWC_COLOR_1_2 0x08
  272. #define SM501_OFF_HWC_COLOR_3 0x0C
  273. #define SM501_DC_ALPHA_CONTROL 0x100
  274. #define SM501_DC_ALPHA_FB_ADDR 0x104
  275. #define SM501_DC_ALPHA_FB_OFFSET 0x108
  276. #define SM501_DC_ALPHA_TL_LOC 0x10C
  277. #define SM501_DC_ALPHA_BR_LOC 0x110
  278. #define SM501_DC_ALPHA_CHROMA_KEY 0x114
  279. #define SM501_DC_ALPHA_COLOR_LOOKUP 0x118
  280. #define SM501_DC_CRT_CONTROL 0x200
  281. #define SM501_DC_CRT_CONTROL_TVP (1 << 15)
  282. #define SM501_DC_CRT_CONTROL_CP (1 << 14)
  283. #define SM501_DC_CRT_CONTROL_VSP (1 << 13)
  284. #define SM501_DC_CRT_CONTROL_HSP (1 << 12)
  285. #define SM501_DC_CRT_CONTROL_VS (1 << 11)
  286. #define SM501_DC_CRT_CONTROL_BLANK (1 << 10)
  287. #define SM501_DC_CRT_CONTROL_SEL (1 << 9)
  288. #define SM501_DC_CRT_CONTROL_TE (1 << 8)
  289. #define SM501_DC_CRT_CONTROL_PIXEL_MASK (0xF << 4)
  290. #define SM501_DC_CRT_CONTROL_GAMMA (1 << 3)
  291. #define SM501_DC_CRT_CONTROL_ENABLE (1 << 2)
  292. #define SM501_DC_CRT_CONTROL_8BPP (0 << 0)
  293. #define SM501_DC_CRT_CONTROL_16BPP (1 << 0)
  294. #define SM501_DC_CRT_CONTROL_32BPP (2 << 0)
  295. #define SM501_DC_CRT_FB_ADDR 0x204
  296. #define SM501_DC_CRT_FB_OFFSET 0x208
  297. #define SM501_DC_CRT_H_TOT 0x20C
  298. #define SM501_DC_CRT_H_SYNC 0x210
  299. #define SM501_DC_CRT_V_TOT 0x214
  300. #define SM501_DC_CRT_V_SYNC 0x218
  301. #define SM501_DC_CRT_SIGNATURE_ANALYZER 0x21C
  302. #define SM501_DC_CRT_CUR_LINE 0x220
  303. #define SM501_DC_CRT_MONITOR_DETECT 0x224
  304. #define SM501_DC_CRT_HWC_BASE 0x230
  305. #define SM501_DC_CRT_HWC_ADDR 0x230
  306. #define SM501_DC_CRT_HWC_LOC 0x234
  307. #define SM501_DC_CRT_HWC_COLOR_1_2 0x238
  308. #define SM501_DC_CRT_HWC_COLOR_3 0x23C
  309. #define SM501_DC_PANEL_PALETTE 0x400
  310. #define SM501_DC_VIDEO_PALETTE 0x800
  311. #define SM501_DC_CRT_PALETTE 0xC00
  312. /* Zoom Video port base */
  313. #define SM501_ZVPORT 0x090000
  314. /* AC97/I2S base */
  315. #define SM501_AC97 0x0A0000
  316. /* 8051 micro controller base */
  317. #define SM501_UCONTROLLER 0x0B0000
  318. /* 8051 micro controller SRAM base */
  319. #define SM501_UCONTROLLER_SRAM 0x0C0000
  320. /* DMA base */
  321. #define SM501_DMA 0x0D0000
  322. /* 2d engine base */
  323. #define SM501_2D_ENGINE 0x100000
  324. #define SM501_2D_SOURCE 0x00
  325. #define SM501_2D_DESTINATION 0x04
  326. #define SM501_2D_DIMENSION 0x08
  327. #define SM501_2D_CONTROL 0x0C
  328. #define SM501_2D_PITCH 0x10
  329. #define SM501_2D_FOREGROUND 0x14
  330. #define SM501_2D_BACKGROUND 0x18
  331. #define SM501_2D_STRETCH 0x1C
  332. #define SM501_2D_COLOR_COMPARE 0x20
  333. #define SM501_2D_COLOR_COMPARE_MASK 0x24
  334. #define SM501_2D_MASK 0x28
  335. #define SM501_2D_CLIP_TL 0x2C
  336. #define SM501_2D_CLIP_BR 0x30
  337. #define SM501_2D_MONO_PATTERN_LOW 0x34
  338. #define SM501_2D_MONO_PATTERN_HIGH 0x38
  339. #define SM501_2D_WINDOW_WIDTH 0x3C
  340. #define SM501_2D_SOURCE_BASE 0x40
  341. #define SM501_2D_DESTINATION_BASE 0x44
  342. #define SM501_2D_ALPHA 0x48
  343. #define SM501_2D_WRAP 0x4C
  344. #define SM501_2D_STATUS 0x50
  345. #define SM501_CSC_Y_SOURCE_BASE 0xC8
  346. #define SM501_CSC_CONSTANTS 0xCC
  347. #define SM501_CSC_Y_SOURCE_X 0xD0
  348. #define SM501_CSC_Y_SOURCE_Y 0xD4
  349. #define SM501_CSC_U_SOURCE_BASE 0xD8
  350. #define SM501_CSC_V_SOURCE_BASE 0xDC
  351. #define SM501_CSC_SOURCE_DIMENSION 0xE0
  352. #define SM501_CSC_SOURCE_PITCH 0xE4
  353. #define SM501_CSC_DESTINATION 0xE8
  354. #define SM501_CSC_DESTINATION_DIMENSION 0xEC
  355. #define SM501_CSC_DESTINATION_PITCH 0xF0
  356. #define SM501_CSC_SCALE_FACTOR 0xF4
  357. #define SM501_CSC_DESTINATION_BASE 0xF8
  358. #define SM501_CSC_CONTROL 0xFC
  359. /* 2d engine data port base */
  360. #define SM501_2D_ENGINE_DATA 0x110000
  361. /* end of register definitions */
  362. #define SM501_HWC_WIDTH 64
  363. #define SM501_HWC_HEIGHT 64
  364. /* SM501 local memory size taken from "linux/drivers/mfd/sm501.c" */
  365. static const uint32_t sm501_mem_local_size[] = {
  366. [0] = 4 * MiB,
  367. [1] = 8 * MiB,
  368. [2] = 16 * MiB,
  369. [3] = 32 * MiB,
  370. [4] = 64 * MiB,
  371. [5] = 2 * MiB,
  372. };
  373. #define get_local_mem_size(s) sm501_mem_local_size[(s)->local_mem_size_index]
  374. typedef struct SM501State {
  375. /* graphic console status */
  376. QemuConsole *con;
  377. /* status & internal resources */
  378. uint32_t local_mem_size_index;
  379. uint8_t *local_mem;
  380. MemoryRegion local_mem_region;
  381. MemoryRegion mmio_region;
  382. MemoryRegion system_config_region;
  383. MemoryRegion i2c_region;
  384. MemoryRegion disp_ctrl_region;
  385. MemoryRegion twoD_engine_region;
  386. uint32_t last_width;
  387. uint32_t last_height;
  388. bool do_full_update; /* perform a full update next time */
  389. uint8_t use_pixman;
  390. I2CBus *i2c_bus;
  391. /* mmio registers */
  392. uint32_t system_control;
  393. uint32_t misc_control;
  394. uint32_t gpio_31_0_control;
  395. uint32_t gpio_63_32_control;
  396. uint32_t dram_control;
  397. uint32_t arbitration_control;
  398. uint32_t irq_mask;
  399. uint32_t misc_timing;
  400. uint32_t power_mode_control;
  401. uint8_t i2c_byte_count;
  402. uint8_t i2c_status;
  403. uint8_t i2c_addr;
  404. uint8_t i2c_data[16];
  405. uint32_t uart0_ier;
  406. uint32_t uart0_lcr;
  407. uint32_t uart0_mcr;
  408. uint32_t uart0_scr;
  409. uint8_t dc_palette[DC_PALETTE_ENTRIES];
  410. uint32_t dc_panel_control;
  411. uint32_t dc_panel_panning_control;
  412. uint32_t dc_panel_fb_addr;
  413. uint32_t dc_panel_fb_offset;
  414. uint32_t dc_panel_fb_width;
  415. uint32_t dc_panel_fb_height;
  416. uint32_t dc_panel_tl_location;
  417. uint32_t dc_panel_br_location;
  418. uint32_t dc_panel_h_total;
  419. uint32_t dc_panel_h_sync;
  420. uint32_t dc_panel_v_total;
  421. uint32_t dc_panel_v_sync;
  422. uint32_t dc_panel_hwc_addr;
  423. uint32_t dc_panel_hwc_location;
  424. uint32_t dc_panel_hwc_color_1_2;
  425. uint32_t dc_panel_hwc_color_3;
  426. uint32_t dc_video_control;
  427. uint32_t dc_crt_control;
  428. uint32_t dc_crt_fb_addr;
  429. uint32_t dc_crt_fb_offset;
  430. uint32_t dc_crt_h_total;
  431. uint32_t dc_crt_h_sync;
  432. uint32_t dc_crt_v_total;
  433. uint32_t dc_crt_v_sync;
  434. uint32_t dc_crt_hwc_addr;
  435. uint32_t dc_crt_hwc_location;
  436. uint32_t dc_crt_hwc_color_1_2;
  437. uint32_t dc_crt_hwc_color_3;
  438. uint32_t twoD_source;
  439. uint32_t twoD_destination;
  440. uint32_t twoD_dimension;
  441. uint32_t twoD_control;
  442. uint32_t twoD_pitch;
  443. uint32_t twoD_foreground;
  444. uint32_t twoD_background;
  445. uint32_t twoD_stretch;
  446. uint32_t twoD_color_compare;
  447. uint32_t twoD_color_compare_mask;
  448. uint32_t twoD_mask;
  449. uint32_t twoD_clip_tl;
  450. uint32_t twoD_clip_br;
  451. uint32_t twoD_mono_pattern_low;
  452. uint32_t twoD_mono_pattern_high;
  453. uint32_t twoD_window_width;
  454. uint32_t twoD_source_base;
  455. uint32_t twoD_destination_base;
  456. uint32_t twoD_alpha;
  457. uint32_t twoD_wrap;
  458. } SM501State;
  459. static uint32_t get_local_mem_size_index(uint32_t size)
  460. {
  461. uint32_t norm_size = 0;
  462. int i, index = 0;
  463. for (i = 0; i < ARRAY_SIZE(sm501_mem_local_size); i++) {
  464. uint32_t new_size = sm501_mem_local_size[i];
  465. if (new_size >= size) {
  466. if (norm_size == 0 || norm_size > new_size) {
  467. norm_size = new_size;
  468. index = i;
  469. }
  470. }
  471. }
  472. return index;
  473. }
  474. static ram_addr_t get_fb_addr(SM501State *s, int crt)
  475. {
  476. return (crt ? s->dc_crt_fb_addr : s->dc_panel_fb_addr) & 0x3FFFFF0;
  477. }
  478. static inline int get_width(SM501State *s, int crt)
  479. {
  480. int width = crt ? s->dc_crt_h_total : s->dc_panel_h_total;
  481. return (width & 0x00000FFF) + 1;
  482. }
  483. static inline int get_height(SM501State *s, int crt)
  484. {
  485. int height = crt ? s->dc_crt_v_total : s->dc_panel_v_total;
  486. return (height & 0x00000FFF) + 1;
  487. }
  488. static inline int get_bpp(SM501State *s, int crt)
  489. {
  490. int bpp = crt ? s->dc_crt_control : s->dc_panel_control;
  491. return 1 << (bpp & 3);
  492. }
  493. /**
  494. * Check the availability of hardware cursor.
  495. * @param crt 0 for PANEL, 1 for CRT.
  496. */
  497. static inline int is_hwc_enabled(SM501State *state, int crt)
  498. {
  499. uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
  500. return addr & SM501_HWC_EN;
  501. }
  502. /**
  503. * Get the address which holds cursor pattern data.
  504. * @param crt 0 for PANEL, 1 for CRT.
  505. */
  506. static inline uint8_t *get_hwc_address(SM501State *state, int crt)
  507. {
  508. uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
  509. return state->local_mem + (addr & 0x03FFFFF0);
  510. }
  511. /**
  512. * Get the cursor position in y coordinate.
  513. * @param crt 0 for PANEL, 1 for CRT.
  514. */
  515. static inline uint32_t get_hwc_y(SM501State *state, int crt)
  516. {
  517. uint32_t location = crt ? state->dc_crt_hwc_location
  518. : state->dc_panel_hwc_location;
  519. return (location & 0x07FF0000) >> 16;
  520. }
  521. /**
  522. * Get the cursor position in x coordinate.
  523. * @param crt 0 for PANEL, 1 for CRT.
  524. */
  525. static inline uint32_t get_hwc_x(SM501State *state, int crt)
  526. {
  527. uint32_t location = crt ? state->dc_crt_hwc_location
  528. : state->dc_panel_hwc_location;
  529. return location & 0x000007FF;
  530. }
  531. /**
  532. * Get the hardware cursor palette.
  533. * @param crt 0 for PANEL, 1 for CRT.
  534. * @param palette pointer to a [3 * 3] array to store color values in
  535. */
  536. static inline void get_hwc_palette(SM501State *state, int crt, uint8_t *palette)
  537. {
  538. int i;
  539. uint32_t color_reg;
  540. uint16_t rgb565;
  541. for (i = 0; i < 3; i++) {
  542. if (i + 1 == 3) {
  543. color_reg = crt ? state->dc_crt_hwc_color_3
  544. : state->dc_panel_hwc_color_3;
  545. } else {
  546. color_reg = crt ? state->dc_crt_hwc_color_1_2
  547. : state->dc_panel_hwc_color_1_2;
  548. }
  549. if (i + 1 == 2) {
  550. rgb565 = (color_reg >> 16) & 0xFFFF;
  551. } else {
  552. rgb565 = color_reg & 0xFFFF;
  553. }
  554. palette[i * 3 + 0] = ((rgb565 >> 11) * 527 + 23) >> 6; /* r */
  555. palette[i * 3 + 1] = (((rgb565 >> 5) & 0x3f) * 259 + 33) >> 6; /* g */
  556. palette[i * 3 + 2] = ((rgb565 & 0x1f) * 527 + 23) >> 6; /* b */
  557. }
  558. }
  559. static inline void hwc_invalidate(SM501State *s, int crt)
  560. {
  561. int w = get_width(s, crt);
  562. int h = get_height(s, crt);
  563. int bpp = get_bpp(s, crt);
  564. int start = get_hwc_y(s, crt);
  565. int end = MIN(h, start + SM501_HWC_HEIGHT) + 1;
  566. start *= w * bpp;
  567. end *= w * bpp;
  568. memory_region_set_dirty(&s->local_mem_region,
  569. get_fb_addr(s, crt) + start, end - start);
  570. }
  571. static void sm501_2d_operation(SM501State *s)
  572. {
  573. int cmd = (s->twoD_control >> 16) & 0x1F;
  574. int rtl = s->twoD_control & BIT(27);
  575. int format = (s->twoD_stretch >> 20) & 3;
  576. int bypp = 1 << format; /* bytes per pixel */
  577. int rop_mode = (s->twoD_control >> 15) & 1; /* 1 for rop2, else rop3 */
  578. /* 1 if rop2 source is the pattern, otherwise the source is the bitmap */
  579. int rop2_source_is_pattern = (s->twoD_control >> 14) & 1;
  580. int rop = s->twoD_control & 0xFF;
  581. unsigned int dst_x = (s->twoD_destination >> 16) & 0x01FFF;
  582. unsigned int dst_y = s->twoD_destination & 0xFFFF;
  583. unsigned int width = (s->twoD_dimension >> 16) & 0x1FFF;
  584. unsigned int height = s->twoD_dimension & 0xFFFF;
  585. uint32_t dst_base = s->twoD_destination_base & 0x03FFFFFF;
  586. unsigned int dst_pitch = (s->twoD_pitch >> 16) & 0x1FFF;
  587. int crt = (s->dc_crt_control & SM501_DC_CRT_CONTROL_SEL) ? 1 : 0;
  588. int fb_len = get_width(s, crt) * get_height(s, crt) * get_bpp(s, crt);
  589. bool overlap = false, fallback = false;
  590. if ((s->twoD_stretch >> 16) & 0xF) {
  591. qemu_log_mask(LOG_UNIMP, "sm501: only XY addressing is supported.\n");
  592. return;
  593. }
  594. if (s->twoD_source_base & BIT(27) || s->twoD_destination_base & BIT(27)) {
  595. qemu_log_mask(LOG_UNIMP, "sm501: only local memory is supported.\n");
  596. return;
  597. }
  598. if (!dst_pitch) {
  599. qemu_log_mask(LOG_GUEST_ERROR, "sm501: Zero dest pitch.\n");
  600. return;
  601. }
  602. if (!width || !height) {
  603. qemu_log_mask(LOG_GUEST_ERROR, "sm501: Zero size 2D op.\n");
  604. return;
  605. }
  606. if (rtl) {
  607. dst_x -= width - 1;
  608. dst_y -= height - 1;
  609. }
  610. if (dst_base >= get_local_mem_size(s) ||
  611. dst_base + (dst_x + width + (dst_y + height) * dst_pitch) * bypp >=
  612. get_local_mem_size(s)) {
  613. qemu_log_mask(LOG_GUEST_ERROR, "sm501: 2D op dest is outside vram.\n");
  614. return;
  615. }
  616. switch (cmd) {
  617. case 0: /* BitBlt */
  618. {
  619. static uint32_t tmp_buf[16384];
  620. unsigned int src_x = (s->twoD_source >> 16) & 0x01FFF;
  621. unsigned int src_y = s->twoD_source & 0xFFFF;
  622. uint32_t src_base = s->twoD_source_base & 0x03FFFFFF;
  623. unsigned int src_pitch = s->twoD_pitch & 0x1FFF;
  624. if (!src_pitch) {
  625. qemu_log_mask(LOG_GUEST_ERROR, "sm501: Zero src pitch.\n");
  626. return;
  627. }
  628. if (rtl) {
  629. src_x -= width - 1;
  630. src_y -= height - 1;
  631. }
  632. if (src_base >= get_local_mem_size(s) ||
  633. src_base + (src_x + width + (src_y + height) * src_pitch) * bypp >=
  634. get_local_mem_size(s)) {
  635. qemu_log_mask(LOG_GUEST_ERROR,
  636. "sm501: 2D op src is outside vram.\n");
  637. return;
  638. }
  639. if ((rop_mode && rop == 0x5) || (!rop_mode && rop == 0x55)) {
  640. /* DSTINVERT, is there a way to do this with pixman? */
  641. unsigned int x, y, i;
  642. uint8_t *d = s->local_mem + dst_base;
  643. for (y = 0; y < height; y++) {
  644. i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
  645. for (x = 0; x < width; x++, i += bypp) {
  646. stn_he_p(&d[i], bypp, ~ldn_he_p(&d[i], bypp));
  647. }
  648. }
  649. } else if (!rop_mode && rop == 0x99) {
  650. /* DSxn, is there a way to do this with pixman? */
  651. unsigned int x, y, i, j;
  652. uint8_t *sp = s->local_mem + src_base;
  653. uint8_t *d = s->local_mem + dst_base;
  654. for (y = 0; y < height; y++) {
  655. i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
  656. j = (src_x + (src_y + y) * src_pitch) * bypp;
  657. for (x = 0; x < width; x++, i += bypp, j += bypp) {
  658. stn_he_p(&d[i], bypp,
  659. ~(ldn_he_p(&sp[j], bypp) ^ ldn_he_p(&d[i], bypp)));
  660. }
  661. }
  662. } else if (!rop_mode && rop == 0xee) {
  663. /* SRCPAINT, is there a way to do this with pixman? */
  664. unsigned int x, y, i, j;
  665. uint8_t *sp = s->local_mem + src_base;
  666. uint8_t *d = s->local_mem + dst_base;
  667. for (y = 0; y < height; y++) {
  668. i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
  669. j = (src_x + (src_y + y) * src_pitch) * bypp;
  670. for (x = 0; x < width; x++, i += bypp, j += bypp) {
  671. stn_he_p(&d[i], bypp,
  672. ldn_he_p(&sp[j], bypp) | ldn_he_p(&d[i], bypp));
  673. }
  674. }
  675. } else {
  676. /* Do copy src for unimplemented ops, better than unpainted area */
  677. if ((rop_mode && (rop != 0xc || rop2_source_is_pattern)) ||
  678. (!rop_mode && rop != 0xcc)) {
  679. qemu_log_mask(LOG_UNIMP,
  680. "sm501: rop%d op %x%s not implemented\n",
  681. (rop_mode ? 2 : 3), rop,
  682. (rop2_source_is_pattern ?
  683. " with pattern source" : ""));
  684. }
  685. /* Ignore no-op blits, some guests seem to do this */
  686. if (src_base == dst_base && src_pitch == dst_pitch &&
  687. src_x == dst_x && src_y == dst_y) {
  688. break;
  689. }
  690. /* Some clients also do 1 pixel blits, avoid overhead for these */
  691. if (width == 1 && height == 1) {
  692. unsigned int si = (src_x + src_y * src_pitch) * bypp;
  693. unsigned int di = (dst_x + dst_y * dst_pitch) * bypp;
  694. stn_he_p(&s->local_mem[dst_base + di], bypp,
  695. ldn_he_p(&s->local_mem[src_base + si], bypp));
  696. break;
  697. }
  698. /* If reverse blit do simple check for overlaps */
  699. if (rtl && src_base == dst_base && src_pitch == dst_pitch) {
  700. overlap = (src_x < dst_x + width && src_x + width > dst_x &&
  701. src_y < dst_y + height && src_y + height > dst_y);
  702. } else if (rtl) {
  703. unsigned int sb, se, db, de;
  704. sb = src_base + (src_x + src_y * src_pitch) * bypp;
  705. se = sb + (width + (height - 1) * src_pitch) * bypp;
  706. db = dst_base + (dst_x + dst_y * dst_pitch) * bypp;
  707. de = db + (width + (height - 1) * dst_pitch) * bypp;
  708. overlap = (db < se && sb < de);
  709. }
  710. if (overlap && (s->use_pixman & BIT(2))) {
  711. /* pixman can't do reverse blit: copy via temporary */
  712. int tmp_stride = DIV_ROUND_UP(width * bypp, sizeof(uint32_t));
  713. uint32_t *tmp = tmp_buf;
  714. if (tmp_stride * sizeof(uint32_t) * height > sizeof(tmp_buf)) {
  715. tmp = g_malloc(tmp_stride * sizeof(uint32_t) * height);
  716. }
  717. fallback = !pixman_blt((uint32_t *)&s->local_mem[src_base],
  718. tmp,
  719. src_pitch * bypp / sizeof(uint32_t),
  720. tmp_stride,
  721. 8 * bypp, 8 * bypp,
  722. src_x, src_y, 0, 0, width, height);
  723. if (!fallback) {
  724. fallback = !pixman_blt(tmp,
  725. (uint32_t *)&s->local_mem[dst_base],
  726. tmp_stride,
  727. dst_pitch * bypp / sizeof(uint32_t),
  728. 8 * bypp, 8 * bypp,
  729. 0, 0, dst_x, dst_y, width, height);
  730. }
  731. if (tmp != tmp_buf) {
  732. g_free(tmp);
  733. }
  734. } else if (!overlap && (s->use_pixman & BIT(1))) {
  735. fallback = !pixman_blt((uint32_t *)&s->local_mem[src_base],
  736. (uint32_t *)&s->local_mem[dst_base],
  737. src_pitch * bypp / sizeof(uint32_t),
  738. dst_pitch * bypp / sizeof(uint32_t),
  739. 8 * bypp, 8 * bypp, src_x, src_y,
  740. dst_x, dst_y, width, height);
  741. } else {
  742. fallback = true;
  743. }
  744. if (fallback) {
  745. uint8_t *sp = s->local_mem + src_base;
  746. uint8_t *d = s->local_mem + dst_base;
  747. unsigned int y, i, j;
  748. for (y = 0; y < height; y++) {
  749. if (overlap) { /* overlap also means rtl */
  750. i = (dst_y + height - 1 - y) * dst_pitch;
  751. i = (dst_x + i) * bypp;
  752. j = (src_y + height - 1 - y) * src_pitch;
  753. j = (src_x + j) * bypp;
  754. memmove(&d[i], &sp[j], width * bypp);
  755. } else {
  756. i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
  757. j = (src_x + (src_y + y) * src_pitch) * bypp;
  758. memcpy(&d[i], &sp[j], width * bypp);
  759. }
  760. }
  761. }
  762. }
  763. break;
  764. }
  765. case 1: /* Rectangle Fill */
  766. {
  767. uint32_t color = s->twoD_foreground;
  768. if (format == 2) {
  769. color = cpu_to_le32(color);
  770. } else if (format == 1) {
  771. color = cpu_to_le16(color);
  772. }
  773. if (!(s->use_pixman & BIT(0)) || (width == 1 && height == 1) ||
  774. !pixman_fill((uint32_t *)&s->local_mem[dst_base],
  775. dst_pitch * bypp / sizeof(uint32_t), 8 * bypp,
  776. dst_x, dst_y, width, height, color)) {
  777. /* fallback when pixman failed or we don't want to call it */
  778. uint8_t *d = s->local_mem + dst_base;
  779. unsigned int x, y, i;
  780. for (y = 0; y < height; y++, i += dst_pitch * bypp) {
  781. i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
  782. for (x = 0; x < width; x++, i += bypp) {
  783. stn_he_p(&d[i], bypp, color);
  784. }
  785. }
  786. }
  787. break;
  788. }
  789. default:
  790. qemu_log_mask(LOG_UNIMP, "sm501: not implemented 2D operation: %d\n",
  791. cmd);
  792. return;
  793. }
  794. if (dst_base >= get_fb_addr(s, crt) &&
  795. dst_base <= get_fb_addr(s, crt) + fb_len) {
  796. int dst_len = MIN(fb_len, ((dst_y + height - 1) * dst_pitch +
  797. dst_x + width) * bypp);
  798. if (dst_len) {
  799. memory_region_set_dirty(&s->local_mem_region, dst_base, dst_len);
  800. }
  801. }
  802. }
  803. static uint64_t sm501_system_config_read(void *opaque, hwaddr addr,
  804. unsigned size)
  805. {
  806. SM501State *s = opaque;
  807. uint32_t ret = 0;
  808. switch (addr) {
  809. case SM501_SYSTEM_CONTROL:
  810. ret = s->system_control;
  811. break;
  812. case SM501_MISC_CONTROL:
  813. ret = s->misc_control;
  814. break;
  815. case SM501_GPIO31_0_CONTROL:
  816. ret = s->gpio_31_0_control;
  817. break;
  818. case SM501_GPIO63_32_CONTROL:
  819. ret = s->gpio_63_32_control;
  820. break;
  821. case SM501_DEVICEID:
  822. ret = 0x050100A0;
  823. break;
  824. case SM501_DRAM_CONTROL:
  825. ret = (s->dram_control & 0x07F107C0) | s->local_mem_size_index << 13;
  826. break;
  827. case SM501_ARBTRTN_CONTROL:
  828. ret = s->arbitration_control;
  829. break;
  830. case SM501_COMMAND_LIST_STATUS:
  831. ret = 0x00180002; /* FIFOs are empty, everything idle */
  832. break;
  833. case SM501_IRQ_MASK:
  834. ret = s->irq_mask;
  835. break;
  836. case SM501_MISC_TIMING:
  837. /* TODO : simulate gate control */
  838. ret = s->misc_timing;
  839. break;
  840. case SM501_CURRENT_GATE:
  841. /* TODO : simulate gate control */
  842. ret = 0x00021807;
  843. break;
  844. case SM501_CURRENT_CLOCK:
  845. ret = 0x2A1A0A09;
  846. break;
  847. case SM501_POWER_MODE_CONTROL:
  848. ret = s->power_mode_control;
  849. break;
  850. case SM501_ENDIAN_CONTROL:
  851. ret = 0; /* Only default little endian mode is supported */
  852. break;
  853. default:
  854. qemu_log_mask(LOG_UNIMP, "sm501: not implemented system config"
  855. "register read. addr=%" HWADDR_PRIx "\n", addr);
  856. }
  857. trace_sm501_system_config_read(addr, ret);
  858. return ret;
  859. }
  860. static void sm501_system_config_write(void *opaque, hwaddr addr,
  861. uint64_t value, unsigned size)
  862. {
  863. SM501State *s = opaque;
  864. trace_sm501_system_config_write((uint32_t)addr, (uint32_t)value);
  865. switch (addr) {
  866. case SM501_SYSTEM_CONTROL:
  867. s->system_control &= 0x10DB0000;
  868. s->system_control |= value & 0xEF00B8F7;
  869. break;
  870. case SM501_MISC_CONTROL:
  871. s->misc_control &= 0xEF;
  872. s->misc_control |= value & 0xFF7FFF10;
  873. break;
  874. case SM501_GPIO31_0_CONTROL:
  875. s->gpio_31_0_control = value;
  876. break;
  877. case SM501_GPIO63_32_CONTROL:
  878. s->gpio_63_32_control = value & 0xFF80FFFF;
  879. break;
  880. case SM501_DRAM_CONTROL:
  881. s->local_mem_size_index = (value >> 13) & 0x7;
  882. /* TODO : check validity of size change */
  883. s->dram_control &= 0x80000000;
  884. s->dram_control |= value & 0x7FFFFFC3;
  885. break;
  886. case SM501_ARBTRTN_CONTROL:
  887. s->arbitration_control = value & 0x37777777;
  888. break;
  889. case SM501_IRQ_MASK:
  890. s->irq_mask = value & 0xFFDF3F5F;
  891. break;
  892. case SM501_MISC_TIMING:
  893. s->misc_timing = value & 0xF31F1FFF;
  894. break;
  895. case SM501_POWER_MODE_0_GATE:
  896. case SM501_POWER_MODE_1_GATE:
  897. case SM501_POWER_MODE_0_CLOCK:
  898. case SM501_POWER_MODE_1_CLOCK:
  899. /* TODO : simulate gate & clock control */
  900. break;
  901. case SM501_POWER_MODE_CONTROL:
  902. s->power_mode_control = value & 0x00000003;
  903. break;
  904. case SM501_ENDIAN_CONTROL:
  905. if (value & 0x00000001) {
  906. qemu_log_mask(LOG_UNIMP, "sm501: system config big endian mode not"
  907. " implemented.\n");
  908. }
  909. break;
  910. default:
  911. qemu_log_mask(LOG_UNIMP, "sm501: not implemented system config"
  912. "register write. addr=%" HWADDR_PRIx
  913. ", val=%" PRIx64 "\n", addr, value);
  914. }
  915. }
  916. static const MemoryRegionOps sm501_system_config_ops = {
  917. .read = sm501_system_config_read,
  918. .write = sm501_system_config_write,
  919. .valid = {
  920. .min_access_size = 4,
  921. .max_access_size = 4,
  922. },
  923. .endianness = DEVICE_LITTLE_ENDIAN,
  924. };
  925. static uint64_t sm501_i2c_read(void *opaque, hwaddr addr, unsigned size)
  926. {
  927. SM501State *s = opaque;
  928. uint8_t ret = 0;
  929. switch (addr) {
  930. case SM501_I2C_BYTE_COUNT:
  931. ret = s->i2c_byte_count;
  932. break;
  933. case SM501_I2C_STATUS:
  934. ret = s->i2c_status;
  935. break;
  936. case SM501_I2C_SLAVE_ADDRESS:
  937. ret = s->i2c_addr;
  938. break;
  939. case SM501_I2C_DATA ... SM501_I2C_DATA + 15:
  940. ret = s->i2c_data[addr - SM501_I2C_DATA];
  941. break;
  942. default:
  943. qemu_log_mask(LOG_UNIMP, "sm501 i2c : not implemented register read."
  944. " addr=0x%" HWADDR_PRIx "\n", addr);
  945. }
  946. trace_sm501_i2c_read((uint32_t)addr, ret);
  947. return ret;
  948. }
  949. static void sm501_i2c_write(void *opaque, hwaddr addr, uint64_t value,
  950. unsigned size)
  951. {
  952. SM501State *s = opaque;
  953. trace_sm501_i2c_write((uint32_t)addr, (uint32_t)value);
  954. switch (addr) {
  955. case SM501_I2C_BYTE_COUNT:
  956. s->i2c_byte_count = value & 0xf;
  957. break;
  958. case SM501_I2C_CONTROL:
  959. if (value & SM501_I2C_CONTROL_ENABLE) {
  960. if (value & SM501_I2C_CONTROL_START) {
  961. bool is_recv = s->i2c_addr & 1;
  962. int res = i2c_start_transfer(s->i2c_bus,
  963. s->i2c_addr >> 1,
  964. is_recv);
  965. if (res) {
  966. s->i2c_status |= SM501_I2C_STATUS_ERROR;
  967. } else {
  968. int i;
  969. for (i = 0; i <= s->i2c_byte_count; i++) {
  970. if (is_recv) {
  971. s->i2c_data[i] = i2c_recv(s->i2c_bus);
  972. } else if (i2c_send(s->i2c_bus, s->i2c_data[i]) < 0) {
  973. s->i2c_status |= SM501_I2C_STATUS_ERROR;
  974. return;
  975. }
  976. }
  977. if (i) {
  978. s->i2c_status = SM501_I2C_STATUS_COMPLETE;
  979. }
  980. }
  981. } else {
  982. i2c_end_transfer(s->i2c_bus);
  983. s->i2c_status &= ~SM501_I2C_STATUS_ERROR;
  984. }
  985. }
  986. break;
  987. case SM501_I2C_RESET:
  988. if ((value & SM501_I2C_RESET_ERROR) == 0) {
  989. s->i2c_status &= ~SM501_I2C_STATUS_ERROR;
  990. }
  991. break;
  992. case SM501_I2C_SLAVE_ADDRESS:
  993. s->i2c_addr = value & 0xff;
  994. break;
  995. case SM501_I2C_DATA ... SM501_I2C_DATA + 15:
  996. s->i2c_data[addr - SM501_I2C_DATA] = value & 0xff;
  997. break;
  998. default:
  999. qemu_log_mask(LOG_UNIMP, "sm501 i2c : not implemented register write. "
  1000. "addr=0x%" HWADDR_PRIx " val=%" PRIx64 "\n", addr, value);
  1001. }
  1002. }
  1003. static const MemoryRegionOps sm501_i2c_ops = {
  1004. .read = sm501_i2c_read,
  1005. .write = sm501_i2c_write,
  1006. .valid = {
  1007. .min_access_size = 1,
  1008. .max_access_size = 1,
  1009. },
  1010. .impl = {
  1011. .min_access_size = 1,
  1012. .max_access_size = 1,
  1013. },
  1014. .endianness = DEVICE_LITTLE_ENDIAN,
  1015. };
  1016. static uint32_t sm501_palette_read(void *opaque, hwaddr addr)
  1017. {
  1018. SM501State *s = opaque;
  1019. trace_sm501_palette_read((uint32_t)addr);
  1020. /* TODO : consider BYTE/WORD access */
  1021. /* TODO : consider endian */
  1022. assert(range_covers_byte(0, 0x400 * 3, addr));
  1023. return *(uint32_t *)&s->dc_palette[addr];
  1024. }
  1025. static void sm501_palette_write(void *opaque, hwaddr addr,
  1026. uint32_t value)
  1027. {
  1028. SM501State *s = opaque;
  1029. trace_sm501_palette_write((uint32_t)addr, value);
  1030. /* TODO : consider BYTE/WORD access */
  1031. /* TODO : consider endian */
  1032. assert(range_covers_byte(0, 0x400 * 3, addr));
  1033. *(uint32_t *)&s->dc_palette[addr] = value;
  1034. s->do_full_update = true;
  1035. }
  1036. static uint64_t sm501_disp_ctrl_read(void *opaque, hwaddr addr,
  1037. unsigned size)
  1038. {
  1039. SM501State *s = opaque;
  1040. uint32_t ret = 0;
  1041. switch (addr) {
  1042. case SM501_DC_PANEL_CONTROL:
  1043. ret = s->dc_panel_control;
  1044. break;
  1045. case SM501_DC_PANEL_PANNING_CONTROL:
  1046. ret = s->dc_panel_panning_control;
  1047. break;
  1048. case SM501_DC_PANEL_COLOR_KEY:
  1049. /* Not implemented yet */
  1050. break;
  1051. case SM501_DC_PANEL_FB_ADDR:
  1052. ret = s->dc_panel_fb_addr;
  1053. break;
  1054. case SM501_DC_PANEL_FB_OFFSET:
  1055. ret = s->dc_panel_fb_offset;
  1056. break;
  1057. case SM501_DC_PANEL_FB_WIDTH:
  1058. ret = s->dc_panel_fb_width;
  1059. break;
  1060. case SM501_DC_PANEL_FB_HEIGHT:
  1061. ret = s->dc_panel_fb_height;
  1062. break;
  1063. case SM501_DC_PANEL_TL_LOC:
  1064. ret = s->dc_panel_tl_location;
  1065. break;
  1066. case SM501_DC_PANEL_BR_LOC:
  1067. ret = s->dc_panel_br_location;
  1068. break;
  1069. case SM501_DC_PANEL_H_TOT:
  1070. ret = s->dc_panel_h_total;
  1071. break;
  1072. case SM501_DC_PANEL_H_SYNC:
  1073. ret = s->dc_panel_h_sync;
  1074. break;
  1075. case SM501_DC_PANEL_V_TOT:
  1076. ret = s->dc_panel_v_total;
  1077. break;
  1078. case SM501_DC_PANEL_V_SYNC:
  1079. ret = s->dc_panel_v_sync;
  1080. break;
  1081. case SM501_DC_PANEL_HWC_ADDR:
  1082. ret = s->dc_panel_hwc_addr;
  1083. break;
  1084. case SM501_DC_PANEL_HWC_LOC:
  1085. ret = s->dc_panel_hwc_location;
  1086. break;
  1087. case SM501_DC_PANEL_HWC_COLOR_1_2:
  1088. ret = s->dc_panel_hwc_color_1_2;
  1089. break;
  1090. case SM501_DC_PANEL_HWC_COLOR_3:
  1091. ret = s->dc_panel_hwc_color_3;
  1092. break;
  1093. case SM501_DC_VIDEO_CONTROL:
  1094. ret = s->dc_video_control;
  1095. break;
  1096. case SM501_DC_CRT_CONTROL:
  1097. ret = s->dc_crt_control;
  1098. break;
  1099. case SM501_DC_CRT_FB_ADDR:
  1100. ret = s->dc_crt_fb_addr;
  1101. break;
  1102. case SM501_DC_CRT_FB_OFFSET:
  1103. ret = s->dc_crt_fb_offset;
  1104. break;
  1105. case SM501_DC_CRT_H_TOT:
  1106. ret = s->dc_crt_h_total;
  1107. break;
  1108. case SM501_DC_CRT_H_SYNC:
  1109. ret = s->dc_crt_h_sync;
  1110. break;
  1111. case SM501_DC_CRT_V_TOT:
  1112. ret = s->dc_crt_v_total;
  1113. break;
  1114. case SM501_DC_CRT_V_SYNC:
  1115. ret = s->dc_crt_v_sync;
  1116. break;
  1117. case SM501_DC_CRT_HWC_ADDR:
  1118. ret = s->dc_crt_hwc_addr;
  1119. break;
  1120. case SM501_DC_CRT_HWC_LOC:
  1121. ret = s->dc_crt_hwc_location;
  1122. break;
  1123. case SM501_DC_CRT_HWC_COLOR_1_2:
  1124. ret = s->dc_crt_hwc_color_1_2;
  1125. break;
  1126. case SM501_DC_CRT_HWC_COLOR_3:
  1127. ret = s->dc_crt_hwc_color_3;
  1128. break;
  1129. case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400 * 3 - 4:
  1130. ret = sm501_palette_read(opaque, addr - SM501_DC_PANEL_PALETTE);
  1131. break;
  1132. default:
  1133. qemu_log_mask(LOG_UNIMP, "sm501: not implemented disp ctrl register "
  1134. "read. addr=%" HWADDR_PRIx "\n", addr);
  1135. }
  1136. trace_sm501_disp_ctrl_read((uint32_t)addr, ret);
  1137. return ret;
  1138. }
  1139. static void sm501_disp_ctrl_write(void *opaque, hwaddr addr,
  1140. uint64_t value, unsigned size)
  1141. {
  1142. SM501State *s = opaque;
  1143. trace_sm501_disp_ctrl_write((uint32_t)addr, (uint32_t)value);
  1144. switch (addr) {
  1145. case SM501_DC_PANEL_CONTROL:
  1146. s->dc_panel_control = value & 0x0FFF73FF;
  1147. break;
  1148. case SM501_DC_PANEL_PANNING_CONTROL:
  1149. s->dc_panel_panning_control = value & 0xFF3FFF3F;
  1150. break;
  1151. case SM501_DC_PANEL_COLOR_KEY:
  1152. /* Not implemented yet */
  1153. break;
  1154. case SM501_DC_PANEL_FB_ADDR:
  1155. s->dc_panel_fb_addr = value & 0x8FFFFFF0;
  1156. if (value & 0x8000000) {
  1157. qemu_log_mask(LOG_UNIMP, "Panel external memory not supported\n");
  1158. }
  1159. s->do_full_update = true;
  1160. break;
  1161. case SM501_DC_PANEL_FB_OFFSET:
  1162. s->dc_panel_fb_offset = value & 0x3FF03FF0;
  1163. break;
  1164. case SM501_DC_PANEL_FB_WIDTH:
  1165. s->dc_panel_fb_width = value & 0x0FFF0FFF;
  1166. break;
  1167. case SM501_DC_PANEL_FB_HEIGHT:
  1168. s->dc_panel_fb_height = value & 0x0FFF0FFF;
  1169. break;
  1170. case SM501_DC_PANEL_TL_LOC:
  1171. s->dc_panel_tl_location = value & 0x07FF07FF;
  1172. break;
  1173. case SM501_DC_PANEL_BR_LOC:
  1174. s->dc_panel_br_location = value & 0x07FF07FF;
  1175. break;
  1176. case SM501_DC_PANEL_H_TOT:
  1177. s->dc_panel_h_total = value & 0x0FFF0FFF;
  1178. break;
  1179. case SM501_DC_PANEL_H_SYNC:
  1180. s->dc_panel_h_sync = value & 0x00FF0FFF;
  1181. break;
  1182. case SM501_DC_PANEL_V_TOT:
  1183. s->dc_panel_v_total = value & 0x0FFF0FFF;
  1184. break;
  1185. case SM501_DC_PANEL_V_SYNC:
  1186. s->dc_panel_v_sync = value & 0x003F0FFF;
  1187. break;
  1188. case SM501_DC_PANEL_HWC_ADDR:
  1189. value &= 0x8FFFFFF0;
  1190. if (value != s->dc_panel_hwc_addr) {
  1191. hwc_invalidate(s, 0);
  1192. s->dc_panel_hwc_addr = value;
  1193. }
  1194. break;
  1195. case SM501_DC_PANEL_HWC_LOC:
  1196. value &= 0x0FFF0FFF;
  1197. if (value != s->dc_panel_hwc_location) {
  1198. hwc_invalidate(s, 0);
  1199. s->dc_panel_hwc_location = value;
  1200. }
  1201. break;
  1202. case SM501_DC_PANEL_HWC_COLOR_1_2:
  1203. s->dc_panel_hwc_color_1_2 = value;
  1204. break;
  1205. case SM501_DC_PANEL_HWC_COLOR_3:
  1206. s->dc_panel_hwc_color_3 = value & 0x0000FFFF;
  1207. break;
  1208. case SM501_DC_VIDEO_CONTROL:
  1209. s->dc_video_control = value & 0x00037FFF;
  1210. break;
  1211. case SM501_DC_CRT_CONTROL:
  1212. s->dc_crt_control = value & 0x0003FFFF;
  1213. break;
  1214. case SM501_DC_CRT_FB_ADDR:
  1215. s->dc_crt_fb_addr = value & 0x8FFFFFF0;
  1216. if (value & 0x8000000) {
  1217. qemu_log_mask(LOG_UNIMP, "CRT external memory not supported\n");
  1218. }
  1219. s->do_full_update = true;
  1220. break;
  1221. case SM501_DC_CRT_FB_OFFSET:
  1222. s->dc_crt_fb_offset = value & 0x3FF03FF0;
  1223. break;
  1224. case SM501_DC_CRT_H_TOT:
  1225. s->dc_crt_h_total = value & 0x0FFF0FFF;
  1226. break;
  1227. case SM501_DC_CRT_H_SYNC:
  1228. s->dc_crt_h_sync = value & 0x00FF0FFF;
  1229. break;
  1230. case SM501_DC_CRT_V_TOT:
  1231. s->dc_crt_v_total = value & 0x0FFF0FFF;
  1232. break;
  1233. case SM501_DC_CRT_V_SYNC:
  1234. s->dc_crt_v_sync = value & 0x003F0FFF;
  1235. break;
  1236. case SM501_DC_CRT_HWC_ADDR:
  1237. value &= 0x8FFFFFF0;
  1238. if (value != s->dc_crt_hwc_addr) {
  1239. hwc_invalidate(s, 1);
  1240. s->dc_crt_hwc_addr = value;
  1241. }
  1242. break;
  1243. case SM501_DC_CRT_HWC_LOC:
  1244. value &= 0x0FFF0FFF;
  1245. if (value != s->dc_crt_hwc_location) {
  1246. hwc_invalidate(s, 1);
  1247. s->dc_crt_hwc_location = value;
  1248. }
  1249. break;
  1250. case SM501_DC_CRT_HWC_COLOR_1_2:
  1251. s->dc_crt_hwc_color_1_2 = value;
  1252. break;
  1253. case SM501_DC_CRT_HWC_COLOR_3:
  1254. s->dc_crt_hwc_color_3 = value & 0x0000FFFF;
  1255. break;
  1256. case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400 * 3 - 4:
  1257. sm501_palette_write(opaque, addr - SM501_DC_PANEL_PALETTE, value);
  1258. break;
  1259. default:
  1260. qemu_log_mask(LOG_UNIMP, "sm501: not implemented disp ctrl register "
  1261. "write. addr=%" HWADDR_PRIx
  1262. ", val=%" PRIx64 "\n", addr, value);
  1263. }
  1264. }
  1265. static const MemoryRegionOps sm501_disp_ctrl_ops = {
  1266. .read = sm501_disp_ctrl_read,
  1267. .write = sm501_disp_ctrl_write,
  1268. .valid = {
  1269. .min_access_size = 4,
  1270. .max_access_size = 4,
  1271. },
  1272. .endianness = DEVICE_LITTLE_ENDIAN,
  1273. };
  1274. static uint64_t sm501_2d_engine_read(void *opaque, hwaddr addr,
  1275. unsigned size)
  1276. {
  1277. SM501State *s = opaque;
  1278. uint32_t ret = 0;
  1279. switch (addr) {
  1280. case SM501_2D_SOURCE:
  1281. ret = s->twoD_source;
  1282. break;
  1283. case SM501_2D_DESTINATION:
  1284. ret = s->twoD_destination;
  1285. break;
  1286. case SM501_2D_DIMENSION:
  1287. ret = s->twoD_dimension;
  1288. break;
  1289. case SM501_2D_CONTROL:
  1290. ret = s->twoD_control;
  1291. break;
  1292. case SM501_2D_PITCH:
  1293. ret = s->twoD_pitch;
  1294. break;
  1295. case SM501_2D_FOREGROUND:
  1296. ret = s->twoD_foreground;
  1297. break;
  1298. case SM501_2D_BACKGROUND:
  1299. ret = s->twoD_background;
  1300. break;
  1301. case SM501_2D_STRETCH:
  1302. ret = s->twoD_stretch;
  1303. break;
  1304. case SM501_2D_COLOR_COMPARE:
  1305. ret = s->twoD_color_compare;
  1306. break;
  1307. case SM501_2D_COLOR_COMPARE_MASK:
  1308. ret = s->twoD_color_compare_mask;
  1309. break;
  1310. case SM501_2D_MASK:
  1311. ret = s->twoD_mask;
  1312. break;
  1313. case SM501_2D_CLIP_TL:
  1314. ret = s->twoD_clip_tl;
  1315. break;
  1316. case SM501_2D_CLIP_BR:
  1317. ret = s->twoD_clip_br;
  1318. break;
  1319. case SM501_2D_MONO_PATTERN_LOW:
  1320. ret = s->twoD_mono_pattern_low;
  1321. break;
  1322. case SM501_2D_MONO_PATTERN_HIGH:
  1323. ret = s->twoD_mono_pattern_high;
  1324. break;
  1325. case SM501_2D_WINDOW_WIDTH:
  1326. ret = s->twoD_window_width;
  1327. break;
  1328. case SM501_2D_SOURCE_BASE:
  1329. ret = s->twoD_source_base;
  1330. break;
  1331. case SM501_2D_DESTINATION_BASE:
  1332. ret = s->twoD_destination_base;
  1333. break;
  1334. case SM501_2D_ALPHA:
  1335. ret = s->twoD_alpha;
  1336. break;
  1337. case SM501_2D_WRAP:
  1338. ret = s->twoD_wrap;
  1339. break;
  1340. case SM501_2D_STATUS:
  1341. ret = 0; /* Should return interrupt status */
  1342. break;
  1343. default:
  1344. qemu_log_mask(LOG_UNIMP, "sm501: not implemented disp ctrl register "
  1345. "read. addr=%" HWADDR_PRIx "\n", addr);
  1346. }
  1347. trace_sm501_2d_engine_read((uint32_t)addr, ret);
  1348. return ret;
  1349. }
  1350. static void sm501_2d_engine_write(void *opaque, hwaddr addr,
  1351. uint64_t value, unsigned size)
  1352. {
  1353. SM501State *s = opaque;
  1354. trace_sm501_2d_engine_write((uint32_t)addr, (uint32_t)value);
  1355. switch (addr) {
  1356. case SM501_2D_SOURCE:
  1357. s->twoD_source = value;
  1358. break;
  1359. case SM501_2D_DESTINATION:
  1360. s->twoD_destination = value;
  1361. break;
  1362. case SM501_2D_DIMENSION:
  1363. s->twoD_dimension = value;
  1364. break;
  1365. case SM501_2D_CONTROL:
  1366. s->twoD_control = value;
  1367. /* do 2d operation if start flag is set. */
  1368. if (value & 0x80000000) {
  1369. sm501_2d_operation(s);
  1370. s->twoD_control &= ~0x80000000; /* start flag down */
  1371. }
  1372. break;
  1373. case SM501_2D_PITCH:
  1374. s->twoD_pitch = value;
  1375. break;
  1376. case SM501_2D_FOREGROUND:
  1377. s->twoD_foreground = value;
  1378. break;
  1379. case SM501_2D_BACKGROUND:
  1380. s->twoD_background = value;
  1381. break;
  1382. case SM501_2D_STRETCH:
  1383. if (((value >> 20) & 3) == 3) {
  1384. value &= ~BIT(20);
  1385. }
  1386. s->twoD_stretch = value;
  1387. break;
  1388. case SM501_2D_COLOR_COMPARE:
  1389. s->twoD_color_compare = value;
  1390. break;
  1391. case SM501_2D_COLOR_COMPARE_MASK:
  1392. s->twoD_color_compare_mask = value;
  1393. break;
  1394. case SM501_2D_MASK:
  1395. s->twoD_mask = value;
  1396. break;
  1397. case SM501_2D_CLIP_TL:
  1398. s->twoD_clip_tl = value;
  1399. break;
  1400. case SM501_2D_CLIP_BR:
  1401. s->twoD_clip_br = value;
  1402. break;
  1403. case SM501_2D_MONO_PATTERN_LOW:
  1404. s->twoD_mono_pattern_low = value;
  1405. break;
  1406. case SM501_2D_MONO_PATTERN_HIGH:
  1407. s->twoD_mono_pattern_high = value;
  1408. break;
  1409. case SM501_2D_WINDOW_WIDTH:
  1410. s->twoD_window_width = value;
  1411. break;
  1412. case SM501_2D_SOURCE_BASE:
  1413. s->twoD_source_base = value;
  1414. break;
  1415. case SM501_2D_DESTINATION_BASE:
  1416. s->twoD_destination_base = value;
  1417. break;
  1418. case SM501_2D_ALPHA:
  1419. s->twoD_alpha = value;
  1420. break;
  1421. case SM501_2D_WRAP:
  1422. s->twoD_wrap = value;
  1423. break;
  1424. case SM501_2D_STATUS:
  1425. /* ignored, writing 0 should clear interrupt status */
  1426. break;
  1427. default:
  1428. qemu_log_mask(LOG_UNIMP, "sm501: not implemented 2d engine register "
  1429. "write. addr=%" HWADDR_PRIx
  1430. ", val=%" PRIx64 "\n", addr, value);
  1431. }
  1432. }
  1433. static const MemoryRegionOps sm501_2d_engine_ops = {
  1434. .read = sm501_2d_engine_read,
  1435. .write = sm501_2d_engine_write,
  1436. .valid = {
  1437. .min_access_size = 4,
  1438. .max_access_size = 4,
  1439. },
  1440. .endianness = DEVICE_LITTLE_ENDIAN,
  1441. };
  1442. /* draw line functions for all console modes */
  1443. typedef void draw_line_func(uint8_t *d, const uint8_t *s,
  1444. int width, const uint32_t *pal);
  1445. typedef void draw_hwc_line_func(uint8_t *d, const uint8_t *s,
  1446. int width, const uint8_t *palette,
  1447. int c_x, int c_y);
  1448. static void draw_line8_32(uint8_t *d, const uint8_t *s, int width,
  1449. const uint32_t *pal)
  1450. {
  1451. uint8_t v, r, g, b;
  1452. do {
  1453. v = ldub_p(s);
  1454. r = (pal[v] >> 16) & 0xff;
  1455. g = (pal[v] >> 8) & 0xff;
  1456. b = (pal[v] >> 0) & 0xff;
  1457. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  1458. s++;
  1459. d += 4;
  1460. } while (--width != 0);
  1461. }
  1462. static void draw_line16_32(uint8_t *d, const uint8_t *s, int width,
  1463. const uint32_t *pal)
  1464. {
  1465. uint16_t rgb565;
  1466. uint8_t r, g, b;
  1467. do {
  1468. rgb565 = lduw_le_p(s);
  1469. r = (rgb565 >> 8) & 0xf8;
  1470. g = (rgb565 >> 3) & 0xfc;
  1471. b = (rgb565 << 3) & 0xf8;
  1472. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  1473. s += 2;
  1474. d += 4;
  1475. } while (--width != 0);
  1476. }
  1477. static void draw_line32_32(uint8_t *d, const uint8_t *s, int width,
  1478. const uint32_t *pal)
  1479. {
  1480. uint8_t r, g, b;
  1481. do {
  1482. r = s[2];
  1483. g = s[1];
  1484. b = s[0];
  1485. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  1486. s += 4;
  1487. d += 4;
  1488. } while (--width != 0);
  1489. }
  1490. /**
  1491. * Draw hardware cursor image on the given line.
  1492. */
  1493. static void draw_hwc_line_32(uint8_t *d, const uint8_t *s, int width,
  1494. const uint8_t *palette, int c_x, int c_y)
  1495. {
  1496. int i;
  1497. uint8_t r, g, b, v, bitset = 0;
  1498. /* get cursor position */
  1499. assert(0 <= c_y && c_y < SM501_HWC_HEIGHT);
  1500. s += SM501_HWC_WIDTH * c_y / 4; /* 4 pixels per byte */
  1501. d += c_x * 4;
  1502. for (i = 0; i < SM501_HWC_WIDTH && c_x + i < width; i++) {
  1503. /* get pixel value */
  1504. if (i % 4 == 0) {
  1505. bitset = ldub_p(s);
  1506. s++;
  1507. }
  1508. v = bitset & 3;
  1509. bitset >>= 2;
  1510. /* write pixel */
  1511. if (v) {
  1512. v--;
  1513. r = palette[v * 3 + 0];
  1514. g = palette[v * 3 + 1];
  1515. b = palette[v * 3 + 2];
  1516. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  1517. }
  1518. d += 4;
  1519. }
  1520. }
  1521. static void sm501_update_display(void *opaque)
  1522. {
  1523. SM501State *s = opaque;
  1524. DisplaySurface *surface = qemu_console_surface(s->con);
  1525. DirtyBitmapSnapshot *snap;
  1526. int y, c_x = 0, c_y = 0;
  1527. int crt = (s->dc_crt_control & SM501_DC_CRT_CONTROL_SEL) ? 1 : 0;
  1528. int width = get_width(s, crt);
  1529. int height = get_height(s, crt);
  1530. int src_bpp = get_bpp(s, crt);
  1531. int dst_bpp = surface_bytes_per_pixel(surface);
  1532. draw_line_func *draw_line = NULL;
  1533. draw_hwc_line_func *draw_hwc_line = NULL;
  1534. int full_update = 0;
  1535. int y_start = -1;
  1536. ram_addr_t offset;
  1537. uint32_t *palette;
  1538. uint8_t hwc_palette[3 * 3];
  1539. uint8_t *hwc_src = NULL;
  1540. assert(dst_bpp == 4); /* Output is always 32-bit RGB */
  1541. if (!((crt ? s->dc_crt_control : s->dc_panel_control)
  1542. & SM501_DC_CRT_CONTROL_ENABLE)) {
  1543. return;
  1544. }
  1545. palette = (uint32_t *)(crt ? &s->dc_palette[SM501_DC_CRT_PALETTE -
  1546. SM501_DC_PANEL_PALETTE]
  1547. : &s->dc_palette[0]);
  1548. /* choose draw_line function */
  1549. switch (src_bpp) {
  1550. case 1:
  1551. draw_line = draw_line8_32;
  1552. break;
  1553. case 2:
  1554. draw_line = draw_line16_32;
  1555. break;
  1556. case 4:
  1557. draw_line = draw_line32_32;
  1558. break;
  1559. default:
  1560. qemu_log_mask(LOG_GUEST_ERROR, "sm501: update display"
  1561. "invalid control register value.\n");
  1562. return;
  1563. }
  1564. /* set up to draw hardware cursor */
  1565. if (is_hwc_enabled(s, crt)) {
  1566. /* choose cursor draw line function */
  1567. draw_hwc_line = draw_hwc_line_32;
  1568. hwc_src = get_hwc_address(s, crt);
  1569. c_x = get_hwc_x(s, crt);
  1570. c_y = get_hwc_y(s, crt);
  1571. get_hwc_palette(s, crt, hwc_palette);
  1572. }
  1573. /* adjust console size */
  1574. if (s->last_width != width || s->last_height != height) {
  1575. qemu_console_resize(s->con, width, height);
  1576. surface = qemu_console_surface(s->con);
  1577. s->last_width = width;
  1578. s->last_height = height;
  1579. full_update = 1;
  1580. }
  1581. /* someone else requested a full update */
  1582. if (s->do_full_update) {
  1583. s->do_full_update = false;
  1584. full_update = 1;
  1585. }
  1586. /* draw each line according to conditions */
  1587. offset = get_fb_addr(s, crt);
  1588. snap = memory_region_snapshot_and_clear_dirty(&s->local_mem_region,
  1589. offset, width * height * src_bpp, DIRTY_MEMORY_VGA);
  1590. for (y = 0; y < height; y++, offset += width * src_bpp) {
  1591. int update, update_hwc;
  1592. /* check if hardware cursor is enabled and we're within its range */
  1593. update_hwc = draw_hwc_line && c_y <= y && y < c_y + SM501_HWC_HEIGHT;
  1594. update = full_update || update_hwc;
  1595. /* check dirty flags for each line */
  1596. update |= memory_region_snapshot_get_dirty(&s->local_mem_region, snap,
  1597. offset, width * src_bpp);
  1598. /* draw line and change status */
  1599. if (update) {
  1600. uint8_t *d = surface_data(surface);
  1601. d += y * width * dst_bpp;
  1602. /* draw graphics layer */
  1603. draw_line(d, s->local_mem + offset, width, palette);
  1604. /* draw hardware cursor */
  1605. if (update_hwc) {
  1606. draw_hwc_line(d, hwc_src, width, hwc_palette, c_x, y - c_y);
  1607. }
  1608. if (y_start < 0) {
  1609. y_start = y;
  1610. }
  1611. } else {
  1612. if (y_start >= 0) {
  1613. /* flush to display */
  1614. dpy_gfx_update(s->con, 0, y_start, width, y - y_start);
  1615. y_start = -1;
  1616. }
  1617. }
  1618. }
  1619. g_free(snap);
  1620. /* complete flush to display */
  1621. if (y_start >= 0) {
  1622. dpy_gfx_update(s->con, 0, y_start, width, y - y_start);
  1623. }
  1624. }
  1625. static const GraphicHwOps sm501_ops = {
  1626. .gfx_update = sm501_update_display,
  1627. };
  1628. static void sm501_reset(SM501State *s)
  1629. {
  1630. s->system_control = 0x00100000; /* 2D engine FIFO empty */
  1631. /*
  1632. * Bits 17 (SH), 7 (CDR), 6:5 (Test), 2:0 (Bus) are all supposed
  1633. * to be determined at reset by GPIO lines which set config bits.
  1634. * We hardwire them:
  1635. * SH = 0 : Hitachi Ready Polarity == Active Low
  1636. * CDR = 0 : do not reset clock divider
  1637. * TEST = 0 : Normal mode (not testing the silicon)
  1638. * BUS = 0 : Hitachi SH3/SH4
  1639. */
  1640. s->misc_control = SM501_MISC_DAC_POWER;
  1641. s->gpio_31_0_control = 0;
  1642. s->gpio_63_32_control = 0;
  1643. s->dram_control = 0;
  1644. s->arbitration_control = 0x05146732;
  1645. s->irq_mask = 0;
  1646. s->misc_timing = 0;
  1647. s->power_mode_control = 0;
  1648. s->i2c_byte_count = 0;
  1649. s->i2c_status = 0;
  1650. s->i2c_addr = 0;
  1651. memset(s->i2c_data, 0, 16);
  1652. s->dc_panel_control = 0x00010000; /* FIFO level 3 */
  1653. s->dc_video_control = 0;
  1654. s->dc_crt_control = 0x00010000;
  1655. s->twoD_source = 0;
  1656. s->twoD_destination = 0;
  1657. s->twoD_dimension = 0;
  1658. s->twoD_control = 0;
  1659. s->twoD_pitch = 0;
  1660. s->twoD_foreground = 0;
  1661. s->twoD_background = 0;
  1662. s->twoD_stretch = 0;
  1663. s->twoD_color_compare = 0;
  1664. s->twoD_color_compare_mask = 0;
  1665. s->twoD_mask = 0;
  1666. s->twoD_clip_tl = 0;
  1667. s->twoD_clip_br = 0;
  1668. s->twoD_mono_pattern_low = 0;
  1669. s->twoD_mono_pattern_high = 0;
  1670. s->twoD_window_width = 0;
  1671. s->twoD_source_base = 0;
  1672. s->twoD_destination_base = 0;
  1673. s->twoD_alpha = 0;
  1674. s->twoD_wrap = 0;
  1675. }
  1676. static void sm501_init(SM501State *s, DeviceState *dev,
  1677. uint32_t local_mem_bytes)
  1678. {
  1679. s->local_mem_size_index = get_local_mem_size_index(local_mem_bytes);
  1680. /* local memory */
  1681. memory_region_init_ram(&s->local_mem_region, OBJECT(dev), "sm501.local",
  1682. get_local_mem_size(s), &error_fatal);
  1683. memory_region_set_log(&s->local_mem_region, true, DIRTY_MEMORY_VGA);
  1684. s->local_mem = memory_region_get_ram_ptr(&s->local_mem_region);
  1685. /* i2c */
  1686. s->i2c_bus = i2c_init_bus(dev, "sm501.i2c");
  1687. /* ddc */
  1688. I2CDDCState *ddc = I2CDDC(qdev_new(TYPE_I2CDDC));
  1689. i2c_slave_set_address(I2C_SLAVE(ddc), 0x50);
  1690. qdev_realize_and_unref(DEVICE(ddc), BUS(s->i2c_bus), &error_abort);
  1691. /* mmio */
  1692. memory_region_init(&s->mmio_region, OBJECT(dev), "sm501.mmio", MMIO_SIZE);
  1693. memory_region_init_io(&s->system_config_region, OBJECT(dev),
  1694. &sm501_system_config_ops, s,
  1695. "sm501-system-config", 0x6c);
  1696. memory_region_add_subregion(&s->mmio_region, SM501_SYS_CONFIG,
  1697. &s->system_config_region);
  1698. memory_region_init_io(&s->i2c_region, OBJECT(dev), &sm501_i2c_ops, s,
  1699. "sm501-i2c", 0x14);
  1700. memory_region_add_subregion(&s->mmio_region, SM501_I2C, &s->i2c_region);
  1701. memory_region_init_io(&s->disp_ctrl_region, OBJECT(dev),
  1702. &sm501_disp_ctrl_ops, s,
  1703. "sm501-disp-ctrl", 0x1000);
  1704. memory_region_add_subregion(&s->mmio_region, SM501_DC,
  1705. &s->disp_ctrl_region);
  1706. memory_region_init_io(&s->twoD_engine_region, OBJECT(dev),
  1707. &sm501_2d_engine_ops, s,
  1708. "sm501-2d-engine", 0x54);
  1709. memory_region_add_subregion(&s->mmio_region, SM501_2D_ENGINE,
  1710. &s->twoD_engine_region);
  1711. /* create qemu graphic console */
  1712. s->con = graphic_console_init(dev, 0, &sm501_ops, s);
  1713. }
  1714. static const VMStateDescription vmstate_sm501_state = {
  1715. .name = "sm501-state",
  1716. .version_id = 1,
  1717. .minimum_version_id = 1,
  1718. .fields = (VMStateField[]) {
  1719. VMSTATE_UINT32(local_mem_size_index, SM501State),
  1720. VMSTATE_UINT32(system_control, SM501State),
  1721. VMSTATE_UINT32(misc_control, SM501State),
  1722. VMSTATE_UINT32(gpio_31_0_control, SM501State),
  1723. VMSTATE_UINT32(gpio_63_32_control, SM501State),
  1724. VMSTATE_UINT32(dram_control, SM501State),
  1725. VMSTATE_UINT32(arbitration_control, SM501State),
  1726. VMSTATE_UINT32(irq_mask, SM501State),
  1727. VMSTATE_UINT32(misc_timing, SM501State),
  1728. VMSTATE_UINT32(power_mode_control, SM501State),
  1729. VMSTATE_UINT32(uart0_ier, SM501State),
  1730. VMSTATE_UINT32(uart0_lcr, SM501State),
  1731. VMSTATE_UINT32(uart0_mcr, SM501State),
  1732. VMSTATE_UINT32(uart0_scr, SM501State),
  1733. VMSTATE_UINT8_ARRAY(dc_palette, SM501State, DC_PALETTE_ENTRIES),
  1734. VMSTATE_UINT32(dc_panel_control, SM501State),
  1735. VMSTATE_UINT32(dc_panel_panning_control, SM501State),
  1736. VMSTATE_UINT32(dc_panel_fb_addr, SM501State),
  1737. VMSTATE_UINT32(dc_panel_fb_offset, SM501State),
  1738. VMSTATE_UINT32(dc_panel_fb_width, SM501State),
  1739. VMSTATE_UINT32(dc_panel_fb_height, SM501State),
  1740. VMSTATE_UINT32(dc_panel_tl_location, SM501State),
  1741. VMSTATE_UINT32(dc_panel_br_location, SM501State),
  1742. VMSTATE_UINT32(dc_panel_h_total, SM501State),
  1743. VMSTATE_UINT32(dc_panel_h_sync, SM501State),
  1744. VMSTATE_UINT32(dc_panel_v_total, SM501State),
  1745. VMSTATE_UINT32(dc_panel_v_sync, SM501State),
  1746. VMSTATE_UINT32(dc_panel_hwc_addr, SM501State),
  1747. VMSTATE_UINT32(dc_panel_hwc_location, SM501State),
  1748. VMSTATE_UINT32(dc_panel_hwc_color_1_2, SM501State),
  1749. VMSTATE_UINT32(dc_panel_hwc_color_3, SM501State),
  1750. VMSTATE_UINT32(dc_video_control, SM501State),
  1751. VMSTATE_UINT32(dc_crt_control, SM501State),
  1752. VMSTATE_UINT32(dc_crt_fb_addr, SM501State),
  1753. VMSTATE_UINT32(dc_crt_fb_offset, SM501State),
  1754. VMSTATE_UINT32(dc_crt_h_total, SM501State),
  1755. VMSTATE_UINT32(dc_crt_h_sync, SM501State),
  1756. VMSTATE_UINT32(dc_crt_v_total, SM501State),
  1757. VMSTATE_UINT32(dc_crt_v_sync, SM501State),
  1758. VMSTATE_UINT32(dc_crt_hwc_addr, SM501State),
  1759. VMSTATE_UINT32(dc_crt_hwc_location, SM501State),
  1760. VMSTATE_UINT32(dc_crt_hwc_color_1_2, SM501State),
  1761. VMSTATE_UINT32(dc_crt_hwc_color_3, SM501State),
  1762. VMSTATE_UINT32(twoD_source, SM501State),
  1763. VMSTATE_UINT32(twoD_destination, SM501State),
  1764. VMSTATE_UINT32(twoD_dimension, SM501State),
  1765. VMSTATE_UINT32(twoD_control, SM501State),
  1766. VMSTATE_UINT32(twoD_pitch, SM501State),
  1767. VMSTATE_UINT32(twoD_foreground, SM501State),
  1768. VMSTATE_UINT32(twoD_background, SM501State),
  1769. VMSTATE_UINT32(twoD_stretch, SM501State),
  1770. VMSTATE_UINT32(twoD_color_compare, SM501State),
  1771. VMSTATE_UINT32(twoD_color_compare_mask, SM501State),
  1772. VMSTATE_UINT32(twoD_mask, SM501State),
  1773. VMSTATE_UINT32(twoD_clip_tl, SM501State),
  1774. VMSTATE_UINT32(twoD_clip_br, SM501State),
  1775. VMSTATE_UINT32(twoD_mono_pattern_low, SM501State),
  1776. VMSTATE_UINT32(twoD_mono_pattern_high, SM501State),
  1777. VMSTATE_UINT32(twoD_window_width, SM501State),
  1778. VMSTATE_UINT32(twoD_source_base, SM501State),
  1779. VMSTATE_UINT32(twoD_destination_base, SM501State),
  1780. VMSTATE_UINT32(twoD_alpha, SM501State),
  1781. VMSTATE_UINT32(twoD_wrap, SM501State),
  1782. /* Added in version 2 */
  1783. VMSTATE_UINT8(i2c_byte_count, SM501State),
  1784. VMSTATE_UINT8(i2c_status, SM501State),
  1785. VMSTATE_UINT8(i2c_addr, SM501State),
  1786. VMSTATE_UINT8_ARRAY(i2c_data, SM501State, 16),
  1787. VMSTATE_END_OF_LIST()
  1788. }
  1789. };
  1790. #define TYPE_SYSBUS_SM501 "sysbus-sm501"
  1791. OBJECT_DECLARE_SIMPLE_TYPE(SM501SysBusState, SYSBUS_SM501)
  1792. struct SM501SysBusState {
  1793. /*< private >*/
  1794. SysBusDevice parent_obj;
  1795. /*< public >*/
  1796. SM501State state;
  1797. uint32_t vram_size;
  1798. SerialMM serial;
  1799. OHCISysBusState ohci;
  1800. };
  1801. static void sm501_realize_sysbus(DeviceState *dev, Error **errp)
  1802. {
  1803. SM501SysBusState *s = SYSBUS_SM501(dev);
  1804. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  1805. MemoryRegion *mr;
  1806. sm501_init(&s->state, dev, s->vram_size);
  1807. if (get_local_mem_size(&s->state) != s->vram_size) {
  1808. error_setg(errp, "Invalid VRAM size, nearest valid size is %" PRIu32,
  1809. get_local_mem_size(&s->state));
  1810. return;
  1811. }
  1812. sysbus_init_mmio(sbd, &s->state.local_mem_region);
  1813. sysbus_init_mmio(sbd, &s->state.mmio_region);
  1814. /* bridge to usb host emulation module */
  1815. sysbus_realize_and_unref(SYS_BUS_DEVICE(&s->ohci), &error_fatal);
  1816. memory_region_add_subregion(&s->state.mmio_region, SM501_USB_HOST,
  1817. sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->ohci), 0));
  1818. sysbus_pass_irq(sbd, SYS_BUS_DEVICE(&s->ohci));
  1819. /* bridge to serial emulation module */
  1820. sysbus_realize(SYS_BUS_DEVICE(&s->serial), &error_fatal);
  1821. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->serial), 0);
  1822. memory_region_add_subregion(&s->state.mmio_region, SM501_UART0, mr);
  1823. /* TODO : chain irq to IRL */
  1824. }
  1825. static Property sm501_sysbus_properties[] = {
  1826. DEFINE_PROP_UINT32("vram-size", SM501SysBusState, vram_size, 0),
  1827. DEFINE_PROP_UINT8("x-pixman", SM501SysBusState, state.use_pixman, 7),
  1828. DEFINE_PROP_END_OF_LIST(),
  1829. };
  1830. static void sm501_reset_sysbus(DeviceState *dev)
  1831. {
  1832. SM501SysBusState *s = SYSBUS_SM501(dev);
  1833. sm501_reset(&s->state);
  1834. }
  1835. static const VMStateDescription vmstate_sm501_sysbus = {
  1836. .name = TYPE_SYSBUS_SM501,
  1837. .version_id = 2,
  1838. .minimum_version_id = 2,
  1839. .fields = (VMStateField[]) {
  1840. VMSTATE_STRUCT(state, SM501SysBusState, 1,
  1841. vmstate_sm501_state, SM501State),
  1842. VMSTATE_END_OF_LIST()
  1843. }
  1844. };
  1845. static void sm501_sysbus_class_init(ObjectClass *klass, void *data)
  1846. {
  1847. DeviceClass *dc = DEVICE_CLASS(klass);
  1848. dc->realize = sm501_realize_sysbus;
  1849. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  1850. dc->desc = "SM501 Multimedia Companion";
  1851. device_class_set_props(dc, sm501_sysbus_properties);
  1852. dc->reset = sm501_reset_sysbus;
  1853. dc->vmsd = &vmstate_sm501_sysbus;
  1854. }
  1855. static void sm501_sysbus_init(Object *o)
  1856. {
  1857. SM501SysBusState *sm501 = SYSBUS_SM501(o);
  1858. OHCISysBusState *ohci = &sm501->ohci;
  1859. SerialMM *smm = &sm501->serial;
  1860. object_initialize_child(o, "ohci", ohci, TYPE_SYSBUS_OHCI);
  1861. object_property_add_alias(o, "dma-offset", OBJECT(ohci), "dma-offset");
  1862. qdev_prop_set_uint32(DEVICE(ohci), "num-ports", 2);
  1863. object_initialize_child(o, "serial", smm, TYPE_SERIAL_MM);
  1864. qdev_set_legacy_instance_id(DEVICE(smm), SM501_UART0, 2);
  1865. qdev_prop_set_uint8(DEVICE(smm), "regshift", 2);
  1866. qdev_prop_set_uint8(DEVICE(smm), "endianness", DEVICE_LITTLE_ENDIAN);
  1867. object_property_add_alias(o, "chardev", OBJECT(smm), "chardev");
  1868. }
  1869. static const TypeInfo sm501_sysbus_info = {
  1870. .name = TYPE_SYSBUS_SM501,
  1871. .parent = TYPE_SYS_BUS_DEVICE,
  1872. .instance_size = sizeof(SM501SysBusState),
  1873. .class_init = sm501_sysbus_class_init,
  1874. .instance_init = sm501_sysbus_init,
  1875. };
  1876. #define TYPE_PCI_SM501 "sm501"
  1877. OBJECT_DECLARE_SIMPLE_TYPE(SM501PCIState, PCI_SM501)
  1878. struct SM501PCIState {
  1879. /*< private >*/
  1880. PCIDevice parent_obj;
  1881. /*< public >*/
  1882. SM501State state;
  1883. uint32_t vram_size;
  1884. };
  1885. static void sm501_realize_pci(PCIDevice *dev, Error **errp)
  1886. {
  1887. SM501PCIState *s = PCI_SM501(dev);
  1888. sm501_init(&s->state, DEVICE(dev), s->vram_size);
  1889. if (get_local_mem_size(&s->state) != s->vram_size) {
  1890. error_setg(errp, "Invalid VRAM size, nearest valid size is %" PRIu32,
  1891. get_local_mem_size(&s->state));
  1892. return;
  1893. }
  1894. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
  1895. &s->state.local_mem_region);
  1896. pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
  1897. &s->state.mmio_region);
  1898. }
  1899. static Property sm501_pci_properties[] = {
  1900. DEFINE_PROP_UINT32("vram-size", SM501PCIState, vram_size, 64 * MiB),
  1901. DEFINE_PROP_UINT8("x-pixman", SM501PCIState, state.use_pixman, 7),
  1902. DEFINE_PROP_END_OF_LIST(),
  1903. };
  1904. static void sm501_reset_pci(DeviceState *dev)
  1905. {
  1906. SM501PCIState *s = PCI_SM501(dev);
  1907. sm501_reset(&s->state);
  1908. /* Bits 2:0 of misc_control register is 001 for PCI */
  1909. s->state.misc_control |= 1;
  1910. }
  1911. static const VMStateDescription vmstate_sm501_pci = {
  1912. .name = TYPE_PCI_SM501,
  1913. .version_id = 2,
  1914. .minimum_version_id = 2,
  1915. .fields = (VMStateField[]) {
  1916. VMSTATE_PCI_DEVICE(parent_obj, SM501PCIState),
  1917. VMSTATE_STRUCT(state, SM501PCIState, 1,
  1918. vmstate_sm501_state, SM501State),
  1919. VMSTATE_END_OF_LIST()
  1920. }
  1921. };
  1922. static void sm501_pci_class_init(ObjectClass *klass, void *data)
  1923. {
  1924. DeviceClass *dc = DEVICE_CLASS(klass);
  1925. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  1926. k->realize = sm501_realize_pci;
  1927. k->vendor_id = PCI_VENDOR_ID_SILICON_MOTION;
  1928. k->device_id = PCI_DEVICE_ID_SM501;
  1929. k->class_id = PCI_CLASS_DISPLAY_OTHER;
  1930. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  1931. dc->desc = "SM501 Display Controller";
  1932. device_class_set_props(dc, sm501_pci_properties);
  1933. dc->reset = sm501_reset_pci;
  1934. dc->hotpluggable = false;
  1935. dc->vmsd = &vmstate_sm501_pci;
  1936. }
  1937. static void sm501_pci_init(Object *o)
  1938. {
  1939. object_property_set_description(o, "x-pixman", "Use pixman for: "
  1940. "1: fill, 2: blit, 4: overlap blit");
  1941. }
  1942. static const TypeInfo sm501_pci_info = {
  1943. .name = TYPE_PCI_SM501,
  1944. .parent = TYPE_PCI_DEVICE,
  1945. .instance_size = sizeof(SM501PCIState),
  1946. .class_init = sm501_pci_class_init,
  1947. .instance_init = sm501_pci_init,
  1948. .interfaces = (InterfaceInfo[]) {
  1949. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  1950. { },
  1951. },
  1952. };
  1953. static void sm501_register_types(void)
  1954. {
  1955. type_register_static(&sm501_sysbus_info);
  1956. type_register_static(&sm501_pci_info);
  1957. }
  1958. type_init(sm501_register_types)