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