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
10 #include "hw/sysbus.h"
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
;
132 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
141 if (!pl110_enabled(s
))
144 switch (surface_bits_per_pixel(surface
)) {
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(surface
, sysbus_address_space(&s
->busdev
),
236 s
->upbase
, s
->cols
, s
->rows
,
237 src_width
, dest_width
, 0,
242 dpy_gfx_update(s
->con
, 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
->con
, s
->cols
, s
->rows
);
256 static void pl110_update_palette(pl110_state
*s
, int n
)
258 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
261 unsigned int r
, g
, b
;
263 raw
= s
->raw_palette
[n
];
265 for (i
= 0; i
< 2; i
++) {
266 r
= (raw
& 0x1f) << 3;
268 g
= (raw
& 0x1f) << 3;
270 b
= (raw
& 0x1f) << 3;
271 /* The I bit is ignored. */
273 switch (surface_bits_per_pixel(surface
)) {
275 s
->palette
[n
] = rgb_to_pixel8(r
, g
, b
);
278 s
->palette
[n
] = rgb_to_pixel15(r
, g
, b
);
281 s
->palette
[n
] = rgb_to_pixel16(r
, g
, b
);
285 s
->palette
[n
] = rgb_to_pixel32(r
, g
, b
);
292 static void pl110_resize(pl110_state
*s
, int width
, int height
)
294 if (width
!= s
->cols
|| height
!= s
->rows
) {
295 if (pl110_enabled(s
)) {
296 qemu_console_resize(s
->con
, width
, height
);
303 /* Update interrupts. */
304 static void pl110_update(pl110_state
*s
)
306 /* TODO: Implement interrupts. */
309 static uint64_t pl110_read(void *opaque
, hwaddr offset
,
312 pl110_state
*s
= (pl110_state
*)opaque
;
314 if (offset
>= 0xfe0 && offset
< 0x1000) {
315 return idregs
[s
->version
][(offset
- 0xfe0) >> 2];
317 if (offset
>= 0x200 && offset
< 0x400) {
318 return s
->raw_palette
[(offset
- 0x200) >> 2];
320 switch (offset
>> 2) {
321 case 0: /* LCDTiming0 */
323 case 1: /* LCDTiming1 */
325 case 2: /* LCDTiming2 */
327 case 3: /* LCDTiming3 */
329 case 4: /* LCDUPBASE */
331 case 5: /* LCDLPBASE */
333 case 6: /* LCDIMSC */
334 if (s
->version
!= PL110
) {
338 case 7: /* LCDControl */
339 if (s
->version
!= PL110
) {
344 return s
->int_status
;
346 return s
->int_status
& s
->int_mask
;
347 case 11: /* LCDUPCURR */
348 /* TODO: Implement vertical refresh. */
350 case 12: /* LCDLPCURR */
353 qemu_log_mask(LOG_GUEST_ERROR
,
354 "pl110_read: Bad offset %x\n", (int)offset
);
359 static void pl110_write(void *opaque
, hwaddr offset
,
360 uint64_t val
, unsigned size
)
362 pl110_state
*s
= (pl110_state
*)opaque
;
365 /* For simplicity invalidate the display whenever a control register
368 if (offset
>= 0x200 && offset
< 0x400) {
370 n
= (offset
- 0x200) >> 2;
371 s
->raw_palette
[(offset
- 0x200) >> 2] = val
;
372 pl110_update_palette(s
, n
);
375 switch (offset
>> 2) {
376 case 0: /* LCDTiming0 */
378 n
= ((val
& 0xfc) + 4) * 4;
379 pl110_resize(s
, n
, s
->rows
);
381 case 1: /* LCDTiming1 */
383 n
= (val
& 0x3ff) + 1;
384 pl110_resize(s
, s
->cols
, n
);
386 case 2: /* LCDTiming2 */
389 case 3: /* LCDTiming3 */
392 case 4: /* LCDUPBASE */
395 case 5: /* LCDLPBASE */
398 case 6: /* LCDIMSC */
399 if (s
->version
!= PL110
) {
406 case 7: /* LCDControl */
407 if (s
->version
!= PL110
) {
412 s
->bpp
= (val
>> 1) & 7;
413 if (pl110_enabled(s
)) {
414 qemu_console_resize(s
->con
, s
->cols
, s
->rows
);
417 case 10: /* LCDICR */
418 s
->int_status
&= ~val
;
422 qemu_log_mask(LOG_GUEST_ERROR
,
423 "pl110_write: Bad offset %x\n", (int)offset
);
427 static const MemoryRegionOps pl110_ops
= {
429 .write
= pl110_write
,
430 .endianness
= DEVICE_NATIVE_ENDIAN
,
433 static void pl110_mux_ctrl_set(void *opaque
, int line
, int level
)
435 pl110_state
*s
= (pl110_state
*)opaque
;
439 static int vmstate_pl110_post_load(void *opaque
, int version_id
)
441 pl110_state
*s
= opaque
;
442 /* Make sure we redraw, and at the right size */
443 pl110_invalidate_display(s
);
447 static const GraphicHwOps pl110_gfx_ops
= {
448 .invalidate
= pl110_invalidate_display
,
449 .gfx_update
= pl110_update_display
,
452 static int pl110_init(SysBusDevice
*dev
)
454 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
456 memory_region_init_io(&s
->iomem
, &pl110_ops
, s
, "pl110", 0x1000);
457 sysbus_init_mmio(dev
, &s
->iomem
);
458 sysbus_init_irq(dev
, &s
->irq
);
459 qdev_init_gpio_in(&s
->busdev
.qdev
, pl110_mux_ctrl_set
, 1);
460 s
->con
= graphic_console_init(DEVICE(dev
), &pl110_gfx_ops
, s
);
464 static int pl110_versatile_init(SysBusDevice
*dev
)
466 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
467 s
->version
= PL110_VERSATILE
;
468 return pl110_init(dev
);
471 static int pl111_init(SysBusDevice
*dev
)
473 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
475 return pl110_init(dev
);
478 static void pl110_class_init(ObjectClass
*klass
, void *data
)
480 DeviceClass
*dc
= DEVICE_CLASS(klass
);
481 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
483 k
->init
= pl110_init
;
485 dc
->vmsd
= &vmstate_pl110
;
488 static const TypeInfo pl110_info
= {
490 .parent
= TYPE_SYS_BUS_DEVICE
,
491 .instance_size
= sizeof(pl110_state
),
492 .class_init
= pl110_class_init
,
495 static void pl110_versatile_class_init(ObjectClass
*klass
, void *data
)
497 DeviceClass
*dc
= DEVICE_CLASS(klass
);
498 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
500 k
->init
= pl110_versatile_init
;
502 dc
->vmsd
= &vmstate_pl110
;
505 static const TypeInfo pl110_versatile_info
= {
506 .name
= "pl110_versatile",
507 .parent
= TYPE_SYS_BUS_DEVICE
,
508 .instance_size
= sizeof(pl110_state
),
509 .class_init
= pl110_versatile_class_init
,
512 static void pl111_class_init(ObjectClass
*klass
, void *data
)
514 DeviceClass
*dc
= DEVICE_CLASS(klass
);
515 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
517 k
->init
= pl111_init
;
519 dc
->vmsd
= &vmstate_pl110
;
522 static const TypeInfo pl111_info
= {
524 .parent
= TYPE_SYS_BUS_DEVICE
,
525 .instance_size
= sizeof(pl110_state
),
526 .class_init
= pl111_class_init
,
529 static void pl110_register_types(void)
531 type_register_static(&pl110_info
);
532 type_register_static(&pl110_versatile_info
);
533 type_register_static(&pl111_info
);
536 type_init(pl110_register_types
)