root / hw / pl181.c @ 5228c2d3
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 | 87ecb68b | pbrook | #include "hw.h" |
11 | 87ecb68b | pbrook | #include "primecell.h" |
12 | a1bb27b1 | pbrook | #include "sd.h" |
13 | a1bb27b1 | pbrook | |
14 | a1bb27b1 | pbrook | //#define DEBUG_PL181 1
|
15 | a1bb27b1 | pbrook | |
16 | a1bb27b1 | pbrook | #ifdef DEBUG_PL181
|
17 | a1bb27b1 | pbrook | #define DPRINTF(fmt, args...) \
|
18 | a1bb27b1 | pbrook | do { printf("pl181: " fmt , ##args); } while (0) |
19 | a1bb27b1 | pbrook | #else
|
20 | a1bb27b1 | pbrook | #define DPRINTF(fmt, args...) 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 | 42a10898 | pbrook | SDState *card; |
27 | a1bb27b1 | pbrook | uint32_t base; |
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 | a1bb27b1 | pbrook | struct sd_request_s 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 | a1bb27b1 | pbrook | 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 | offset -= s->base; |
265 | a1bb27b1 | pbrook | if (offset >= 0xfe0 && offset < 0x1000) { |
266 | a1bb27b1 | pbrook | return pl181_id[(offset - 0xfe0) >> 2]; |
267 | a1bb27b1 | pbrook | } |
268 | a1bb27b1 | pbrook | switch (offset) {
|
269 | a1bb27b1 | pbrook | case 0x00: /* Power */ |
270 | a1bb27b1 | pbrook | return s->power;
|
271 | a1bb27b1 | pbrook | case 0x04: /* Clock */ |
272 | a1bb27b1 | pbrook | return s->clock;
|
273 | a1bb27b1 | pbrook | case 0x08: /* Argument */ |
274 | a1bb27b1 | pbrook | return s->cmdarg;
|
275 | a1bb27b1 | pbrook | case 0x0c: /* Command */ |
276 | a1bb27b1 | pbrook | return s->cmd;
|
277 | a1bb27b1 | pbrook | case 0x10: /* RespCmd */ |
278 | a1bb27b1 | pbrook | return s->respcmd;
|
279 | a1bb27b1 | pbrook | case 0x14: /* Response0 */ |
280 | a1bb27b1 | pbrook | return s->response[0]; |
281 | a1bb27b1 | pbrook | case 0x18: /* Response1 */ |
282 | a1bb27b1 | pbrook | return s->response[1]; |
283 | a1bb27b1 | pbrook | case 0x1c: /* Response2 */ |
284 | a1bb27b1 | pbrook | return s->response[2]; |
285 | a1bb27b1 | pbrook | case 0x20: /* Response3 */ |
286 | a1bb27b1 | pbrook | return s->response[3]; |
287 | a1bb27b1 | pbrook | case 0x24: /* DataTimer */ |
288 | a1bb27b1 | pbrook | return s->datatimer;
|
289 | a1bb27b1 | pbrook | case 0x28: /* DataLength */ |
290 | a1bb27b1 | pbrook | return s->datalength;
|
291 | a1bb27b1 | pbrook | case 0x2c: /* DataCtrl */ |
292 | a1bb27b1 | pbrook | return s->datactrl;
|
293 | a1bb27b1 | pbrook | case 0x30: /* DataCnt */ |
294 | a1bb27b1 | pbrook | return s->datacnt;
|
295 | a1bb27b1 | pbrook | case 0x34: /* Status */ |
296 | 6361cdb6 | pbrook | tmp = s->status; |
297 | 6361cdb6 | pbrook | if (s->linux_hack) {
|
298 | 6361cdb6 | pbrook | s->linux_hack = 0;
|
299 | 6361cdb6 | pbrook | pl181_fifo_run(s); |
300 | 6361cdb6 | pbrook | pl181_update(s); |
301 | 6361cdb6 | pbrook | } |
302 | 6361cdb6 | pbrook | return tmp;
|
303 | a1bb27b1 | pbrook | case 0x3c: /* Mask0 */ |
304 | a1bb27b1 | pbrook | return s->mask[0]; |
305 | a1bb27b1 | pbrook | case 0x40: /* Mask1 */ |
306 | a1bb27b1 | pbrook | return s->mask[1]; |
307 | a1bb27b1 | pbrook | case 0x48: /* FifoCnt */ |
308 | 6361cdb6 | pbrook | /* The documentation is somewhat vague about exactly what FifoCnt
|
309 | 6361cdb6 | pbrook | does. On real hardware it appears to be when decrememnted
|
310 | 6361cdb6 | pbrook | when a word is transfered between the FIFO and the serial
|
311 | 6361cdb6 | pbrook | data engine. DataCnt is decremented after each byte is
|
312 | 6361cdb6 | pbrook | transfered between the serial engine and the card.
|
313 | 6361cdb6 | pbrook | We don't emulate this level of detail, so both can be the same. */
|
314 | 6361cdb6 | pbrook | tmp = (s->datacnt + 3) >> 2; |
315 | 6361cdb6 | pbrook | if (s->linux_hack) {
|
316 | 6361cdb6 | pbrook | s->linux_hack = 0;
|
317 | 6361cdb6 | pbrook | pl181_fifo_run(s); |
318 | 6361cdb6 | pbrook | pl181_update(s); |
319 | 6361cdb6 | pbrook | } |
320 | 6361cdb6 | pbrook | return tmp;
|
321 | a1bb27b1 | pbrook | case 0x80: case 0x84: case 0x88: case 0x8c: /* FifoData */ |
322 | a1bb27b1 | pbrook | case 0x90: case 0x94: case 0x98: case 0x9c: |
323 | a1bb27b1 | pbrook | case 0xa0: case 0xa4: case 0xa8: case 0xac: |
324 | a1bb27b1 | pbrook | case 0xb0: case 0xb4: case 0xb8: case 0xbc: |
325 | 6361cdb6 | pbrook | if (s->fifo_len == 0) { |
326 | a1bb27b1 | pbrook | fprintf(stderr, "pl181: Unexpected FIFO read\n");
|
327 | a1bb27b1 | pbrook | return 0; |
328 | a1bb27b1 | pbrook | } else {
|
329 | a1bb27b1 | pbrook | uint32_t value; |
330 | a1bb27b1 | pbrook | value = pl181_fifo_pop(s); |
331 | 6361cdb6 | pbrook | s->linux_hack = 1;
|
332 | a1bb27b1 | pbrook | pl181_fifo_run(s); |
333 | a1bb27b1 | pbrook | pl181_update(s); |
334 | a1bb27b1 | pbrook | return value;
|
335 | a1bb27b1 | pbrook | } |
336 | a1bb27b1 | pbrook | default:
|
337 | 4d1165fa | pbrook | cpu_abort (cpu_single_env, "pl181_read: Bad offset %x\n", (int)offset); |
338 | a1bb27b1 | pbrook | return 0; |
339 | a1bb27b1 | pbrook | } |
340 | a1bb27b1 | pbrook | } |
341 | a1bb27b1 | pbrook | |
342 | a1bb27b1 | pbrook | static void pl181_write(void *opaque, target_phys_addr_t offset, |
343 | a1bb27b1 | pbrook | uint32_t value) |
344 | a1bb27b1 | pbrook | { |
345 | a1bb27b1 | pbrook | pl181_state *s = (pl181_state *)opaque; |
346 | a1bb27b1 | pbrook | |
347 | a1bb27b1 | pbrook | offset -= s->base; |
348 | a1bb27b1 | pbrook | switch (offset) {
|
349 | a1bb27b1 | pbrook | case 0x00: /* Power */ |
350 | a1bb27b1 | pbrook | s->power = value & 0xff;
|
351 | a1bb27b1 | pbrook | break;
|
352 | a1bb27b1 | pbrook | case 0x04: /* Clock */ |
353 | a1bb27b1 | pbrook | s->clock = value & 0xff;
|
354 | a1bb27b1 | pbrook | break;
|
355 | a1bb27b1 | pbrook | case 0x08: /* Argument */ |
356 | a1bb27b1 | pbrook | s->cmdarg = value; |
357 | a1bb27b1 | pbrook | break;
|
358 | a1bb27b1 | pbrook | case 0x0c: /* Command */ |
359 | a1bb27b1 | pbrook | s->cmd = value; |
360 | a1bb27b1 | pbrook | if (s->cmd & PL181_CMD_ENABLE) {
|
361 | a1bb27b1 | pbrook | if (s->cmd & PL181_CMD_INTERRUPT) {
|
362 | a1bb27b1 | pbrook | fprintf(stderr, "pl181: Interrupt mode not implemented\n");
|
363 | a1bb27b1 | pbrook | abort(); |
364 | a1bb27b1 | pbrook | } if (s->cmd & PL181_CMD_PENDING) {
|
365 | a1bb27b1 | pbrook | fprintf(stderr, "pl181: Pending commands not implemented\n");
|
366 | a1bb27b1 | pbrook | abort(); |
367 | a1bb27b1 | pbrook | } else {
|
368 | a1bb27b1 | pbrook | pl181_send_command(s); |
369 | a1bb27b1 | pbrook | pl181_fifo_run(s); |
370 | a1bb27b1 | pbrook | } |
371 | a1bb27b1 | pbrook | /* The command has completed one way or the other. */
|
372 | a1bb27b1 | pbrook | s->cmd &= ~PL181_CMD_ENABLE; |
373 | a1bb27b1 | pbrook | } |
374 | a1bb27b1 | pbrook | break;
|
375 | a1bb27b1 | pbrook | case 0x24: /* DataTimer */ |
376 | a1bb27b1 | pbrook | s->datatimer = value; |
377 | a1bb27b1 | pbrook | break;
|
378 | a1bb27b1 | pbrook | case 0x28: /* DataLength */ |
379 | a1bb27b1 | pbrook | s->datalength = value & 0xffff;
|
380 | a1bb27b1 | pbrook | break;
|
381 | a1bb27b1 | pbrook | case 0x2c: /* DataCtrl */ |
382 | a1bb27b1 | pbrook | s->datactrl = value & 0xff;
|
383 | a1bb27b1 | pbrook | if (value & PL181_DATA_ENABLE) {
|
384 | a1bb27b1 | pbrook | s->datacnt = s->datalength; |
385 | a1bb27b1 | pbrook | pl181_fifo_run(s); |
386 | a1bb27b1 | pbrook | } |
387 | a1bb27b1 | pbrook | break;
|
388 | a1bb27b1 | pbrook | case 0x38: /* Clear */ |
389 | a1bb27b1 | pbrook | s->status &= ~(value & 0x7ff);
|
390 | a1bb27b1 | pbrook | break;
|
391 | a1bb27b1 | pbrook | case 0x3c: /* Mask0 */ |
392 | a1bb27b1 | pbrook | s->mask[0] = value;
|
393 | a1bb27b1 | pbrook | break;
|
394 | a1bb27b1 | pbrook | case 0x40: /* Mask1 */ |
395 | a1bb27b1 | pbrook | s->mask[1] = value;
|
396 | a1bb27b1 | pbrook | break;
|
397 | a1bb27b1 | pbrook | case 0x80: case 0x84: case 0x88: case 0x8c: /* FifoData */ |
398 | a1bb27b1 | pbrook | case 0x90: case 0x94: case 0x98: case 0x9c: |
399 | a1bb27b1 | pbrook | case 0xa0: case 0xa4: case 0xa8: case 0xac: |
400 | a1bb27b1 | pbrook | case 0xb0: case 0xb4: case 0xb8: case 0xbc: |
401 | 6361cdb6 | pbrook | if (s->datacnt == 0) { |
402 | a1bb27b1 | pbrook | fprintf(stderr, "pl181: Unexpected FIFO write\n");
|
403 | a1bb27b1 | pbrook | } else {
|
404 | a1bb27b1 | pbrook | pl181_fifo_push(s, value); |
405 | a1bb27b1 | pbrook | pl181_fifo_run(s); |
406 | a1bb27b1 | pbrook | } |
407 | a1bb27b1 | pbrook | break;
|
408 | a1bb27b1 | pbrook | default:
|
409 | 4d1165fa | pbrook | cpu_abort (cpu_single_env, "pl181_write: Bad offset %x\n", (int)offset); |
410 | a1bb27b1 | pbrook | } |
411 | a1bb27b1 | pbrook | pl181_update(s); |
412 | a1bb27b1 | pbrook | } |
413 | a1bb27b1 | pbrook | |
414 | a1bb27b1 | pbrook | static CPUReadMemoryFunc *pl181_readfn[] = {
|
415 | a1bb27b1 | pbrook | pl181_read, |
416 | a1bb27b1 | pbrook | pl181_read, |
417 | a1bb27b1 | pbrook | pl181_read |
418 | a1bb27b1 | pbrook | }; |
419 | a1bb27b1 | pbrook | |
420 | a1bb27b1 | pbrook | static CPUWriteMemoryFunc *pl181_writefn[] = {
|
421 | a1bb27b1 | pbrook | pl181_write, |
422 | a1bb27b1 | pbrook | pl181_write, |
423 | a1bb27b1 | pbrook | pl181_write |
424 | a1bb27b1 | pbrook | }; |
425 | a1bb27b1 | pbrook | |
426 | a1bb27b1 | pbrook | static void pl181_reset(void *opaque) |
427 | a1bb27b1 | pbrook | { |
428 | a1bb27b1 | pbrook | pl181_state *s = (pl181_state *)opaque; |
429 | a1bb27b1 | pbrook | |
430 | a1bb27b1 | pbrook | s->power = 0;
|
431 | a1bb27b1 | pbrook | s->cmdarg = 0;
|
432 | a1bb27b1 | pbrook | s->cmd = 0;
|
433 | a1bb27b1 | pbrook | s->datatimer = 0;
|
434 | a1bb27b1 | pbrook | s->datalength = 0;
|
435 | a1bb27b1 | pbrook | s->respcmd = 0;
|
436 | a1bb27b1 | pbrook | s->response[0] = 0; |
437 | a1bb27b1 | pbrook | s->response[1] = 0; |
438 | a1bb27b1 | pbrook | s->response[2] = 0; |
439 | a1bb27b1 | pbrook | s->response[3] = 0; |
440 | a1bb27b1 | pbrook | s->datatimer = 0;
|
441 | a1bb27b1 | pbrook | s->datalength = 0;
|
442 | a1bb27b1 | pbrook | s->datactrl = 0;
|
443 | a1bb27b1 | pbrook | s->datacnt = 0;
|
444 | a1bb27b1 | pbrook | s->status = 0;
|
445 | 6361cdb6 | pbrook | s->linux_hack = 0;
|
446 | a1bb27b1 | pbrook | s->mask[0] = 0; |
447 | a1bb27b1 | pbrook | s->mask[1] = 0; |
448 | a1bb27b1 | pbrook | } |
449 | a1bb27b1 | pbrook | |
450 | a1bb27b1 | pbrook | void pl181_init(uint32_t base, BlockDriverState *bd,
|
451 | d537cf6c | pbrook | qemu_irq irq0, qemu_irq irq1) |
452 | a1bb27b1 | pbrook | { |
453 | a1bb27b1 | pbrook | int iomemtype;
|
454 | a1bb27b1 | pbrook | pl181_state *s; |
455 | a1bb27b1 | pbrook | |
456 | a1bb27b1 | pbrook | s = (pl181_state *)qemu_mallocz(sizeof(pl181_state));
|
457 | a1bb27b1 | pbrook | iomemtype = cpu_register_io_memory(0, pl181_readfn,
|
458 | a1bb27b1 | pbrook | pl181_writefn, s); |
459 | 187337f8 | pbrook | cpu_register_physical_memory(base, 0x00001000, iomemtype);
|
460 | a1bb27b1 | pbrook | s->base = base; |
461 | 775616c3 | pbrook | s->card = sd_init(bd, 0);
|
462 | a1bb27b1 | pbrook | s->irq[0] = irq0;
|
463 | a1bb27b1 | pbrook | s->irq[1] = irq1;
|
464 | a1bb27b1 | pbrook | qemu_register_reset(pl181_reset, s); |
465 | a1bb27b1 | pbrook | pl181_reset(s); |
466 | a1bb27b1 | pbrook | /* ??? Save/restore. */
|
467 | a1bb27b1 | pbrook | } |