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"
48 static void printf(const char *format
, ...)
55 len
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
58 lcd_puts(0, line
++, buf
);
60 if(line
>= LCD_HEIGHT
/SYSFONT_HEIGHT
)
65 #define MCI_NO_RESP (0<<0)
66 #define MCI_RESP (1<<0)
67 #define MCI_LONG_RESP (1<<1)
69 /* controller registers */
70 #define SD_BASE 0xC6070000
72 #define SD_REG(x) (*(volatile unsigned long *) (SD_BASE+x))
74 #define MCI_CTRL SD_REG(0x00)
77 #define CTRL_RESET (1<<0)
78 #define FIFO_RESET (1<<1)
79 #define DMA_RESET (1<<2)
80 #define INT_ENABLE (1<<4)
81 #define DMA_ENABLE (1<<5)
82 #define READ_WAIT (1<<6)
83 #define SEND_IRQ_RESP (1<<7)
84 #define ABRT_READ_DATA (1<<8)
85 #define SEND_CCSD (1<<9)
86 #define SEND_AS_CCSD (1<<10)
87 #define EN_OD_PULLUP (1<<24)
90 #define MCI_PWREN SD_REG(0x04) /* power enable */
91 #define MCI_CLKDIV SD_REG(0x08) /* clock divider */
92 #define MCI_CLKSRC SD_REG(0x0C) /* clock source */
93 #define MCI_CLKENA SD_REG(0x10) /* clock enable */
94 #define MCI_TMOUT SD_REG(0x14) /* timeout */
96 #define MCI_CTYPE SD_REG(0x18) /* card type */
97 /* 1 bit per card, set = wide bus */
99 #define MCI_BLKSIZ SD_REG(0x1C) /* block size */
100 #define MCI_BYTCNT SD_REG(0x20) /* byte count */
101 #define MCI_MASK SD_REG(0x24) /* interrupt mask */
103 #define MCI_ARGUMENT SD_REG(0x28)
104 #define MCI_COMMAND SD_REG(0x2C)
106 /* command bits (bits 5:0 are the command index) */
107 #define CMD_RESP_EXP_BIT (1<<6)
108 #define CMD_RESP_LENGTH_BIT (1<<7)
109 #define CMD_CHECK_CRC_BIT (1<<8)
110 #define CMD_DATA_EXP_BIT (1<<9)
111 #define CMD_RW_BIT (1<<10)
112 #define CMD_TRANSMODE_BIT (1<<11)
113 #define CMD_SENT_AUTO_STOP_BIT (1<<12)
114 #define CMD_WAIT_PRV_DAT_BIT (1<<13)
115 #define CMD_ABRT_CMD_BIT (1<<14)
116 #define CMD_SEND_INIT_BIT (1<<15)
117 #define CMD_SEND_CLK_ONLY (1<<21)
118 #define CMD_READ_CEATA (1<<22)
119 #define CMD_CCS_EXPECTED (1<<23)
120 #define CMD_DONE_BIT (1<<31)
123 #define MCI_RESP0 SD_REG(0x30)
124 #define MCI_RESP1 SD_REG(0x34)
125 #define MCI_RESP2 SD_REG(0x38)
126 #define MCI_RESP3 SD_REG(0x3C)
128 #define MCI_MASK_STATUS SD_REG(0x40) /* masked interrupt status */
129 #define MCI_RAW_STATUS SD_REG(0x44) /* raw interrupt status, also used as
131 #define MCI_STATUS SD_REG(0x48)
135 * & 0xBA80 = MCI_INT_DCRC | MCI_INT_DRTO | MCI_INT_FRUN | \
136 * MCI_INT_HLE | MCI_INT_SBE | MCI_INT_EBE
138 * & 0x428 = MCI_INT_DTO | MCI_INT_RXDR | MCI_INT_HTO
139 * & 0x418 = MCI_INT_DTO | MCI_INT_TXDR | MCI_INT_HTO
143 #define MCI_INT_CRDDET (1<<0) /* card detect */
144 #define MCI_INT_RE (1<<1) /* response error */
145 #define MCI_INT_CD (1<<2) /* command done */
146 #define MCI_INT_DTO (1<<3) /* data transfer over */
147 #define MCI_INT_TXDR (1<<4) /* tx fifo data request */
148 #define MCI_INT_RXDR (1<<5) /* rx fifo data request */
149 #define MCI_INT_RCRC (1<<6) /* response crc error */
150 #define MCI_INT_DCRC (1<<7) /* data crc error */
151 #define MCI_INT_RTO (1<<8) /* response timeout */
152 #define MCI_INT_DRTO (1<<9) /* data read timeout */
153 #define MCI_INT_HTO (1<<10) /* data starv timeout */
154 #define MCI_INT_FRUN (1<<11) /* fifo over/underrun */
155 #define MCI_INT_HLE (1<<12) /* hw locked while error */
156 #define MCI_INT_SBE (1<<13) /* start bit error */
157 #define MCI_INT_ACD (1<<14) /* auto command done */
158 #define MCI_INT_EBE (1<<15) /* end bit error */
159 #define MCI_INT_SDIO (0xf<<16)
161 #define MCI_ERROR (MCI_INT_RE | MCI_INT_RCRC | MCI_INT_DCRC | MCI_INT_RTO \
162 | MCI_INT_DRTO | MCI_INT_HTO | MCI_INT_FRUN | MCI_INT_HLE \
163 | MCI_INT_SBE | MCI_INT_EBE)
165 #define MCI_FIFOTH SD_REG(0x4C) /* FIFO threshold */
166 /* TX watermark : bits 11:0
167 * RX watermark : bits 27:16
168 * DMA MTRANS SIZE : bits 30:28
169 * bits 31, 15:12 : unused
171 #define MCI_FIFOTH_MASK 0x8000f000
173 #define MCI_CDETECT SD_REG(0x50) /* card detect */
174 #define MCI_WRTPRT SD_REG(0x54) /* write protect */
175 #define MCI_GPIO SD_REG(0x58)
176 #define MCI_TCBCNT SD_REG(0x5C) /* transferred CIU byte count */
177 #define MCI_TBBCNT SD_REG(0x60) /* transferred host/DMA to/from bytes */
178 #define MCI_DEBNCE SD_REG(0x64) /* card detect debounce */
179 #define MCI_USRID SD_REG(0x68) /* user id */
180 #define MCI_VERID SD_REG(0x6C) /* version id */
182 #define MCI_HCON SD_REG(0x70) /* hardware config */
184 * bits 5:1 : maximum card index */
186 #define MCI_BMOD SD_REG(0x80) /* bus mode */
187 #define MCI_PLDMND SD_REG(0x84) /* poll demand */
188 #define MCI_DBADDR SD_REG(0x88) /* descriptor base address */
189 #define MCI_IDSTS SD_REG(0x8C) /* internal DMAC status */
190 #define MCI_IDINTEN SD_REG(0x90) /* internal DMAC interrupt enable */
191 #define MCI_DSCADDR SD_REG(0x94) /* current host descriptor address */
192 #define MCI_BUFADDR SD_REG(0x98) /* current host buffer address */
194 #define MCI_FIFO ((unsigned long *) (SD_BASE+0x100))
196 static int sd_init_card(void);
197 static void init_controller(void);
199 static tCardInfo card_info
;
201 /* for compatibility */
202 static long last_disk_activity
= -1;
204 #define MIN_YIELD_PERIOD 5 /* ticks */
205 static long next_yield
= 0;
207 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x200)/sizeof(long)];
208 static const char sd_thread_name
[] = "ata/sd";
209 static struct mutex sd_mtx SHAREDBSS_ATTR
;
210 static struct event_queue sd_queue
;
212 static bool sd_enabled
= false;
215 static struct wakeup transfer_completion_signal
;
216 static volatile bool retry
;
218 static inline void mci_delay(void) { int i
= 0xffff; while(i
--) ; }
222 MCI_CTRL
&= ~INT_ENABLE
;
223 const int status
= MCI_MASK_STATUS
;
225 MCI_RAW_STATUS
= status
; /* clear status */
227 if(status
& MCI_ERROR
)
229 else if(status
& MCI_INT_DTO
)
230 wakeup_signal(&transfer_completion_signal
);
232 MCI_CTRL
|= INT_ENABLE
;
235 static bool send_cmd(const int cmd
, const int arg
, const int flags
,
236 unsigned long *response
)
239 val
= cmd
| CMD_DONE_BIT
;
242 val
|= CMD_RESP_EXP_BIT
;
243 if(flags
& MCI_LONG_RESP
)
244 val
|= CMD_RESP_LENGTH_BIT
;
247 if(cmd
== SD_READ_MULTIPLE_BLOCK
|| cmd
== SD_WRITE_MULTIPLE_BLOCK
)
249 val
|= CMD_WAIT_PRV_DAT_BIT
| CMD_DATA_EXP_BIT
;
250 if(cmd
== SD_WRITE_MULTIPLE_BLOCK
)
251 val
|= CMD_RW_BIT
| CMD_CHECK_CRC_BIT
;
254 int tmp
= MCI_CLKENA
;
257 MCI_COMMAND
= CMD_DONE_BIT
|CMD_SEND_CLK_ONLY
|CMD_WAIT_PRV_DAT_BIT
;
260 while(max
-- && MCI_COMMAND
& CMD_DONE_BIT
);
265 MCI_COMMAND
= CMD_DONE_BIT
|CMD_SEND_CLK_ONLY
|CMD_WAIT_PRV_DAT_BIT
;
268 while(max
-- && MCI_COMMAND
& CMD_DONE_BIT
);
272 MCI_COMMAND
= CMD_DONE_BIT
|CMD_SEND_CLK_ONLY
|CMD_WAIT_PRV_DAT_BIT
;
275 while(max
-- && MCI_COMMAND
& CMD_DONE_BIT
);
282 MCI_CTRL
|= INT_ENABLE
;
285 while(max
-- && MCI_COMMAND
& CMD_DONE_BIT
); /* wait for cmd completion */
291 if(flags
& MCI_LONG_RESP
)
293 /* store the response in little endian order for the words */
294 response
[0] = MCI_RESP3
;
295 response
[1] = MCI_RESP2
;
296 response
[2] = MCI_RESP1
;
297 response
[3] = MCI_RESP0
;
300 response
[0] = MCI_RESP0
;
305 static int sd_init_card(void)
307 unsigned long response
;
308 unsigned long temp_reg
[4];
309 int max_tries
= 100; /* max acmd41 attemps */
313 if(!send_cmd(SD_GO_IDLE_STATE
, 0, MCI_NO_RESP
, NULL
))
319 if(send_cmd(SD_SEND_IF_COND
, 0x1AA, MCI_RESP
, &response
))
320 if((response
& 0xFFF) == 0x1AA)
324 /* some MicroSD cards seems to need more delays, so play safe */
330 if( !send_cmd(SD_APP_CMD
, 0, MCI_RESP
, &response
) /*||
331 !(response & (1<<5))*/ )
337 if(!send_cmd(SD_APP_OP_COND
, (sdhc
? 0x40FF8000 : (1<<23)),
338 MCI_RESP
, &card_info
.ocr
))
340 } while(!(card_info
.ocr
& (1<<31)) && max_tries
--);
350 if(!send_cmd(SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
, card_info
.cid
))
354 if(!send_cmd(SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
, &card_info
.rca
))
358 if(!send_cmd(SD_SEND_CSD
, card_info
.rca
,
359 MCI_RESP
|MCI_LONG_RESP
, temp_reg
))
363 card_info
.csd
[3-i
] = temp_reg
[i
];
365 sd_parse_csd(&card_info
);
367 if(!send_cmd(SD_APP_CMD
, 0, MCI_RESP
, &response
) ||
368 !send_cmd(42, 0, MCI_NO_RESP
, NULL
)) /* disconnect the 50 KOhm pull-up
369 resistor on CD/DAT3 */
372 if(!send_cmd(SD_APP_CMD
, card_info
.rca
, MCI_NO_RESP
, NULL
))
375 if(!send_cmd(SD_SET_BUS_WIDTH
, card_info
.rca
| 2, MCI_NO_RESP
, NULL
))
378 MCI_CTYPE
= (1<<0); /* Bus width = 4 */
380 if(!send_cmd(SD_SELECT_CARD
, card_info
.rca
, MCI_NO_RESP
, NULL
))
383 /* not sent in init_card() by OF */
384 if(!send_cmd(SD_SET_BLOCKLEN
, card_info
.blocksize
, MCI_NO_RESP
,
388 card_info
.initialized
= 1;
393 static void sd_thread(void) __attribute__((noreturn
));
394 static void sd_thread(void)
396 struct queue_event ev
;
397 bool idle_notified
= false;
401 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
406 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
408 idle_notified
= false;
412 /* never let a timer wrap confuse us */
413 next_yield
= current_tick
;
417 call_storage_idle_notifys(false);
418 idle_notified
= true;
423 case SYS_USB_CONNECTED
:
424 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
425 /* Wait until the USB cable is extracted again */
426 usb_wait_for_disconnect(&sd_queue
);
429 case SYS_USB_DISCONNECTED
:
430 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
437 static void init_controller(void)
439 int idx
= (MCI_HCON
>> 1) & 31;
440 int idx_bits
= (1 << idx
) -1;
442 MCI_PWREN
&= ~idx_bits
;
443 MCI_PWREN
= idx_bits
;
447 MCI_CTRL
|= CTRL_RESET
; /* FIXME: FIFO & DMA reset? */
448 while(MCI_CTRL
& CTRL_RESET
)
451 MCI_RAW_STATUS
= 0xffffffff;
453 MCI_CTRL
|= INT_ENABLE
;
454 MCI_TMOUT
= 0xffffffff;
458 MCI_CLKENA
= idx_bits
;
461 MCI_COMMAND
= CMD_DONE_BIT
|CMD_SEND_CLK_ONLY
|CMD_WAIT_PRV_DAT_BIT
;
463 while(max
-- && (MCI_COMMAND
& CMD_DONE_BIT
)) ;
465 MCI_DEBNCE
= 0xfffff; /* default value */
467 MCI_FIFOTH
&= MCI_FIFOTH_MASK
;
468 MCI_FIFOTH
|= 0x503f0080;
470 MCI_MASK
= 0xffffffff & ~(MCI_INT_ACD
|MCI_INT_CRDDET
);
476 CGU_PERI
|= CGU_MCI_CLOCK_ENABLE
;
478 CGU_IDE
= (1<<7) /* AHB interface enable */ |
479 (1<<6) /* interface enable */ |
480 ((CLK_DIV(AS3525_PLLA_FREQ
, AS3525_IDE_FREQ
) - 1) << 2) |
481 1; /* clock source = PLLA */
483 CGU_MEMSTICK
= (1<<8) | (1<<7) |
484 (CLK_DIV(AS3525_PLLA_FREQ
, AS3525_MS_FREQ
) -1) | 1;
487 *(volatile int*)(CGU_BASE
+0x3C) = (1<<7) |
488 (CLK_DIV(AS3525_PLLA_FREQ
, 24000000) -1) | 1;
490 wakeup_init(&transfer_completion_signal
);
492 VIC_INT_ENABLE
|= INTERRUPT_NAND
;
495 ret
= sd_init_card();
502 queue_init(&sd_queue
, true);
503 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
504 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
) IF_COP(, CPU
));
513 #ifdef STORAGE_GET_INFO
514 void sd_get_info(struct storage_info
*info
)
516 info
->sector_size
=card_info
.blocksize
;
517 info
->num_sectors
=card_info
.numblocks
;
518 info
->vendor
="Rockbox";
519 info
->product
= "Internal Storage";
520 info
->revision
="0.00";
524 static int sd_wait_for_state(unsigned int state
)
526 unsigned long response
;
527 unsigned int timeout
= 100; /* ticks */
528 long t
= current_tick
;
534 if(!send_cmd(SD_SEND_STATUS
, card_info
.rca
,
535 MCI_RESP
, &response
))
538 if (((response
>> 9) & 0xf) == state
)
541 if(TIME_AFTER(current_tick
, t
+ timeout
))
542 return -10 * ((response
>> 9) & 0xf);
544 if (TIME_AFTER((tick
= current_tick
), next_yield
))
547 timeout
+= current_tick
- tick
;
548 next_yield
= tick
+ MIN_YIELD_PERIOD
;
553 static int sd_transfer_sectors(unsigned long start
, int count
, void* buf
, bool write
)
556 /* This is debug code, not functional yet */
559 printf("Entering SD transfer");
560 printf("THIS IS DEBUG CODE !");
562 printf("All your controllers");
563 printf("are belong to us.");
564 volatile int delay
= 0x500000;
568 #endif /* debug warning */
573 panicf("unaligned transfer");
575 /* skip SanDisk OF */
583 if (card_info
.initialized
<= 0)
585 ret
= sd_init_card();
586 if (!(card_info
.initialized
))
588 panicf("card not initialised (%d)", ret
);
589 goto sd_transfer_error
;
593 last_disk_activity
= current_tick
;
594 ret
= sd_wait_for_state(SD_TRAN
);
597 static const char *st
[9] = {
598 "IDLE", "RDY", "IDENT", "STBY", "TRAN", "DATA", "RCV", "PRG", "DIS"
601 panicf("wait for state failed (%s)", st
[(-ret
/ 10) % 9]);
603 panicf("wait for state failed");
604 goto sd_transfer_error
;
609 const int cmd
= write
? SD_WRITE_MULTIPLE_BLOCK
: SD_READ_MULTIPLE_BLOCK
;
611 /* Interrupt handler might set this to true during transfer */
617 MCI_BYTCNT
= count
* 512;
619 MCI_CTRL
|= (FIFO_RESET
|DMA_RESET
);
620 while(MCI_CTRL
& (FIFO_RESET
|DMA_RESET
))
623 MCI_CTRL
|= DMA_ENABLE
;
624 MCI_MASK
= MCI_INT_CD
|MCI_INT_DTO
|MCI_INT_DCRC
|MCI_INT_DRTO
| \
625 MCI_INT_HTO
|MCI_INT_FRUN
|MCI_INT_HLE
|MCI_INT_SBE
|MCI_INT_EBE
;
627 MCI_FIFOTH
&= MCI_FIFOTH_MASK
;
628 MCI_FIFOTH
|= 0x503f0080;
631 if(card_info
.ocr
& (1<<30) ) /* SDHC */
632 ret
= send_cmd(cmd
, start
, MCI_NO_RESP
, NULL
);
634 ret
= send_cmd(cmd
, start
* SD_BLOCK_SIZE
,
638 panicf("transfer multiple blocks failed (%d)", ret
);
641 dma_enable_channel(0, buf
, MCI_FIFO
, DMA_PERI_SD
,
642 DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
, NULL
);
644 dma_enable_channel(0, MCI_FIFO
, buf
, DMA_PERI_SD
,
645 DMAC_FLOWCTRL_PERI_PERI_TO_MEM
, false, true, 0, DMA_S8
, NULL
);
651 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
654 int delay
= 0x1000000; while(delay
--) ;
656 last_disk_activity
= current_tick
;
658 if(!send_cmd(SD_STOP_TRANSMISSION
, 0, MCI_NO_RESP
, NULL
))
661 panicf("STOP TRANSMISSION failed");
662 goto sd_transfer_error
;
665 ret
= sd_wait_for_state(SD_TRAN
);
668 panicf(" wait for state TRAN failed (%d)", ret
);
669 goto sd_transfer_error
;
678 mutex_unlock(&sd_mtx
);
682 panicf("transfer error : %d",ret
);
683 card_info
.initialized
= 0;
687 int sd_read_sectors(unsigned long start
, int count
, void* buf
)
689 return sd_transfer_sectors(start
, count
, buf
, false);
692 int sd_write_sectors(unsigned long start
, int count
, const void* buf
)
694 #if defined(BOOTLOADER) /* we don't need write support in bootloader */
700 return sd_transfer_sectors(start
, count
, (void*)buf
, true);
705 long sd_last_disk_activity(void)
707 return last_disk_activity
;
710 void sd_enable(bool on
)
717 tCardInfo
*card_get_info_target(int card_no
)
723 #endif /* BOOTLOADER */