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
11 #include "ui/console.h"
12 #include "framebuffer.h"
13 #include "ui/pixel_ops.h"
15 #define PL110_CR_EN 0x001
16 #define PL110_CR_BGR 0x100
17 #define PL110_CR_BEBO 0x200
18 #define PL110_CR_BEPO 0x400
19 #define PL110_CR_PWR 0x800
29 BPP_16_565
, /* PL111 only */
30 BPP_12
/* PL111 only */
34 /* The Versatile/PB uses a slightly modified PL110 controller. */
56 enum pl110_bppmode bpp
;
59 uint32_t palette
[256];
60 uint32_t raw_palette
[128];
64 static int vmstate_pl110_post_load(void *opaque
, int version_id
);
66 static const VMStateDescription vmstate_pl110
= {
69 .minimum_version_id
= 1,
70 .post_load
= vmstate_pl110_post_load
,
71 .fields
= (VMStateField
[]) {
72 VMSTATE_INT32(version
, pl110_state
),
73 VMSTATE_UINT32_ARRAY(timing
, pl110_state
, 4),
74 VMSTATE_UINT32(cr
, pl110_state
),
75 VMSTATE_UINT32(upbase
, pl110_state
),
76 VMSTATE_UINT32(lpbase
, pl110_state
),
77 VMSTATE_UINT32(int_status
, pl110_state
),
78 VMSTATE_UINT32(int_mask
, pl110_state
),
79 VMSTATE_INT32(cols
, pl110_state
),
80 VMSTATE_INT32(rows
, pl110_state
),
81 VMSTATE_UINT32(bpp
, pl110_state
),
82 VMSTATE_INT32(invalidate
, pl110_state
),
83 VMSTATE_UINT32_ARRAY(palette
, pl110_state
, 256),
84 VMSTATE_UINT32_ARRAY(raw_palette
, pl110_state
, 128),
85 VMSTATE_UINT32_V(mux_ctrl
, pl110_state
, 2),
90 static const unsigned char pl110_id
[] =
91 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
93 /* The Arm documentation (DDI0224C) says the CLDC on the Versatile board
94 has a different ID. However Linux only looks for the normal ID. */
96 static const unsigned char pl110_versatile_id
[] =
97 { 0x93, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
99 #define pl110_versatile_id pl110_id
102 static const unsigned char pl111_id
[] = {
103 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
106 /* Indexed by pl110_version */
107 static const unsigned char *idregs
[] = {
114 #include "pl110_template.h"
116 #include "pl110_template.h"
118 #include "pl110_template.h"
120 #include "pl110_template.h"
122 #include "pl110_template.h"
124 static int pl110_enabled(pl110_state
*s
)
126 return (s
->cr
& PL110_CR_EN
) && (s
->cr
& PL110_CR_PWR
);
129 static void pl110_update_display(void *opaque
)
131 pl110_state
*s
= (pl110_state
*)opaque
;
140 if (!pl110_enabled(s
))
143 switch (ds_get_bits_per_pixel(s
->ds
)) {
147 fntable
= pl110_draw_fn_8
;
151 fntable
= pl110_draw_fn_15
;
155 fntable
= pl110_draw_fn_16
;
159 fntable
= pl110_draw_fn_24
;
163 fntable
= pl110_draw_fn_32
;
167 fprintf(stderr
, "pl110: Bad color depth\n");
170 if (s
->cr
& PL110_CR_BGR
)
175 if ((s
->version
!= PL111
) && (s
->bpp
== BPP_16
)) {
176 /* The PL110's native 16 bit mode is 5551; however
177 * most boards with a PL110 implement an external
178 * mux which allows bits to be reshuffled to give
179 * 565 format. The mux is typically controlled by
180 * an external system register.
181 * This is controlled by a GPIO input pin
182 * so boards can wire it up to their register.
184 * The PL111 straightforwardly implements both
185 * 5551 and 565 under control of the bpp field
186 * in the LCDControl register.
188 switch (s
->mux_ctrl
) {
189 case 3: /* 565 BGR */
190 bpp_offset
= (BPP_16_565
- BPP_16
);
194 case 0: /* 888; also if we have loaded vmstate from an old version */
195 case 2: /* 565 RGB */
197 /* treat as 565 but honour BGR bit */
198 bpp_offset
+= (BPP_16_565
- BPP_16
);
203 if (s
->cr
& PL110_CR_BEBO
)
204 fn
= fntable
[s
->bpp
+ 8 + bpp_offset
];
205 else if (s
->cr
& PL110_CR_BEPO
)
206 fn
= fntable
[s
->bpp
+ 16 + bpp_offset
];
208 fn
= fntable
[s
->bpp
+ bpp_offset
];
232 dest_width
*= s
->cols
;
234 framebuffer_update_display(s
->ds
, sysbus_address_space(&s
->busdev
),
235 s
->upbase
, s
->cols
, s
->rows
,
236 src_width
, dest_width
, 0,
241 dpy_gfx_update(s
->ds
, 0, first
, s
->cols
, last
- first
+ 1);
246 static void pl110_invalidate_display(void * opaque
)
248 pl110_state
*s
= (pl110_state
*)opaque
;
250 if (pl110_enabled(s
)) {
251 qemu_console_resize(s
->ds
, s
->cols
, s
->rows
);
255 static void pl110_update_palette(pl110_state
*s
, int n
)
259 unsigned int r
, g
, b
;
261 raw
= s
->raw_palette
[n
];
263 for (i
= 0; i
< 2; i
++) {
264 r
= (raw
& 0x1f) << 3;
266 g
= (raw
& 0x1f) << 3;
268 b
= (raw
& 0x1f) << 3;
269 /* The I bit is ignored. */
271 switch (ds_get_bits_per_pixel(s
->ds
)) {
273 s
->palette
[n
] = rgb_to_pixel8(r
, g
, b
);
276 s
->palette
[n
] = rgb_to_pixel15(r
, g
, b
);
279 s
->palette
[n
] = rgb_to_pixel16(r
, g
, b
);
283 s
->palette
[n
] = rgb_to_pixel32(r
, g
, b
);
290 static void pl110_resize(pl110_state
*s
, int width
, int height
)
292 if (width
!= s
->cols
|| height
!= s
->rows
) {
293 if (pl110_enabled(s
)) {
294 qemu_console_resize(s
->ds
, width
, height
);
301 /* Update interrupts. */
302 static void pl110_update(pl110_state
*s
)
304 /* TODO: Implement interrupts. */
307 static uint64_t pl110_read(void *opaque
, hwaddr offset
,
310 pl110_state
*s
= (pl110_state
*)opaque
;
312 if (offset
>= 0xfe0 && offset
< 0x1000) {
313 return idregs
[s
->version
][(offset
- 0xfe0) >> 2];
315 if (offset
>= 0x200 && offset
< 0x400) {
316 return s
->raw_palette
[(offset
- 0x200) >> 2];
318 switch (offset
>> 2) {
319 case 0: /* LCDTiming0 */
321 case 1: /* LCDTiming1 */
323 case 2: /* LCDTiming2 */
325 case 3: /* LCDTiming3 */
327 case 4: /* LCDUPBASE */
329 case 5: /* LCDLPBASE */
331 case 6: /* LCDIMSC */
332 if (s
->version
!= PL110
) {
336 case 7: /* LCDControl */
337 if (s
->version
!= PL110
) {
342 return s
->int_status
;
344 return s
->int_status
& s
->int_mask
;
345 case 11: /* LCDUPCURR */
346 /* TODO: Implement vertical refresh. */
348 case 12: /* LCDLPCURR */
351 qemu_log_mask(LOG_GUEST_ERROR
,
352 "pl110_read: Bad offset %x\n", (int)offset
);
357 static void pl110_write(void *opaque
, hwaddr 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_palette
[(offset
- 0x200) >> 2] = val
;
370 pl110_update_palette(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 qemu_log_mask(LOG_GUEST_ERROR
,
421 "pl110_write: Bad offset %x\n", (int)offset
);
425 static const MemoryRegionOps pl110_ops
= {
427 .write
= pl110_write
,
428 .endianness
= DEVICE_NATIVE_ENDIAN
,
431 static void pl110_mux_ctrl_set(void *opaque
, int line
, int level
)
433 pl110_state
*s
= (pl110_state
*)opaque
;
437 static int vmstate_pl110_post_load(void *opaque
, int version_id
)
439 pl110_state
*s
= opaque
;
440 /* Make sure we redraw, and at the right size */
441 pl110_invalidate_display(s
);
445 static int pl110_init(SysBusDevice
*dev
)
447 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
449 memory_region_init_io(&s
->iomem
, &pl110_ops
, s
, "pl110", 0x1000);
450 sysbus_init_mmio(dev
, &s
->iomem
);
451 sysbus_init_irq(dev
, &s
->irq
);
452 qdev_init_gpio_in(&s
->busdev
.qdev
, pl110_mux_ctrl_set
, 1);
453 s
->ds
= graphic_console_init(pl110_update_display
,
454 pl110_invalidate_display
,
459 static int pl110_versatile_init(SysBusDevice
*dev
)
461 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
462 s
->version
= PL110_VERSATILE
;
463 return pl110_init(dev
);
466 static int pl111_init(SysBusDevice
*dev
)
468 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
470 return pl110_init(dev
);
473 static void pl110_class_init(ObjectClass
*klass
, void *data
)
475 DeviceClass
*dc
= DEVICE_CLASS(klass
);
476 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
478 k
->init
= pl110_init
;
480 dc
->vmsd
= &vmstate_pl110
;
483 static const TypeInfo pl110_info
= {
485 .parent
= TYPE_SYS_BUS_DEVICE
,
486 .instance_size
= sizeof(pl110_state
),
487 .class_init
= pl110_class_init
,
490 static void pl110_versatile_class_init(ObjectClass
*klass
, void *data
)
492 DeviceClass
*dc
= DEVICE_CLASS(klass
);
493 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
495 k
->init
= pl110_versatile_init
;
497 dc
->vmsd
= &vmstate_pl110
;
500 static const TypeInfo pl110_versatile_info
= {
501 .name
= "pl110_versatile",
502 .parent
= TYPE_SYS_BUS_DEVICE
,
503 .instance_size
= sizeof(pl110_state
),
504 .class_init
= pl110_versatile_class_init
,
507 static void pl111_class_init(ObjectClass
*klass
, void *data
)
509 DeviceClass
*dc
= DEVICE_CLASS(klass
);
510 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
512 k
->init
= pl111_init
;
514 dc
->vmsd
= &vmstate_pl110
;
517 static const TypeInfo pl111_info
= {
519 .parent
= TYPE_SYS_BUS_DEVICE
,
520 .instance_size
= sizeof(pl110_state
),
521 .class_init
= pl111_class_init
,
524 static void pl110_register_types(void)
526 type_register_static(&pl110_info
);
527 type_register_static(&pl110_versatile_info
);
528 type_register_static(&pl111_info
);
531 type_init(pl110_register_types
)