sm501.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  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-mm.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. #ifdef CONFIG_PIXMAN
  365. #define DEFAULT_X_PIXMAN 7
  366. #else
  367. #define DEFAULT_X_PIXMAN 0
  368. #endif
  369. /* SM501 local memory size taken from "linux/drivers/mfd/sm501.c" */
  370. static const uint32_t sm501_mem_local_size[] = {
  371. [0] = 4 * MiB,
  372. [1] = 8 * MiB,
  373. [2] = 16 * MiB,
  374. [3] = 32 * MiB,
  375. [4] = 64 * MiB,
  376. [5] = 2 * MiB,
  377. };
  378. #define get_local_mem_size(s) sm501_mem_local_size[(s)->local_mem_size_index]
  379. typedef struct SM501State {
  380. /* graphic console status */
  381. QemuConsole *con;
  382. /* status & internal resources */
  383. uint32_t local_mem_size_index;
  384. uint8_t *local_mem;
  385. MemoryRegion local_mem_region;
  386. MemoryRegion mmio_region;
  387. MemoryRegion system_config_region;
  388. MemoryRegion i2c_region;
  389. MemoryRegion disp_ctrl_region;
  390. MemoryRegion twoD_engine_region;
  391. uint32_t last_width;
  392. uint32_t last_height;
  393. bool do_full_update; /* perform a full update next time */
  394. uint8_t use_pixman;
  395. I2CBus *i2c_bus;
  396. /* mmio registers */
  397. uint32_t system_control;
  398. uint32_t misc_control;
  399. uint32_t gpio_31_0_control;
  400. uint32_t gpio_63_32_control;
  401. uint32_t dram_control;
  402. uint32_t arbitration_control;
  403. uint32_t irq_mask;
  404. uint32_t misc_timing;
  405. uint32_t power_mode_control;
  406. uint8_t i2c_byte_count;
  407. uint8_t i2c_status;
  408. uint8_t i2c_addr;
  409. uint8_t i2c_data[16];
  410. uint32_t uart0_ier;
  411. uint32_t uart0_lcr;
  412. uint32_t uart0_mcr;
  413. uint32_t uart0_scr;
  414. uint8_t dc_palette[DC_PALETTE_ENTRIES];
  415. uint32_t dc_panel_control;
  416. uint32_t dc_panel_panning_control;
  417. uint32_t dc_panel_fb_addr;
  418. uint32_t dc_panel_fb_offset;
  419. uint32_t dc_panel_fb_width;
  420. uint32_t dc_panel_fb_height;
  421. uint32_t dc_panel_tl_location;
  422. uint32_t dc_panel_br_location;
  423. uint32_t dc_panel_h_total;
  424. uint32_t dc_panel_h_sync;
  425. uint32_t dc_panel_v_total;
  426. uint32_t dc_panel_v_sync;
  427. uint32_t dc_panel_hwc_addr;
  428. uint32_t dc_panel_hwc_location;
  429. uint32_t dc_panel_hwc_color_1_2;
  430. uint32_t dc_panel_hwc_color_3;
  431. uint32_t dc_video_control;
  432. uint32_t dc_crt_control;
  433. uint32_t dc_crt_fb_addr;
  434. uint32_t dc_crt_fb_offset;
  435. uint32_t dc_crt_h_total;
  436. uint32_t dc_crt_h_sync;
  437. uint32_t dc_crt_v_total;
  438. uint32_t dc_crt_v_sync;
  439. uint32_t dc_crt_hwc_addr;
  440. uint32_t dc_crt_hwc_location;
  441. uint32_t dc_crt_hwc_color_1_2;
  442. uint32_t dc_crt_hwc_color_3;
  443. uint32_t twoD_source;
  444. uint32_t twoD_destination;
  445. uint32_t twoD_dimension;
  446. uint32_t twoD_control;
  447. uint32_t twoD_pitch;
  448. uint32_t twoD_foreground;
  449. uint32_t twoD_background;
  450. uint32_t twoD_stretch;
  451. uint32_t twoD_color_compare;
  452. uint32_t twoD_color_compare_mask;
  453. uint32_t twoD_mask;
  454. uint32_t twoD_clip_tl;
  455. uint32_t twoD_clip_br;
  456. uint32_t twoD_mono_pattern_low;
  457. uint32_t twoD_mono_pattern_high;
  458. uint32_t twoD_window_width;
  459. uint32_t twoD_source_base;
  460. uint32_t twoD_destination_base;
  461. uint32_t twoD_alpha;
  462. uint32_t twoD_wrap;
  463. } SM501State;
  464. static uint32_t get_local_mem_size_index(uint32_t size)
  465. {
  466. uint32_t norm_size = 0;
  467. int i, index = 0;
  468. for (i = 0; i < ARRAY_SIZE(sm501_mem_local_size); i++) {
  469. uint32_t new_size = sm501_mem_local_size[i];
  470. if (new_size >= size) {
  471. if (norm_size == 0 || norm_size > new_size) {
  472. norm_size = new_size;
  473. index = i;
  474. }
  475. }
  476. }
  477. return index;
  478. }
  479. static ram_addr_t get_fb_addr(SM501State *s, int crt)
  480. {
  481. return (crt ? s->dc_crt_fb_addr : s->dc_panel_fb_addr) & 0x3FFFFF0;
  482. }
  483. static inline int get_width(SM501State *s, int crt)
  484. {
  485. int width = crt ? s->dc_crt_h_total : s->dc_panel_h_total;
  486. return (width & 0x00000FFF) + 1;
  487. }
  488. static inline int get_height(SM501State *s, int crt)
  489. {
  490. int height = crt ? s->dc_crt_v_total : s->dc_panel_v_total;
  491. return (height & 0x00000FFF) + 1;
  492. }
  493. static inline int get_bpp(SM501State *s, int crt)
  494. {
  495. int bpp = crt ? s->dc_crt_control : s->dc_panel_control;
  496. return 1 << (bpp & 3);
  497. }
  498. /**
  499. * Check the availability of hardware cursor.
  500. * @param crt 0 for PANEL, 1 for CRT.
  501. */
  502. static inline int is_hwc_enabled(SM501State *state, int crt)
  503. {
  504. uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
  505. return addr & SM501_HWC_EN;
  506. }
  507. /**
  508. * Get the address which holds cursor pattern data.
  509. * @param crt 0 for PANEL, 1 for CRT.
  510. */
  511. static inline uint8_t *get_hwc_address(SM501State *state, int crt)
  512. {
  513. uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
  514. return state->local_mem + (addr & 0x03FFFFF0);
  515. }
  516. /**
  517. * Get the cursor position in y coordinate.
  518. * @param crt 0 for PANEL, 1 for CRT.
  519. */
  520. static inline uint32_t get_hwc_y(SM501State *state, int crt)
  521. {
  522. uint32_t location = crt ? state->dc_crt_hwc_location
  523. : state->dc_panel_hwc_location;
  524. return (location & 0x07FF0000) >> 16;
  525. }
  526. /**
  527. * Get the cursor position in x coordinate.
  528. * @param crt 0 for PANEL, 1 for CRT.
  529. */
  530. static inline uint32_t get_hwc_x(SM501State *state, int crt)
  531. {
  532. uint32_t location = crt ? state->dc_crt_hwc_location
  533. : state->dc_panel_hwc_location;
  534. return location & 0x000007FF;
  535. }
  536. /**
  537. * Get the hardware cursor palette.
  538. * @param crt 0 for PANEL, 1 for CRT.
  539. * @param palette pointer to a [3 * 3] array to store color values in
  540. */
  541. static inline void get_hwc_palette(SM501State *state, int crt, uint8_t *palette)
  542. {
  543. int i;
  544. uint32_t color_reg;
  545. uint16_t rgb565;
  546. for (i = 0; i < 3; i++) {
  547. if (i + 1 == 3) {
  548. color_reg = crt ? state->dc_crt_hwc_color_3
  549. : state->dc_panel_hwc_color_3;
  550. } else {
  551. color_reg = crt ? state->dc_crt_hwc_color_1_2
  552. : state->dc_panel_hwc_color_1_2;
  553. }
  554. if (i + 1 == 2) {
  555. rgb565 = (color_reg >> 16) & 0xFFFF;
  556. } else {
  557. rgb565 = color_reg & 0xFFFF;
  558. }
  559. palette[i * 3 + 0] = ((rgb565 >> 11) * 527 + 23) >> 6; /* r */
  560. palette[i * 3 + 1] = (((rgb565 >> 5) & 0x3f) * 259 + 33) >> 6; /* g */
  561. palette[i * 3 + 2] = ((rgb565 & 0x1f) * 527 + 23) >> 6; /* b */
  562. }
  563. }
  564. static inline void hwc_invalidate(SM501State *s, int crt)
  565. {
  566. int w = get_width(s, crt);
  567. int h = get_height(s, crt);
  568. int bpp = get_bpp(s, crt);
  569. int start = get_hwc_y(s, crt);
  570. int end = MIN(h, start + SM501_HWC_HEIGHT) + 1;
  571. start *= w * bpp;
  572. end *= w * bpp;
  573. memory_region_set_dirty(&s->local_mem_region,
  574. get_fb_addr(s, crt) + start, end - start);
  575. }
  576. static void sm501_2d_operation(SM501State *s)
  577. {
  578. int cmd = (s->twoD_control >> 16) & 0x1F;
  579. int rtl = s->twoD_control & BIT(27);
  580. int format = (s->twoD_stretch >> 20) & 3;
  581. int bypp = 1 << format; /* bytes per pixel */
  582. int rop_mode = (s->twoD_control >> 15) & 1; /* 1 for rop2, else rop3 */
  583. /* 1 if rop2 source is the pattern, otherwise the source is the bitmap */
  584. int rop2_source_is_pattern = (s->twoD_control >> 14) & 1;
  585. int rop = s->twoD_control & 0xFF;
  586. unsigned int dst_x = (s->twoD_destination >> 16) & 0x01FFF;
  587. unsigned int dst_y = s->twoD_destination & 0xFFFF;
  588. unsigned int width = (s->twoD_dimension >> 16) & 0x1FFF;
  589. unsigned int height = s->twoD_dimension & 0xFFFF;
  590. uint32_t dst_base = s->twoD_destination_base & 0x03FFFFFF;
  591. unsigned int dst_pitch = (s->twoD_pitch >> 16) & 0x1FFF;
  592. int crt = (s->dc_crt_control & SM501_DC_CRT_CONTROL_SEL) ? 1 : 0;
  593. int fb_len = get_width(s, crt) * get_height(s, crt) * get_bpp(s, crt);
  594. bool overlap = false, fallback = false;
  595. if ((s->twoD_stretch >> 16) & 0xF) {
  596. qemu_log_mask(LOG_UNIMP, "sm501: only XY addressing is supported.\n");
  597. return;
  598. }
  599. if (s->twoD_source_base & BIT(27) || s->twoD_destination_base & BIT(27)) {
  600. qemu_log_mask(LOG_UNIMP, "sm501: only local memory is supported.\n");
  601. return;
  602. }
  603. if (!dst_pitch) {
  604. qemu_log_mask(LOG_GUEST_ERROR, "sm501: Zero dest pitch.\n");
  605. return;
  606. }
  607. if (!width || !height) {
  608. qemu_log_mask(LOG_GUEST_ERROR, "sm501: Zero size 2D op.\n");
  609. return;
  610. }
  611. if (rtl) {
  612. dst_x -= width - 1;
  613. dst_y -= height - 1;
  614. }
  615. if (dst_base >= get_local_mem_size(s) ||
  616. dst_base + (dst_x + width + (dst_y + height) * dst_pitch) * bypp >=
  617. get_local_mem_size(s)) {
  618. qemu_log_mask(LOG_GUEST_ERROR, "sm501: 2D op dest is outside vram.\n");
  619. return;
  620. }
  621. switch (cmd) {
  622. case 0: /* BitBlt */
  623. {
  624. unsigned int src_x = (s->twoD_source >> 16) & 0x01FFF;
  625. unsigned int src_y = s->twoD_source & 0xFFFF;
  626. uint32_t src_base = s->twoD_source_base & 0x03FFFFFF;
  627. unsigned int src_pitch = s->twoD_pitch & 0x1FFF;
  628. if (!src_pitch) {
  629. qemu_log_mask(LOG_GUEST_ERROR, "sm501: Zero src pitch.\n");
  630. return;
  631. }
  632. if (rtl) {
  633. src_x -= width - 1;
  634. src_y -= height - 1;
  635. }
  636. if (src_base >= get_local_mem_size(s) ||
  637. src_base + (src_x + width + (src_y + height) * src_pitch) * bypp >=
  638. get_local_mem_size(s)) {
  639. qemu_log_mask(LOG_GUEST_ERROR,
  640. "sm501: 2D op src is outside vram.\n");
  641. return;
  642. }
  643. if ((rop_mode && rop == 0x5) || (!rop_mode && rop == 0x55)) {
  644. /* DSTINVERT, is there a way to do this with pixman? */
  645. unsigned int x, y, i;
  646. uint8_t *d = s->local_mem + dst_base;
  647. for (y = 0; y < height; y++) {
  648. i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
  649. for (x = 0; x < width; x++, i += bypp) {
  650. stn_he_p(&d[i], bypp, ~ldn_he_p(&d[i], bypp));
  651. }
  652. }
  653. } else if (!rop_mode && rop == 0x99) {
  654. /* DSxn, is there a way to do this with pixman? */
  655. unsigned int x, y, i, j;
  656. uint8_t *sp = s->local_mem + src_base;
  657. uint8_t *d = s->local_mem + dst_base;
  658. for (y = 0; y < height; y++) {
  659. i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
  660. j = (src_x + (src_y + y) * src_pitch) * bypp;
  661. for (x = 0; x < width; x++, i += bypp, j += bypp) {
  662. stn_he_p(&d[i], bypp,
  663. ~(ldn_he_p(&sp[j], bypp) ^ ldn_he_p(&d[i], bypp)));
  664. }
  665. }
  666. } else if (!rop_mode && rop == 0xee) {
  667. /* SRCPAINT, is there a way to do this with pixman? */
  668. unsigned int x, y, i, j;
  669. uint8_t *sp = s->local_mem + src_base;
  670. uint8_t *d = s->local_mem + dst_base;
  671. for (y = 0; y < height; y++) {
  672. i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
  673. j = (src_x + (src_y + y) * src_pitch) * bypp;
  674. for (x = 0; x < width; x++, i += bypp, j += bypp) {
  675. stn_he_p(&d[i], bypp,
  676. ldn_he_p(&sp[j], bypp) | ldn_he_p(&d[i], bypp));
  677. }
  678. }
  679. } else {
  680. /* Do copy src for unimplemented ops, better than unpainted area */
  681. if ((rop_mode && (rop != 0xc || rop2_source_is_pattern)) ||
  682. (!rop_mode && rop != 0xcc)) {
  683. qemu_log_mask(LOG_UNIMP,
  684. "sm501: rop%d op %x%s not implemented\n",
  685. (rop_mode ? 2 : 3), rop,
  686. (rop2_source_is_pattern ?
  687. " with pattern source" : ""));
  688. }
  689. /* Ignore no-op blits, some guests seem to do this */
  690. if (src_base == dst_base && src_pitch == dst_pitch &&
  691. src_x == dst_x && src_y == dst_y) {
  692. break;
  693. }
  694. /* Some clients also do 1 pixel blits, avoid overhead for these */
  695. if (width == 1 && height == 1) {
  696. unsigned int si = (src_x + src_y * src_pitch) * bypp;
  697. unsigned int di = (dst_x + dst_y * dst_pitch) * bypp;
  698. stn_he_p(&s->local_mem[dst_base + di], bypp,
  699. ldn_he_p(&s->local_mem[src_base + si], bypp));
  700. break;
  701. }
  702. /* If reverse blit do simple check for overlaps */
  703. if (rtl && src_base == dst_base && src_pitch == dst_pitch) {
  704. overlap = (src_x < dst_x + width && src_x + width > dst_x &&
  705. src_y < dst_y + height && src_y + height > dst_y);
  706. } else if (rtl) {
  707. unsigned int sb, se, db, de;
  708. sb = src_base + (src_x + src_y * src_pitch) * bypp;
  709. se = sb + (width + (height - 1) * src_pitch) * bypp;
  710. db = dst_base + (dst_x + dst_y * dst_pitch) * bypp;
  711. de = db + (width + (height - 1) * dst_pitch) * bypp;
  712. overlap = (db < se && sb < de);
  713. }
  714. #ifdef CONFIG_PIXMAN
  715. if (overlap && (s->use_pixman & BIT(2))) {
  716. /* pixman can't do reverse blit: copy via temporary */
  717. int tmp_stride = DIV_ROUND_UP(width * bypp, sizeof(uint32_t));
  718. static uint32_t tmp_buf[16384];
  719. uint32_t *tmp = tmp_buf;
  720. if (tmp_stride * sizeof(uint32_t) * height > sizeof(tmp_buf)) {
  721. tmp = g_malloc(tmp_stride * sizeof(uint32_t) * height);
  722. }
  723. fallback = !pixman_blt((uint32_t *)&s->local_mem[src_base],
  724. tmp,
  725. src_pitch * bypp / sizeof(uint32_t),
  726. tmp_stride,
  727. 8 * bypp, 8 * bypp,
  728. src_x, src_y, 0, 0, width, height);
  729. if (!fallback) {
  730. fallback = !pixman_blt(tmp,
  731. (uint32_t *)&s->local_mem[dst_base],
  732. tmp_stride,
  733. dst_pitch * bypp / sizeof(uint32_t),
  734. 8 * bypp, 8 * bypp,
  735. 0, 0, dst_x, dst_y, width, height);
  736. }
  737. if (tmp != tmp_buf) {
  738. g_free(tmp);
  739. }
  740. } else if (!overlap && (s->use_pixman & BIT(1))) {
  741. fallback = !pixman_blt((uint32_t *)&s->local_mem[src_base],
  742. (uint32_t *)&s->local_mem[dst_base],
  743. src_pitch * bypp / sizeof(uint32_t),
  744. dst_pitch * bypp / sizeof(uint32_t),
  745. 8 * bypp, 8 * bypp, src_x, src_y,
  746. dst_x, dst_y, width, height);
  747. } else
  748. #endif
  749. {
  750. fallback = true;
  751. }
  752. if (fallback) {
  753. uint8_t *sp = s->local_mem + src_base;
  754. uint8_t *d = s->local_mem + dst_base;
  755. unsigned int y, i, j;
  756. for (y = 0; y < height; y++) {
  757. if (overlap) { /* overlap also means rtl */
  758. i = (dst_y + height - 1 - y) * dst_pitch;
  759. i = (dst_x + i) * bypp;
  760. j = (src_y + height - 1 - y) * src_pitch;
  761. j = (src_x + j) * bypp;
  762. memmove(&d[i], &sp[j], width * bypp);
  763. } else {
  764. i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
  765. j = (src_x + (src_y + y) * src_pitch) * bypp;
  766. memcpy(&d[i], &sp[j], width * bypp);
  767. }
  768. }
  769. }
  770. }
  771. break;
  772. }
  773. case 1: /* Rectangle Fill */
  774. {
  775. uint32_t color = s->twoD_foreground;
  776. if (format == 2) {
  777. color = cpu_to_le32(color);
  778. } else if (format == 1) {
  779. color = cpu_to_le16(color);
  780. }
  781. #ifdef CONFIG_PIXMAN
  782. if (!(s->use_pixman & BIT(0)) || (width == 1 && height == 1) ||
  783. !pixman_fill((uint32_t *)&s->local_mem[dst_base],
  784. dst_pitch * bypp / sizeof(uint32_t), 8 * bypp,
  785. dst_x, dst_y, width, height, color))
  786. #endif
  787. {
  788. /* fallback when pixman failed or we don't want to call it */
  789. uint8_t *d = s->local_mem + dst_base;
  790. unsigned int x, y, i;
  791. for (y = 0; y < height; y++) {
  792. i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
  793. for (x = 0; x < width; x++, i += bypp) {
  794. stn_he_p(&d[i], bypp, color);
  795. }
  796. }
  797. }
  798. break;
  799. }
  800. default:
  801. qemu_log_mask(LOG_UNIMP, "sm501: not implemented 2D operation: %d\n",
  802. cmd);
  803. return;
  804. }
  805. if (dst_base >= get_fb_addr(s, crt) &&
  806. dst_base <= get_fb_addr(s, crt) + fb_len) {
  807. int dst_len = MIN(fb_len, ((dst_y + height - 1) * dst_pitch +
  808. dst_x + width) * bypp);
  809. if (dst_len) {
  810. memory_region_set_dirty(&s->local_mem_region, dst_base, dst_len);
  811. }
  812. }
  813. }
  814. static uint64_t sm501_system_config_read(void *opaque, hwaddr addr,
  815. unsigned size)
  816. {
  817. SM501State *s = opaque;
  818. uint32_t ret = 0;
  819. switch (addr) {
  820. case SM501_SYSTEM_CONTROL:
  821. ret = s->system_control;
  822. break;
  823. case SM501_MISC_CONTROL:
  824. ret = s->misc_control;
  825. break;
  826. case SM501_GPIO31_0_CONTROL:
  827. ret = s->gpio_31_0_control;
  828. break;
  829. case SM501_GPIO63_32_CONTROL:
  830. ret = s->gpio_63_32_control;
  831. break;
  832. case SM501_DEVICEID:
  833. ret = 0x050100A0;
  834. break;
  835. case SM501_DRAM_CONTROL:
  836. ret = (s->dram_control & 0x07F107C0) | s->local_mem_size_index << 13;
  837. break;
  838. case SM501_ARBTRTN_CONTROL:
  839. ret = s->arbitration_control;
  840. break;
  841. case SM501_COMMAND_LIST_STATUS:
  842. ret = 0x00180002; /* FIFOs are empty, everything idle */
  843. break;
  844. case SM501_IRQ_MASK:
  845. ret = s->irq_mask;
  846. break;
  847. case SM501_MISC_TIMING:
  848. /* TODO : simulate gate control */
  849. ret = s->misc_timing;
  850. break;
  851. case SM501_CURRENT_GATE:
  852. /* TODO : simulate gate control */
  853. ret = 0x00021807;
  854. break;
  855. case SM501_CURRENT_CLOCK:
  856. ret = 0x2A1A0A09;
  857. break;
  858. case SM501_POWER_MODE_CONTROL:
  859. ret = s->power_mode_control;
  860. break;
  861. case SM501_ENDIAN_CONTROL:
  862. ret = 0; /* Only default little endian mode is supported */
  863. break;
  864. default:
  865. qemu_log_mask(LOG_UNIMP, "sm501: not implemented system config"
  866. "register read. addr=%" HWADDR_PRIx "\n", addr);
  867. }
  868. trace_sm501_system_config_read(addr, ret);
  869. return ret;
  870. }
  871. static void sm501_system_config_write(void *opaque, hwaddr addr,
  872. uint64_t value, unsigned size)
  873. {
  874. SM501State *s = opaque;
  875. trace_sm501_system_config_write((uint32_t)addr, (uint32_t)value);
  876. switch (addr) {
  877. case SM501_SYSTEM_CONTROL:
  878. s->system_control &= 0x10DB0000;
  879. s->system_control |= value & 0xEF00B8F7;
  880. break;
  881. case SM501_MISC_CONTROL:
  882. s->misc_control &= 0xEF;
  883. s->misc_control |= value & 0xFF7FFF10;
  884. break;
  885. case SM501_GPIO31_0_CONTROL:
  886. s->gpio_31_0_control = value;
  887. break;
  888. case SM501_GPIO63_32_CONTROL:
  889. s->gpio_63_32_control = value & 0xFF80FFFF;
  890. break;
  891. case SM501_DRAM_CONTROL:
  892. s->local_mem_size_index = (value >> 13) & 0x7;
  893. /* TODO : check validity of size change */
  894. s->dram_control &= 0x80000000;
  895. s->dram_control |= value & 0x7FFFFFC3;
  896. break;
  897. case SM501_ARBTRTN_CONTROL:
  898. s->arbitration_control = value & 0x37777777;
  899. break;
  900. case SM501_IRQ_MASK:
  901. s->irq_mask = value & 0xFFDF3F5F;
  902. break;
  903. case SM501_MISC_TIMING:
  904. s->misc_timing = value & 0xF31F1FFF;
  905. break;
  906. case SM501_POWER_MODE_0_GATE:
  907. case SM501_POWER_MODE_1_GATE:
  908. case SM501_POWER_MODE_0_CLOCK:
  909. case SM501_POWER_MODE_1_CLOCK:
  910. /* TODO : simulate gate & clock control */
  911. break;
  912. case SM501_POWER_MODE_CONTROL:
  913. s->power_mode_control = value & 0x00000003;
  914. break;
  915. case SM501_ENDIAN_CONTROL:
  916. if (value & 0x00000001) {
  917. qemu_log_mask(LOG_UNIMP, "sm501: system config big endian mode not"
  918. " implemented.\n");
  919. }
  920. break;
  921. default:
  922. qemu_log_mask(LOG_UNIMP, "sm501: not implemented system config"
  923. "register write. addr=%" HWADDR_PRIx
  924. ", val=%" PRIx64 "\n", addr, value);
  925. }
  926. }
  927. static const MemoryRegionOps sm501_system_config_ops = {
  928. .read = sm501_system_config_read,
  929. .write = sm501_system_config_write,
  930. .valid = {
  931. .min_access_size = 4,
  932. .max_access_size = 4,
  933. },
  934. .endianness = DEVICE_LITTLE_ENDIAN,
  935. };
  936. static uint64_t sm501_i2c_read(void *opaque, hwaddr addr, unsigned size)
  937. {
  938. SM501State *s = opaque;
  939. uint8_t ret = 0;
  940. switch (addr) {
  941. case SM501_I2C_BYTE_COUNT:
  942. ret = s->i2c_byte_count;
  943. break;
  944. case SM501_I2C_STATUS:
  945. ret = s->i2c_status;
  946. break;
  947. case SM501_I2C_SLAVE_ADDRESS:
  948. ret = s->i2c_addr;
  949. break;
  950. case SM501_I2C_DATA ... SM501_I2C_DATA + 15:
  951. ret = s->i2c_data[addr - SM501_I2C_DATA];
  952. break;
  953. default:
  954. qemu_log_mask(LOG_UNIMP, "sm501 i2c : not implemented register read."
  955. " addr=0x%" HWADDR_PRIx "\n", addr);
  956. }
  957. trace_sm501_i2c_read((uint32_t)addr, ret);
  958. return ret;
  959. }
  960. static void sm501_i2c_write(void *opaque, hwaddr addr, uint64_t value,
  961. unsigned size)
  962. {
  963. SM501State *s = opaque;
  964. trace_sm501_i2c_write((uint32_t)addr, (uint32_t)value);
  965. switch (addr) {
  966. case SM501_I2C_BYTE_COUNT:
  967. s->i2c_byte_count = value & 0xf;
  968. break;
  969. case SM501_I2C_CONTROL:
  970. if (value & SM501_I2C_CONTROL_ENABLE) {
  971. if (value & SM501_I2C_CONTROL_START) {
  972. bool is_recv = s->i2c_addr & 1;
  973. int res = i2c_start_transfer(s->i2c_bus,
  974. s->i2c_addr >> 1,
  975. is_recv);
  976. if (res) {
  977. s->i2c_status |= SM501_I2C_STATUS_ERROR;
  978. } else {
  979. int i;
  980. for (i = 0; i <= s->i2c_byte_count; i++) {
  981. if (is_recv) {
  982. s->i2c_data[i] = i2c_recv(s->i2c_bus);
  983. } else if (i2c_send(s->i2c_bus, s->i2c_data[i]) < 0) {
  984. s->i2c_status |= SM501_I2C_STATUS_ERROR;
  985. return;
  986. }
  987. }
  988. if (i) {
  989. s->i2c_status = SM501_I2C_STATUS_COMPLETE;
  990. }
  991. }
  992. } else {
  993. i2c_end_transfer(s->i2c_bus);
  994. s->i2c_status &= ~SM501_I2C_STATUS_ERROR;
  995. }
  996. }
  997. break;
  998. case SM501_I2C_RESET:
  999. if ((value & SM501_I2C_RESET_ERROR) == 0) {
  1000. s->i2c_status &= ~SM501_I2C_STATUS_ERROR;
  1001. }
  1002. break;
  1003. case SM501_I2C_SLAVE_ADDRESS:
  1004. s->i2c_addr = value & 0xff;
  1005. break;
  1006. case SM501_I2C_DATA ... SM501_I2C_DATA + 15:
  1007. s->i2c_data[addr - SM501_I2C_DATA] = value & 0xff;
  1008. break;
  1009. default:
  1010. qemu_log_mask(LOG_UNIMP, "sm501 i2c : not implemented register write. "
  1011. "addr=0x%" HWADDR_PRIx " val=%" PRIx64 "\n", addr, value);
  1012. }
  1013. }
  1014. static const MemoryRegionOps sm501_i2c_ops = {
  1015. .read = sm501_i2c_read,
  1016. .write = sm501_i2c_write,
  1017. .valid = {
  1018. .min_access_size = 1,
  1019. .max_access_size = 1,
  1020. },
  1021. .impl = {
  1022. .min_access_size = 1,
  1023. .max_access_size = 1,
  1024. },
  1025. .endianness = DEVICE_LITTLE_ENDIAN,
  1026. };
  1027. static uint32_t sm501_palette_read(void *opaque, hwaddr addr)
  1028. {
  1029. SM501State *s = opaque;
  1030. trace_sm501_palette_read((uint32_t)addr);
  1031. /* TODO : consider BYTE/WORD access */
  1032. /* TODO : consider endian */
  1033. assert(range_covers_byte(0, 0x400 * 3, addr));
  1034. return *(uint32_t *)&s->dc_palette[addr];
  1035. }
  1036. static void sm501_palette_write(void *opaque, hwaddr addr,
  1037. uint32_t value)
  1038. {
  1039. SM501State *s = opaque;
  1040. trace_sm501_palette_write((uint32_t)addr, value);
  1041. /* TODO : consider BYTE/WORD access */
  1042. /* TODO : consider endian */
  1043. assert(range_covers_byte(0, 0x400 * 3, addr));
  1044. *(uint32_t *)&s->dc_palette[addr] = value;
  1045. s->do_full_update = true;
  1046. }
  1047. static uint64_t sm501_disp_ctrl_read(void *opaque, hwaddr addr,
  1048. unsigned size)
  1049. {
  1050. SM501State *s = opaque;
  1051. uint32_t ret = 0;
  1052. switch (addr) {
  1053. case SM501_DC_PANEL_CONTROL:
  1054. ret = s->dc_panel_control;
  1055. break;
  1056. case SM501_DC_PANEL_PANNING_CONTROL:
  1057. ret = s->dc_panel_panning_control;
  1058. break;
  1059. case SM501_DC_PANEL_COLOR_KEY:
  1060. /* Not implemented yet */
  1061. break;
  1062. case SM501_DC_PANEL_FB_ADDR:
  1063. ret = s->dc_panel_fb_addr;
  1064. break;
  1065. case SM501_DC_PANEL_FB_OFFSET:
  1066. ret = s->dc_panel_fb_offset;
  1067. break;
  1068. case SM501_DC_PANEL_FB_WIDTH:
  1069. ret = s->dc_panel_fb_width;
  1070. break;
  1071. case SM501_DC_PANEL_FB_HEIGHT:
  1072. ret = s->dc_panel_fb_height;
  1073. break;
  1074. case SM501_DC_PANEL_TL_LOC:
  1075. ret = s->dc_panel_tl_location;
  1076. break;
  1077. case SM501_DC_PANEL_BR_LOC:
  1078. ret = s->dc_panel_br_location;
  1079. break;
  1080. case SM501_DC_PANEL_H_TOT:
  1081. ret = s->dc_panel_h_total;
  1082. break;
  1083. case SM501_DC_PANEL_H_SYNC:
  1084. ret = s->dc_panel_h_sync;
  1085. break;
  1086. case SM501_DC_PANEL_V_TOT:
  1087. ret = s->dc_panel_v_total;
  1088. break;
  1089. case SM501_DC_PANEL_V_SYNC:
  1090. ret = s->dc_panel_v_sync;
  1091. break;
  1092. case SM501_DC_PANEL_HWC_ADDR:
  1093. ret = s->dc_panel_hwc_addr;
  1094. break;
  1095. case SM501_DC_PANEL_HWC_LOC:
  1096. ret = s->dc_panel_hwc_location;
  1097. break;
  1098. case SM501_DC_PANEL_HWC_COLOR_1_2:
  1099. ret = s->dc_panel_hwc_color_1_2;
  1100. break;
  1101. case SM501_DC_PANEL_HWC_COLOR_3:
  1102. ret = s->dc_panel_hwc_color_3;
  1103. break;
  1104. case SM501_DC_VIDEO_CONTROL:
  1105. ret = s->dc_video_control;
  1106. break;
  1107. case SM501_DC_CRT_CONTROL:
  1108. ret = s->dc_crt_control;
  1109. break;
  1110. case SM501_DC_CRT_FB_ADDR:
  1111. ret = s->dc_crt_fb_addr;
  1112. break;
  1113. case SM501_DC_CRT_FB_OFFSET:
  1114. ret = s->dc_crt_fb_offset;
  1115. break;
  1116. case SM501_DC_CRT_H_TOT:
  1117. ret = s->dc_crt_h_total;
  1118. break;
  1119. case SM501_DC_CRT_H_SYNC:
  1120. ret = s->dc_crt_h_sync;
  1121. break;
  1122. case SM501_DC_CRT_V_TOT:
  1123. ret = s->dc_crt_v_total;
  1124. break;
  1125. case SM501_DC_CRT_V_SYNC:
  1126. ret = s->dc_crt_v_sync;
  1127. break;
  1128. case SM501_DC_CRT_HWC_ADDR:
  1129. ret = s->dc_crt_hwc_addr;
  1130. break;
  1131. case SM501_DC_CRT_HWC_LOC:
  1132. ret = s->dc_crt_hwc_location;
  1133. break;
  1134. case SM501_DC_CRT_HWC_COLOR_1_2:
  1135. ret = s->dc_crt_hwc_color_1_2;
  1136. break;
  1137. case SM501_DC_CRT_HWC_COLOR_3:
  1138. ret = s->dc_crt_hwc_color_3;
  1139. break;
  1140. case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400 * 3 - 4:
  1141. ret = sm501_palette_read(opaque, addr - SM501_DC_PANEL_PALETTE);
  1142. break;
  1143. default:
  1144. qemu_log_mask(LOG_UNIMP, "sm501: not implemented disp ctrl register "
  1145. "read. addr=%" HWADDR_PRIx "\n", addr);
  1146. }
  1147. trace_sm501_disp_ctrl_read((uint32_t)addr, ret);
  1148. return ret;
  1149. }
  1150. static void sm501_disp_ctrl_write(void *opaque, hwaddr addr,
  1151. uint64_t value, unsigned size)
  1152. {
  1153. SM501State *s = opaque;
  1154. trace_sm501_disp_ctrl_write((uint32_t)addr, (uint32_t)value);
  1155. switch (addr) {
  1156. case SM501_DC_PANEL_CONTROL:
  1157. s->dc_panel_control = value & 0x0FFF73FF;
  1158. break;
  1159. case SM501_DC_PANEL_PANNING_CONTROL:
  1160. s->dc_panel_panning_control = value & 0xFF3FFF3F;
  1161. break;
  1162. case SM501_DC_PANEL_COLOR_KEY:
  1163. /* Not implemented yet */
  1164. break;
  1165. case SM501_DC_PANEL_FB_ADDR:
  1166. s->dc_panel_fb_addr = value & 0x8FFFFFF0;
  1167. if (value & 0x8000000) {
  1168. qemu_log_mask(LOG_UNIMP, "Panel external memory not supported\n");
  1169. }
  1170. s->do_full_update = true;
  1171. break;
  1172. case SM501_DC_PANEL_FB_OFFSET:
  1173. s->dc_panel_fb_offset = value & 0x3FF03FF0;
  1174. break;
  1175. case SM501_DC_PANEL_FB_WIDTH:
  1176. s->dc_panel_fb_width = value & 0x0FFF0FFF;
  1177. break;
  1178. case SM501_DC_PANEL_FB_HEIGHT:
  1179. s->dc_panel_fb_height = value & 0x0FFF0FFF;
  1180. break;
  1181. case SM501_DC_PANEL_TL_LOC:
  1182. s->dc_panel_tl_location = value & 0x07FF07FF;
  1183. break;
  1184. case SM501_DC_PANEL_BR_LOC:
  1185. s->dc_panel_br_location = value & 0x07FF07FF;
  1186. break;
  1187. case SM501_DC_PANEL_H_TOT:
  1188. s->dc_panel_h_total = value & 0x0FFF0FFF;
  1189. break;
  1190. case SM501_DC_PANEL_H_SYNC:
  1191. s->dc_panel_h_sync = value & 0x00FF0FFF;
  1192. break;
  1193. case SM501_DC_PANEL_V_TOT:
  1194. s->dc_panel_v_total = value & 0x0FFF0FFF;
  1195. break;
  1196. case SM501_DC_PANEL_V_SYNC:
  1197. s->dc_panel_v_sync = value & 0x003F0FFF;
  1198. break;
  1199. case SM501_DC_PANEL_HWC_ADDR:
  1200. value &= 0x8FFFFFF0;
  1201. if (value != s->dc_panel_hwc_addr) {
  1202. hwc_invalidate(s, 0);
  1203. s->dc_panel_hwc_addr = value;
  1204. }
  1205. break;
  1206. case SM501_DC_PANEL_HWC_LOC:
  1207. value &= 0x0FFF0FFF;
  1208. if (value != s->dc_panel_hwc_location) {
  1209. hwc_invalidate(s, 0);
  1210. s->dc_panel_hwc_location = value;
  1211. }
  1212. break;
  1213. case SM501_DC_PANEL_HWC_COLOR_1_2:
  1214. s->dc_panel_hwc_color_1_2 = value;
  1215. break;
  1216. case SM501_DC_PANEL_HWC_COLOR_3:
  1217. s->dc_panel_hwc_color_3 = value & 0x0000FFFF;
  1218. break;
  1219. case SM501_DC_VIDEO_CONTROL:
  1220. s->dc_video_control = value & 0x00037FFF;
  1221. break;
  1222. case SM501_DC_CRT_CONTROL:
  1223. s->dc_crt_control = value & 0x0003FFFF;
  1224. break;
  1225. case SM501_DC_CRT_FB_ADDR:
  1226. s->dc_crt_fb_addr = value & 0x8FFFFFF0;
  1227. if (value & 0x8000000) {
  1228. qemu_log_mask(LOG_UNIMP, "CRT external memory not supported\n");
  1229. }
  1230. s->do_full_update = true;
  1231. break;
  1232. case SM501_DC_CRT_FB_OFFSET:
  1233. s->dc_crt_fb_offset = value & 0x3FF03FF0;
  1234. break;
  1235. case SM501_DC_CRT_H_TOT:
  1236. s->dc_crt_h_total = value & 0x0FFF0FFF;
  1237. break;
  1238. case SM501_DC_CRT_H_SYNC:
  1239. s->dc_crt_h_sync = value & 0x00FF0FFF;
  1240. break;
  1241. case SM501_DC_CRT_V_TOT:
  1242. s->dc_crt_v_total = value & 0x0FFF0FFF;
  1243. break;
  1244. case SM501_DC_CRT_V_SYNC:
  1245. s->dc_crt_v_sync = value & 0x003F0FFF;
  1246. break;
  1247. case SM501_DC_CRT_HWC_ADDR:
  1248. value &= 0x8FFFFFF0;
  1249. if (value != s->dc_crt_hwc_addr) {
  1250. hwc_invalidate(s, 1);
  1251. s->dc_crt_hwc_addr = value;
  1252. }
  1253. break;
  1254. case SM501_DC_CRT_HWC_LOC:
  1255. value &= 0x0FFF0FFF;
  1256. if (value != s->dc_crt_hwc_location) {
  1257. hwc_invalidate(s, 1);
  1258. s->dc_crt_hwc_location = value;
  1259. }
  1260. break;
  1261. case SM501_DC_CRT_HWC_COLOR_1_2:
  1262. s->dc_crt_hwc_color_1_2 = value;
  1263. break;
  1264. case SM501_DC_CRT_HWC_COLOR_3:
  1265. s->dc_crt_hwc_color_3 = value & 0x0000FFFF;
  1266. break;
  1267. case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400 * 3 - 4:
  1268. sm501_palette_write(opaque, addr - SM501_DC_PANEL_PALETTE, value);
  1269. break;
  1270. default:
  1271. qemu_log_mask(LOG_UNIMP, "sm501: not implemented disp ctrl register "
  1272. "write. addr=%" HWADDR_PRIx
  1273. ", val=%" PRIx64 "\n", addr, value);
  1274. }
  1275. }
  1276. static const MemoryRegionOps sm501_disp_ctrl_ops = {
  1277. .read = sm501_disp_ctrl_read,
  1278. .write = sm501_disp_ctrl_write,
  1279. .valid = {
  1280. .min_access_size = 4,
  1281. .max_access_size = 4,
  1282. },
  1283. .endianness = DEVICE_LITTLE_ENDIAN,
  1284. };
  1285. static uint64_t sm501_2d_engine_read(void *opaque, hwaddr addr,
  1286. unsigned size)
  1287. {
  1288. SM501State *s = opaque;
  1289. uint32_t ret = 0;
  1290. switch (addr) {
  1291. case SM501_2D_SOURCE:
  1292. ret = s->twoD_source;
  1293. break;
  1294. case SM501_2D_DESTINATION:
  1295. ret = s->twoD_destination;
  1296. break;
  1297. case SM501_2D_DIMENSION:
  1298. ret = s->twoD_dimension;
  1299. break;
  1300. case SM501_2D_CONTROL:
  1301. ret = s->twoD_control;
  1302. break;
  1303. case SM501_2D_PITCH:
  1304. ret = s->twoD_pitch;
  1305. break;
  1306. case SM501_2D_FOREGROUND:
  1307. ret = s->twoD_foreground;
  1308. break;
  1309. case SM501_2D_BACKGROUND:
  1310. ret = s->twoD_background;
  1311. break;
  1312. case SM501_2D_STRETCH:
  1313. ret = s->twoD_stretch;
  1314. break;
  1315. case SM501_2D_COLOR_COMPARE:
  1316. ret = s->twoD_color_compare;
  1317. break;
  1318. case SM501_2D_COLOR_COMPARE_MASK:
  1319. ret = s->twoD_color_compare_mask;
  1320. break;
  1321. case SM501_2D_MASK:
  1322. ret = s->twoD_mask;
  1323. break;
  1324. case SM501_2D_CLIP_TL:
  1325. ret = s->twoD_clip_tl;
  1326. break;
  1327. case SM501_2D_CLIP_BR:
  1328. ret = s->twoD_clip_br;
  1329. break;
  1330. case SM501_2D_MONO_PATTERN_LOW:
  1331. ret = s->twoD_mono_pattern_low;
  1332. break;
  1333. case SM501_2D_MONO_PATTERN_HIGH:
  1334. ret = s->twoD_mono_pattern_high;
  1335. break;
  1336. case SM501_2D_WINDOW_WIDTH:
  1337. ret = s->twoD_window_width;
  1338. break;
  1339. case SM501_2D_SOURCE_BASE:
  1340. ret = s->twoD_source_base;
  1341. break;
  1342. case SM501_2D_DESTINATION_BASE:
  1343. ret = s->twoD_destination_base;
  1344. break;
  1345. case SM501_2D_ALPHA:
  1346. ret = s->twoD_alpha;
  1347. break;
  1348. case SM501_2D_WRAP:
  1349. ret = s->twoD_wrap;
  1350. break;
  1351. case SM501_2D_STATUS:
  1352. ret = 0; /* Should return interrupt status */
  1353. break;
  1354. default:
  1355. qemu_log_mask(LOG_UNIMP, "sm501: not implemented disp ctrl register "
  1356. "read. addr=%" HWADDR_PRIx "\n", addr);
  1357. }
  1358. trace_sm501_2d_engine_read((uint32_t)addr, ret);
  1359. return ret;
  1360. }
  1361. static void sm501_2d_engine_write(void *opaque, hwaddr addr,
  1362. uint64_t value, unsigned size)
  1363. {
  1364. SM501State *s = opaque;
  1365. trace_sm501_2d_engine_write((uint32_t)addr, (uint32_t)value);
  1366. switch (addr) {
  1367. case SM501_2D_SOURCE:
  1368. s->twoD_source = value;
  1369. break;
  1370. case SM501_2D_DESTINATION:
  1371. s->twoD_destination = value;
  1372. break;
  1373. case SM501_2D_DIMENSION:
  1374. s->twoD_dimension = value;
  1375. break;
  1376. case SM501_2D_CONTROL:
  1377. s->twoD_control = value;
  1378. /* do 2d operation if start flag is set. */
  1379. if (value & 0x80000000) {
  1380. sm501_2d_operation(s);
  1381. s->twoD_control &= ~0x80000000; /* start flag down */
  1382. }
  1383. break;
  1384. case SM501_2D_PITCH:
  1385. s->twoD_pitch = value;
  1386. break;
  1387. case SM501_2D_FOREGROUND:
  1388. s->twoD_foreground = value;
  1389. break;
  1390. case SM501_2D_BACKGROUND:
  1391. s->twoD_background = value;
  1392. break;
  1393. case SM501_2D_STRETCH:
  1394. if (((value >> 20) & 3) == 3) {
  1395. value &= ~BIT(20);
  1396. }
  1397. s->twoD_stretch = value;
  1398. break;
  1399. case SM501_2D_COLOR_COMPARE:
  1400. s->twoD_color_compare = value;
  1401. break;
  1402. case SM501_2D_COLOR_COMPARE_MASK:
  1403. s->twoD_color_compare_mask = value;
  1404. break;
  1405. case SM501_2D_MASK:
  1406. s->twoD_mask = value;
  1407. break;
  1408. case SM501_2D_CLIP_TL:
  1409. s->twoD_clip_tl = value;
  1410. break;
  1411. case SM501_2D_CLIP_BR:
  1412. s->twoD_clip_br = value;
  1413. break;
  1414. case SM501_2D_MONO_PATTERN_LOW:
  1415. s->twoD_mono_pattern_low = value;
  1416. break;
  1417. case SM501_2D_MONO_PATTERN_HIGH:
  1418. s->twoD_mono_pattern_high = value;
  1419. break;
  1420. case SM501_2D_WINDOW_WIDTH:
  1421. s->twoD_window_width = value;
  1422. break;
  1423. case SM501_2D_SOURCE_BASE:
  1424. s->twoD_source_base = value;
  1425. break;
  1426. case SM501_2D_DESTINATION_BASE:
  1427. s->twoD_destination_base = value;
  1428. break;
  1429. case SM501_2D_ALPHA:
  1430. s->twoD_alpha = value;
  1431. break;
  1432. case SM501_2D_WRAP:
  1433. s->twoD_wrap = value;
  1434. break;
  1435. case SM501_2D_STATUS:
  1436. /* ignored, writing 0 should clear interrupt status */
  1437. break;
  1438. default:
  1439. qemu_log_mask(LOG_UNIMP, "sm501: not implemented 2d engine register "
  1440. "write. addr=%" HWADDR_PRIx
  1441. ", val=%" PRIx64 "\n", addr, value);
  1442. }
  1443. }
  1444. static const MemoryRegionOps sm501_2d_engine_ops = {
  1445. .read = sm501_2d_engine_read,
  1446. .write = sm501_2d_engine_write,
  1447. .valid = {
  1448. .min_access_size = 4,
  1449. .max_access_size = 4,
  1450. },
  1451. .endianness = DEVICE_LITTLE_ENDIAN,
  1452. };
  1453. /* draw line functions for all console modes */
  1454. typedef void draw_line_func(uint8_t *d, const uint8_t *s,
  1455. int width, const uint32_t *pal);
  1456. typedef void draw_hwc_line_func(uint8_t *d, const uint8_t *s,
  1457. int width, const uint8_t *palette,
  1458. int c_x, int c_y);
  1459. static void draw_line8_32(uint8_t *d, const uint8_t *s, int width,
  1460. const uint32_t *pal)
  1461. {
  1462. uint8_t v, r, g, b;
  1463. do {
  1464. v = ldub_p(s);
  1465. r = (pal[v] >> 16) & 0xff;
  1466. g = (pal[v] >> 8) & 0xff;
  1467. b = (pal[v] >> 0) & 0xff;
  1468. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  1469. s++;
  1470. d += 4;
  1471. } while (--width != 0);
  1472. }
  1473. static void draw_line16_32(uint8_t *d, const uint8_t *s, int width,
  1474. const uint32_t *pal)
  1475. {
  1476. uint16_t rgb565;
  1477. uint8_t r, g, b;
  1478. do {
  1479. rgb565 = lduw_le_p(s);
  1480. r = (rgb565 >> 8) & 0xf8;
  1481. g = (rgb565 >> 3) & 0xfc;
  1482. b = (rgb565 << 3) & 0xf8;
  1483. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  1484. s += 2;
  1485. d += 4;
  1486. } while (--width != 0);
  1487. }
  1488. static void draw_line32_32(uint8_t *d, const uint8_t *s, int width,
  1489. const uint32_t *pal)
  1490. {
  1491. uint8_t r, g, b;
  1492. do {
  1493. r = s[2];
  1494. g = s[1];
  1495. b = s[0];
  1496. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  1497. s += 4;
  1498. d += 4;
  1499. } while (--width != 0);
  1500. }
  1501. /**
  1502. * Draw hardware cursor image on the given line.
  1503. */
  1504. static void draw_hwc_line_32(uint8_t *d, const uint8_t *s, int width,
  1505. const uint8_t *palette, int c_x, int c_y)
  1506. {
  1507. int i;
  1508. uint8_t r, g, b, v, bitset = 0;
  1509. /* get cursor position */
  1510. assert(0 <= c_y && c_y < SM501_HWC_HEIGHT);
  1511. s += SM501_HWC_WIDTH * c_y / 4; /* 4 pixels per byte */
  1512. d += c_x * 4;
  1513. for (i = 0; i < SM501_HWC_WIDTH && c_x + i < width; i++) {
  1514. /* get pixel value */
  1515. if (i % 4 == 0) {
  1516. bitset = ldub_p(s);
  1517. s++;
  1518. }
  1519. v = bitset & 3;
  1520. bitset >>= 2;
  1521. /* write pixel */
  1522. if (v) {
  1523. v--;
  1524. r = palette[v * 3 + 0];
  1525. g = palette[v * 3 + 1];
  1526. b = palette[v * 3 + 2];
  1527. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  1528. }
  1529. d += 4;
  1530. }
  1531. }
  1532. static void sm501_update_display(void *opaque)
  1533. {
  1534. SM501State *s = opaque;
  1535. DisplaySurface *surface = qemu_console_surface(s->con);
  1536. DirtyBitmapSnapshot *snap;
  1537. int y, c_x = 0, c_y = 0;
  1538. int crt = (s->dc_crt_control & SM501_DC_CRT_CONTROL_SEL) ? 1 : 0;
  1539. int width = get_width(s, crt);
  1540. int height = get_height(s, crt);
  1541. int src_bpp = get_bpp(s, crt);
  1542. int dst_bpp = surface_bytes_per_pixel(surface);
  1543. draw_line_func *draw_line = NULL;
  1544. draw_hwc_line_func *draw_hwc_line = NULL;
  1545. int full_update = 0;
  1546. int y_start = -1;
  1547. ram_addr_t offset;
  1548. uint32_t *palette;
  1549. uint8_t hwc_palette[3 * 3];
  1550. uint8_t *hwc_src = NULL;
  1551. assert(dst_bpp == 4); /* Output is always 32-bit RGB */
  1552. if (!((crt ? s->dc_crt_control : s->dc_panel_control)
  1553. & SM501_DC_CRT_CONTROL_ENABLE)) {
  1554. return;
  1555. }
  1556. palette = (uint32_t *)(crt ? &s->dc_palette[SM501_DC_CRT_PALETTE -
  1557. SM501_DC_PANEL_PALETTE]
  1558. : &s->dc_palette[0]);
  1559. /* choose draw_line function */
  1560. switch (src_bpp) {
  1561. case 1:
  1562. draw_line = draw_line8_32;
  1563. break;
  1564. case 2:
  1565. draw_line = draw_line16_32;
  1566. break;
  1567. case 4:
  1568. draw_line = draw_line32_32;
  1569. break;
  1570. default:
  1571. qemu_log_mask(LOG_GUEST_ERROR, "sm501: update display"
  1572. "invalid control register value.\n");
  1573. return;
  1574. }
  1575. /* set up to draw hardware cursor */
  1576. if (is_hwc_enabled(s, crt)) {
  1577. /* choose cursor draw line function */
  1578. draw_hwc_line = draw_hwc_line_32;
  1579. hwc_src = get_hwc_address(s, crt);
  1580. c_x = get_hwc_x(s, crt);
  1581. c_y = get_hwc_y(s, crt);
  1582. get_hwc_palette(s, crt, hwc_palette);
  1583. }
  1584. /* adjust console size */
  1585. if (s->last_width != width || s->last_height != height) {
  1586. qemu_console_resize(s->con, width, height);
  1587. surface = qemu_console_surface(s->con);
  1588. s->last_width = width;
  1589. s->last_height = height;
  1590. full_update = 1;
  1591. }
  1592. /* someone else requested a full update */
  1593. if (s->do_full_update) {
  1594. s->do_full_update = false;
  1595. full_update = 1;
  1596. }
  1597. /* draw each line according to conditions */
  1598. offset = get_fb_addr(s, crt);
  1599. snap = memory_region_snapshot_and_clear_dirty(&s->local_mem_region,
  1600. offset, width * height * src_bpp, DIRTY_MEMORY_VGA);
  1601. for (y = 0; y < height; y++, offset += width * src_bpp) {
  1602. int update, update_hwc;
  1603. /* check if hardware cursor is enabled and we're within its range */
  1604. update_hwc = draw_hwc_line && c_y <= y && y < c_y + SM501_HWC_HEIGHT;
  1605. update = full_update || update_hwc;
  1606. /* check dirty flags for each line */
  1607. update |= memory_region_snapshot_get_dirty(&s->local_mem_region, snap,
  1608. offset, width * src_bpp);
  1609. /* draw line and change status */
  1610. if (update) {
  1611. uint8_t *d = surface_data(surface);
  1612. d += y * width * dst_bpp;
  1613. /* draw graphics layer */
  1614. draw_line(d, s->local_mem + offset, width, palette);
  1615. /* draw hardware cursor */
  1616. if (update_hwc) {
  1617. draw_hwc_line(d, hwc_src, width, hwc_palette, c_x, y - c_y);
  1618. }
  1619. if (y_start < 0) {
  1620. y_start = y;
  1621. }
  1622. } else {
  1623. if (y_start >= 0) {
  1624. /* flush to display */
  1625. dpy_gfx_update(s->con, 0, y_start, width, y - y_start);
  1626. y_start = -1;
  1627. }
  1628. }
  1629. }
  1630. g_free(snap);
  1631. /* complete flush to display */
  1632. if (y_start >= 0) {
  1633. dpy_gfx_update(s->con, 0, y_start, width, y - y_start);
  1634. }
  1635. }
  1636. static const GraphicHwOps sm501_ops = {
  1637. .gfx_update = sm501_update_display,
  1638. };
  1639. static void sm501_reset(SM501State *s)
  1640. {
  1641. s->system_control = 0x00100000; /* 2D engine FIFO empty */
  1642. /*
  1643. * Bits 17 (SH), 7 (CDR), 6:5 (Test), 2:0 (Bus) are all supposed
  1644. * to be determined at reset by GPIO lines which set config bits.
  1645. * We hardwire them:
  1646. * SH = 0 : Hitachi Ready Polarity == Active Low
  1647. * CDR = 0 : do not reset clock divider
  1648. * TEST = 0 : Normal mode (not testing the silicon)
  1649. * BUS = 0 : Hitachi SH3/SH4
  1650. */
  1651. s->misc_control = SM501_MISC_DAC_POWER;
  1652. s->gpio_31_0_control = 0;
  1653. s->gpio_63_32_control = 0;
  1654. s->dram_control = 0;
  1655. s->arbitration_control = 0x05146732;
  1656. s->irq_mask = 0;
  1657. s->misc_timing = 0;
  1658. s->power_mode_control = 0;
  1659. s->i2c_byte_count = 0;
  1660. s->i2c_status = 0;
  1661. s->i2c_addr = 0;
  1662. memset(s->i2c_data, 0, 16);
  1663. s->dc_panel_control = 0x00010000; /* FIFO level 3 */
  1664. s->dc_video_control = 0;
  1665. s->dc_crt_control = 0x00010000;
  1666. s->twoD_source = 0;
  1667. s->twoD_destination = 0;
  1668. s->twoD_dimension = 0;
  1669. s->twoD_control = 0;
  1670. s->twoD_pitch = 0;
  1671. s->twoD_foreground = 0;
  1672. s->twoD_background = 0;
  1673. s->twoD_stretch = 0;
  1674. s->twoD_color_compare = 0;
  1675. s->twoD_color_compare_mask = 0;
  1676. s->twoD_mask = 0;
  1677. s->twoD_clip_tl = 0;
  1678. s->twoD_clip_br = 0;
  1679. s->twoD_mono_pattern_low = 0;
  1680. s->twoD_mono_pattern_high = 0;
  1681. s->twoD_window_width = 0;
  1682. s->twoD_source_base = 0;
  1683. s->twoD_destination_base = 0;
  1684. s->twoD_alpha = 0;
  1685. s->twoD_wrap = 0;
  1686. }
  1687. static void sm501_init(SM501State *s, DeviceState *dev,
  1688. uint32_t local_mem_bytes)
  1689. {
  1690. #ifndef CONFIG_PIXMAN
  1691. if (s->use_pixman != 0) {
  1692. warn_report("x-pixman != 0, not effective without PIXMAN");
  1693. }
  1694. #endif
  1695. s->local_mem_size_index = get_local_mem_size_index(local_mem_bytes);
  1696. /* local memory */
  1697. memory_region_init_ram(&s->local_mem_region, OBJECT(dev), "sm501.local",
  1698. get_local_mem_size(s), &error_fatal);
  1699. memory_region_set_log(&s->local_mem_region, true, DIRTY_MEMORY_VGA);
  1700. s->local_mem = memory_region_get_ram_ptr(&s->local_mem_region);
  1701. /* i2c */
  1702. s->i2c_bus = i2c_init_bus(dev, "sm501.i2c");
  1703. /* ddc */
  1704. I2CDDCState *ddc = I2CDDC(qdev_new(TYPE_I2CDDC));
  1705. i2c_slave_set_address(I2C_SLAVE(ddc), 0x50);
  1706. qdev_realize_and_unref(DEVICE(ddc), BUS(s->i2c_bus), &error_abort);
  1707. /* mmio */
  1708. memory_region_init(&s->mmio_region, OBJECT(dev), "sm501.mmio", MMIO_SIZE);
  1709. memory_region_init_io(&s->system_config_region, OBJECT(dev),
  1710. &sm501_system_config_ops, s,
  1711. "sm501-system-config", 0x6c);
  1712. memory_region_add_subregion(&s->mmio_region, SM501_SYS_CONFIG,
  1713. &s->system_config_region);
  1714. memory_region_init_io(&s->i2c_region, OBJECT(dev), &sm501_i2c_ops, s,
  1715. "sm501-i2c", 0x14);
  1716. memory_region_add_subregion(&s->mmio_region, SM501_I2C, &s->i2c_region);
  1717. memory_region_init_io(&s->disp_ctrl_region, OBJECT(dev),
  1718. &sm501_disp_ctrl_ops, s,
  1719. "sm501-disp-ctrl", 0x1000);
  1720. memory_region_add_subregion(&s->mmio_region, SM501_DC,
  1721. &s->disp_ctrl_region);
  1722. memory_region_init_io(&s->twoD_engine_region, OBJECT(dev),
  1723. &sm501_2d_engine_ops, s,
  1724. "sm501-2d-engine", 0x54);
  1725. memory_region_add_subregion(&s->mmio_region, SM501_2D_ENGINE,
  1726. &s->twoD_engine_region);
  1727. /* create qemu graphic console */
  1728. s->con = graphic_console_init(dev, 0, &sm501_ops, s);
  1729. }
  1730. static const VMStateDescription vmstate_sm501_state = {
  1731. .name = "sm501-state",
  1732. .version_id = 1,
  1733. .minimum_version_id = 1,
  1734. .fields = (const VMStateField[]) {
  1735. VMSTATE_UINT32(local_mem_size_index, SM501State),
  1736. VMSTATE_UINT32(system_control, SM501State),
  1737. VMSTATE_UINT32(misc_control, SM501State),
  1738. VMSTATE_UINT32(gpio_31_0_control, SM501State),
  1739. VMSTATE_UINT32(gpio_63_32_control, SM501State),
  1740. VMSTATE_UINT32(dram_control, SM501State),
  1741. VMSTATE_UINT32(arbitration_control, SM501State),
  1742. VMSTATE_UINT32(irq_mask, SM501State),
  1743. VMSTATE_UINT32(misc_timing, SM501State),
  1744. VMSTATE_UINT32(power_mode_control, SM501State),
  1745. VMSTATE_UINT32(uart0_ier, SM501State),
  1746. VMSTATE_UINT32(uart0_lcr, SM501State),
  1747. VMSTATE_UINT32(uart0_mcr, SM501State),
  1748. VMSTATE_UINT32(uart0_scr, SM501State),
  1749. VMSTATE_UINT8_ARRAY(dc_palette, SM501State, DC_PALETTE_ENTRIES),
  1750. VMSTATE_UINT32(dc_panel_control, SM501State),
  1751. VMSTATE_UINT32(dc_panel_panning_control, SM501State),
  1752. VMSTATE_UINT32(dc_panel_fb_addr, SM501State),
  1753. VMSTATE_UINT32(dc_panel_fb_offset, SM501State),
  1754. VMSTATE_UINT32(dc_panel_fb_width, SM501State),
  1755. VMSTATE_UINT32(dc_panel_fb_height, SM501State),
  1756. VMSTATE_UINT32(dc_panel_tl_location, SM501State),
  1757. VMSTATE_UINT32(dc_panel_br_location, SM501State),
  1758. VMSTATE_UINT32(dc_panel_h_total, SM501State),
  1759. VMSTATE_UINT32(dc_panel_h_sync, SM501State),
  1760. VMSTATE_UINT32(dc_panel_v_total, SM501State),
  1761. VMSTATE_UINT32(dc_panel_v_sync, SM501State),
  1762. VMSTATE_UINT32(dc_panel_hwc_addr, SM501State),
  1763. VMSTATE_UINT32(dc_panel_hwc_location, SM501State),
  1764. VMSTATE_UINT32(dc_panel_hwc_color_1_2, SM501State),
  1765. VMSTATE_UINT32(dc_panel_hwc_color_3, SM501State),
  1766. VMSTATE_UINT32(dc_video_control, SM501State),
  1767. VMSTATE_UINT32(dc_crt_control, SM501State),
  1768. VMSTATE_UINT32(dc_crt_fb_addr, SM501State),
  1769. VMSTATE_UINT32(dc_crt_fb_offset, SM501State),
  1770. VMSTATE_UINT32(dc_crt_h_total, SM501State),
  1771. VMSTATE_UINT32(dc_crt_h_sync, SM501State),
  1772. VMSTATE_UINT32(dc_crt_v_total, SM501State),
  1773. VMSTATE_UINT32(dc_crt_v_sync, SM501State),
  1774. VMSTATE_UINT32(dc_crt_hwc_addr, SM501State),
  1775. VMSTATE_UINT32(dc_crt_hwc_location, SM501State),
  1776. VMSTATE_UINT32(dc_crt_hwc_color_1_2, SM501State),
  1777. VMSTATE_UINT32(dc_crt_hwc_color_3, SM501State),
  1778. VMSTATE_UINT32(twoD_source, SM501State),
  1779. VMSTATE_UINT32(twoD_destination, SM501State),
  1780. VMSTATE_UINT32(twoD_dimension, SM501State),
  1781. VMSTATE_UINT32(twoD_control, SM501State),
  1782. VMSTATE_UINT32(twoD_pitch, SM501State),
  1783. VMSTATE_UINT32(twoD_foreground, SM501State),
  1784. VMSTATE_UINT32(twoD_background, SM501State),
  1785. VMSTATE_UINT32(twoD_stretch, SM501State),
  1786. VMSTATE_UINT32(twoD_color_compare, SM501State),
  1787. VMSTATE_UINT32(twoD_color_compare_mask, SM501State),
  1788. VMSTATE_UINT32(twoD_mask, SM501State),
  1789. VMSTATE_UINT32(twoD_clip_tl, SM501State),
  1790. VMSTATE_UINT32(twoD_clip_br, SM501State),
  1791. VMSTATE_UINT32(twoD_mono_pattern_low, SM501State),
  1792. VMSTATE_UINT32(twoD_mono_pattern_high, SM501State),
  1793. VMSTATE_UINT32(twoD_window_width, SM501State),
  1794. VMSTATE_UINT32(twoD_source_base, SM501State),
  1795. VMSTATE_UINT32(twoD_destination_base, SM501State),
  1796. VMSTATE_UINT32(twoD_alpha, SM501State),
  1797. VMSTATE_UINT32(twoD_wrap, SM501State),
  1798. /* Added in version 2 */
  1799. VMSTATE_UINT8(i2c_byte_count, SM501State),
  1800. VMSTATE_UINT8(i2c_status, SM501State),
  1801. VMSTATE_UINT8(i2c_addr, SM501State),
  1802. VMSTATE_UINT8_ARRAY(i2c_data, SM501State, 16),
  1803. VMSTATE_END_OF_LIST()
  1804. }
  1805. };
  1806. #define TYPE_SYSBUS_SM501 "sysbus-sm501"
  1807. OBJECT_DECLARE_SIMPLE_TYPE(SM501SysBusState, SYSBUS_SM501)
  1808. struct SM501SysBusState {
  1809. /*< private >*/
  1810. SysBusDevice parent_obj;
  1811. /*< public >*/
  1812. SM501State state;
  1813. uint32_t vram_size;
  1814. SerialMM serial;
  1815. OHCISysBusState ohci;
  1816. };
  1817. static void sm501_realize_sysbus(DeviceState *dev, Error **errp)
  1818. {
  1819. SM501SysBusState *s = SYSBUS_SM501(dev);
  1820. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  1821. MemoryRegion *mr;
  1822. sm501_init(&s->state, dev, s->vram_size);
  1823. if (get_local_mem_size(&s->state) != s->vram_size) {
  1824. error_setg(errp, "Invalid VRAM size, nearest valid size is %" PRIu32,
  1825. get_local_mem_size(&s->state));
  1826. return;
  1827. }
  1828. sysbus_init_mmio(sbd, &s->state.local_mem_region);
  1829. sysbus_init_mmio(sbd, &s->state.mmio_region);
  1830. /* bridge to usb host emulation module */
  1831. sysbus_realize_and_unref(SYS_BUS_DEVICE(&s->ohci), &error_fatal);
  1832. memory_region_add_subregion(&s->state.mmio_region, SM501_USB_HOST,
  1833. sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->ohci), 0));
  1834. sysbus_pass_irq(sbd, SYS_BUS_DEVICE(&s->ohci));
  1835. /* bridge to serial emulation module */
  1836. sysbus_realize(SYS_BUS_DEVICE(&s->serial), &error_fatal);
  1837. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->serial), 0);
  1838. memory_region_add_subregion(&s->state.mmio_region, SM501_UART0, mr);
  1839. /* TODO : chain irq to IRL */
  1840. }
  1841. static Property sm501_sysbus_properties[] = {
  1842. DEFINE_PROP_UINT32("vram-size", SM501SysBusState, vram_size, 0),
  1843. /* this a debug option, prefer PROP_UINT over PROP_BIT for simplicity */
  1844. DEFINE_PROP_UINT8("x-pixman", SM501SysBusState, state.use_pixman, DEFAULT_X_PIXMAN),
  1845. DEFINE_PROP_END_OF_LIST(),
  1846. };
  1847. static void sm501_reset_sysbus(DeviceState *dev)
  1848. {
  1849. SM501SysBusState *s = SYSBUS_SM501(dev);
  1850. sm501_reset(&s->state);
  1851. }
  1852. static const VMStateDescription vmstate_sm501_sysbus = {
  1853. .name = TYPE_SYSBUS_SM501,
  1854. .version_id = 2,
  1855. .minimum_version_id = 2,
  1856. .fields = (const VMStateField[]) {
  1857. VMSTATE_STRUCT(state, SM501SysBusState, 1,
  1858. vmstate_sm501_state, SM501State),
  1859. VMSTATE_END_OF_LIST()
  1860. }
  1861. };
  1862. static void sm501_sysbus_class_init(ObjectClass *klass, void *data)
  1863. {
  1864. DeviceClass *dc = DEVICE_CLASS(klass);
  1865. dc->realize = sm501_realize_sysbus;
  1866. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  1867. dc->desc = "SM501 Multimedia Companion";
  1868. device_class_set_props(dc, sm501_sysbus_properties);
  1869. device_class_set_legacy_reset(dc, sm501_reset_sysbus);
  1870. dc->vmsd = &vmstate_sm501_sysbus;
  1871. }
  1872. static void sm501_sysbus_init(Object *o)
  1873. {
  1874. SM501SysBusState *sm501 = SYSBUS_SM501(o);
  1875. OHCISysBusState *ohci = &sm501->ohci;
  1876. SerialMM *smm = &sm501->serial;
  1877. object_initialize_child(o, "ohci", ohci, TYPE_SYSBUS_OHCI);
  1878. object_property_add_alias(o, "dma-offset", OBJECT(ohci), "dma-offset");
  1879. qdev_prop_set_uint32(DEVICE(ohci), "num-ports", 2);
  1880. object_initialize_child(o, "serial", smm, TYPE_SERIAL_MM);
  1881. qdev_set_legacy_instance_id(DEVICE(smm), SM501_UART0, 2);
  1882. qdev_prop_set_uint8(DEVICE(smm), "regshift", 2);
  1883. qdev_prop_set_uint8(DEVICE(smm), "endianness", DEVICE_LITTLE_ENDIAN);
  1884. object_property_add_alias(o, "chardev", OBJECT(smm), "chardev");
  1885. }
  1886. static const TypeInfo sm501_sysbus_info = {
  1887. .name = TYPE_SYSBUS_SM501,
  1888. .parent = TYPE_SYS_BUS_DEVICE,
  1889. .instance_size = sizeof(SM501SysBusState),
  1890. .class_init = sm501_sysbus_class_init,
  1891. .instance_init = sm501_sysbus_init,
  1892. };
  1893. #define TYPE_PCI_SM501 "sm501"
  1894. OBJECT_DECLARE_SIMPLE_TYPE(SM501PCIState, PCI_SM501)
  1895. struct SM501PCIState {
  1896. /*< private >*/
  1897. PCIDevice parent_obj;
  1898. /*< public >*/
  1899. SM501State state;
  1900. uint32_t vram_size;
  1901. };
  1902. static void sm501_realize_pci(PCIDevice *dev, Error **errp)
  1903. {
  1904. SM501PCIState *s = PCI_SM501(dev);
  1905. sm501_init(&s->state, DEVICE(dev), s->vram_size);
  1906. if (get_local_mem_size(&s->state) != s->vram_size) {
  1907. error_setg(errp, "Invalid VRAM size, nearest valid size is %" PRIu32,
  1908. get_local_mem_size(&s->state));
  1909. return;
  1910. }
  1911. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
  1912. &s->state.local_mem_region);
  1913. pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
  1914. &s->state.mmio_region);
  1915. }
  1916. static Property sm501_pci_properties[] = {
  1917. DEFINE_PROP_UINT32("vram-size", SM501PCIState, vram_size, 64 * MiB),
  1918. DEFINE_PROP_UINT8("x-pixman", SM501PCIState, state.use_pixman, DEFAULT_X_PIXMAN),
  1919. DEFINE_PROP_END_OF_LIST(),
  1920. };
  1921. static void sm501_reset_pci(DeviceState *dev)
  1922. {
  1923. SM501PCIState *s = PCI_SM501(dev);
  1924. sm501_reset(&s->state);
  1925. /* Bits 2:0 of misc_control register is 001 for PCI */
  1926. s->state.misc_control |= 1;
  1927. }
  1928. static const VMStateDescription vmstate_sm501_pci = {
  1929. .name = TYPE_PCI_SM501,
  1930. .version_id = 2,
  1931. .minimum_version_id = 2,
  1932. .fields = (const VMStateField[]) {
  1933. VMSTATE_PCI_DEVICE(parent_obj, SM501PCIState),
  1934. VMSTATE_STRUCT(state, SM501PCIState, 1,
  1935. vmstate_sm501_state, SM501State),
  1936. VMSTATE_END_OF_LIST()
  1937. }
  1938. };
  1939. static void sm501_pci_class_init(ObjectClass *klass, void *data)
  1940. {
  1941. DeviceClass *dc = DEVICE_CLASS(klass);
  1942. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  1943. k->realize = sm501_realize_pci;
  1944. k->vendor_id = PCI_VENDOR_ID_SILICON_MOTION;
  1945. k->device_id = PCI_DEVICE_ID_SM501;
  1946. k->class_id = PCI_CLASS_DISPLAY_OTHER;
  1947. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  1948. dc->desc = "SM501 Display Controller";
  1949. device_class_set_props(dc, sm501_pci_properties);
  1950. device_class_set_legacy_reset(dc, sm501_reset_pci);
  1951. dc->hotpluggable = false;
  1952. dc->vmsd = &vmstate_sm501_pci;
  1953. }
  1954. static void sm501_pci_init(Object *o)
  1955. {
  1956. object_property_set_description(o, "x-pixman", "Use pixman for: "
  1957. "1: fill, 2: blit, 4: overlap blit");
  1958. }
  1959. static const TypeInfo sm501_pci_info = {
  1960. .name = TYPE_PCI_SM501,
  1961. .parent = TYPE_PCI_DEVICE,
  1962. .instance_size = sizeof(SM501PCIState),
  1963. .class_init = sm501_pci_class_init,
  1964. .instance_init = sm501_pci_init,
  1965. .interfaces = (InterfaceInfo[]) {
  1966. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  1967. { },
  1968. },
  1969. };
  1970. static void sm501_register_types(void)
  1971. {
  1972. type_register_static(&sm501_sysbus_info);
  1973. type_register_static(&sm501_pci_info);
  1974. }
  1975. type_init(sm501_register_types)