2 * Arm PrimeCell PL011 UART
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
11 #include "qemu-char.h"
23 uint32_t read_fifo
[16];
33 const unsigned char *id
;
36 #define PL011_INT_TX 0x20
37 #define PL011_INT_RX 0x10
39 #define PL011_FLAG_TXFE 0x80
40 #define PL011_FLAG_RXFF 0x40
41 #define PL011_FLAG_TXFF 0x20
42 #define PL011_FLAG_RXFE 0x10
44 static const unsigned char pl011_id_arm
[8] =
45 { 0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
46 static const unsigned char pl011_id_luminary
[8] =
47 { 0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 };
49 static void pl011_update(pl011_state
*s
)
53 flags
= s
->int_level
& s
->int_enabled
;
54 qemu_set_irq(s
->irq
, flags
!= 0);
57 static uint64_t pl011_read(void *opaque
, target_phys_addr_t offset
,
60 pl011_state
*s
= (pl011_state
*)opaque
;
63 if (offset
>= 0xfe0 && offset
< 0x1000) {
64 return s
->id
[(offset
- 0xfe0) >> 2];
66 switch (offset
>> 2) {
68 s
->flags
&= ~PL011_FLAG_RXFF
;
69 c
= s
->read_fifo
[s
->read_pos
];
70 if (s
->read_count
> 0) {
72 if (++s
->read_pos
== 16)
75 if (s
->read_count
== 0) {
76 s
->flags
|= PL011_FLAG_RXFE
;
78 if (s
->read_count
== s
->read_trigger
- 1)
79 s
->int_level
&= ~ PL011_INT_RX
;
82 qemu_chr_accept_input(s
->chr
);
89 case 8: /* UARTILPR */
91 case 9: /* UARTIBRD */
93 case 10: /* UARTFBRD */
95 case 11: /* UARTLCR_H */
99 case 13: /* UARTIFLS */
101 case 14: /* UARTIMSC */
102 return s
->int_enabled
;
103 case 15: /* UARTRIS */
105 case 16: /* UARTMIS */
106 return s
->int_level
& s
->int_enabled
;
107 case 18: /* UARTDMACR */
110 hw_error("pl011_read: Bad offset %x\n", (int)offset
);
115 static void pl011_set_read_trigger(pl011_state
*s
)
118 /* The docs say the RX interrupt is triggered when the FIFO exceeds
119 the threshold. However linux only reads the FIFO in response to an
120 interrupt. Triggering the interrupt when the FIFO is non-empty seems
121 to make things work. */
123 s
->read_trigger
= (s
->ifl
>> 1) & 0x1c;
129 static void pl011_write(void *opaque
, target_phys_addr_t offset
,
130 uint64_t value
, unsigned size
)
132 pl011_state
*s
= (pl011_state
*)opaque
;
135 switch (offset
>> 2) {
137 /* ??? Check if transmitter is enabled. */
140 qemu_chr_fe_write(s
->chr
, &ch
, 1);
141 s
->int_level
|= PL011_INT_TX
;
148 /* Writes to Flag register are ignored. */
150 case 8: /* UARTUARTILPR */
153 case 9: /* UARTIBRD */
156 case 10: /* UARTFBRD */
159 case 11: /* UARTLCR_H */
161 pl011_set_read_trigger(s
);
163 case 12: /* UARTCR */
164 /* ??? Need to implement the enable and loopback bits. */
167 case 13: /* UARTIFS */
169 pl011_set_read_trigger(s
);
171 case 14: /* UARTIMSC */
172 s
->int_enabled
= value
;
175 case 17: /* UARTICR */
176 s
->int_level
&= ~value
;
179 case 18: /* UARTDMACR */
182 hw_error("PL011: DMA not implemented\n");
185 hw_error("pl011_write: Bad offset %x\n", (int)offset
);
189 static int pl011_can_receive(void *opaque
)
191 pl011_state
*s
= (pl011_state
*)opaque
;
194 return s
->read_count
< 16;
196 return s
->read_count
< 1;
199 static void pl011_put_fifo(void *opaque
, uint32_t value
)
201 pl011_state
*s
= (pl011_state
*)opaque
;
204 slot
= s
->read_pos
+ s
->read_count
;
207 s
->read_fifo
[slot
] = value
;
209 s
->flags
&= ~PL011_FLAG_RXFE
;
210 if (s
->cr
& 0x10 || s
->read_count
== 16) {
211 s
->flags
|= PL011_FLAG_RXFF
;
213 if (s
->read_count
== s
->read_trigger
) {
214 s
->int_level
|= PL011_INT_RX
;
219 static void pl011_receive(void *opaque
, const uint8_t *buf
, int size
)
221 pl011_put_fifo(opaque
, *buf
);
224 static void pl011_event(void *opaque
, int event
)
226 if (event
== CHR_EVENT_BREAK
)
227 pl011_put_fifo(opaque
, 0x400);
230 static const MemoryRegionOps pl011_ops
= {
232 .write
= pl011_write
,
233 .endianness
= DEVICE_NATIVE_ENDIAN
,
236 static const VMStateDescription vmstate_pl011
= {
239 .minimum_version_id
= 1,
240 .minimum_version_id_old
= 1,
241 .fields
= (VMStateField
[]) {
242 VMSTATE_UINT32(readbuff
, pl011_state
),
243 VMSTATE_UINT32(flags
, pl011_state
),
244 VMSTATE_UINT32(lcr
, pl011_state
),
245 VMSTATE_UINT32(cr
, pl011_state
),
246 VMSTATE_UINT32(dmacr
, pl011_state
),
247 VMSTATE_UINT32(int_enabled
, pl011_state
),
248 VMSTATE_UINT32(int_level
, pl011_state
),
249 VMSTATE_UINT32_ARRAY(read_fifo
, pl011_state
, 16),
250 VMSTATE_UINT32(ilpr
, pl011_state
),
251 VMSTATE_UINT32(ibrd
, pl011_state
),
252 VMSTATE_UINT32(fbrd
, pl011_state
),
253 VMSTATE_UINT32(ifl
, pl011_state
),
254 VMSTATE_INT32(read_pos
, pl011_state
),
255 VMSTATE_INT32(read_count
, pl011_state
),
256 VMSTATE_INT32(read_trigger
, pl011_state
),
257 VMSTATE_END_OF_LIST()
261 static int pl011_init(SysBusDevice
*dev
, const unsigned char *id
)
263 pl011_state
*s
= FROM_SYSBUS(pl011_state
, dev
);
265 memory_region_init_io(&s
->iomem
, &pl011_ops
, s
, "pl011", 0x1000);
266 sysbus_init_mmio(dev
, &s
->iomem
);
267 sysbus_init_irq(dev
, &s
->irq
);
269 s
->chr
= qemu_char_get_next_serial();
276 qemu_chr_add_handlers(s
->chr
, pl011_can_receive
, pl011_receive
,
279 vmstate_register(&dev
->qdev
, -1, &vmstate_pl011
, s
);
283 static int pl011_arm_init(SysBusDevice
*dev
)
285 return pl011_init(dev
, pl011_id_arm
);
288 static int pl011_luminary_init(SysBusDevice
*dev
)
290 return pl011_init(dev
, pl011_id_luminary
);
293 static void pl011_arm_class_init(ObjectClass
*klass
, void *data
)
295 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
297 sdc
->init
= pl011_arm_init
;
300 static TypeInfo pl011_arm_info
= {
302 .parent
= TYPE_SYS_BUS_DEVICE
,
303 .instance_size
= sizeof(pl011_state
),
304 .class_init
= pl011_arm_class_init
,
307 static void pl011_luminary_class_init(ObjectClass
*klass
, void *data
)
309 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
311 sdc
->init
= pl011_luminary_init
;
314 static TypeInfo pl011_luminary_info
= {
315 .name
= "pl011_luminary",
316 .parent
= TYPE_SYS_BUS_DEVICE
,
317 .instance_size
= sizeof(pl011_state
),
318 .class_init
= pl011_luminary_class_init
,
321 static void pl011_register_types(void)
323 type_register_static(&pl011_arm_info
);
324 type_register_static(&pl011_luminary_info
);
327 type_init(pl011_register_types
)