root / hw / pl181.c @ 3ced9f7a
History | View | Annotate | Download (13.6 kB)
1 | 5fafdf24 | ths | /*
|
---|---|---|---|
2 | a1bb27b1 | pbrook | * Arm PrimeCell PL181 MultiMedia Card Interface
|
3 | a1bb27b1 | pbrook | *
|
4 | a1bb27b1 | pbrook | * Copyright (c) 2007 CodeSourcery.
|
5 | a1bb27b1 | pbrook | * Written by Paul Brook
|
6 | a1bb27b1 | pbrook | *
|
7 | a1bb27b1 | pbrook | * This code is licenced under the GPL.
|
8 | a1bb27b1 | pbrook | */
|
9 | a1bb27b1 | pbrook | |
10 | aa9311d8 | Paul Brook | #include "sysbus.h" |
11 | a1bb27b1 | pbrook | #include "sd.h" |
12 | aa9311d8 | Paul Brook | #include "sysemu.h" |
13 | a1bb27b1 | pbrook | |
14 | a1bb27b1 | pbrook | //#define DEBUG_PL181 1
|
15 | a1bb27b1 | pbrook | |
16 | a1bb27b1 | pbrook | #ifdef DEBUG_PL181
|
17 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) \
|
18 | 001faf32 | Blue Swirl | do { printf("pl181: " fmt , ## __VA_ARGS__); } while (0) |
19 | a1bb27b1 | pbrook | #else
|
20 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) do {} while(0) |
21 | a1bb27b1 | pbrook | #endif
|
22 | a1bb27b1 | pbrook | |
23 | a1bb27b1 | pbrook | #define PL181_FIFO_LEN 16 |
24 | a1bb27b1 | pbrook | |
25 | a1bb27b1 | pbrook | typedef struct { |
26 | aa9311d8 | Paul Brook | SysBusDevice busdev; |
27 | 42a10898 | pbrook | SDState *card; |
28 | a1bb27b1 | pbrook | uint32_t clock; |
29 | a1bb27b1 | pbrook | uint32_t power; |
30 | a1bb27b1 | pbrook | uint32_t cmdarg; |
31 | a1bb27b1 | pbrook | uint32_t cmd; |
32 | a1bb27b1 | pbrook | uint32_t datatimer; |
33 | a1bb27b1 | pbrook | uint32_t datalength; |
34 | a1bb27b1 | pbrook | uint32_t respcmd; |
35 | a1bb27b1 | pbrook | uint32_t response[4];
|
36 | a1bb27b1 | pbrook | uint32_t datactrl; |
37 | a1bb27b1 | pbrook | uint32_t datacnt; |
38 | a1bb27b1 | pbrook | uint32_t status; |
39 | a1bb27b1 | pbrook | uint32_t mask[2];
|
40 | a1bb27b1 | pbrook | int fifo_pos;
|
41 | a1bb27b1 | pbrook | int fifo_len;
|
42 | 6361cdb6 | pbrook | /* The linux 2.6.21 driver is buggy, and misbehaves if new data arrives
|
43 | 6361cdb6 | pbrook | while it is reading the FIFO. We hack around this be defering
|
44 | 6361cdb6 | pbrook | subsequent transfers until after the driver polls the status word.
|
45 | 6361cdb6 | pbrook | http://www.arm.linux.org.uk/developer/patches/viewpatch.php?id=4446/1
|
46 | 6361cdb6 | pbrook | */
|
47 | 6361cdb6 | pbrook | int linux_hack;
|
48 | a1bb27b1 | pbrook | uint32_t fifo[PL181_FIFO_LEN]; |
49 | d537cf6c | pbrook | qemu_irq irq[2];
|
50 | a1bb27b1 | pbrook | } pl181_state; |
51 | a1bb27b1 | pbrook | |
52 | a1bb27b1 | pbrook | #define PL181_CMD_INDEX 0x3f |
53 | a1bb27b1 | pbrook | #define PL181_CMD_RESPONSE (1 << 6) |
54 | a1bb27b1 | pbrook | #define PL181_CMD_LONGRESP (1 << 7) |
55 | a1bb27b1 | pbrook | #define PL181_CMD_INTERRUPT (1 << 8) |
56 | a1bb27b1 | pbrook | #define PL181_CMD_PENDING (1 << 9) |
57 | a1bb27b1 | pbrook | #define PL181_CMD_ENABLE (1 << 10) |
58 | a1bb27b1 | pbrook | |
59 | a1bb27b1 | pbrook | #define PL181_DATA_ENABLE (1 << 0) |
60 | a1bb27b1 | pbrook | #define PL181_DATA_DIRECTION (1 << 1) |
61 | a1bb27b1 | pbrook | #define PL181_DATA_MODE (1 << 2) |
62 | a1bb27b1 | pbrook | #define PL181_DATA_DMAENABLE (1 << 3) |
63 | a1bb27b1 | pbrook | |
64 | a1bb27b1 | pbrook | #define PL181_STATUS_CMDCRCFAIL (1 << 0) |
65 | a1bb27b1 | pbrook | #define PL181_STATUS_DATACRCFAIL (1 << 1) |
66 | a1bb27b1 | pbrook | #define PL181_STATUS_CMDTIMEOUT (1 << 2) |
67 | a1bb27b1 | pbrook | #define PL181_STATUS_DATATIMEOUT (1 << 3) |
68 | a1bb27b1 | pbrook | #define PL181_STATUS_TXUNDERRUN (1 << 4) |
69 | a1bb27b1 | pbrook | #define PL181_STATUS_RXOVERRUN (1 << 5) |
70 | a1bb27b1 | pbrook | #define PL181_STATUS_CMDRESPEND (1 << 6) |
71 | a1bb27b1 | pbrook | #define PL181_STATUS_CMDSENT (1 << 7) |
72 | a1bb27b1 | pbrook | #define PL181_STATUS_DATAEND (1 << 8) |
73 | a1bb27b1 | pbrook | #define PL181_STATUS_DATABLOCKEND (1 << 10) |
74 | a1bb27b1 | pbrook | #define PL181_STATUS_CMDACTIVE (1 << 11) |
75 | a1bb27b1 | pbrook | #define PL181_STATUS_TXACTIVE (1 << 12) |
76 | a1bb27b1 | pbrook | #define PL181_STATUS_RXACTIVE (1 << 13) |
77 | a1bb27b1 | pbrook | #define PL181_STATUS_TXFIFOHALFEMPTY (1 << 14) |
78 | a1bb27b1 | pbrook | #define PL181_STATUS_RXFIFOHALFFULL (1 << 15) |
79 | a1bb27b1 | pbrook | #define PL181_STATUS_TXFIFOFULL (1 << 16) |
80 | a1bb27b1 | pbrook | #define PL181_STATUS_RXFIFOFULL (1 << 17) |
81 | a1bb27b1 | pbrook | #define PL181_STATUS_TXFIFOEMPTY (1 << 18) |
82 | a1bb27b1 | pbrook | #define PL181_STATUS_RXFIFOEMPTY (1 << 19) |
83 | a1bb27b1 | pbrook | #define PL181_STATUS_TXDATAAVLBL (1 << 20) |
84 | a1bb27b1 | pbrook | #define PL181_STATUS_RXDATAAVLBL (1 << 21) |
85 | a1bb27b1 | pbrook | |
86 | a1bb27b1 | pbrook | #define PL181_STATUS_TX_FIFO (PL181_STATUS_TXACTIVE \
|
87 | a1bb27b1 | pbrook | |PL181_STATUS_TXFIFOHALFEMPTY \ |
88 | a1bb27b1 | pbrook | |PL181_STATUS_TXFIFOFULL \ |
89 | a1bb27b1 | pbrook | |PL181_STATUS_TXFIFOEMPTY \ |
90 | a1bb27b1 | pbrook | |PL181_STATUS_TXDATAAVLBL) |
91 | a1bb27b1 | pbrook | #define PL181_STATUS_RX_FIFO (PL181_STATUS_RXACTIVE \
|
92 | a1bb27b1 | pbrook | |PL181_STATUS_RXFIFOHALFFULL \ |
93 | a1bb27b1 | pbrook | |PL181_STATUS_RXFIFOFULL \ |
94 | a1bb27b1 | pbrook | |PL181_STATUS_RXFIFOEMPTY \ |
95 | a1bb27b1 | pbrook | |PL181_STATUS_RXDATAAVLBL) |
96 | a1bb27b1 | pbrook | |
97 | a1bb27b1 | pbrook | static const unsigned char pl181_id[] = |
98 | a1bb27b1 | pbrook | { 0x81, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 }; |
99 | a1bb27b1 | pbrook | |
100 | a1bb27b1 | pbrook | static void pl181_update(pl181_state *s) |
101 | a1bb27b1 | pbrook | { |
102 | a1bb27b1 | pbrook | int i;
|
103 | a1bb27b1 | pbrook | for (i = 0; i < 2; i++) { |
104 | d537cf6c | pbrook | qemu_set_irq(s->irq[i], (s->status & s->mask[i]) != 0);
|
105 | a1bb27b1 | pbrook | } |
106 | a1bb27b1 | pbrook | } |
107 | a1bb27b1 | pbrook | |
108 | a1bb27b1 | pbrook | static void pl181_fifo_push(pl181_state *s, uint32_t value) |
109 | a1bb27b1 | pbrook | { |
110 | a1bb27b1 | pbrook | int n;
|
111 | a1bb27b1 | pbrook | |
112 | a1bb27b1 | pbrook | if (s->fifo_len == PL181_FIFO_LEN) {
|
113 | a1bb27b1 | pbrook | fprintf(stderr, "pl181: FIFO overflow\n");
|
114 | a1bb27b1 | pbrook | return;
|
115 | a1bb27b1 | pbrook | } |
116 | a1bb27b1 | pbrook | n = (s->fifo_pos + s->fifo_len) & (PL181_FIFO_LEN - 1);
|
117 | a1bb27b1 | pbrook | s->fifo_len++; |
118 | a1bb27b1 | pbrook | s->fifo[n] = value; |
119 | a1bb27b1 | pbrook | DPRINTF("FIFO push %08x\n", (int)value); |
120 | a1bb27b1 | pbrook | } |
121 | a1bb27b1 | pbrook | |
122 | a1bb27b1 | pbrook | static uint32_t pl181_fifo_pop(pl181_state *s)
|
123 | a1bb27b1 | pbrook | { |
124 | a1bb27b1 | pbrook | uint32_t value; |
125 | a1bb27b1 | pbrook | |
126 | a1bb27b1 | pbrook | if (s->fifo_len == 0) { |
127 | a1bb27b1 | pbrook | fprintf(stderr, "pl181: FIFO underflow\n");
|
128 | a1bb27b1 | pbrook | return 0; |
129 | a1bb27b1 | pbrook | } |
130 | a1bb27b1 | pbrook | value = s->fifo[s->fifo_pos]; |
131 | a1bb27b1 | pbrook | s->fifo_len--; |
132 | a1bb27b1 | pbrook | s->fifo_pos = (s->fifo_pos + 1) & (PL181_FIFO_LEN - 1); |
133 | a1bb27b1 | pbrook | DPRINTF("FIFO pop %08x\n", (int)value); |
134 | a1bb27b1 | pbrook | return value;
|
135 | a1bb27b1 | pbrook | } |
136 | a1bb27b1 | pbrook | |
137 | a1bb27b1 | pbrook | static void pl181_send_command(pl181_state *s) |
138 | a1bb27b1 | pbrook | { |
139 | bc24a225 | Paul Brook | SDRequest request; |
140 | a1bb27b1 | pbrook | uint8_t response[16];
|
141 | a1bb27b1 | pbrook | int rlen;
|
142 | a1bb27b1 | pbrook | |
143 | a1bb27b1 | pbrook | request.cmd = s->cmd & PL181_CMD_INDEX; |
144 | a1bb27b1 | pbrook | request.arg = s->cmdarg; |
145 | a1bb27b1 | pbrook | DPRINTF("Command %d %08x\n", request.cmd, request.arg);
|
146 | a1bb27b1 | pbrook | rlen = sd_do_command(s->card, &request, response); |
147 | a1bb27b1 | pbrook | if (rlen < 0) |
148 | a1bb27b1 | pbrook | goto error;
|
149 | a1bb27b1 | pbrook | if (s->cmd & PL181_CMD_RESPONSE) {
|
150 | a1bb27b1 | pbrook | #define RWORD(n) ((response[n] << 24) | (response[n + 1] << 16) \ |
151 | a1bb27b1 | pbrook | | (response[n + 2] << 8) | response[n + 3]) |
152 | a1bb27b1 | pbrook | if (rlen == 0 || (rlen == 4 && (s->cmd & PL181_CMD_LONGRESP))) |
153 | a1bb27b1 | pbrook | goto error;
|
154 | a1bb27b1 | pbrook | if (rlen != 4 && rlen != 16) |
155 | a1bb27b1 | pbrook | goto error;
|
156 | a1bb27b1 | pbrook | s->response[0] = RWORD(0); |
157 | a1bb27b1 | pbrook | if (rlen == 4) { |
158 | a1bb27b1 | pbrook | s->response[1] = s->response[2] = s->response[3] = 0; |
159 | a1bb27b1 | pbrook | } else {
|
160 | a1bb27b1 | pbrook | s->response[1] = RWORD(4); |
161 | a1bb27b1 | pbrook | s->response[2] = RWORD(8); |
162 | a1bb27b1 | pbrook | s->response[3] = RWORD(12) & ~1; |
163 | a1bb27b1 | pbrook | } |
164 | aa1f17c1 | ths | DPRINTF("Response received\n");
|
165 | a1bb27b1 | pbrook | s->status |= PL181_STATUS_CMDRESPEND; |
166 | a1bb27b1 | pbrook | #undef RWORD
|
167 | a1bb27b1 | pbrook | } else {
|
168 | a1bb27b1 | pbrook | DPRINTF("Command sent\n");
|
169 | a1bb27b1 | pbrook | s->status |= PL181_STATUS_CMDSENT; |
170 | a1bb27b1 | pbrook | } |
171 | a1bb27b1 | pbrook | return;
|
172 | a1bb27b1 | pbrook | |
173 | a1bb27b1 | pbrook | error:
|
174 | a1bb27b1 | pbrook | DPRINTF("Timeout\n");
|
175 | a1bb27b1 | pbrook | s->status |= PL181_STATUS_CMDTIMEOUT; |
176 | a1bb27b1 | pbrook | } |
177 | a1bb27b1 | pbrook | |
178 | aa1f17c1 | ths | /* Transfer data between the card and the FIFO. This is complicated by
|
179 | a1bb27b1 | pbrook | the FIFO holding 32-bit words and the card taking data in single byte
|
180 | a1bb27b1 | pbrook | chunks. FIFO bytes are transferred in little-endian order. */
|
181 | 3b46e624 | ths | |
182 | a1bb27b1 | pbrook | static void pl181_fifo_run(pl181_state *s) |
183 | a1bb27b1 | pbrook | { |
184 | a1bb27b1 | pbrook | uint32_t bits; |
185 | a1bb27b1 | pbrook | uint32_t value; |
186 | a1bb27b1 | pbrook | int n;
|
187 | a1bb27b1 | pbrook | int limit;
|
188 | a1bb27b1 | pbrook | int is_read;
|
189 | a1bb27b1 | pbrook | |
190 | a1bb27b1 | pbrook | is_read = (s->datactrl & PL181_DATA_DIRECTION) != 0;
|
191 | 6361cdb6 | pbrook | if (s->datacnt != 0 && (!is_read || sd_data_ready(s->card)) |
192 | 6361cdb6 | pbrook | && !s->linux_hack) { |
193 | a1bb27b1 | pbrook | limit = is_read ? PL181_FIFO_LEN : 0;
|
194 | a1bb27b1 | pbrook | n = 0;
|
195 | a1bb27b1 | pbrook | value = 0;
|
196 | a1bb27b1 | pbrook | while (s->datacnt && s->fifo_len != limit) {
|
197 | a1bb27b1 | pbrook | if (is_read) {
|
198 | a1bb27b1 | pbrook | value |= (uint32_t)sd_read_data(s->card) << (n * 8);
|
199 | a1bb27b1 | pbrook | n++; |
200 | a1bb27b1 | pbrook | if (n == 4) { |
201 | a1bb27b1 | pbrook | pl181_fifo_push(s, value); |
202 | a1bb27b1 | pbrook | value = 0;
|
203 | a1bb27b1 | pbrook | n = 0;
|
204 | a1bb27b1 | pbrook | } |
205 | a1bb27b1 | pbrook | } else {
|
206 | a1bb27b1 | pbrook | if (n == 0) { |
207 | a1bb27b1 | pbrook | value = pl181_fifo_pop(s); |
208 | a1bb27b1 | pbrook | n = 4;
|
209 | a1bb27b1 | pbrook | } |
210 | a1bb27b1 | pbrook | sd_write_data(s->card, value & 0xff);
|
211 | a1bb27b1 | pbrook | value >>= 8;
|
212 | a1bb27b1 | pbrook | n--; |
213 | a1bb27b1 | pbrook | } |
214 | a1bb27b1 | pbrook | s->datacnt--; |
215 | a1bb27b1 | pbrook | } |
216 | a1bb27b1 | pbrook | if (n && is_read) {
|
217 | a1bb27b1 | pbrook | pl181_fifo_push(s, value); |
218 | a1bb27b1 | pbrook | } |
219 | a1bb27b1 | pbrook | } |
220 | a1bb27b1 | pbrook | s->status &= ~(PL181_STATUS_RX_FIFO | PL181_STATUS_TX_FIFO); |
221 | a1bb27b1 | pbrook | if (s->datacnt == 0) { |
222 | a1bb27b1 | pbrook | s->status |= PL181_STATUS_DATAEND; |
223 | a1bb27b1 | pbrook | /* HACK: */
|
224 | a1bb27b1 | pbrook | s->status |= PL181_STATUS_DATABLOCKEND; |
225 | a1bb27b1 | pbrook | DPRINTF("Transfer Complete\n");
|
226 | a1bb27b1 | pbrook | } |
227 | 6361cdb6 | pbrook | if (s->datacnt == 0 && s->fifo_len == 0) { |
228 | a1bb27b1 | pbrook | s->datactrl &= ~PL181_DATA_ENABLE; |
229 | a1bb27b1 | pbrook | DPRINTF("Data engine idle\n");
|
230 | a1bb27b1 | pbrook | } else {
|
231 | a1bb27b1 | pbrook | /* Update FIFO bits. */
|
232 | a1bb27b1 | pbrook | bits = PL181_STATUS_TXACTIVE | PL181_STATUS_RXACTIVE; |
233 | a1bb27b1 | pbrook | if (s->fifo_len == 0) { |
234 | a1bb27b1 | pbrook | bits |= PL181_STATUS_TXFIFOEMPTY; |
235 | a1bb27b1 | pbrook | bits |= PL181_STATUS_RXFIFOEMPTY; |
236 | a1bb27b1 | pbrook | } else {
|
237 | a1bb27b1 | pbrook | bits |= PL181_STATUS_TXDATAAVLBL; |
238 | a1bb27b1 | pbrook | bits |= PL181_STATUS_RXDATAAVLBL; |
239 | a1bb27b1 | pbrook | } |
240 | a1bb27b1 | pbrook | if (s->fifo_len == 16) { |
241 | a1bb27b1 | pbrook | bits |= PL181_STATUS_TXFIFOFULL; |
242 | a1bb27b1 | pbrook | bits |= PL181_STATUS_RXFIFOFULL; |
243 | a1bb27b1 | pbrook | } |
244 | a1bb27b1 | pbrook | if (s->fifo_len <= 8) { |
245 | a1bb27b1 | pbrook | bits |= PL181_STATUS_TXFIFOHALFEMPTY; |
246 | a1bb27b1 | pbrook | } |
247 | a1bb27b1 | pbrook | if (s->fifo_len >= 8) { |
248 | a1bb27b1 | pbrook | bits |= PL181_STATUS_RXFIFOHALFFULL; |
249 | a1bb27b1 | pbrook | } |
250 | a1bb27b1 | pbrook | if (s->datactrl & PL181_DATA_DIRECTION) {
|
251 | a1bb27b1 | pbrook | bits &= PL181_STATUS_RX_FIFO; |
252 | a1bb27b1 | pbrook | } else {
|
253 | a1bb27b1 | pbrook | bits &= PL181_STATUS_TX_FIFO; |
254 | a1bb27b1 | pbrook | } |
255 | a1bb27b1 | pbrook | s->status |= bits; |
256 | a1bb27b1 | pbrook | } |
257 | a1bb27b1 | pbrook | } |
258 | a1bb27b1 | pbrook | |
259 | c227f099 | Anthony Liguori | static uint32_t pl181_read(void *opaque, target_phys_addr_t offset) |
260 | a1bb27b1 | pbrook | { |
261 | a1bb27b1 | pbrook | pl181_state *s = (pl181_state *)opaque; |
262 | 6361cdb6 | pbrook | uint32_t tmp; |
263 | a1bb27b1 | pbrook | |
264 | a1bb27b1 | pbrook | if (offset >= 0xfe0 && offset < 0x1000) { |
265 | a1bb27b1 | pbrook | return pl181_id[(offset - 0xfe0) >> 2]; |
266 | a1bb27b1 | pbrook | } |
267 | a1bb27b1 | pbrook | switch (offset) {
|
268 | a1bb27b1 | pbrook | case 0x00: /* Power */ |
269 | a1bb27b1 | pbrook | return s->power;
|
270 | a1bb27b1 | pbrook | case 0x04: /* Clock */ |
271 | a1bb27b1 | pbrook | return s->clock;
|
272 | a1bb27b1 | pbrook | case 0x08: /* Argument */ |
273 | a1bb27b1 | pbrook | return s->cmdarg;
|
274 | a1bb27b1 | pbrook | case 0x0c: /* Command */ |
275 | a1bb27b1 | pbrook | return s->cmd;
|
276 | a1bb27b1 | pbrook | case 0x10: /* RespCmd */ |
277 | a1bb27b1 | pbrook | return s->respcmd;
|
278 | a1bb27b1 | pbrook | case 0x14: /* Response0 */ |
279 | a1bb27b1 | pbrook | return s->response[0]; |
280 | a1bb27b1 | pbrook | case 0x18: /* Response1 */ |
281 | a1bb27b1 | pbrook | return s->response[1]; |
282 | a1bb27b1 | pbrook | case 0x1c: /* Response2 */ |
283 | a1bb27b1 | pbrook | return s->response[2]; |
284 | a1bb27b1 | pbrook | case 0x20: /* Response3 */ |
285 | a1bb27b1 | pbrook | return s->response[3]; |
286 | a1bb27b1 | pbrook | case 0x24: /* DataTimer */ |
287 | a1bb27b1 | pbrook | return s->datatimer;
|
288 | a1bb27b1 | pbrook | case 0x28: /* DataLength */ |
289 | a1bb27b1 | pbrook | return s->datalength;
|
290 | a1bb27b1 | pbrook | case 0x2c: /* DataCtrl */ |
291 | a1bb27b1 | pbrook | return s->datactrl;
|
292 | a1bb27b1 | pbrook | case 0x30: /* DataCnt */ |
293 | a1bb27b1 | pbrook | return s->datacnt;
|
294 | a1bb27b1 | pbrook | case 0x34: /* Status */ |
295 | 6361cdb6 | pbrook | tmp = s->status; |
296 | 6361cdb6 | pbrook | if (s->linux_hack) {
|
297 | 6361cdb6 | pbrook | s->linux_hack = 0;
|
298 | 6361cdb6 | pbrook | pl181_fifo_run(s); |
299 | 6361cdb6 | pbrook | pl181_update(s); |
300 | 6361cdb6 | pbrook | } |
301 | 6361cdb6 | pbrook | return tmp;
|
302 | a1bb27b1 | pbrook | case 0x3c: /* Mask0 */ |
303 | a1bb27b1 | pbrook | return s->mask[0]; |
304 | a1bb27b1 | pbrook | case 0x40: /* Mask1 */ |
305 | a1bb27b1 | pbrook | return s->mask[1]; |
306 | a1bb27b1 | pbrook | case 0x48: /* FifoCnt */ |
307 | 6361cdb6 | pbrook | /* The documentation is somewhat vague about exactly what FifoCnt
|
308 | 6361cdb6 | pbrook | does. On real hardware it appears to be when decrememnted
|
309 | 6361cdb6 | pbrook | when a word is transfered between the FIFO and the serial
|
310 | 6361cdb6 | pbrook | data engine. DataCnt is decremented after each byte is
|
311 | 6361cdb6 | pbrook | transfered between the serial engine and the card.
|
312 | 6361cdb6 | pbrook | We don't emulate this level of detail, so both can be the same. */
|
313 | 6361cdb6 | pbrook | tmp = (s->datacnt + 3) >> 2; |
314 | 6361cdb6 | pbrook | if (s->linux_hack) {
|
315 | 6361cdb6 | pbrook | s->linux_hack = 0;
|
316 | 6361cdb6 | pbrook | pl181_fifo_run(s); |
317 | 6361cdb6 | pbrook | pl181_update(s); |
318 | 6361cdb6 | pbrook | } |
319 | 6361cdb6 | pbrook | return tmp;
|
320 | a1bb27b1 | pbrook | case 0x80: case 0x84: case 0x88: case 0x8c: /* FifoData */ |
321 | a1bb27b1 | pbrook | case 0x90: case 0x94: case 0x98: case 0x9c: |
322 | a1bb27b1 | pbrook | case 0xa0: case 0xa4: case 0xa8: case 0xac: |
323 | a1bb27b1 | pbrook | case 0xb0: case 0xb4: case 0xb8: case 0xbc: |
324 | 6361cdb6 | pbrook | if (s->fifo_len == 0) { |
325 | a1bb27b1 | pbrook | fprintf(stderr, "pl181: Unexpected FIFO read\n");
|
326 | a1bb27b1 | pbrook | return 0; |
327 | a1bb27b1 | pbrook | } else {
|
328 | a1bb27b1 | pbrook | uint32_t value; |
329 | a1bb27b1 | pbrook | value = pl181_fifo_pop(s); |
330 | 6361cdb6 | pbrook | s->linux_hack = 1;
|
331 | a1bb27b1 | pbrook | pl181_fifo_run(s); |
332 | a1bb27b1 | pbrook | pl181_update(s); |
333 | a1bb27b1 | pbrook | return value;
|
334 | a1bb27b1 | pbrook | } |
335 | a1bb27b1 | pbrook | default:
|
336 | 2ac71179 | Paul Brook | hw_error("pl181_read: Bad offset %x\n", (int)offset); |
337 | a1bb27b1 | pbrook | return 0; |
338 | a1bb27b1 | pbrook | } |
339 | a1bb27b1 | pbrook | } |
340 | a1bb27b1 | pbrook | |
341 | c227f099 | Anthony Liguori | static void pl181_write(void *opaque, target_phys_addr_t offset, |
342 | a1bb27b1 | pbrook | uint32_t value) |
343 | a1bb27b1 | pbrook | { |
344 | a1bb27b1 | pbrook | pl181_state *s = (pl181_state *)opaque; |
345 | a1bb27b1 | pbrook | |
346 | a1bb27b1 | pbrook | switch (offset) {
|
347 | a1bb27b1 | pbrook | case 0x00: /* Power */ |
348 | a1bb27b1 | pbrook | s->power = value & 0xff;
|
349 | a1bb27b1 | pbrook | break;
|
350 | a1bb27b1 | pbrook | case 0x04: /* Clock */ |
351 | a1bb27b1 | pbrook | s->clock = value & 0xff;
|
352 | a1bb27b1 | pbrook | break;
|
353 | a1bb27b1 | pbrook | case 0x08: /* Argument */ |
354 | a1bb27b1 | pbrook | s->cmdarg = value; |
355 | a1bb27b1 | pbrook | break;
|
356 | a1bb27b1 | pbrook | case 0x0c: /* Command */ |
357 | a1bb27b1 | pbrook | s->cmd = value; |
358 | a1bb27b1 | pbrook | if (s->cmd & PL181_CMD_ENABLE) {
|
359 | a1bb27b1 | pbrook | if (s->cmd & PL181_CMD_INTERRUPT) {
|
360 | a1bb27b1 | pbrook | fprintf(stderr, "pl181: Interrupt mode not implemented\n");
|
361 | a1bb27b1 | pbrook | abort(); |
362 | a1bb27b1 | pbrook | } if (s->cmd & PL181_CMD_PENDING) {
|
363 | a1bb27b1 | pbrook | fprintf(stderr, "pl181: Pending commands not implemented\n");
|
364 | a1bb27b1 | pbrook | abort(); |
365 | a1bb27b1 | pbrook | } else {
|
366 | a1bb27b1 | pbrook | pl181_send_command(s); |
367 | a1bb27b1 | pbrook | pl181_fifo_run(s); |
368 | a1bb27b1 | pbrook | } |
369 | a1bb27b1 | pbrook | /* The command has completed one way or the other. */
|
370 | a1bb27b1 | pbrook | s->cmd &= ~PL181_CMD_ENABLE; |
371 | a1bb27b1 | pbrook | } |
372 | a1bb27b1 | pbrook | break;
|
373 | a1bb27b1 | pbrook | case 0x24: /* DataTimer */ |
374 | a1bb27b1 | pbrook | s->datatimer = value; |
375 | a1bb27b1 | pbrook | break;
|
376 | a1bb27b1 | pbrook | case 0x28: /* DataLength */ |
377 | a1bb27b1 | pbrook | s->datalength = value & 0xffff;
|
378 | a1bb27b1 | pbrook | break;
|
379 | a1bb27b1 | pbrook | case 0x2c: /* DataCtrl */ |
380 | a1bb27b1 | pbrook | s->datactrl = value & 0xff;
|
381 | a1bb27b1 | pbrook | if (value & PL181_DATA_ENABLE) {
|
382 | a1bb27b1 | pbrook | s->datacnt = s->datalength; |
383 | a1bb27b1 | pbrook | pl181_fifo_run(s); |
384 | a1bb27b1 | pbrook | } |
385 | a1bb27b1 | pbrook | break;
|
386 | a1bb27b1 | pbrook | case 0x38: /* Clear */ |
387 | a1bb27b1 | pbrook | s->status &= ~(value & 0x7ff);
|
388 | a1bb27b1 | pbrook | break;
|
389 | a1bb27b1 | pbrook | case 0x3c: /* Mask0 */ |
390 | a1bb27b1 | pbrook | s->mask[0] = value;
|
391 | a1bb27b1 | pbrook | break;
|
392 | a1bb27b1 | pbrook | case 0x40: /* Mask1 */ |
393 | a1bb27b1 | pbrook | s->mask[1] = value;
|
394 | a1bb27b1 | pbrook | break;
|
395 | a1bb27b1 | pbrook | case 0x80: case 0x84: case 0x88: case 0x8c: /* FifoData */ |
396 | a1bb27b1 | pbrook | case 0x90: case 0x94: case 0x98: case 0x9c: |
397 | a1bb27b1 | pbrook | case 0xa0: case 0xa4: case 0xa8: case 0xac: |
398 | a1bb27b1 | pbrook | case 0xb0: case 0xb4: case 0xb8: case 0xbc: |
399 | 6361cdb6 | pbrook | if (s->datacnt == 0) { |
400 | a1bb27b1 | pbrook | fprintf(stderr, "pl181: Unexpected FIFO write\n");
|
401 | a1bb27b1 | pbrook | } else {
|
402 | a1bb27b1 | pbrook | pl181_fifo_push(s, value); |
403 | a1bb27b1 | pbrook | pl181_fifo_run(s); |
404 | a1bb27b1 | pbrook | } |
405 | a1bb27b1 | pbrook | break;
|
406 | a1bb27b1 | pbrook | default:
|
407 | 2ac71179 | Paul Brook | hw_error("pl181_write: Bad offset %x\n", (int)offset); |
408 | a1bb27b1 | pbrook | } |
409 | a1bb27b1 | pbrook | pl181_update(s); |
410 | a1bb27b1 | pbrook | } |
411 | a1bb27b1 | pbrook | |
412 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const pl181_readfn[] = { |
413 | a1bb27b1 | pbrook | pl181_read, |
414 | a1bb27b1 | pbrook | pl181_read, |
415 | a1bb27b1 | pbrook | pl181_read |
416 | a1bb27b1 | pbrook | }; |
417 | a1bb27b1 | pbrook | |
418 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const pl181_writefn[] = { |
419 | a1bb27b1 | pbrook | pl181_write, |
420 | a1bb27b1 | pbrook | pl181_write, |
421 | a1bb27b1 | pbrook | pl181_write |
422 | a1bb27b1 | pbrook | }; |
423 | a1bb27b1 | pbrook | |
424 | a1bb27b1 | pbrook | static void pl181_reset(void *opaque) |
425 | a1bb27b1 | pbrook | { |
426 | a1bb27b1 | pbrook | pl181_state *s = (pl181_state *)opaque; |
427 | a1bb27b1 | pbrook | |
428 | a1bb27b1 | pbrook | s->power = 0;
|
429 | a1bb27b1 | pbrook | s->cmdarg = 0;
|
430 | a1bb27b1 | pbrook | s->cmd = 0;
|
431 | a1bb27b1 | pbrook | s->datatimer = 0;
|
432 | a1bb27b1 | pbrook | s->datalength = 0;
|
433 | a1bb27b1 | pbrook | s->respcmd = 0;
|
434 | a1bb27b1 | pbrook | s->response[0] = 0; |
435 | a1bb27b1 | pbrook | s->response[1] = 0; |
436 | a1bb27b1 | pbrook | s->response[2] = 0; |
437 | a1bb27b1 | pbrook | s->response[3] = 0; |
438 | a1bb27b1 | pbrook | s->datatimer = 0;
|
439 | a1bb27b1 | pbrook | s->datalength = 0;
|
440 | a1bb27b1 | pbrook | s->datactrl = 0;
|
441 | a1bb27b1 | pbrook | s->datacnt = 0;
|
442 | a1bb27b1 | pbrook | s->status = 0;
|
443 | 6361cdb6 | pbrook | s->linux_hack = 0;
|
444 | a1bb27b1 | pbrook | s->mask[0] = 0; |
445 | a1bb27b1 | pbrook | s->mask[1] = 0; |
446 | a1bb27b1 | pbrook | } |
447 | a1bb27b1 | pbrook | |
448 | 81a322d4 | Gerd Hoffmann | static int pl181_init(SysBusDevice *dev) |
449 | a1bb27b1 | pbrook | { |
450 | a1bb27b1 | pbrook | int iomemtype;
|
451 | aa9311d8 | Paul Brook | pl181_state *s = FROM_SYSBUS(pl181_state, dev); |
452 | aa9311d8 | Paul Brook | BlockDriverState *bd; |
453 | a1bb27b1 | pbrook | |
454 | 1eed09cb | Avi Kivity | iomemtype = cpu_register_io_memory(pl181_readfn, |
455 | a1bb27b1 | pbrook | pl181_writefn, s); |
456 | aa9311d8 | Paul Brook | sysbus_init_mmio(dev, 0x1000, iomemtype);
|
457 | aa9311d8 | Paul Brook | sysbus_init_irq(dev, &s->irq[0]);
|
458 | aa9311d8 | Paul Brook | sysbus_init_irq(dev, &s->irq[1]);
|
459 | aa9311d8 | Paul Brook | bd = qdev_init_bdrv(&dev->qdev, IF_SD); |
460 | 775616c3 | pbrook | s->card = sd_init(bd, 0);
|
461 | a08d4367 | Jan Kiszka | qemu_register_reset(pl181_reset, s); |
462 | a1bb27b1 | pbrook | pl181_reset(s); |
463 | a1bb27b1 | pbrook | /* ??? Save/restore. */
|
464 | 81a322d4 | Gerd Hoffmann | return 0; |
465 | a1bb27b1 | pbrook | } |
466 | aa9311d8 | Paul Brook | |
467 | aa9311d8 | Paul Brook | static void pl181_register_devices(void) |
468 | aa9311d8 | Paul Brook | { |
469 | aa9311d8 | Paul Brook | sysbus_register_dev("pl181", sizeof(pl181_state), pl181_init); |
470 | aa9311d8 | Paul Brook | } |
471 | aa9311d8 | Paul Brook | |
472 | aa9311d8 | Paul Brook | device_init(pl181_register_devices) |