1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Daniel Ankers
11 * Copyright © 2008-2009 Rafaël Carré
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "config.h" /* for HAVE_MULTIVOLUME */
34 #include "pl081.h" /* DMA controller */
35 #include "dma-target.h" /* DMA request lines */
36 #include "clock-target.h"
39 #include "ata_idle_notify.h"
47 #define MCI_NO_RESP (0<<0)
48 #define MCI_RESP (1<<0)
49 #define MCI_LONG_RESP (1<<1)
51 /* controller registers */
52 #define SD_BASE 0xC6070000
54 #define SD_REG(x) (*(volatile unsigned long *) (SD_BASE+x))
56 #define MCI_CTRL SD_REG(0x00)
59 #define CTRL_RESET (1<<0)
60 #define FIFO_RESET (1<<1)
61 #define DMA_RESET (1<<2)
62 #define INT_ENABLE (1<<4)
63 #define DMA_ENABLE (1<<5)
64 #define READ_WAIT (1<<6)
65 #define SEND_IRQ_RESP (1<<7)
66 #define ABRT_READ_DATA (1<<8)
67 #define SEND_CCSD (1<<9)
68 #define SEND_AS_CCSD (1<<10)
69 #define EN_OD_PULLUP (1<<24)
72 #define MCI_PWREN SD_REG(0x04) /* power enable */
73 #define MCI_CLKDIV SD_REG(0x08) /* clock divider */
74 #define MCI_CLKSRC SD_REG(0x0C) /* clock source */
75 #define MCI_CLKENA SD_REG(0x10) /* clock enable */
76 #define MCI_TMOUT SD_REG(0x14) /* timeout */
78 #define MCI_CTYPE SD_REG(0x18) /* card type */
79 /* 1 bit per card, set = wide bus */
81 #define MCI_BLKSIZ SD_REG(0x1C) /* block size */
82 #define MCI_BYTCNT SD_REG(0x20) /* byte count */
83 #define MCI_MASK SD_REG(0x24) /* interrupt mask */
85 #define MCI_ARGUMENT SD_REG(0x28)
86 #define MCI_COMMAND SD_REG(0x2C)
88 /* command bits (bits 5:0 are the command index) */
89 #define CMD_RESP_EXP_BIT (1<<6)
90 #define CMD_RESP_LENGTH_BIT (1<<7)
91 #define CMD_CHECK_CRC_BIT (1<<8)
92 #define CMD_DATA_EXP_BIT (1<<9)
93 #define CMD_RW_BIT (1<<10)
94 #define CMD_TRANSMODE_BIT (1<<11)
95 #define CMD_SENT_AUTO_STOP_BIT (1<<12)
96 #define CMD_WAIT_PRV_DAT_BIT (1<<13)
97 #define CMD_ABRT_CMD_BIT (1<<14)
98 #define CMD_SEND_INIT_BIT (1<<15)
99 #define CMD_SEND_CLK_ONLY (1<<21)
100 #define CMD_READ_CEATA (1<<22)
101 #define CMD_CCS_EXPECTED (1<<23)
102 #define CMD_DONE_BIT (1<<31)
105 #define MCI_RESP0 SD_REG(0x30)
106 #define MCI_RESP1 SD_REG(0x34)
107 #define MCI_RESP2 SD_REG(0x38)
108 #define MCI_RESP3 SD_REG(0x3C)
110 #define MCI_MASK_STATUS SD_REG(0x40) /* masked interrupt status */
111 #define MCI_RAW_STATUS SD_REG(0x44) /* raw interrupt status, also used as
113 #define MCI_STATUS SD_REG(0x48)
117 * & 0xBA80 = MCI_INT_DCRC | MCI_INT_DRTO | MCI_INT_FRUN | \
118 * MCI_INT_HLE | MCI_INT_SBE | MCI_INT_EBE
120 * & 0x428 = MCI_INT_DTO | MCI_INT_RXDR | MCI_INT_HTO
121 * & 0x418 = MCI_INT_DTO | MCI_INT_TXDR | MCI_INT_HTO
125 #define MCI_INT_CRDDET (1<<0) /* card detect */
126 #define MCI_INT_RE (1<<1) /* response error */
127 #define MCI_INT_CD (1<<2) /* command done */
128 #define MCI_INT_DTO (1<<3) /* data transfer over */
129 #define MCI_INT_TXDR (1<<4) /* tx fifo data request */
130 #define MCI_INT_RXDR (1<<5) /* rx fifo data request */
131 #define MCI_INT_RCRC (1<<6) /* response crc error */
132 #define MCI_INT_DCRC (1<<7) /* data crc error */
133 #define MCI_INT_RTO (1<<8) /* response timeout */
134 #define MCI_INT_DRTO (1<<9) /* data read timeout */
135 #define MCI_INT_HTO (1<<10) /* data starv timeout */
136 #define MCI_INT_FRUN (1<<11) /* fifo over/underrun */
137 #define MCI_INT_HLE (1<<12) /* hw locked while error */
138 #define MCI_INT_SBE (1<<13) /* start bit error */
139 #define MCI_INT_ACD (1<<14) /* auto command done */
140 #define MCI_INT_EBE (1<<15) /* end bit error */
141 #define MCI_INT_SDIO (0xf<<16)
143 #define MCI_ERROR (MCI_INT_RE | MCI_INT_RCRC | MCI_INT_DCRC /*| MCI_INT_RTO*/ \
144 | MCI_INT_DRTO | MCI_INT_HTO | MCI_INT_FRUN | MCI_INT_HLE \
145 | MCI_INT_SBE | MCI_INT_EBE)
147 #define MCI_FIFOTH SD_REG(0x4C) /* FIFO threshold */
148 /* TX watermark : bits 11:0
149 * RX watermark : bits 27:16
150 * DMA MTRANS SIZE : bits 30:28
151 * bits 31, 15:12 : unused
153 #define MCI_FIFOTH_MASK 0x8000f000
155 #define MCI_CDETECT SD_REG(0x50) /* card detect */
156 #define MCI_WRTPRT SD_REG(0x54) /* write protect */
157 #define MCI_GPIO SD_REG(0x58)
158 #define MCI_TCBCNT SD_REG(0x5C) /* transferred CIU byte count */
159 #define MCI_TBBCNT SD_REG(0x60) /* transferred host/DMA to/from bytes */
160 #define MCI_DEBNCE SD_REG(0x64) /* card detect debounce */
161 #define MCI_USRID SD_REG(0x68) /* user id */
162 #define MCI_VERID SD_REG(0x6C) /* version id */
164 #define MCI_HCON SD_REG(0x70) /* hardware config */
166 * bits 5:1 : maximum card index */
168 #define MCI_BMOD SD_REG(0x80) /* bus mode */
169 #define MCI_PLDMND SD_REG(0x84) /* poll demand */
170 #define MCI_DBADDR SD_REG(0x88) /* descriptor base address */
171 #define MCI_IDSTS SD_REG(0x8C) /* internal DMAC status */
172 #define MCI_IDINTEN SD_REG(0x90) /* internal DMAC interrupt enable */
173 #define MCI_DSCADDR SD_REG(0x94) /* current host descriptor address */
174 #define MCI_BUFADDR SD_REG(0x98) /* current host buffer address */
176 #define MCI_FIFO ((unsigned long *) (SD_BASE+0x100))
178 #define UNALIGNED_NUM_SECTORS 10
179 static unsigned char aligned_buffer
[UNALIGNED_NUM_SECTORS
* SD_BLOCK_SIZE
] __attribute__((aligned(32))); /* align on cache line size */
180 static unsigned char *uncached_buffer
= UNCACHED_ADDR(&aligned_buffer
[0]);
182 static int sd_init_card(void);
183 static void init_controller(void);
185 static tCardInfo card_info
;
187 /* for compatibility */
188 static long last_disk_activity
= -1;
190 #define MIN_YIELD_PERIOD 5 /* ticks */
191 static long next_yield
= 0;
193 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x200)/sizeof(long)];
194 static const char sd_thread_name
[] = "ata/sd";
195 static struct mutex sd_mtx SHAREDBSS_ATTR
;
196 static struct event_queue sd_queue
;
198 bool sd_enabled
= false;
201 static struct wakeup transfer_completion_signal
;
202 static volatile bool retry
;
203 static volatile bool data_transfer
= false;
205 static inline void mci_delay(void) { int i
= 0xffff; while(i
--) ; }
209 MCI_CTRL
&= ~INT_ENABLE
;
210 const int status
= MCI_MASK_STATUS
;
212 MCI_RAW_STATUS
= status
; /* clear status */
214 if(status
& MCI_ERROR
)
217 if(data_transfer
&& status
& (MCI_INT_DTO
|MCI_ERROR
))
218 wakeup_signal(&transfer_completion_signal
);
220 MCI_CTRL
|= INT_ENABLE
;
223 static bool send_cmd(const int cmd
, const int arg
, const int flags
,
224 unsigned long *response
)
230 MCI_COMMAND
|= CMD_RESP_EXP_BIT
;
231 if(flags
& MCI_LONG_RESP
)
232 MCI_COMMAND
|= CMD_RESP_LENGTH_BIT
;
235 if(cmd
== SD_READ_MULTIPLE_BLOCK
|| cmd
== SD_WRITE_MULTIPLE_BLOCK
)
237 MCI_COMMAND
|= CMD_WAIT_PRV_DAT_BIT
| CMD_DATA_EXP_BIT
;
238 if(cmd
== SD_WRITE_MULTIPLE_BLOCK
)
239 MCI_COMMAND
|= CMD_RW_BIT
| CMD_CHECK_CRC_BIT
;
242 int clkena
= MCI_CLKENA
;
246 MCI_COMMAND
|= CMD_DONE_BIT
;
249 while(MCI_COMMAND
& CMD_DONE_BIT
)
251 if(--max
== 0) /* timeout */
262 if(flags
& MCI_LONG_RESP
)
264 /* store the response in little endian order for the words */
265 response
[0] = MCI_RESP3
;
266 response
[1] = MCI_RESP2
;
267 response
[2] = MCI_RESP1
;
268 response
[3] = MCI_RESP0
;
271 response
[0] = MCI_RESP0
;
276 static int sd_init_card(void)
278 unsigned long response
;
279 unsigned long temp_reg
[4];
280 int max_tries
= 100; /* max acmd41 attemps */
284 if(!send_cmd(SD_GO_IDLE_STATE
, 0, MCI_NO_RESP
, NULL
))
290 if(send_cmd(SD_SEND_IF_COND
, 0x1AA, MCI_RESP
, &response
))
291 if((response
& 0xFFF) == 0x1AA)
295 /* some MicroSD cards seems to need more delays, so play safe */
301 if( !send_cmd(SD_APP_CMD
, 0, MCI_RESP
, &response
) ||
302 !(response
& (1<<5)))
308 if(!send_cmd(SD_APP_OP_COND
, (sd_v2
? 0x40FF8000 : (1<<23)),
309 MCI_RESP
, &card_info
.ocr
))
311 } while(!(card_info
.ocr
& (1<<31)) && max_tries
--);
321 if(!send_cmd(SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
, card_info
.cid
))
325 if(!send_cmd(SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
, &card_info
.rca
))
329 if(!send_cmd(SD_SEND_CSD
, card_info
.rca
,
330 MCI_RESP
|MCI_LONG_RESP
, temp_reg
))
334 card_info
.csd
[3-i
] = temp_reg
[i
];
336 sd_parse_csd(&card_info
);
338 if(!send_cmd(SD_APP_CMD
, 0, MCI_RESP
, &response
) ||
339 !send_cmd(42, 0, MCI_NO_RESP
, NULL
)) /* disconnect the 50 KOhm pull-up
340 resistor on CD/DAT3 */
343 if(!send_cmd(SD_APP_CMD
, card_info
.rca
, MCI_NO_RESP
, NULL
))
346 if(!send_cmd(SD_SET_BUS_WIDTH
, card_info
.rca
| 2, MCI_NO_RESP
, NULL
))
349 if(!send_cmd(SD_SELECT_CARD
, card_info
.rca
, MCI_NO_RESP
, NULL
))
352 /* not sent in init_card() by OF */
353 if(!send_cmd(SD_SET_BLOCKLEN
, card_info
.blocksize
, MCI_NO_RESP
,
357 card_info
.initialized
= 1;
362 static void sd_thread(void) __attribute__((noreturn
));
363 static void sd_thread(void)
365 struct queue_event ev
;
366 bool idle_notified
= false;
370 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
375 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
377 idle_notified
= false;
381 /* never let a timer wrap confuse us */
382 next_yield
= current_tick
;
386 call_storage_idle_notifys(false);
387 idle_notified
= true;
392 case SYS_USB_CONNECTED
:
393 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
394 /* Wait until the USB cable is extracted again */
395 usb_wait_for_disconnect(&sd_queue
);
398 case SYS_USB_DISCONNECTED
:
399 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
406 static void init_controller(void)
408 int idx
= (MCI_HCON
>> 1) & 31;
409 int idx_bits
= (1 << idx
) -1;
411 MCI_PWREN
&= ~idx_bits
;
412 MCI_PWREN
= idx_bits
;
419 MCI_CTRL
|= CTRL_RESET
;
420 while(MCI_CTRL
& CTRL_RESET
)
423 MCI_RAW_STATUS
= 0xffffffff;
425 MCI_TMOUT
= 0xffffffff;
429 MCI_CLKENA
= idx_bits
;
432 MCI_COMMAND
= CMD_DONE_BIT
|CMD_SEND_CLK_ONLY
|CMD_WAIT_PRV_DAT_BIT
;
433 while(MCI_COMMAND
& CMD_DONE_BIT
)
436 MCI_DEBNCE
= 0xfffff; /* default value */
438 MCI_FIFOTH
&= MCI_FIFOTH_MASK
;
439 MCI_FIFOTH
|= 0x503f0080;
441 MCI_MASK
= 0xffffffff & ~(MCI_INT_ACD
|MCI_INT_CRDDET
);
443 MCI_CTRL
|= INT_ENABLE
;
449 CGU_PERI
|= CGU_MCI_CLOCK_ENABLE
;
451 CGU_IDE
= (1<<7) /* AHB interface enable */ |
452 (1<<6) /* interface enable */ |
453 ((CLK_DIV(AS3525_PLLA_FREQ
, AS3525_IDE_FREQ
) - 1) << 2) |
454 1; /* clock source = PLLA */
456 CGU_MEMSTICK
= (1<<8) | (1<<7) |
457 ((CLK_DIV(AS3525_PLLA_FREQ
, AS3525_MS_FREQ
) -1) << 2) | 1;
459 /* FIXME: divider should be shifted by 2, but doing prevents card
461 *(volatile int*)(CGU_BASE
+0x3C) = (1<<7) |
462 (CLK_DIV(AS3525_PLLA_FREQ
, 24000000) -1) | 1;
464 wakeup_init(&transfer_completion_signal
);
466 VIC_INT_ENABLE
|= INTERRUPT_NAND
;
469 ret
= sd_init_card();
476 queue_init(&sd_queue
, true);
477 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
478 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
) IF_COP(, CPU
));
487 #ifdef STORAGE_GET_INFO
488 void sd_get_info(struct storage_info
*info
)
490 info
->sector_size
=card_info
.blocksize
;
491 info
->num_sectors
=card_info
.numblocks
;
492 info
->vendor
="Rockbox";
493 info
->product
= "Internal Storage";
494 info
->revision
="0.00";
498 static int sd_wait_for_state(unsigned int state
)
500 unsigned long response
;
501 unsigned int timeout
= 100; /* ticks */
502 long t
= current_tick
;
508 if(!send_cmd(SD_SEND_STATUS
, card_info
.rca
,
509 MCI_RESP
, &response
))
512 if (((response
>> 9) & 0xf) == state
)
515 if(TIME_AFTER(current_tick
, t
+ timeout
))
516 return -10 * ((response
>> 9) & 0xf);
518 if (TIME_AFTER((tick
= current_tick
), next_yield
))
521 timeout
+= current_tick
- tick
;
522 next_yield
= tick
+ MIN_YIELD_PERIOD
;
527 static int sd_transfer_sectors(unsigned long start
, int count
, void* buf
, bool write
)
531 /* skip SanDisk OF */
539 if (card_info
.initialized
<= 0)
541 ret
= sd_init_card();
542 if (!(card_info
.initialized
))
544 panicf("card not initialised (%d)", ret
);
545 goto sd_transfer_error
;
549 last_disk_activity
= current_tick
;
550 ret
= sd_wait_for_state(SD_TRAN
);
553 static const char *st
[9] = {
554 "IDLE", "RDY", "IDENT", "STBY", "TRAN", "DATA", "RCV", "PRG", "DIS"
557 panicf("wait for state failed (%s)", st
[(-ret
/ 10) % 9]);
559 panicf("wait for state failed");
560 goto sd_transfer_error
;
565 const int cmd
= write
? SD_WRITE_MULTIPLE_BLOCK
: SD_READ_MULTIPLE_BLOCK
;
567 /* Interrupt handler might set this to true during transfer */
570 void *dma_buf
= aligned_buffer
;
571 unsigned int transfer
= count
;
572 if(transfer
> UNALIGNED_NUM_SECTORS
)
573 transfer
= UNALIGNED_NUM_SECTORS
;
576 memcpy(uncached_buffer
, buf
, transfer
* SD_BLOCK_SIZE
);
580 MCI_BLKSIZ
= SD_BLOCK_SIZE
;
581 MCI_BYTCNT
= transfer
* SD_BLOCK_SIZE
;
583 MCI_CTRL
|= (FIFO_RESET
|DMA_RESET
);
584 while(MCI_CTRL
& (FIFO_RESET
|DMA_RESET
))
587 MCI_CTRL
|= DMA_ENABLE
;
588 MCI_MASK
= MCI_INT_CD
|MCI_INT_DTO
|MCI_INT_DCRC
|MCI_INT_DRTO
| \
589 MCI_INT_HTO
|MCI_INT_FRUN
|MCI_INT_HLE
|MCI_INT_SBE
|MCI_INT_EBE
;
591 MCI_FIFOTH
&= MCI_FIFOTH_MASK
;
592 MCI_FIFOTH
|= 0x503f0080;
595 if(card_info
.ocr
& (1<<30) ) /* SDHC */
596 ret
= send_cmd(cmd
, start
, MCI_NO_RESP
, NULL
);
598 ret
= send_cmd(cmd
, start
* SD_BLOCK_SIZE
,
602 panicf("transfer multiple blocks failed (%d)", ret
);
605 dma_enable_channel(0, dma_buf
, MCI_FIFO
, DMA_PERI_SD
,
606 DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
, NULL
);
608 dma_enable_channel(0, MCI_FIFO
, dma_buf
, DMA_PERI_SD
,
609 DMAC_FLOWCTRL_PERI_PERI_TO_MEM
, false, true, 0, DMA_S8
, NULL
);
611 data_transfer
= true;
612 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
613 data_transfer
= false;
615 last_disk_activity
= current_tick
;
617 if(!send_cmd(SD_STOP_TRANSMISSION
, 0, MCI_NO_RESP
, NULL
))
620 panicf("STOP TRANSMISSION failed");
621 goto sd_transfer_error
;
624 ret
= sd_wait_for_state(SD_TRAN
);
627 panicf(" wait for state TRAN failed (%d)", ret
);
628 goto sd_transfer_error
;
634 memcpy(buf
, uncached_buffer
, transfer
* SD_BLOCK_SIZE
);
635 buf
+= transfer
* SD_BLOCK_SIZE
;
639 } while(retry
|| count
);
646 mutex_unlock(&sd_mtx
);
650 panicf("transfer error : %d",ret
);
651 card_info
.initialized
= 0;
655 int sd_read_sectors(unsigned long start
, int count
, void* buf
)
657 return sd_transfer_sectors(start
, count
, buf
, false);
660 int sd_write_sectors(unsigned long start
, int count
, const void* buf
)
662 #if 1 /* disabled until stable*/ \
663 || defined(BOOTLOADER) /* we don't need write support in bootloader */
669 return sd_transfer_sectors(start
, count
, (void*)buf
, true);
674 long sd_last_disk_activity(void)
676 return last_disk_activity
;
679 void sd_enable(bool on
)
686 tCardInfo
*card_get_info_target(int card_no
)
692 #endif /* BOOTLOADER */