Fix setting counter limit to 0 (Robert Reif)
[qemu/qemu_0_9_1_stable.git] / hw / pl110.c
blobe5b2b2363b329ddf05138daab9c9421c20ae546d
1 /*
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
8 */
10 #include "hw.h"
11 #include "primecell.h"
12 #include "console.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
20 enum pl110_bppmode
22 BPP_1,
23 BPP_2,
24 BPP_4,
25 BPP_8,
26 BPP_16,
27 BPP_32
30 typedef struct {
31 uint32_t base;
32 DisplayState *ds;
33 /* The Versatile/PB uses a slightly modified PL110 controller. */
34 int versatile;
35 uint32_t timing[4];
36 uint32_t cr;
37 uint32_t upbase;
38 uint32_t lpbase;
39 uint32_t int_status;
40 uint32_t int_mask;
41 int cols;
42 int rows;
43 enum pl110_bppmode bpp;
44 int invalidate;
45 uint32_t pallette[256];
46 uint32_t raw_pallette[128];
47 qemu_irq irq;
48 } pl110_state;
50 static const unsigned char pl110_id[] =
51 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
53 /* The Arm documentation (DDI0224C) says the CLDC on the Versatile board
54 has a different ID. However Linux only looks for the normal ID. */
55 #if 0
56 static const unsigned char pl110_versatile_id[] =
57 { 0x93, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
58 #else
59 #define pl110_versatile_id pl110_id
60 #endif
62 static inline uint32_t rgb_to_pixel8(unsigned int r, unsigned int g, unsigned b)
64 return ((r >> 5) << 5) | ((g >> 5) << 2) | (b >> 6);
67 static inline uint32_t rgb_to_pixel15(unsigned int r, unsigned int g, unsigned b)
69 return ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
72 static inline uint32_t rgb_to_pixel16(unsigned int r, unsigned int g, unsigned b)
74 return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
77 static inline uint32_t rgb_to_pixel24(unsigned int r, unsigned int g, unsigned b)
79 return (r << 16) | (g << 8) | b;
82 static inline uint32_t rgb_to_pixel32(unsigned int r, unsigned int g, unsigned b)
84 return (r << 16) | (g << 8) | b;
87 typedef void (*drawfn)(uint32_t *, uint8_t *, const uint8_t *, int);
89 #define BITS 8
90 #include "pl110_template.h"
91 #define BITS 15
92 #include "pl110_template.h"
93 #define BITS 16
94 #include "pl110_template.h"
95 #define BITS 24
96 #include "pl110_template.h"
97 #define BITS 32
98 #include "pl110_template.h"
100 static int pl110_enabled(pl110_state *s)
102 return (s->cr & PL110_CR_EN) && (s->cr & PL110_CR_PWR);
105 static void pl110_update_display(void *opaque)
107 pl110_state *s = (pl110_state *)opaque;
108 drawfn* fntable;
109 drawfn fn;
110 uint32_t *pallette;
111 uint32_t addr;
112 uint32_t base;
113 int dest_width;
114 int src_width;
115 uint8_t *dest;
116 uint8_t *src;
117 int first, last = 0;
118 int dirty, new_dirty;
119 int i;
120 int bpp_offset;
122 if (!pl110_enabled(s))
123 return;
125 switch (s->ds->depth) {
126 case 0:
127 return;
128 case 8:
129 fntable = pl110_draw_fn_8;
130 dest_width = 1;
131 break;
132 case 15:
133 fntable = pl110_draw_fn_15;
134 dest_width = 2;
135 break;
136 case 16:
137 fntable = pl110_draw_fn_16;
138 dest_width = 2;
139 break;
140 case 24:
141 fntable = pl110_draw_fn_24;
142 dest_width = 3;
143 break;
144 case 32:
145 fntable = pl110_draw_fn_32;
146 dest_width = 4;
147 break;
148 default:
149 fprintf(stderr, "pl110: Bad color depth\n");
150 exit(1);
152 if (s->cr & PL110_CR_BGR)
153 bpp_offset = 0;
154 else
155 bpp_offset = 18;
157 if (s->cr & PL110_CR_BEBO)
158 fn = fntable[s->bpp + 6 + bpp_offset];
159 else if (s->cr & PL110_CR_BEPO)
160 fn = fntable[s->bpp + 12 + bpp_offset];
161 else
162 fn = fntable[s->bpp + bpp_offset];
164 src_width = s->cols;
165 switch (s->bpp) {
166 case BPP_1:
167 src_width >>= 3;
168 break;
169 case BPP_2:
170 src_width >>= 2;
171 break;
172 case BPP_4:
173 src_width >>= 1;
174 break;
175 case BPP_8:
176 break;
177 case BPP_16:
178 src_width <<= 1;
179 break;
180 case BPP_32:
181 src_width <<= 2;
182 break;
184 dest_width *= s->cols;
185 pallette = s->pallette;
186 base = s->upbase;
187 /* HACK: Arm aliases physical memory at 0x80000000. */
188 if (base > 0x80000000)
189 base -= 0x80000000;
190 src = phys_ram_base + base;
191 dest = s->ds->data;
192 first = -1;
193 addr = base;
195 dirty = cpu_physical_memory_get_dirty(addr, VGA_DIRTY_FLAG);
196 new_dirty = dirty;
197 for (i = 0; i < s->rows; i++) {
198 if ((addr & ~TARGET_PAGE_MASK) + src_width >= TARGET_PAGE_SIZE) {
199 uint32_t tmp;
200 new_dirty = 0;
201 for (tmp = 0; tmp < src_width; tmp += TARGET_PAGE_SIZE) {
202 new_dirty |= cpu_physical_memory_get_dirty(addr + tmp,
203 VGA_DIRTY_FLAG);
207 if (dirty || new_dirty || s->invalidate) {
208 fn(pallette, dest, src, s->cols);
209 if (first == -1)
210 first = i;
211 last = i;
213 dirty = new_dirty;
214 addr += src_width;
215 dest += dest_width;
216 src += src_width;
218 if (first < 0)
219 return;
221 s->invalidate = 0;
222 cpu_physical_memory_reset_dirty(base + first * src_width,
223 base + (last + 1) * src_width,
224 VGA_DIRTY_FLAG);
225 dpy_update(s->ds, 0, first, s->cols, last - first + 1);
228 static void pl110_invalidate_display(void * opaque)
230 pl110_state *s = (pl110_state *)opaque;
231 s->invalidate = 1;
234 static void pl110_update_pallette(pl110_state *s, int n)
236 int i;
237 uint32_t raw;
238 unsigned int r, g, b;
240 raw = s->raw_pallette[n];
241 n <<= 1;
242 for (i = 0; i < 2; i++) {
243 r = (raw & 0x1f) << 3;
244 raw >>= 5;
245 g = (raw & 0x1f) << 3;
246 raw >>= 5;
247 b = (raw & 0x1f) << 3;
248 /* The I bit is ignored. */
249 raw >>= 6;
250 switch (s->ds->depth) {
251 case 8:
252 s->pallette[n] = rgb_to_pixel8(r, g, b);
253 break;
254 case 15:
255 s->pallette[n] = rgb_to_pixel15(r, g, b);
256 break;
257 case 16:
258 s->pallette[n] = rgb_to_pixel16(r, g, b);
259 break;
260 case 24:
261 case 32:
262 s->pallette[n] = rgb_to_pixel32(r, g, b);
263 break;
265 n++;
269 static void pl110_resize(pl110_state *s, int width, int height)
271 if (width != s->cols || height != s->rows) {
272 if (pl110_enabled(s)) {
273 dpy_resize(s->ds, width, height);
276 s->cols = width;
277 s->rows = height;
280 /* Update interrupts. */
281 static void pl110_update(pl110_state *s)
283 /* TODO: Implement interrupts. */
286 static uint32_t pl110_read(void *opaque, target_phys_addr_t offset)
288 pl110_state *s = (pl110_state *)opaque;
290 offset -= s->base;
291 if (offset >= 0xfe0 && offset < 0x1000) {
292 if (s->versatile)
293 return pl110_versatile_id[(offset - 0xfe0) >> 2];
294 else
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 */
302 return s->timing[0];
303 case 1: /* LCDTiming1 */
304 return s->timing[1];
305 case 2: /* LCDTiming2 */
306 return s->timing[2];
307 case 3: /* LCDTiming3 */
308 return s->timing[3];
309 case 4: /* LCDUPBASE */
310 return s->upbase;
311 case 5: /* LCDLPBASE */
312 return s->lpbase;
313 case 6: /* LCDIMSC */
314 if (s->versatile)
315 return s->cr;
316 return s->int_mask;
317 case 7: /* LCDControl */
318 if (s->versatile)
319 return s->int_mask;
320 return s->cr;
321 case 8: /* LCDRIS */
322 return s->int_status;
323 case 9: /* LCDMIS */
324 return s->int_status & s->int_mask;
325 case 11: /* LCDUPCURR */
326 /* TODO: Implement vertical refresh. */
327 return s->upbase;
328 case 12: /* LCDLPCURR */
329 return s->lpbase;
330 default:
331 cpu_abort (cpu_single_env, "pl110_read: Bad offset %x\n", (int)offset);
332 return 0;
336 static void pl110_write(void *opaque, target_phys_addr_t offset,
337 uint32_t val)
339 pl110_state *s = (pl110_state *)opaque;
340 int n;
342 /* For simplicity invalidate the display whenever a control register
343 is writen to. */
344 s->invalidate = 1;
345 offset -= s->base;
346 if (offset >= 0x200 && offset < 0x400) {
347 /* Pallette. */
348 n = (offset - 0x200) >> 2;
349 s->raw_pallette[(offset - 0x200) >> 2] = val;
350 pl110_update_pallette(s, n);
351 return;
353 switch (offset >> 2) {
354 case 0: /* LCDTiming0 */
355 s->timing[0] = val;
356 n = ((val & 0xfc) + 4) * 4;
357 pl110_resize(s, n, s->rows);
358 break;
359 case 1: /* LCDTiming1 */
360 s->timing[1] = val;
361 n = (val & 0x3ff) + 1;
362 pl110_resize(s, s->cols, n);
363 break;
364 case 2: /* LCDTiming2 */
365 s->timing[2] = val;
366 break;
367 case 3: /* LCDTiming3 */
368 s->timing[3] = val;
369 break;
370 case 4: /* LCDUPBASE */
371 s->upbase = val;
372 break;
373 case 5: /* LCDLPBASE */
374 s->lpbase = val;
375 break;
376 case 6: /* LCDIMSC */
377 if (s->versatile)
378 goto control;
379 imsc:
380 s->int_mask = val;
381 pl110_update(s);
382 break;
383 case 7: /* LCDControl */
384 if (s->versatile)
385 goto imsc;
386 control:
387 s->cr = val;
388 s->bpp = (val >> 1) & 7;
389 if (pl110_enabled(s)) {
390 dpy_resize(s->ds, s->cols, s->rows);
392 break;
393 case 10: /* LCDICR */
394 s->int_status &= ~val;
395 pl110_update(s);
396 break;
397 default:
398 cpu_abort (cpu_single_env, "pl110_write: Bad offset %x\n", (int)offset);
402 static CPUReadMemoryFunc *pl110_readfn[] = {
403 pl110_read,
404 pl110_read,
405 pl110_read
408 static CPUWriteMemoryFunc *pl110_writefn[] = {
409 pl110_write,
410 pl110_write,
411 pl110_write
414 void *pl110_init(DisplayState *ds, uint32_t base, qemu_irq irq,
415 int versatile)
417 pl110_state *s;
418 int iomemtype;
420 s = (pl110_state *)qemu_mallocz(sizeof(pl110_state));
421 iomemtype = cpu_register_io_memory(0, pl110_readfn,
422 pl110_writefn, s);
423 cpu_register_physical_memory(base, 0x00001000, iomemtype);
424 s->base = base;
425 s->ds = ds;
426 s->versatile = versatile;
427 s->irq = irq;
428 graphic_console_init(ds, pl110_update_display, pl110_invalidate_display,
429 NULL, s);
430 /* ??? Save/restore. */
431 return s;