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"
17 #define PL110_CR_EN 0x001
18 #define PL110_CR_BGR 0x100
19 #define PL110_CR_BEBO 0x200
20 #define PL110_CR_BEPO 0x400
21 #define PL110_CR_PWR 0x800
31 BPP_16_565, /* PL111 only */
32 BPP_12 /* PL111 only */
36 /* The Versatile/PB uses a slightly modified PL110 controller. */
44 #define TYPE_PL110 "pl110"
45 #define PL110(obj) OBJECT_CHECK(PL110State, (obj), TYPE_PL110)
47 typedef struct PL110State {
48 SysBusDevice parent_obj;
51 MemoryRegionSection fbsection;
63 enum pl110_bppmode bpp;
66 uint32_t palette[256];
67 uint32_t raw_palette[128];
71 static int vmstate_pl110_post_load(void *opaque, int version_id);
73 static const VMStateDescription vmstate_pl110 = {
76 .minimum_version_id = 1,
77 .post_load = vmstate_pl110_post_load,
78 .fields = (VMStateField[]) {
79 VMSTATE_INT32(version, PL110State),
80 VMSTATE_UINT32_ARRAY(timing, PL110State, 4),
81 VMSTATE_UINT32(cr, PL110State),
82 VMSTATE_UINT32(upbase, PL110State),
83 VMSTATE_UINT32(lpbase, PL110State),
84 VMSTATE_UINT32(int_status, PL110State),
85 VMSTATE_UINT32(int_mask, PL110State),
86 VMSTATE_INT32(cols, PL110State),
87 VMSTATE_INT32(rows, PL110State),
88 VMSTATE_UINT32(bpp, PL110State),
89 VMSTATE_INT32(invalidate, PL110State),
90 VMSTATE_UINT32_ARRAY(palette, PL110State, 256),
91 VMSTATE_UINT32_ARRAY(raw_palette, PL110State, 128),
92 VMSTATE_UINT32_V(mux_ctrl, PL110State, 2),
97 static const unsigned char pl110_id[] =
98 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
100 static const unsigned char pl111_id[] = {
101 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
105 /* Indexed by pl110_version */
106 static const unsigned char *idregs[] = {
108 /* The ARM documentation (DDI0224C) says the CLCDC on the Versatile board
109 * has a different ID (0x93, 0x10, 0x04, 0x00, ...). However the hardware
110 * itself has the same ID values as a stock PL110, and guests (in
111 * particular Linux) rely on this. We emulate what the hardware does,
112 * rather than what the docs claim it ought to do.
119 #include "pl110_template.h"
121 #include "pl110_template.h"
123 #include "pl110_template.h"
125 #include "pl110_template.h"
127 #include "pl110_template.h"
129 static int pl110_enabled(PL110State *s)
131 return (s->cr & PL110_CR_EN) && (s->cr & PL110_CR_PWR);
134 static void pl110_update_display(void *opaque)
136 PL110State *s = (PL110State *)opaque;
138 DisplaySurface *surface = qemu_console_surface(s->con);
147 if (!pl110_enabled(s)) {
151 sbd = SYS_BUS_DEVICE(s);
153 switch (surface_bits_per_pixel(surface)) {
157 fntable = pl110_draw_fn_8;
161 fntable = pl110_draw_fn_15;
165 fntable = pl110_draw_fn_16;
169 fntable = pl110_draw_fn_24;
173 fntable = pl110_draw_fn_32;
177 fprintf(stderr, "pl110: Bad color depth\n");
180 if (s->cr & PL110_CR_BGR)
185 if ((s->version != PL111) && (s->bpp == BPP_16)) {
186 /* The PL110's native 16 bit mode is 5551; however
187 * most boards with a PL110 implement an external
188 * mux which allows bits to be reshuffled to give
189 * 565 format. The mux is typically controlled by
190 * an external system register.
191 * This is controlled by a GPIO input pin
192 * so boards can wire it up to their register.
194 * The PL111 straightforwardly implements both
195 * 5551 and 565 under control of the bpp field
196 * in the LCDControl register.
198 switch (s->mux_ctrl) {
199 case 3: /* 565 BGR */
200 bpp_offset = (BPP_16_565 - BPP_16);
204 case 0: /* 888; also if we have loaded vmstate from an old version */
205 case 2: /* 565 RGB */
207 /* treat as 565 but honour BGR bit */
208 bpp_offset += (BPP_16_565 - BPP_16);
213 if (s->cr & PL110_CR_BEBO)
214 fn = fntable[s->bpp + 8 + bpp_offset];
215 else if (s->cr & PL110_CR_BEPO)
216 fn = fntable[s->bpp + 16 + bpp_offset];
218 fn = fntable[s->bpp + bpp_offset];
242 dest_width *= s->cols;
245 framebuffer_update_memory_section(&s->fbsection,
246 sysbus_address_space(sbd),
251 framebuffer_update_display(surface, &s->fbsection,
253 src_width, dest_width, 0,
259 dpy_gfx_update(s->con, 0, first, s->cols, last - first + 1);
264 static void pl110_invalidate_display(void * opaque)
266 PL110State *s = (PL110State *)opaque;
268 if (pl110_enabled(s)) {
269 qemu_console_resize(s->con, s->cols, s->rows);
273 static void pl110_update_palette(PL110State *s, int n)
275 DisplaySurface *surface = qemu_console_surface(s->con);
278 unsigned int r, g, b;
280 raw = s->raw_palette[n];
282 for (i = 0; i < 2; i++) {
283 r = (raw & 0x1f) << 3;
285 g = (raw & 0x1f) << 3;
287 b = (raw & 0x1f) << 3;
288 /* The I bit is ignored. */
290 switch (surface_bits_per_pixel(surface)) {
292 s->palette[n] = rgb_to_pixel8(r, g, b);
295 s->palette[n] = rgb_to_pixel15(r, g, b);
298 s->palette[n] = rgb_to_pixel16(r, g, b);
302 s->palette[n] = rgb_to_pixel32(r, g, b);
309 static void pl110_resize(PL110State *s, int width, int height)
311 if (width != s->cols || height != s->rows) {
312 if (pl110_enabled(s)) {
313 qemu_console_resize(s->con, width, height);
320 /* Update interrupts. */
321 static void pl110_update(PL110State *s)
323 /* TODO: Implement interrupts. */
326 static uint64_t pl110_read(void *opaque, hwaddr offset,
329 PL110State *s = (PL110State *)opaque;
331 if (offset >= 0xfe0 && offset < 0x1000) {
332 return idregs[s->version][(offset - 0xfe0) >> 2];
334 if (offset >= 0x200 && offset < 0x400) {
335 return s->raw_palette[(offset - 0x200) >> 2];
337 switch (offset >> 2) {
338 case 0: /* LCDTiming0 */
340 case 1: /* LCDTiming1 */
342 case 2: /* LCDTiming2 */
344 case 3: /* LCDTiming3 */
346 case 4: /* LCDUPBASE */
348 case 5: /* LCDLPBASE */
350 case 6: /* LCDIMSC */
351 if (s->version != PL110) {
355 case 7: /* LCDControl */
356 if (s->version != PL110) {
361 return s->int_status;
363 return s->int_status & s->int_mask;
364 case 11: /* LCDUPCURR */
365 /* TODO: Implement vertical refresh. */
367 case 12: /* LCDLPCURR */
370 qemu_log_mask(LOG_GUEST_ERROR,
371 "pl110_read: Bad offset %x\n", (int)offset);
376 static void pl110_write(void *opaque, hwaddr offset,
377 uint64_t val, unsigned size)
379 PL110State *s = (PL110State *)opaque;
382 /* For simplicity invalidate the display whenever a control register
385 if (offset >= 0x200 && offset < 0x400) {
387 n = (offset - 0x200) >> 2;
388 s->raw_palette[(offset - 0x200) >> 2] = val;
389 pl110_update_palette(s, n);
392 switch (offset >> 2) {
393 case 0: /* LCDTiming0 */
395 n = ((val & 0xfc) + 4) * 4;
396 pl110_resize(s, n, s->rows);
398 case 1: /* LCDTiming1 */
400 n = (val & 0x3ff) + 1;
401 pl110_resize(s, s->cols, n);
403 case 2: /* LCDTiming2 */
406 case 3: /* LCDTiming3 */
409 case 4: /* LCDUPBASE */
412 case 5: /* LCDLPBASE */
415 case 6: /* LCDIMSC */
416 if (s->version != PL110) {
423 case 7: /* LCDControl */
424 if (s->version != PL110) {
429 s->bpp = (val >> 1) & 7;
430 if (pl110_enabled(s)) {
431 qemu_console_resize(s->con, s->cols, s->rows);
434 case 10: /* LCDICR */
435 s->int_status &= ~val;
439 qemu_log_mask(LOG_GUEST_ERROR,
440 "pl110_write: Bad offset %x\n", (int)offset);
444 static const MemoryRegionOps pl110_ops = {
446 .write = pl110_write,
447 .endianness = DEVICE_NATIVE_ENDIAN,
450 static void pl110_mux_ctrl_set(void *opaque, int line, int level)
452 PL110State *s = (PL110State *)opaque;
456 static int vmstate_pl110_post_load(void *opaque, int version_id)
458 PL110State *s = opaque;
459 /* Make sure we redraw, and at the right size */
460 pl110_invalidate_display(s);
464 static const GraphicHwOps pl110_gfx_ops = {
465 .invalidate = pl110_invalidate_display,
466 .gfx_update = pl110_update_display,
469 static void pl110_realize(DeviceState *dev, Error **errp)
471 PL110State *s = PL110(dev);
472 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
474 memory_region_init_io(&s->iomem, OBJECT(s), &pl110_ops, s, "pl110", 0x1000);
475 sysbus_init_mmio(sbd, &s->iomem);
476 sysbus_init_irq(sbd, &s->irq);
477 qdev_init_gpio_in(dev, pl110_mux_ctrl_set, 1);
478 s->con = graphic_console_init(dev, 0, &pl110_gfx_ops, s);
481 static void pl110_init(Object *obj)
483 PL110State *s = PL110(obj);
488 static void pl110_versatile_init(Object *obj)
490 PL110State *s = PL110(obj);
492 s->version = PL110_VERSATILE;
495 static void pl111_init(Object *obj)
497 PL110State *s = PL110(obj);
502 static void pl110_class_init(ObjectClass *klass, void *data)
504 DeviceClass *dc = DEVICE_CLASS(klass);
506 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
507 dc->vmsd = &vmstate_pl110;
508 dc->realize = pl110_realize;
511 static const TypeInfo pl110_info = {
513 .parent = TYPE_SYS_BUS_DEVICE,
514 .instance_size = sizeof(PL110State),
515 .instance_init = pl110_init,
516 .class_init = pl110_class_init,
519 static const TypeInfo pl110_versatile_info = {
520 .name = "pl110_versatile",
521 .parent = TYPE_PL110,
522 .instance_init = pl110_versatile_init,
525 static const TypeInfo pl111_info = {
527 .parent = TYPE_PL110,
528 .instance_init = pl111_init,
531 static void pl110_register_types(void)
533 type_register_static(&pl110_info);
534 type_register_static(&pl110_versatile_info);
535 type_register_static(&pl111_info);
538 type_init(pl110_register_types)