2 * Arm PrimeCell PL110 Color LCD Controller
4 * Copyright (c) 2005 CodeSourcery, LLC.
5 * Written by Paul Brook
7 * This code is licenced under the GNU LGPL
12 #define PL110_CR_EN 0x001
13 #define PL110_CR_BEBO 0x200
14 #define PL110_CR_BEPO 0x400
15 #define PL110_CR_PWR 0x800
39 enum pl110_bppmode bpp;
41 uint32_t pallette[256];
42 uint32_t raw_pallette[128];
46 static const unsigned char pl110_id[] =
47 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
49 static inline uint32_t rgb_to_pixel8(unsigned int r, unsigned int g, unsigned b)
51 return ((r >> 5) << 5) | ((g >> 5) << 2) | (b >> 6);
54 static inline uint32_t rgb_to_pixel15(unsigned int r, unsigned int g, unsigned b)
56 return ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
59 static inline uint32_t rgb_to_pixel16(unsigned int r, unsigned int g, unsigned b)
61 return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
64 static inline uint32_t rgb_to_pixel24(unsigned int r, unsigned int g, unsigned b)
66 return (r << 16) | (g << 8) | b;
69 static inline uint32_t rgb_to_pixel32(unsigned int r, unsigned int g, unsigned b)
71 return (r << 16) | (g << 8) | b;
74 typedef void (*drawfn)(uint32_t *, uint8_t *, const uint8_t *, int);
77 #include "pl110_template.h"
79 #include "pl110_template.h"
81 #include "pl110_template.h"
83 #include "pl110_template.h"
85 #include "pl110_template.h"
87 static int pl110_enabled(pl110_state *s)
89 return (s->cr & PL110_CR_EN) && (s->cr & PL110_CR_PWR);
92 void pl110_update_display(void *opaque)
94 pl110_state *s = (pl110_state *)opaque;
105 int dirty, new_dirty;
108 if (!pl110_enabled(s))
111 switch (s->ds->depth) {
115 fntable = pl110_draw_fn_8;
119 fntable = pl110_draw_fn_15;
123 fntable = pl110_draw_fn_16;
127 fntable = pl110_draw_fn_24;
131 fntable = pl110_draw_fn_32;
135 fprintf(stderr, "pl110: Bad color depth\n");
138 if (s->cr & PL110_CR_BEBO)
139 fn = fntable[s->bpp + 6];
140 else if (s->cr & PL110_CR_BEPO)
141 fn = fntable[s->bpp + 12];
143 fn = fntable[s->bpp];
165 dest_width *= s->cols;
166 pallette = s->pallette;
168 /* HACK: Arm aliases physical memory at 0x80000000. */
169 if (base > 0x80000000)
171 src = phys_ram_base + base;
176 dirty = cpu_physical_memory_get_dirty(addr, VGA_DIRTY_FLAG);
177 for (i = 0; i < s->rows; i++) {
179 if ((addr & TARGET_PAGE_MASK) + src_width >= TARGET_PAGE_SIZE) {
181 for (tmp = 0; tmp < src_width; tmp += TARGET_PAGE_SIZE) {
182 new_dirty |= cpu_physical_memory_get_dirty(addr + tmp,
187 if (dirty || new_dirty || s->invalidate) {
188 fn(pallette, dest, src, s->cols);
202 cpu_physical_memory_reset_dirty(base + first * src_width,
203 base + (last + 1) * src_width,
205 dpy_update(s->ds, 0, first, s->cols, last - first + 1);
208 void pl110_invalidate_display(void * opaque)
210 pl110_state *s = (pl110_state *)opaque;
214 static void pl110_update_pallette(pl110_state *s, int n)
218 unsigned int r, g, b;
220 raw = s->raw_pallette[n];
222 for (i = 0; i < 2; i++) {
223 r = (raw & 0x1f) << 3;
225 g = (raw & 0x1f) << 3;
227 b = (raw & 0x1f) << 3;
228 /* The I bit is ignored. */
230 switch (s->ds->depth) {
232 s->pallette[n] = rgb_to_pixel8(r, g, b);
235 s->pallette[n] = rgb_to_pixel15(r, g, b);
238 s->pallette[n] = rgb_to_pixel16(r, g, b);
242 s->pallette[n] = rgb_to_pixel32(r, g, b);
249 static void pl110_resize(pl110_state *s, int width, int height)
251 if (width != s->cols || height != s->rows) {
252 if (pl110_enabled(s)) {
253 dpy_resize(s->ds, s->cols, s->rows);
260 /* Update interrupts. */
261 static void pl110_update(pl110_state *s)
263 /* TODO: Implement interrupts. */
266 static uint32_t pl110_read(void *opaque, target_phys_addr_t offset)
268 pl110_state *s = (pl110_state *)opaque;
271 if (offset >= 0xfe0 && offset < 0x1000) {
272 return pl110_id[(offset - 0xfe0) >> 2];
274 if (offset >= 0x200 && offset < 0x400) {
275 return s->raw_pallette[(offset - 0x200) >> 2];
277 switch (offset >> 2) {
278 case 0: /* LCDTiming0 */
280 case 1: /* LCDTiming1 */
282 case 2: /* LCDTiming2 */
284 case 3: /* LCDTiming3 */
286 case 4: /* LCDUPBASE */
288 case 5: /* LCDLPBASE */
290 case 6: /* LCDIMSC */
292 case 7: /* LCDControl */
295 return s->int_status;
297 return s->int_status & s->int_mask;
298 case 11: /* LCDUPCURR */
299 /* TODO: Implement vertical refresh. */
301 case 12: /* LCDLPCURR */
304 cpu_abort (cpu_single_env, "pl110_read: Bad offset %x\n", offset);
309 static void pl110_write(void *opaque, target_phys_addr_t offset,
312 pl110_state *s = (pl110_state *)opaque;
315 /* For simplicity invalidate the display whenever a control register
319 if (offset >= 0x200 && offset < 0x400) {
321 n = (offset - 0x200) >> 2;
322 s->raw_pallette[(offset - 0x200) >> 2] = val;
323 pl110_update_pallette(s, n);
326 switch (offset >> 2) {
327 case 0: /* LCDTiming0 */
329 n = ((val & 0xfc) + 4) * 4;
330 pl110_resize(s, n, s->rows);
332 case 1: /* LCDTiming1 */
334 n = (val & 0x3ff) + 1;
335 pl110_resize(s, s->cols, n);
337 case 2: /* LCDTiming2 */
340 case 3: /* LCDTiming3 */
343 case 4: /* LCDUPBASE */
346 case 5: /* LCDLPBASE */
349 case 6: /* LCDIMSC */
353 case 7: /* LCDControl */
355 s->bpp = (val >> 1) & 7;
356 if (pl110_enabled(s)) {
357 dpy_resize(s->ds, s->cols, s->rows);
360 case 10: /* LCDICR */
361 s->int_status &= ~val;
365 cpu_abort (cpu_single_env, "pl110_write: Bad offset %x\n", offset);
369 static CPUReadMemoryFunc *pl110_readfn[] = {
375 static CPUWriteMemoryFunc *pl110_writefn[] = {
381 void *pl110_init(DisplayState *ds, uint32_t base, void *pic, int irq)
386 s = (pl110_state *)qemu_mallocz(sizeof(pl110_state));
387 iomemtype = cpu_register_io_memory(0, pl110_readfn,
389 cpu_register_physical_memory(base, 0x007fffff, iomemtype);
394 /* ??? Save/restore. */