1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Daniel Ankers
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 #include "ata-sd-target.h"
22 #include "ata_idle_notify.h"
32 #define BLOCK_SIZE 512
33 #define SECTOR_SIZE 512
34 #define BLOCKS_PER_BANK 0x7a7800
36 #define STATUS_REG (*(volatile unsigned int *)(0x70008204))
37 #define REG_1 (*(volatile unsigned int *)(0x70008208))
38 #define UNKNOWN (*(volatile unsigned int *)(0x70008210))
39 #define BLOCK_SIZE_REG (*(volatile unsigned int *)(0x7000821c))
40 #define BLOCK_COUNT_REG (*(volatile unsigned int *)(0x70008220))
41 #define REG_5 (*(volatile unsigned int *)(0x70008224))
42 #define CMD_REG0 (*(volatile unsigned int *)(0x70008228))
43 #define CMD_REG1 (*(volatile unsigned int *)(0x7000822c))
44 #define CMD_REG2 (*(volatile unsigned int *)(0x70008230))
45 #define RESPONSE_REG (*(volatile unsigned int *)(0x70008234))
46 #define SD_STATE_REG (*(volatile unsigned int *)(0x70008238))
47 #define REG_11 (*(volatile unsigned int *)(0x70008240))
48 #define REG_12 (*(volatile unsigned int *)(0x70008244))
49 #define DATA_REG (*(volatile unsigned int *)(0x70008280))
52 #define DATA_DONE (1 << 12)
53 #define CMD_DONE (1 << 13)
54 #define ERROR_BITS (0x3f)
55 #define READY_FOR_DATA (1 << 8)
56 #define FIFO_FULL (1 << 7)
57 #define FIFO_EMPTY (1 << 6)
59 #define CMD_OK 0x0 /* Command was successful */
60 #define CMD_ERROR_2 0x2 /* SD did not respond to command (either it doesn't
61 understand the command or is not inserted) */
74 #define FIFO_LEN 16 /* FIFO is 16 words deep */
77 #define GO_IDLE_STATE 0
78 #define ALL_SEND_CID 2
79 #define SEND_RELATIVE_ADDR 3
83 #define DESELECT_CARD 7
84 #define SEND_IF_COND 8
87 #define STOP_TRANSMISSION 12
88 #define SEND_STATUS 13
89 #define GO_INACTIVE_STATE 15
90 #define SET_BLOCKLEN 16
91 #define READ_SINGLE_BLOCK 17
92 #define READ_MULTIPLE_BLOCK 18
93 #define SEND_NUM_WR_BLOCKS 22
94 #define WRITE_BLOCK 24
95 #define WRITE_MULTIPLE_BLOCK 25
96 #define ERASE_WR_BLK_START 32
97 #define ERASE_WR_BLK_END 33
104 #define EC_WAIT_STATE_FAILED 3
105 #define EC_CHECK_TIMEOUT_FAILED 4
106 #define EC_POWER_UP 5
107 #define EC_READ_TIMEOUT 6
108 #define EC_WRITE_TIMEOUT 7
109 #define EC_TRAN_SEL_BANK 8
110 #define EC_TRAN_READ_ENTRY 9
111 #define EC_TRAN_READ_EXIT 10
112 #define EC_TRAN_WRITE_ENTRY 11
113 #define EC_TRAN_WRITE_EXIT 12
114 #define EC_FIFO_SEL_BANK_EMPTY 13
115 #define EC_FIFO_SEL_BANK_DONE 14
116 #define EC_FIFO_ENA_BANK_EMPTY 15
117 #define EC_FIFO_READ_FULL 16
118 #define EC_FIFO_WR_EMPTY 17
119 #define EC_FIFO_WR_DONE 18
120 #define EC_COMMAND 19
123 /* Application Specific commands */
124 #define SET_BUS_WIDTH 6
125 #define SD_APP_OP_COND 41
127 /** global, exported variables **/
129 #define NUM_VOLUMES 2
131 #define NUM_VOLUMES 1
134 /* for compatibility */
135 int ata_spinup_time
= 0;
137 long last_disk_activity
= -1;
139 /** static, private data **/
140 static bool initialized
= false;
142 static long next_yield
= 0;
143 #define MIN_YIELD_PERIOD 1000
145 static tSDCardInfo card_info
[2];
146 static tSDCardInfo
*currcard
= NULL
; /* current active card */
148 struct sd_card_status
154 static struct sd_card_status sd_status
[NUM_VOLUMES
] =
162 /* Shoot for around 75% usage */
163 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x1c0)/sizeof(long)];
164 static const char sd_thread_name
[] = "ata/sd";
165 static struct mutex sd_mtx SHAREDBSS_ATTR
;
166 static struct event_queue sd_queue
;
168 /* Posted when card plugged status has changed */
170 /* Actions taken by sd_thread when card status has changed */
171 enum sd_thread_actions
178 /* Private Functions */
180 static unsigned int check_time
[NUM_EC
];
182 static inline bool sd_check_timeout(long timeout
, int id
)
184 return !TIME_AFTER(USEC_TIMER
, check_time
[id
] + timeout
);
187 static bool sd_poll_status(unsigned int trigger
, long timeout
)
191 while ((STATUS_REG
& trigger
) == 0)
193 long time
= USEC_TIMER
;
195 if (TIME_AFTER(time
, next_yield
))
197 long ty
= USEC_TIMER
;
199 timeout
+= USEC_TIMER
- ty
;
200 next_yield
= ty
+ MIN_YIELD_PERIOD
;
203 if (TIME_AFTER(time
, t
+ timeout
))
210 static int sd_command(unsigned int cmd
, unsigned long arg1
,
211 unsigned int *response
, unsigned int type
)
213 int i
, words
; /* Number of 16 bit words to read from RESPONSE_REG */
214 unsigned int data
[9];
217 CMD_REG1
= (unsigned int)((arg1
& 0xffff0000) >> 16);
218 CMD_REG2
= (unsigned int)((arg1
& 0xffff));
221 if (!sd_poll_status(CMD_DONE
, 100000))
224 if ((STATUS_REG
& ERROR_BITS
) != CMD_OK
)
225 /* Error sending command */
226 return -EC_COMMAND
- (STATUS_REG
& ERROR_BITS
)*100;
228 if (cmd
== GO_IDLE_STATE
)
229 return 0; /* no response here */
231 words
= (type
== 2) ? 9 : 3;
233 for (i
= 0; i
< words
; i
++) /* RESPONSE_REG is read MSB first */
234 data
[i
] = RESPONSE_REG
; /* Read most significant 16-bit word */
236 if (response
== NULL
)
238 /* response discarded */
242 /* Response type 2 has the following structure:
243 * [135:135] Start Bit - '0'
244 * [134:134] Transmission bit - '0'
245 * [133:128] Reserved - '111111'
246 * [127:001] CID or CSD register including internal CRC7
247 * [000:000] End Bit - '1'
249 response
[3] = (data
[0]<<24) + (data
[1]<<8) + (data
[2]>>8);
250 response
[2] = (data
[2]<<24) + (data
[3]<<8) + (data
[4]>>8);
251 response
[1] = (data
[4]<<24) + (data
[5]<<8) + (data
[6]>>8);
252 response
[0] = (data
[6]<<24) + (data
[7]<<8) + (data
[8]>>8);
256 /* Response types 1, 1b, 3, 6, 7 have the following structure:
257 * Types 4 and 5 are not supported.
259 * [47] Start bit - '0'
260 * [46] Transmission bit - '0'
261 * [45:40] R1, R1b, R6, R7: Command index
262 * R3: Reserved - '111111'
263 * [39:8] R1, R1b: Card Status
266 * [15: 0] Card Status Bits 23, 22, 19, 12:0
268 * [22] ILLEGAL_COMMAND
270 * [12:9] CURRENT_STATE
277 * [1:0] Reserved for test mode
278 * R7: [19:16] Voltage accepted
279 * [15:8] echo-back of check pattern
280 * [7:1] R1, R1b: CRC7
281 * R3: Reserved - '1111111'
284 response
[0] = (data
[0]<<24) + (data
[1]<<8) + (data
[2]>>8);
290 static int sd_wait_for_state(unsigned int state
, int id
)
292 unsigned int response
= 0;
293 unsigned int timeout
= 0x80000;
295 check_time
[id
] = USEC_TIMER
;
299 int ret
= sd_command(SEND_STATUS
, currcard
->rca
, &response
, 1);
305 if (((response
>> 9) & 0xf) == state
)
307 SD_STATE_REG
= state
;
311 if (!sd_check_timeout(timeout
, id
))
312 return -EC_WAIT_STATE_FAILED
*100 - id
;
315 if (TIME_AFTER(us
, next_yield
))
318 timeout
+= USEC_TIMER
- us
;
319 next_yield
= us
+ MIN_YIELD_PERIOD
;
324 static inline void copy_read_sectors_fast(unsigned char **buf
)
326 /* Copy one chunk of 16 words using best method for start alignment */
327 switch ( (intptr_t)*buf
& 3 )
331 "ldmia %[data], { r2-r9 } \r\n"
332 "orr r2, r2, r3, lsl #16 \r\n"
333 "orr r4, r4, r5, lsl #16 \r\n"
334 "orr r6, r6, r7, lsl #16 \r\n"
335 "orr r8, r8, r9, lsl #16 \r\n"
336 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
337 "ldmia %[data], { r2-r9 } \r\n"
338 "orr r2, r2, r3, lsl #16 \r\n"
339 "orr r4, r4, r5, lsl #16 \r\n"
340 "orr r6, r6, r7, lsl #16 \r\n"
341 "orr r8, r8, r9, lsl #16 \r\n"
342 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
344 : [data
]"r"(&DATA_REG
)
345 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9"
350 "ldmia %[data], { r2-r9 } \r\n"
351 "orr r3, r2, r3, lsl #16 \r\n"
352 "strb r3, [%[buf]], #1 \r\n"
353 "mov r3, r3, lsr #8 \r\n"
354 "strh r3, [%[buf]], #2 \r\n"
355 "mov r3, r3, lsr #16 \r\n"
356 "orr r3, r3, r4, lsl #8 \r\n"
357 "orr r3, r3, r5, lsl #24 \r\n"
358 "mov r5, r5, lsr #8 \r\n"
359 "orr r5, r5, r6, lsl #8 \r\n"
360 "orr r5, r5, r7, lsl #24 \r\n"
361 "mov r7, r7, lsr #8 \r\n"
362 "orr r7, r7, r8, lsl #8 \r\n"
363 "orr r7, r7, r9, lsl #24 \r\n"
364 "mov r2, r9, lsr #8 \r\n"
365 "stmia %[buf]!, { r3, r5, r7 } \r\n"
366 "ldmia %[data], { r3-r10 } \r\n"
367 "orr r2, r2, r3, lsl #8 \r\n"
368 "orr r2, r2, r4, lsl #24 \r\n"
369 "mov r4, r4, lsr #8 \r\n"
370 "orr r4, r4, r5, lsl #8 \r\n"
371 "orr r4, r4, r6, lsl #24 \r\n"
372 "mov r6, r6, lsr #8 \r\n"
373 "orr r6, r6, r7, lsl #8 \r\n"
374 "orr r6, r6, r8, lsl #24 \r\n"
375 "mov r8, r8, lsr #8 \r\n"
376 "orr r8, r8, r9, lsl #8 \r\n"
377 "orr r8, r8, r10, lsl #24 \r\n"
378 "mov r10, r10, lsr #8 \r\n"
379 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
380 "strb r10, [%[buf]], #1 \r\n"
382 : [data
]"r"(&DATA_REG
)
383 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
388 "ldmia %[data], { r2-r9 } \r\n"
389 "strh r2, [%[buf]], #2 \r\n"
390 "orr r3, r3, r4, lsl #16 \r\n"
391 "orr r5, r5, r6, lsl #16 \r\n"
392 "orr r7, r7, r8, lsl #16 \r\n"
393 "stmia %[buf]!, { r3, r5, r7 } \r\n"
394 "ldmia %[data], { r2-r8, r10 } \r\n"
395 "orr r2, r9, r2, lsl #16 \r\n"
396 "orr r3, r3, r4, lsl #16 \r\n"
397 "orr r5, r5, r6, lsl #16 \r\n"
398 "orr r7, r7, r8, lsl #16 \r\n"
399 "stmia %[buf]!, { r2, r3, r5, r7 } \r\n"
400 "strh r10, [%[buf]], #2 \r\n"
402 : [data
]"r"(&DATA_REG
)
403 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
408 "ldmia %[data], { r2-r9 } \r\n"
409 "orr r3, r2, r3, lsl #16 \r\n"
410 "strb r3, [%[buf]], #1 \r\n"
411 "mov r3, r3, lsr #8 \r\n"
412 "orr r3, r3, r4, lsl #24 \r\n"
413 "mov r4, r4, lsr #8 \r\n"
414 "orr r5, r4, r5, lsl #8 \r\n"
415 "orr r5, r5, r6, lsl #24 \r\n"
416 "mov r6, r6, lsr #8 \r\n"
417 "orr r7, r6, r7, lsl #8 \r\n"
418 "orr r7, r7, r8, lsl #24 \r\n"
419 "mov r8, r8, lsr #8 \r\n"
420 "orr r2, r8, r9, lsl #8 \r\n"
421 "stmia %[buf]!, { r3, r5, r7 } \r\n"
422 "ldmia %[data], { r3-r10 } \r\n"
423 "orr r2, r2, r3, lsl #24 \r\n"
424 "mov r3, r3, lsr #8 \r\n"
425 "orr r4, r3, r4, lsl #8 \r\n"
426 "orr r4, r4, r5, lsl #24 \r\n"
427 "mov r5, r5, lsr #8 \r\n"
428 "orr r6, r5, r6, lsl #8 \r\n"
429 "orr r6, r6, r7, lsl #24 \r\n"
430 "mov r7, r7, lsr #8 \r\n"
431 "orr r8, r7, r8, lsl #8 \r\n"
432 "orr r8, r8, r9, lsl #24 \r\n"
433 "mov r9, r9, lsr #8 \r\n"
434 "orr r10, r9, r10, lsl #8 \r\n"
435 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
436 "strh r10, [%[buf]], #2 \r\n"
437 "mov r10, r10, lsr #16 \r\n"
438 "strb r10, [%[buf]], #1 \r\n"
440 : [data
]"r"(&DATA_REG
)
441 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
447 static inline void copy_read_sectors_slow(unsigned char** buf
)
452 /* Copy one chunk of 16 words */
455 "ldrh %[t], [%[data]] \r\n"
456 "strb %[t], [%[buf]], #1 \r\n"
457 "mov %[t], %[t], lsr #8 \r\n"
458 "strb %[t], [%[buf]], #1 \r\n"
459 "subs %[cnt], %[cnt], #1 \r\n"
461 : [cnt
]"+&r"(cnt
), [buf
]"+&r"(*buf
),
463 : [data
]"r"(&DATA_REG
)
467 /* Writes have to be kept slow for now */
468 static inline void copy_write_sectors(const unsigned char** buf
)
478 } while (--cnt
> 0); /* tail loop is faster */
481 static int sd_select_bank(unsigned char bank
)
483 unsigned char card_data
[512];
484 const unsigned char* write_buf
;
487 memset(card_data
, 0, 512);
489 ret
= sd_wait_for_state(TRAN
, EC_TRAN_SEL_BANK
);
493 BLOCK_SIZE_REG
= 512;
496 ret
= sd_command(35, 0, NULL
, 0x1c0d); /* CMD35 is vendor specific */
504 /* Write the card data */
505 write_buf
= card_data
;
506 for (i
= 0; i
< BLOCK_SIZE
/2; i
+= FIFO_LEN
)
508 /* Wait for the FIFO to empty */
509 if (sd_poll_status(FIFO_EMPTY
, 10000))
511 copy_write_sectors(&write_buf
); /* Copy one chunk of 16 words */
515 return -EC_FIFO_SEL_BANK_EMPTY
;
518 if (!sd_poll_status(DATA_DONE
, 10000))
519 return -EC_FIFO_SEL_BANK_DONE
;
521 currcard
->current_bank
= bank
;
526 static void sd_card_mux(int card_no
)
528 /* Set the current card mux */
529 #if defined(SANSA_E200) || defined(PHILIPS_SA9200)
534 GPIO_CLEAR_BITWISE(GPIOA_ENABLE
, 0x7a);
535 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x7a);
536 GPIO_SET_BITWISE(GPIOD_ENABLE
, 0x1f);
537 GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL
, 0x1f);
538 GPIO_SET_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
540 outl((inl(0x70000014) & ~(0x3ffff)) | 0x255aa, 0x70000014);
546 GPIO_CLEAR_BITWISE(GPIOD_ENABLE
, 0x1f);
547 GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
548 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x7a);
549 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL
, 0x7a);
550 GPIO_SET_BITWISE( GPIOA_OUTPUT_EN
, 0x7a);
552 outl(inl(0x70000014) & ~(0x3ffff), 0x70000014);
554 #else /* SANSA_C200 */
559 GPIO_CLEAR_BITWISE(GPIOD_ENABLE
, 0x1f);
560 GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
561 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x7a);
562 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL
, 0x7a);
563 GPIO_SET_BITWISE( GPIOA_OUTPUT_EN
, 0x7a);
565 outl(inl(0x70000014) & ~(0x3ffff), 0x70000014);
571 GPIO_CLEAR_BITWISE(GPIOA_ENABLE
, 0x7a);
572 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x7a);
573 GPIO_SET_BITWISE(GPIOD_ENABLE
, 0x1f);
574 GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL
, 0x1f);
575 GPIO_SET_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
577 outl((inl(0x70000014) & ~(0x3ffff)) | 0x255aa, 0x70000014);
582 static void sd_init_device(int card_no
)
584 /* SD Protocol registers */
586 unsigned int response
= 0;
590 unsigned long c_mult
;
591 unsigned char carddata
[512];
592 unsigned char *dataptr
;
595 /* Enable and initialise controller */
598 /* Initialise card data as blank */
599 memset(currcard
, 0, sizeof(*currcard
));
601 /* Switch card mux to card to initialize */
602 sd_card_mux(card_no
);
607 REG_12
&= ~(3 << 12);
609 REG_11
&= ~(3 << 12);
612 DEV_EN
|= DEV_ATA
; /* Enable controller */
613 DEV_RS
|= DEV_ATA
; /* Reset controller */
614 DEV_RS
&=~DEV_ATA
; /* Clear Reset */
620 ret
= sd_command(GO_IDLE_STATE
, 0, NULL
, 256);
622 goto card_init_error
;
624 check_time
[EC_POWER_UP
] = USEC_TIMER
;
628 - non-SDHC cards simply ignore SEND_IF_COND (CMD8) and we get error -219,
629 which we can just ignore and assume we're dealing with standard SD.
630 - SDHC cards echo back the argument into the response. This is how we
631 tell if the card is SDHC.
633 ret
= sd_command(SEND_IF_COND
,0x1aa, &response
,7);
634 if ( (ret
< 0) && (ret
!=-219) )
635 goto card_init_error
;
638 while ((currcard
->ocr
& (1 << 31)) == 0) /* until card is powered up */
640 ret
= sd_command(APP_CMD
, currcard
->rca
, NULL
, 1);
642 goto card_init_error
;
645 if(response
== 0x1aa)
648 ret
= sd_command(SD_APP_OP_COND
, (1<<30)|0x100000,
652 #endif /* HAVE_HOTSWAP */
655 ret
= sd_command(SD_APP_OP_COND
, 0x100000, &currcard
->ocr
, 3);
659 goto card_init_error
;
661 if (!sd_check_timeout(5000000, EC_POWER_UP
))
664 goto card_init_error
;
668 ret
= sd_command(ALL_SEND_CID
, 0, currcard
->cid
, 2);
670 goto card_init_error
;
672 ret
= sd_command(SEND_RELATIVE_ADDR
, 0, &currcard
->rca
, 1);
674 goto card_init_error
;
676 ret
= sd_command(SEND_CSD
, currcard
->rca
, currcard
->csd
, 2);
678 goto card_init_error
;
680 /* These calculations come from the Sandisk SD card product manual */
681 if( (currcard
->csd
[3]>>30) == 0)
683 /* CSD version 1.0 */
684 c_size
= ((currcard
->csd
[2] & 0x3ff) << 2) + (currcard
->csd
[1]>>30) + 1;
685 c_mult
= 4 << ((currcard
->csd
[1] >> 15) & 7);
686 currcard
->max_read_bl_len
= 1 << ((currcard
->csd
[2] >> 16) & 15);
687 currcard
->block_size
= BLOCK_SIZE
; /* Always use 512 byte blocks */
688 currcard
->numblocks
= c_size
* c_mult
* (currcard
->max_read_bl_len
/512);
689 currcard
->capacity
= currcard
->numblocks
* currcard
->block_size
;
692 else if( (currcard
->csd
[3]>>30) == 1)
694 /* CSD version 2.0 */
695 c_size
= ((currcard
->csd
[2] & 0x3f) << 16) + (currcard
->csd
[1]>>16) + 1;
696 currcard
->max_read_bl_len
= 1 << ((currcard
->csd
[2] >> 16) & 0xf);
697 currcard
->block_size
= BLOCK_SIZE
; /* Always use 512 byte blocks */
698 currcard
->numblocks
= c_size
<< 10;
699 currcard
->capacity
= currcard
->numblocks
* currcard
->block_size
;
701 #endif /* HAVE_HOTSWAP */
705 ret
= sd_command(SELECT_CARD
, currcard
->rca
, NULL
, 129);
707 goto card_init_error
;
709 ret
= sd_command(APP_CMD
, currcard
->rca
, NULL
, 1);
711 goto card_init_error
;
713 ret
= sd_command(SET_BUS_WIDTH
, currcard
->rca
| 2, NULL
, 1); /* 4 bit */
715 goto card_init_error
;
717 ret
= sd_command(SET_BLOCKLEN
, currcard
->block_size
, NULL
, 1);
719 goto card_init_error
;
721 BLOCK_SIZE_REG
= currcard
->block_size
;
723 /* If this card is >4GB & not SDHC, then we need to enable bank switching */
724 if( (currcard
->numblocks
>= BLOCKS_PER_BANK
) &&
725 ((currcard
->ocr
& (1<<30)) == 0) )
730 ret
= sd_command(SWITCH_FUNC
, 0x80ffffef, NULL
, 0x1c05);
732 goto card_init_error
;
734 /* Read 512 bytes from the card.
735 The first 512 bits contain the status information
736 TODO: Do something useful with this! */
738 for (i
= 0; i
< BLOCK_SIZE
/2; i
+= FIFO_LEN
)
740 /* Wait for the FIFO to be full */
741 if (sd_poll_status(FIFO_FULL
, 100000))
743 copy_read_sectors_slow(&dataptr
);
747 ret
= -EC_FIFO_ENA_BANK_EMPTY
;
748 goto card_init_error
;
752 currcard
->initialized
= 1;
755 /* Card failed to initialize so disable it */
757 currcard
->initialized
= ret
;
760 /* lock must already be aquired */
761 static void sd_select_device(int card_no
)
763 currcard
= &card_info
[card_no
];
767 /* Main card always gets a chance */
768 sd_status
[0].retry
= 0;
771 if (currcard
->initialized
> 0)
773 /* This card is already initialized - switch to it */
774 sd_card_mux(card_no
);
778 if (currcard
->initialized
== 0)
780 /* Card needs (re)init */
781 sd_init_device(card_no
);
787 static void ata_led(bool onoff
)
792 int ata_read_sectors(IF_MV2(int drive
,) unsigned long start
, int incount
,
799 unsigned char *buf
, *buf_end
;
802 /* TODO: Add DMA support. */
809 if (drive
!= 0 && !card_detect_target())
811 /* no external sd-card inserted */
816 sd_select_device(drive
);
818 if (currcard
->initialized
< 0)
820 ret
= currcard
->initialized
;
824 last_disk_activity
= current_tick
;
826 /* Only switch banks with non-SDHC cards */
827 if((currcard
->ocr
& (1<<30))==0)
829 bank
= start
/ BLOCKS_PER_BANK
;
831 if (currcard
->current_bank
!= bank
)
833 ret
= sd_select_bank(bank
);
838 start
-= bank
* BLOCKS_PER_BANK
;
841 ret
= sd_wait_for_state(TRAN
, EC_TRAN_READ_ENTRY
);
845 BLOCK_COUNT_REG
= incount
;
848 if(currcard
->ocr
& (1<<30) )
851 ret
= sd_command(READ_MULTIPLE_BLOCK
, start
, NULL
, 0x1c25);
856 ret
= sd_command(READ_MULTIPLE_BLOCK
, start
* BLOCK_SIZE
, NULL
, 0x1c25);
861 /* TODO: Don't assume BLOCK_SIZE == SECTOR_SIZE */
863 buf_end
= (unsigned char *)inbuf
+ incount
* currcard
->block_size
;
864 for (buf
= inbuf
; buf
< buf_end
;)
866 /* Wait for the FIFO to be full */
867 if (sd_poll_status(FIFO_FULL
, 0x80000))
869 copy_read_sectors_fast(&buf
); /* Copy one chunk of 16 words */
870 /* TODO: Switch bank if necessary */
874 ret
= -EC_FIFO_READ_FULL
;
878 last_disk_activity
= current_tick
;
880 ret
= sd_command(STOP_TRANSMISSION
, 0, NULL
, 1);
884 ret
= sd_wait_for_state(TRAN
, EC_TRAN_READ_EXIT
);
891 mutex_unlock(&sd_mtx
);
896 if (sd_status
[drive
].retry
< sd_status
[drive
].retry_max
897 && ret
!= -EC_NOCARD
)
899 sd_status
[drive
].retry
++;
900 currcard
->initialized
= 0;
906 int ata_write_sectors(IF_MV2(int drive
,) unsigned long start
, int count
,
909 /* Write support is not finished yet */
910 /* TODO: The standard suggests using ACMD23 prior to writing multiple blocks
911 to improve performance */
916 const unsigned char *buf
, *buf_end
;
924 if (drive
!= 0 && !card_detect_target())
926 /* no external sd-card inserted */
928 goto ata_write_error
;
931 sd_select_device(drive
);
933 if (currcard
->initialized
< 0)
935 ret
= currcard
->initialized
;
936 goto ata_write_error
;
939 /* Only switch banks with non-SDHC cards */
940 if((currcard
->ocr
& (1<<30))==0)
942 bank
= start
/ BLOCKS_PER_BANK
;
944 if (currcard
->current_bank
!= bank
)
946 ret
= sd_select_bank(bank
);
948 goto ata_write_error
;
951 start
-= bank
* BLOCKS_PER_BANK
;
954 check_time
[EC_WRITE_TIMEOUT
] = USEC_TIMER
;
956 ret
= sd_wait_for_state(TRAN
, EC_TRAN_WRITE_ENTRY
);
958 goto ata_write_error
;
960 BLOCK_COUNT_REG
= count
;
963 if(currcard
->ocr
& (1<<30) )
966 ret
= sd_command(WRITE_MULTIPLE_BLOCK
, start
, NULL
, 0x1c2d);
971 ret
= sd_command(WRITE_MULTIPLE_BLOCK
, start
*BLOCK_SIZE
, NULL
, 0x1c2d);
974 goto ata_write_error
;
976 buf_end
= outbuf
+ count
* currcard
->block_size
- 2*FIFO_LEN
;
978 for (buf
= outbuf
; buf
<= buf_end
;)
982 /* Set SD_STATE_REG to PRG for the last buffer fill */
986 udelay(2); /* needed here (loop is too fast :-) */
988 /* Wait for the FIFO to empty */
989 if (sd_poll_status(FIFO_EMPTY
, 0x80000))
991 copy_write_sectors(&buf
); /* Copy one chunk of 16 words */
992 /* TODO: Switch bank if necessary */
996 ret
= -EC_FIFO_WR_EMPTY
;
997 goto ata_write_error
;
1000 last_disk_activity
= current_tick
;
1002 if (!sd_poll_status(DATA_DONE
, 0x80000))
1004 ret
= -EC_FIFO_WR_DONE
;
1005 goto ata_write_error
;
1008 ret
= sd_command(STOP_TRANSMISSION
, 0, NULL
, 1);
1010 goto ata_write_error
;
1012 ret
= sd_wait_for_state(TRAN
, EC_TRAN_WRITE_EXIT
);
1014 goto ata_write_error
;
1019 mutex_unlock(&sd_mtx
);
1024 if (sd_status
[drive
].retry
< sd_status
[drive
].retry_max
1025 && ret
!= -EC_NOCARD
)
1027 sd_status
[drive
].retry
++;
1028 currcard
->initialized
= 0;
1029 goto ata_write_retry
;
1034 static void sd_thread(void) __attribute__((noreturn
));
1035 static void sd_thread(void)
1037 struct queue_event ev
;
1038 bool idle_notified
= false;
1042 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
1047 case SYS_HOTSWAP_INSERTED
:
1048 case SYS_HOTSWAP_EXTRACTED
:
1049 fat_lock(); /* lock-out FAT activity first -
1050 prevent deadlocking via disk_mount that
1051 would cause a reverse-order attempt with
1053 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
1054 into driver that bypass the fat cache */
1056 /* We now have exclusive control of fat cache and ata */
1058 disk_unmount(1); /* release "by force", ensure file
1059 descriptors aren't leaked and any busy
1060 ones are invalid if mounting */
1062 /* Force card init for new card, re-init for re-inserted one or
1063 * clear if the last attempt to init failed with an error. */
1064 card_info
[1].initialized
= 0;
1065 sd_status
[1].retry
= 0;
1067 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
1070 queue_broadcast(SYS_FS_CHANGED
, 0);
1072 /* Access is now safe */
1073 mutex_unlock(&sd_mtx
);
1078 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
1080 idle_notified
= false;
1084 /* never let a timer wrap confuse us */
1085 next_yield
= USEC_TIMER
;
1089 call_ata_idle_notifys(false);
1090 idle_notified
= true;
1094 case SYS_USB_CONNECTED
:
1095 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
1096 /* Wait until the USB cable is extracted again */
1097 usb_wait_for_disconnect(&sd_queue
);
1100 case SYS_USB_DISCONNECTED
:
1101 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
1108 void ata_spindown(int seconds
)
1113 bool ata_disk_is_active(void)
1118 void ata_sleep(void)
1126 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
1127 int ata_hard_reset(void)
1132 int ata_soft_reset(void)
1137 void ata_enable(bool on
)
1141 DEV_EN
|= DEV_ATA
; /* Enable controller */
1145 DEV_EN
&= ~DEV_ATA
; /* Disable controller */
1150 void card_enable_monitoring_target(bool on
)
1155 GPIO_SET_BITWISE(GPIOA_INT_EN
, 0x80);
1156 #elif defined(SANSA_C200)
1157 GPIO_SET_BITWISE(GPIOL_INT_EN
, 0x08);
1163 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1164 #elif defined(SANSA_C200)
1165 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1176 mutex_init(&sd_mtx
);
1178 mutex_lock(&sd_mtx
);
1186 /* init controller */
1187 outl(inl(0x70000088) & ~(0x4), 0x70000088);
1188 outl(inl(0x7000008c) & ~(0x4), 0x7000008c);
1189 GPO32_ENABLE
|= 0x4;
1191 GPIO_SET_BITWISE(GPIOG_ENABLE
, (0x3 << 5));
1192 GPIO_SET_BITWISE(GPIOG_OUTPUT_EN
, (0x3 << 5));
1193 GPIO_SET_BITWISE(GPIOG_OUTPUT_VAL
, (0x3 << 5));
1196 /* enable card detection port - mask interrupt first */
1198 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1200 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x80);
1201 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x80);
1202 #elif defined SANSA_C200
1203 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1205 GPIO_CLEAR_BITWISE(GPIOL_OUTPUT_EN
, 0x08);
1206 GPIO_SET_BITWISE(GPIOL_ENABLE
, 0x08);
1209 sd_select_device(0);
1211 if (currcard
->initialized
< 0)
1212 ret
= currcard
->initialized
;
1214 queue_init(&sd_queue
, true);
1215 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
1216 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
)
1219 /* enable interupt for the mSD card */
1223 CPU_INT_EN
= HI_MASK
;
1224 CPU_HI_INT_EN
= GPIO0_MASK
;
1226 GPIOA_INT_LEV
= (0x80 << 8) | (~GPIOA_INPUT_VAL
& 0x80);
1228 GPIOA_INT_CLR
= 0x80;
1229 #elif defined SANSA_C200
1230 CPU_INT_EN
= HI_MASK
;
1231 CPU_HI_INT_EN
= GPIO2_MASK
;
1233 GPIOL_INT_LEV
= (0x08 << 8) | (~GPIOL_INPUT_VAL
& 0x08);
1235 GPIOL_INT_CLR
= 0x08;
1240 mutex_unlock(&sd_mtx
);
1245 /* move the sd-card info to mmc struct */
1246 tCardInfo
*card_get_info_target(int card_no
)
1249 static tCardInfo card
;
1250 static const char mantissa
[] = { /* *10 */
1251 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
1252 static const int exponent
[] = { /* use varies */
1253 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 };
1255 card
.initialized
= card_info
[card_no
].initialized
;
1256 card
.ocr
= card_info
[card_no
].ocr
;
1257 for(i
=0; i
<4; i
++) card
.csd
[i
] = card_info
[card_no
].csd
[3-i
];
1258 for(i
=0; i
<4; i
++) card
.cid
[i
] = card_info
[card_no
].cid
[3-i
];
1259 card
.numblocks
= card_info
[card_no
].numblocks
;
1260 card
.blocksize
= card_info
[card_no
].block_size
;
1261 card
.size
= card_info
[card_no
].capacity
< 0xffffffff ?
1262 card_info
[card_no
].capacity
: 0xffffffff;
1263 card
.block_exp
= card_info
[card_no
].block_exp
;
1264 temp
= card_extract_bits(card
.csd
, 29, 3);
1265 card
.speed
= mantissa
[card_extract_bits(card
.csd
, 25, 4)]
1266 * exponent
[temp
> 2 ? 7 : temp
+ 4];
1267 card
.nsac
= 100 * card_extract_bits(card
.csd
, 16, 8);
1268 temp
= card_extract_bits(card
.csd
, 13, 3);
1269 card
.tsac
= mantissa
[card_extract_bits(card
.csd
, 9, 4)]
1270 * exponent
[temp
] / 10;
1271 card
.cid
[0] = htobe32(card
.cid
[0]); /* ascii chars here */
1272 card
.cid
[1] = htobe32(card
.cid
[1]); /* ascii chars here */
1273 temp
= *((char*)card
.cid
+13); /* adjust year<=>month, 1997 <=> 2000 */
1274 *((char*)card
.cid
+13) = (unsigned char)((temp
>> 4) | (temp
<< 4)) + 3;
1279 bool card_detect_target(void)
1283 return (GPIOA_INPUT_VAL
& 0x80) == 0; /* low active */
1284 #elif defined SANSA_C200
1285 return (GPIOL_INPUT_VAL
& 0x08) != 0; /* high active */
1293 static bool sd1_oneshot_callback(struct timeout
*tmo
)
1297 /* This is called only if the state was stable for 300ms - check state
1298 * and post appropriate event. */
1299 if (card_detect_target())
1300 queue_broadcast(SYS_HOTSWAP_INSERTED
, 0);
1302 queue_broadcast(SYS_HOTSWAP_EXTRACTED
, 0);
1307 /* called on insertion/removal interrupt */
1308 void microsd_int(void)
1310 static struct timeout sd1_oneshot
;
1313 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1314 GPIOA_INT_LEV
= (0x80 << 8) | (~GPIOA_INPUT_VAL
& 0x80);
1315 GPIOA_INT_CLR
= 0x80;
1316 GPIO_SET_BITWISE(GPIOA_INT_EN
, 0x80);
1318 #elif defined SANSA_C200
1319 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1320 GPIOL_INT_LEV
= (0x08 << 8) | (~GPIOL_INPUT_VAL
& 0x08);
1321 GPIOL_INT_CLR
= 0x08;
1322 GPIO_SET_BITWISE(GPIOL_INT_EN
, 0x08);
1324 timeout_register(&sd1_oneshot
, sd1_oneshot_callback
, (3*HZ
/10), 0);
1327 #endif /* HAVE_HOTSWAP */