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 "chardev/char-fe.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
);
90 qemu_chr_fe_accept_input(&s
->chr
);
99 case 8: /* UARTILPR */
102 case 9: /* UARTIBRD */
105 case 10: /* UARTFBRD */
108 case 11: /* UARTLCR_H */
111 case 12: /* UARTCR */
114 case 13: /* UARTIFLS */
117 case 14: /* UARTIMSC */
120 case 15: /* UARTRIS */
123 case 16: /* UARTMIS */
124 r
= s
->int_level
& s
->int_enabled
;
126 case 18: /* UARTDMACR */
129 case 0x3f8 ... 0x400:
130 r
= s
->id
[(offset
- 0xfe0) >> 2];
133 qemu_log_mask(LOG_GUEST_ERROR
,
134 "pl011_read: Bad offset %x\n", (int)offset
);
139 trace_pl011_read(offset
, r
);
143 static void pl011_set_read_trigger(PL011State
*s
)
146 /* The docs say the RX interrupt is triggered when the FIFO exceeds
147 the threshold. However linux only reads the FIFO in response to an
148 interrupt. Triggering the interrupt when the FIFO is non-empty seems
149 to make things work. */
151 s
->read_trigger
= (s
->ifl
>> 1) & 0x1c;
157 static void pl011_write(void *opaque
, hwaddr offset
,
158 uint64_t value
, unsigned size
)
160 PL011State
*s
= (PL011State
*)opaque
;
163 trace_pl011_write(offset
, value
);
165 switch (offset
>> 2) {
167 /* ??? Check if transmitter is enabled. */
169 /* XXX this blocks entire thread. Rewrite to use
170 * qemu_chr_fe_write and background I/O callbacks */
171 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
172 s
->int_level
|= PL011_INT_TX
;
175 case 1: /* UARTRSR/UARTECR */
179 /* Writes to Flag register are ignored. */
181 case 8: /* UARTUARTILPR */
184 case 9: /* UARTIBRD */
187 case 10: /* UARTFBRD */
190 case 11: /* UARTLCR_H */
191 /* Reset the FIFO state on FIFO enable or disable */
192 if ((s
->lcr
^ value
) & 0x10) {
197 pl011_set_read_trigger(s
);
199 case 12: /* UARTCR */
200 /* ??? Need to implement the enable and loopback bits. */
203 case 13: /* UARTIFS */
205 pl011_set_read_trigger(s
);
207 case 14: /* UARTIMSC */
208 s
->int_enabled
= value
;
211 case 17: /* UARTICR */
212 s
->int_level
&= ~value
;
215 case 18: /* UARTDMACR */
218 qemu_log_mask(LOG_UNIMP
, "pl011: DMA not implemented\n");
222 qemu_log_mask(LOG_GUEST_ERROR
,
223 "pl011_write: Bad offset %x\n", (int)offset
);
227 static int pl011_can_receive(void *opaque
)
229 PL011State
*s
= (PL011State
*)opaque
;
233 r
= s
->read_count
< 16;
235 r
= s
->read_count
< 1;
237 trace_pl011_can_receive(s
->lcr
, s
->read_count
, r
);
241 static void pl011_put_fifo(void *opaque
, uint32_t value
)
243 PL011State
*s
= (PL011State
*)opaque
;
246 slot
= s
->read_pos
+ s
->read_count
;
249 s
->read_fifo
[slot
] = value
;
251 s
->flags
&= ~PL011_FLAG_RXFE
;
252 trace_pl011_put_fifo(value
, s
->read_count
);
253 if (!(s
->lcr
& 0x10) || s
->read_count
== 16) {
254 trace_pl011_put_fifo_full();
255 s
->flags
|= PL011_FLAG_RXFF
;
257 if (s
->read_count
== s
->read_trigger
) {
258 s
->int_level
|= PL011_INT_RX
;
263 static void pl011_receive(void *opaque
, const uint8_t *buf
, int size
)
265 pl011_put_fifo(opaque
, *buf
);
268 static void pl011_event(void *opaque
, int event
)
270 if (event
== CHR_EVENT_BREAK
)
271 pl011_put_fifo(opaque
, 0x400);
274 static const MemoryRegionOps pl011_ops
= {
276 .write
= pl011_write
,
277 .endianness
= DEVICE_NATIVE_ENDIAN
,
280 static const VMStateDescription vmstate_pl011
= {
283 .minimum_version_id
= 2,
284 .fields
= (VMStateField
[]) {
285 VMSTATE_UINT32(readbuff
, PL011State
),
286 VMSTATE_UINT32(flags
, PL011State
),
287 VMSTATE_UINT32(lcr
, PL011State
),
288 VMSTATE_UINT32(rsr
, PL011State
),
289 VMSTATE_UINT32(cr
, PL011State
),
290 VMSTATE_UINT32(dmacr
, PL011State
),
291 VMSTATE_UINT32(int_enabled
, PL011State
),
292 VMSTATE_UINT32(int_level
, PL011State
),
293 VMSTATE_UINT32_ARRAY(read_fifo
, PL011State
, 16),
294 VMSTATE_UINT32(ilpr
, PL011State
),
295 VMSTATE_UINT32(ibrd
, PL011State
),
296 VMSTATE_UINT32(fbrd
, PL011State
),
297 VMSTATE_UINT32(ifl
, PL011State
),
298 VMSTATE_INT32(read_pos
, PL011State
),
299 VMSTATE_INT32(read_count
, PL011State
),
300 VMSTATE_INT32(read_trigger
, PL011State
),
301 VMSTATE_END_OF_LIST()
305 static Property pl011_properties
[] = {
306 DEFINE_PROP_CHR("chardev", PL011State
, chr
),
307 DEFINE_PROP_END_OF_LIST(),
310 static void pl011_init(Object
*obj
)
312 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
313 PL011State
*s
= PL011(obj
);
315 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl011_ops
, s
, "pl011", 0x1000);
316 sysbus_init_mmio(sbd
, &s
->iomem
);
317 sysbus_init_irq(sbd
, &s
->irq
);
324 s
->id
= pl011_id_arm
;
327 static void pl011_realize(DeviceState
*dev
, Error
**errp
)
329 PL011State
*s
= PL011(dev
);
331 qemu_chr_fe_set_handlers(&s
->chr
, pl011_can_receive
, pl011_receive
,
332 pl011_event
, s
, NULL
, true);
335 static void pl011_class_init(ObjectClass
*oc
, void *data
)
337 DeviceClass
*dc
= DEVICE_CLASS(oc
);
339 dc
->realize
= pl011_realize
;
340 dc
->vmsd
= &vmstate_pl011
;
341 dc
->props
= pl011_properties
;
344 static const TypeInfo pl011_arm_info
= {
346 .parent
= TYPE_SYS_BUS_DEVICE
,
347 .instance_size
= sizeof(PL011State
),
348 .instance_init
= pl011_init
,
349 .class_init
= pl011_class_init
,
352 static void pl011_luminary_init(Object
*obj
)
354 PL011State
*s
= PL011(obj
);
356 s
->id
= pl011_id_luminary
;
359 static const TypeInfo pl011_luminary_info
= {
360 .name
= "pl011_luminary",
361 .parent
= TYPE_PL011
,
362 .instance_init
= pl011_luminary_init
,
365 static void pl011_register_types(void)
367 type_register_static(&pl011_arm_info
);
368 type_register_static(&pl011_luminary_info
);
371 type_init(pl011_register_types
)