1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Daniel Ankers
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h" /* for HAVE_MULTIVOLUME */
24 #include "ata-sd-target.h"
25 #include "ata_idle_notify.h"
37 #define BLOCK_SIZE 512
38 #define SECTOR_SIZE 512
39 #define BLOCKS_PER_BANK 0x7a7800
41 #define STATUS_REG (*(volatile unsigned int *)(0x70008204))
42 #define REG_1 (*(volatile unsigned int *)(0x70008208))
43 #define UNKNOWN (*(volatile unsigned int *)(0x70008210))
44 #define BLOCK_SIZE_REG (*(volatile unsigned int *)(0x7000821c))
45 #define BLOCK_COUNT_REG (*(volatile unsigned int *)(0x70008220))
46 #define REG_5 (*(volatile unsigned int *)(0x70008224))
47 #define CMD_REG0 (*(volatile unsigned int *)(0x70008228))
48 #define CMD_REG1 (*(volatile unsigned int *)(0x7000822c))
49 #define CMD_REG2 (*(volatile unsigned int *)(0x70008230))
50 #define RESPONSE_REG (*(volatile unsigned int *)(0x70008234))
51 #define SD_STATE_REG (*(volatile unsigned int *)(0x70008238))
52 #define REG_11 (*(volatile unsigned int *)(0x70008240))
53 #define REG_12 (*(volatile unsigned int *)(0x70008244))
54 #define DATA_REG (*(volatile unsigned int *)(0x70008280))
57 #define DATA_DONE (1 << 12)
58 #define CMD_DONE (1 << 13)
59 #define ERROR_BITS (0x3f)
60 #define READY_FOR_DATA (1 << 8)
61 #define FIFO_FULL (1 << 7)
62 #define FIFO_EMPTY (1 << 6)
64 #define CMD_OK 0x0 /* Command was successful */
65 #define CMD_ERROR_2 0x2 /* SD did not respond to command (either it doesn't
66 understand the command or is not inserted) */
68 #define FIFO_LEN 16 /* FIFO is 16 words deep */
73 #define EC_WAIT_STATE_FAILED 3
74 #define EC_CHECK_TIMEOUT_FAILED 4
76 #define EC_READ_TIMEOUT 6
77 #define EC_WRITE_TIMEOUT 7
78 #define EC_TRAN_SEL_BANK 8
79 #define EC_TRAN_READ_ENTRY 9
80 #define EC_TRAN_READ_EXIT 10
81 #define EC_TRAN_WRITE_ENTRY 11
82 #define EC_TRAN_WRITE_EXIT 12
83 #define EC_FIFO_SEL_BANK_EMPTY 13
84 #define EC_FIFO_SEL_BANK_DONE 14
85 #define EC_FIFO_ENA_BANK_EMPTY 15
86 #define EC_FIFO_READ_FULL 16
87 #define EC_FIFO_WR_EMPTY 17
88 #define EC_FIFO_WR_DONE 18
92 /* for compatibility */
93 static long last_disk_activity
= -1;
95 /** static, private data **/
96 static bool initialized
= false;
98 static long next_yield
= 0;
99 #define MIN_YIELD_PERIOD 1000
101 static tSDCardInfo card_info
[2];
102 static tSDCardInfo
*currcard
= NULL
; /* current active card */
104 struct sd_card_status
110 static struct sd_card_status sd_status
[NUM_VOLUMES
] =
113 #ifdef HAVE_MULTIVOLUME
118 /* Shoot for around 75% usage */
119 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x1c0)/sizeof(long)];
120 static const char sd_thread_name
[] = "ata/sd";
121 static struct mutex sd_mtx SHAREDBSS_ATTR
;
122 static struct event_queue sd_queue
;
124 /* Posted when card plugged status has changed */
126 /* Actions taken by sd_thread when card status has changed */
127 enum sd_thread_actions
134 /* Private Functions */
136 static unsigned int check_time
[NUM_EC
];
138 static inline bool sd_check_timeout(long timeout
, int id
)
140 return !TIME_AFTER(USEC_TIMER
, check_time
[id
] + timeout
);
143 static bool sd_poll_status(unsigned int trigger
, long timeout
)
147 while ((STATUS_REG
& trigger
) == 0)
149 long time
= USEC_TIMER
;
151 if (TIME_AFTER(time
, next_yield
))
153 long ty
= USEC_TIMER
;
155 timeout
+= USEC_TIMER
- ty
;
156 next_yield
= ty
+ MIN_YIELD_PERIOD
;
159 if (TIME_AFTER(time
, t
+ timeout
))
166 static int sd_command(unsigned int cmd
, unsigned long arg1
,
167 unsigned int *response
, unsigned int type
)
169 int i
, words
; /* Number of 16 bit words to read from RESPONSE_REG */
170 unsigned int data
[9];
173 CMD_REG1
= (unsigned int)((arg1
& 0xffff0000) >> 16);
174 CMD_REG2
= (unsigned int)((arg1
& 0xffff));
177 if (!sd_poll_status(CMD_DONE
, 100000))
180 if ((STATUS_REG
& ERROR_BITS
) != CMD_OK
)
181 /* Error sending command */
182 return -EC_COMMAND
- (STATUS_REG
& ERROR_BITS
)*100;
184 if (cmd
== SD_GO_IDLE_STATE
)
185 return 0; /* no response here */
187 words
= (type
== 2) ? 9 : 3;
189 for (i
= 0; i
< words
; i
++) /* RESPONSE_REG is read MSB first */
190 data
[i
] = RESPONSE_REG
; /* Read most significant 16-bit word */
192 if (response
== NULL
)
194 /* response discarded */
198 /* Response type 2 has the following structure:
199 * [135:135] Start Bit - '0'
200 * [134:134] Transmission bit - '0'
201 * [133:128] Reserved - '111111'
202 * [127:001] CID or CSD register including internal CRC7
203 * [000:000] End Bit - '1'
205 response
[3] = (data
[0]<<24) + (data
[1]<<8) + (data
[2]>>8);
206 response
[2] = (data
[2]<<24) + (data
[3]<<8) + (data
[4]>>8);
207 response
[1] = (data
[4]<<24) + (data
[5]<<8) + (data
[6]>>8);
208 response
[0] = (data
[6]<<24) + (data
[7]<<8) + (data
[8]>>8);
212 /* Response types 1, 1b, 3, 6, 7 have the following structure:
213 * Types 4 and 5 are not supported.
215 * [47] Start bit - '0'
216 * [46] Transmission bit - '0'
217 * [45:40] R1, R1b, R6, R7: Command index
218 * R3: Reserved - '111111'
219 * [39:8] R1, R1b: Card Status
222 * [15: 0] Card Status Bits 23, 22, 19, 12:0
224 * [22] ILLEGAL_COMMAND
226 * [12:9] CURRENT_STATE
233 * [1:0] Reserved for test mode
234 * R7: [19:16] Voltage accepted
235 * [15:8] echo-back of check pattern
236 * [7:1] R1, R1b: CRC7
237 * R3: Reserved - '1111111'
240 response
[0] = (data
[0]<<24) + (data
[1]<<8) + (data
[2]>>8);
246 static int sd_wait_for_state(unsigned int state
, int id
)
248 unsigned int response
= 0;
249 unsigned int timeout
= 0x80000;
251 check_time
[id
] = USEC_TIMER
;
255 int ret
= sd_command(SD_SEND_STATUS
, currcard
->rca
, &response
, 1);
261 if (((response
>> 9) & 0xf) == state
)
263 SD_STATE_REG
= state
;
267 if (!sd_check_timeout(timeout
, id
))
268 return -EC_WAIT_STATE_FAILED
*100 - id
;
271 if (TIME_AFTER(us
, next_yield
))
274 timeout
+= USEC_TIMER
- us
;
275 next_yield
= us
+ MIN_YIELD_PERIOD
;
280 static inline void copy_read_sectors_fast(unsigned char **buf
)
282 /* Copy one chunk of 16 words using best method for start alignment */
283 switch ( (intptr_t)*buf
& 3 )
287 "ldmia %[data], { r2-r9 } \r\n"
288 "orr r2, r2, r3, lsl #16 \r\n"
289 "orr r4, r4, r5, lsl #16 \r\n"
290 "orr r6, r6, r7, lsl #16 \r\n"
291 "orr r8, r8, r9, lsl #16 \r\n"
292 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
293 "ldmia %[data], { r2-r9 } \r\n"
294 "orr r2, r2, r3, lsl #16 \r\n"
295 "orr r4, r4, r5, lsl #16 \r\n"
296 "orr r6, r6, r7, lsl #16 \r\n"
297 "orr r8, r8, r9, lsl #16 \r\n"
298 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
300 : [data
]"r"(&DATA_REG
)
301 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9"
306 "ldmia %[data], { r2-r9 } \r\n"
307 "orr r3, r2, r3, lsl #16 \r\n"
308 "strb r3, [%[buf]], #1 \r\n"
309 "mov r3, r3, lsr #8 \r\n"
310 "strh r3, [%[buf]], #2 \r\n"
311 "mov r3, r3, lsr #16 \r\n"
312 "orr r3, r3, r4, lsl #8 \r\n"
313 "orr r3, r3, r5, lsl #24 \r\n"
314 "mov r5, r5, lsr #8 \r\n"
315 "orr r5, r5, r6, lsl #8 \r\n"
316 "orr r5, r5, r7, lsl #24 \r\n"
317 "mov r7, r7, lsr #8 \r\n"
318 "orr r7, r7, r8, lsl #8 \r\n"
319 "orr r7, r7, r9, lsl #24 \r\n"
320 "mov r2, r9, lsr #8 \r\n"
321 "stmia %[buf]!, { r3, r5, r7 } \r\n"
322 "ldmia %[data], { r3-r10 } \r\n"
323 "orr r2, r2, r3, lsl #8 \r\n"
324 "orr r2, r2, r4, lsl #24 \r\n"
325 "mov r4, r4, lsr #8 \r\n"
326 "orr r4, r4, r5, lsl #8 \r\n"
327 "orr r4, r4, r6, lsl #24 \r\n"
328 "mov r6, r6, lsr #8 \r\n"
329 "orr r6, r6, r7, lsl #8 \r\n"
330 "orr r6, r6, r8, lsl #24 \r\n"
331 "mov r8, r8, lsr #8 \r\n"
332 "orr r8, r8, r9, lsl #8 \r\n"
333 "orr r8, r8, r10, lsl #24 \r\n"
334 "mov r10, r10, lsr #8 \r\n"
335 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
336 "strb r10, [%[buf]], #1 \r\n"
338 : [data
]"r"(&DATA_REG
)
339 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
344 "ldmia %[data], { r2-r9 } \r\n"
345 "strh r2, [%[buf]], #2 \r\n"
346 "orr r3, r3, r4, lsl #16 \r\n"
347 "orr r5, r5, r6, lsl #16 \r\n"
348 "orr r7, r7, r8, lsl #16 \r\n"
349 "stmia %[buf]!, { r3, r5, r7 } \r\n"
350 "ldmia %[data], { r2-r8, r10 } \r\n"
351 "orr r2, r9, r2, lsl #16 \r\n"
352 "orr r3, r3, r4, lsl #16 \r\n"
353 "orr r5, r5, r6, lsl #16 \r\n"
354 "orr r7, r7, r8, lsl #16 \r\n"
355 "stmia %[buf]!, { r2, r3, r5, r7 } \r\n"
356 "strh r10, [%[buf]], #2 \r\n"
358 : [data
]"r"(&DATA_REG
)
359 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
364 "ldmia %[data], { r2-r9 } \r\n"
365 "orr r3, r2, r3, lsl #16 \r\n"
366 "strb r3, [%[buf]], #1 \r\n"
367 "mov r3, r3, lsr #8 \r\n"
368 "orr r3, r3, r4, lsl #24 \r\n"
369 "mov r4, r4, lsr #8 \r\n"
370 "orr r5, r4, r5, lsl #8 \r\n"
371 "orr r5, r5, r6, lsl #24 \r\n"
372 "mov r6, r6, lsr #8 \r\n"
373 "orr r7, r6, r7, lsl #8 \r\n"
374 "orr r7, r7, r8, lsl #24 \r\n"
375 "mov r8, r8, lsr #8 \r\n"
376 "orr r2, r8, r9, lsl #8 \r\n"
377 "stmia %[buf]!, { r3, r5, r7 } \r\n"
378 "ldmia %[data], { r3-r10 } \r\n"
379 "orr r2, r2, r3, lsl #24 \r\n"
380 "mov r3, r3, lsr #8 \r\n"
381 "orr r4, r3, r4, lsl #8 \r\n"
382 "orr r4, r4, r5, lsl #24 \r\n"
383 "mov r5, r5, lsr #8 \r\n"
384 "orr r6, r5, r6, lsl #8 \r\n"
385 "orr r6, r6, r7, lsl #24 \r\n"
386 "mov r7, r7, lsr #8 \r\n"
387 "orr r8, r7, r8, lsl #8 \r\n"
388 "orr r8, r8, r9, lsl #24 \r\n"
389 "mov r9, r9, lsr #8 \r\n"
390 "orr r10, r9, r10, lsl #8 \r\n"
391 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
392 "strh r10, [%[buf]], #2 \r\n"
393 "mov r10, r10, lsr #16 \r\n"
394 "strb r10, [%[buf]], #1 \r\n"
396 : [data
]"r"(&DATA_REG
)
397 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
403 static inline void copy_read_sectors_slow(unsigned char** buf
)
408 /* Copy one chunk of 16 words */
411 "ldrh %[t], [%[data]] \r\n"
412 "strb %[t], [%[buf]], #1 \r\n"
413 "mov %[t], %[t], lsr #8 \r\n"
414 "strb %[t], [%[buf]], #1 \r\n"
415 "subs %[cnt], %[cnt], #1 \r\n"
417 : [cnt
]"+&r"(cnt
), [buf
]"+&r"(*buf
),
419 : [data
]"r"(&DATA_REG
)
423 /* Writes have to be kept slow for now */
424 static inline void copy_write_sectors(const unsigned char** buf
)
434 } while (--cnt
> 0); /* tail loop is faster */
437 static int sd_select_bank(unsigned char bank
)
439 unsigned char card_data
[512];
440 const unsigned char* write_buf
;
443 memset(card_data
, 0, 512);
445 ret
= sd_wait_for_state(SD_TRAN
, EC_TRAN_SEL_BANK
);
449 BLOCK_SIZE_REG
= 512;
452 ret
= sd_command(35, 0, NULL
, 0x1c0d); /* CMD35 is vendor specific */
456 SD_STATE_REG
= SD_PRG
;
460 /* Write the card data */
461 write_buf
= card_data
;
462 for (i
= 0; i
< BLOCK_SIZE
/2; i
+= FIFO_LEN
)
464 /* Wait for the FIFO to empty */
465 if (sd_poll_status(FIFO_EMPTY
, 10000))
467 copy_write_sectors(&write_buf
); /* Copy one chunk of 16 words */
471 return -EC_FIFO_SEL_BANK_EMPTY
;
474 if (!sd_poll_status(DATA_DONE
, 10000))
475 return -EC_FIFO_SEL_BANK_DONE
;
477 currcard
->current_bank
= bank
;
482 static void sd_card_mux(int card_no
)
484 /* Set the current card mux */
485 #if defined(SANSA_E200) || defined(PHILIPS_SA9200)
490 GPIO_CLEAR_BITWISE(GPIOA_ENABLE
, 0x7a);
491 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x7a);
492 GPIO_SET_BITWISE(GPIOD_ENABLE
, 0x1f);
493 GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL
, 0x1f);
494 GPIO_SET_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
496 outl((inl(0x70000014) & ~(0x3ffff)) | 0x255aa, 0x70000014);
502 GPIO_CLEAR_BITWISE(GPIOD_ENABLE
, 0x1f);
503 GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
504 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x7a);
505 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL
, 0x7a);
506 GPIO_SET_BITWISE( GPIOA_OUTPUT_EN
, 0x7a);
508 outl(inl(0x70000014) & ~(0x3ffff), 0x70000014);
510 #else /* SANSA_C200 */
515 GPIO_CLEAR_BITWISE(GPIOD_ENABLE
, 0x1f);
516 GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
517 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x7a);
518 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL
, 0x7a);
519 GPIO_SET_BITWISE( GPIOA_OUTPUT_EN
, 0x7a);
521 outl(inl(0x70000014) & ~(0x3ffff), 0x70000014);
527 GPIO_CLEAR_BITWISE(GPIOA_ENABLE
, 0x7a);
528 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x7a);
529 GPIO_SET_BITWISE(GPIOD_ENABLE
, 0x1f);
530 GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL
, 0x1f);
531 GPIO_SET_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
533 outl((inl(0x70000014) & ~(0x3ffff)) | 0x255aa, 0x70000014);
538 static void sd_init_device(int card_no
)
540 /* SD Protocol registers */
542 unsigned int response
= 0;
546 unsigned long c_mult
;
547 unsigned char carddata
[512];
548 unsigned char *dataptr
;
551 /* Enable and initialise controller */
554 /* Initialise card data as blank */
555 memset(currcard
, 0, sizeof(*currcard
));
557 /* Switch card mux to card to initialize */
558 sd_card_mux(card_no
);
563 REG_12
&= ~(3 << 12);
565 REG_11
&= ~(3 << 12);
568 DEV_EN
|= DEV_ATA
; /* Enable controller */
569 DEV_RS
|= DEV_ATA
; /* Reset controller */
570 DEV_RS
&=~DEV_ATA
; /* Clear Reset */
572 SD_STATE_REG
= SD_TRAN
;
576 ret
= sd_command(SD_GO_IDLE_STATE
, 0, NULL
, 256);
578 goto card_init_error
;
580 check_time
[EC_POWER_UP
] = USEC_TIMER
;
584 - non-SDHC cards simply ignore SD_SEND_IF_COND (CMD8) and we get error -219,
585 which we can just ignore and assume we're dealing with standard SD.
586 - SDHC cards echo back the argument into the response. This is how we
587 tell if the card is SDHC.
589 ret
= sd_command(SD_SEND_IF_COND
,0x1aa, &response
,7);
590 if ( (ret
< 0) && (ret
!=-219) )
591 goto card_init_error
;
594 while ((currcard
->ocr
& (1 << 31)) == 0) /* until card is powered up */
596 ret
= sd_command(SD_APP_CMD
, currcard
->rca
, NULL
, 1);
598 goto card_init_error
;
601 if(response
== 0x1aa)
604 ret
= sd_command(SD_APP_OP_COND
, (1<<30)|0x100000,
608 #endif /* HAVE_HOTSWAP */
611 ret
= sd_command(SD_APP_OP_COND
, 0x100000, &currcard
->ocr
, 3);
615 goto card_init_error
;
617 if (!sd_check_timeout(5000000, EC_POWER_UP
))
620 goto card_init_error
;
624 ret
= sd_command(SD_ALL_SEND_CID
, 0, currcard
->cid
, 2);
626 goto card_init_error
;
628 ret
= sd_command(SD_SEND_RELATIVE_ADDR
, 0, &currcard
->rca
, 1);
630 goto card_init_error
;
632 ret
= sd_command(SD_SEND_CSD
, currcard
->rca
, currcard
->csd
, 2);
634 goto card_init_error
;
636 /* These calculations come from the Sandisk SD card product manual */
637 if( (currcard
->csd
[3]>>30) == 0)
639 /* CSD version 1.0 */
640 c_size
= ((currcard
->csd
[2] & 0x3ff) << 2) + (currcard
->csd
[1]>>30) + 1;
641 c_mult
= 4 << ((currcard
->csd
[1] >> 15) & 7);
642 currcard
->max_read_bl_len
= 1 << ((currcard
->csd
[2] >> 16) & 15);
643 currcard
->block_size
= BLOCK_SIZE
; /* Always use 512 byte blocks */
644 currcard
->numblocks
= c_size
* c_mult
* (currcard
->max_read_bl_len
/512);
645 currcard
->capacity
= currcard
->numblocks
* currcard
->block_size
;
648 else if( (currcard
->csd
[3]>>30) == 1)
650 /* CSD version 2.0 */
651 c_size
= ((currcard
->csd
[2] & 0x3f) << 16) + (currcard
->csd
[1]>>16) + 1;
652 currcard
->max_read_bl_len
= 1 << ((currcard
->csd
[2] >> 16) & 0xf);
653 currcard
->block_size
= BLOCK_SIZE
; /* Always use 512 byte blocks */
654 currcard
->numblocks
= c_size
<< 10;
655 currcard
->capacity
= currcard
->numblocks
* currcard
->block_size
;
657 #endif /* HAVE_HOTSWAP */
661 ret
= sd_command(SD_SELECT_CARD
, currcard
->rca
, NULL
, 129);
663 goto card_init_error
;
665 ret
= sd_command(SD_APP_CMD
, currcard
->rca
, NULL
, 1);
667 goto card_init_error
;
669 ret
= sd_command(SD_SET_BUS_WIDTH
, currcard
->rca
| 2, NULL
, 1); /* 4 bit */
671 goto card_init_error
;
673 ret
= sd_command(SD_SET_BLOCKLEN
, currcard
->block_size
, NULL
, 1);
675 goto card_init_error
;
677 BLOCK_SIZE_REG
= currcard
->block_size
;
679 /* If this card is >4GB & not SDHC, then we need to enable bank switching */
680 if( (currcard
->numblocks
>= BLOCKS_PER_BANK
) &&
681 ((currcard
->ocr
& (1<<30)) == 0) )
683 SD_STATE_REG
= SD_TRAN
;
686 ret
= sd_command(SD_SWITCH_FUNC
, 0x80ffffef, NULL
, 0x1c05);
688 goto card_init_error
;
690 /* Read 512 bytes from the card.
691 The first 512 bits contain the status information
692 TODO: Do something useful with this! */
694 for (i
= 0; i
< BLOCK_SIZE
/2; i
+= FIFO_LEN
)
696 /* Wait for the FIFO to be full */
697 if (sd_poll_status(FIFO_FULL
, 100000))
699 copy_read_sectors_slow(&dataptr
);
703 ret
= -EC_FIFO_ENA_BANK_EMPTY
;
704 goto card_init_error
;
708 currcard
->initialized
= 1;
711 /* Card failed to initialize so disable it */
713 currcard
->initialized
= ret
;
716 /* lock must already be aquired */
717 static void sd_select_device(int card_no
)
719 currcard
= &card_info
[card_no
];
723 /* Main card always gets a chance */
724 sd_status
[0].retry
= 0;
727 if (currcard
->initialized
> 0)
729 /* This card is already initialized - switch to it */
730 sd_card_mux(card_no
);
734 if (currcard
->initialized
== 0)
736 /* Card needs (re)init */
737 sd_init_device(card_no
);
743 static void sd_led(bool onoff
)
748 int sd_read_sectors(IF_MV2(int drive
,) unsigned long start
, int incount
,
751 #ifndef HAVE_MULTIVOLUME
755 unsigned char *buf
, *buf_end
;
758 /* TODO: Add DMA support. */
765 if (drive
!= 0 && !card_detect_target())
767 /* no external sd-card inserted */
772 sd_select_device(drive
);
774 if (currcard
->initialized
< 0)
776 ret
= currcard
->initialized
;
780 last_disk_activity
= current_tick
;
782 /* Only switch banks with non-SDHC cards */
783 if((currcard
->ocr
& (1<<30))==0)
785 bank
= start
/ BLOCKS_PER_BANK
;
787 if (currcard
->current_bank
!= bank
)
789 ret
= sd_select_bank(bank
);
794 start
-= bank
* BLOCKS_PER_BANK
;
797 ret
= sd_wait_for_state(SD_TRAN
, EC_TRAN_READ_ENTRY
);
801 BLOCK_COUNT_REG
= incount
;
804 if(currcard
->ocr
& (1<<30) )
807 ret
= sd_command(SD_READ_MULTIPLE_BLOCK
, start
, NULL
, 0x1c25);
812 ret
= sd_command(SD_READ_MULTIPLE_BLOCK
, start
* BLOCK_SIZE
, NULL
, 0x1c25);
817 /* TODO: Don't assume BLOCK_SIZE == SECTOR_SIZE */
819 buf_end
= (unsigned char *)inbuf
+ incount
* currcard
->block_size
;
820 for (buf
= inbuf
; buf
< buf_end
;)
822 /* Wait for the FIFO to be full */
823 if (sd_poll_status(FIFO_FULL
, 0x80000))
825 copy_read_sectors_fast(&buf
); /* Copy one chunk of 16 words */
826 /* TODO: Switch bank if necessary */
830 ret
= -EC_FIFO_READ_FULL
;
834 last_disk_activity
= current_tick
;
836 ret
= sd_command(SD_STOP_TRANSMISSION
, 0, NULL
, 1);
840 ret
= sd_wait_for_state(SD_TRAN
, EC_TRAN_READ_EXIT
);
848 mutex_unlock(&sd_mtx
);
853 if (sd_status
[drive
].retry
< sd_status
[drive
].retry_max
854 && ret
!= -EC_NOCARD
)
856 sd_status
[drive
].retry
++;
857 currcard
->initialized
= 0;
863 int sd_write_sectors(IF_MV2(int drive
,) unsigned long start
, int count
,
866 /* Write support is not finished yet */
867 /* TODO: The standard suggests using ACMD23 prior to writing multiple blocks
868 to improve performance */
869 #ifndef HAVE_MULTIVOLUME
873 const unsigned char *buf
, *buf_end
;
881 if (drive
!= 0 && !card_detect_target())
883 /* no external sd-card inserted */
888 sd_select_device(drive
);
890 if (currcard
->initialized
< 0)
892 ret
= currcard
->initialized
;
896 /* Only switch banks with non-SDHC cards */
897 if((currcard
->ocr
& (1<<30))==0)
899 bank
= start
/ BLOCKS_PER_BANK
;
901 if (currcard
->current_bank
!= bank
)
903 ret
= sd_select_bank(bank
);
908 start
-= bank
* BLOCKS_PER_BANK
;
911 check_time
[EC_WRITE_TIMEOUT
] = USEC_TIMER
;
913 ret
= sd_wait_for_state(SD_TRAN
, EC_TRAN_WRITE_ENTRY
);
917 BLOCK_COUNT_REG
= count
;
920 if(currcard
->ocr
& (1<<30) )
923 ret
= sd_command(SD_WRITE_MULTIPLE_BLOCK
, start
, NULL
, 0x1c2d);
928 ret
= sd_command(SD_WRITE_MULTIPLE_BLOCK
, start
*BLOCK_SIZE
, NULL
, 0x1c2d);
933 buf_end
= outbuf
+ count
* currcard
->block_size
- 2*FIFO_LEN
;
935 for (buf
= outbuf
; buf
<= buf_end
;)
939 /* Set SD_STATE_REG to SD_PRG for the last buffer fill */
940 SD_STATE_REG
= SD_PRG
;
943 udelay(2); /* needed here (loop is too fast :-) */
945 /* Wait for the FIFO to empty */
946 if (sd_poll_status(FIFO_EMPTY
, 0x80000))
948 copy_write_sectors(&buf
); /* Copy one chunk of 16 words */
949 /* TODO: Switch bank if necessary */
953 ret
= -EC_FIFO_WR_EMPTY
;
957 last_disk_activity
= current_tick
;
959 if (!sd_poll_status(DATA_DONE
, 0x80000))
961 ret
= -EC_FIFO_WR_DONE
;
965 ret
= sd_command(SD_STOP_TRANSMISSION
, 0, NULL
, 1);
969 ret
= sd_wait_for_state(SD_TRAN
, EC_TRAN_WRITE_EXIT
);
977 mutex_unlock(&sd_mtx
);
982 if (sd_status
[drive
].retry
< sd_status
[drive
].retry_max
983 && ret
!= -EC_NOCARD
)
985 sd_status
[drive
].retry
++;
986 currcard
->initialized
= 0;
992 static void sd_thread(void) __attribute__((noreturn
));
993 static void sd_thread(void)
995 struct queue_event ev
;
996 bool idle_notified
= false;
1000 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
1005 case SYS_HOTSWAP_INSERTED
:
1006 case SYS_HOTSWAP_EXTRACTED
:
1007 fat_lock(); /* lock-out FAT activity first -
1008 prevent deadlocking via disk_mount that
1009 would cause a reverse-order attempt with
1011 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
1012 into driver that bypass the fat cache */
1014 /* We now have exclusive control of fat cache and ata */
1016 disk_unmount(1); /* release "by force", ensure file
1017 descriptors aren't leaked and any busy
1018 ones are invalid if mounting */
1020 /* Force card init for new card, re-init for re-inserted one or
1021 * clear if the last attempt to init failed with an error. */
1022 card_info
[1].initialized
= 0;
1023 sd_status
[1].retry
= 0;
1025 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
1028 queue_broadcast(SYS_FS_CHANGED
, 0);
1030 /* Access is now safe */
1031 mutex_unlock(&sd_mtx
);
1036 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
1038 idle_notified
= false;
1042 /* never let a timer wrap confuse us */
1043 next_yield
= USEC_TIMER
;
1047 call_storage_idle_notifys(false);
1048 idle_notified
= true;
1052 case SYS_USB_CONNECTED
:
1053 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
1054 /* Wait until the USB cable is extracted again */
1055 usb_wait_for_disconnect(&sd_queue
);
1058 case SYS_USB_DISCONNECTED
:
1059 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
1065 void sd_enable(bool on
)
1069 DEV_EN
|= DEV_ATA
; /* Enable controller */
1073 DEV_EN
&= ~DEV_ATA
; /* Disable controller */
1078 void card_enable_monitoring_target(bool on
)
1083 GPIO_SET_BITWISE(GPIOA_INT_EN
, 0x80);
1084 #elif defined(SANSA_C200)
1085 GPIO_SET_BITWISE(GPIOL_INT_EN
, 0x08);
1091 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1092 #elif defined(SANSA_C200)
1093 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1104 mutex_init(&sd_mtx
);
1106 mutex_lock(&sd_mtx
);
1114 /* init controller */
1115 outl(inl(0x70000088) & ~(0x4), 0x70000088);
1116 outl(inl(0x7000008c) & ~(0x4), 0x7000008c);
1117 GPO32_ENABLE
|= 0x4;
1119 GPIO_SET_BITWISE(GPIOG_ENABLE
, (0x3 << 5));
1120 GPIO_SET_BITWISE(GPIOG_OUTPUT_EN
, (0x3 << 5));
1121 GPIO_SET_BITWISE(GPIOG_OUTPUT_VAL
, (0x3 << 5));
1124 /* enable card detection port - mask interrupt first */
1126 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1128 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x80);
1129 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x80);
1130 #elif defined SANSA_C200
1131 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1133 GPIO_CLEAR_BITWISE(GPIOL_OUTPUT_EN
, 0x08);
1134 GPIO_SET_BITWISE(GPIOL_ENABLE
, 0x08);
1137 sd_select_device(0);
1139 if (currcard
->initialized
< 0)
1140 ret
= currcard
->initialized
;
1142 queue_init(&sd_queue
, true);
1143 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
1144 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
)
1147 /* enable interupt for the mSD card */
1151 CPU_INT_EN
= HI_MASK
;
1152 CPU_HI_INT_EN
= GPIO0_MASK
;
1154 GPIOA_INT_LEV
= (0x80 << 8) | (~GPIOA_INPUT_VAL
& 0x80);
1156 GPIOA_INT_CLR
= 0x80;
1157 #elif defined SANSA_C200
1158 CPU_INT_EN
= HI_MASK
;
1159 CPU_HI_INT_EN
= GPIO2_MASK
;
1161 GPIOL_INT_LEV
= (0x08 << 8) | (~GPIOL_INPUT_VAL
& 0x08);
1163 GPIOL_INT_CLR
= 0x08;
1168 mutex_unlock(&sd_mtx
);
1173 /* move the sd-card info to mmc struct */
1174 tCardInfo
*card_get_info_target(int card_no
)
1177 static tCardInfo card
;
1178 static const char mantissa
[] = { /* *10 */
1179 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
1180 static const int exponent
[] = { /* use varies */
1181 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 };
1183 card
.initialized
= card_info
[card_no
].initialized
;
1184 card
.ocr
= card_info
[card_no
].ocr
;
1185 for(i
=0; i
<4; i
++) card
.csd
[i
] = card_info
[card_no
].csd
[3-i
];
1186 for(i
=0; i
<4; i
++) card
.cid
[i
] = card_info
[card_no
].cid
[3-i
];
1187 card
.numblocks
= card_info
[card_no
].numblocks
;
1188 card
.blocksize
= card_info
[card_no
].block_size
;
1189 temp
= card_extract_bits(card
.csd
, 29, 3);
1190 card
.speed
= mantissa
[card_extract_bits(card
.csd
, 25, 4)]
1191 * exponent
[temp
> 2 ? 7 : temp
+ 4];
1192 card
.nsac
= 100 * card_extract_bits(card
.csd
, 16, 8);
1193 temp
= card_extract_bits(card
.csd
, 13, 3);
1194 card
.tsac
= mantissa
[card_extract_bits(card
.csd
, 9, 4)]
1195 * exponent
[temp
] / 10;
1196 card
.cid
[0] = htobe32(card
.cid
[0]); /* ascii chars here */
1197 card
.cid
[1] = htobe32(card
.cid
[1]); /* ascii chars here */
1198 temp
= *((char*)card
.cid
+13); /* adjust year<=>month, 1997 <=> 2000 */
1199 *((char*)card
.cid
+13) = (unsigned char)((temp
>> 4) | (temp
<< 4)) + 3;
1204 bool card_detect_target(void)
1208 return (GPIOA_INPUT_VAL
& 0x80) == 0; /* low active */
1209 #elif defined SANSA_C200
1210 return (GPIOL_INPUT_VAL
& 0x08) != 0; /* high active */
1218 static int sd1_oneshot_callback(struct timeout
*tmo
)
1222 /* This is called only if the state was stable for 300ms - check state
1223 * and post appropriate event. */
1224 if (card_detect_target())
1225 queue_broadcast(SYS_HOTSWAP_INSERTED
, 0);
1227 queue_broadcast(SYS_HOTSWAP_EXTRACTED
, 0);
1232 /* called on insertion/removal interrupt */
1233 void microsd_int(void)
1235 static struct timeout sd1_oneshot
;
1238 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1239 GPIOA_INT_LEV
= (0x80 << 8) | (~GPIOA_INPUT_VAL
& 0x80);
1240 GPIOA_INT_CLR
= 0x80;
1241 GPIO_SET_BITWISE(GPIOA_INT_EN
, 0x80);
1243 #elif defined SANSA_C200
1244 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1245 GPIOL_INT_LEV
= (0x08 << 8) | (~GPIOL_INPUT_VAL
& 0x08);
1246 GPIOL_INT_CLR
= 0x08;
1247 GPIO_SET_BITWISE(GPIOL_INT_EN
, 0x08);
1249 timeout_register(&sd1_oneshot
, sd1_oneshot_callback
, (3*HZ
/10), 0);
1251 #endif /* HAVE_HOTSWAP */
1253 long sd_last_disk_activity(void)
1255 return last_disk_activity
;
1258 #ifdef STORAGE_GET_INFO
1259 void sd_get_info(IF_MV2(int drive
,) struct storage_info
*info
)
1261 #ifndef HAVE_MULTIVOLUME
1264 info
->sector_size
=card_info
[drive
].block_size
;
1265 info
->num_sectors
=card_info
[drive
].numblocks
;
1266 info
->vendor
="Rockbox";
1269 info
->product
="Internal Storage";
1273 info
->product
="SD Card Slot";
1275 info
->revision
="0.00";
1280 bool sd_removable(IF_MV_NONVOID(int drive
))
1282 #ifndef HAVE_MULTIVOLUME
1288 bool sd_present(IF_MV_NONVOID(int drive
))
1290 #ifndef HAVE_MULTIVOLUME
1293 return (card_info
[drive
].initialized
&& card_info
[drive
].numblocks
> 0);
1305 void sd_spindown(int seconds
)