2 * Arm PrimeCell PL011 UART
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 #include "qemu/osdep.h"
11 #include "hw/sysbus.h"
12 #include "sysemu/char.h"
16 #define TYPE_PL011 "pl011"
17 #define PL011(obj) OBJECT_CHECK(PL011State, (obj), TYPE_PL011)
19 typedef struct PL011State
{
20 SysBusDevice parent_obj
;
31 uint32_t read_fifo
[16];
41 const unsigned char *id
;
44 #define PL011_INT_TX 0x20
45 #define PL011_INT_RX 0x10
47 #define PL011_FLAG_TXFE 0x80
48 #define PL011_FLAG_RXFF 0x40
49 #define PL011_FLAG_TXFF 0x20
50 #define PL011_FLAG_RXFE 0x10
52 static const unsigned char pl011_id_arm
[8] =
53 { 0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
54 static const unsigned char pl011_id_luminary
[8] =
55 { 0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 };
57 static void pl011_update(PL011State
*s
)
61 flags
= s
->int_level
& s
->int_enabled
;
62 trace_pl011_irq_state(flags
!= 0);
63 qemu_set_irq(s
->irq
, flags
!= 0);
66 static uint64_t pl011_read(void *opaque
, hwaddr offset
,
69 PL011State
*s
= (PL011State
*)opaque
;
73 switch (offset
>> 2) {
75 s
->flags
&= ~PL011_FLAG_RXFF
;
76 c
= s
->read_fifo
[s
->read_pos
];
77 if (s
->read_count
> 0) {
79 if (++s
->read_pos
== 16)
82 if (s
->read_count
== 0) {
83 s
->flags
|= PL011_FLAG_RXFE
;
85 if (s
->read_count
== s
->read_trigger
- 1)
86 s
->int_level
&= ~ PL011_INT_RX
;
87 trace_pl011_read_fifo(s
->read_count
);
91 qemu_chr_accept_input(s
->chr
);
101 case 8: /* UARTILPR */
104 case 9: /* UARTIBRD */
107 case 10: /* UARTFBRD */
110 case 11: /* UARTLCR_H */
113 case 12: /* UARTCR */
116 case 13: /* UARTIFLS */
119 case 14: /* UARTIMSC */
122 case 15: /* UARTRIS */
125 case 16: /* UARTMIS */
126 r
= s
->int_level
& s
->int_enabled
;
128 case 18: /* UARTDMACR */
131 case 0x3f8 ... 0x400:
132 r
= s
->id
[(offset
- 0xfe0) >> 2];
135 qemu_log_mask(LOG_GUEST_ERROR
,
136 "pl011_read: Bad offset %x\n", (int)offset
);
141 trace_pl011_read(offset
, r
);
145 static void pl011_set_read_trigger(PL011State
*s
)
148 /* The docs say the RX interrupt is triggered when the FIFO exceeds
149 the threshold. However linux only reads the FIFO in response to an
150 interrupt. Triggering the interrupt when the FIFO is non-empty seems
151 to make things work. */
153 s
->read_trigger
= (s
->ifl
>> 1) & 0x1c;
159 static void pl011_write(void *opaque
, hwaddr offset
,
160 uint64_t value
, unsigned size
)
162 PL011State
*s
= (PL011State
*)opaque
;
165 trace_pl011_write(offset
, value
);
167 switch (offset
>> 2) {
169 /* ??? Check if transmitter is enabled. */
172 /* XXX this blocks entire thread. Rewrite to use
173 * qemu_chr_fe_write and background I/O callbacks */
174 qemu_chr_fe_write_all(s
->chr
, &ch
, 1);
175 s
->int_level
|= PL011_INT_TX
;
178 case 1: /* UARTRSR/UARTECR */
182 /* Writes to Flag register are ignored. */
184 case 8: /* UARTUARTILPR */
187 case 9: /* UARTIBRD */
190 case 10: /* UARTFBRD */
193 case 11: /* UARTLCR_H */
194 /* Reset the FIFO state on FIFO enable or disable */
195 if ((s
->lcr
^ value
) & 0x10) {
200 pl011_set_read_trigger(s
);
202 case 12: /* UARTCR */
203 /* ??? Need to implement the enable and loopback bits. */
206 case 13: /* UARTIFS */
208 pl011_set_read_trigger(s
);
210 case 14: /* UARTIMSC */
211 s
->int_enabled
= value
;
214 case 17: /* UARTICR */
215 s
->int_level
&= ~value
;
218 case 18: /* UARTDMACR */
221 qemu_log_mask(LOG_UNIMP
, "pl011: DMA not implemented\n");
225 qemu_log_mask(LOG_GUEST_ERROR
,
226 "pl011_write: Bad offset %x\n", (int)offset
);
230 static int pl011_can_receive(void *opaque
)
232 PL011State
*s
= (PL011State
*)opaque
;
236 r
= s
->read_count
< 16;
238 r
= s
->read_count
< 1;
240 trace_pl011_can_receive(s
->lcr
, s
->read_count
, r
);
244 static void pl011_put_fifo(void *opaque
, uint32_t value
)
246 PL011State
*s
= (PL011State
*)opaque
;
249 slot
= s
->read_pos
+ s
->read_count
;
252 s
->read_fifo
[slot
] = value
;
254 s
->flags
&= ~PL011_FLAG_RXFE
;
255 trace_pl011_put_fifo(value
, s
->read_count
);
256 if (!(s
->lcr
& 0x10) || s
->read_count
== 16) {
257 trace_pl011_put_fifo_full();
258 s
->flags
|= PL011_FLAG_RXFF
;
260 if (s
->read_count
== s
->read_trigger
) {
261 s
->int_level
|= PL011_INT_RX
;
266 static void pl011_receive(void *opaque
, const uint8_t *buf
, int size
)
268 pl011_put_fifo(opaque
, *buf
);
271 static void pl011_event(void *opaque
, int event
)
273 if (event
== CHR_EVENT_BREAK
)
274 pl011_put_fifo(opaque
, 0x400);
277 static const MemoryRegionOps pl011_ops
= {
279 .write
= pl011_write
,
280 .endianness
= DEVICE_NATIVE_ENDIAN
,
283 static const VMStateDescription vmstate_pl011
= {
286 .minimum_version_id
= 2,
287 .fields
= (VMStateField
[]) {
288 VMSTATE_UINT32(readbuff
, PL011State
),
289 VMSTATE_UINT32(flags
, PL011State
),
290 VMSTATE_UINT32(lcr
, PL011State
),
291 VMSTATE_UINT32(rsr
, PL011State
),
292 VMSTATE_UINT32(cr
, PL011State
),
293 VMSTATE_UINT32(dmacr
, PL011State
),
294 VMSTATE_UINT32(int_enabled
, PL011State
),
295 VMSTATE_UINT32(int_level
, PL011State
),
296 VMSTATE_UINT32_ARRAY(read_fifo
, PL011State
, 16),
297 VMSTATE_UINT32(ilpr
, PL011State
),
298 VMSTATE_UINT32(ibrd
, PL011State
),
299 VMSTATE_UINT32(fbrd
, PL011State
),
300 VMSTATE_UINT32(ifl
, PL011State
),
301 VMSTATE_INT32(read_pos
, PL011State
),
302 VMSTATE_INT32(read_count
, PL011State
),
303 VMSTATE_INT32(read_trigger
, PL011State
),
304 VMSTATE_END_OF_LIST()
308 static Property pl011_properties
[] = {
309 DEFINE_PROP_CHR("chardev", PL011State
, chr
),
310 DEFINE_PROP_END_OF_LIST(),
313 static void pl011_init(Object
*obj
)
315 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
316 PL011State
*s
= PL011(obj
);
318 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl011_ops
, s
, "pl011", 0x1000);
319 sysbus_init_mmio(sbd
, &s
->iomem
);
320 sysbus_init_irq(sbd
, &s
->irq
);
327 s
->id
= pl011_id_arm
;
330 static void pl011_realize(DeviceState
*dev
, Error
**errp
)
332 PL011State
*s
= PL011(dev
);
335 qemu_chr_add_handlers(s
->chr
, pl011_can_receive
, pl011_receive
,
340 static void pl011_class_init(ObjectClass
*oc
, void *data
)
342 DeviceClass
*dc
= DEVICE_CLASS(oc
);
344 dc
->realize
= pl011_realize
;
345 dc
->vmsd
= &vmstate_pl011
;
346 dc
->props
= pl011_properties
;
349 static const TypeInfo pl011_arm_info
= {
351 .parent
= TYPE_SYS_BUS_DEVICE
,
352 .instance_size
= sizeof(PL011State
),
353 .instance_init
= pl011_init
,
354 .class_init
= pl011_class_init
,
357 static void pl011_luminary_init(Object
*obj
)
359 PL011State
*s
= PL011(obj
);
361 s
->id
= pl011_id_luminary
;
364 static const TypeInfo pl011_luminary_info
= {
365 .name
= "pl011_luminary",
366 .parent
= TYPE_PL011
,
367 .instance_init
= pl011_luminary_init
,
370 static void pl011_register_types(void)
372 type_register_static(&pl011_arm_info
);
373 type_register_static(&pl011_luminary_info
);
376 type_init(pl011_register_types
)