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
12 #include "framebuffer.h"
14 #define PL110_CR_EN 0x001
15 #define PL110_CR_BGR 0x100
16 #define PL110_CR_BEBO 0x200
17 #define PL110_CR_BEPO 0x400
18 #define PL110_CR_PWR 0x800
28 BPP_16_565, /* PL111 only */
29 BPP_12 /* PL111 only */
33 /* The Versatile/PB uses a slightly modified PL110 controller. */
54 enum pl110_bppmode bpp;
57 uint32_t pallette[256];
58 uint32_t raw_pallette[128];
62 static const VMStateDescription vmstate_pl110 = {
65 .minimum_version_id = 1,
66 .fields = (VMStateField[]) {
67 VMSTATE_INT32(version, pl110_state),
68 VMSTATE_UINT32_ARRAY(timing, pl110_state, 4),
69 VMSTATE_UINT32(cr, pl110_state),
70 VMSTATE_UINT32(upbase, pl110_state),
71 VMSTATE_UINT32(lpbase, pl110_state),
72 VMSTATE_UINT32(int_status, pl110_state),
73 VMSTATE_UINT32(int_mask, pl110_state),
74 VMSTATE_INT32(cols, pl110_state),
75 VMSTATE_INT32(rows, pl110_state),
76 VMSTATE_UINT32(bpp, pl110_state),
77 VMSTATE_INT32(invalidate, pl110_state),
78 VMSTATE_UINT32_ARRAY(pallette, pl110_state, 256),
79 VMSTATE_UINT32_ARRAY(raw_pallette, pl110_state, 128),
80 VMSTATE_UINT32_V(mux_ctrl, pl110_state, 2),
85 static const unsigned char pl110_id[] =
86 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
88 /* The Arm documentation (DDI0224C) says the CLDC on the Versatile board
89 has a different ID. However Linux only looks for the normal ID. */
91 static const unsigned char pl110_versatile_id[] =
92 { 0x93, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
94 #define pl110_versatile_id pl110_id
97 static const unsigned char pl111_id[] = {
98 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
101 /* Indexed by pl110_version */
102 static const unsigned char *idregs[] = {
108 #include "pixel_ops.h"
111 #include "pl110_template.h"
113 #include "pl110_template.h"
115 #include "pl110_template.h"
117 #include "pl110_template.h"
119 #include "pl110_template.h"
121 static int pl110_enabled(pl110_state *s)
123 return (s->cr & PL110_CR_EN) && (s->cr & PL110_CR_PWR);
126 static void pl110_update_display(void *opaque)
128 pl110_state *s = (pl110_state *)opaque;
137 if (!pl110_enabled(s))
140 switch (ds_get_bits_per_pixel(s->ds)) {
144 fntable = pl110_draw_fn_8;
148 fntable = pl110_draw_fn_15;
152 fntable = pl110_draw_fn_16;
156 fntable = pl110_draw_fn_24;
160 fntable = pl110_draw_fn_32;
164 fprintf(stderr, "pl110: Bad color depth\n");
167 if (s->cr & PL110_CR_BGR)
172 if ((s->version != PL111) && (s->bpp == BPP_16)) {
173 /* The PL110's native 16 bit mode is 5551; however
174 * most boards with a PL110 implement an external
175 * mux which allows bits to be reshuffled to give
176 * 565 format. The mux is typically controlled by
177 * an external system register.
178 * This is controlled by a GPIO input pin
179 * so boards can wire it up to their register.
181 * The PL111 straightforwardly implements both
182 * 5551 and 565 under control of the bpp field
183 * in the LCDControl register.
185 switch (s->mux_ctrl) {
186 case 3: /* 565 BGR */
187 bpp_offset = (BPP_16_565 - BPP_16);
191 case 0: /* 888; also if we have loaded vmstate from an old version */
192 case 2: /* 565 RGB */
194 /* treat as 565 but honour BGR bit */
195 bpp_offset += (BPP_16_565 - BPP_16);
200 if (s->cr & PL110_CR_BEBO)
201 fn = fntable[s->bpp + 8 + bpp_offset];
202 else if (s->cr & PL110_CR_BEPO)
203 fn = fntable[s->bpp + 16 + bpp_offset];
205 fn = fntable[s->bpp + bpp_offset];
229 dest_width *= s->cols;
231 framebuffer_update_display(s->ds,
232 s->upbase, s->cols, s->rows,
233 src_width, dest_width, 0,
238 dpy_update(s->ds, 0, first, s->cols, last - first + 1);
243 static void pl110_invalidate_display(void * opaque)
245 pl110_state *s = (pl110_state *)opaque;
247 if (pl110_enabled(s)) {
248 qemu_console_resize(s->ds, s->cols, s->rows);
252 static void pl110_update_pallette(pl110_state *s, int n)
256 unsigned int r, g, b;
258 raw = s->raw_pallette[n];
260 for (i = 0; i < 2; i++) {
261 r = (raw & 0x1f) << 3;
263 g = (raw & 0x1f) << 3;
265 b = (raw & 0x1f) << 3;
266 /* The I bit is ignored. */
268 switch (ds_get_bits_per_pixel(s->ds)) {
270 s->pallette[n] = rgb_to_pixel8(r, g, b);
273 s->pallette[n] = rgb_to_pixel15(r, g, b);
276 s->pallette[n] = rgb_to_pixel16(r, g, b);
280 s->pallette[n] = rgb_to_pixel32(r, g, b);
287 static void pl110_resize(pl110_state *s, int width, int height)
289 if (width != s->cols || height != s->rows) {
290 if (pl110_enabled(s)) {
291 qemu_console_resize(s->ds, width, height);
298 /* Update interrupts. */
299 static void pl110_update(pl110_state *s)
301 /* TODO: Implement interrupts. */
304 static uint32_t pl110_read(void *opaque, target_phys_addr_t offset)
306 pl110_state *s = (pl110_state *)opaque;
308 if (offset >= 0xfe0 && offset < 0x1000) {
309 return idregs[s->version][(offset - 0xfe0) >> 2];
311 if (offset >= 0x200 && offset < 0x400) {
312 return s->raw_pallette[(offset - 0x200) >> 2];
314 switch (offset >> 2) {
315 case 0: /* LCDTiming0 */
317 case 1: /* LCDTiming1 */
319 case 2: /* LCDTiming2 */
321 case 3: /* LCDTiming3 */
323 case 4: /* LCDUPBASE */
325 case 5: /* LCDLPBASE */
327 case 6: /* LCDIMSC */
328 if (s->version != PL110) {
332 case 7: /* LCDControl */
333 if (s->version != PL110) {
338 return s->int_status;
340 return s->int_status & s->int_mask;
341 case 11: /* LCDUPCURR */
342 /* TODO: Implement vertical refresh. */
344 case 12: /* LCDLPCURR */
347 hw_error("pl110_read: Bad offset %x\n", (int)offset);
352 static void pl110_write(void *opaque, target_phys_addr_t offset,
355 pl110_state *s = (pl110_state *)opaque;
358 /* For simplicity invalidate the display whenever a control register
361 if (offset >= 0x200 && offset < 0x400) {
363 n = (offset - 0x200) >> 2;
364 s->raw_pallette[(offset - 0x200) >> 2] = val;
365 pl110_update_pallette(s, n);
368 switch (offset >> 2) {
369 case 0: /* LCDTiming0 */
371 n = ((val & 0xfc) + 4) * 4;
372 pl110_resize(s, n, s->rows);
374 case 1: /* LCDTiming1 */
376 n = (val & 0x3ff) + 1;
377 pl110_resize(s, s->cols, n);
379 case 2: /* LCDTiming2 */
382 case 3: /* LCDTiming3 */
385 case 4: /* LCDUPBASE */
388 case 5: /* LCDLPBASE */
391 case 6: /* LCDIMSC */
392 if (s->version != PL110) {
399 case 7: /* LCDControl */
400 if (s->version != PL110) {
405 s->bpp = (val >> 1) & 7;
406 if (pl110_enabled(s)) {
407 qemu_console_resize(s->ds, s->cols, s->rows);
410 case 10: /* LCDICR */
411 s->int_status &= ~val;
415 hw_error("pl110_write: Bad offset %x\n", (int)offset);
419 static CPUReadMemoryFunc * const pl110_readfn[] = {
425 static CPUWriteMemoryFunc * const pl110_writefn[] = {
431 static void pl110_mux_ctrl_set(void *opaque, int line, int level)
433 pl110_state *s = (pl110_state *)opaque;
437 static int pl110_init(SysBusDevice *dev)
439 pl110_state *s = FROM_SYSBUS(pl110_state, dev);
442 iomemtype = cpu_register_io_memory(pl110_readfn,
444 DEVICE_NATIVE_ENDIAN);
445 sysbus_init_mmio(dev, 0x1000, iomemtype);
446 sysbus_init_irq(dev, &s->irq);
447 qdev_init_gpio_in(&s->busdev.qdev, pl110_mux_ctrl_set, 1);
448 s->ds = graphic_console_init(pl110_update_display,
449 pl110_invalidate_display,
454 static int pl110_versatile_init(SysBusDevice *dev)
456 pl110_state *s = FROM_SYSBUS(pl110_state, dev);
457 s->version = PL110_VERSATILE;
458 return pl110_init(dev);
461 static int pl111_init(SysBusDevice *dev)
463 pl110_state *s = FROM_SYSBUS(pl110_state, dev);
465 return pl110_init(dev);
468 static SysBusDeviceInfo pl110_info = {
470 .qdev.name = "pl110",
471 .qdev.size = sizeof(pl110_state),
472 .qdev.vmsd = &vmstate_pl110,
476 static SysBusDeviceInfo pl110_versatile_info = {
477 .init = pl110_versatile_init,
478 .qdev.name = "pl110_versatile",
479 .qdev.size = sizeof(pl110_state),
480 .qdev.vmsd = &vmstate_pl110,
484 static SysBusDeviceInfo pl111_info = {
486 .qdev.name = "pl111",
487 .qdev.size = sizeof(pl110_state),
488 .qdev.vmsd = &vmstate_pl110,
492 static void pl110_register_devices(void)
494 sysbus_register_withprop(&pl110_info);
495 sysbus_register_withprop(&pl110_versatile_info);
496 sysbus_register_withprop(&pl111_info);
499 device_init(pl110_register_devices)