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 pallette
[256];
59 uint32_t raw_pallette
[128];
63 static const VMStateDescription vmstate_pl110
= {
66 .minimum_version_id
= 1,
67 .fields
= (VMStateField
[]) {
68 VMSTATE_INT32(version
, pl110_state
),
69 VMSTATE_UINT32_ARRAY(timing
, pl110_state
, 4),
70 VMSTATE_UINT32(cr
, pl110_state
),
71 VMSTATE_UINT32(upbase
, pl110_state
),
72 VMSTATE_UINT32(lpbase
, pl110_state
),
73 VMSTATE_UINT32(int_status
, pl110_state
),
74 VMSTATE_UINT32(int_mask
, pl110_state
),
75 VMSTATE_INT32(cols
, pl110_state
),
76 VMSTATE_INT32(rows
, pl110_state
),
77 VMSTATE_UINT32(bpp
, pl110_state
),
78 VMSTATE_INT32(invalidate
, pl110_state
),
79 VMSTATE_UINT32_ARRAY(pallette
, pl110_state
, 256),
80 VMSTATE_UINT32_ARRAY(raw_pallette
, pl110_state
, 128),
81 VMSTATE_UINT32_V(mux_ctrl
, pl110_state
, 2),
86 static const unsigned char pl110_id
[] =
87 { 0x10, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
89 /* The Arm documentation (DDI0224C) says the CLDC on the Versatile board
90 has a different ID. However Linux only looks for the normal ID. */
92 static const unsigned char pl110_versatile_id
[] =
93 { 0x93, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
95 #define pl110_versatile_id pl110_id
98 static const unsigned char pl111_id
[] = {
99 0x11, 0x11, 0x24, 0x00, 0x0d, 0xf0, 0x05, 0xb1
102 /* Indexed by pl110_version */
103 static const unsigned char *idregs
[] = {
109 #include "pixel_ops.h"
112 #include "pl110_template.h"
114 #include "pl110_template.h"
116 #include "pl110_template.h"
118 #include "pl110_template.h"
120 #include "pl110_template.h"
122 static int pl110_enabled(pl110_state
*s
)
124 return (s
->cr
& PL110_CR_EN
) && (s
->cr
& PL110_CR_PWR
);
127 static void pl110_update_display(void *opaque
)
129 pl110_state
*s
= (pl110_state
*)opaque
;
138 if (!pl110_enabled(s
))
141 switch (ds_get_bits_per_pixel(s
->ds
)) {
145 fntable
= pl110_draw_fn_8
;
149 fntable
= pl110_draw_fn_15
;
153 fntable
= pl110_draw_fn_16
;
157 fntable
= pl110_draw_fn_24
;
161 fntable
= pl110_draw_fn_32
;
165 fprintf(stderr
, "pl110: Bad color depth\n");
168 if (s
->cr
& PL110_CR_BGR
)
173 if ((s
->version
!= PL111
) && (s
->bpp
== BPP_16
)) {
174 /* The PL110's native 16 bit mode is 5551; however
175 * most boards with a PL110 implement an external
176 * mux which allows bits to be reshuffled to give
177 * 565 format. The mux is typically controlled by
178 * an external system register.
179 * This is controlled by a GPIO input pin
180 * so boards can wire it up to their register.
182 * The PL111 straightforwardly implements both
183 * 5551 and 565 under control of the bpp field
184 * in the LCDControl register.
186 switch (s
->mux_ctrl
) {
187 case 3: /* 565 BGR */
188 bpp_offset
= (BPP_16_565
- BPP_16
);
192 case 0: /* 888; also if we have loaded vmstate from an old version */
193 case 2: /* 565 RGB */
195 /* treat as 565 but honour BGR bit */
196 bpp_offset
+= (BPP_16_565
- BPP_16
);
201 if (s
->cr
& PL110_CR_BEBO
)
202 fn
= fntable
[s
->bpp
+ 8 + bpp_offset
];
203 else if (s
->cr
& PL110_CR_BEPO
)
204 fn
= fntable
[s
->bpp
+ 16 + bpp_offset
];
206 fn
= fntable
[s
->bpp
+ bpp_offset
];
230 dest_width
*= s
->cols
;
232 framebuffer_update_display(s
->ds
,
233 s
->upbase
, s
->cols
, s
->rows
,
234 src_width
, dest_width
, 0,
239 dpy_update(s
->ds
, 0, first
, s
->cols
, last
- first
+ 1);
244 static void pl110_invalidate_display(void * opaque
)
246 pl110_state
*s
= (pl110_state
*)opaque
;
248 if (pl110_enabled(s
)) {
249 qemu_console_resize(s
->ds
, s
->cols
, s
->rows
);
253 static void pl110_update_pallette(pl110_state
*s
, int n
)
257 unsigned int r
, g
, b
;
259 raw
= s
->raw_pallette
[n
];
261 for (i
= 0; i
< 2; i
++) {
262 r
= (raw
& 0x1f) << 3;
264 g
= (raw
& 0x1f) << 3;
266 b
= (raw
& 0x1f) << 3;
267 /* The I bit is ignored. */
269 switch (ds_get_bits_per_pixel(s
->ds
)) {
271 s
->pallette
[n
] = rgb_to_pixel8(r
, g
, b
);
274 s
->pallette
[n
] = rgb_to_pixel15(r
, g
, b
);
277 s
->pallette
[n
] = rgb_to_pixel16(r
, g
, b
);
281 s
->pallette
[n
] = rgb_to_pixel32(r
, g
, b
);
288 static void pl110_resize(pl110_state
*s
, int width
, int height
)
290 if (width
!= s
->cols
|| height
!= s
->rows
) {
291 if (pl110_enabled(s
)) {
292 qemu_console_resize(s
->ds
, width
, height
);
299 /* Update interrupts. */
300 static void pl110_update(pl110_state
*s
)
302 /* TODO: Implement interrupts. */
305 static uint64_t pl110_read(void *opaque
, target_phys_addr_t offset
,
308 pl110_state
*s
= (pl110_state
*)opaque
;
310 if (offset
>= 0xfe0 && offset
< 0x1000) {
311 return idregs
[s
->version
][(offset
- 0xfe0) >> 2];
313 if (offset
>= 0x200 && offset
< 0x400) {
314 return s
->raw_pallette
[(offset
- 0x200) >> 2];
316 switch (offset
>> 2) {
317 case 0: /* LCDTiming0 */
319 case 1: /* LCDTiming1 */
321 case 2: /* LCDTiming2 */
323 case 3: /* LCDTiming3 */
325 case 4: /* LCDUPBASE */
327 case 5: /* LCDLPBASE */
329 case 6: /* LCDIMSC */
330 if (s
->version
!= PL110
) {
334 case 7: /* LCDControl */
335 if (s
->version
!= PL110
) {
340 return s
->int_status
;
342 return s
->int_status
& s
->int_mask
;
343 case 11: /* LCDUPCURR */
344 /* TODO: Implement vertical refresh. */
346 case 12: /* LCDLPCURR */
349 hw_error("pl110_read: Bad offset %x\n", (int)offset
);
354 static void pl110_write(void *opaque
, target_phys_addr_t offset
,
355 uint64_t val
, unsigned size
)
357 pl110_state
*s
= (pl110_state
*)opaque
;
360 /* For simplicity invalidate the display whenever a control register
363 if (offset
>= 0x200 && offset
< 0x400) {
365 n
= (offset
- 0x200) >> 2;
366 s
->raw_pallette
[(offset
- 0x200) >> 2] = val
;
367 pl110_update_pallette(s
, n
);
370 switch (offset
>> 2) {
371 case 0: /* LCDTiming0 */
373 n
= ((val
& 0xfc) + 4) * 4;
374 pl110_resize(s
, n
, s
->rows
);
376 case 1: /* LCDTiming1 */
378 n
= (val
& 0x3ff) + 1;
379 pl110_resize(s
, s
->cols
, n
);
381 case 2: /* LCDTiming2 */
384 case 3: /* LCDTiming3 */
387 case 4: /* LCDUPBASE */
390 case 5: /* LCDLPBASE */
393 case 6: /* LCDIMSC */
394 if (s
->version
!= PL110
) {
401 case 7: /* LCDControl */
402 if (s
->version
!= PL110
) {
407 s
->bpp
= (val
>> 1) & 7;
408 if (pl110_enabled(s
)) {
409 qemu_console_resize(s
->ds
, s
->cols
, s
->rows
);
412 case 10: /* LCDICR */
413 s
->int_status
&= ~val
;
417 hw_error("pl110_write: Bad offset %x\n", (int)offset
);
421 static const MemoryRegionOps pl110_ops
= {
423 .write
= pl110_write
,
424 .endianness
= DEVICE_NATIVE_ENDIAN
,
427 static void pl110_mux_ctrl_set(void *opaque
, int line
, int level
)
429 pl110_state
*s
= (pl110_state
*)opaque
;
433 static int pl110_init(SysBusDevice
*dev
)
435 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
437 memory_region_init_io(&s
->iomem
, &pl110_ops
, s
, "pl110", 0x1000);
438 sysbus_init_mmio(dev
, &s
->iomem
);
439 sysbus_init_irq(dev
, &s
->irq
);
440 qdev_init_gpio_in(&s
->busdev
.qdev
, pl110_mux_ctrl_set
, 1);
441 s
->ds
= graphic_console_init(pl110_update_display
,
442 pl110_invalidate_display
,
447 static int pl110_versatile_init(SysBusDevice
*dev
)
449 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
450 s
->version
= PL110_VERSATILE
;
451 return pl110_init(dev
);
454 static int pl111_init(SysBusDevice
*dev
)
456 pl110_state
*s
= FROM_SYSBUS(pl110_state
, dev
);
458 return pl110_init(dev
);
461 static SysBusDeviceInfo pl110_info
= {
463 .qdev
.name
= "pl110",
464 .qdev
.size
= sizeof(pl110_state
),
465 .qdev
.vmsd
= &vmstate_pl110
,
469 static SysBusDeviceInfo pl110_versatile_info
= {
470 .init
= pl110_versatile_init
,
471 .qdev
.name
= "pl110_versatile",
472 .qdev
.size
= sizeof(pl110_state
),
473 .qdev
.vmsd
= &vmstate_pl110
,
477 static SysBusDeviceInfo pl111_info
= {
479 .qdev
.name
= "pl111",
480 .qdev
.size
= sizeof(pl110_state
),
481 .qdev
.vmsd
= &vmstate_pl110
,
485 static void pl110_register_devices(void)
487 sysbus_register_withprop(&pl110_info
);
488 sysbus_register_withprop(&pl110_versatile_info
);
489 sysbus_register_withprop(&pl111_info
);
492 device_init(pl110_register_devices
)