]> git.proxmox.com Git - mirror_qemu.git/blob - hw/display/sm501.c
sm501: Add support for panel layer
[mirror_qemu.git] / hw / display / sm501.c
1 /*
2 * QEMU SM501 Device
3 *
4 * Copyright (c) 2008 Shin-ichiro KAWASAKI
5 * Copyright (c) 2016 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/cutils.h"
28 #include "qapi/error.h"
29 #include "qemu-common.h"
30 #include "cpu.h"
31 #include "hw/hw.h"
32 #include "hw/char/serial.h"
33 #include "ui/console.h"
34 #include "hw/devices.h"
35 #include "hw/sysbus.h"
36 #include "hw/pci/pci.h"
37 #include "qemu/range.h"
38 #include "ui/pixel_ops.h"
39 #include "exec/address-spaces.h"
40
41 /*
42 * Status: 2010/05/07
43 * - Minimum implementation for Linux console : mmio regs and CRT layer.
44 * - 2D graphics acceleration partially supported : only fill rectangle.
45 *
46 * Status: 2016/12/04
47 * - Misc fixes: endianness, hardware cursor
48 * - Panel support
49 *
50 * TODO:
51 * - Touch panel support
52 * - USB support
53 * - UART support
54 * - More 2D graphics engine support
55 * - Performance tuning
56 */
57
58 /*#define DEBUG_SM501*/
59 /*#define DEBUG_BITBLT*/
60
61 #ifdef DEBUG_SM501
62 #define SM501_DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
63 #else
64 #define SM501_DPRINTF(fmt, ...) do {} while (0)
65 #endif
66
67 #define MMIO_BASE_OFFSET 0x3e00000
68 #define MMIO_SIZE 0x200000
69
70 /* SM501 register definitions taken from "linux/include/linux/sm501-regs.h" */
71
72 /* System Configuration area */
73 /* System config base */
74 #define SM501_SYS_CONFIG (0x000000)
75
76 /* config 1 */
77 #define SM501_SYSTEM_CONTROL (0x000000)
78
79 #define SM501_SYSCTRL_PANEL_TRISTATE (1 << 0)
80 #define SM501_SYSCTRL_MEM_TRISTATE (1 << 1)
81 #define SM501_SYSCTRL_CRT_TRISTATE (1 << 2)
82
83 #define SM501_SYSCTRL_PCI_SLAVE_BURST_MASK (3 << 4)
84 #define SM501_SYSCTRL_PCI_SLAVE_BURST_1 (0 << 4)
85 #define SM501_SYSCTRL_PCI_SLAVE_BURST_2 (1 << 4)
86 #define SM501_SYSCTRL_PCI_SLAVE_BURST_4 (2 << 4)
87 #define SM501_SYSCTRL_PCI_SLAVE_BURST_8 (3 << 4)
88
89 #define SM501_SYSCTRL_PCI_CLOCK_RUN_EN (1 << 6)
90 #define SM501_SYSCTRL_PCI_RETRY_DISABLE (1 << 7)
91 #define SM501_SYSCTRL_PCI_SUBSYS_LOCK (1 << 11)
92 #define SM501_SYSCTRL_PCI_BURST_READ_EN (1 << 15)
93
94 /* miscellaneous control */
95
96 #define SM501_MISC_CONTROL (0x000004)
97
98 #define SM501_MISC_BUS_SH (0x0)
99 #define SM501_MISC_BUS_PCI (0x1)
100 #define SM501_MISC_BUS_XSCALE (0x2)
101 #define SM501_MISC_BUS_NEC (0x6)
102 #define SM501_MISC_BUS_MASK (0x7)
103
104 #define SM501_MISC_VR_62MB (1 << 3)
105 #define SM501_MISC_CDR_RESET (1 << 7)
106 #define SM501_MISC_USB_LB (1 << 8)
107 #define SM501_MISC_USB_SLAVE (1 << 9)
108 #define SM501_MISC_BL_1 (1 << 10)
109 #define SM501_MISC_MC (1 << 11)
110 #define SM501_MISC_DAC_POWER (1 << 12)
111 #define SM501_MISC_IRQ_INVERT (1 << 16)
112 #define SM501_MISC_SH (1 << 17)
113
114 #define SM501_MISC_HOLD_EMPTY (0 << 18)
115 #define SM501_MISC_HOLD_8 (1 << 18)
116 #define SM501_MISC_HOLD_16 (2 << 18)
117 #define SM501_MISC_HOLD_24 (3 << 18)
118 #define SM501_MISC_HOLD_32 (4 << 18)
119 #define SM501_MISC_HOLD_MASK (7 << 18)
120
121 #define SM501_MISC_FREQ_12 (1 << 24)
122 #define SM501_MISC_PNL_24BIT (1 << 25)
123 #define SM501_MISC_8051_LE (1 << 26)
124
125
126
127 #define SM501_GPIO31_0_CONTROL (0x000008)
128 #define SM501_GPIO63_32_CONTROL (0x00000C)
129 #define SM501_DRAM_CONTROL (0x000010)
130
131 /* command list */
132 #define SM501_ARBTRTN_CONTROL (0x000014)
133
134 /* command list */
135 #define SM501_COMMAND_LIST_STATUS (0x000024)
136
137 /* interrupt debug */
138 #define SM501_RAW_IRQ_STATUS (0x000028)
139 #define SM501_RAW_IRQ_CLEAR (0x000028)
140 #define SM501_IRQ_STATUS (0x00002C)
141 #define SM501_IRQ_MASK (0x000030)
142 #define SM501_DEBUG_CONTROL (0x000034)
143
144 /* power management */
145 #define SM501_POWERMODE_P2X_SRC (1 << 29)
146 #define SM501_POWERMODE_V2X_SRC (1 << 20)
147 #define SM501_POWERMODE_M_SRC (1 << 12)
148 #define SM501_POWERMODE_M1_SRC (1 << 4)
149
150 #define SM501_CURRENT_GATE (0x000038)
151 #define SM501_CURRENT_CLOCK (0x00003C)
152 #define SM501_POWER_MODE_0_GATE (0x000040)
153 #define SM501_POWER_MODE_0_CLOCK (0x000044)
154 #define SM501_POWER_MODE_1_GATE (0x000048)
155 #define SM501_POWER_MODE_1_CLOCK (0x00004C)
156 #define SM501_SLEEP_MODE_GATE (0x000050)
157 #define SM501_POWER_MODE_CONTROL (0x000054)
158
159 /* power gates for units within the 501 */
160 #define SM501_GATE_HOST (0)
161 #define SM501_GATE_MEMORY (1)
162 #define SM501_GATE_DISPLAY (2)
163 #define SM501_GATE_2D_ENGINE (3)
164 #define SM501_GATE_CSC (4)
165 #define SM501_GATE_ZVPORT (5)
166 #define SM501_GATE_GPIO (6)
167 #define SM501_GATE_UART0 (7)
168 #define SM501_GATE_UART1 (8)
169 #define SM501_GATE_SSP (10)
170 #define SM501_GATE_USB_HOST (11)
171 #define SM501_GATE_USB_GADGET (12)
172 #define SM501_GATE_UCONTROLLER (17)
173 #define SM501_GATE_AC97 (18)
174
175 /* panel clock */
176 #define SM501_CLOCK_P2XCLK (24)
177 /* crt clock */
178 #define SM501_CLOCK_V2XCLK (16)
179 /* main clock */
180 #define SM501_CLOCK_MCLK (8)
181 /* SDRAM controller clock */
182 #define SM501_CLOCK_M1XCLK (0)
183
184 /* config 2 */
185 #define SM501_PCI_MASTER_BASE (0x000058)
186 #define SM501_ENDIAN_CONTROL (0x00005C)
187 #define SM501_DEVICEID (0x000060)
188 /* 0x050100A0 */
189
190 #define SM501_DEVICEID_SM501 (0x05010000)
191 #define SM501_DEVICEID_IDMASK (0xffff0000)
192 #define SM501_DEVICEID_REVMASK (0x000000ff)
193
194 #define SM501_PLLCLOCK_COUNT (0x000064)
195 #define SM501_MISC_TIMING (0x000068)
196 #define SM501_CURRENT_SDRAM_CLOCK (0x00006C)
197
198 #define SM501_PROGRAMMABLE_PLL_CONTROL (0x000074)
199
200 /* GPIO base */
201 #define SM501_GPIO (0x010000)
202 #define SM501_GPIO_DATA_LOW (0x00)
203 #define SM501_GPIO_DATA_HIGH (0x04)
204 #define SM501_GPIO_DDR_LOW (0x08)
205 #define SM501_GPIO_DDR_HIGH (0x0C)
206 #define SM501_GPIO_IRQ_SETUP (0x10)
207 #define SM501_GPIO_IRQ_STATUS (0x14)
208 #define SM501_GPIO_IRQ_RESET (0x14)
209
210 /* I2C controller base */
211 #define SM501_I2C (0x010040)
212 #define SM501_I2C_BYTE_COUNT (0x00)
213 #define SM501_I2C_CONTROL (0x01)
214 #define SM501_I2C_STATUS (0x02)
215 #define SM501_I2C_RESET (0x02)
216 #define SM501_I2C_SLAVE_ADDRESS (0x03)
217 #define SM501_I2C_DATA (0x04)
218
219 /* SSP base */
220 #define SM501_SSP (0x020000)
221
222 /* Uart 0 base */
223 #define SM501_UART0 (0x030000)
224
225 /* Uart 1 base */
226 #define SM501_UART1 (0x030020)
227
228 /* USB host port base */
229 #define SM501_USB_HOST (0x040000)
230
231 /* USB slave/gadget base */
232 #define SM501_USB_GADGET (0x060000)
233
234 /* USB slave/gadget data port base */
235 #define SM501_USB_GADGET_DATA (0x070000)
236
237 /* Display controller/video engine base */
238 #define SM501_DC (0x080000)
239
240 /* common defines for the SM501 address registers */
241 #define SM501_ADDR_FLIP (1 << 31)
242 #define SM501_ADDR_EXT (1 << 27)
243 #define SM501_ADDR_CS1 (1 << 26)
244 #define SM501_ADDR_MASK (0x3f << 26)
245
246 #define SM501_FIFO_MASK (0x3 << 16)
247 #define SM501_FIFO_1 (0x0 << 16)
248 #define SM501_FIFO_3 (0x1 << 16)
249 #define SM501_FIFO_7 (0x2 << 16)
250 #define SM501_FIFO_11 (0x3 << 16)
251
252 /* common registers for panel and the crt */
253 #define SM501_OFF_DC_H_TOT (0x000)
254 #define SM501_OFF_DC_V_TOT (0x008)
255 #define SM501_OFF_DC_H_SYNC (0x004)
256 #define SM501_OFF_DC_V_SYNC (0x00C)
257
258 #define SM501_DC_PANEL_CONTROL (0x000)
259
260 #define SM501_DC_PANEL_CONTROL_FPEN (1 << 27)
261 #define SM501_DC_PANEL_CONTROL_BIAS (1 << 26)
262 #define SM501_DC_PANEL_CONTROL_DATA (1 << 25)
263 #define SM501_DC_PANEL_CONTROL_VDD (1 << 24)
264 #define SM501_DC_PANEL_CONTROL_DP (1 << 23)
265
266 #define SM501_DC_PANEL_CONTROL_TFT_888 (0 << 21)
267 #define SM501_DC_PANEL_CONTROL_TFT_333 (1 << 21)
268 #define SM501_DC_PANEL_CONTROL_TFT_444 (2 << 21)
269
270 #define SM501_DC_PANEL_CONTROL_DE (1 << 20)
271
272 #define SM501_DC_PANEL_CONTROL_LCD_TFT (0 << 18)
273 #define SM501_DC_PANEL_CONTROL_LCD_STN8 (1 << 18)
274 #define SM501_DC_PANEL_CONTROL_LCD_STN12 (2 << 18)
275
276 #define SM501_DC_PANEL_CONTROL_CP (1 << 14)
277 #define SM501_DC_PANEL_CONTROL_VSP (1 << 13)
278 #define SM501_DC_PANEL_CONTROL_HSP (1 << 12)
279 #define SM501_DC_PANEL_CONTROL_CK (1 << 9)
280 #define SM501_DC_PANEL_CONTROL_TE (1 << 8)
281 #define SM501_DC_PANEL_CONTROL_VPD (1 << 7)
282 #define SM501_DC_PANEL_CONTROL_VP (1 << 6)
283 #define SM501_DC_PANEL_CONTROL_HPD (1 << 5)
284 #define SM501_DC_PANEL_CONTROL_HP (1 << 4)
285 #define SM501_DC_PANEL_CONTROL_GAMMA (1 << 3)
286 #define SM501_DC_PANEL_CONTROL_EN (1 << 2)
287
288 #define SM501_DC_PANEL_CONTROL_8BPP (0 << 0)
289 #define SM501_DC_PANEL_CONTROL_16BPP (1 << 0)
290 #define SM501_DC_PANEL_CONTROL_32BPP (2 << 0)
291
292
293 #define SM501_DC_PANEL_PANNING_CONTROL (0x004)
294 #define SM501_DC_PANEL_COLOR_KEY (0x008)
295 #define SM501_DC_PANEL_FB_ADDR (0x00C)
296 #define SM501_DC_PANEL_FB_OFFSET (0x010)
297 #define SM501_DC_PANEL_FB_WIDTH (0x014)
298 #define SM501_DC_PANEL_FB_HEIGHT (0x018)
299 #define SM501_DC_PANEL_TL_LOC (0x01C)
300 #define SM501_DC_PANEL_BR_LOC (0x020)
301 #define SM501_DC_PANEL_H_TOT (0x024)
302 #define SM501_DC_PANEL_H_SYNC (0x028)
303 #define SM501_DC_PANEL_V_TOT (0x02C)
304 #define SM501_DC_PANEL_V_SYNC (0x030)
305 #define SM501_DC_PANEL_CUR_LINE (0x034)
306
307 #define SM501_DC_VIDEO_CONTROL (0x040)
308 #define SM501_DC_VIDEO_FB0_ADDR (0x044)
309 #define SM501_DC_VIDEO_FB_WIDTH (0x048)
310 #define SM501_DC_VIDEO_FB0_LAST_ADDR (0x04C)
311 #define SM501_DC_VIDEO_TL_LOC (0x050)
312 #define SM501_DC_VIDEO_BR_LOC (0x054)
313 #define SM501_DC_VIDEO_SCALE (0x058)
314 #define SM501_DC_VIDEO_INIT_SCALE (0x05C)
315 #define SM501_DC_VIDEO_YUV_CONSTANTS (0x060)
316 #define SM501_DC_VIDEO_FB1_ADDR (0x064)
317 #define SM501_DC_VIDEO_FB1_LAST_ADDR (0x068)
318
319 #define SM501_DC_VIDEO_ALPHA_CONTROL (0x080)
320 #define SM501_DC_VIDEO_ALPHA_FB_ADDR (0x084)
321 #define SM501_DC_VIDEO_ALPHA_FB_OFFSET (0x088)
322 #define SM501_DC_VIDEO_ALPHA_FB_LAST_ADDR (0x08C)
323 #define SM501_DC_VIDEO_ALPHA_TL_LOC (0x090)
324 #define SM501_DC_VIDEO_ALPHA_BR_LOC (0x094)
325 #define SM501_DC_VIDEO_ALPHA_SCALE (0x098)
326 #define SM501_DC_VIDEO_ALPHA_INIT_SCALE (0x09C)
327 #define SM501_DC_VIDEO_ALPHA_CHROMA_KEY (0x0A0)
328 #define SM501_DC_VIDEO_ALPHA_COLOR_LOOKUP (0x0A4)
329
330 #define SM501_DC_PANEL_HWC_BASE (0x0F0)
331 #define SM501_DC_PANEL_HWC_ADDR (0x0F0)
332 #define SM501_DC_PANEL_HWC_LOC (0x0F4)
333 #define SM501_DC_PANEL_HWC_COLOR_1_2 (0x0F8)
334 #define SM501_DC_PANEL_HWC_COLOR_3 (0x0FC)
335
336 #define SM501_HWC_EN (1 << 31)
337
338 #define SM501_OFF_HWC_ADDR (0x00)
339 #define SM501_OFF_HWC_LOC (0x04)
340 #define SM501_OFF_HWC_COLOR_1_2 (0x08)
341 #define SM501_OFF_HWC_COLOR_3 (0x0C)
342
343 #define SM501_DC_ALPHA_CONTROL (0x100)
344 #define SM501_DC_ALPHA_FB_ADDR (0x104)
345 #define SM501_DC_ALPHA_FB_OFFSET (0x108)
346 #define SM501_DC_ALPHA_TL_LOC (0x10C)
347 #define SM501_DC_ALPHA_BR_LOC (0x110)
348 #define SM501_DC_ALPHA_CHROMA_KEY (0x114)
349 #define SM501_DC_ALPHA_COLOR_LOOKUP (0x118)
350
351 #define SM501_DC_CRT_CONTROL (0x200)
352
353 #define SM501_DC_CRT_CONTROL_TVP (1 << 15)
354 #define SM501_DC_CRT_CONTROL_CP (1 << 14)
355 #define SM501_DC_CRT_CONTROL_VSP (1 << 13)
356 #define SM501_DC_CRT_CONTROL_HSP (1 << 12)
357 #define SM501_DC_CRT_CONTROL_VS (1 << 11)
358 #define SM501_DC_CRT_CONTROL_BLANK (1 << 10)
359 #define SM501_DC_CRT_CONTROL_SEL (1 << 9)
360 #define SM501_DC_CRT_CONTROL_TE (1 << 8)
361 #define SM501_DC_CRT_CONTROL_PIXEL_MASK (0xF << 4)
362 #define SM501_DC_CRT_CONTROL_GAMMA (1 << 3)
363 #define SM501_DC_CRT_CONTROL_ENABLE (1 << 2)
364
365 #define SM501_DC_CRT_CONTROL_8BPP (0 << 0)
366 #define SM501_DC_CRT_CONTROL_16BPP (1 << 0)
367 #define SM501_DC_CRT_CONTROL_32BPP (2 << 0)
368
369 #define SM501_DC_CRT_FB_ADDR (0x204)
370 #define SM501_DC_CRT_FB_OFFSET (0x208)
371 #define SM501_DC_CRT_H_TOT (0x20C)
372 #define SM501_DC_CRT_H_SYNC (0x210)
373 #define SM501_DC_CRT_V_TOT (0x214)
374 #define SM501_DC_CRT_V_SYNC (0x218)
375 #define SM501_DC_CRT_SIGNATURE_ANALYZER (0x21C)
376 #define SM501_DC_CRT_CUR_LINE (0x220)
377 #define SM501_DC_CRT_MONITOR_DETECT (0x224)
378
379 #define SM501_DC_CRT_HWC_BASE (0x230)
380 #define SM501_DC_CRT_HWC_ADDR (0x230)
381 #define SM501_DC_CRT_HWC_LOC (0x234)
382 #define SM501_DC_CRT_HWC_COLOR_1_2 (0x238)
383 #define SM501_DC_CRT_HWC_COLOR_3 (0x23C)
384
385 #define SM501_DC_PANEL_PALETTE (0x400)
386
387 #define SM501_DC_VIDEO_PALETTE (0x800)
388
389 #define SM501_DC_CRT_PALETTE (0xC00)
390
391 /* Zoom Video port base */
392 #define SM501_ZVPORT (0x090000)
393
394 /* AC97/I2S base */
395 #define SM501_AC97 (0x0A0000)
396
397 /* 8051 micro controller base */
398 #define SM501_UCONTROLLER (0x0B0000)
399
400 /* 8051 micro controller SRAM base */
401 #define SM501_UCONTROLLER_SRAM (0x0C0000)
402
403 /* DMA base */
404 #define SM501_DMA (0x0D0000)
405
406 /* 2d engine base */
407 #define SM501_2D_ENGINE (0x100000)
408 #define SM501_2D_SOURCE (0x00)
409 #define SM501_2D_DESTINATION (0x04)
410 #define SM501_2D_DIMENSION (0x08)
411 #define SM501_2D_CONTROL (0x0C)
412 #define SM501_2D_PITCH (0x10)
413 #define SM501_2D_FOREGROUND (0x14)
414 #define SM501_2D_BACKGROUND (0x18)
415 #define SM501_2D_STRETCH (0x1C)
416 #define SM501_2D_COLOR_COMPARE (0x20)
417 #define SM501_2D_COLOR_COMPARE_MASK (0x24)
418 #define SM501_2D_MASK (0x28)
419 #define SM501_2D_CLIP_TL (0x2C)
420 #define SM501_2D_CLIP_BR (0x30)
421 #define SM501_2D_MONO_PATTERN_LOW (0x34)
422 #define SM501_2D_MONO_PATTERN_HIGH (0x38)
423 #define SM501_2D_WINDOW_WIDTH (0x3C)
424 #define SM501_2D_SOURCE_BASE (0x40)
425 #define SM501_2D_DESTINATION_BASE (0x44)
426 #define SM501_2D_ALPHA (0x48)
427 #define SM501_2D_WRAP (0x4C)
428 #define SM501_2D_STATUS (0x50)
429
430 #define SM501_CSC_Y_SOURCE_BASE (0xC8)
431 #define SM501_CSC_CONSTANTS (0xCC)
432 #define SM501_CSC_Y_SOURCE_X (0xD0)
433 #define SM501_CSC_Y_SOURCE_Y (0xD4)
434 #define SM501_CSC_U_SOURCE_BASE (0xD8)
435 #define SM501_CSC_V_SOURCE_BASE (0xDC)
436 #define SM501_CSC_SOURCE_DIMENSION (0xE0)
437 #define SM501_CSC_SOURCE_PITCH (0xE4)
438 #define SM501_CSC_DESTINATION (0xE8)
439 #define SM501_CSC_DESTINATION_DIMENSION (0xEC)
440 #define SM501_CSC_DESTINATION_PITCH (0xF0)
441 #define SM501_CSC_SCALE_FACTOR (0xF4)
442 #define SM501_CSC_DESTINATION_BASE (0xF8)
443 #define SM501_CSC_CONTROL (0xFC)
444
445 /* 2d engine data port base */
446 #define SM501_2D_ENGINE_DATA (0x110000)
447
448 /* end of register definitions */
449
450 #define SM501_HWC_WIDTH (64)
451 #define SM501_HWC_HEIGHT (64)
452
453 /* SM501 local memory size taken from "linux/drivers/mfd/sm501.c" */
454 static const uint32_t sm501_mem_local_size[] = {
455 [0] = 4 * M_BYTE,
456 [1] = 8 * M_BYTE,
457 [2] = 16 * M_BYTE,
458 [3] = 32 * M_BYTE,
459 [4] = 64 * M_BYTE,
460 [5] = 2 * M_BYTE,
461 };
462 #define get_local_mem_size(s) sm501_mem_local_size[(s)->local_mem_size_index]
463
464 typedef struct SM501State {
465 /* graphic console status */
466 QemuConsole *con;
467
468 /* status & internal resources */
469 uint32_t local_mem_size_index;
470 uint8_t *local_mem;
471 MemoryRegion local_mem_region;
472 MemoryRegion mmio_region;
473 MemoryRegion system_config_region;
474 MemoryRegion disp_ctrl_region;
475 MemoryRegion twoD_engine_region;
476 uint32_t last_width;
477 uint32_t last_height;
478
479 /* mmio registers */
480 uint32_t system_control;
481 uint32_t misc_control;
482 uint32_t gpio_31_0_control;
483 uint32_t gpio_63_32_control;
484 uint32_t dram_control;
485 uint32_t arbitration_control;
486 uint32_t irq_mask;
487 uint32_t misc_timing;
488 uint32_t power_mode_control;
489
490 uint32_t uart0_ier;
491 uint32_t uart0_lcr;
492 uint32_t uart0_mcr;
493 uint32_t uart0_scr;
494
495 uint8_t dc_palette[0x400 * 3];
496
497 uint32_t dc_panel_control;
498 uint32_t dc_panel_panning_control;
499 uint32_t dc_panel_fb_addr;
500 uint32_t dc_panel_fb_offset;
501 uint32_t dc_panel_fb_width;
502 uint32_t dc_panel_fb_height;
503 uint32_t dc_panel_tl_location;
504 uint32_t dc_panel_br_location;
505 uint32_t dc_panel_h_total;
506 uint32_t dc_panel_h_sync;
507 uint32_t dc_panel_v_total;
508 uint32_t dc_panel_v_sync;
509
510 uint32_t dc_panel_hwc_addr;
511 uint32_t dc_panel_hwc_location;
512 uint32_t dc_panel_hwc_color_1_2;
513 uint32_t dc_panel_hwc_color_3;
514
515 uint32_t dc_crt_control;
516 uint32_t dc_crt_fb_addr;
517 uint32_t dc_crt_fb_offset;
518 uint32_t dc_crt_h_total;
519 uint32_t dc_crt_h_sync;
520 uint32_t dc_crt_v_total;
521 uint32_t dc_crt_v_sync;
522
523 uint32_t dc_crt_hwc_addr;
524 uint32_t dc_crt_hwc_location;
525 uint32_t dc_crt_hwc_color_1_2;
526 uint32_t dc_crt_hwc_color_3;
527
528 uint32_t twoD_source;
529 uint32_t twoD_destination;
530 uint32_t twoD_dimension;
531 uint32_t twoD_control;
532 uint32_t twoD_pitch;
533 uint32_t twoD_foreground;
534 uint32_t twoD_stretch;
535 uint32_t twoD_color_compare_mask;
536 uint32_t twoD_mask;
537 uint32_t twoD_window_width;
538 uint32_t twoD_source_base;
539 uint32_t twoD_destination_base;
540
541 } SM501State;
542
543 static uint32_t get_local_mem_size_index(uint32_t size)
544 {
545 uint32_t norm_size = 0;
546 int i, index = 0;
547
548 for (i = 0; i < ARRAY_SIZE(sm501_mem_local_size); i++) {
549 uint32_t new_size = sm501_mem_local_size[i];
550 if (new_size >= size) {
551 if (norm_size == 0 || norm_size > new_size) {
552 norm_size = new_size;
553 index = i;
554 }
555 }
556 }
557
558 return index;
559 }
560
561 static inline int get_width(SM501State *s, int crt)
562 {
563 int width = crt ? s->dc_crt_h_total : s->dc_panel_h_total;
564 return (width & 0x00000FFF) + 1;
565 }
566
567 static inline int get_height(SM501State *s, int crt)
568 {
569 int height = crt ? s->dc_crt_v_total : s->dc_panel_v_total;
570 return (height & 0x00000FFF) + 1;
571 }
572
573 static inline int get_bpp(SM501State *s, int crt)
574 {
575 int bpp = crt ? s->dc_crt_control : s->dc_panel_control;
576 return 1 << (bpp & 3);
577 }
578
579 /**
580 * Check the availability of hardware cursor.
581 * @param crt 0 for PANEL, 1 for CRT.
582 */
583 static inline int is_hwc_enabled(SM501State *state, int crt)
584 {
585 uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
586 return addr & SM501_HWC_EN;
587 }
588
589 /**
590 * Get the address which holds cursor pattern data.
591 * @param crt 0 for PANEL, 1 for CRT.
592 */
593 static inline uint8_t *get_hwc_address(SM501State *state, int crt)
594 {
595 uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
596 return state->local_mem + (addr & 0x03FFFFF0);
597 }
598
599 /**
600 * Get the cursor position in y coordinate.
601 * @param crt 0 for PANEL, 1 for CRT.
602 */
603 static inline uint32_t get_hwc_y(SM501State *state, int crt)
604 {
605 uint32_t location = crt ? state->dc_crt_hwc_location
606 : state->dc_panel_hwc_location;
607 return (location & 0x07FF0000) >> 16;
608 }
609
610 /**
611 * Get the cursor position in x coordinate.
612 * @param crt 0 for PANEL, 1 for CRT.
613 */
614 static inline uint32_t get_hwc_x(SM501State *state, int crt)
615 {
616 uint32_t location = crt ? state->dc_crt_hwc_location
617 : state->dc_panel_hwc_location;
618 return location & 0x000007FF;
619 }
620
621 /**
622 * Get the hardware cursor palette.
623 * @param crt 0 for PANEL, 1 for CRT.
624 * @param palette pointer to a [3 * 3] array to store color values in
625 */
626 static inline void get_hwc_palette(SM501State *state, int crt, uint8_t *palette)
627 {
628 int i;
629 uint32_t color_reg;
630 uint16_t rgb565;
631
632 for (i = 0; i < 3; i++) {
633 if (i + 1 == 3) {
634 color_reg = crt ? state->dc_crt_hwc_color_3
635 : state->dc_panel_hwc_color_3;
636 } else {
637 color_reg = crt ? state->dc_crt_hwc_color_1_2
638 : state->dc_panel_hwc_color_1_2;
639 }
640
641 if (i + 1 == 2) {
642 rgb565 = (color_reg >> 16) & 0xFFFF;
643 } else {
644 rgb565 = color_reg & 0xFFFF;
645 }
646 palette[i * 3 + 0] = (rgb565 << 3) & 0xf8; /* red */
647 palette[i * 3 + 1] = (rgb565 >> 3) & 0xfc; /* green */
648 palette[i * 3 + 2] = (rgb565 >> 8) & 0xf8; /* blue */
649 }
650 }
651
652 static inline void hwc_invalidate(SM501State *s, int crt)
653 {
654 int w = get_width(s, crt);
655 int h = get_height(s, crt);
656 int bpp = get_bpp(s, crt);
657 int start = get_hwc_y(s, crt);
658 int end = MIN(h, start + SM501_HWC_HEIGHT) + 1;
659
660 start *= w * bpp;
661 end *= w * bpp;
662
663 memory_region_set_dirty(&s->local_mem_region, start, end - start);
664 }
665
666 static void sm501_2d_operation(SM501State *s)
667 {
668 /* obtain operation parameters */
669 int operation = (s->twoD_control >> 16) & 0x1f;
670 int rtl = s->twoD_control & 0x8000000;
671 int src_x = (s->twoD_source >> 16) & 0x01FFF;
672 int src_y = s->twoD_source & 0xFFFF;
673 int dst_x = (s->twoD_destination >> 16) & 0x01FFF;
674 int dst_y = s->twoD_destination & 0xFFFF;
675 int operation_width = (s->twoD_dimension >> 16) & 0x1FFF;
676 int operation_height = s->twoD_dimension & 0xFFFF;
677 uint32_t color = s->twoD_foreground;
678 int format_flags = (s->twoD_stretch >> 20) & 0x3;
679 int addressing = (s->twoD_stretch >> 16) & 0xF;
680
681 /* get frame buffer info */
682 uint8_t *src = s->local_mem + (s->twoD_source_base & 0x03FFFFFF);
683 uint8_t *dst = s->local_mem + (s->twoD_destination_base & 0x03FFFFFF);
684 int src_width = (s->dc_crt_h_total & 0x00000FFF) + 1;
685 int dst_width = (s->dc_crt_h_total & 0x00000FFF) + 1;
686
687 if (addressing != 0x0) {
688 printf("%s: only XY addressing is supported.\n", __func__);
689 abort();
690 }
691
692 if ((s->twoD_source_base & 0x08000000) ||
693 (s->twoD_destination_base & 0x08000000)) {
694 printf("%s: only local memory is supported.\n", __func__);
695 abort();
696 }
697
698 switch (operation) {
699 case 0x00: /* copy area */
700 #define COPY_AREA(_bpp, _pixel_type, rtl) { \
701 int y, x, index_d, index_s; \
702 for (y = 0; y < operation_height; y++) { \
703 for (x = 0; x < operation_width; x++) { \
704 if (rtl) { \
705 index_s = ((src_y - y) * src_width + src_x - x) * _bpp; \
706 index_d = ((dst_y - y) * dst_width + dst_x - x) * _bpp; \
707 } else { \
708 index_s = ((src_y + y) * src_width + src_x + x) * _bpp; \
709 index_d = ((dst_y + y) * dst_width + dst_x + x) * _bpp; \
710 } \
711 *(_pixel_type *)&dst[index_d] = *(_pixel_type *)&src[index_s];\
712 } \
713 } \
714 }
715 switch (format_flags) {
716 case 0:
717 COPY_AREA(1, uint8_t, rtl);
718 break;
719 case 1:
720 COPY_AREA(2, uint16_t, rtl);
721 break;
722 case 2:
723 COPY_AREA(4, uint32_t, rtl);
724 break;
725 }
726 break;
727
728 case 0x01: /* fill rectangle */
729 #define FILL_RECT(_bpp, _pixel_type) { \
730 int y, x; \
731 for (y = 0; y < operation_height; y++) { \
732 for (x = 0; x < operation_width; x++) { \
733 int index = ((dst_y + y) * dst_width + dst_x + x) * _bpp; \
734 *(_pixel_type *)&dst[index] = (_pixel_type)color; \
735 } \
736 } \
737 }
738
739 switch (format_flags) {
740 case 0:
741 FILL_RECT(1, uint8_t);
742 break;
743 case 1:
744 FILL_RECT(2, uint16_t);
745 break;
746 case 2:
747 FILL_RECT(4, uint32_t);
748 break;
749 }
750 break;
751
752 default:
753 printf("non-implemented SM501 2D operation. %d\n", operation);
754 abort();
755 break;
756 }
757 }
758
759 static uint64_t sm501_system_config_read(void *opaque, hwaddr addr,
760 unsigned size)
761 {
762 SM501State *s = (SM501State *)opaque;
763 uint32_t ret = 0;
764 SM501_DPRINTF("sm501 system config regs : read addr=%x\n", (int)addr);
765
766 switch (addr) {
767 case SM501_SYSTEM_CONTROL:
768 ret = s->system_control;
769 break;
770 case SM501_MISC_CONTROL:
771 ret = s->misc_control;
772 break;
773 case SM501_GPIO31_0_CONTROL:
774 ret = s->gpio_31_0_control;
775 break;
776 case SM501_GPIO63_32_CONTROL:
777 ret = s->gpio_63_32_control;
778 break;
779 case SM501_DEVICEID:
780 ret = 0x050100A0;
781 break;
782 case SM501_DRAM_CONTROL:
783 ret = (s->dram_control & 0x07F107C0) | s->local_mem_size_index << 13;
784 break;
785 case SM501_ARBTRTN_CONTROL:
786 ret = s->arbitration_control;
787 break;
788 case SM501_IRQ_MASK:
789 ret = s->irq_mask;
790 break;
791 case SM501_MISC_TIMING:
792 /* TODO : simulate gate control */
793 ret = s->misc_timing;
794 break;
795 case SM501_CURRENT_GATE:
796 /* TODO : simulate gate control */
797 ret = 0x00021807;
798 break;
799 case SM501_CURRENT_CLOCK:
800 ret = 0x2A1A0A09;
801 break;
802 case SM501_POWER_MODE_CONTROL:
803 ret = s->power_mode_control;
804 break;
805
806 default:
807 printf("sm501 system config : not implemented register read."
808 " addr=%x\n", (int)addr);
809 abort();
810 }
811
812 return ret;
813 }
814
815 static void sm501_system_config_write(void *opaque, hwaddr addr,
816 uint64_t value, unsigned size)
817 {
818 SM501State *s = (SM501State *)opaque;
819 SM501_DPRINTF("sm501 system config regs : write addr=%x, val=%x\n",
820 (uint32_t)addr, (uint32_t)value);
821
822 switch (addr) {
823 case SM501_SYSTEM_CONTROL:
824 s->system_control = value & 0xE300B8F7;
825 break;
826 case SM501_MISC_CONTROL:
827 s->misc_control = value & 0xFF7FFF20;
828 break;
829 case SM501_GPIO31_0_CONTROL:
830 s->gpio_31_0_control = value;
831 break;
832 case SM501_GPIO63_32_CONTROL:
833 s->gpio_63_32_control = value;
834 break;
835 case SM501_DRAM_CONTROL:
836 s->local_mem_size_index = (value >> 13) & 0x7;
837 /* TODO : check validity of size change */
838 s->dram_control |= value & 0x7FFFFFC3;
839 break;
840 case SM501_ARBTRTN_CONTROL:
841 s->arbitration_control = value & 0x37777777;
842 break;
843 case SM501_IRQ_MASK:
844 s->irq_mask = value;
845 break;
846 case SM501_MISC_TIMING:
847 s->misc_timing = value & 0xF31F1FFF;
848 break;
849 case SM501_POWER_MODE_0_GATE:
850 case SM501_POWER_MODE_1_GATE:
851 case SM501_POWER_MODE_0_CLOCK:
852 case SM501_POWER_MODE_1_CLOCK:
853 /* TODO : simulate gate & clock control */
854 break;
855 case SM501_POWER_MODE_CONTROL:
856 s->power_mode_control = value & 0x00000003;
857 break;
858
859 default:
860 printf("sm501 system config : not implemented register write."
861 " addr=%x, val=%x\n", (int)addr, (uint32_t)value);
862 abort();
863 }
864 }
865
866 static const MemoryRegionOps sm501_system_config_ops = {
867 .read = sm501_system_config_read,
868 .write = sm501_system_config_write,
869 .valid = {
870 .min_access_size = 4,
871 .max_access_size = 4,
872 },
873 .endianness = DEVICE_LITTLE_ENDIAN,
874 };
875
876 static uint32_t sm501_palette_read(void *opaque, hwaddr addr)
877 {
878 SM501State *s = (SM501State *)opaque;
879 SM501_DPRINTF("sm501 palette read addr=%x\n", (int)addr);
880
881 /* TODO : consider BYTE/WORD access */
882 /* TODO : consider endian */
883
884 assert(range_covers_byte(0, 0x400 * 3, addr));
885 return *(uint32_t *)&s->dc_palette[addr];
886 }
887
888 static void sm501_palette_write(void *opaque, hwaddr addr,
889 uint32_t value)
890 {
891 SM501State *s = (SM501State *)opaque;
892 SM501_DPRINTF("sm501 palette write addr=%x, val=%x\n",
893 (int)addr, value);
894
895 /* TODO : consider BYTE/WORD access */
896 /* TODO : consider endian */
897
898 assert(range_covers_byte(0, 0x400 * 3, addr));
899 *(uint32_t *)&s->dc_palette[addr] = value;
900 }
901
902 static uint64_t sm501_disp_ctrl_read(void *opaque, hwaddr addr,
903 unsigned size)
904 {
905 SM501State *s = (SM501State *)opaque;
906 uint32_t ret = 0;
907 SM501_DPRINTF("sm501 disp ctrl regs : read addr=%x\n", (int)addr);
908
909 switch (addr) {
910
911 case SM501_DC_PANEL_CONTROL:
912 ret = s->dc_panel_control;
913 break;
914 case SM501_DC_PANEL_PANNING_CONTROL:
915 ret = s->dc_panel_panning_control;
916 break;
917 case SM501_DC_PANEL_FB_ADDR:
918 ret = s->dc_panel_fb_addr;
919 break;
920 case SM501_DC_PANEL_FB_OFFSET:
921 ret = s->dc_panel_fb_offset;
922 break;
923 case SM501_DC_PANEL_FB_WIDTH:
924 ret = s->dc_panel_fb_width;
925 break;
926 case SM501_DC_PANEL_FB_HEIGHT:
927 ret = s->dc_panel_fb_height;
928 break;
929 case SM501_DC_PANEL_TL_LOC:
930 ret = s->dc_panel_tl_location;
931 break;
932 case SM501_DC_PANEL_BR_LOC:
933 ret = s->dc_panel_br_location;
934 break;
935
936 case SM501_DC_PANEL_H_TOT:
937 ret = s->dc_panel_h_total;
938 break;
939 case SM501_DC_PANEL_H_SYNC:
940 ret = s->dc_panel_h_sync;
941 break;
942 case SM501_DC_PANEL_V_TOT:
943 ret = s->dc_panel_v_total;
944 break;
945 case SM501_DC_PANEL_V_SYNC:
946 ret = s->dc_panel_v_sync;
947 break;
948
949 case SM501_DC_CRT_CONTROL:
950 ret = s->dc_crt_control;
951 break;
952 case SM501_DC_CRT_FB_ADDR:
953 ret = s->dc_crt_fb_addr;
954 break;
955 case SM501_DC_CRT_FB_OFFSET:
956 ret = s->dc_crt_fb_offset;
957 break;
958 case SM501_DC_CRT_H_TOT:
959 ret = s->dc_crt_h_total;
960 break;
961 case SM501_DC_CRT_H_SYNC:
962 ret = s->dc_crt_h_sync;
963 break;
964 case SM501_DC_CRT_V_TOT:
965 ret = s->dc_crt_v_total;
966 break;
967 case SM501_DC_CRT_V_SYNC:
968 ret = s->dc_crt_v_sync;
969 break;
970
971 case SM501_DC_CRT_HWC_ADDR:
972 ret = s->dc_crt_hwc_addr;
973 break;
974 case SM501_DC_CRT_HWC_LOC:
975 ret = s->dc_crt_hwc_location;
976 break;
977 case SM501_DC_CRT_HWC_COLOR_1_2:
978 ret = s->dc_crt_hwc_color_1_2;
979 break;
980 case SM501_DC_CRT_HWC_COLOR_3:
981 ret = s->dc_crt_hwc_color_3;
982 break;
983
984 case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400 * 3 - 4:
985 ret = sm501_palette_read(opaque, addr - SM501_DC_PANEL_PALETTE);
986 break;
987
988 default:
989 printf("sm501 disp ctrl : not implemented register read."
990 " addr=%x\n", (int)addr);
991 abort();
992 }
993
994 return ret;
995 }
996
997 static void sm501_disp_ctrl_write(void *opaque, hwaddr addr,
998 uint64_t value, unsigned size)
999 {
1000 SM501State *s = (SM501State *)opaque;
1001 SM501_DPRINTF("sm501 disp ctrl regs : write addr=%x, val=%x\n",
1002 (unsigned)addr, (unsigned)value);
1003
1004 switch (addr) {
1005 case SM501_DC_PANEL_CONTROL:
1006 s->dc_panel_control = value & 0x0FFF73FF;
1007 break;
1008 case SM501_DC_PANEL_PANNING_CONTROL:
1009 s->dc_panel_panning_control = value & 0xFF3FFF3F;
1010 break;
1011 case SM501_DC_PANEL_FB_ADDR:
1012 s->dc_panel_fb_addr = value & 0x8FFFFFF0;
1013 break;
1014 case SM501_DC_PANEL_FB_OFFSET:
1015 s->dc_panel_fb_offset = value & 0x3FF03FF0;
1016 break;
1017 case SM501_DC_PANEL_FB_WIDTH:
1018 s->dc_panel_fb_width = value & 0x0FFF0FFF;
1019 break;
1020 case SM501_DC_PANEL_FB_HEIGHT:
1021 s->dc_panel_fb_height = value & 0x0FFF0FFF;
1022 break;
1023 case SM501_DC_PANEL_TL_LOC:
1024 s->dc_panel_tl_location = value & 0x07FF07FF;
1025 break;
1026 case SM501_DC_PANEL_BR_LOC:
1027 s->dc_panel_br_location = value & 0x07FF07FF;
1028 break;
1029
1030 case SM501_DC_PANEL_H_TOT:
1031 s->dc_panel_h_total = value & 0x0FFF0FFF;
1032 break;
1033 case SM501_DC_PANEL_H_SYNC:
1034 s->dc_panel_h_sync = value & 0x00FF0FFF;
1035 break;
1036 case SM501_DC_PANEL_V_TOT:
1037 s->dc_panel_v_total = value & 0x0FFF0FFF;
1038 break;
1039 case SM501_DC_PANEL_V_SYNC:
1040 s->dc_panel_v_sync = value & 0x003F0FFF;
1041 break;
1042
1043 case SM501_DC_PANEL_HWC_ADDR:
1044 value &= 0x8FFFFFF0;
1045 if (value != s->dc_panel_hwc_addr) {
1046 hwc_invalidate(s, 0);
1047 s->dc_panel_hwc_addr = value;
1048 }
1049 break;
1050 case SM501_DC_PANEL_HWC_LOC:
1051 value &= 0x0FFF0FFF;
1052 if (value != s->dc_panel_hwc_location) {
1053 hwc_invalidate(s, 0);
1054 s->dc_panel_hwc_location = value;
1055 }
1056 break;
1057 case SM501_DC_PANEL_HWC_COLOR_1_2:
1058 s->dc_panel_hwc_color_1_2 = value;
1059 break;
1060 case SM501_DC_PANEL_HWC_COLOR_3:
1061 s->dc_panel_hwc_color_3 = value & 0x0000FFFF;
1062 break;
1063
1064 case SM501_DC_CRT_CONTROL:
1065 s->dc_crt_control = value & 0x0003FFFF;
1066 break;
1067 case SM501_DC_CRT_FB_ADDR:
1068 s->dc_crt_fb_addr = value & 0x8FFFFFF0;
1069 break;
1070 case SM501_DC_CRT_FB_OFFSET:
1071 s->dc_crt_fb_offset = value & 0x3FF03FF0;
1072 break;
1073 case SM501_DC_CRT_H_TOT:
1074 s->dc_crt_h_total = value & 0x0FFF0FFF;
1075 break;
1076 case SM501_DC_CRT_H_SYNC:
1077 s->dc_crt_h_sync = value & 0x00FF0FFF;
1078 break;
1079 case SM501_DC_CRT_V_TOT:
1080 s->dc_crt_v_total = value & 0x0FFF0FFF;
1081 break;
1082 case SM501_DC_CRT_V_SYNC:
1083 s->dc_crt_v_sync = value & 0x003F0FFF;
1084 break;
1085
1086 case SM501_DC_CRT_HWC_ADDR:
1087 value &= 0x8FFFFFF0;
1088 if (value != s->dc_crt_hwc_addr) {
1089 hwc_invalidate(s, 1);
1090 s->dc_crt_hwc_addr = value;
1091 }
1092 break;
1093 case SM501_DC_CRT_HWC_LOC:
1094 value &= 0x0FFF0FFF;
1095 if (value != s->dc_crt_hwc_location) {
1096 hwc_invalidate(s, 1);
1097 s->dc_crt_hwc_location = value;
1098 }
1099 break;
1100 case SM501_DC_CRT_HWC_COLOR_1_2:
1101 s->dc_crt_hwc_color_1_2 = value;
1102 break;
1103 case SM501_DC_CRT_HWC_COLOR_3:
1104 s->dc_crt_hwc_color_3 = value & 0x0000FFFF;
1105 break;
1106
1107 case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400 * 3 - 4:
1108 sm501_palette_write(opaque, addr - SM501_DC_PANEL_PALETTE, value);
1109 break;
1110
1111 default:
1112 printf("sm501 disp ctrl : not implemented register write."
1113 " addr=%x, val=%x\n", (int)addr, (unsigned)value);
1114 abort();
1115 }
1116 }
1117
1118 static const MemoryRegionOps sm501_disp_ctrl_ops = {
1119 .read = sm501_disp_ctrl_read,
1120 .write = sm501_disp_ctrl_write,
1121 .valid = {
1122 .min_access_size = 4,
1123 .max_access_size = 4,
1124 },
1125 .endianness = DEVICE_LITTLE_ENDIAN,
1126 };
1127
1128 static uint64_t sm501_2d_engine_read(void *opaque, hwaddr addr,
1129 unsigned size)
1130 {
1131 SM501State *s = (SM501State *)opaque;
1132 uint32_t ret = 0;
1133 SM501_DPRINTF("sm501 2d engine regs : read addr=%x\n", (int)addr);
1134
1135 switch (addr) {
1136 case SM501_2D_SOURCE_BASE:
1137 ret = s->twoD_source_base;
1138 break;
1139 default:
1140 printf("sm501 disp ctrl : not implemented register read."
1141 " addr=%x\n", (int)addr);
1142 abort();
1143 }
1144
1145 return ret;
1146 }
1147
1148 static void sm501_2d_engine_write(void *opaque, hwaddr addr,
1149 uint64_t value, unsigned size)
1150 {
1151 SM501State *s = (SM501State *)opaque;
1152 SM501_DPRINTF("sm501 2d engine regs : write addr=%x, val=%x\n",
1153 (unsigned)addr, (unsigned)value);
1154
1155 switch (addr) {
1156 case SM501_2D_SOURCE:
1157 s->twoD_source = value;
1158 break;
1159 case SM501_2D_DESTINATION:
1160 s->twoD_destination = value;
1161 break;
1162 case SM501_2D_DIMENSION:
1163 s->twoD_dimension = value;
1164 break;
1165 case SM501_2D_CONTROL:
1166 s->twoD_control = value;
1167
1168 /* do 2d operation if start flag is set. */
1169 if (value & 0x80000000) {
1170 sm501_2d_operation(s);
1171 s->twoD_control &= ~0x80000000; /* start flag down */
1172 }
1173
1174 break;
1175 case SM501_2D_PITCH:
1176 s->twoD_pitch = value;
1177 break;
1178 case SM501_2D_FOREGROUND:
1179 s->twoD_foreground = value;
1180 break;
1181 case SM501_2D_STRETCH:
1182 s->twoD_stretch = value;
1183 break;
1184 case SM501_2D_COLOR_COMPARE_MASK:
1185 s->twoD_color_compare_mask = value;
1186 break;
1187 case SM501_2D_MASK:
1188 s->twoD_mask = value;
1189 break;
1190 case SM501_2D_WINDOW_WIDTH:
1191 s->twoD_window_width = value;
1192 break;
1193 case SM501_2D_SOURCE_BASE:
1194 s->twoD_source_base = value;
1195 break;
1196 case SM501_2D_DESTINATION_BASE:
1197 s->twoD_destination_base = value;
1198 break;
1199 default:
1200 printf("sm501 2d engine : not implemented register write."
1201 " addr=%x, val=%x\n", (int)addr, (unsigned)value);
1202 abort();
1203 }
1204 }
1205
1206 static const MemoryRegionOps sm501_2d_engine_ops = {
1207 .read = sm501_2d_engine_read,
1208 .write = sm501_2d_engine_write,
1209 .valid = {
1210 .min_access_size = 4,
1211 .max_access_size = 4,
1212 },
1213 .endianness = DEVICE_LITTLE_ENDIAN,
1214 };
1215
1216 /* draw line functions for all console modes */
1217
1218 typedef void draw_line_func(uint8_t *d, const uint8_t *s,
1219 int width, const uint32_t *pal);
1220
1221 typedef void draw_hwc_line_func(uint8_t *d, const uint8_t *s,
1222 int width, const uint8_t *palette,
1223 int c_x, int c_y);
1224
1225 #define DEPTH 8
1226 #include "sm501_template.h"
1227
1228 #define DEPTH 15
1229 #include "sm501_template.h"
1230
1231 #define BGR_FORMAT
1232 #define DEPTH 15
1233 #include "sm501_template.h"
1234
1235 #define DEPTH 16
1236 #include "sm501_template.h"
1237
1238 #define BGR_FORMAT
1239 #define DEPTH 16
1240 #include "sm501_template.h"
1241
1242 #define DEPTH 32
1243 #include "sm501_template.h"
1244
1245 #define BGR_FORMAT
1246 #define DEPTH 32
1247 #include "sm501_template.h"
1248
1249 static draw_line_func *draw_line8_funcs[] = {
1250 draw_line8_8,
1251 draw_line8_15,
1252 draw_line8_16,
1253 draw_line8_32,
1254 draw_line8_32bgr,
1255 draw_line8_15bgr,
1256 draw_line8_16bgr,
1257 };
1258
1259 static draw_line_func *draw_line16_funcs[] = {
1260 draw_line16_8,
1261 draw_line16_15,
1262 draw_line16_16,
1263 draw_line16_32,
1264 draw_line16_32bgr,
1265 draw_line16_15bgr,
1266 draw_line16_16bgr,
1267 };
1268
1269 static draw_line_func *draw_line32_funcs[] = {
1270 draw_line32_8,
1271 draw_line32_15,
1272 draw_line32_16,
1273 draw_line32_32,
1274 draw_line32_32bgr,
1275 draw_line32_15bgr,
1276 draw_line32_16bgr,
1277 };
1278
1279 static draw_hwc_line_func *draw_hwc_line_funcs[] = {
1280 draw_hwc_line_8,
1281 draw_hwc_line_15,
1282 draw_hwc_line_16,
1283 draw_hwc_line_32,
1284 draw_hwc_line_32bgr,
1285 draw_hwc_line_15bgr,
1286 draw_hwc_line_16bgr,
1287 };
1288
1289 static inline int get_depth_index(DisplaySurface *surface)
1290 {
1291 switch (surface_bits_per_pixel(surface)) {
1292 default:
1293 case 8:
1294 return 0;
1295 case 15:
1296 return 1;
1297 case 16:
1298 return 2;
1299 case 32:
1300 if (is_surface_bgr(surface)) {
1301 return 4;
1302 } else {
1303 return 3;
1304 }
1305 }
1306 }
1307
1308 static void sm501_update_display(void *opaque)
1309 {
1310 SM501State *s = (SM501State *)opaque;
1311 DisplaySurface *surface = qemu_console_surface(s->con);
1312 int y, c_x = 0, c_y = 0;
1313 int crt = (s->dc_crt_control & SM501_DC_CRT_CONTROL_SEL) ? 1 : 0;
1314 int width = get_width(s, crt);
1315 int height = get_height(s, crt);
1316 int src_bpp = get_bpp(s, crt);
1317 int dst_bpp = surface_bytes_per_pixel(surface);
1318 int dst_depth_index = get_depth_index(surface);
1319 draw_line_func *draw_line = NULL;
1320 draw_hwc_line_func *draw_hwc_line = NULL;
1321 int full_update = 0;
1322 int y_start = -1;
1323 ram_addr_t page_min = ~0l;
1324 ram_addr_t page_max = 0l;
1325 ram_addr_t offset;
1326 uint32_t *palette;
1327 uint8_t hwc_palette[3 * 3];
1328 uint8_t *hwc_src = NULL;
1329
1330 if (!((crt ? s->dc_crt_control : s->dc_panel_control)
1331 & SM501_DC_CRT_CONTROL_ENABLE)) {
1332 return;
1333 }
1334
1335 palette = (uint32_t *)(crt ? &s->dc_palette[SM501_DC_CRT_PALETTE -
1336 SM501_DC_PANEL_PALETTE]
1337 : &s->dc_palette[0]);
1338
1339 /* choose draw_line function */
1340 switch (src_bpp) {
1341 case 1:
1342 draw_line = draw_line8_funcs[dst_depth_index];
1343 break;
1344 case 2:
1345 draw_line = draw_line16_funcs[dst_depth_index];
1346 break;
1347 case 4:
1348 draw_line = draw_line32_funcs[dst_depth_index];
1349 break;
1350 default:
1351 printf("sm501 update display : invalid control register value.\n");
1352 abort();
1353 break;
1354 }
1355
1356 /* set up to draw hardware cursor */
1357 if (is_hwc_enabled(s, crt)) {
1358 /* choose cursor draw line function */
1359 draw_hwc_line = draw_hwc_line_funcs[dst_depth_index];
1360 hwc_src = get_hwc_address(s, crt);
1361 c_x = get_hwc_x(s, crt);
1362 c_y = get_hwc_y(s, crt);
1363 get_hwc_palette(s, crt, hwc_palette);
1364 }
1365
1366 /* adjust console size */
1367 if (s->last_width != width || s->last_height != height) {
1368 qemu_console_resize(s->con, width, height);
1369 surface = qemu_console_surface(s->con);
1370 s->last_width = width;
1371 s->last_height = height;
1372 full_update = 1;
1373 }
1374
1375 /* draw each line according to conditions */
1376 memory_region_sync_dirty_bitmap(&s->local_mem_region);
1377 for (y = 0, offset = 0; y < height; y++, offset += width * src_bpp) {
1378 int update, update_hwc;
1379 ram_addr_t page0 = offset;
1380 ram_addr_t page1 = offset + width * src_bpp - 1;
1381
1382 /* check if hardware cursor is enabled and we're within its range */
1383 update_hwc = draw_hwc_line && c_y <= y && y < c_y + SM501_HWC_HEIGHT;
1384 update = full_update || update_hwc;
1385 /* check dirty flags for each line */
1386 update |= memory_region_get_dirty(&s->local_mem_region, page0,
1387 page1 - page0, DIRTY_MEMORY_VGA);
1388
1389 /* draw line and change status */
1390 if (update) {
1391 uint8_t *d = surface_data(surface);
1392 d += y * width * dst_bpp;
1393
1394 /* draw graphics layer */
1395 draw_line(d, s->local_mem + offset, width, palette);
1396
1397 /* draw hardware cursor */
1398 if (update_hwc) {
1399 draw_hwc_line(d, hwc_src, width, hwc_palette, c_x, y - c_y);
1400 }
1401
1402 if (y_start < 0) {
1403 y_start = y;
1404 }
1405 if (page0 < page_min) {
1406 page_min = page0;
1407 }
1408 if (page1 > page_max) {
1409 page_max = page1;
1410 }
1411 } else {
1412 if (y_start >= 0) {
1413 /* flush to display */
1414 dpy_gfx_update(s->con, 0, y_start, width, y - y_start);
1415 y_start = -1;
1416 }
1417 }
1418 }
1419
1420 /* complete flush to display */
1421 if (y_start >= 0) {
1422 dpy_gfx_update(s->con, 0, y_start, width, y - y_start);
1423 }
1424
1425 /* clear dirty flags */
1426 if (page_min != ~0l) {
1427 memory_region_reset_dirty(&s->local_mem_region,
1428 page_min, page_max + TARGET_PAGE_SIZE,
1429 DIRTY_MEMORY_VGA);
1430 }
1431 }
1432
1433 static const GraphicHwOps sm501_ops = {
1434 .gfx_update = sm501_update_display,
1435 };
1436
1437 static void sm501_reset(SM501State *s)
1438 {
1439 s->system_control = 0x00100000; /* 2D engine FIFO empty */
1440 /* Bits 17 (SH), 7 (CDR), 6:5 (Test), 2:0 (Bus) are all supposed
1441 * to be determined at reset by GPIO lines which set config bits.
1442 * We hardwire them:
1443 * SH = 0 : Hitachi Ready Polarity == Active Low
1444 * CDR = 0 : do not reset clock divider
1445 * TEST = 0 : Normal mode (not testing the silicon)
1446 * BUS = 0 : Hitachi SH3/SH4
1447 */
1448 s->misc_control = SM501_MISC_DAC_POWER;
1449 s->gpio_31_0_control = 0;
1450 s->gpio_63_32_control = 0;
1451 s->dram_control = 0;
1452 s->arbitration_control = 0x05146732;
1453 s->irq_mask = 0;
1454 s->misc_timing = 0;
1455 s->power_mode_control = 0;
1456 s->dc_panel_control = 0x00010000; /* FIFO level 3 */
1457 s->dc_crt_control = 0x00010000;
1458 s->twoD_control = 0;
1459 }
1460
1461 static void sm501_init(SM501State *s, DeviceState *dev,
1462 uint32_t local_mem_bytes)
1463 {
1464 s->local_mem_size_index = get_local_mem_size_index(local_mem_bytes);
1465 SM501_DPRINTF("sm501 local mem size=%x. index=%d\n", get_local_mem_size(s),
1466 s->local_mem_size_index);
1467
1468 /* local memory */
1469 memory_region_init_ram(&s->local_mem_region, OBJECT(dev), "sm501.local",
1470 get_local_mem_size(s), &error_fatal);
1471 vmstate_register_ram_global(&s->local_mem_region);
1472 memory_region_set_log(&s->local_mem_region, true, DIRTY_MEMORY_VGA);
1473 s->local_mem = memory_region_get_ram_ptr(&s->local_mem_region);
1474
1475 /* mmio */
1476 memory_region_init(&s->mmio_region, OBJECT(dev), "sm501.mmio", MMIO_SIZE);
1477 memory_region_init_io(&s->system_config_region, OBJECT(dev),
1478 &sm501_system_config_ops, s,
1479 "sm501-system-config", 0x6c);
1480 memory_region_add_subregion(&s->mmio_region, SM501_SYS_CONFIG,
1481 &s->system_config_region);
1482 memory_region_init_io(&s->disp_ctrl_region, OBJECT(dev),
1483 &sm501_disp_ctrl_ops, s,
1484 "sm501-disp-ctrl", 0x1000);
1485 memory_region_add_subregion(&s->mmio_region, SM501_DC,
1486 &s->disp_ctrl_region);
1487 memory_region_init_io(&s->twoD_engine_region, OBJECT(dev),
1488 &sm501_2d_engine_ops, s,
1489 "sm501-2d-engine", 0x54);
1490 memory_region_add_subregion(&s->mmio_region, SM501_2D_ENGINE,
1491 &s->twoD_engine_region);
1492
1493 /* create qemu graphic console */
1494 s->con = graphic_console_init(DEVICE(dev), 0, &sm501_ops, s);
1495 }
1496
1497 #define TYPE_SYSBUS_SM501 "sysbus-sm501"
1498 #define SYSBUS_SM501(obj) \
1499 OBJECT_CHECK(SM501SysBusState, (obj), TYPE_SYSBUS_SM501)
1500
1501 typedef struct {
1502 /*< private >*/
1503 SysBusDevice parent_obj;
1504 /*< public >*/
1505 SM501State state;
1506 uint32_t vram_size;
1507 uint32_t base;
1508 void *chr_state;
1509 } SM501SysBusState;
1510
1511 static void sm501_realize_sysbus(DeviceState *dev, Error **errp)
1512 {
1513 SM501SysBusState *s = SYSBUS_SM501(dev);
1514 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1515 DeviceState *usb_dev;
1516
1517 sm501_init(&s->state, dev, s->vram_size);
1518 if (get_local_mem_size(&s->state) != s->vram_size) {
1519 error_setg(errp, "Invalid VRAM size, nearest valid size is %" PRIu32,
1520 get_local_mem_size(&s->state));
1521 return;
1522 }
1523 sysbus_init_mmio(sbd, &s->state.local_mem_region);
1524 sysbus_init_mmio(sbd, &s->state.mmio_region);
1525
1526 /* bridge to usb host emulation module */
1527 usb_dev = qdev_create(NULL, "sysbus-ohci");
1528 qdev_prop_set_uint32(usb_dev, "num-ports", 2);
1529 qdev_prop_set_uint64(usb_dev, "dma-offset", s->base);
1530 qdev_init_nofail(usb_dev);
1531 memory_region_add_subregion(&s->state.mmio_region, SM501_USB_HOST,
1532 sysbus_mmio_get_region(SYS_BUS_DEVICE(usb_dev), 0));
1533 sysbus_pass_irq(sbd, SYS_BUS_DEVICE(usb_dev));
1534
1535 /* bridge to serial emulation module */
1536 if (s->chr_state) {
1537 serial_mm_init(&s->state.mmio_region, SM501_UART0, 2,
1538 NULL, /* TODO : chain irq to IRL */
1539 115200, s->chr_state, DEVICE_LITTLE_ENDIAN);
1540 }
1541 }
1542
1543 static Property sm501_sysbus_properties[] = {
1544 DEFINE_PROP_UINT32("vram-size", SM501SysBusState, vram_size, 0),
1545 DEFINE_PROP_UINT32("base", SM501SysBusState, base, 0),
1546 DEFINE_PROP_PTR("chr-state", SM501SysBusState, chr_state),
1547 DEFINE_PROP_END_OF_LIST(),
1548 };
1549
1550 static void sm501_reset_sysbus(DeviceState *dev)
1551 {
1552 SM501SysBusState *s = SYSBUS_SM501(dev);
1553 sm501_reset(&s->state);
1554 }
1555
1556 static void sm501_sysbus_class_init(ObjectClass *klass, void *data)
1557 {
1558 DeviceClass *dc = DEVICE_CLASS(klass);
1559
1560 dc->realize = sm501_realize_sysbus;
1561 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
1562 dc->desc = "SM501 Multimedia Companion";
1563 dc->props = sm501_sysbus_properties;
1564 dc->reset = sm501_reset_sysbus;
1565 /* Note: pointer property "chr-state" may remain null, thus
1566 * no need for dc->cannot_instantiate_with_device_add_yet = true;
1567 */
1568 }
1569
1570 static const TypeInfo sm501_sysbus_info = {
1571 .name = TYPE_SYSBUS_SM501,
1572 .parent = TYPE_SYS_BUS_DEVICE,
1573 .instance_size = sizeof(SM501SysBusState),
1574 .class_init = sm501_sysbus_class_init,
1575 };
1576
1577 #define TYPE_PCI_SM501 "sm501"
1578 #define PCI_SM501(obj) OBJECT_CHECK(SM501PCIState, (obj), TYPE_PCI_SM501)
1579
1580 typedef struct {
1581 /*< private >*/
1582 PCIDevice parent_obj;
1583 /*< public >*/
1584 SM501State state;
1585 uint32_t vram_size;
1586 } SM501PCIState;
1587
1588 static void sm501_realize_pci(PCIDevice *dev, Error **errp)
1589 {
1590 SM501PCIState *s = PCI_SM501(dev);
1591
1592 sm501_init(&s->state, DEVICE(dev), s->vram_size);
1593 if (get_local_mem_size(&s->state) != s->vram_size) {
1594 error_setg(errp, "Invalid VRAM size, nearest valid size is %" PRIu32,
1595 get_local_mem_size(&s->state));
1596 return;
1597 }
1598 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
1599 &s->state.local_mem_region);
1600 pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
1601 &s->state.mmio_region);
1602 }
1603
1604 static Property sm501_pci_properties[] = {
1605 DEFINE_PROP_UINT32("vram-size", SM501PCIState, vram_size, 64 * M_BYTE),
1606 DEFINE_PROP_END_OF_LIST(),
1607 };
1608
1609 static void sm501_reset_pci(DeviceState *dev)
1610 {
1611 SM501PCIState *s = PCI_SM501(dev);
1612 sm501_reset(&s->state);
1613 /* Bits 2:0 of misc_control register is 001 for PCI */
1614 s->state.misc_control |= 1;
1615 }
1616
1617 static void sm501_pci_class_init(ObjectClass *klass, void *data)
1618 {
1619 DeviceClass *dc = DEVICE_CLASS(klass);
1620 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1621
1622 k->realize = sm501_realize_pci;
1623 k->vendor_id = PCI_VENDOR_ID_SILICON_MOTION;
1624 k->device_id = PCI_DEVICE_ID_SM501;
1625 k->class_id = PCI_CLASS_DISPLAY_OTHER;
1626 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
1627 dc->desc = "SM501 Display Controller";
1628 dc->props = sm501_pci_properties;
1629 dc->reset = sm501_reset_pci;
1630 dc->hotpluggable = false;
1631 }
1632
1633 static const TypeInfo sm501_pci_info = {
1634 .name = TYPE_PCI_SM501,
1635 .parent = TYPE_PCI_DEVICE,
1636 .instance_size = sizeof(SM501PCIState),
1637 .class_init = sm501_pci_class_init,
1638 };
1639
1640 static void sm501_register_types(void)
1641 {
1642 type_register_static(&sm501_sysbus_info);
1643 type_register_static(&sm501_pci_info);
1644 }
1645
1646 type_init(sm501_register_types)