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 ****************************************************************************/
22 #include "hotswap-target.h"
24 #include "ata-target.h"
25 #include "ata_idle_notify.h"
35 #define BLOCK_SIZE 512
36 #define SECTOR_SIZE 512
37 #define BLOCKS_PER_BANK 0x7a7800
39 #define STATUS_REG (*(volatile unsigned int *)(0x70008204))
40 #define REG_1 (*(volatile unsigned int *)(0x70008208))
41 #define UNKNOWN (*(volatile unsigned int *)(0x70008210))
42 #define BLOCK_SIZE_REG (*(volatile unsigned int *)(0x7000821c))
43 #define BLOCK_COUNT_REG (*(volatile unsigned int *)(0x70008220))
44 #define REG_5 (*(volatile unsigned int *)(0x70008224))
45 #define CMD_REG0 (*(volatile unsigned int *)(0x70008228))
46 #define CMD_REG1 (*(volatile unsigned int *)(0x7000822c))
47 #define CMD_REG2 (*(volatile unsigned int *)(0x70008230))
48 #define RESPONSE_REG (*(volatile unsigned int *)(0x70008234))
49 #define SD_STATE_REG (*(volatile unsigned int *)(0x70008238))
50 #define REG_11 (*(volatile unsigned int *)(0x70008240))
51 #define REG_12 (*(volatile unsigned int *)(0x70008244))
52 #define DATA_REG (*(volatile unsigned int *)(0x70008280))
55 #define DATA_DONE (1 << 12)
56 #define CMD_DONE (1 << 13)
57 #define ERROR_BITS (0x3f)
58 #define READY_FOR_DATA (1 << 8)
59 #define FIFO_FULL (1 << 7)
60 #define FIFO_EMPTY (1 << 6)
62 #define CMD_OK 0x0 /* Command was successful */
63 #define CMD_ERROR_2 0x2 /* SD did not respond to command (either it doesn't
64 understand the command or is not inserted) */
77 #define FIFO_LEN 16 /* FIFO is 16 words deep */
80 #define GO_IDLE_STATE 0
81 #define ALL_SEND_CID 2
82 #define SEND_RELATIVE_ADDR 3
86 #define DESELECT_CARD 7
87 #define SEND_IF_COND 8
90 #define STOP_TRANSMISSION 12
91 #define SEND_STATUS 13
92 #define GO_INACTIVE_STATE 15
93 #define SET_BLOCKLEN 16
94 #define READ_SINGLE_BLOCK 17
95 #define READ_MULTIPLE_BLOCK 18
96 #define SEND_NUM_WR_BLOCKS 22
97 #define WRITE_BLOCK 24
98 #define WRITE_MULTIPLE_BLOCK 25
99 #define ERASE_WR_BLK_START 32
100 #define ERASE_WR_BLK_END 33
107 #define EC_WAIT_STATE_FAILED 3
108 #define EC_CHECK_TIMEOUT_FAILED 4
109 #define EC_POWER_UP 5
110 #define EC_READ_TIMEOUT 6
111 #define EC_WRITE_TIMEOUT 7
112 #define EC_TRAN_SEL_BANK 8
113 #define EC_TRAN_READ_ENTRY 9
114 #define EC_TRAN_READ_EXIT 10
115 #define EC_TRAN_WRITE_ENTRY 11
116 #define EC_TRAN_WRITE_EXIT 12
117 #define EC_FIFO_SEL_BANK_EMPTY 13
118 #define EC_FIFO_SEL_BANK_DONE 14
119 #define EC_FIFO_ENA_BANK_EMPTY 15
120 #define EC_FIFO_READ_FULL 16
121 #define EC_FIFO_WR_EMPTY 17
122 #define EC_FIFO_WR_DONE 18
123 #define EC_COMMAND 19
126 /* Application Specific commands */
127 #define SET_BUS_WIDTH 6
128 #define SD_APP_OP_COND 41
130 /** global, exported variables **/
132 #define NUM_VOLUMES 2
134 #define NUM_VOLUMES 1
137 /* for compatibility */
138 int ata_spinup_time
= 0;
140 long last_disk_activity
= -1;
142 /** static, private data **/
143 static bool initialized
= false;
145 static long next_yield
= 0;
146 #define MIN_YIELD_PERIOD 1000
148 static tSDCardInfo card_info
[2];
149 static tSDCardInfo
*currcard
= NULL
; /* current active card */
151 struct sd_card_status
157 static struct sd_card_status sd_status
[NUM_VOLUMES
] =
165 /* Shoot for around 75% usage */
166 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x1c0)/sizeof(long)];
167 static const char sd_thread_name
[] = "ata/sd";
168 static struct mutex sd_mtx SHAREDBSS_ATTR
;
169 static struct event_queue sd_queue
;
171 /* Posted when card plugged status has changed */
173 /* Actions taken by sd_thread when card status has changed */
174 enum sd_thread_actions
181 /* Private Functions */
183 static unsigned int check_time
[NUM_EC
];
185 static inline bool sd_check_timeout(long timeout
, int id
)
187 return !TIME_AFTER(USEC_TIMER
, check_time
[id
] + timeout
);
190 static bool sd_poll_status(unsigned int trigger
, long timeout
)
194 while ((STATUS_REG
& trigger
) == 0)
196 long time
= USEC_TIMER
;
198 if (TIME_AFTER(time
, next_yield
))
200 long ty
= USEC_TIMER
;
202 timeout
+= USEC_TIMER
- ty
;
203 next_yield
= ty
+ MIN_YIELD_PERIOD
;
206 if (TIME_AFTER(time
, t
+ timeout
))
213 static int sd_command(unsigned int cmd
, unsigned long arg1
,
214 unsigned int *response
, unsigned int type
)
216 int i
, words
; /* Number of 16 bit words to read from RESPONSE_REG */
217 unsigned int data
[9];
220 CMD_REG1
= (unsigned int)((arg1
& 0xffff0000) >> 16);
221 CMD_REG2
= (unsigned int)((arg1
& 0xffff));
224 if (!sd_poll_status(CMD_DONE
, 100000))
227 if ((STATUS_REG
& ERROR_BITS
) != CMD_OK
)
228 /* Error sending command */
229 return -EC_COMMAND
- (STATUS_REG
& ERROR_BITS
)*100;
231 if (cmd
== GO_IDLE_STATE
)
232 return 0; /* no response here */
234 words
= (type
== 2) ? 9 : 3;
236 for (i
= 0; i
< words
; i
++) /* RESPONSE_REG is read MSB first */
237 data
[i
] = RESPONSE_REG
; /* Read most significant 16-bit word */
239 if (response
== NULL
)
241 /* response discarded */
245 /* Response type 2 has the following structure:
246 * [135:135] Start Bit - '0'
247 * [134:134] Transmission bit - '0'
248 * [133:128] Reserved - '111111'
249 * [127:001] CID or CSD register including internal CRC7
250 * [000:000] End Bit - '1'
252 response
[3] = (data
[0]<<24) + (data
[1]<<8) + (data
[2]>>8);
253 response
[2] = (data
[2]<<24) + (data
[3]<<8) + (data
[4]>>8);
254 response
[1] = (data
[4]<<24) + (data
[5]<<8) + (data
[6]>>8);
255 response
[0] = (data
[6]<<24) + (data
[7]<<8) + (data
[8]>>8);
259 /* Response types 1, 1b, 3, 6, 7 have the following structure:
260 * Types 4 and 5 are not supported.
262 * [47] Start bit - '0'
263 * [46] Transmission bit - '0'
264 * [45:40] R1, R1b, R6, R7: Command index
265 * R3: Reserved - '111111'
266 * [39:8] R1, R1b: Card Status
269 * [15: 0] Card Status Bits 23, 22, 19, 12:0
271 * [22] ILLEGAL_COMMAND
273 * [12:9] CURRENT_STATE
280 * [1:0] Reserved for test mode
281 * R7: [19:16] Voltage accepted
282 * [15:8] echo-back of check pattern
283 * [7:1] R1, R1b: CRC7
284 * R3: Reserved - '1111111'
287 response
[0] = (data
[0]<<24) + (data
[1]<<8) + (data
[2]>>8);
293 static int sd_wait_for_state(unsigned int state
, int id
)
295 unsigned int response
= 0;
296 unsigned int timeout
= 0x80000;
298 check_time
[id
] = USEC_TIMER
;
302 int ret
= sd_command(SEND_STATUS
, currcard
->rca
, &response
, 1);
308 if (((response
>> 9) & 0xf) == state
)
310 SD_STATE_REG
= state
;
314 if (!sd_check_timeout(timeout
, id
))
315 return -EC_WAIT_STATE_FAILED
*100 - id
;
318 if (TIME_AFTER(us
, next_yield
))
321 timeout
+= USEC_TIMER
- us
;
322 next_yield
= us
+ MIN_YIELD_PERIOD
;
327 static inline void copy_read_sectors_fast(unsigned char **buf
)
329 /* Copy one chunk of 16 words using best method for start alignment */
330 switch ( (intptr_t)*buf
& 3 )
334 "ldmia %[data], { r2-r9 } \r\n"
335 "orr r2, r2, r3, lsl #16 \r\n"
336 "orr r4, r4, r5, lsl #16 \r\n"
337 "orr r6, r6, r7, lsl #16 \r\n"
338 "orr r8, r8, r9, lsl #16 \r\n"
339 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
340 "ldmia %[data], { r2-r9 } \r\n"
341 "orr r2, r2, r3, lsl #16 \r\n"
342 "orr r4, r4, r5, lsl #16 \r\n"
343 "orr r6, r6, r7, lsl #16 \r\n"
344 "orr r8, r8, r9, lsl #16 \r\n"
345 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
347 : [data
]"r"(&DATA_REG
)
348 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9"
353 "ldmia %[data], { r2-r9 } \r\n"
354 "orr r3, r2, r3, lsl #16 \r\n"
355 "strb r3, [%[buf]], #1 \r\n"
356 "mov r3, r3, lsr #8 \r\n"
357 "strh r3, [%[buf]], #2 \r\n"
358 "mov r3, r3, lsr #16 \r\n"
359 "orr r3, r3, r4, lsl #8 \r\n"
360 "orr r3, r3, r5, lsl #24 \r\n"
361 "mov r5, r5, lsr #8 \r\n"
362 "orr r5, r5, r6, lsl #8 \r\n"
363 "orr r5, r5, r7, lsl #24 \r\n"
364 "mov r7, r7, lsr #8 \r\n"
365 "orr r7, r7, r8, lsl #8 \r\n"
366 "orr r7, r7, r9, lsl #24 \r\n"
367 "mov r2, r9, lsr #8 \r\n"
368 "stmia %[buf]!, { r3, r5, r7 } \r\n"
369 "ldmia %[data], { r3-r10 } \r\n"
370 "orr r2, r2, r3, lsl #8 \r\n"
371 "orr r2, r2, r4, lsl #24 \r\n"
372 "mov r4, r4, lsr #8 \r\n"
373 "orr r4, r4, r5, lsl #8 \r\n"
374 "orr r4, r4, r6, lsl #24 \r\n"
375 "mov r6, r6, lsr #8 \r\n"
376 "orr r6, r6, r7, lsl #8 \r\n"
377 "orr r6, r6, r8, lsl #24 \r\n"
378 "mov r8, r8, lsr #8 \r\n"
379 "orr r8, r8, r9, lsl #8 \r\n"
380 "orr r8, r8, r10, lsl #24 \r\n"
381 "mov r10, r10, lsr #8 \r\n"
382 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
383 "strb r10, [%[buf]], #1 \r\n"
385 : [data
]"r"(&DATA_REG
)
386 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
391 "ldmia %[data], { r2-r9 } \r\n"
392 "strh r2, [%[buf]], #2 \r\n"
393 "orr r3, r3, r4, lsl #16 \r\n"
394 "orr r5, r5, r6, lsl #16 \r\n"
395 "orr r7, r7, r8, lsl #16 \r\n"
396 "stmia %[buf]!, { r3, r5, r7 } \r\n"
397 "ldmia %[data], { r2-r8, r10 } \r\n"
398 "orr r2, r9, r2, lsl #16 \r\n"
399 "orr r3, r3, r4, lsl #16 \r\n"
400 "orr r5, r5, r6, lsl #16 \r\n"
401 "orr r7, r7, r8, lsl #16 \r\n"
402 "stmia %[buf]!, { r2, r3, r5, r7 } \r\n"
403 "strh r10, [%[buf]], #2 \r\n"
405 : [data
]"r"(&DATA_REG
)
406 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
411 "ldmia %[data], { r2-r9 } \r\n"
412 "orr r3, r2, r3, lsl #16 \r\n"
413 "strb r3, [%[buf]], #1 \r\n"
414 "mov r3, r3, lsr #8 \r\n"
415 "orr r3, r3, r4, lsl #24 \r\n"
416 "mov r4, r4, lsr #8 \r\n"
417 "orr r5, r4, r5, lsl #8 \r\n"
418 "orr r5, r5, r6, lsl #24 \r\n"
419 "mov r6, r6, lsr #8 \r\n"
420 "orr r7, r6, r7, lsl #8 \r\n"
421 "orr r7, r7, r8, lsl #24 \r\n"
422 "mov r8, r8, lsr #8 \r\n"
423 "orr r2, r8, r9, lsl #8 \r\n"
424 "stmia %[buf]!, { r3, r5, r7 } \r\n"
425 "ldmia %[data], { r3-r10 } \r\n"
426 "orr r2, r2, r3, lsl #24 \r\n"
427 "mov r3, r3, lsr #8 \r\n"
428 "orr r4, r3, r4, lsl #8 \r\n"
429 "orr r4, r4, r5, lsl #24 \r\n"
430 "mov r5, r5, lsr #8 \r\n"
431 "orr r6, r5, r6, lsl #8 \r\n"
432 "orr r6, r6, r7, lsl #24 \r\n"
433 "mov r7, r7, lsr #8 \r\n"
434 "orr r8, r7, r8, lsl #8 \r\n"
435 "orr r8, r8, r9, lsl #24 \r\n"
436 "mov r9, r9, lsr #8 \r\n"
437 "orr r10, r9, r10, lsl #8 \r\n"
438 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
439 "strh r10, [%[buf]], #2 \r\n"
440 "mov r10, r10, lsr #16 \r\n"
441 "strb r10, [%[buf]], #1 \r\n"
443 : [data
]"r"(&DATA_REG
)
444 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
450 static inline void copy_read_sectors_slow(unsigned char** buf
)
455 /* Copy one chunk of 16 words */
458 "ldrh %[t], [%[data]] \r\n"
459 "strb %[t], [%[buf]], #1 \r\n"
460 "mov %[t], %[t], lsr #8 \r\n"
461 "strb %[t], [%[buf]], #1 \r\n"
462 "subs %[cnt], %[cnt], #1 \r\n"
464 : [cnt
]"+&r"(cnt
), [buf
]"+&r"(*buf
),
466 : [data
]"r"(&DATA_REG
)
470 /* Writes have to be kept slow for now */
471 static inline void copy_write_sectors(const unsigned char** buf
)
481 } while (--cnt
> 0); /* tail loop is faster */
484 static int sd_select_bank(unsigned char bank
)
486 unsigned char card_data
[512];
487 const unsigned char* write_buf
;
490 memset(card_data
, 0, 512);
492 ret
= sd_wait_for_state(TRAN
, EC_TRAN_SEL_BANK
);
496 BLOCK_SIZE_REG
= 512;
499 ret
= sd_command(35, 0, NULL
, 0x1c0d); /* CMD35 is vendor specific */
507 /* Write the card data */
508 write_buf
= card_data
;
509 for (i
= 0; i
< BLOCK_SIZE
/2; i
+= FIFO_LEN
)
511 /* Wait for the FIFO to empty */
512 if (sd_poll_status(FIFO_EMPTY
, 10000))
514 copy_write_sectors(&write_buf
); /* Copy one chunk of 16 words */
518 return -EC_FIFO_SEL_BANK_EMPTY
;
521 if (!sd_poll_status(DATA_DONE
, 10000))
522 return -EC_FIFO_SEL_BANK_DONE
;
524 currcard
->current_bank
= bank
;
529 static void sd_card_mux(int card_no
)
531 /* Set the current card mux */
537 GPIO_CLEAR_BITWISE(GPIOA_ENABLE
, 0x7a);
538 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x7a);
539 GPIO_SET_BITWISE(GPIOD_ENABLE
, 0x1f);
540 GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL
, 0x1f);
541 GPIO_SET_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
543 outl((inl(0x70000014) & ~(0x3ffff)) | 0x255aa, 0x70000014);
549 GPIO_CLEAR_BITWISE(GPIOD_ENABLE
, 0x1f);
550 GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
551 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x7a);
552 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL
, 0x7a);
553 GPIO_SET_BITWISE( GPIOA_OUTPUT_EN
, 0x7a);
555 outl(inl(0x70000014) & ~(0x3ffff), 0x70000014);
557 #else /* SANSA_C200 */
562 GPIO_CLEAR_BITWISE(GPIOD_ENABLE
, 0x1f);
563 GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
564 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x7a);
565 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL
, 0x7a);
566 GPIO_SET_BITWISE( GPIOA_OUTPUT_EN
, 0x7a);
568 outl(inl(0x70000014) & ~(0x3ffff), 0x70000014);
574 GPIO_CLEAR_BITWISE(GPIOA_ENABLE
, 0x7a);
575 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x7a);
576 GPIO_SET_BITWISE(GPIOD_ENABLE
, 0x1f);
577 GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL
, 0x1f);
578 GPIO_SET_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
580 outl((inl(0x70000014) & ~(0x3ffff)) | 0x255aa, 0x70000014);
585 static void sd_init_device(int card_no
)
587 /* SD Protocol registers */
589 unsigned int response
= 0;
593 unsigned long c_mult
;
594 unsigned char carddata
[512];
595 unsigned char *dataptr
;
598 /* Enable and initialise controller */
601 /* Initialise card data as blank */
602 memset(currcard
, 0, sizeof(*currcard
));
604 /* Switch card mux to card to initialize */
605 sd_card_mux(card_no
);
610 REG_12
&= ~(3 << 12);
612 REG_11
&= ~(3 << 12);
615 DEV_EN
|= DEV_ATA
; /* Enable controller */
616 DEV_RS
|= DEV_ATA
; /* Reset controller */
617 DEV_RS
&=~DEV_ATA
; /* Clear Reset */
623 ret
= sd_command(GO_IDLE_STATE
, 0, NULL
, 256);
625 goto card_init_error
;
627 check_time
[EC_POWER_UP
] = USEC_TIMER
;
631 - non-SDHC cards simply ignore SEND_IF_COND (CMD8) and we get error -219,
632 which we can just ignore and assume we're dealing with standard SD.
633 - SDHC cards echo back the argument into the response. This is how we
634 tell if the card is SDHC.
636 ret
= sd_command(SEND_IF_COND
,0x1aa, &response
,7);
637 if ( (ret
< 0) && (ret
!=-219) )
638 goto card_init_error
;
641 while ((currcard
->ocr
& (1 << 31)) == 0) /* until card is powered up */
643 ret
= sd_command(APP_CMD
, currcard
->rca
, NULL
, 1);
645 goto card_init_error
;
648 if(response
== 0x1aa)
651 ret
= sd_command(SD_APP_OP_COND
, (1<<30)|0x100000,
655 #endif /* HAVE_HOTSWAP */
658 ret
= sd_command(SD_APP_OP_COND
, 0x100000, &currcard
->ocr
, 3);
662 goto card_init_error
;
664 if (!sd_check_timeout(5000000, EC_POWER_UP
))
667 goto card_init_error
;
671 ret
= sd_command(ALL_SEND_CID
, 0, currcard
->cid
, 2);
673 goto card_init_error
;
675 ret
= sd_command(SEND_RELATIVE_ADDR
, 0, &currcard
->rca
, 1);
677 goto card_init_error
;
679 ret
= sd_command(SEND_CSD
, currcard
->rca
, currcard
->csd
, 2);
681 goto card_init_error
;
683 /* These calculations come from the Sandisk SD card product manual */
684 if( (currcard
->csd
[3]>>30) == 0)
686 /* CSD version 1.0 */
687 c_size
= ((currcard
->csd
[2] & 0x3ff) << 2) + (currcard
->csd
[1]>>30) + 1;
688 c_mult
= 4 << ((currcard
->csd
[1] >> 15) & 7);
689 currcard
->max_read_bl_len
= 1 << ((currcard
->csd
[2] >> 16) & 15);
690 currcard
->block_size
= BLOCK_SIZE
; /* Always use 512 byte blocks */
691 currcard
->numblocks
= c_size
* c_mult
* (currcard
->max_read_bl_len
/512);
692 currcard
->capacity
= currcard
->numblocks
* currcard
->block_size
;
695 else if( (currcard
->csd
[3]>>30) == 1)
697 /* CSD version 2.0 */
698 c_size
= ((currcard
->csd
[2] & 0x3f) << 16) + (currcard
->csd
[1]>>16) + 1;
699 currcard
->max_read_bl_len
= 1 << ((currcard
->csd
[2] >> 16) & 0xf);
700 currcard
->block_size
= BLOCK_SIZE
; /* Always use 512 byte blocks */
701 currcard
->numblocks
= c_size
<< 10;
702 currcard
->capacity
= currcard
->numblocks
* currcard
->block_size
;
704 #endif /* HAVE_HOTSWAP */
708 ret
= sd_command(SELECT_CARD
, currcard
->rca
, NULL
, 129);
710 goto card_init_error
;
712 ret
= sd_command(APP_CMD
, currcard
->rca
, NULL
, 1);
714 goto card_init_error
;
716 ret
= sd_command(SET_BUS_WIDTH
, currcard
->rca
| 2, NULL
, 1); /* 4 bit */
718 goto card_init_error
;
720 ret
= sd_command(SET_BLOCKLEN
, currcard
->block_size
, NULL
, 1);
722 goto card_init_error
;
724 BLOCK_SIZE_REG
= currcard
->block_size
;
726 /* If this card is >4GB & not SDHC, then we need to enable bank switching */
727 if( (currcard
->numblocks
>= BLOCKS_PER_BANK
) &&
728 ((currcard
->ocr
& (1<<30)) == 0) )
733 ret
= sd_command(SWITCH_FUNC
, 0x80ffffef, NULL
, 0x1c05);
735 goto card_init_error
;
737 /* Read 512 bytes from the card.
738 The first 512 bits contain the status information
739 TODO: Do something useful with this! */
741 for (i
= 0; i
< BLOCK_SIZE
/2; i
+= FIFO_LEN
)
743 /* Wait for the FIFO to be full */
744 if (sd_poll_status(FIFO_FULL
, 100000))
746 copy_read_sectors_slow(&dataptr
);
750 ret
= -EC_FIFO_ENA_BANK_EMPTY
;
751 goto card_init_error
;
755 currcard
->initialized
= 1;
758 /* Card failed to initialize so disable it */
760 currcard
->initialized
= ret
;
763 /* lock must already be aquired */
764 static void sd_select_device(int card_no
)
766 currcard
= &card_info
[card_no
];
770 /* Main card always gets a chance */
771 sd_status
[0].retry
= 0;
774 if (currcard
->initialized
> 0)
776 /* This card is already initialized - switch to it */
777 sd_card_mux(card_no
);
781 if (currcard
->initialized
== 0)
783 /* Card needs (re)init */
784 sd_init_device(card_no
);
790 void ata_led(bool onoff
)
795 int ata_read_sectors(IF_MV2(int drive
,) unsigned long start
, int incount
,
802 unsigned char *buf
, *buf_end
;
805 /* TODO: Add DMA support. */
812 if (drive
!= 0 && !card_detect_target())
814 /* no external sd-card inserted */
819 sd_select_device(drive
);
821 if (currcard
->initialized
< 0)
823 ret
= currcard
->initialized
;
827 last_disk_activity
= current_tick
;
829 /* Only switch banks with non-SDHC cards */
830 if((currcard
->ocr
& (1<<30))==0)
832 bank
= start
/ BLOCKS_PER_BANK
;
834 if (currcard
->current_bank
!= bank
)
836 ret
= sd_select_bank(bank
);
841 start
-= bank
* BLOCKS_PER_BANK
;
844 ret
= sd_wait_for_state(TRAN
, EC_TRAN_READ_ENTRY
);
848 BLOCK_COUNT_REG
= incount
;
851 if(currcard
->ocr
& (1<<30) )
854 ret
= sd_command(READ_MULTIPLE_BLOCK
, start
, NULL
, 0x1c25);
859 ret
= sd_command(READ_MULTIPLE_BLOCK
, start
* BLOCK_SIZE
, NULL
, 0x1c25);
864 /* TODO: Don't assume BLOCK_SIZE == SECTOR_SIZE */
866 buf_end
= (unsigned char *)inbuf
+ incount
* currcard
->block_size
;
867 for (buf
= inbuf
; buf
< buf_end
;)
869 /* Wait for the FIFO to be full */
870 if (sd_poll_status(FIFO_FULL
, 0x80000))
872 copy_read_sectors_fast(&buf
); /* Copy one chunk of 16 words */
873 /* TODO: Switch bank if necessary */
877 ret
= -EC_FIFO_READ_FULL
;
881 last_disk_activity
= current_tick
;
883 ret
= sd_command(STOP_TRANSMISSION
, 0, NULL
, 1);
887 ret
= sd_wait_for_state(TRAN
, EC_TRAN_READ_EXIT
);
894 mutex_unlock(&sd_mtx
);
899 if (sd_status
[drive
].retry
< sd_status
[drive
].retry_max
900 && ret
!= -EC_NOCARD
)
902 sd_status
[drive
].retry
++;
903 currcard
->initialized
= 0;
909 int ata_write_sectors(IF_MV2(int drive
,) unsigned long start
, int count
,
912 /* Write support is not finished yet */
913 /* TODO: The standard suggests using ACMD23 prior to writing multiple blocks
914 to improve performance */
919 const unsigned char *buf
, *buf_end
;
927 if (drive
!= 0 && !card_detect_target())
929 /* no external sd-card inserted */
931 goto ata_write_error
;
934 sd_select_device(drive
);
936 if (currcard
->initialized
< 0)
938 ret
= currcard
->initialized
;
939 goto ata_write_error
;
942 /* Only switch banks with non-SDHC cards */
943 if((currcard
->ocr
& (1<<30))==0)
945 bank
= start
/ BLOCKS_PER_BANK
;
947 if (currcard
->current_bank
!= bank
)
949 ret
= sd_select_bank(bank
);
951 goto ata_write_error
;
954 start
-= bank
* BLOCKS_PER_BANK
;
957 check_time
[EC_WRITE_TIMEOUT
] = USEC_TIMER
;
959 ret
= sd_wait_for_state(TRAN
, EC_TRAN_WRITE_ENTRY
);
961 goto ata_write_error
;
963 BLOCK_COUNT_REG
= count
;
966 if(currcard
->ocr
& (1<<30) )
969 ret
= sd_command(WRITE_MULTIPLE_BLOCK
, start
, NULL
, 0x1c2d);
974 ret
= sd_command(WRITE_MULTIPLE_BLOCK
, start
*BLOCK_SIZE
, NULL
, 0x1c2d);
977 goto ata_write_error
;
979 buf_end
= outbuf
+ count
* currcard
->block_size
- 2*FIFO_LEN
;
981 for (buf
= outbuf
; buf
<= buf_end
;)
985 /* Set SD_STATE_REG to PRG for the last buffer fill */
989 udelay(2); /* needed here (loop is too fast :-) */
991 /* Wait for the FIFO to empty */
992 if (sd_poll_status(FIFO_EMPTY
, 0x80000))
994 copy_write_sectors(&buf
); /* Copy one chunk of 16 words */
995 /* TODO: Switch bank if necessary */
999 ret
= -EC_FIFO_WR_EMPTY
;
1000 goto ata_write_error
;
1003 last_disk_activity
= current_tick
;
1005 if (!sd_poll_status(DATA_DONE
, 0x80000))
1007 ret
= -EC_FIFO_WR_DONE
;
1008 goto ata_write_error
;
1011 ret
= sd_command(STOP_TRANSMISSION
, 0, NULL
, 1);
1013 goto ata_write_error
;
1015 ret
= sd_wait_for_state(TRAN
, EC_TRAN_WRITE_EXIT
);
1017 goto ata_write_error
;
1022 mutex_unlock(&sd_mtx
);
1027 if (sd_status
[drive
].retry
< sd_status
[drive
].retry_max
1028 && ret
!= -EC_NOCARD
)
1030 sd_status
[drive
].retry
++;
1031 currcard
->initialized
= 0;
1032 goto ata_write_retry
;
1037 static void sd_thread(void) __attribute__((noreturn
));
1038 static void sd_thread(void)
1040 struct queue_event ev
;
1041 bool idle_notified
= false;
1045 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
1050 case SYS_HOTSWAP_INSERTED
:
1051 case SYS_HOTSWAP_EXTRACTED
:
1052 fat_lock(); /* lock-out FAT activity first -
1053 prevent deadlocking via disk_mount that
1054 would cause a reverse-order attempt with
1056 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
1057 into driver that bypass the fat cache */
1059 /* We now have exclusive control of fat cache and ata */
1061 disk_unmount(1); /* release "by force", ensure file
1062 descriptors aren't leaked and any busy
1063 ones are invalid if mounting */
1065 /* Force card init for new card, re-init for re-inserted one or
1066 * clear if the last attempt to init failed with an error. */
1067 card_info
[1].initialized
= 0;
1068 sd_status
[1].retry
= 0;
1070 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
1073 queue_broadcast(SYS_FS_CHANGED
, 0);
1075 /* Access is now safe */
1076 mutex_unlock(&sd_mtx
);
1081 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
1083 idle_notified
= false;
1087 /* never let a timer wrap confuse us */
1088 next_yield
= USEC_TIMER
;
1092 call_ata_idle_notifys(false);
1093 idle_notified
= true;
1097 case SYS_USB_CONNECTED
:
1098 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
1099 /* Wait until the USB cable is extracted again */
1100 usb_wait_for_disconnect(&sd_queue
);
1103 case SYS_USB_DISCONNECTED
:
1104 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
1111 void ata_spindown(int seconds
)
1116 bool ata_disk_is_active(void)
1121 void ata_sleep(void)
1129 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
1130 int ata_hard_reset(void)
1135 int ata_soft_reset(void)
1140 void ata_enable(bool on
)
1144 DEV_EN
|= DEV_ATA
; /* Enable controller */
1148 DEV_EN
&= ~DEV_ATA
; /* Disable controller */
1153 void card_enable_monitoring_target(bool on
)
1158 GPIO_SET_BITWISE(GPIOA_INT_EN
, 0x80);
1159 #elif defined(SANSA_C200)
1160 GPIO_SET_BITWISE(GPIOL_INT_EN
, 0x08);
1166 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1167 #elif defined(SANSA_C200)
1168 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1179 mutex_init(&sd_mtx
);
1181 mutex_lock(&sd_mtx
);
1189 /* init controller */
1190 outl(inl(0x70000088) & ~(0x4), 0x70000088);
1191 outl(inl(0x7000008c) & ~(0x4), 0x7000008c);
1192 GPO32_ENABLE
|= 0x4;
1194 GPIO_SET_BITWISE(GPIOG_ENABLE
, (0x3 << 5));
1195 GPIO_SET_BITWISE(GPIOG_OUTPUT_EN
, (0x3 << 5));
1196 GPIO_SET_BITWISE(GPIOG_OUTPUT_VAL
, (0x3 << 5));
1199 /* enable card detection port - mask interrupt first */
1201 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1203 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x80);
1204 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x80);
1205 #elif defined SANSA_C200
1206 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1208 GPIO_CLEAR_BITWISE(GPIOL_OUTPUT_EN
, 0x08);
1209 GPIO_SET_BITWISE(GPIOL_ENABLE
, 0x08);
1212 sd_select_device(0);
1214 if (currcard
->initialized
< 0)
1215 ret
= currcard
->initialized
;
1217 queue_init(&sd_queue
, true);
1218 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
1219 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
)
1222 /* enable interupt for the mSD card */
1226 CPU_INT_EN
= HI_MASK
;
1227 CPU_HI_INT_EN
= GPIO0_MASK
;
1229 GPIOA_INT_LEV
= (0x80 << 8) | (~GPIOA_INPUT_VAL
& 0x80);
1231 GPIOA_INT_CLR
= 0x80;
1232 #elif defined SANSA_C200
1233 CPU_INT_EN
= HI_MASK
;
1234 CPU_HI_INT_EN
= GPIO2_MASK
;
1236 GPIOL_INT_LEV
= (0x08 << 8) | (~GPIOL_INPUT_VAL
& 0x08);
1238 GPIOL_INT_CLR
= 0x08;
1243 mutex_unlock(&sd_mtx
);
1248 /* move the sd-card info to mmc struct */
1249 tCardInfo
*card_get_info_target(int card_no
)
1252 static tCardInfo card
;
1253 static const char mantissa
[] = { /* *10 */
1254 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
1255 static const int exponent
[] = { /* use varies */
1256 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 };
1258 card
.initialized
= card_info
[card_no
].initialized
;
1259 card
.ocr
= card_info
[card_no
].ocr
;
1260 for(i
=0; i
<4; i
++) card
.csd
[i
] = card_info
[card_no
].csd
[3-i
];
1261 for(i
=0; i
<4; i
++) card
.cid
[i
] = card_info
[card_no
].cid
[3-i
];
1262 card
.numblocks
= card_info
[card_no
].numblocks
;
1263 card
.blocksize
= card_info
[card_no
].block_size
;
1264 card
.size
= card_info
[card_no
].capacity
< 0xffffffff ?
1265 card_info
[card_no
].capacity
: 0xffffffff;
1266 card
.block_exp
= card_info
[card_no
].block_exp
;
1267 temp
= card_extract_bits(card
.csd
, 29, 3);
1268 card
.speed
= mantissa
[card_extract_bits(card
.csd
, 25, 4)]
1269 * exponent
[temp
> 2 ? 7 : temp
+ 4];
1270 card
.nsac
= 100 * card_extract_bits(card
.csd
, 16, 8);
1271 temp
= card_extract_bits(card
.csd
, 13, 3);
1272 card
.tsac
= mantissa
[card_extract_bits(card
.csd
, 9, 4)]
1273 * exponent
[temp
] / 10;
1274 card
.cid
[0] = htobe32(card
.cid
[0]); /* ascii chars here */
1275 card
.cid
[1] = htobe32(card
.cid
[1]); /* ascii chars here */
1276 temp
= *((char*)card
.cid
+13); /* adjust year<=>month, 1997 <=> 2000 */
1277 *((char*)card
.cid
+13) = (unsigned char)((temp
>> 4) | (temp
<< 4)) + 3;
1283 bool card_detect_target(void)
1286 return (GPIOA_INPUT_VAL
& 0x80) == 0; /* low active */
1287 #elif defined SANSA_C200
1288 return (GPIOL_INPUT_VAL
& 0x08) != 0; /* high active */
1292 static bool sd1_oneshot_callback(struct timeout
*tmo
)
1296 /* This is called only if the state was stable for 300ms - check state
1297 * and post appropriate event. */
1298 if (card_detect_target())
1299 queue_broadcast(SYS_HOTSWAP_INSERTED
, 0);
1301 queue_broadcast(SYS_HOTSWAP_EXTRACTED
, 0);
1306 /* called on insertion/removal interrupt */
1307 void microsd_int(void)
1309 static struct timeout sd1_oneshot
;
1312 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1313 GPIOA_INT_LEV
= (0x80 << 8) | (~GPIOA_INPUT_VAL
& 0x80);
1314 GPIOA_INT_CLR
= 0x80;
1315 GPIO_SET_BITWISE(GPIOA_INT_EN
, 0x80);
1317 #elif defined SANSA_C200
1318 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1319 GPIOL_INT_LEV
= (0x08 << 8) | (~GPIOL_INPUT_VAL
& 0x08);
1320 GPIOL_INT_CLR
= 0x08;
1321 GPIO_SET_BITWISE(GPIOL_INT_EN
, 0x08);
1323 timeout_register(&sd1_oneshot
, sd1_oneshot_callback
, (3*HZ
/10), 0);
1326 #endif /* HAVE_HOTSWAP */