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. */
55 enum pl110_bppmode bpp;
58 uint32_t pallette[256];
59 uint32_t raw_pallette[128];
63 static int vmstate_pl110_post_load(void *opaque, int version_id);
65 static const VMStateDescription vmstate_pl110 = {
68 .minimum_version_id = 1,
69 .post_load = vmstate_pl110_post_load,
70 .fields = (VMStateField[]) {
71 VMSTATE_INT32(version, pl110_state),
72 VMSTATE_UINT32_ARRAY(timing, pl110_state, 4),
73 VMSTATE_UINT32(cr, pl110_state),
74 VMSTATE_UINT32(upbase, pl110_state),
75 VMSTATE_UINT32(lpbase, pl110_state),
76 VMSTATE_UINT32(int_status, pl110_state),
77 VMSTATE_UINT32(int_mask, pl110_state),
78 VMSTATE_INT32(cols, pl110_state),
79 VMSTATE_INT32(rows, pl110_state),
80 VMSTATE_UINT32(bpp, pl110_state),
81 VMSTATE_INT32(invalidate, pl110_state),
82 VMSTATE_UINT32_ARRAY(pallette, pl110_state, 256),
83 VMSTATE_UINT32_ARRAY(raw_pallette, pl110_state, 128),
84 VMSTATE_UINT32_V(mux_ctrl, pl110_state, 2),
89 static const unsigned char pl110_id[] =
90 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
92 /* The Arm documentation (DDI0224C) says the CLDC on the Versatile board
93 has a different ID. However Linux only looks for the normal ID. */
95 static const unsigned char pl110_versatile_id[] =
96 { 0x93, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
98 #define pl110_versatile_id pl110_id
101 static const unsigned char pl111_id[] = {
102 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
105 /* Indexed by pl110_version */
106 static const unsigned char *idregs[] = {
112 #include "pixel_ops.h"
115 #include "pl110_template.h"
117 #include "pl110_template.h"
119 #include "pl110_template.h"
121 #include "pl110_template.h"
123 #include "pl110_template.h"
125 static int pl110_enabled(pl110_state *s)
127 return (s->cr & PL110_CR_EN) && (s->cr & PL110_CR_PWR);
130 static void pl110_update_display(void *opaque)
132 pl110_state *s = (pl110_state *)opaque;
141 if (!pl110_enabled(s))
144 switch (ds_get_bits_per_pixel(s->ds)) {
148 fntable = pl110_draw_fn_8;
152 fntable = pl110_draw_fn_15;
156 fntable = pl110_draw_fn_16;
160 fntable = pl110_draw_fn_24;
164 fntable = pl110_draw_fn_32;
168 fprintf(stderr, "pl110: Bad color depth\n");
171 if (s->cr & PL110_CR_BGR)
176 if ((s->version != PL111) && (s->bpp == BPP_16)) {
177 /* The PL110's native 16 bit mode is 5551; however
178 * most boards with a PL110 implement an external
179 * mux which allows bits to be reshuffled to give
180 * 565 format. The mux is typically controlled by
181 * an external system register.
182 * This is controlled by a GPIO input pin
183 * so boards can wire it up to their register.
185 * The PL111 straightforwardly implements both
186 * 5551 and 565 under control of the bpp field
187 * in the LCDControl register.
189 switch (s->mux_ctrl) {
190 case 3: /* 565 BGR */
191 bpp_offset = (BPP_16_565 - BPP_16);
195 case 0: /* 888; also if we have loaded vmstate from an old version */
196 case 2: /* 565 RGB */
198 /* treat as 565 but honour BGR bit */
199 bpp_offset += (BPP_16_565 - BPP_16);
204 if (s->cr & PL110_CR_BEBO)
205 fn = fntable[s->bpp + 8 + bpp_offset];
206 else if (s->cr & PL110_CR_BEPO)
207 fn = fntable[s->bpp + 16 + bpp_offset];
209 fn = fntable[s->bpp + bpp_offset];
233 dest_width *= s->cols;
235 framebuffer_update_display(s->ds, sysbus_address_space(&s->busdev),
236 s->upbase, s->cols, s->rows,
237 src_width, dest_width, 0,
242 dpy_update(s->ds, 0, first, s->cols, last - first + 1);
247 static void pl110_invalidate_display(void * opaque)
249 pl110_state *s = (pl110_state *)opaque;
251 if (pl110_enabled(s)) {
252 qemu_console_resize(s->ds, s->cols, s->rows);
256 static void pl110_update_pallette(pl110_state *s, int n)
260 unsigned int r, g, b;
262 raw = s->raw_pallette[n];
264 for (i = 0; i < 2; i++) {
265 r = (raw & 0x1f) << 3;
267 g = (raw & 0x1f) << 3;
269 b = (raw & 0x1f) << 3;
270 /* The I bit is ignored. */
272 switch (ds_get_bits_per_pixel(s->ds)) {
274 s->pallette[n] = rgb_to_pixel8(r, g, b);
277 s->pallette[n] = rgb_to_pixel15(r, g, b);
280 s->pallette[n] = rgb_to_pixel16(r, g, b);
284 s->pallette[n] = rgb_to_pixel32(r, g, b);
291 static void pl110_resize(pl110_state *s, int width, int height)
293 if (width != s->cols || height != s->rows) {
294 if (pl110_enabled(s)) {
295 qemu_console_resize(s->ds, width, height);
302 /* Update interrupts. */
303 static void pl110_update(pl110_state *s)
305 /* TODO: Implement interrupts. */
308 static uint64_t pl110_read(void *opaque, target_phys_addr_t offset,
311 pl110_state *s = (pl110_state *)opaque;
313 if (offset >= 0xfe0 && offset < 0x1000) {
314 return idregs[s->version][(offset - 0xfe0) >> 2];
316 if (offset >= 0x200 && offset < 0x400) {
317 return s->raw_pallette[(offset - 0x200) >> 2];
319 switch (offset >> 2) {
320 case 0: /* LCDTiming0 */
322 case 1: /* LCDTiming1 */
324 case 2: /* LCDTiming2 */
326 case 3: /* LCDTiming3 */
328 case 4: /* LCDUPBASE */
330 case 5: /* LCDLPBASE */
332 case 6: /* LCDIMSC */
333 if (s->version != PL110) {
337 case 7: /* LCDControl */
338 if (s->version != PL110) {
343 return s->int_status;
345 return s->int_status & s->int_mask;
346 case 11: /* LCDUPCURR */
347 /* TODO: Implement vertical refresh. */
349 case 12: /* LCDLPCURR */
352 hw_error("pl110_read: Bad offset %x\n", (int)offset);
357 static void pl110_write(void *opaque, target_phys_addr_t offset,
358 uint64_t val, unsigned size)
360 pl110_state *s = (pl110_state *)opaque;
363 /* For simplicity invalidate the display whenever a control register
366 if (offset >= 0x200 && offset < 0x400) {
368 n = (offset - 0x200) >> 2;
369 s->raw_pallette[(offset - 0x200) >> 2] = val;
370 pl110_update_pallette(s, n);
373 switch (offset >> 2) {
374 case 0: /* LCDTiming0 */
376 n = ((val & 0xfc) + 4) * 4;
377 pl110_resize(s, n, s->rows);
379 case 1: /* LCDTiming1 */
381 n = (val & 0x3ff) + 1;
382 pl110_resize(s, s->cols, n);
384 case 2: /* LCDTiming2 */
387 case 3: /* LCDTiming3 */
390 case 4: /* LCDUPBASE */
393 case 5: /* LCDLPBASE */
396 case 6: /* LCDIMSC */
397 if (s->version != PL110) {
404 case 7: /* LCDControl */
405 if (s->version != PL110) {
410 s->bpp = (val >> 1) & 7;
411 if (pl110_enabled(s)) {
412 qemu_console_resize(s->ds, s->cols, s->rows);
415 case 10: /* LCDICR */
416 s->int_status &= ~val;
420 hw_error("pl110_write: Bad offset %x\n", (int)offset);
424 static const MemoryRegionOps pl110_ops = {
426 .write = pl110_write,
427 .endianness = DEVICE_NATIVE_ENDIAN,
430 static void pl110_mux_ctrl_set(void *opaque, int line, int level)
432 pl110_state *s = (pl110_state *)opaque;
436 static int vmstate_pl110_post_load(void *opaque, int version_id)
438 pl110_state *s = opaque;
439 /* Make sure we redraw, and at the right size */
440 pl110_invalidate_display(s);
444 static int pl110_init(SysBusDevice *dev)
446 pl110_state *s = FROM_SYSBUS(pl110_state, dev);
448 memory_region_init_io(&s->iomem, &pl110_ops, s, "pl110", 0x1000);
449 sysbus_init_mmio(dev, &s->iomem);
450 sysbus_init_irq(dev, &s->irq);
451 qdev_init_gpio_in(&s->busdev.qdev, pl110_mux_ctrl_set, 1);
452 s->ds = graphic_console_init(pl110_update_display,
453 pl110_invalidate_display,
458 static int pl110_versatile_init(SysBusDevice *dev)
460 pl110_state *s = FROM_SYSBUS(pl110_state, dev);
461 s->version = PL110_VERSATILE;
462 return pl110_init(dev);
465 static int pl111_init(SysBusDevice *dev)
467 pl110_state *s = FROM_SYSBUS(pl110_state, dev);
469 return pl110_init(dev);
472 static SysBusDeviceInfo pl110_info = {
474 .qdev.name = "pl110",
475 .qdev.size = sizeof(pl110_state),
476 .qdev.vmsd = &vmstate_pl110,
480 static SysBusDeviceInfo pl110_versatile_info = {
481 .init = pl110_versatile_init,
482 .qdev.name = "pl110_versatile",
483 .qdev.size = sizeof(pl110_state),
484 .qdev.vmsd = &vmstate_pl110,
488 static SysBusDeviceInfo pl111_info = {
490 .qdev.name = "pl111",
491 .qdev.size = sizeof(pl110_state),
492 .qdev.vmsd = &vmstate_pl110,
496 static void pl110_register_devices(void)
498 sysbus_register_withprop(&pl110_info);
499 sysbus_register_withprop(&pl110_versatile_info);
500 sysbus_register_withprop(&pl111_info);
503 device_init(pl110_register_devices)