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
;
27 uint32_t read_fifo
[16];
37 const unsigned char *id
;
40 #define PL011_INT_TX 0x20
41 #define PL011_INT_RX 0x10
43 #define PL011_FLAG_TXFE 0x80
44 #define PL011_FLAG_RXFF 0x40
45 #define PL011_FLAG_TXFF 0x20
46 #define PL011_FLAG_RXFE 0x10
48 static const unsigned char pl011_id_arm
[8] =
49 { 0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
50 static const unsigned char pl011_id_luminary
[8] =
51 { 0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 };
53 static void pl011_update(PL011State
*s
)
57 flags
= s
->int_level
& s
->int_enabled
;
58 qemu_set_irq(s
->irq
, flags
!= 0);
61 static uint64_t pl011_read(void *opaque
, hwaddr offset
,
64 PL011State
*s
= (PL011State
*)opaque
;
67 if (offset
>= 0xfe0 && offset
< 0x1000) {
68 return s
->id
[(offset
- 0xfe0) >> 2];
70 switch (offset
>> 2) {
72 s
->flags
&= ~PL011_FLAG_RXFF
;
73 c
= s
->read_fifo
[s
->read_pos
];
74 if (s
->read_count
> 0) {
76 if (++s
->read_pos
== 16)
79 if (s
->read_count
== 0) {
80 s
->flags
|= PL011_FLAG_RXFE
;
82 if (s
->read_count
== s
->read_trigger
- 1)
83 s
->int_level
&= ~ PL011_INT_RX
;
86 qemu_chr_accept_input(s
->chr
);
93 case 8: /* UARTILPR */
95 case 9: /* UARTIBRD */
97 case 10: /* UARTFBRD */
99 case 11: /* UARTLCR_H */
101 case 12: /* UARTCR */
103 case 13: /* UARTIFLS */
105 case 14: /* UARTIMSC */
106 return s
->int_enabled
;
107 case 15: /* UARTRIS */
109 case 16: /* UARTMIS */
110 return s
->int_level
& s
->int_enabled
;
111 case 18: /* UARTDMACR */
114 qemu_log_mask(LOG_GUEST_ERROR
,
115 "pl011_read: Bad offset %x\n", (int)offset
);
120 static void pl011_set_read_trigger(PL011State
*s
)
123 /* The docs say the RX interrupt is triggered when the FIFO exceeds
124 the threshold. However linux only reads the FIFO in response to an
125 interrupt. Triggering the interrupt when the FIFO is non-empty seems
126 to make things work. */
128 s
->read_trigger
= (s
->ifl
>> 1) & 0x1c;
134 static void pl011_write(void *opaque
, hwaddr offset
,
135 uint64_t value
, unsigned size
)
137 PL011State
*s
= (PL011State
*)opaque
;
140 switch (offset
>> 2) {
142 /* ??? Check if transmitter is enabled. */
145 qemu_chr_fe_write(s
->chr
, &ch
, 1);
146 s
->int_level
|= PL011_INT_TX
;
153 /* Writes to Flag register are ignored. */
155 case 8: /* UARTUARTILPR */
158 case 9: /* UARTIBRD */
161 case 10: /* UARTFBRD */
164 case 11: /* UARTLCR_H */
166 pl011_set_read_trigger(s
);
168 case 12: /* UARTCR */
169 /* ??? Need to implement the enable and loopback bits. */
172 case 13: /* UARTIFS */
174 pl011_set_read_trigger(s
);
176 case 14: /* UARTIMSC */
177 s
->int_enabled
= value
;
180 case 17: /* UARTICR */
181 s
->int_level
&= ~value
;
184 case 18: /* UARTDMACR */
187 qemu_log_mask(LOG_UNIMP
, "pl011: DMA not implemented\n");
191 qemu_log_mask(LOG_GUEST_ERROR
,
192 "pl011_write: Bad offset %x\n", (int)offset
);
196 static int pl011_can_receive(void *opaque
)
198 PL011State
*s
= (PL011State
*)opaque
;
201 return s
->read_count
< 16;
203 return s
->read_count
< 1;
206 static void pl011_put_fifo(void *opaque
, uint32_t value
)
208 PL011State
*s
= (PL011State
*)opaque
;
211 slot
= s
->read_pos
+ s
->read_count
;
214 s
->read_fifo
[slot
] = value
;
216 s
->flags
&= ~PL011_FLAG_RXFE
;
217 if (s
->cr
& 0x10 || s
->read_count
== 16) {
218 s
->flags
|= PL011_FLAG_RXFF
;
220 if (s
->read_count
== s
->read_trigger
) {
221 s
->int_level
|= PL011_INT_RX
;
226 static void pl011_receive(void *opaque
, const uint8_t *buf
, int size
)
228 pl011_put_fifo(opaque
, *buf
);
231 static void pl011_event(void *opaque
, int event
)
233 if (event
== CHR_EVENT_BREAK
)
234 pl011_put_fifo(opaque
, 0x400);
237 static const MemoryRegionOps pl011_ops
= {
239 .write
= pl011_write
,
240 .endianness
= DEVICE_NATIVE_ENDIAN
,
243 static const VMStateDescription vmstate_pl011
= {
246 .minimum_version_id
= 1,
247 .minimum_version_id_old
= 1,
248 .fields
= (VMStateField
[]) {
249 VMSTATE_UINT32(readbuff
, PL011State
),
250 VMSTATE_UINT32(flags
, PL011State
),
251 VMSTATE_UINT32(lcr
, PL011State
),
252 VMSTATE_UINT32(cr
, PL011State
),
253 VMSTATE_UINT32(dmacr
, PL011State
),
254 VMSTATE_UINT32(int_enabled
, PL011State
),
255 VMSTATE_UINT32(int_level
, PL011State
),
256 VMSTATE_UINT32_ARRAY(read_fifo
, PL011State
, 16),
257 VMSTATE_UINT32(ilpr
, PL011State
),
258 VMSTATE_UINT32(ibrd
, PL011State
),
259 VMSTATE_UINT32(fbrd
, PL011State
),
260 VMSTATE_UINT32(ifl
, PL011State
),
261 VMSTATE_INT32(read_pos
, PL011State
),
262 VMSTATE_INT32(read_count
, PL011State
),
263 VMSTATE_INT32(read_trigger
, PL011State
),
264 VMSTATE_END_OF_LIST()
268 static void pl011_init(Object
*obj
)
270 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
271 PL011State
*s
= PL011(obj
);
273 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl011_ops
, s
, "pl011", 0x1000);
274 sysbus_init_mmio(sbd
, &s
->iomem
);
275 sysbus_init_irq(sbd
, &s
->irq
);
282 s
->id
= pl011_id_arm
;
285 static void pl011_realize(DeviceState
*dev
, Error
**errp
)
287 PL011State
*s
= PL011(dev
);
289 s
->chr
= qemu_char_get_next_serial();
292 qemu_chr_add_handlers(s
->chr
, pl011_can_receive
, pl011_receive
,
297 static void pl011_class_init(ObjectClass
*oc
, void *data
)
299 DeviceClass
*dc
= DEVICE_CLASS(oc
);
301 dc
->realize
= pl011_realize
;
302 dc
->vmsd
= &vmstate_pl011
;
305 static const TypeInfo pl011_arm_info
= {
307 .parent
= TYPE_SYS_BUS_DEVICE
,
308 .instance_size
= sizeof(PL011State
),
309 .instance_init
= pl011_init
,
310 .class_init
= pl011_class_init
,
313 static void pl011_luminary_init(Object
*obj
)
315 PL011State
*s
= PL011(obj
);
317 s
->id
= pl011_id_luminary
;
320 static const TypeInfo pl011_luminary_info
= {
321 .name
= "pl011_luminary",
322 .parent
= TYPE_PL011
,
323 .instance_init
= pl011_luminary_init
,
326 static void pl011_register_types(void)
328 type_register_static(&pl011_arm_info
);
329 type_register_static(&pl011_luminary_info
);
332 type_init(pl011_register_types
)