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 "qemu/osdep.h"
11 #include "hw/sysbus.h"
12 #include "hw/ssi/pl022.h"
13 #include "hw/ssi/ssi.h"
16 //#define DEBUG_PL022 1
19 #define DPRINTF(fmt, ...) \
20 do { printf("pl022: " fmt , ## __VA_ARGS__); } while (0)
21 #define BADF(fmt, ...) \
22 do { fprintf(stderr, "pl022: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
24 #define DPRINTF(fmt, ...) do {} while(0)
25 #define BADF(fmt, ...) \
26 do { fprintf(stderr, "pl022: error: " fmt , ## __VA_ARGS__);} while (0)
29 #define PL022_CR1_LBM 0x01
30 #define PL022_CR1_SSE 0x02
31 #define PL022_CR1_MS 0x04
32 #define PL022_CR1_SDO 0x08
34 #define PL022_SR_TFE 0x01
35 #define PL022_SR_TNF 0x02
36 #define PL022_SR_RNE 0x04
37 #define PL022_SR_RFF 0x08
38 #define PL022_SR_BSY 0x10
40 #define PL022_INT_ROR 0x01
41 #define PL022_INT_RT 0x02
42 #define PL022_INT_RX 0x04
43 #define PL022_INT_TX 0x08
45 static const unsigned char pl022_id
[8] =
46 { 0x22, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
48 static void pl022_update(PL022State
*s
)
51 if (s
->tx_fifo_len
== 0)
52 s
->sr
|= PL022_SR_TFE
;
53 if (s
->tx_fifo_len
!= 8)
54 s
->sr
|= PL022_SR_TNF
;
55 if (s
->rx_fifo_len
!= 0)
56 s
->sr
|= PL022_SR_RNE
;
57 if (s
->rx_fifo_len
== 8)
58 s
->sr
|= PL022_SR_RFF
;
60 s
->sr
|= PL022_SR_BSY
;
62 if (s
->rx_fifo_len
>= 4)
63 s
->is
|= PL022_INT_RX
;
64 if (s
->tx_fifo_len
<= 4)
65 s
->is
|= PL022_INT_TX
;
67 qemu_set_irq(s
->irq
, (s
->is
& s
->im
) != 0);
70 static void pl022_xfer(PL022State
*s
)
76 if ((s
->cr1
& PL022_CR1_SSE
) == 0) {
78 DPRINTF("Disabled\n");
82 DPRINTF("Maybe xfer %d/%d\n", s
->tx_fifo_len
, s
->rx_fifo_len
);
83 i
= (s
->tx_fifo_head
- s
->tx_fifo_len
) & 7;
85 /* ??? We do not emulate the line speed.
86 This may break some applications. The are two problematic cases:
87 (a) A driver feeds data into the TX FIFO until it is full,
88 and only then drains the RX FIFO. On real hardware the CPU can
89 feed data fast enough that the RX fifo never gets chance to overflow.
90 (b) A driver transmits data, deliberately allowing the RX FIFO to
91 overflow because it ignores the RX data anyway.
93 We choose to support (a) by stalling the transmit engine if it would
94 cause the RX FIFO to overflow. In practice much transmit-only code
95 falls into (a) because it flushes the RX FIFO to determine when
96 the transfer has completed. */
97 while (s
->tx_fifo_len
&& s
->rx_fifo_len
< 8) {
100 if (s
->cr1
& PL022_CR1_LBM
) {
103 val
= ssi_transfer(s
->ssi
, val
);
105 s
->rx_fifo
[o
] = val
& s
->bitmask
;
115 static uint64_t pl022_read(void *opaque
, hwaddr offset
,
118 PL022State
*s
= (PL022State
*)opaque
;
121 if (offset
>= 0xfe0 && offset
< 0x1000) {
122 return pl022_id
[(offset
- 0xfe0) >> 2];
130 if (s
->rx_fifo_len
) {
131 val
= s
->rx_fifo
[(s
->rx_fifo_head
- s
->rx_fifo_len
) & 7];
132 DPRINTF("RX %02x\n", val
);
141 case 0x10: /* CPSR */
143 case 0x14: /* IMSC */
148 return s
->im
& s
->is
;
149 case 0x24: /* DMACR */
150 /* Not implemented. */
153 qemu_log_mask(LOG_GUEST_ERROR
,
154 "pl022_read: Bad offset %x\n", (int)offset
);
159 static void pl022_write(void *opaque
, hwaddr offset
,
160 uint64_t value
, unsigned size
)
162 PL022State
*s
= (PL022State
*)opaque
;
167 /* Clock rate and format are ignored. */
168 s
->bitmask
= (1 << ((value
& 15) + 1)) - 1;
172 if ((s
->cr1
& (PL022_CR1_MS
| PL022_CR1_SSE
))
173 == (PL022_CR1_MS
| PL022_CR1_SSE
)) {
174 BADF("SPI slave mode not implemented\n");
179 if (s
->tx_fifo_len
< 8) {
180 DPRINTF("TX %02x\n", (unsigned)value
);
181 s
->tx_fifo
[s
->tx_fifo_head
] = value
& s
->bitmask
;
182 s
->tx_fifo_head
= (s
->tx_fifo_head
+ 1) & 7;
187 case 0x10: /* CPSR */
188 /* Prescaler. Ignored. */
189 s
->cpsr
= value
& 0xff;
191 case 0x14: /* IMSC */
197 * write-1-to-clear: bit 0 clears ROR, bit 1 clears RT;
198 * RX and TX interrupts cannot be cleared this way.
200 value
&= PL022_INT_ROR
| PL022_INT_RT
;
203 case 0x24: /* DMACR */
205 qemu_log_mask(LOG_UNIMP
, "pl022: DMA not implemented\n");
209 qemu_log_mask(LOG_GUEST_ERROR
,
210 "pl022_write: Bad offset %x\n", (int)offset
);
214 static void pl022_reset(DeviceState
*dev
)
216 PL022State
*s
= PL022(dev
);
221 s
->is
= PL022_INT_TX
;
222 s
->sr
= PL022_SR_TFE
| PL022_SR_TNF
;
225 static const MemoryRegionOps pl022_ops
= {
227 .write
= pl022_write
,
228 .endianness
= DEVICE_NATIVE_ENDIAN
,
231 static int pl022_post_load(void *opaque
, int version_id
)
233 PL022State
*s
= opaque
;
235 if (s
->tx_fifo_head
< 0 ||
236 s
->tx_fifo_head
>= ARRAY_SIZE(s
->tx_fifo
) ||
237 s
->rx_fifo_head
< 0 ||
238 s
->rx_fifo_head
>= ARRAY_SIZE(s
->rx_fifo
)) {
244 static const VMStateDescription vmstate_pl022
= {
247 .minimum_version_id
= 1,
248 .post_load
= pl022_post_load
,
249 .fields
= (VMStateField
[]) {
250 VMSTATE_UINT32(cr0
, PL022State
),
251 VMSTATE_UINT32(cr1
, PL022State
),
252 VMSTATE_UINT32(bitmask
, PL022State
),
253 VMSTATE_UINT32(sr
, PL022State
),
254 VMSTATE_UINT32(cpsr
, PL022State
),
255 VMSTATE_UINT32(is
, PL022State
),
256 VMSTATE_UINT32(im
, PL022State
),
257 VMSTATE_INT32(tx_fifo_head
, PL022State
),
258 VMSTATE_INT32(rx_fifo_head
, PL022State
),
259 VMSTATE_INT32(tx_fifo_len
, PL022State
),
260 VMSTATE_INT32(rx_fifo_len
, PL022State
),
261 VMSTATE_UINT16(tx_fifo
[0], PL022State
),
262 VMSTATE_UINT16(rx_fifo
[0], PL022State
),
263 VMSTATE_UINT16(tx_fifo
[1], PL022State
),
264 VMSTATE_UINT16(rx_fifo
[1], PL022State
),
265 VMSTATE_UINT16(tx_fifo
[2], PL022State
),
266 VMSTATE_UINT16(rx_fifo
[2], PL022State
),
267 VMSTATE_UINT16(tx_fifo
[3], PL022State
),
268 VMSTATE_UINT16(rx_fifo
[3], PL022State
),
269 VMSTATE_UINT16(tx_fifo
[4], PL022State
),
270 VMSTATE_UINT16(rx_fifo
[4], PL022State
),
271 VMSTATE_UINT16(tx_fifo
[5], PL022State
),
272 VMSTATE_UINT16(rx_fifo
[5], PL022State
),
273 VMSTATE_UINT16(tx_fifo
[6], PL022State
),
274 VMSTATE_UINT16(rx_fifo
[6], PL022State
),
275 VMSTATE_UINT16(tx_fifo
[7], PL022State
),
276 VMSTATE_UINT16(rx_fifo
[7], PL022State
),
277 VMSTATE_END_OF_LIST()
281 static void pl022_realize(DeviceState
*dev
, Error
**errp
)
283 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
284 PL022State
*s
= PL022(dev
);
286 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl022_ops
, s
, "pl022", 0x1000);
287 sysbus_init_mmio(sbd
, &s
->iomem
);
288 sysbus_init_irq(sbd
, &s
->irq
);
289 s
->ssi
= ssi_create_bus(dev
, "ssi");
292 static void pl022_class_init(ObjectClass
*klass
, void *data
)
294 DeviceClass
*dc
= DEVICE_CLASS(klass
);
296 dc
->reset
= pl022_reset
;
297 dc
->vmsd
= &vmstate_pl022
;
298 dc
->realize
= pl022_realize
;
301 static const TypeInfo pl022_info
= {
303 .parent
= TYPE_SYS_BUS_DEVICE
,
304 .instance_size
= sizeof(PL022State
),
305 .class_init
= pl022_class_init
,
308 static void pl022_register_types(void)
310 type_register_static(&pl022_info
);
313 type_init(pl022_register_types
)