2 * Arm PrimeCell PL110 Color LCD Controller
4 * Copyright (c) 2005-2009 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GNU LGPL
10 #include "qemu/osdep.h"
12 #include "hw/sysbus.h"
13 #include "migration/vmstate.h"
14 #include "ui/console.h"
15 #include "framebuffer.h"
16 #include "ui/pixel_ops.h"
17 #include "qemu/timer.h"
19 #include "qemu/module.h"
20 #include "qom/object.h"
22 #define PL110_CR_EN 0x001
23 #define PL110_CR_BGR 0x100
24 #define PL110_CR_BEBO 0x200
25 #define PL110_CR_BEPO 0x400
26 #define PL110_CR_PWR 0x800
27 #define PL110_IE_NB 0x004
28 #define PL110_IE_VC 0x008
38 BPP_16_565
, /* PL111 only */
39 BPP_12
/* PL111 only */
43 /* The Versatile/PB uses a slightly modified PL110 controller. */
47 VERSION_PL110_VERSATILE
,
51 #define TYPE_PL110 "pl110"
52 OBJECT_DECLARE_SIMPLE_TYPE(PL110State
, PL110
)
55 SysBusDevice parent_obj
;
58 MemoryRegionSection fbsection
;
60 QEMUTimer
*vblank_timer
;
71 enum pl110_bppmode bpp
;
74 uint32_t palette
[256];
75 uint32_t raw_palette
[128];
79 static int vmstate_pl110_post_load(void *opaque
, int version_id
);
81 static const VMStateDescription vmstate_pl110
= {
84 .minimum_version_id
= 1,
85 .post_load
= vmstate_pl110_post_load
,
86 .fields
= (VMStateField
[]) {
87 VMSTATE_INT32(version
, PL110State
),
88 VMSTATE_UINT32_ARRAY(timing
, PL110State
, 4),
89 VMSTATE_UINT32(cr
, PL110State
),
90 VMSTATE_UINT32(upbase
, PL110State
),
91 VMSTATE_UINT32(lpbase
, PL110State
),
92 VMSTATE_UINT32(int_status
, PL110State
),
93 VMSTATE_UINT32(int_mask
, PL110State
),
94 VMSTATE_INT32(cols
, PL110State
),
95 VMSTATE_INT32(rows
, PL110State
),
96 VMSTATE_UINT32(bpp
, PL110State
),
97 VMSTATE_INT32(invalidate
, PL110State
),
98 VMSTATE_UINT32_ARRAY(palette
, PL110State
, 256),
99 VMSTATE_UINT32_ARRAY(raw_palette
, PL110State
, 128),
100 VMSTATE_UINT32_V(mux_ctrl
, PL110State
, 2),
101 VMSTATE_END_OF_LIST()
105 static const unsigned char pl110_id
[] =
106 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
108 static const unsigned char pl111_id
[] = {
109 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
113 /* Indexed by pl110_version */
114 static const unsigned char *idregs
[] = {
116 /* The ARM documentation (DDI0224C) says the CLCDC on the Versatile board
117 * has a different ID (0x93, 0x10, 0x04, 0x00, ...). However the hardware
118 * itself has the same ID values as a stock PL110, and guests (in
119 * particular Linux) rely on this. We emulate what the hardware does,
120 * rather than what the docs claim it ought to do.
126 #define COPY_PIXEL(to, from) do { *(uint32_t *)to = from; to += 4; } while (0)
131 #include "pl110_template.h"
133 #include "pl110_template.h"
135 #include "pl110_template.h"
140 #include "pl110_template.h"
142 #include "pl110_template.h"
144 #include "pl110_template.h"
149 static drawfn pl110_draw_fn_32
[48] = {
150 pl110_draw_line1_lblp_bgr
,
151 pl110_draw_line2_lblp_bgr
,
152 pl110_draw_line4_lblp_bgr
,
153 pl110_draw_line8_lblp_bgr
,
154 pl110_draw_line16_555_lblp_bgr
,
155 pl110_draw_line32_lblp_bgr
,
156 pl110_draw_line16_lblp_bgr
,
157 pl110_draw_line12_lblp_bgr
,
159 pl110_draw_line1_bbbp_bgr
,
160 pl110_draw_line2_bbbp_bgr
,
161 pl110_draw_line4_bbbp_bgr
,
162 pl110_draw_line8_bbbp_bgr
,
163 pl110_draw_line16_555_bbbp_bgr
,
164 pl110_draw_line32_bbbp_bgr
,
165 pl110_draw_line16_bbbp_bgr
,
166 pl110_draw_line12_bbbp_bgr
,
168 pl110_draw_line1_lbbp_bgr
,
169 pl110_draw_line2_lbbp_bgr
,
170 pl110_draw_line4_lbbp_bgr
,
171 pl110_draw_line8_lbbp_bgr
,
172 pl110_draw_line16_555_lbbp_bgr
,
173 pl110_draw_line32_lbbp_bgr
,
174 pl110_draw_line16_lbbp_bgr
,
175 pl110_draw_line12_lbbp_bgr
,
177 pl110_draw_line1_lblp_rgb
,
178 pl110_draw_line2_lblp_rgb
,
179 pl110_draw_line4_lblp_rgb
,
180 pl110_draw_line8_lblp_rgb
,
181 pl110_draw_line16_555_lblp_rgb
,
182 pl110_draw_line32_lblp_rgb
,
183 pl110_draw_line16_lblp_rgb
,
184 pl110_draw_line12_lblp_rgb
,
186 pl110_draw_line1_bbbp_rgb
,
187 pl110_draw_line2_bbbp_rgb
,
188 pl110_draw_line4_bbbp_rgb
,
189 pl110_draw_line8_bbbp_rgb
,
190 pl110_draw_line16_555_bbbp_rgb
,
191 pl110_draw_line32_bbbp_rgb
,
192 pl110_draw_line16_bbbp_rgb
,
193 pl110_draw_line12_bbbp_rgb
,
195 pl110_draw_line1_lbbp_rgb
,
196 pl110_draw_line2_lbbp_rgb
,
197 pl110_draw_line4_lbbp_rgb
,
198 pl110_draw_line8_lbbp_rgb
,
199 pl110_draw_line16_555_lbbp_rgb
,
200 pl110_draw_line32_lbbp_rgb
,
201 pl110_draw_line16_lbbp_rgb
,
202 pl110_draw_line12_lbbp_rgb
,
205 static int pl110_enabled(PL110State
*s
)
207 return (s
->cr
& PL110_CR_EN
) && (s
->cr
& PL110_CR_PWR
);
210 static void pl110_update_display(void *opaque
)
212 PL110State
*s
= (PL110State
*)opaque
;
214 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
221 if (!pl110_enabled(s
)) {
225 sbd
= SYS_BUS_DEVICE(s
);
227 if (s
->cr
& PL110_CR_BGR
)
232 if ((s
->version
!= VERSION_PL111
) && (s
->bpp
== BPP_16
)) {
233 /* The PL110's native 16 bit mode is 5551; however
234 * most boards with a PL110 implement an external
235 * mux which allows bits to be reshuffled to give
236 * 565 format. The mux is typically controlled by
237 * an external system register.
238 * This is controlled by a GPIO input pin
239 * so boards can wire it up to their register.
241 * The PL111 straightforwardly implements both
242 * 5551 and 565 under control of the bpp field
243 * in the LCDControl register.
245 switch (s
->mux_ctrl
) {
246 case 3: /* 565 BGR */
247 bpp_offset
= (BPP_16_565
- BPP_16
);
251 case 0: /* 888; also if we have loaded vmstate from an old version */
252 case 2: /* 565 RGB */
254 /* treat as 565 but honour BGR bit */
255 bpp_offset
+= (BPP_16_565
- BPP_16
);
260 if (s
->cr
& PL110_CR_BEBO
) {
261 fn
= pl110_draw_fn_32
[s
->bpp
+ 8 + bpp_offset
];
262 } else if (s
->cr
& PL110_CR_BEPO
) {
263 fn
= pl110_draw_fn_32
[s
->bpp
+ 16 + bpp_offset
];
265 fn
= pl110_draw_fn_32
[s
->bpp
+ bpp_offset
];
292 framebuffer_update_memory_section(&s
->fbsection
,
293 sysbus_address_space(sbd
),
298 framebuffer_update_display(surface
, &s
->fbsection
,
300 src_width
, s
->cols
* 4, 0,
306 dpy_gfx_update(s
->con
, 0, first
, s
->cols
, last
- first
+ 1);
311 static void pl110_invalidate_display(void * opaque
)
313 PL110State
*s
= (PL110State
*)opaque
;
315 if (pl110_enabled(s
)) {
316 qemu_console_resize(s
->con
, s
->cols
, s
->rows
);
320 static void pl110_update_palette(PL110State
*s
, int n
)
322 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
325 unsigned int r
, g
, b
;
327 raw
= s
->raw_palette
[n
];
329 for (i
= 0; i
< 2; i
++) {
330 r
= (raw
& 0x1f) << 3;
332 g
= (raw
& 0x1f) << 3;
334 b
= (raw
& 0x1f) << 3;
335 /* The I bit is ignored. */
337 switch (surface_bits_per_pixel(surface
)) {
339 s
->palette
[n
] = rgb_to_pixel8(r
, g
, b
);
342 s
->palette
[n
] = rgb_to_pixel15(r
, g
, b
);
345 s
->palette
[n
] = rgb_to_pixel16(r
, g
, b
);
349 s
->palette
[n
] = rgb_to_pixel32(r
, g
, b
);
356 static void pl110_resize(PL110State
*s
, int width
, int height
)
358 if (width
!= s
->cols
|| height
!= s
->rows
) {
359 if (pl110_enabled(s
)) {
360 qemu_console_resize(s
->con
, width
, height
);
367 /* Update interrupts. */
368 static void pl110_update(PL110State
*s
)
370 /* Raise IRQ if enabled and any status bit is 1 */
371 if (s
->int_status
& s
->int_mask
) {
372 qemu_irq_raise(s
->irq
);
374 qemu_irq_lower(s
->irq
);
378 static void pl110_vblank_interrupt(void *opaque
)
380 PL110State
*s
= opaque
;
382 /* Fire the vertical compare and next base IRQs and re-arm */
383 s
->int_status
|= (PL110_IE_NB
| PL110_IE_VC
);
384 timer_mod(s
->vblank_timer
,
385 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
386 NANOSECONDS_PER_SECOND
/ 60);
390 static uint64_t pl110_read(void *opaque
, hwaddr offset
,
393 PL110State
*s
= (PL110State
*)opaque
;
395 if (offset
>= 0xfe0 && offset
< 0x1000) {
396 return idregs
[s
->version
][(offset
- 0xfe0) >> 2];
398 if (offset
>= 0x200 && offset
< 0x400) {
399 return s
->raw_palette
[(offset
- 0x200) >> 2];
401 switch (offset
>> 2) {
402 case 0: /* LCDTiming0 */
404 case 1: /* LCDTiming1 */
406 case 2: /* LCDTiming2 */
408 case 3: /* LCDTiming3 */
410 case 4: /* LCDUPBASE */
412 case 5: /* LCDLPBASE */
414 case 6: /* LCDIMSC */
415 if (s
->version
!= VERSION_PL110
) {
419 case 7: /* LCDControl */
420 if (s
->version
!= VERSION_PL110
) {
425 return s
->int_status
;
427 return s
->int_status
& s
->int_mask
;
428 case 11: /* LCDUPCURR */
429 /* TODO: Implement vertical refresh. */
431 case 12: /* LCDLPCURR */
434 qemu_log_mask(LOG_GUEST_ERROR
,
435 "pl110_read: Bad offset %x\n", (int)offset
);
440 static void pl110_write(void *opaque
, hwaddr offset
,
441 uint64_t val
, unsigned size
)
443 PL110State
*s
= (PL110State
*)opaque
;
446 /* For simplicity invalidate the display whenever a control register
449 if (offset
>= 0x200 && offset
< 0x400) {
451 n
= (offset
- 0x200) >> 2;
452 s
->raw_palette
[(offset
- 0x200) >> 2] = val
;
453 pl110_update_palette(s
, n
);
456 switch (offset
>> 2) {
457 case 0: /* LCDTiming0 */
459 n
= ((val
& 0xfc) + 4) * 4;
460 pl110_resize(s
, n
, s
->rows
);
462 case 1: /* LCDTiming1 */
464 n
= (val
& 0x3ff) + 1;
465 pl110_resize(s
, s
->cols
, n
);
467 case 2: /* LCDTiming2 */
470 case 3: /* LCDTiming3 */
473 case 4: /* LCDUPBASE */
476 case 5: /* LCDLPBASE */
479 case 6: /* LCDIMSC */
480 if (s
->version
!= VERSION_PL110
) {
487 case 7: /* LCDControl */
488 if (s
->version
!= VERSION_PL110
) {
493 s
->bpp
= (val
>> 1) & 7;
494 if (pl110_enabled(s
)) {
495 qemu_console_resize(s
->con
, s
->cols
, s
->rows
);
496 timer_mod(s
->vblank_timer
,
497 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
498 NANOSECONDS_PER_SECOND
/ 60);
500 timer_del(s
->vblank_timer
);
503 case 10: /* LCDICR */
504 s
->int_status
&= ~val
;
508 qemu_log_mask(LOG_GUEST_ERROR
,
509 "pl110_write: Bad offset %x\n", (int)offset
);
513 static const MemoryRegionOps pl110_ops
= {
515 .write
= pl110_write
,
516 .endianness
= DEVICE_NATIVE_ENDIAN
,
519 static void pl110_mux_ctrl_set(void *opaque
, int line
, int level
)
521 PL110State
*s
= (PL110State
*)opaque
;
525 static int vmstate_pl110_post_load(void *opaque
, int version_id
)
527 PL110State
*s
= opaque
;
528 /* Make sure we redraw, and at the right size */
529 pl110_invalidate_display(s
);
533 static const GraphicHwOps pl110_gfx_ops
= {
534 .invalidate
= pl110_invalidate_display
,
535 .gfx_update
= pl110_update_display
,
538 static void pl110_realize(DeviceState
*dev
, Error
**errp
)
540 PL110State
*s
= PL110(dev
);
541 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
543 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl110_ops
, s
, "pl110", 0x1000);
544 sysbus_init_mmio(sbd
, &s
->iomem
);
545 sysbus_init_irq(sbd
, &s
->irq
);
546 s
->vblank_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
547 pl110_vblank_interrupt
, s
);
548 qdev_init_gpio_in(dev
, pl110_mux_ctrl_set
, 1);
549 s
->con
= graphic_console_init(dev
, 0, &pl110_gfx_ops
, s
);
552 static void pl110_init(Object
*obj
)
554 PL110State
*s
= PL110(obj
);
556 s
->version
= VERSION_PL110
;
559 static void pl110_versatile_init(Object
*obj
)
561 PL110State
*s
= PL110(obj
);
563 s
->version
= VERSION_PL110_VERSATILE
;
566 static void pl111_init(Object
*obj
)
568 PL110State
*s
= PL110(obj
);
570 s
->version
= VERSION_PL111
;
573 static void pl110_class_init(ObjectClass
*klass
, void *data
)
575 DeviceClass
*dc
= DEVICE_CLASS(klass
);
577 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
578 dc
->vmsd
= &vmstate_pl110
;
579 dc
->realize
= pl110_realize
;
582 static const TypeInfo pl110_info
= {
584 .parent
= TYPE_SYS_BUS_DEVICE
,
585 .instance_size
= sizeof(PL110State
),
586 .instance_init
= pl110_init
,
587 .class_init
= pl110_class_init
,
590 static const TypeInfo pl110_versatile_info
= {
591 .name
= "pl110_versatile",
592 .parent
= TYPE_PL110
,
593 .instance_init
= pl110_versatile_init
,
596 static const TypeInfo pl111_info
= {
598 .parent
= TYPE_PL110
,
599 .instance_init
= pl111_init
,
602 static void pl110_register_types(void)
604 type_register_static(&pl110_info
);
605 type_register_static(&pl110_versatile_info
);
606 type_register_static(&pl111_info
);
609 type_init(pl110_register_types
)