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"
21 #define PL110_CR_EN 0x001
22 #define PL110_CR_BGR 0x100
23 #define PL110_CR_BEBO 0x200
24 #define PL110_CR_BEPO 0x400
25 #define PL110_CR_PWR 0x800
26 #define PL110_IE_NB 0x004
27 #define PL110_IE_VC 0x008
37 BPP_16_565
, /* PL111 only */
38 BPP_12
/* PL111 only */
42 /* The Versatile/PB uses a slightly modified PL110 controller. */
50 #define TYPE_PL110 "pl110"
51 #define PL110(obj) OBJECT_CHECK(PL110State, (obj), TYPE_PL110)
53 typedef struct PL110State
{
54 SysBusDevice parent_obj
;
57 MemoryRegionSection fbsection
;
59 QEMUTimer
*vblank_timer
;
70 enum pl110_bppmode bpp
;
73 uint32_t palette
[256];
74 uint32_t raw_palette
[128];
78 static int vmstate_pl110_post_load(void *opaque
, int version_id
);
80 static const VMStateDescription vmstate_pl110
= {
83 .minimum_version_id
= 1,
84 .post_load
= vmstate_pl110_post_load
,
85 .fields
= (VMStateField
[]) {
86 VMSTATE_INT32(version
, PL110State
),
87 VMSTATE_UINT32_ARRAY(timing
, PL110State
, 4),
88 VMSTATE_UINT32(cr
, PL110State
),
89 VMSTATE_UINT32(upbase
, PL110State
),
90 VMSTATE_UINT32(lpbase
, PL110State
),
91 VMSTATE_UINT32(int_status
, PL110State
),
92 VMSTATE_UINT32(int_mask
, PL110State
),
93 VMSTATE_INT32(cols
, PL110State
),
94 VMSTATE_INT32(rows
, PL110State
),
95 VMSTATE_UINT32(bpp
, PL110State
),
96 VMSTATE_INT32(invalidate
, PL110State
),
97 VMSTATE_UINT32_ARRAY(palette
, PL110State
, 256),
98 VMSTATE_UINT32_ARRAY(raw_palette
, PL110State
, 128),
99 VMSTATE_UINT32_V(mux_ctrl
, PL110State
, 2),
100 VMSTATE_END_OF_LIST()
104 static const unsigned char pl110_id
[] =
105 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
107 static const unsigned char pl111_id
[] = {
108 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
112 /* Indexed by pl110_version */
113 static const unsigned char *idregs
[] = {
115 /* The ARM documentation (DDI0224C) says the CLCDC on the Versatile board
116 * has a different ID (0x93, 0x10, 0x04, 0x00, ...). However the hardware
117 * itself has the same ID values as a stock PL110, and guests (in
118 * particular Linux) rely on this. We emulate what the hardware does,
119 * rather than what the docs claim it ought to do.
126 #include "pl110_template.h"
128 #include "pl110_template.h"
130 #include "pl110_template.h"
132 #include "pl110_template.h"
134 #include "pl110_template.h"
136 static int pl110_enabled(PL110State
*s
)
138 return (s
->cr
& PL110_CR_EN
) && (s
->cr
& PL110_CR_PWR
);
141 static void pl110_update_display(void *opaque
)
143 PL110State
*s
= (PL110State
*)opaque
;
145 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
154 if (!pl110_enabled(s
)) {
158 sbd
= SYS_BUS_DEVICE(s
);
160 switch (surface_bits_per_pixel(surface
)) {
164 fntable
= pl110_draw_fn_8
;
168 fntable
= pl110_draw_fn_15
;
172 fntable
= pl110_draw_fn_16
;
176 fntable
= pl110_draw_fn_24
;
180 fntable
= pl110_draw_fn_32
;
184 fprintf(stderr
, "pl110: Bad color depth\n");
187 if (s
->cr
& PL110_CR_BGR
)
192 if ((s
->version
!= PL111
) && (s
->bpp
== BPP_16
)) {
193 /* The PL110's native 16 bit mode is 5551; however
194 * most boards with a PL110 implement an external
195 * mux which allows bits to be reshuffled to give
196 * 565 format. The mux is typically controlled by
197 * an external system register.
198 * This is controlled by a GPIO input pin
199 * so boards can wire it up to their register.
201 * The PL111 straightforwardly implements both
202 * 5551 and 565 under control of the bpp field
203 * in the LCDControl register.
205 switch (s
->mux_ctrl
) {
206 case 3: /* 565 BGR */
207 bpp_offset
= (BPP_16_565
- BPP_16
);
211 case 0: /* 888; also if we have loaded vmstate from an old version */
212 case 2: /* 565 RGB */
214 /* treat as 565 but honour BGR bit */
215 bpp_offset
+= (BPP_16_565
- BPP_16
);
220 if (s
->cr
& PL110_CR_BEBO
)
221 fn
= fntable
[s
->bpp
+ 8 + bpp_offset
];
222 else if (s
->cr
& PL110_CR_BEPO
)
223 fn
= fntable
[s
->bpp
+ 16 + bpp_offset
];
225 fn
= fntable
[s
->bpp
+ bpp_offset
];
249 dest_width
*= s
->cols
;
252 framebuffer_update_memory_section(&s
->fbsection
,
253 sysbus_address_space(sbd
),
258 framebuffer_update_display(surface
, &s
->fbsection
,
260 src_width
, dest_width
, 0,
266 dpy_gfx_update(s
->con
, 0, first
, s
->cols
, last
- first
+ 1);
271 static void pl110_invalidate_display(void * opaque
)
273 PL110State
*s
= (PL110State
*)opaque
;
275 if (pl110_enabled(s
)) {
276 qemu_console_resize(s
->con
, s
->cols
, s
->rows
);
280 static void pl110_update_palette(PL110State
*s
, int n
)
282 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
285 unsigned int r
, g
, b
;
287 raw
= s
->raw_palette
[n
];
289 for (i
= 0; i
< 2; i
++) {
290 r
= (raw
& 0x1f) << 3;
292 g
= (raw
& 0x1f) << 3;
294 b
= (raw
& 0x1f) << 3;
295 /* The I bit is ignored. */
297 switch (surface_bits_per_pixel(surface
)) {
299 s
->palette
[n
] = rgb_to_pixel8(r
, g
, b
);
302 s
->palette
[n
] = rgb_to_pixel15(r
, g
, b
);
305 s
->palette
[n
] = rgb_to_pixel16(r
, g
, b
);
309 s
->palette
[n
] = rgb_to_pixel32(r
, g
, b
);
316 static void pl110_resize(PL110State
*s
, int width
, int height
)
318 if (width
!= s
->cols
|| height
!= s
->rows
) {
319 if (pl110_enabled(s
)) {
320 qemu_console_resize(s
->con
, width
, height
);
327 /* Update interrupts. */
328 static void pl110_update(PL110State
*s
)
330 /* Raise IRQ if enabled and any status bit is 1 */
331 if (s
->int_status
& s
->int_mask
) {
332 qemu_irq_raise(s
->irq
);
334 qemu_irq_lower(s
->irq
);
338 static void pl110_vblank_interrupt(void *opaque
)
340 PL110State
*s
= opaque
;
342 /* Fire the vertical compare and next base IRQs and re-arm */
343 s
->int_status
|= (PL110_IE_NB
| PL110_IE_VC
);
344 timer_mod(s
->vblank_timer
,
345 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
346 NANOSECONDS_PER_SECOND
/ 60);
350 static uint64_t pl110_read(void *opaque
, hwaddr offset
,
353 PL110State
*s
= (PL110State
*)opaque
;
355 if (offset
>= 0xfe0 && offset
< 0x1000) {
356 return idregs
[s
->version
][(offset
- 0xfe0) >> 2];
358 if (offset
>= 0x200 && offset
< 0x400) {
359 return s
->raw_palette
[(offset
- 0x200) >> 2];
361 switch (offset
>> 2) {
362 case 0: /* LCDTiming0 */
364 case 1: /* LCDTiming1 */
366 case 2: /* LCDTiming2 */
368 case 3: /* LCDTiming3 */
370 case 4: /* LCDUPBASE */
372 case 5: /* LCDLPBASE */
374 case 6: /* LCDIMSC */
375 if (s
->version
!= PL110
) {
379 case 7: /* LCDControl */
380 if (s
->version
!= PL110
) {
385 return s
->int_status
;
387 return s
->int_status
& s
->int_mask
;
388 case 11: /* LCDUPCURR */
389 /* TODO: Implement vertical refresh. */
391 case 12: /* LCDLPCURR */
394 qemu_log_mask(LOG_GUEST_ERROR
,
395 "pl110_read: Bad offset %x\n", (int)offset
);
400 static void pl110_write(void *opaque
, hwaddr offset
,
401 uint64_t val
, unsigned size
)
403 PL110State
*s
= (PL110State
*)opaque
;
406 /* For simplicity invalidate the display whenever a control register
409 if (offset
>= 0x200 && offset
< 0x400) {
411 n
= (offset
- 0x200) >> 2;
412 s
->raw_palette
[(offset
- 0x200) >> 2] = val
;
413 pl110_update_palette(s
, n
);
416 switch (offset
>> 2) {
417 case 0: /* LCDTiming0 */
419 n
= ((val
& 0xfc) + 4) * 4;
420 pl110_resize(s
, n
, s
->rows
);
422 case 1: /* LCDTiming1 */
424 n
= (val
& 0x3ff) + 1;
425 pl110_resize(s
, s
->cols
, n
);
427 case 2: /* LCDTiming2 */
430 case 3: /* LCDTiming3 */
433 case 4: /* LCDUPBASE */
436 case 5: /* LCDLPBASE */
439 case 6: /* LCDIMSC */
440 if (s
->version
!= PL110
) {
447 case 7: /* LCDControl */
448 if (s
->version
!= PL110
) {
453 s
->bpp
= (val
>> 1) & 7;
454 if (pl110_enabled(s
)) {
455 qemu_console_resize(s
->con
, s
->cols
, s
->rows
);
456 timer_mod(s
->vblank_timer
,
457 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
458 NANOSECONDS_PER_SECOND
/ 60);
460 timer_del(s
->vblank_timer
);
463 case 10: /* LCDICR */
464 s
->int_status
&= ~val
;
468 qemu_log_mask(LOG_GUEST_ERROR
,
469 "pl110_write: Bad offset %x\n", (int)offset
);
473 static const MemoryRegionOps pl110_ops
= {
475 .write
= pl110_write
,
476 .endianness
= DEVICE_NATIVE_ENDIAN
,
479 static void pl110_mux_ctrl_set(void *opaque
, int line
, int level
)
481 PL110State
*s
= (PL110State
*)opaque
;
485 static int vmstate_pl110_post_load(void *opaque
, int version_id
)
487 PL110State
*s
= opaque
;
488 /* Make sure we redraw, and at the right size */
489 pl110_invalidate_display(s
);
493 static const GraphicHwOps pl110_gfx_ops
= {
494 .invalidate
= pl110_invalidate_display
,
495 .gfx_update
= pl110_update_display
,
498 static void pl110_realize(DeviceState
*dev
, Error
**errp
)
500 PL110State
*s
= PL110(dev
);
501 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
503 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl110_ops
, s
, "pl110", 0x1000);
504 sysbus_init_mmio(sbd
, &s
->iomem
);
505 sysbus_init_irq(sbd
, &s
->irq
);
506 s
->vblank_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
507 pl110_vblank_interrupt
, s
);
508 qdev_init_gpio_in(dev
, pl110_mux_ctrl_set
, 1);
509 s
->con
= graphic_console_init(dev
, 0, &pl110_gfx_ops
, s
);
512 static void pl110_init(Object
*obj
)
514 PL110State
*s
= PL110(obj
);
519 static void pl110_versatile_init(Object
*obj
)
521 PL110State
*s
= PL110(obj
);
523 s
->version
= PL110_VERSATILE
;
526 static void pl111_init(Object
*obj
)
528 PL110State
*s
= PL110(obj
);
533 static void pl110_class_init(ObjectClass
*klass
, void *data
)
535 DeviceClass
*dc
= DEVICE_CLASS(klass
);
537 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
538 dc
->vmsd
= &vmstate_pl110
;
539 dc
->realize
= pl110_realize
;
542 static const TypeInfo pl110_info
= {
544 .parent
= TYPE_SYS_BUS_DEVICE
,
545 .instance_size
= sizeof(PL110State
),
546 .instance_init
= pl110_init
,
547 .class_init
= pl110_class_init
,
550 static const TypeInfo pl110_versatile_info
= {
551 .name
= "pl110_versatile",
552 .parent
= TYPE_PL110
,
553 .instance_init
= pl110_versatile_init
,
556 static const TypeInfo pl111_info
= {
558 .parent
= TYPE_PL110
,
559 .instance_init
= pl111_init
,
562 static void pl110_register_types(void)
564 type_register_static(&pl110_info
);
565 type_register_static(&pl110_versatile_info
);
566 type_register_static(&pl111_info
);
569 type_init(pl110_register_types
)