2 * Arm PrimeCell PL022 Synchronous Serial Port
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
11 #include "primecell.h"
13 //#define DEBUG_PL022 1
16 #define DPRINTF(fmt, args...) \
17 do { printf("pl022: " fmt , ##args); } while (0)
18 #define BADF(fmt, args...) \
19 do { fprintf(stderr, "pl022: error: " fmt , ##args); exit(1);} while (0)
21 #define DPRINTF(fmt, args...) do {} while(0)
22 #define BADF(fmt, args...) \
23 do { fprintf(stderr, "pl022: error: " fmt , ##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
50 /* The FIFO head points to the next empty entry. */
58 int (*xfer_cb
)(void *, int);
62 static const unsigned char pl022_id
[8] =
63 { 0x22, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
65 static void pl022_update(pl022_state
*s
)
68 if (s
->tx_fifo_len
== 0)
69 s
->sr
|= PL022_SR_TFE
;
70 if (s
->tx_fifo_len
!= 8)
71 s
->sr
|= PL022_SR_TNF
;
72 if (s
->rx_fifo_len
!= 0)
73 s
->sr
|= PL022_SR_RNE
;
74 if (s
->rx_fifo_len
== 8)
75 s
->sr
|= PL022_SR_RFF
;
77 s
->sr
|= PL022_SR_BSY
;
79 if (s
->rx_fifo_len
>= 4)
80 s
->is
|= PL022_INT_RX
;
81 if (s
->tx_fifo_len
<= 4)
82 s
->is
|= PL022_INT_TX
;
84 qemu_set_irq(s
->irq
, (s
->is
& s
->im
) != 0);
87 static void pl022_xfer(pl022_state
*s
)
93 if ((s
->cr1
& PL022_CR1_SSE
) == 0) {
95 DPRINTF("Disabled\n");
99 DPRINTF("Maybe xfer %d/%d\n", s
->tx_fifo_len
, s
->rx_fifo_len
);
100 i
= (s
->tx_fifo_head
- s
->tx_fifo_len
) & 7;
102 /* ??? We do not emulate the line speed.
103 This may break some applications. The are two problematic cases:
104 (a) A driver feeds data into the TX FIFO until it is full,
105 and only then drains the RX FIFO. On real hardware the CPU can
106 feed data fast enough that the RX fifo never gets chance to overflow.
107 (b) A driver transmits data, deliberately allowing the RX FIFO to
108 overflow because it ignores the RX data anyway.
110 We choose to support (a) by stalling the transmit engine if it would
111 cause the RX FIFO to overflow. In practice much transmit-only code
112 falls into (a) because it flushes the RX FIFO to determine when
113 the transfer has completed. */
114 while (s
->tx_fifo_len
&& s
->rx_fifo_len
< 8) {
117 if (s
->cr1
& PL022_CR1_LBM
) {
119 } else if (s
->xfer_cb
) {
120 val
= s
->xfer_cb(s
->opaque
, val
);
124 s
->rx_fifo
[o
] = val
& s
->bitmask
;
134 static uint32_t pl022_read(void *opaque
, target_phys_addr_t 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 cpu_abort (cpu_single_env
, "pl022_read: Bad offset %x\n",
177 static void pl022_write(void *opaque
, target_phys_addr_t offset
,
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", 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 cpu_abort (cpu_single_env
, "pl022: DMA not implemented\n");
218 cpu_abort (cpu_single_env
, "pl022_write: Bad offset %x\n",
223 static void pl022_reset(pl022_state
*s
)
228 s
->is
= PL022_INT_TX
;
229 s
->sr
= PL022_SR_TFE
| PL022_SR_TNF
;
232 static CPUReadMemoryFunc
*pl022_readfn
[] = {
238 static CPUWriteMemoryFunc
*pl022_writefn
[] = {
244 static void pl022_save(QEMUFile
*f
, void *opaque
)
246 pl022_state
*s
= (pl022_state
*)opaque
;
249 qemu_put_be32(f
, s
->cr0
);
250 qemu_put_be32(f
, s
->cr1
);
251 qemu_put_be32(f
, s
->bitmask
);
252 qemu_put_be32(f
, s
->sr
);
253 qemu_put_be32(f
, s
->cpsr
);
254 qemu_put_be32(f
, s
->is
);
255 qemu_put_be32(f
, s
->im
);
256 qemu_put_be32(f
, s
->tx_fifo_head
);
257 qemu_put_be32(f
, s
->rx_fifo_head
);
258 qemu_put_be32(f
, s
->tx_fifo_len
);
259 qemu_put_be32(f
, s
->rx_fifo_len
);
260 for (i
= 0; i
< 8; i
++) {
261 qemu_put_be16(f
, s
->tx_fifo
[i
]);
262 qemu_put_be16(f
, s
->rx_fifo
[i
]);
266 static int pl022_load(QEMUFile
*f
, void *opaque
, int version_id
)
268 pl022_state
*s
= (pl022_state
*)opaque
;
274 s
->cr0
= qemu_get_be32(f
);
275 s
->cr1
= qemu_get_be32(f
);
276 s
->bitmask
= qemu_get_be32(f
);
277 s
->sr
= qemu_get_be32(f
);
278 s
->cpsr
= qemu_get_be32(f
);
279 s
->is
= qemu_get_be32(f
);
280 s
->im
= qemu_get_be32(f
);
281 s
->tx_fifo_head
= qemu_get_be32(f
);
282 s
->rx_fifo_head
= qemu_get_be32(f
);
283 s
->tx_fifo_len
= qemu_get_be32(f
);
284 s
->rx_fifo_len
= qemu_get_be32(f
);
285 for (i
= 0; i
< 8; i
++) {
286 s
->tx_fifo
[i
] = qemu_get_be16(f
);
287 s
->rx_fifo
[i
] = qemu_get_be16(f
);
293 void pl022_init(uint32_t base
, qemu_irq irq
, int (*xfer_cb
)(void *, int),
299 s
= (pl022_state
*)qemu_mallocz(sizeof(pl022_state
));
300 iomemtype
= cpu_register_io_memory(0, pl022_readfn
,
302 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
304 s
->xfer_cb
= xfer_cb
;
307 register_savevm("pl022_ssp", -1, 1, pl022_save
, pl022_load
, s
);