2 * Arm PrimeCell PL110 Color LCD Controller
4 * Copyright (c) 2005-2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GNU LGPL
11 #include "primecell.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
34 /* The Versatile/PB uses a slightly modified PL110 controller. */
44 enum pl110_bppmode bpp;
46 uint32_t pallette[256];
47 uint32_t raw_pallette[128];
51 static const unsigned char pl110_id[] =
52 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
54 /* The Arm documentation (DDI0224C) says the CLDC on the Versatile board
55 has a different ID. However Linux only looks for the normal ID. */
57 static const unsigned char pl110_versatile_id[] =
58 { 0x93, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
60 #define pl110_versatile_id pl110_id
63 static inline uint32_t rgb_to_pixel8(unsigned int r, unsigned int g, unsigned b)
65 return ((r >> 5) << 5) | ((g >> 5) << 2) | (b >> 6);
68 static inline uint32_t rgb_to_pixel15(unsigned int r, unsigned int g, unsigned b)
70 return ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
73 static inline uint32_t rgb_to_pixel16(unsigned int r, unsigned int g, unsigned b)
75 return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
78 static inline uint32_t rgb_to_pixel24(unsigned int r, unsigned int g, unsigned b)
80 return (r << 16) | (g << 8) | b;
83 static inline uint32_t rgb_to_pixel32(unsigned int r, unsigned int g, unsigned b)
85 return (r << 16) | (g << 8) | b;
88 typedef void (*drawfn)(uint32_t *, uint8_t *, const uint8_t *, int);
91 #include "pl110_template.h"
93 #include "pl110_template.h"
95 #include "pl110_template.h"
97 #include "pl110_template.h"
99 #include "pl110_template.h"
101 static int pl110_enabled(pl110_state *s)
103 return (s->cr & PL110_CR_EN) && (s->cr & PL110_CR_PWR);
106 static void pl110_update_display(void *opaque)
108 pl110_state *s = (pl110_state *)opaque;
119 int dirty, new_dirty;
123 if (!pl110_enabled(s))
126 switch (ds_get_bits_per_pixel(s->ds)) {
130 fntable = pl110_draw_fn_8;
134 fntable = pl110_draw_fn_15;
138 fntable = pl110_draw_fn_16;
142 fntable = pl110_draw_fn_24;
146 fntable = pl110_draw_fn_32;
150 fprintf(stderr, "pl110: Bad color depth\n");
153 if (s->cr & PL110_CR_BGR)
158 if (s->cr & PL110_CR_BEBO)
159 fn = fntable[s->bpp + 6 + bpp_offset];
160 else if (s->cr & PL110_CR_BEPO)
161 fn = fntable[s->bpp + 12 + bpp_offset];
163 fn = fntable[s->bpp + bpp_offset];
185 dest_width *= s->cols;
186 pallette = s->pallette;
188 /* HACK: Arm aliases physical memory at 0x80000000. */
189 if (base > 0x80000000)
191 src = phys_ram_base + base;
192 dest = ds_get_data(s->ds);
196 dirty = cpu_physical_memory_get_dirty(addr, VGA_DIRTY_FLAG);
198 for (i = 0; i < s->rows; i++) {
199 if ((addr & ~TARGET_PAGE_MASK) + src_width >= TARGET_PAGE_SIZE) {
202 for (tmp = 0; tmp < src_width; tmp += TARGET_PAGE_SIZE) {
203 new_dirty |= cpu_physical_memory_get_dirty(addr + tmp,
208 if (dirty || new_dirty || s->invalidate) {
209 fn(pallette, dest, src, s->cols);
223 cpu_physical_memory_reset_dirty(base + first * src_width,
224 base + (last + 1) * src_width,
226 dpy_update(s->ds, 0, first, s->cols, last - first + 1);
229 static void pl110_invalidate_display(void * opaque)
231 pl110_state *s = (pl110_state *)opaque;
235 static void pl110_update_pallette(pl110_state *s, int n)
239 unsigned int r, g, b;
241 raw = s->raw_pallette[n];
243 for (i = 0; i < 2; i++) {
244 r = (raw & 0x1f) << 3;
246 g = (raw & 0x1f) << 3;
248 b = (raw & 0x1f) << 3;
249 /* The I bit is ignored. */
251 switch (ds_get_bits_per_pixel(s->ds)) {
253 s->pallette[n] = rgb_to_pixel8(r, g, b);
256 s->pallette[n] = rgb_to_pixel15(r, g, b);
259 s->pallette[n] = rgb_to_pixel16(r, g, b);
263 s->pallette[n] = rgb_to_pixel32(r, g, b);
270 static void pl110_resize(pl110_state *s, int width, int height)
272 if (width != s->cols || height != s->rows) {
273 if (pl110_enabled(s)) {
274 qemu_console_resize(s->console, width, height);
281 /* Update interrupts. */
282 static void pl110_update(pl110_state *s)
284 /* TODO: Implement interrupts. */
287 static uint32_t pl110_read(void *opaque, target_phys_addr_t offset)
289 pl110_state *s = (pl110_state *)opaque;
291 if (offset >= 0xfe0 && offset < 0x1000) {
293 return pl110_versatile_id[(offset - 0xfe0) >> 2];
295 return pl110_id[(offset - 0xfe0) >> 2];
297 if (offset >= 0x200 && offset < 0x400) {
298 return s->raw_pallette[(offset - 0x200) >> 2];
300 switch (offset >> 2) {
301 case 0: /* LCDTiming0 */
303 case 1: /* LCDTiming1 */
305 case 2: /* LCDTiming2 */
307 case 3: /* LCDTiming3 */
309 case 4: /* LCDUPBASE */
311 case 5: /* LCDLPBASE */
313 case 6: /* LCDIMSC */
317 case 7: /* LCDControl */
322 return s->int_status;
324 return s->int_status & s->int_mask;
325 case 11: /* LCDUPCURR */
326 /* TODO: Implement vertical refresh. */
328 case 12: /* LCDLPCURR */
331 cpu_abort (cpu_single_env, "pl110_read: Bad offset %x\n", (int)offset);
336 static void pl110_write(void *opaque, target_phys_addr_t offset,
339 pl110_state *s = (pl110_state *)opaque;
342 /* For simplicity invalidate the display whenever a control register
345 if (offset >= 0x200 && offset < 0x400) {
347 n = (offset - 0x200) >> 2;
348 s->raw_pallette[(offset - 0x200) >> 2] = val;
349 pl110_update_pallette(s, n);
352 switch (offset >> 2) {
353 case 0: /* LCDTiming0 */
355 n = ((val & 0xfc) + 4) * 4;
356 pl110_resize(s, n, s->rows);
358 case 1: /* LCDTiming1 */
360 n = (val & 0x3ff) + 1;
361 pl110_resize(s, s->cols, n);
363 case 2: /* LCDTiming2 */
366 case 3: /* LCDTiming3 */
369 case 4: /* LCDUPBASE */
372 case 5: /* LCDLPBASE */
375 case 6: /* LCDIMSC */
382 case 7: /* LCDControl */
387 s->bpp = (val >> 1) & 7;
388 if (pl110_enabled(s)) {
389 qemu_console_resize(s->console, s->cols, s->rows);
392 case 10: /* LCDICR */
393 s->int_status &= ~val;
397 cpu_abort (cpu_single_env, "pl110_write: Bad offset %x\n", (int)offset);
401 static CPUReadMemoryFunc *pl110_readfn[] = {
407 static CPUWriteMemoryFunc *pl110_writefn[] = {
413 void *pl110_init(DisplayState *ds, uint32_t base, qemu_irq irq,
419 s = (pl110_state *)qemu_mallocz(sizeof(pl110_state));
420 iomemtype = cpu_register_io_memory(0, pl110_readfn,
422 cpu_register_physical_memory(base, 0x00001000, iomemtype);
424 s->versatile = versatile;
426 s->console = graphic_console_init(ds, pl110_update_display,
427 pl110_invalidate_display,
429 /* ??? Save/restore. */