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"
11 #include "hw/sysbus.h"
12 #include "ui/console.h"
13 #include "framebuffer.h"
14 #include "ui/pixel_ops.h"
15 #include "qemu/timer.h"
18 #define PL110_CR_EN 0x001
19 #define PL110_CR_BGR 0x100
20 #define PL110_CR_BEBO 0x200
21 #define PL110_CR_BEPO 0x400
22 #define PL110_CR_PWR 0x800
23 #define PL110_IE_NB 0x004
24 #define PL110_IE_VC 0x008
34 BPP_16_565
, /* PL111 only */
35 BPP_12
/* PL111 only */
39 /* The Versatile/PB uses a slightly modified PL110 controller. */
47 #define TYPE_PL110 "pl110"
48 #define PL110(obj) OBJECT_CHECK(PL110State, (obj), TYPE_PL110)
50 typedef struct PL110State
{
51 SysBusDevice parent_obj
;
54 MemoryRegionSection fbsection
;
56 QEMUTimer
*vblank_timer
;
67 enum pl110_bppmode bpp
;
70 uint32_t palette
[256];
71 uint32_t raw_palette
[128];
75 static int vmstate_pl110_post_load(void *opaque
, int version_id
);
77 static const VMStateDescription vmstate_pl110
= {
80 .minimum_version_id
= 1,
81 .post_load
= vmstate_pl110_post_load
,
82 .fields
= (VMStateField
[]) {
83 VMSTATE_INT32(version
, PL110State
),
84 VMSTATE_UINT32_ARRAY(timing
, PL110State
, 4),
85 VMSTATE_UINT32(cr
, PL110State
),
86 VMSTATE_UINT32(upbase
, PL110State
),
87 VMSTATE_UINT32(lpbase
, PL110State
),
88 VMSTATE_UINT32(int_status
, PL110State
),
89 VMSTATE_UINT32(int_mask
, PL110State
),
90 VMSTATE_INT32(cols
, PL110State
),
91 VMSTATE_INT32(rows
, PL110State
),
92 VMSTATE_UINT32(bpp
, PL110State
),
93 VMSTATE_INT32(invalidate
, PL110State
),
94 VMSTATE_UINT32_ARRAY(palette
, PL110State
, 256),
95 VMSTATE_UINT32_ARRAY(raw_palette
, PL110State
, 128),
96 VMSTATE_UINT32_V(mux_ctrl
, PL110State
, 2),
101 static const unsigned char pl110_id
[] =
102 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
104 static const unsigned char pl111_id
[] = {
105 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
109 /* Indexed by pl110_version */
110 static const unsigned char *idregs
[] = {
112 /* The ARM documentation (DDI0224C) says the CLCDC on the Versatile board
113 * has a different ID (0x93, 0x10, 0x04, 0x00, ...). However the hardware
114 * itself has the same ID values as a stock PL110, and guests (in
115 * particular Linux) rely on this. We emulate what the hardware does,
116 * rather than what the docs claim it ought to do.
123 #include "pl110_template.h"
125 #include "pl110_template.h"
127 #include "pl110_template.h"
129 #include "pl110_template.h"
131 #include "pl110_template.h"
133 static int pl110_enabled(PL110State
*s
)
135 return (s
->cr
& PL110_CR_EN
) && (s
->cr
& PL110_CR_PWR
);
138 static void pl110_update_display(void *opaque
)
140 PL110State
*s
= (PL110State
*)opaque
;
142 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
151 if (!pl110_enabled(s
)) {
155 sbd
= SYS_BUS_DEVICE(s
);
157 switch (surface_bits_per_pixel(surface
)) {
161 fntable
= pl110_draw_fn_8
;
165 fntable
= pl110_draw_fn_15
;
169 fntable
= pl110_draw_fn_16
;
173 fntable
= pl110_draw_fn_24
;
177 fntable
= pl110_draw_fn_32
;
181 fprintf(stderr
, "pl110: Bad color depth\n");
184 if (s
->cr
& PL110_CR_BGR
)
189 if ((s
->version
!= PL111
) && (s
->bpp
== BPP_16
)) {
190 /* The PL110's native 16 bit mode is 5551; however
191 * most boards with a PL110 implement an external
192 * mux which allows bits to be reshuffled to give
193 * 565 format. The mux is typically controlled by
194 * an external system register.
195 * This is controlled by a GPIO input pin
196 * so boards can wire it up to their register.
198 * The PL111 straightforwardly implements both
199 * 5551 and 565 under control of the bpp field
200 * in the LCDControl register.
202 switch (s
->mux_ctrl
) {
203 case 3: /* 565 BGR */
204 bpp_offset
= (BPP_16_565
- BPP_16
);
208 case 0: /* 888; also if we have loaded vmstate from an old version */
209 case 2: /* 565 RGB */
211 /* treat as 565 but honour BGR bit */
212 bpp_offset
+= (BPP_16_565
- BPP_16
);
217 if (s
->cr
& PL110_CR_BEBO
)
218 fn
= fntable
[s
->bpp
+ 8 + bpp_offset
];
219 else if (s
->cr
& PL110_CR_BEPO
)
220 fn
= fntable
[s
->bpp
+ 16 + bpp_offset
];
222 fn
= fntable
[s
->bpp
+ bpp_offset
];
246 dest_width
*= s
->cols
;
249 framebuffer_update_memory_section(&s
->fbsection
,
250 sysbus_address_space(sbd
),
255 framebuffer_update_display(surface
, &s
->fbsection
,
257 src_width
, dest_width
, 0,
263 dpy_gfx_update(s
->con
, 0, first
, s
->cols
, last
- first
+ 1);
268 static void pl110_invalidate_display(void * opaque
)
270 PL110State
*s
= (PL110State
*)opaque
;
272 if (pl110_enabled(s
)) {
273 qemu_console_resize(s
->con
, s
->cols
, s
->rows
);
277 static void pl110_update_palette(PL110State
*s
, int n
)
279 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
282 unsigned int r
, g
, b
;
284 raw
= s
->raw_palette
[n
];
286 for (i
= 0; i
< 2; i
++) {
287 r
= (raw
& 0x1f) << 3;
289 g
= (raw
& 0x1f) << 3;
291 b
= (raw
& 0x1f) << 3;
292 /* The I bit is ignored. */
294 switch (surface_bits_per_pixel(surface
)) {
296 s
->palette
[n
] = rgb_to_pixel8(r
, g
, b
);
299 s
->palette
[n
] = rgb_to_pixel15(r
, g
, b
);
302 s
->palette
[n
] = rgb_to_pixel16(r
, g
, b
);
306 s
->palette
[n
] = rgb_to_pixel32(r
, g
, b
);
313 static void pl110_resize(PL110State
*s
, int width
, int height
)
315 if (width
!= s
->cols
|| height
!= s
->rows
) {
316 if (pl110_enabled(s
)) {
317 qemu_console_resize(s
->con
, width
, height
);
324 /* Update interrupts. */
325 static void pl110_update(PL110State
*s
)
327 /* Raise IRQ if enabled and any status bit is 1 */
328 if (s
->int_status
& s
->int_mask
) {
329 qemu_irq_raise(s
->irq
);
331 qemu_irq_lower(s
->irq
);
335 static void pl110_vblank_interrupt(void *opaque
)
337 PL110State
*s
= opaque
;
339 /* Fire the vertical compare and next base IRQs and re-arm */
340 s
->int_status
|= (PL110_IE_NB
| PL110_IE_VC
);
341 timer_mod(s
->vblank_timer
,
342 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
343 NANOSECONDS_PER_SECOND
/ 60);
347 static uint64_t pl110_read(void *opaque
, hwaddr offset
,
350 PL110State
*s
= (PL110State
*)opaque
;
352 if (offset
>= 0xfe0 && offset
< 0x1000) {
353 return idregs
[s
->version
][(offset
- 0xfe0) >> 2];
355 if (offset
>= 0x200 && offset
< 0x400) {
356 return s
->raw_palette
[(offset
- 0x200) >> 2];
358 switch (offset
>> 2) {
359 case 0: /* LCDTiming0 */
361 case 1: /* LCDTiming1 */
363 case 2: /* LCDTiming2 */
365 case 3: /* LCDTiming3 */
367 case 4: /* LCDUPBASE */
369 case 5: /* LCDLPBASE */
371 case 6: /* LCDIMSC */
372 if (s
->version
!= PL110
) {
376 case 7: /* LCDControl */
377 if (s
->version
!= PL110
) {
382 return s
->int_status
;
384 return s
->int_status
& s
->int_mask
;
385 case 11: /* LCDUPCURR */
386 /* TODO: Implement vertical refresh. */
388 case 12: /* LCDLPCURR */
391 qemu_log_mask(LOG_GUEST_ERROR
,
392 "pl110_read: Bad offset %x\n", (int)offset
);
397 static void pl110_write(void *opaque
, hwaddr offset
,
398 uint64_t val
, unsigned size
)
400 PL110State
*s
= (PL110State
*)opaque
;
403 /* For simplicity invalidate the display whenever a control register
406 if (offset
>= 0x200 && offset
< 0x400) {
408 n
= (offset
- 0x200) >> 2;
409 s
->raw_palette
[(offset
- 0x200) >> 2] = val
;
410 pl110_update_palette(s
, n
);
413 switch (offset
>> 2) {
414 case 0: /* LCDTiming0 */
416 n
= ((val
& 0xfc) + 4) * 4;
417 pl110_resize(s
, n
, s
->rows
);
419 case 1: /* LCDTiming1 */
421 n
= (val
& 0x3ff) + 1;
422 pl110_resize(s
, s
->cols
, n
);
424 case 2: /* LCDTiming2 */
427 case 3: /* LCDTiming3 */
430 case 4: /* LCDUPBASE */
433 case 5: /* LCDLPBASE */
436 case 6: /* LCDIMSC */
437 if (s
->version
!= PL110
) {
444 case 7: /* LCDControl */
445 if (s
->version
!= PL110
) {
450 s
->bpp
= (val
>> 1) & 7;
451 if (pl110_enabled(s
)) {
452 qemu_console_resize(s
->con
, s
->cols
, s
->rows
);
453 timer_mod(s
->vblank_timer
,
454 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
455 NANOSECONDS_PER_SECOND
/ 60);
457 timer_del(s
->vblank_timer
);
460 case 10: /* LCDICR */
461 s
->int_status
&= ~val
;
465 qemu_log_mask(LOG_GUEST_ERROR
,
466 "pl110_write: Bad offset %x\n", (int)offset
);
470 static const MemoryRegionOps pl110_ops
= {
472 .write
= pl110_write
,
473 .endianness
= DEVICE_NATIVE_ENDIAN
,
476 static void pl110_mux_ctrl_set(void *opaque
, int line
, int level
)
478 PL110State
*s
= (PL110State
*)opaque
;
482 static int vmstate_pl110_post_load(void *opaque
, int version_id
)
484 PL110State
*s
= opaque
;
485 /* Make sure we redraw, and at the right size */
486 pl110_invalidate_display(s
);
490 static const GraphicHwOps pl110_gfx_ops
= {
491 .invalidate
= pl110_invalidate_display
,
492 .gfx_update
= pl110_update_display
,
495 static void pl110_realize(DeviceState
*dev
, Error
**errp
)
497 PL110State
*s
= PL110(dev
);
498 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
500 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl110_ops
, s
, "pl110", 0x1000);
501 sysbus_init_mmio(sbd
, &s
->iomem
);
502 sysbus_init_irq(sbd
, &s
->irq
);
503 s
->vblank_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
504 pl110_vblank_interrupt
, s
);
505 qdev_init_gpio_in(dev
, pl110_mux_ctrl_set
, 1);
506 s
->con
= graphic_console_init(dev
, 0, &pl110_gfx_ops
, s
);
509 static void pl110_init(Object
*obj
)
511 PL110State
*s
= PL110(obj
);
516 static void pl110_versatile_init(Object
*obj
)
518 PL110State
*s
= PL110(obj
);
520 s
->version
= PL110_VERSATILE
;
523 static void pl111_init(Object
*obj
)
525 PL110State
*s
= PL110(obj
);
530 static void pl110_class_init(ObjectClass
*klass
, void *data
)
532 DeviceClass
*dc
= DEVICE_CLASS(klass
);
534 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
535 dc
->vmsd
= &vmstate_pl110
;
536 dc
->realize
= pl110_realize
;
539 static const TypeInfo pl110_info
= {
541 .parent
= TYPE_SYS_BUS_DEVICE
,
542 .instance_size
= sizeof(PL110State
),
543 .instance_init
= pl110_init
,
544 .class_init
= pl110_class_init
,
547 static const TypeInfo pl110_versatile_info
= {
548 .name
= "pl110_versatile",
549 .parent
= TYPE_PL110
,
550 .instance_init
= pl110_versatile_init
,
553 static const TypeInfo pl111_info
= {
555 .parent
= TYPE_PL110
,
556 .instance_init
= pl111_init
,
559 static void pl110_register_types(void)
561 type_register_static(&pl110_info
);
562 type_register_static(&pl110_versatile_info
);
563 type_register_static(&pl111_info
);
566 type_init(pl110_register_types
)