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
12 #include "framebuffer.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
28 BPP_16_565
, /* PL111 only */
29 BPP_12
/* PL111 only */
33 /* The Versatile/PB uses a slightly modified PL110 controller. */
55 enum pl110_bppmode bpp
;
58 uint32_t palette
[256];
59 uint32_t raw_palette
[128];
63 static int vmstate_pl110_post_load(void *opaque
, int version_id
);
65 static const VMStateDescription vmstate_pl110
= {
68 .minimum_version_id
= 1,
69 .post_load
= vmstate_pl110_post_load
,
70 .fields
= (VMStateField
[]) {
71 VMSTATE_INT32(version
, pl110_state
),
72 VMSTATE_UINT32_ARRAY(timing
, pl110_state
, 4),
73 VMSTATE_UINT32(cr
, pl110_state
),
74 VMSTATE_UINT32(upbase
, pl110_state
),
75 VMSTATE_UINT32(lpbase
, pl110_state
),
76 VMSTATE_UINT32(int_status
, pl110_state
),
77 VMSTATE_UINT32(int_mask
, pl110_state
),
78 VMSTATE_INT32(cols
, pl110_state
),
79 VMSTATE_INT32(rows
, pl110_state
),
80 VMSTATE_UINT32(bpp
, pl110_state
),
81 VMSTATE_INT32(invalidate
, pl110_state
),
82 VMSTATE_UINT32_ARRAY(palette
, pl110_state
, 256),
83 VMSTATE_UINT32_ARRAY(raw_palette
, pl110_state
, 128),
84 VMSTATE_UINT32_V(mux_ctrl
, pl110_state
, 2),
89 static const unsigned char pl110_id
[] =
90 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
92 /* The Arm documentation (DDI0224C) says the CLDC on the Versatile board
93 has a different ID. However Linux only looks for the normal ID. */
95 static const unsigned char pl110_versatile_id
[] =
96 { 0x93, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
98 #define pl110_versatile_id pl110_id
101 static const unsigned char pl111_id
[] = {
102 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
105 /* Indexed by pl110_version */
106 static const unsigned char *idregs
[] = {
112 #include "pixel_ops.h"
115 #include "pl110_template.h"
117 #include "pl110_template.h"
119 #include "pl110_template.h"
121 #include "pl110_template.h"
123 #include "pl110_template.h"
125 static int pl110_enabled(pl110_state
*s
)
127 return (s
->cr
& PL110_CR_EN
) && (s
->cr
& PL110_CR_PWR
);
130 static void pl110_update_display(void *opaque
)
132 pl110_state
*s
= (pl110_state
*)opaque
;
141 if (!pl110_enabled(s
))
144 switch (ds_get_bits_per_pixel(s
->ds
)) {
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(s
->ds
, sysbus_address_space(&s
->busdev
),
236 s
->upbase
, s
->cols
, s
->rows
,
237 src_width
, dest_width
, 0,
242 dpy_gfx_update(s
->ds
, 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
->ds
, s
->cols
, s
->rows
);
256 static void pl110_update_palette(pl110_state
*s
, int n
)
260 unsigned int r
, g
, b
;
262 raw
= s
->raw_palette
[n
];
264 for (i
= 0; i
< 2; i
++) {
265 r
= (raw
& 0x1f) << 3;
267 g
= (raw
& 0x1f) << 3;
269 b
= (raw
& 0x1f) << 3;
270 /* The I bit is ignored. */
272 switch (ds_get_bits_per_pixel(s
->ds
)) {
274 s
->palette
[n
] = rgb_to_pixel8(r
, g
, b
);
277 s
->palette
[n
] = rgb_to_pixel15(r
, g
, b
);
280 s
->palette
[n
] = rgb_to_pixel16(r
, g
, b
);
284 s
->palette
[n
] = rgb_to_pixel32(r
, g
, b
);
291 static void pl110_resize(pl110_state
*s
, int width
, int height
)
293 if (width
!= s
->cols
|| height
!= s
->rows
) {
294 if (pl110_enabled(s
)) {
295 qemu_console_resize(s
->ds
, width
, height
);
302 /* Update interrupts. */
303 static void pl110_update(pl110_state
*s
)
305 /* TODO: Implement interrupts. */
308 static uint64_t pl110_read(void *opaque
, hwaddr offset
,
311 pl110_state
*s
= (pl110_state
*)opaque
;
313 if (offset
>= 0xfe0 && offset
< 0x1000) {
314 return idregs
[s
->version
][(offset
- 0xfe0) >> 2];
316 if (offset
>= 0x200 && offset
< 0x400) {
317 return s
->raw_palette
[(offset
- 0x200) >> 2];
319 switch (offset
>> 2) {
320 case 0: /* LCDTiming0 */
322 case 1: /* LCDTiming1 */
324 case 2: /* LCDTiming2 */
326 case 3: /* LCDTiming3 */
328 case 4: /* LCDUPBASE */
330 case 5: /* LCDLPBASE */
332 case 6: /* LCDIMSC */
333 if (s
->version
!= PL110
) {
337 case 7: /* LCDControl */
338 if (s
->version
!= PL110
) {
343 return s
->int_status
;
345 return s
->int_status
& s
->int_mask
;
346 case 11: /* LCDUPCURR */
347 /* TODO: Implement vertical refresh. */
349 case 12: /* LCDLPCURR */
352 qemu_log_mask(LOG_GUEST_ERROR
,
353 "pl110_read: Bad offset %x\n", (int)offset
);
358 static void pl110_write(void *opaque
, hwaddr offset
,
359 uint64_t val
, unsigned size
)
361 pl110_state
*s
= (pl110_state
*)opaque
;
364 /* For simplicity invalidate the display whenever a control register
367 if (offset
>= 0x200 && offset
< 0x400) {
369 n
= (offset
- 0x200) >> 2;
370 s
->raw_palette
[(offset
- 0x200) >> 2] = val
;
371 pl110_update_palette(s
, n
);
374 switch (offset
>> 2) {
375 case 0: /* LCDTiming0 */
377 n
= ((val
& 0xfc) + 4) * 4;
378 pl110_resize(s
, n
, s
->rows
);
380 case 1: /* LCDTiming1 */
382 n
= (val
& 0x3ff) + 1;
383 pl110_resize(s
, s
->cols
, n
);
385 case 2: /* LCDTiming2 */
388 case 3: /* LCDTiming3 */
391 case 4: /* LCDUPBASE */
394 case 5: /* LCDLPBASE */
397 case 6: /* LCDIMSC */
398 if (s
->version
!= PL110
) {
405 case 7: /* LCDControl */
406 if (s
->version
!= PL110
) {
411 s
->bpp
= (val
>> 1) & 7;
412 if (pl110_enabled(s
)) {
413 qemu_console_resize(s
->ds
, s
->cols
, s
->rows
);
416 case 10: /* LCDICR */
417 s
->int_status
&= ~val
;
421 qemu_log_mask(LOG_GUEST_ERROR
,
422 "pl110_write: Bad offset %x\n", (int)offset
);
426 static const MemoryRegionOps pl110_ops
= {
428 .write
= pl110_write
,
429 .endianness
= DEVICE_NATIVE_ENDIAN
,
432 static void pl110_mux_ctrl_set(void *opaque
, int line
, int level
)
434 pl110_state
*s
= (pl110_state
*)opaque
;
438 static int vmstate_pl110_post_load(void *opaque
, int version_id
)
440 pl110_state
*s
= opaque
;
441 /* Make sure we redraw, and at the right size */
442 pl110_invalidate_display(s
);
446 static int pl110_init(SysBusDevice
*dev
)
448 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
450 memory_region_init_io(&s
->iomem
, &pl110_ops
, s
, "pl110", 0x1000);
451 sysbus_init_mmio(dev
, &s
->iomem
);
452 sysbus_init_irq(dev
, &s
->irq
);
453 qdev_init_gpio_in(&s
->busdev
.qdev
, pl110_mux_ctrl_set
, 1);
454 s
->ds
= graphic_console_init(pl110_update_display
,
455 pl110_invalidate_display
,
460 static int pl110_versatile_init(SysBusDevice
*dev
)
462 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
463 s
->version
= PL110_VERSATILE
;
464 return pl110_init(dev
);
467 static int pl111_init(SysBusDevice
*dev
)
469 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
471 return pl110_init(dev
);
474 static void pl110_class_init(ObjectClass
*klass
, void *data
)
476 DeviceClass
*dc
= DEVICE_CLASS(klass
);
477 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
479 k
->init
= pl110_init
;
481 dc
->vmsd
= &vmstate_pl110
;
484 static TypeInfo pl110_info
= {
486 .parent
= TYPE_SYS_BUS_DEVICE
,
487 .instance_size
= sizeof(pl110_state
),
488 .class_init
= pl110_class_init
,
491 static void pl110_versatile_class_init(ObjectClass
*klass
, void *data
)
493 DeviceClass
*dc
= DEVICE_CLASS(klass
);
494 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
496 k
->init
= pl110_versatile_init
;
498 dc
->vmsd
= &vmstate_pl110
;
501 static TypeInfo pl110_versatile_info
= {
502 .name
= "pl110_versatile",
503 .parent
= TYPE_SYS_BUS_DEVICE
,
504 .instance_size
= sizeof(pl110_state
),
505 .class_init
= pl110_versatile_class_init
,
508 static void pl111_class_init(ObjectClass
*klass
, void *data
)
510 DeviceClass
*dc
= DEVICE_CLASS(klass
);
511 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
513 k
->init
= pl111_init
;
515 dc
->vmsd
= &vmstate_pl110
;
518 static TypeInfo pl111_info
= {
520 .parent
= TYPE_SYS_BUS_DEVICE
,
521 .instance_size
= sizeof(pl110_state
),
522 .class_init
= pl111_class_init
,
525 static void pl110_register_types(void)
527 type_register_static(&pl110_info
);
528 type_register_static(&pl110_versatile_info
);
529 type_register_static(&pl111_info
);
532 type_init(pl110_register_types
)