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 ****************************************************************************/
23 #include "ata-sd-target.h"
24 #include "ata_idle_notify.h"
34 #define BLOCK_SIZE 512
35 #define SECTOR_SIZE 512
36 #define BLOCKS_PER_BANK 0x7a7800
38 #define STATUS_REG (*(volatile unsigned int *)(0x70008204))
39 #define REG_1 (*(volatile unsigned int *)(0x70008208))
40 #define UNKNOWN (*(volatile unsigned int *)(0x70008210))
41 #define BLOCK_SIZE_REG (*(volatile unsigned int *)(0x7000821c))
42 #define BLOCK_COUNT_REG (*(volatile unsigned int *)(0x70008220))
43 #define REG_5 (*(volatile unsigned int *)(0x70008224))
44 #define CMD_REG0 (*(volatile unsigned int *)(0x70008228))
45 #define CMD_REG1 (*(volatile unsigned int *)(0x7000822c))
46 #define CMD_REG2 (*(volatile unsigned int *)(0x70008230))
47 #define RESPONSE_REG (*(volatile unsigned int *)(0x70008234))
48 #define SD_STATE_REG (*(volatile unsigned int *)(0x70008238))
49 #define REG_11 (*(volatile unsigned int *)(0x70008240))
50 #define REG_12 (*(volatile unsigned int *)(0x70008244))
51 #define DATA_REG (*(volatile unsigned int *)(0x70008280))
54 #define DATA_DONE (1 << 12)
55 #define CMD_DONE (1 << 13)
56 #define ERROR_BITS (0x3f)
57 #define READY_FOR_DATA (1 << 8)
58 #define FIFO_FULL (1 << 7)
59 #define FIFO_EMPTY (1 << 6)
61 #define CMD_OK 0x0 /* Command was successful */
62 #define CMD_ERROR_2 0x2 /* SD did not respond to command (either it doesn't
63 understand the command or is not inserted) */
76 #define FIFO_LEN 16 /* FIFO is 16 words deep */
79 #define GO_IDLE_STATE 0
80 #define ALL_SEND_CID 2
81 #define SEND_RELATIVE_ADDR 3
85 #define DESELECT_CARD 7
86 #define SEND_IF_COND 8
89 #define STOP_TRANSMISSION 12
90 #define SEND_STATUS 13
91 #define GO_INACTIVE_STATE 15
92 #define SET_BLOCKLEN 16
93 #define READ_SINGLE_BLOCK 17
94 #define READ_MULTIPLE_BLOCK 18
95 #define SEND_NUM_WR_BLOCKS 22
96 #define WRITE_BLOCK 24
97 #define WRITE_MULTIPLE_BLOCK 25
98 #define ERASE_WR_BLK_START 32
99 #define ERASE_WR_BLK_END 33
106 #define EC_WAIT_STATE_FAILED 3
107 #define EC_CHECK_TIMEOUT_FAILED 4
108 #define EC_POWER_UP 5
109 #define EC_READ_TIMEOUT 6
110 #define EC_WRITE_TIMEOUT 7
111 #define EC_TRAN_SEL_BANK 8
112 #define EC_TRAN_READ_ENTRY 9
113 #define EC_TRAN_READ_EXIT 10
114 #define EC_TRAN_WRITE_ENTRY 11
115 #define EC_TRAN_WRITE_EXIT 12
116 #define EC_FIFO_SEL_BANK_EMPTY 13
117 #define EC_FIFO_SEL_BANK_DONE 14
118 #define EC_FIFO_ENA_BANK_EMPTY 15
119 #define EC_FIFO_READ_FULL 16
120 #define EC_FIFO_WR_EMPTY 17
121 #define EC_FIFO_WR_DONE 18
122 #define EC_COMMAND 19
125 /* Application Specific commands */
126 #define SET_BUS_WIDTH 6
127 #define SD_APP_OP_COND 41
129 /** global, exported variables **/
131 #define NUM_VOLUMES 2
133 #define NUM_VOLUMES 1
136 /* for compatibility */
137 int ata_spinup_time
= 0;
139 long last_disk_activity
= -1;
141 /** static, private data **/
142 static bool initialized
= false;
144 static long next_yield
= 0;
145 #define MIN_YIELD_PERIOD 1000
147 static tSDCardInfo card_info
[2];
148 static tSDCardInfo
*currcard
= NULL
; /* current active card */
150 struct sd_card_status
156 static struct sd_card_status sd_status
[NUM_VOLUMES
] =
164 /* Shoot for around 75% usage */
165 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x1c0)/sizeof(long)];
166 static const char sd_thread_name
[] = "ata/sd";
167 static struct mutex sd_mtx SHAREDBSS_ATTR
;
168 static struct event_queue sd_queue
;
170 /* Posted when card plugged status has changed */
172 /* Actions taken by sd_thread when card status has changed */
173 enum sd_thread_actions
180 /* Private Functions */
182 static unsigned int check_time
[NUM_EC
];
184 static inline bool sd_check_timeout(long timeout
, int id
)
186 return !TIME_AFTER(USEC_TIMER
, check_time
[id
] + timeout
);
189 static bool sd_poll_status(unsigned int trigger
, long timeout
)
193 while ((STATUS_REG
& trigger
) == 0)
195 long time
= USEC_TIMER
;
197 if (TIME_AFTER(time
, next_yield
))
199 long ty
= USEC_TIMER
;
201 timeout
+= USEC_TIMER
- ty
;
202 next_yield
= ty
+ MIN_YIELD_PERIOD
;
205 if (TIME_AFTER(time
, t
+ timeout
))
212 static int sd_command(unsigned int cmd
, unsigned long arg1
,
213 unsigned int *response
, unsigned int type
)
215 int i
, words
; /* Number of 16 bit words to read from RESPONSE_REG */
216 unsigned int data
[9];
219 CMD_REG1
= (unsigned int)((arg1
& 0xffff0000) >> 16);
220 CMD_REG2
= (unsigned int)((arg1
& 0xffff));
223 if (!sd_poll_status(CMD_DONE
, 100000))
226 if ((STATUS_REG
& ERROR_BITS
) != CMD_OK
)
227 /* Error sending command */
228 return -EC_COMMAND
- (STATUS_REG
& ERROR_BITS
)*100;
230 if (cmd
== GO_IDLE_STATE
)
231 return 0; /* no response here */
233 words
= (type
== 2) ? 9 : 3;
235 for (i
= 0; i
< words
; i
++) /* RESPONSE_REG is read MSB first */
236 data
[i
] = RESPONSE_REG
; /* Read most significant 16-bit word */
238 if (response
== NULL
)
240 /* response discarded */
244 /* Response type 2 has the following structure:
245 * [135:135] Start Bit - '0'
246 * [134:134] Transmission bit - '0'
247 * [133:128] Reserved - '111111'
248 * [127:001] CID or CSD register including internal CRC7
249 * [000:000] End Bit - '1'
251 response
[3] = (data
[0]<<24) + (data
[1]<<8) + (data
[2]>>8);
252 response
[2] = (data
[2]<<24) + (data
[3]<<8) + (data
[4]>>8);
253 response
[1] = (data
[4]<<24) + (data
[5]<<8) + (data
[6]>>8);
254 response
[0] = (data
[6]<<24) + (data
[7]<<8) + (data
[8]>>8);
258 /* Response types 1, 1b, 3, 6, 7 have the following structure:
259 * Types 4 and 5 are not supported.
261 * [47] Start bit - '0'
262 * [46] Transmission bit - '0'
263 * [45:40] R1, R1b, R6, R7: Command index
264 * R3: Reserved - '111111'
265 * [39:8] R1, R1b: Card Status
268 * [15: 0] Card Status Bits 23, 22, 19, 12:0
270 * [22] ILLEGAL_COMMAND
272 * [12:9] CURRENT_STATE
279 * [1:0] Reserved for test mode
280 * R7: [19:16] Voltage accepted
281 * [15:8] echo-back of check pattern
282 * [7:1] R1, R1b: CRC7
283 * R3: Reserved - '1111111'
286 response
[0] = (data
[0]<<24) + (data
[1]<<8) + (data
[2]>>8);
292 static int sd_wait_for_state(unsigned int state
, int id
)
294 unsigned int response
= 0;
295 unsigned int timeout
= 0x80000;
297 check_time
[id
] = USEC_TIMER
;
301 int ret
= sd_command(SEND_STATUS
, currcard
->rca
, &response
, 1);
307 if (((response
>> 9) & 0xf) == state
)
309 SD_STATE_REG
= state
;
313 if (!sd_check_timeout(timeout
, id
))
314 return -EC_WAIT_STATE_FAILED
*100 - id
;
317 if (TIME_AFTER(us
, next_yield
))
320 timeout
+= USEC_TIMER
- us
;
321 next_yield
= us
+ MIN_YIELD_PERIOD
;
326 static inline void copy_read_sectors_fast(unsigned char **buf
)
328 /* Copy one chunk of 16 words using best method for start alignment */
329 switch ( (intptr_t)*buf
& 3 )
333 "ldmia %[data], { r2-r9 } \r\n"
334 "orr r2, r2, r3, lsl #16 \r\n"
335 "orr r4, r4, r5, lsl #16 \r\n"
336 "orr r6, r6, r7, lsl #16 \r\n"
337 "orr r8, r8, r9, lsl #16 \r\n"
338 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
339 "ldmia %[data], { r2-r9 } \r\n"
340 "orr r2, r2, r3, lsl #16 \r\n"
341 "orr r4, r4, r5, lsl #16 \r\n"
342 "orr r6, r6, r7, lsl #16 \r\n"
343 "orr r8, r8, r9, lsl #16 \r\n"
344 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
346 : [data
]"r"(&DATA_REG
)
347 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9"
352 "ldmia %[data], { r2-r9 } \r\n"
353 "orr r3, r2, r3, lsl #16 \r\n"
354 "strb r3, [%[buf]], #1 \r\n"
355 "mov r3, r3, lsr #8 \r\n"
356 "strh r3, [%[buf]], #2 \r\n"
357 "mov r3, r3, lsr #16 \r\n"
358 "orr r3, r3, r4, lsl #8 \r\n"
359 "orr r3, r3, r5, lsl #24 \r\n"
360 "mov r5, r5, lsr #8 \r\n"
361 "orr r5, r5, r6, lsl #8 \r\n"
362 "orr r5, r5, r7, lsl #24 \r\n"
363 "mov r7, r7, lsr #8 \r\n"
364 "orr r7, r7, r8, lsl #8 \r\n"
365 "orr r7, r7, r9, lsl #24 \r\n"
366 "mov r2, r9, lsr #8 \r\n"
367 "stmia %[buf]!, { r3, r5, r7 } \r\n"
368 "ldmia %[data], { r3-r10 } \r\n"
369 "orr r2, r2, r3, lsl #8 \r\n"
370 "orr r2, r2, r4, lsl #24 \r\n"
371 "mov r4, r4, lsr #8 \r\n"
372 "orr r4, r4, r5, lsl #8 \r\n"
373 "orr r4, r4, r6, lsl #24 \r\n"
374 "mov r6, r6, lsr #8 \r\n"
375 "orr r6, r6, r7, lsl #8 \r\n"
376 "orr r6, r6, r8, lsl #24 \r\n"
377 "mov r8, r8, lsr #8 \r\n"
378 "orr r8, r8, r9, lsl #8 \r\n"
379 "orr r8, r8, r10, lsl #24 \r\n"
380 "mov r10, r10, lsr #8 \r\n"
381 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
382 "strb r10, [%[buf]], #1 \r\n"
384 : [data
]"r"(&DATA_REG
)
385 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
390 "ldmia %[data], { r2-r9 } \r\n"
391 "strh r2, [%[buf]], #2 \r\n"
392 "orr r3, r3, r4, lsl #16 \r\n"
393 "orr r5, r5, r6, lsl #16 \r\n"
394 "orr r7, r7, r8, lsl #16 \r\n"
395 "stmia %[buf]!, { r3, r5, r7 } \r\n"
396 "ldmia %[data], { r2-r8, r10 } \r\n"
397 "orr r2, r9, r2, lsl #16 \r\n"
398 "orr r3, r3, r4, lsl #16 \r\n"
399 "orr r5, r5, r6, lsl #16 \r\n"
400 "orr r7, r7, r8, lsl #16 \r\n"
401 "stmia %[buf]!, { r2, r3, r5, r7 } \r\n"
402 "strh r10, [%[buf]], #2 \r\n"
404 : [data
]"r"(&DATA_REG
)
405 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
410 "ldmia %[data], { r2-r9 } \r\n"
411 "orr r3, r2, r3, lsl #16 \r\n"
412 "strb r3, [%[buf]], #1 \r\n"
413 "mov r3, r3, lsr #8 \r\n"
414 "orr r3, r3, r4, lsl #24 \r\n"
415 "mov r4, r4, lsr #8 \r\n"
416 "orr r5, r4, r5, lsl #8 \r\n"
417 "orr r5, r5, r6, lsl #24 \r\n"
418 "mov r6, r6, lsr #8 \r\n"
419 "orr r7, r6, r7, lsl #8 \r\n"
420 "orr r7, r7, r8, lsl #24 \r\n"
421 "mov r8, r8, lsr #8 \r\n"
422 "orr r2, r8, r9, lsl #8 \r\n"
423 "stmia %[buf]!, { r3, r5, r7 } \r\n"
424 "ldmia %[data], { r3-r10 } \r\n"
425 "orr r2, r2, r3, lsl #24 \r\n"
426 "mov r3, r3, lsr #8 \r\n"
427 "orr r4, r3, r4, lsl #8 \r\n"
428 "orr r4, r4, r5, lsl #24 \r\n"
429 "mov r5, r5, lsr #8 \r\n"
430 "orr r6, r5, r6, lsl #8 \r\n"
431 "orr r6, r6, r7, lsl #24 \r\n"
432 "mov r7, r7, lsr #8 \r\n"
433 "orr r8, r7, r8, lsl #8 \r\n"
434 "orr r8, r8, r9, lsl #24 \r\n"
435 "mov r9, r9, lsr #8 \r\n"
436 "orr r10, r9, r10, lsl #8 \r\n"
437 "stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
438 "strh r10, [%[buf]], #2 \r\n"
439 "mov r10, r10, lsr #16 \r\n"
440 "strb r10, [%[buf]], #1 \r\n"
442 : [data
]"r"(&DATA_REG
)
443 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
449 static inline void copy_read_sectors_slow(unsigned char** buf
)
454 /* Copy one chunk of 16 words */
457 "ldrh %[t], [%[data]] \r\n"
458 "strb %[t], [%[buf]], #1 \r\n"
459 "mov %[t], %[t], lsr #8 \r\n"
460 "strb %[t], [%[buf]], #1 \r\n"
461 "subs %[cnt], %[cnt], #1 \r\n"
463 : [cnt
]"+&r"(cnt
), [buf
]"+&r"(*buf
),
465 : [data
]"r"(&DATA_REG
)
469 /* Writes have to be kept slow for now */
470 static inline void copy_write_sectors(const unsigned char** buf
)
480 } while (--cnt
> 0); /* tail loop is faster */
483 static int sd_select_bank(unsigned char bank
)
485 unsigned char card_data
[512];
486 const unsigned char* write_buf
;
489 memset(card_data
, 0, 512);
491 ret
= sd_wait_for_state(TRAN
, EC_TRAN_SEL_BANK
);
495 BLOCK_SIZE_REG
= 512;
498 ret
= sd_command(35, 0, NULL
, 0x1c0d); /* CMD35 is vendor specific */
506 /* Write the card data */
507 write_buf
= card_data
;
508 for (i
= 0; i
< BLOCK_SIZE
/2; i
+= FIFO_LEN
)
510 /* Wait for the FIFO to empty */
511 if (sd_poll_status(FIFO_EMPTY
, 10000))
513 copy_write_sectors(&write_buf
); /* Copy one chunk of 16 words */
517 return -EC_FIFO_SEL_BANK_EMPTY
;
520 if (!sd_poll_status(DATA_DONE
, 10000))
521 return -EC_FIFO_SEL_BANK_DONE
;
523 currcard
->current_bank
= bank
;
528 static void sd_card_mux(int card_no
)
530 /* Set the current card mux */
531 #if defined(SANSA_E200) || defined(PHILIPS_SA9200)
536 GPIO_CLEAR_BITWISE(GPIOA_ENABLE
, 0x7a);
537 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x7a);
538 GPIO_SET_BITWISE(GPIOD_ENABLE
, 0x1f);
539 GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL
, 0x1f);
540 GPIO_SET_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
542 outl((inl(0x70000014) & ~(0x3ffff)) | 0x255aa, 0x70000014);
548 GPIO_CLEAR_BITWISE(GPIOD_ENABLE
, 0x1f);
549 GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
550 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x7a);
551 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL
, 0x7a);
552 GPIO_SET_BITWISE( GPIOA_OUTPUT_EN
, 0x7a);
554 outl(inl(0x70000014) & ~(0x3ffff), 0x70000014);
556 #else /* SANSA_C200 */
561 GPIO_CLEAR_BITWISE(GPIOD_ENABLE
, 0x1f);
562 GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
563 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x7a);
564 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL
, 0x7a);
565 GPIO_SET_BITWISE( GPIOA_OUTPUT_EN
, 0x7a);
567 outl(inl(0x70000014) & ~(0x3ffff), 0x70000014);
573 GPIO_CLEAR_BITWISE(GPIOA_ENABLE
, 0x7a);
574 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x7a);
575 GPIO_SET_BITWISE(GPIOD_ENABLE
, 0x1f);
576 GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL
, 0x1f);
577 GPIO_SET_BITWISE(GPIOD_OUTPUT_EN
, 0x1f);
579 outl((inl(0x70000014) & ~(0x3ffff)) | 0x255aa, 0x70000014);
584 static void sd_init_device(int card_no
)
586 /* SD Protocol registers */
588 unsigned int response
= 0;
592 unsigned long c_mult
;
593 unsigned char carddata
[512];
594 unsigned char *dataptr
;
597 /* Enable and initialise controller */
600 /* Initialise card data as blank */
601 memset(currcard
, 0, sizeof(*currcard
));
603 /* Switch card mux to card to initialize */
604 sd_card_mux(card_no
);
609 REG_12
&= ~(3 << 12);
611 REG_11
&= ~(3 << 12);
614 DEV_EN
|= DEV_ATA
; /* Enable controller */
615 DEV_RS
|= DEV_ATA
; /* Reset controller */
616 DEV_RS
&=~DEV_ATA
; /* Clear Reset */
622 ret
= sd_command(GO_IDLE_STATE
, 0, NULL
, 256);
624 goto card_init_error
;
626 check_time
[EC_POWER_UP
] = USEC_TIMER
;
630 - non-SDHC cards simply ignore SEND_IF_COND (CMD8) and we get error -219,
631 which we can just ignore and assume we're dealing with standard SD.
632 - SDHC cards echo back the argument into the response. This is how we
633 tell if the card is SDHC.
635 ret
= sd_command(SEND_IF_COND
,0x1aa, &response
,7);
636 if ( (ret
< 0) && (ret
!=-219) )
637 goto card_init_error
;
640 while ((currcard
->ocr
& (1 << 31)) == 0) /* until card is powered up */
642 ret
= sd_command(APP_CMD
, currcard
->rca
, NULL
, 1);
644 goto card_init_error
;
647 if(response
== 0x1aa)
650 ret
= sd_command(SD_APP_OP_COND
, (1<<30)|0x100000,
654 #endif /* HAVE_HOTSWAP */
657 ret
= sd_command(SD_APP_OP_COND
, 0x100000, &currcard
->ocr
, 3);
661 goto card_init_error
;
663 if (!sd_check_timeout(5000000, EC_POWER_UP
))
666 goto card_init_error
;
670 ret
= sd_command(ALL_SEND_CID
, 0, currcard
->cid
, 2);
672 goto card_init_error
;
674 ret
= sd_command(SEND_RELATIVE_ADDR
, 0, &currcard
->rca
, 1);
676 goto card_init_error
;
678 ret
= sd_command(SEND_CSD
, currcard
->rca
, currcard
->csd
, 2);
680 goto card_init_error
;
682 /* These calculations come from the Sandisk SD card product manual */
683 if( (currcard
->csd
[3]>>30) == 0)
685 /* CSD version 1.0 */
686 c_size
= ((currcard
->csd
[2] & 0x3ff) << 2) + (currcard
->csd
[1]>>30) + 1;
687 c_mult
= 4 << ((currcard
->csd
[1] >> 15) & 7);
688 currcard
->max_read_bl_len
= 1 << ((currcard
->csd
[2] >> 16) & 15);
689 currcard
->block_size
= BLOCK_SIZE
; /* Always use 512 byte blocks */
690 currcard
->numblocks
= c_size
* c_mult
* (currcard
->max_read_bl_len
/512);
691 currcard
->capacity
= currcard
->numblocks
* currcard
->block_size
;
694 else if( (currcard
->csd
[3]>>30) == 1)
696 /* CSD version 2.0 */
697 c_size
= ((currcard
->csd
[2] & 0x3f) << 16) + (currcard
->csd
[1]>>16) + 1;
698 currcard
->max_read_bl_len
= 1 << ((currcard
->csd
[2] >> 16) & 0xf);
699 currcard
->block_size
= BLOCK_SIZE
; /* Always use 512 byte blocks */
700 currcard
->numblocks
= c_size
<< 10;
701 currcard
->capacity
= currcard
->numblocks
* currcard
->block_size
;
703 #endif /* HAVE_HOTSWAP */
707 ret
= sd_command(SELECT_CARD
, currcard
->rca
, NULL
, 129);
709 goto card_init_error
;
711 ret
= sd_command(APP_CMD
, currcard
->rca
, NULL
, 1);
713 goto card_init_error
;
715 ret
= sd_command(SET_BUS_WIDTH
, currcard
->rca
| 2, NULL
, 1); /* 4 bit */
717 goto card_init_error
;
719 ret
= sd_command(SET_BLOCKLEN
, currcard
->block_size
, NULL
, 1);
721 goto card_init_error
;
723 BLOCK_SIZE_REG
= currcard
->block_size
;
725 /* If this card is >4GB & not SDHC, then we need to enable bank switching */
726 if( (currcard
->numblocks
>= BLOCKS_PER_BANK
) &&
727 ((currcard
->ocr
& (1<<30)) == 0) )
732 ret
= sd_command(SWITCH_FUNC
, 0x80ffffef, NULL
, 0x1c05);
734 goto card_init_error
;
736 /* Read 512 bytes from the card.
737 The first 512 bits contain the status information
738 TODO: Do something useful with this! */
740 for (i
= 0; i
< BLOCK_SIZE
/2; i
+= FIFO_LEN
)
742 /* Wait for the FIFO to be full */
743 if (sd_poll_status(FIFO_FULL
, 100000))
745 copy_read_sectors_slow(&dataptr
);
749 ret
= -EC_FIFO_ENA_BANK_EMPTY
;
750 goto card_init_error
;
754 currcard
->initialized
= 1;
757 /* Card failed to initialize so disable it */
759 currcard
->initialized
= ret
;
762 /* lock must already be aquired */
763 static void sd_select_device(int card_no
)
765 currcard
= &card_info
[card_no
];
769 /* Main card always gets a chance */
770 sd_status
[0].retry
= 0;
773 if (currcard
->initialized
> 0)
775 /* This card is already initialized - switch to it */
776 sd_card_mux(card_no
);
780 if (currcard
->initialized
== 0)
782 /* Card needs (re)init */
783 sd_init_device(card_no
);
789 static void ata_led(bool onoff
)
794 int ata_read_sectors(IF_MV2(int drive
,) unsigned long start
, int incount
,
801 unsigned char *buf
, *buf_end
;
804 /* TODO: Add DMA support. */
811 if (drive
!= 0 && !card_detect_target())
813 /* no external sd-card inserted */
818 sd_select_device(drive
);
820 if (currcard
->initialized
< 0)
822 ret
= currcard
->initialized
;
826 last_disk_activity
= current_tick
;
828 /* Only switch banks with non-SDHC cards */
829 if((currcard
->ocr
& (1<<30))==0)
831 bank
= start
/ BLOCKS_PER_BANK
;
833 if (currcard
->current_bank
!= bank
)
835 ret
= sd_select_bank(bank
);
840 start
-= bank
* BLOCKS_PER_BANK
;
843 ret
= sd_wait_for_state(TRAN
, EC_TRAN_READ_ENTRY
);
847 BLOCK_COUNT_REG
= incount
;
850 if(currcard
->ocr
& (1<<30) )
853 ret
= sd_command(READ_MULTIPLE_BLOCK
, start
, NULL
, 0x1c25);
858 ret
= sd_command(READ_MULTIPLE_BLOCK
, start
* BLOCK_SIZE
, NULL
, 0x1c25);
863 /* TODO: Don't assume BLOCK_SIZE == SECTOR_SIZE */
865 buf_end
= (unsigned char *)inbuf
+ incount
* currcard
->block_size
;
866 for (buf
= inbuf
; buf
< buf_end
;)
868 /* Wait for the FIFO to be full */
869 if (sd_poll_status(FIFO_FULL
, 0x80000))
871 copy_read_sectors_fast(&buf
); /* Copy one chunk of 16 words */
872 /* TODO: Switch bank if necessary */
876 ret
= -EC_FIFO_READ_FULL
;
880 last_disk_activity
= current_tick
;
882 ret
= sd_command(STOP_TRANSMISSION
, 0, NULL
, 1);
886 ret
= sd_wait_for_state(TRAN
, EC_TRAN_READ_EXIT
);
893 mutex_unlock(&sd_mtx
);
898 if (sd_status
[drive
].retry
< sd_status
[drive
].retry_max
899 && ret
!= -EC_NOCARD
)
901 sd_status
[drive
].retry
++;
902 currcard
->initialized
= 0;
908 int ata_write_sectors(IF_MV2(int drive
,) unsigned long start
, int count
,
911 /* Write support is not finished yet */
912 /* TODO: The standard suggests using ACMD23 prior to writing multiple blocks
913 to improve performance */
918 const unsigned char *buf
, *buf_end
;
926 if (drive
!= 0 && !card_detect_target())
928 /* no external sd-card inserted */
930 goto ata_write_error
;
933 sd_select_device(drive
);
935 if (currcard
->initialized
< 0)
937 ret
= currcard
->initialized
;
938 goto ata_write_error
;
941 /* Only switch banks with non-SDHC cards */
942 if((currcard
->ocr
& (1<<30))==0)
944 bank
= start
/ BLOCKS_PER_BANK
;
946 if (currcard
->current_bank
!= bank
)
948 ret
= sd_select_bank(bank
);
950 goto ata_write_error
;
953 start
-= bank
* BLOCKS_PER_BANK
;
956 check_time
[EC_WRITE_TIMEOUT
] = USEC_TIMER
;
958 ret
= sd_wait_for_state(TRAN
, EC_TRAN_WRITE_ENTRY
);
960 goto ata_write_error
;
962 BLOCK_COUNT_REG
= count
;
965 if(currcard
->ocr
& (1<<30) )
968 ret
= sd_command(WRITE_MULTIPLE_BLOCK
, start
, NULL
, 0x1c2d);
973 ret
= sd_command(WRITE_MULTIPLE_BLOCK
, start
*BLOCK_SIZE
, NULL
, 0x1c2d);
976 goto ata_write_error
;
978 buf_end
= outbuf
+ count
* currcard
->block_size
- 2*FIFO_LEN
;
980 for (buf
= outbuf
; buf
<= buf_end
;)
984 /* Set SD_STATE_REG to PRG for the last buffer fill */
988 udelay(2); /* needed here (loop is too fast :-) */
990 /* Wait for the FIFO to empty */
991 if (sd_poll_status(FIFO_EMPTY
, 0x80000))
993 copy_write_sectors(&buf
); /* Copy one chunk of 16 words */
994 /* TODO: Switch bank if necessary */
998 ret
= -EC_FIFO_WR_EMPTY
;
999 goto ata_write_error
;
1002 last_disk_activity
= current_tick
;
1004 if (!sd_poll_status(DATA_DONE
, 0x80000))
1006 ret
= -EC_FIFO_WR_DONE
;
1007 goto ata_write_error
;
1010 ret
= sd_command(STOP_TRANSMISSION
, 0, NULL
, 1);
1012 goto ata_write_error
;
1014 ret
= sd_wait_for_state(TRAN
, EC_TRAN_WRITE_EXIT
);
1016 goto ata_write_error
;
1021 mutex_unlock(&sd_mtx
);
1026 if (sd_status
[drive
].retry
< sd_status
[drive
].retry_max
1027 && ret
!= -EC_NOCARD
)
1029 sd_status
[drive
].retry
++;
1030 currcard
->initialized
= 0;
1031 goto ata_write_retry
;
1036 static void sd_thread(void) __attribute__((noreturn
));
1037 static void sd_thread(void)
1039 struct queue_event ev
;
1040 bool idle_notified
= false;
1044 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
1049 case SYS_HOTSWAP_INSERTED
:
1050 case SYS_HOTSWAP_EXTRACTED
:
1051 fat_lock(); /* lock-out FAT activity first -
1052 prevent deadlocking via disk_mount that
1053 would cause a reverse-order attempt with
1055 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
1056 into driver that bypass the fat cache */
1058 /* We now have exclusive control of fat cache and ata */
1060 disk_unmount(1); /* release "by force", ensure file
1061 descriptors aren't leaked and any busy
1062 ones are invalid if mounting */
1064 /* Force card init for new card, re-init for re-inserted one or
1065 * clear if the last attempt to init failed with an error. */
1066 card_info
[1].initialized
= 0;
1067 sd_status
[1].retry
= 0;
1069 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
1072 queue_broadcast(SYS_FS_CHANGED
, 0);
1074 /* Access is now safe */
1075 mutex_unlock(&sd_mtx
);
1080 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
1082 idle_notified
= false;
1086 /* never let a timer wrap confuse us */
1087 next_yield
= USEC_TIMER
;
1091 call_ata_idle_notifys(false);
1092 idle_notified
= true;
1096 case SYS_USB_CONNECTED
:
1097 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
1098 /* Wait until the USB cable is extracted again */
1099 usb_wait_for_disconnect(&sd_queue
);
1102 case SYS_USB_DISCONNECTED
:
1103 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
1110 void ata_spindown(int seconds
)
1115 bool ata_disk_is_active(void)
1120 void ata_sleep(void)
1128 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
1129 int ata_hard_reset(void)
1134 int ata_soft_reset(void)
1139 void ata_enable(bool on
)
1143 DEV_EN
|= DEV_ATA
; /* Enable controller */
1147 DEV_EN
&= ~DEV_ATA
; /* Disable controller */
1152 void card_enable_monitoring_target(bool on
)
1157 GPIO_SET_BITWISE(GPIOA_INT_EN
, 0x80);
1158 #elif defined(SANSA_C200)
1159 GPIO_SET_BITWISE(GPIOL_INT_EN
, 0x08);
1165 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1166 #elif defined(SANSA_C200)
1167 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1178 mutex_init(&sd_mtx
);
1180 mutex_lock(&sd_mtx
);
1188 /* init controller */
1189 outl(inl(0x70000088) & ~(0x4), 0x70000088);
1190 outl(inl(0x7000008c) & ~(0x4), 0x7000008c);
1191 GPO32_ENABLE
|= 0x4;
1193 GPIO_SET_BITWISE(GPIOG_ENABLE
, (0x3 << 5));
1194 GPIO_SET_BITWISE(GPIOG_OUTPUT_EN
, (0x3 << 5));
1195 GPIO_SET_BITWISE(GPIOG_OUTPUT_VAL
, (0x3 << 5));
1198 /* enable card detection port - mask interrupt first */
1200 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1202 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN
, 0x80);
1203 GPIO_SET_BITWISE(GPIOA_ENABLE
, 0x80);
1204 #elif defined SANSA_C200
1205 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1207 GPIO_CLEAR_BITWISE(GPIOL_OUTPUT_EN
, 0x08);
1208 GPIO_SET_BITWISE(GPIOL_ENABLE
, 0x08);
1211 sd_select_device(0);
1213 if (currcard
->initialized
< 0)
1214 ret
= currcard
->initialized
;
1216 queue_init(&sd_queue
, true);
1217 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
1218 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
)
1221 /* enable interupt for the mSD card */
1225 CPU_INT_EN
= HI_MASK
;
1226 CPU_HI_INT_EN
= GPIO0_MASK
;
1228 GPIOA_INT_LEV
= (0x80 << 8) | (~GPIOA_INPUT_VAL
& 0x80);
1230 GPIOA_INT_CLR
= 0x80;
1231 #elif defined SANSA_C200
1232 CPU_INT_EN
= HI_MASK
;
1233 CPU_HI_INT_EN
= GPIO2_MASK
;
1235 GPIOL_INT_LEV
= (0x08 << 8) | (~GPIOL_INPUT_VAL
& 0x08);
1237 GPIOL_INT_CLR
= 0x08;
1242 mutex_unlock(&sd_mtx
);
1247 /* move the sd-card info to mmc struct */
1248 tCardInfo
*card_get_info_target(int card_no
)
1251 static tCardInfo card
;
1252 static const char mantissa
[] = { /* *10 */
1253 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
1254 static const int exponent
[] = { /* use varies */
1255 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 };
1257 card
.initialized
= card_info
[card_no
].initialized
;
1258 card
.ocr
= card_info
[card_no
].ocr
;
1259 for(i
=0; i
<4; i
++) card
.csd
[i
] = card_info
[card_no
].csd
[3-i
];
1260 for(i
=0; i
<4; i
++) card
.cid
[i
] = card_info
[card_no
].cid
[3-i
];
1261 card
.numblocks
= card_info
[card_no
].numblocks
;
1262 card
.blocksize
= card_info
[card_no
].block_size
;
1263 card
.size
= card_info
[card_no
].capacity
< 0xffffffff ?
1264 card_info
[card_no
].capacity
: 0xffffffff;
1265 card
.block_exp
= card_info
[card_no
].block_exp
;
1266 temp
= card_extract_bits(card
.csd
, 29, 3);
1267 card
.speed
= mantissa
[card_extract_bits(card
.csd
, 25, 4)]
1268 * exponent
[temp
> 2 ? 7 : temp
+ 4];
1269 card
.nsac
= 100 * card_extract_bits(card
.csd
, 16, 8);
1270 temp
= card_extract_bits(card
.csd
, 13, 3);
1271 card
.tsac
= mantissa
[card_extract_bits(card
.csd
, 9, 4)]
1272 * exponent
[temp
] / 10;
1273 card
.cid
[0] = htobe32(card
.cid
[0]); /* ascii chars here */
1274 card
.cid
[1] = htobe32(card
.cid
[1]); /* ascii chars here */
1275 temp
= *((char*)card
.cid
+13); /* adjust year<=>month, 1997 <=> 2000 */
1276 *((char*)card
.cid
+13) = (unsigned char)((temp
>> 4) | (temp
<< 4)) + 3;
1281 bool card_detect_target(void)
1285 return (GPIOA_INPUT_VAL
& 0x80) == 0; /* low active */
1286 #elif defined SANSA_C200
1287 return (GPIOL_INPUT_VAL
& 0x08) != 0; /* high active */
1295 static bool sd1_oneshot_callback(struct timeout
*tmo
)
1299 /* This is called only if the state was stable for 300ms - check state
1300 * and post appropriate event. */
1301 if (card_detect_target())
1302 queue_broadcast(SYS_HOTSWAP_INSERTED
, 0);
1304 queue_broadcast(SYS_HOTSWAP_EXTRACTED
, 0);
1309 /* called on insertion/removal interrupt */
1310 void microsd_int(void)
1312 static struct timeout sd1_oneshot
;
1315 GPIO_CLEAR_BITWISE(GPIOA_INT_EN
, 0x80);
1316 GPIOA_INT_LEV
= (0x80 << 8) | (~GPIOA_INPUT_VAL
& 0x80);
1317 GPIOA_INT_CLR
= 0x80;
1318 GPIO_SET_BITWISE(GPIOA_INT_EN
, 0x80);
1320 #elif defined SANSA_C200
1321 GPIO_CLEAR_BITWISE(GPIOL_INT_EN
, 0x08);
1322 GPIOL_INT_LEV
= (0x08 << 8) | (~GPIOL_INPUT_VAL
& 0x08);
1323 GPIOL_INT_CLR
= 0x08;
1324 GPIO_SET_BITWISE(GPIOL_INT_EN
, 0x08);
1326 timeout_register(&sd1_oneshot
, sd1_oneshot_callback
, (3*HZ
/10), 0);
1329 #endif /* HAVE_HOTSWAP */