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 "hw/sysbus.h"
11 #include "sysemu/char.h"
13 #define TYPE_PL011 "pl011"
14 #define PL011(obj) OBJECT_CHECK(PL011State, (obj), TYPE_PL011)
16 typedef struct PL011State
{
17 SysBusDevice parent_obj
;
28 uint32_t read_fifo
[16];
38 const unsigned char *id
;
41 #define PL011_INT_TX 0x20
42 #define PL011_INT_RX 0x10
44 #define PL011_FLAG_TXFE 0x80
45 #define PL011_FLAG_RXFF 0x40
46 #define PL011_FLAG_TXFF 0x20
47 #define PL011_FLAG_RXFE 0x10
49 static const unsigned char pl011_id_arm
[8] =
50 { 0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
51 static const unsigned char pl011_id_luminary
[8] =
52 { 0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 };
54 static void pl011_update(PL011State
*s
)
58 flags
= s
->int_level
& s
->int_enabled
;
59 qemu_set_irq(s
->irq
, flags
!= 0);
62 static uint64_t pl011_read(void *opaque
, hwaddr offset
,
65 PL011State
*s
= (PL011State
*)opaque
;
68 if (offset
>= 0xfe0 && offset
< 0x1000) {
69 return s
->id
[(offset
- 0xfe0) >> 2];
71 switch (offset
>> 2) {
73 s
->flags
&= ~PL011_FLAG_RXFF
;
74 c
= s
->read_fifo
[s
->read_pos
];
75 if (s
->read_count
> 0) {
77 if (++s
->read_pos
== 16)
80 if (s
->read_count
== 0) {
81 s
->flags
|= PL011_FLAG_RXFE
;
83 if (s
->read_count
== s
->read_trigger
- 1)
84 s
->int_level
&= ~ PL011_INT_RX
;
88 qemu_chr_accept_input(s
->chr
);
95 case 8: /* UARTILPR */
97 case 9: /* UARTIBRD */
99 case 10: /* UARTFBRD */
101 case 11: /* UARTLCR_H */
103 case 12: /* UARTCR */
105 case 13: /* UARTIFLS */
107 case 14: /* UARTIMSC */
108 return s
->int_enabled
;
109 case 15: /* UARTRIS */
111 case 16: /* UARTMIS */
112 return s
->int_level
& s
->int_enabled
;
113 case 18: /* UARTDMACR */
116 qemu_log_mask(LOG_GUEST_ERROR
,
117 "pl011_read: Bad offset %x\n", (int)offset
);
122 static void pl011_set_read_trigger(PL011State
*s
)
125 /* The docs say the RX interrupt is triggered when the FIFO exceeds
126 the threshold. However linux only reads the FIFO in response to an
127 interrupt. Triggering the interrupt when the FIFO is non-empty seems
128 to make things work. */
130 s
->read_trigger
= (s
->ifl
>> 1) & 0x1c;
136 static void pl011_write(void *opaque
, hwaddr offset
,
137 uint64_t value
, unsigned size
)
139 PL011State
*s
= (PL011State
*)opaque
;
142 switch (offset
>> 2) {
144 /* ??? Check if transmitter is enabled. */
147 qemu_chr_fe_write(s
->chr
, &ch
, 1);
148 s
->int_level
|= PL011_INT_TX
;
151 case 1: /* UARTRSR/UARTECR */
155 /* Writes to Flag register are ignored. */
157 case 8: /* UARTUARTILPR */
160 case 9: /* UARTIBRD */
163 case 10: /* UARTFBRD */
166 case 11: /* UARTLCR_H */
167 /* Reset the FIFO state on FIFO enable or disable */
168 if ((s
->lcr
^ value
) & 0x10) {
173 pl011_set_read_trigger(s
);
175 case 12: /* UARTCR */
176 /* ??? Need to implement the enable and loopback bits. */
179 case 13: /* UARTIFS */
181 pl011_set_read_trigger(s
);
183 case 14: /* UARTIMSC */
184 s
->int_enabled
= value
;
187 case 17: /* UARTICR */
188 s
->int_level
&= ~value
;
191 case 18: /* UARTDMACR */
194 qemu_log_mask(LOG_UNIMP
, "pl011: DMA not implemented\n");
198 qemu_log_mask(LOG_GUEST_ERROR
,
199 "pl011_write: Bad offset %x\n", (int)offset
);
203 static int pl011_can_receive(void *opaque
)
205 PL011State
*s
= (PL011State
*)opaque
;
208 return s
->read_count
< 16;
210 return s
->read_count
< 1;
213 static void pl011_put_fifo(void *opaque
, uint32_t value
)
215 PL011State
*s
= (PL011State
*)opaque
;
218 slot
= s
->read_pos
+ s
->read_count
;
221 s
->read_fifo
[slot
] = value
;
223 s
->flags
&= ~PL011_FLAG_RXFE
;
224 if (!(s
->lcr
& 0x10) || s
->read_count
== 16) {
225 s
->flags
|= PL011_FLAG_RXFF
;
227 if (s
->read_count
== s
->read_trigger
) {
228 s
->int_level
|= PL011_INT_RX
;
233 static void pl011_receive(void *opaque
, const uint8_t *buf
, int size
)
235 pl011_put_fifo(opaque
, *buf
);
238 static void pl011_event(void *opaque
, int event
)
240 if (event
== CHR_EVENT_BREAK
)
241 pl011_put_fifo(opaque
, 0x400);
244 static const MemoryRegionOps pl011_ops
= {
246 .write
= pl011_write
,
247 .endianness
= DEVICE_NATIVE_ENDIAN
,
250 static const VMStateDescription vmstate_pl011
= {
253 .minimum_version_id
= 2,
254 .fields
= (VMStateField
[]) {
255 VMSTATE_UINT32(readbuff
, PL011State
),
256 VMSTATE_UINT32(flags
, PL011State
),
257 VMSTATE_UINT32(lcr
, PL011State
),
258 VMSTATE_UINT32(rsr
, PL011State
),
259 VMSTATE_UINT32(cr
, PL011State
),
260 VMSTATE_UINT32(dmacr
, PL011State
),
261 VMSTATE_UINT32(int_enabled
, PL011State
),
262 VMSTATE_UINT32(int_level
, PL011State
),
263 VMSTATE_UINT32_ARRAY(read_fifo
, PL011State
, 16),
264 VMSTATE_UINT32(ilpr
, PL011State
),
265 VMSTATE_UINT32(ibrd
, PL011State
),
266 VMSTATE_UINT32(fbrd
, PL011State
),
267 VMSTATE_UINT32(ifl
, PL011State
),
268 VMSTATE_INT32(read_pos
, PL011State
),
269 VMSTATE_INT32(read_count
, PL011State
),
270 VMSTATE_INT32(read_trigger
, PL011State
),
271 VMSTATE_END_OF_LIST()
275 static void pl011_init(Object
*obj
)
277 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
278 PL011State
*s
= PL011(obj
);
280 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl011_ops
, s
, "pl011", 0x1000);
281 sysbus_init_mmio(sbd
, &s
->iomem
);
282 sysbus_init_irq(sbd
, &s
->irq
);
289 s
->id
= pl011_id_arm
;
292 static void pl011_realize(DeviceState
*dev
, Error
**errp
)
294 PL011State
*s
= PL011(dev
);
296 s
->chr
= qemu_char_get_next_serial();
299 qemu_chr_add_handlers(s
->chr
, pl011_can_receive
, pl011_receive
,
304 static void pl011_class_init(ObjectClass
*oc
, void *data
)
306 DeviceClass
*dc
= DEVICE_CLASS(oc
);
308 dc
->realize
= pl011_realize
;
309 dc
->vmsd
= &vmstate_pl011
;
312 static const TypeInfo pl011_arm_info
= {
314 .parent
= TYPE_SYS_BUS_DEVICE
,
315 .instance_size
= sizeof(PL011State
),
316 .instance_init
= pl011_init
,
317 .class_init
= pl011_class_init
,
320 static void pl011_luminary_init(Object
*obj
)
322 PL011State
*s
= PL011(obj
);
324 s
->id
= pl011_id_luminary
;
327 static const TypeInfo pl011_luminary_info
= {
328 .name
= "pl011_luminary",
329 .parent
= TYPE_PL011
,
330 .instance_init
= pl011_luminary_init
,
333 static void pl011_register_types(void)
335 type_register_static(&pl011_arm_info
);
336 type_register_static(&pl011_luminary_info
);
339 type_init(pl011_register_types
)