2 * Arm PrimeCell PL022 Synchronous Serial Port
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 #include "hw/sysbus.h"
13 //#define DEBUG_PL022 1
16 #define DPRINTF(fmt, ...) \
17 do { printf("pl022: " fmt , ## __VA_ARGS__); } while (0)
18 #define BADF(fmt, ...) \
19 do { fprintf(stderr, "pl022: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
21 #define DPRINTF(fmt, ...) do {} while(0)
22 #define BADF(fmt, ...) \
23 do { fprintf(stderr, "pl022: error: " fmt , ## __VA_ARGS__);} while (0)
26 #define PL022_CR1_LBM 0x01
27 #define PL022_CR1_SSE 0x02
28 #define PL022_CR1_MS 0x04
29 #define PL022_CR1_SDO 0x08
31 #define PL022_SR_TFE 0x01
32 #define PL022_SR_TNF 0x02
33 #define PL022_SR_RNE 0x04
34 #define PL022_SR_RFF 0x08
35 #define PL022_SR_BSY 0x10
37 #define PL022_INT_ROR 0x01
38 #define PL022_INT_RT 0x04
39 #define PL022_INT_RX 0x04
40 #define PL022_INT_TX 0x08
52 /* The FIFO head points to the next empty entry. */
63 static const unsigned char pl022_id
[8] =
64 { 0x22, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
66 static void pl022_update(pl022_state
*s
)
69 if (s
->tx_fifo_len
== 0)
70 s
->sr
|= PL022_SR_TFE
;
71 if (s
->tx_fifo_len
!= 8)
72 s
->sr
|= PL022_SR_TNF
;
73 if (s
->rx_fifo_len
!= 0)
74 s
->sr
|= PL022_SR_RNE
;
75 if (s
->rx_fifo_len
== 8)
76 s
->sr
|= PL022_SR_RFF
;
78 s
->sr
|= PL022_SR_BSY
;
80 if (s
->rx_fifo_len
>= 4)
81 s
->is
|= PL022_INT_RX
;
82 if (s
->tx_fifo_len
<= 4)
83 s
->is
|= PL022_INT_TX
;
85 qemu_set_irq(s
->irq
, (s
->is
& s
->im
) != 0);
88 static void pl022_xfer(pl022_state
*s
)
94 if ((s
->cr1
& PL022_CR1_SSE
) == 0) {
96 DPRINTF("Disabled\n");
100 DPRINTF("Maybe xfer %d/%d\n", s
->tx_fifo_len
, s
->rx_fifo_len
);
101 i
= (s
->tx_fifo_head
- s
->tx_fifo_len
) & 7;
103 /* ??? We do not emulate the line speed.
104 This may break some applications. The are two problematic cases:
105 (a) A driver feeds data into the TX FIFO until it is full,
106 and only then drains the RX FIFO. On real hardware the CPU can
107 feed data fast enough that the RX fifo never gets chance to overflow.
108 (b) A driver transmits data, deliberately allowing the RX FIFO to
109 overflow because it ignores the RX data anyway.
111 We choose to support (a) by stalling the transmit engine if it would
112 cause the RX FIFO to overflow. In practice much transmit-only code
113 falls into (a) because it flushes the RX FIFO to determine when
114 the transfer has completed. */
115 while (s
->tx_fifo_len
&& s
->rx_fifo_len
< 8) {
118 if (s
->cr1
& PL022_CR1_LBM
) {
121 val
= ssi_transfer(s
->ssi
, val
);
123 s
->rx_fifo
[o
] = val
& s
->bitmask
;
133 static uint64_t pl022_read(void *opaque
, hwaddr offset
,
136 pl022_state
*s
= (pl022_state
*)opaque
;
139 if (offset
>= 0xfe0 && offset
< 0x1000) {
140 return pl022_id
[(offset
- 0xfe0) >> 2];
148 if (s
->rx_fifo_len
) {
149 val
= s
->rx_fifo
[(s
->rx_fifo_head
- s
->rx_fifo_len
) & 7];
150 DPRINTF("RX %02x\n", val
);
159 case 0x10: /* CPSR */
161 case 0x14: /* IMSC */
166 return s
->im
& s
->is
;
167 case 0x20: /* DMACR */
168 /* Not implemented. */
171 qemu_log_mask(LOG_GUEST_ERROR
,
172 "pl022_read: Bad offset %x\n", (int)offset
);
177 static void pl022_write(void *opaque
, hwaddr offset
,
178 uint64_t value
, unsigned size
)
180 pl022_state
*s
= (pl022_state
*)opaque
;
185 /* Clock rate and format are ignored. */
186 s
->bitmask
= (1 << ((value
& 15) + 1)) - 1;
190 if ((s
->cr1
& (PL022_CR1_MS
| PL022_CR1_SSE
))
191 == (PL022_CR1_MS
| PL022_CR1_SSE
)) {
192 BADF("SPI slave mode not implemented\n");
197 if (s
->tx_fifo_len
< 8) {
198 DPRINTF("TX %02x\n", (unsigned)value
);
199 s
->tx_fifo
[s
->tx_fifo_head
] = value
& s
->bitmask
;
200 s
->tx_fifo_head
= (s
->tx_fifo_head
+ 1) & 7;
205 case 0x10: /* CPSR */
206 /* Prescaler. Ignored. */
207 s
->cpsr
= value
& 0xff;
209 case 0x14: /* IMSC */
213 case 0x20: /* DMACR */
215 qemu_log_mask(LOG_UNIMP
, "pl022: DMA not implemented\n");
219 qemu_log_mask(LOG_GUEST_ERROR
,
220 "pl022_write: Bad offset %x\n", (int)offset
);
224 static void pl022_reset(pl022_state
*s
)
229 s
->is
= PL022_INT_TX
;
230 s
->sr
= PL022_SR_TFE
| PL022_SR_TNF
;
233 static const MemoryRegionOps pl022_ops
= {
235 .write
= pl022_write
,
236 .endianness
= DEVICE_NATIVE_ENDIAN
,
239 static const VMStateDescription vmstate_pl022
= {
242 .minimum_version_id
= 1,
243 .minimum_version_id_old
= 1,
244 .fields
= (VMStateField
[]) {
245 VMSTATE_UINT32(cr0
, pl022_state
),
246 VMSTATE_UINT32(cr1
, pl022_state
),
247 VMSTATE_UINT32(bitmask
, pl022_state
),
248 VMSTATE_UINT32(sr
, pl022_state
),
249 VMSTATE_UINT32(cpsr
, pl022_state
),
250 VMSTATE_UINT32(is
, pl022_state
),
251 VMSTATE_UINT32(im
, pl022_state
),
252 VMSTATE_INT32(tx_fifo_head
, pl022_state
),
253 VMSTATE_INT32(rx_fifo_head
, pl022_state
),
254 VMSTATE_INT32(tx_fifo_len
, pl022_state
),
255 VMSTATE_INT32(rx_fifo_len
, pl022_state
),
256 VMSTATE_UINT16(tx_fifo
[0], pl022_state
),
257 VMSTATE_UINT16(rx_fifo
[0], pl022_state
),
258 VMSTATE_UINT16(tx_fifo
[1], pl022_state
),
259 VMSTATE_UINT16(rx_fifo
[1], pl022_state
),
260 VMSTATE_UINT16(tx_fifo
[2], pl022_state
),
261 VMSTATE_UINT16(rx_fifo
[2], pl022_state
),
262 VMSTATE_UINT16(tx_fifo
[3], pl022_state
),
263 VMSTATE_UINT16(rx_fifo
[3], pl022_state
),
264 VMSTATE_UINT16(tx_fifo
[4], pl022_state
),
265 VMSTATE_UINT16(rx_fifo
[4], pl022_state
),
266 VMSTATE_UINT16(tx_fifo
[5], pl022_state
),
267 VMSTATE_UINT16(rx_fifo
[5], pl022_state
),
268 VMSTATE_UINT16(tx_fifo
[6], pl022_state
),
269 VMSTATE_UINT16(rx_fifo
[6], pl022_state
),
270 VMSTATE_UINT16(tx_fifo
[7], pl022_state
),
271 VMSTATE_UINT16(rx_fifo
[7], pl022_state
),
272 VMSTATE_END_OF_LIST()
276 static int pl022_init(SysBusDevice
*dev
)
278 pl022_state
*s
= FROM_SYSBUS(pl022_state
, dev
);
280 memory_region_init_io(&s
->iomem
, &pl022_ops
, s
, "pl022", 0x1000);
281 sysbus_init_mmio(dev
, &s
->iomem
);
282 sysbus_init_irq(dev
, &s
->irq
);
283 s
->ssi
= ssi_create_bus(&dev
->qdev
, "ssi");
285 vmstate_register(&dev
->qdev
, -1, &vmstate_pl022
, s
);
289 static void pl022_class_init(ObjectClass
*klass
, void *data
)
291 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
293 sdc
->init
= pl022_init
;
296 static const TypeInfo pl022_info
= {
298 .parent
= TYPE_SYS_BUS_DEVICE
,
299 .instance_size
= sizeof(pl022_state
),
300 .class_init
= pl022_class_init
,
303 static void pl022_register_types(void)
305 type_register_static(&pl022_info
);
308 type_init(pl022_register_types
)