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 "hw/sysbus.h"
11 #include "ui/console.h"
12 #include "framebuffer.h"
13 #include "ui/pixel_ops.h"
15 #define PL110_CR_EN 0x001
16 #define PL110_CR_BGR 0x100
17 #define PL110_CR_BEBO 0x200
18 #define PL110_CR_BEPO 0x400
19 #define PL110_CR_PWR 0x800
29 BPP_16_565, /* PL111 only */
30 BPP_12 /* PL111 only */
34 /* The Versatile/PB uses a slightly modified PL110 controller. */
42 #define TYPE_PL110 "pl110"
43 #define PL110(obj) OBJECT_CHECK(PL110State, (obj), TYPE_PL110)
45 typedef struct PL110State {
46 SysBusDevice parent_obj;
60 enum pl110_bppmode bpp;
63 uint32_t palette[256];
64 uint32_t raw_palette[128];
68 static int vmstate_pl110_post_load(void *opaque, int version_id);
70 static const VMStateDescription vmstate_pl110 = {
73 .minimum_version_id = 1,
74 .post_load = vmstate_pl110_post_load,
75 .fields = (VMStateField[]) {
76 VMSTATE_INT32(version, PL110State),
77 VMSTATE_UINT32_ARRAY(timing, PL110State, 4),
78 VMSTATE_UINT32(cr, PL110State),
79 VMSTATE_UINT32(upbase, PL110State),
80 VMSTATE_UINT32(lpbase, PL110State),
81 VMSTATE_UINT32(int_status, PL110State),
82 VMSTATE_UINT32(int_mask, PL110State),
83 VMSTATE_INT32(cols, PL110State),
84 VMSTATE_INT32(rows, PL110State),
85 VMSTATE_UINT32(bpp, PL110State),
86 VMSTATE_INT32(invalidate, PL110State),
87 VMSTATE_UINT32_ARRAY(palette, PL110State, 256),
88 VMSTATE_UINT32_ARRAY(raw_palette, PL110State, 128),
89 VMSTATE_UINT32_V(mux_ctrl, PL110State, 2),
94 static const unsigned char pl110_id[] =
95 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
97 static const unsigned char pl111_id[] = {
98 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
102 /* Indexed by pl110_version */
103 static const unsigned char *idregs[] = {
105 /* The ARM documentation (DDI0224C) says the CLCDC on the Versatile board
106 * has a different ID (0x93, 0x10, 0x04, 0x00, ...). However the hardware
107 * itself has the same ID values as a stock PL110, and guests (in
108 * particular Linux) rely on this. We emulate what the hardware does,
109 * rather than what the docs claim it ought to do.
116 #include "pl110_template.h"
118 #include "pl110_template.h"
120 #include "pl110_template.h"
122 #include "pl110_template.h"
124 #include "pl110_template.h"
126 static int pl110_enabled(PL110State *s)
128 return (s->cr & PL110_CR_EN) && (s->cr & PL110_CR_PWR);
131 static void pl110_update_display(void *opaque)
133 PL110State *s = (PL110State *)opaque;
135 DisplaySurface *surface = qemu_console_surface(s->con);
144 if (!pl110_enabled(s)) {
148 sbd = SYS_BUS_DEVICE(s);
150 switch (surface_bits_per_pixel(surface)) {
154 fntable = pl110_draw_fn_8;
158 fntable = pl110_draw_fn_15;
162 fntable = pl110_draw_fn_16;
166 fntable = pl110_draw_fn_24;
170 fntable = pl110_draw_fn_32;
174 fprintf(stderr, "pl110: Bad color depth\n");
177 if (s->cr & PL110_CR_BGR)
182 if ((s->version != PL111) && (s->bpp == BPP_16)) {
183 /* The PL110's native 16 bit mode is 5551; however
184 * most boards with a PL110 implement an external
185 * mux which allows bits to be reshuffled to give
186 * 565 format. The mux is typically controlled by
187 * an external system register.
188 * This is controlled by a GPIO input pin
189 * so boards can wire it up to their register.
191 * The PL111 straightforwardly implements both
192 * 5551 and 565 under control of the bpp field
193 * in the LCDControl register.
195 switch (s->mux_ctrl) {
196 case 3: /* 565 BGR */
197 bpp_offset = (BPP_16_565 - BPP_16);
201 case 0: /* 888; also if we have loaded vmstate from an old version */
202 case 2: /* 565 RGB */
204 /* treat as 565 but honour BGR bit */
205 bpp_offset += (BPP_16_565 - BPP_16);
210 if (s->cr & PL110_CR_BEBO)
211 fn = fntable[s->bpp + 8 + bpp_offset];
212 else if (s->cr & PL110_CR_BEPO)
213 fn = fntable[s->bpp + 16 + bpp_offset];
215 fn = fntable[s->bpp + bpp_offset];
239 dest_width *= s->cols;
241 framebuffer_update_display(surface, sysbus_address_space(sbd),
242 s->upbase, s->cols, s->rows,
243 src_width, dest_width, 0,
248 dpy_gfx_update(s->con, 0, first, s->cols, last - first + 1);
253 static void pl110_invalidate_display(void * opaque)
255 PL110State *s = (PL110State *)opaque;
257 if (pl110_enabled(s)) {
258 qemu_console_resize(s->con, s->cols, s->rows);
262 static void pl110_update_palette(PL110State *s, int n)
264 DisplaySurface *surface = qemu_console_surface(s->con);
267 unsigned int r, g, b;
269 raw = s->raw_palette[n];
271 for (i = 0; i < 2; i++) {
272 r = (raw & 0x1f) << 3;
274 g = (raw & 0x1f) << 3;
276 b = (raw & 0x1f) << 3;
277 /* The I bit is ignored. */
279 switch (surface_bits_per_pixel(surface)) {
281 s->palette[n] = rgb_to_pixel8(r, g, b);
284 s->palette[n] = rgb_to_pixel15(r, g, b);
287 s->palette[n] = rgb_to_pixel16(r, g, b);
291 s->palette[n] = rgb_to_pixel32(r, g, b);
298 static void pl110_resize(PL110State *s, int width, int height)
300 if (width != s->cols || height != s->rows) {
301 if (pl110_enabled(s)) {
302 qemu_console_resize(s->con, width, height);
309 /* Update interrupts. */
310 static void pl110_update(PL110State *s)
312 /* TODO: Implement interrupts. */
315 static uint64_t pl110_read(void *opaque, hwaddr offset,
318 PL110State *s = (PL110State *)opaque;
320 if (offset >= 0xfe0 && offset < 0x1000) {
321 return idregs[s->version][(offset - 0xfe0) >> 2];
323 if (offset >= 0x200 && offset < 0x400) {
324 return s->raw_palette[(offset - 0x200) >> 2];
326 switch (offset >> 2) {
327 case 0: /* LCDTiming0 */
329 case 1: /* LCDTiming1 */
331 case 2: /* LCDTiming2 */
333 case 3: /* LCDTiming3 */
335 case 4: /* LCDUPBASE */
337 case 5: /* LCDLPBASE */
339 case 6: /* LCDIMSC */
340 if (s->version != PL110) {
344 case 7: /* LCDControl */
345 if (s->version != PL110) {
350 return s->int_status;
352 return s->int_status & s->int_mask;
353 case 11: /* LCDUPCURR */
354 /* TODO: Implement vertical refresh. */
356 case 12: /* LCDLPCURR */
359 qemu_log_mask(LOG_GUEST_ERROR,
360 "pl110_read: Bad offset %x\n", (int)offset);
365 static void pl110_write(void *opaque, hwaddr offset,
366 uint64_t val, unsigned size)
368 PL110State *s = (PL110State *)opaque;
371 /* For simplicity invalidate the display whenever a control register
374 if (offset >= 0x200 && offset < 0x400) {
376 n = (offset - 0x200) >> 2;
377 s->raw_palette[(offset - 0x200) >> 2] = val;
378 pl110_update_palette(s, n);
381 switch (offset >> 2) {
382 case 0: /* LCDTiming0 */
384 n = ((val & 0xfc) + 4) * 4;
385 pl110_resize(s, n, s->rows);
387 case 1: /* LCDTiming1 */
389 n = (val & 0x3ff) + 1;
390 pl110_resize(s, s->cols, n);
392 case 2: /* LCDTiming2 */
395 case 3: /* LCDTiming3 */
398 case 4: /* LCDUPBASE */
401 case 5: /* LCDLPBASE */
404 case 6: /* LCDIMSC */
405 if (s->version != PL110) {
412 case 7: /* LCDControl */
413 if (s->version != PL110) {
418 s->bpp = (val >> 1) & 7;
419 if (pl110_enabled(s)) {
420 qemu_console_resize(s->con, s->cols, s->rows);
423 case 10: /* LCDICR */
424 s->int_status &= ~val;
428 qemu_log_mask(LOG_GUEST_ERROR,
429 "pl110_write: Bad offset %x\n", (int)offset);
433 static const MemoryRegionOps pl110_ops = {
435 .write = pl110_write,
436 .endianness = DEVICE_NATIVE_ENDIAN,
439 static void pl110_mux_ctrl_set(void *opaque, int line, int level)
441 PL110State *s = (PL110State *)opaque;
445 static int vmstate_pl110_post_load(void *opaque, int version_id)
447 PL110State *s = opaque;
448 /* Make sure we redraw, and at the right size */
449 pl110_invalidate_display(s);
453 static const GraphicHwOps pl110_gfx_ops = {
454 .invalidate = pl110_invalidate_display,
455 .gfx_update = pl110_update_display,
458 static int pl110_initfn(SysBusDevice *sbd)
460 DeviceState *dev = DEVICE(sbd);
461 PL110State *s = PL110(dev);
463 memory_region_init_io(&s->iomem, OBJECT(s), &pl110_ops, s, "pl110", 0x1000);
464 sysbus_init_mmio(sbd, &s->iomem);
465 sysbus_init_irq(sbd, &s->irq);
466 qdev_init_gpio_in(dev, pl110_mux_ctrl_set, 1);
467 s->con = graphic_console_init(dev, 0, &pl110_gfx_ops, s);
471 static void pl110_init(Object *obj)
473 PL110State *s = PL110(obj);
478 static void pl110_versatile_init(Object *obj)
480 PL110State *s = PL110(obj);
482 s->version = PL110_VERSATILE;
485 static void pl111_init(Object *obj)
487 PL110State *s = PL110(obj);
492 static void pl110_class_init(ObjectClass *klass, void *data)
494 DeviceClass *dc = DEVICE_CLASS(klass);
495 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
497 k->init = pl110_initfn;
498 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
499 dc->vmsd = &vmstate_pl110;
502 static const TypeInfo pl110_info = {
504 .parent = TYPE_SYS_BUS_DEVICE,
505 .instance_size = sizeof(PL110State),
506 .instance_init = pl110_init,
507 .class_init = pl110_class_init,
510 static const TypeInfo pl110_versatile_info = {
511 .name = "pl110_versatile",
512 .parent = TYPE_PL110,
513 .instance_init = pl110_versatile_init,
516 static const TypeInfo pl111_info = {
518 .parent = TYPE_PL110,
519 .instance_init = pl111_init,
522 static void pl110_register_types(void)
524 type_register_static(&pl110_info);
525 type_register_static(&pl110_versatile_info);
526 type_register_static(&pl111_info);
529 type_init(pl110_register_types)