2 * Arm PrimeCell PL181 MultiMedia Card Interface
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
13 //#define DEBUG_PL181 1
16 #define DPRINTF(fmt, args...) \
17 do { printf("pl181: " fmt , ##args); } while (0)
19 #define DPRINTF(fmt, args...) do {} while(0)
22 #define PL181_FIFO_LEN 16
42 uint32_t fifo
[PL181_FIFO_LEN
];
47 #define PL181_CMD_INDEX 0x3f
48 #define PL181_CMD_RESPONSE (1 << 6)
49 #define PL181_CMD_LONGRESP (1 << 7)
50 #define PL181_CMD_INTERRUPT (1 << 8)
51 #define PL181_CMD_PENDING (1 << 9)
52 #define PL181_CMD_ENABLE (1 << 10)
54 #define PL181_DATA_ENABLE (1 << 0)
55 #define PL181_DATA_DIRECTION (1 << 1)
56 #define PL181_DATA_MODE (1 << 2)
57 #define PL181_DATA_DMAENABLE (1 << 3)
59 #define PL181_STATUS_CMDCRCFAIL (1 << 0)
60 #define PL181_STATUS_DATACRCFAIL (1 << 1)
61 #define PL181_STATUS_CMDTIMEOUT (1 << 2)
62 #define PL181_STATUS_DATATIMEOUT (1 << 3)
63 #define PL181_STATUS_TXUNDERRUN (1 << 4)
64 #define PL181_STATUS_RXOVERRUN (1 << 5)
65 #define PL181_STATUS_CMDRESPEND (1 << 6)
66 #define PL181_STATUS_CMDSENT (1 << 7)
67 #define PL181_STATUS_DATAEND (1 << 8)
68 #define PL181_STATUS_DATABLOCKEND (1 << 10)
69 #define PL181_STATUS_CMDACTIVE (1 << 11)
70 #define PL181_STATUS_TXACTIVE (1 << 12)
71 #define PL181_STATUS_RXACTIVE (1 << 13)
72 #define PL181_STATUS_TXFIFOHALFEMPTY (1 << 14)
73 #define PL181_STATUS_RXFIFOHALFFULL (1 << 15)
74 #define PL181_STATUS_TXFIFOFULL (1 << 16)
75 #define PL181_STATUS_RXFIFOFULL (1 << 17)
76 #define PL181_STATUS_TXFIFOEMPTY (1 << 18)
77 #define PL181_STATUS_RXFIFOEMPTY (1 << 19)
78 #define PL181_STATUS_TXDATAAVLBL (1 << 20)
79 #define PL181_STATUS_RXDATAAVLBL (1 << 21)
81 #define PL181_STATUS_TX_FIFO (PL181_STATUS_TXACTIVE \
82 |PL181_STATUS_TXFIFOHALFEMPTY \
83 |PL181_STATUS_TXFIFOFULL \
84 |PL181_STATUS_TXFIFOEMPTY \
85 |PL181_STATUS_TXDATAAVLBL)
86 #define PL181_STATUS_RX_FIFO (PL181_STATUS_RXACTIVE \
87 |PL181_STATUS_RXFIFOHALFFULL \
88 |PL181_STATUS_RXFIFOFULL \
89 |PL181_STATUS_RXFIFOEMPTY \
90 |PL181_STATUS_RXDATAAVLBL)
92 static const unsigned char pl181_id
[] =
93 { 0x81, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
95 static void pl181_update(pl181_state
*s
)
98 for (i
= 0; i
< 2; i
++) {
99 pic_set_irq_new(s
->pic
, s
->irq
[i
], (s
->status
& s
->mask
[i
]) != 0);
103 static void pl181_fifo_push(pl181_state
*s
, uint32_t value
)
107 if (s
->fifo_len
== PL181_FIFO_LEN
) {
108 fprintf(stderr
, "pl181: FIFO overflow\n");
111 n
= (s
->fifo_pos
+ s
->fifo_len
) & (PL181_FIFO_LEN
- 1);
114 DPRINTF("FIFO push %08x\n", (int)value
);
117 static uint32_t pl181_fifo_pop(pl181_state
*s
)
121 if (s
->fifo_len
== 0) {
122 fprintf(stderr
, "pl181: FIFO underflow\n");
125 value
= s
->fifo
[s
->fifo_pos
];
127 s
->fifo_pos
= (s
->fifo_pos
+ 1) & (PL181_FIFO_LEN
- 1);
128 DPRINTF("FIFO pop %08x\n", (int)value
);
132 static void pl181_send_command(pl181_state
*s
)
134 struct sd_request_s request
;
135 uint8_t response
[16];
138 request
.cmd
= s
->cmd
& PL181_CMD_INDEX
;
139 request
.arg
= s
->cmdarg
;
140 DPRINTF("Command %d %08x\n", request
.cmd
, request
.arg
);
141 rlen
= sd_do_command(s
->card
, &request
, response
);
144 if (s
->cmd
& PL181_CMD_RESPONSE
) {
145 #define RWORD(n) ((response[n] << 24) | (response[n + 1] << 16) \
146 | (response[n + 2] << 8) | response[n + 3])
147 if (rlen
== 0 || (rlen
== 4 && (s
->cmd
& PL181_CMD_LONGRESP
)))
149 if (rlen
!= 4 && rlen
!= 16)
151 s
->response
[0] = RWORD(0);
153 s
->response
[1] = s
->response
[2] = s
->response
[3] = 0;
155 s
->response
[1] = RWORD(4);
156 s
->response
[2] = RWORD(8);
157 s
->response
[3] = RWORD(12) & ~1;
159 DPRINTF("Response recieved\n");
160 s
->status
|= PL181_STATUS_CMDRESPEND
;
163 DPRINTF("Command sent\n");
164 s
->status
|= PL181_STATUS_CMDSENT
;
169 DPRINTF("Timeout\n");
170 s
->status
|= PL181_STATUS_CMDTIMEOUT
;
173 /* Transfer data between teh card and the FIFO. This is complicated by
174 the FIFO holding 32-bit words and the card taking data in single byte
175 chunks. FIFO bytes are transferred in little-endian order. */
177 static void pl181_fifo_run(pl181_state
*s
)
185 is_read
= (s
->datactrl
& PL181_DATA_DIRECTION
) != 0;
186 if (s
->datacnt
!= 0 && (!is_read
|| sd_data_ready(s
->card
))) {
187 limit
= is_read
? PL181_FIFO_LEN
: 0;
190 while (s
->datacnt
&& s
->fifo_len
!= limit
) {
192 value
|= (uint32_t)sd_read_data(s
->card
) << (n
* 8);
195 pl181_fifo_push(s
, value
);
201 value
= pl181_fifo_pop(s
);
204 sd_write_data(s
->card
, value
& 0xff);
211 pl181_fifo_push(s
, value
);
214 s
->status
&= ~(PL181_STATUS_RX_FIFO
| PL181_STATUS_TX_FIFO
);
215 if (s
->datacnt
== 0) {
216 s
->status
|= PL181_STATUS_DATAEND
;
218 s
->status
|= PL181_STATUS_DATABLOCKEND
;
219 DPRINTF("Transfer Complete\n");
221 if (s
->datacnt
== 0 && s
->fifocnt
== 0) {
222 s
->datactrl
&= ~PL181_DATA_ENABLE
;
223 DPRINTF("Data engine idle\n");
225 /* Update FIFO bits. */
226 bits
= PL181_STATUS_TXACTIVE
| PL181_STATUS_RXACTIVE
;
227 if (s
->fifo_len
== 0) {
228 bits
|= PL181_STATUS_TXFIFOEMPTY
;
229 bits
|= PL181_STATUS_RXFIFOEMPTY
;
231 bits
|= PL181_STATUS_TXDATAAVLBL
;
232 bits
|= PL181_STATUS_RXDATAAVLBL
;
234 if (s
->fifo_len
== 16) {
235 bits
|= PL181_STATUS_TXFIFOFULL
;
236 bits
|= PL181_STATUS_RXFIFOFULL
;
238 if (s
->fifo_len
<= 8) {
239 bits
|= PL181_STATUS_TXFIFOHALFEMPTY
;
241 if (s
->fifo_len
>= 8) {
242 bits
|= PL181_STATUS_RXFIFOHALFFULL
;
244 if (s
->datactrl
& PL181_DATA_DIRECTION
) {
245 bits
&= PL181_STATUS_RX_FIFO
;
247 bits
&= PL181_STATUS_TX_FIFO
;
253 static uint32_t pl181_read(void *opaque
, target_phys_addr_t offset
)
255 pl181_state
*s
= (pl181_state
*)opaque
;
258 if (offset
>= 0xfe0 && offset
< 0x1000) {
259 return pl181_id
[(offset
- 0xfe0) >> 2];
262 case 0x00: /* Power */
264 case 0x04: /* Clock */
266 case 0x08: /* Argument */
268 case 0x0c: /* Command */
270 case 0x10: /* RespCmd */
272 case 0x14: /* Response0 */
273 return s
->response
[0];
274 case 0x18: /* Response1 */
275 return s
->response
[1];
276 case 0x1c: /* Response2 */
277 return s
->response
[2];
278 case 0x20: /* Response3 */
279 return s
->response
[3];
280 case 0x24: /* DataTimer */
282 case 0x28: /* DataLength */
283 return s
->datalength
;
284 case 0x2c: /* DataCtrl */
286 case 0x30: /* DataCnt */
288 case 0x34: /* Status */
290 case 0x3c: /* Mask0 */
292 case 0x40: /* Mask1 */
294 case 0x48: /* FifoCnt */
296 case 0x80: case 0x84: case 0x88: case 0x8c: /* FifoData */
297 case 0x90: case 0x94: case 0x98: case 0x9c:
298 case 0xa0: case 0xa4: case 0xa8: case 0xac:
299 case 0xb0: case 0xb4: case 0xb8: case 0xbc:
300 if (s
->fifocnt
== 0) {
301 fprintf(stderr
, "pl181: Unexpected FIFO read\n");
306 value
= pl181_fifo_pop(s
);
312 cpu_abort (cpu_single_env
, "pl181_read: Bad offset %x\n", offset
);
317 static void pl181_write(void *opaque
, target_phys_addr_t offset
,
320 pl181_state
*s
= (pl181_state
*)opaque
;
324 case 0x00: /* Power */
325 s
->power
= value
& 0xff;
327 case 0x04: /* Clock */
328 s
->clock
= value
& 0xff;
330 case 0x08: /* Argument */
333 case 0x0c: /* Command */
335 if (s
->cmd
& PL181_CMD_ENABLE
) {
336 if (s
->cmd
& PL181_CMD_INTERRUPT
) {
337 fprintf(stderr
, "pl181: Interrupt mode not implemented\n");
339 } if (s
->cmd
& PL181_CMD_PENDING
) {
340 fprintf(stderr
, "pl181: Pending commands not implemented\n");
343 pl181_send_command(s
);
346 /* The command has completed one way or the other. */
347 s
->cmd
&= ~PL181_CMD_ENABLE
;
350 case 0x24: /* DataTimer */
351 s
->datatimer
= value
;
353 case 0x28: /* DataLength */
354 s
->datalength
= value
& 0xffff;
356 case 0x2c: /* DataCtrl */
357 s
->datactrl
= value
& 0xff;
358 if (value
& PL181_DATA_ENABLE
) {
359 s
->datacnt
= s
->datalength
;
360 s
->fifocnt
= (s
->datalength
+ 3) >> 2;
364 case 0x38: /* Clear */
365 s
->status
&= ~(value
& 0x7ff);
367 case 0x3c: /* Mask0 */
370 case 0x40: /* Mask1 */
373 case 0x80: case 0x84: case 0x88: case 0x8c: /* FifoData */
374 case 0x90: case 0x94: case 0x98: case 0x9c:
375 case 0xa0: case 0xa4: case 0xa8: case 0xac:
376 case 0xb0: case 0xb4: case 0xb8: case 0xbc:
377 if (s
->fifocnt
== 0) {
378 fprintf(stderr
, "pl181: Unexpected FIFO write\n");
381 pl181_fifo_push(s
, value
);
386 cpu_abort (cpu_single_env
, "pl181_write: Bad offset %x\n", offset
);
391 static CPUReadMemoryFunc
*pl181_readfn
[] = {
397 static CPUWriteMemoryFunc
*pl181_writefn
[] = {
403 static void pl181_reset(void *opaque
)
405 pl181_state
*s
= (pl181_state
*)opaque
;
427 void pl181_init(uint32_t base
, BlockDriverState
*bd
,
428 void *pic
, int irq0
, int irq1
)
433 s
= (pl181_state
*)qemu_mallocz(sizeof(pl181_state
));
434 iomemtype
= cpu_register_io_memory(0, pl181_readfn
,
436 cpu_register_physical_memory(base
, 0x00000fff, iomemtype
);
438 s
->card
= sd_init(bd
);
442 qemu_register_reset(pl181_reset
, s
);
444 /* ??? Save/restore. */