Gigabeat S: Implement a genuine udelay function. Timer is gated to not run in WFI...
[kugel-rb.git] / firmware / target / arm / ata-sd-pp.c
blob3a095e1321ef8a45ac9a094db5db4d8fc1a7e670
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 */
22 #include "fat.h"
23 #include "hotswap.h"
24 #include "ata-sd-target.h"
25 #include "ata_idle_notify.h"
26 #include "system.h"
27 #include <string.h>
28 #include "thread.h"
29 #include "led.h"
30 #include "disk.h"
31 #include "cpu.h"
32 #include "panic.h"
33 #include "usb.h"
34 #include "sd.h"
35 #include "storage.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))
56 /* STATUS_REG bits */
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 */
70 #define EC_OK 0
71 #define EC_FAILED 1
72 #define EC_NOCARD 2
73 #define EC_WAIT_STATE_FAILED 3
74 #define EC_CHECK_TIMEOUT_FAILED 4
75 #define EC_POWER_UP 5
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
89 #define EC_COMMAND 19
90 #define NUM_EC 20
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
106 int retry;
107 int retry_max;
110 static struct sd_card_status sd_status[NUM_VOLUMES] =
112 { 0, 1 },
113 #ifdef HAVE_MULTIVOLUME
114 { 0, 10 }
115 #endif
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 */
125 #define SD_HOTSWAP 1
126 /* Actions taken by sd_thread when card status has changed */
127 enum sd_thread_actions
129 SDA_NONE = 0x0,
130 SDA_UNMOUNTED = 0x1,
131 SDA_MOUNTED = 0x2
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)
145 long t = USEC_TIMER;
147 while ((STATUS_REG & trigger) == 0)
149 long time = USEC_TIMER;
151 if (TIME_AFTER(time, next_yield))
153 long ty = USEC_TIMER;
154 yield();
155 timeout += USEC_TIMER - ty;
156 next_yield = ty + MIN_YIELD_PERIOD;
159 if (TIME_AFTER(time, t + timeout))
160 return false;
163 return true;
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];
172 CMD_REG0 = cmd;
173 CMD_REG1 = (unsigned int)((arg1 & 0xffff0000) >> 16);
174 CMD_REG2 = (unsigned int)((arg1 & 0xffff));
175 UNKNOWN = type;
177 if (!sd_poll_status(CMD_DONE, 100000))
178 return -EC_COMMAND;
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 */
196 else if (type == 2)
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);
210 else
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
220 * R3: OCR Register
221 * R6: [31:16] RCA
222 * [15: 0] Card Status Bits 23, 22, 19, 12:0
223 * [23] COM_CRC_ERROR
224 * [22] ILLEGAL_COMMAND
225 * [19] ERROR
226 * [12:9] CURRENT_STATE
227 * [8] READY_FOR_DATA
228 * [7:6]
229 * [5] SD_APP_CMD
230 * [4]
231 * [3] AKE_SEQ_ERROR
232 * [2] Reserved
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'
238 * [0] End Bit - '1'
240 response[0] = (data[0]<<24) + (data[1]<<8) + (data[2]>>8);
243 return 0;
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;
253 while (1)
255 int ret = sd_command(SD_SEND_STATUS, currcard->rca, &response, 1);
256 long us;
258 if (ret < 0)
259 return ret*100 - id;
261 if (((response >> 9) & 0xf) == state)
263 SD_STATE_REG = state;
264 return 0;
267 if (!sd_check_timeout(timeout, id))
268 return -EC_WAIT_STATE_FAILED*100 - id;
270 us = USEC_TIMER;
271 if (TIME_AFTER(us, next_yield))
273 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 )
285 case 0:
286 asm volatile (
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"
299 : [buf]"+&r"(*buf)
300 : [data]"r"(&DATA_REG)
301 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9"
303 break;
304 case 1:
305 asm volatile (
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"
337 : [buf]"+&r"(*buf)
338 : [data]"r"(&DATA_REG)
339 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
341 break;
342 case 2:
343 asm volatile (
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"
357 : [buf]"+&r"(*buf)
358 : [data]"r"(&DATA_REG)
359 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
361 break;
362 case 3:
363 asm volatile (
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"
395 : [buf]"+&r"(*buf)
396 : [data]"r"(&DATA_REG)
397 : "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
399 break;
403 static inline void copy_read_sectors_slow(unsigned char** buf)
405 int cnt = FIFO_LEN;
406 int t;
408 /* Copy one chunk of 16 words */
409 asm volatile (
410 "1: \r\n"
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"
416 "bgt 1b \r\n"
417 : [cnt]"+&r"(cnt), [buf]"+&r"(*buf),
418 [t]"=&r"(t)
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)
426 int cnt = FIFO_LEN;
427 unsigned t;
431 t = *(*buf)++;
432 t |= *(*buf)++ << 8;
433 DATA_REG = t;
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;
441 int i, ret;
443 memset(card_data, 0, 512);
445 ret = sd_wait_for_state(SD_TRAN, EC_TRAN_SEL_BANK);
446 if (ret < 0)
447 return ret;
449 BLOCK_SIZE_REG = 512;
450 BLOCK_COUNT_REG = 1;
452 ret = sd_command(35, 0, NULL, 0x1c0d); /* CMD35 is vendor specific */
453 if (ret < 0)
454 return ret;
456 SD_STATE_REG = SD_PRG;
458 card_data[0] = bank;
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 */
468 continue;
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;
479 return 0;
482 static void sd_card_mux(int card_no)
484 /* Set the current card mux */
485 #if defined(SANSA_E200) || defined(PHILIPS_SA9200)
486 if (card_no == 0)
488 GPO32_VAL |= 0x4;
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);
498 else
500 GPO32_VAL &= ~0x4;
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 */
511 if (card_no == 0)
513 GPO32_VAL |= 0x4;
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);
523 else
525 GPO32_VAL &= ~0x4;
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);
535 #endif
538 static void sd_init_device(int card_no)
540 /* SD Protocol registers */
541 #ifdef HAVE_HOTSWAP
542 unsigned int response = 0;
543 #endif
544 unsigned int i;
545 unsigned int c_size;
546 unsigned long c_mult;
547 unsigned char carddata[512];
548 unsigned char *dataptr;
549 int ret;
551 /* Enable and initialise controller */
552 REG_1 = 6;
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);
560 /* Init NAND */
561 REG_11 |= (1 << 15);
562 REG_12 |= (1 << 15);
563 REG_12 &= ~(3 << 12);
564 REG_12 |= (1 << 13);
565 REG_11 &= ~(3 << 12);
566 REG_11 |= (1 << 13);
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;
574 REG_5 = 0xf;
576 ret = sd_command(SD_GO_IDLE_STATE, 0, NULL, 256);
577 if (ret < 0)
578 goto card_init_error;
580 check_time[EC_POWER_UP] = USEC_TIMER;
582 #ifdef HAVE_HOTSWAP
583 /* Check for SDHC:
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;
592 #endif
594 while ((currcard->ocr & (1 << 31)) == 0) /* until card is powered up */
596 ret = sd_command(SD_APP_CMD, currcard->rca, NULL, 1);
597 if (ret < 0)
598 goto card_init_error;
600 #ifdef HAVE_HOTSWAP
601 if(response == 0x1aa)
603 /* SDHC */
604 ret = sd_command(SD_APP_OP_COND, (1<<30)|0x100000,
605 &currcard->ocr, 3);
607 else
608 #endif /* HAVE_HOTSWAP */
610 /* SD Standard */
611 ret = sd_command(SD_APP_OP_COND, 0x100000, &currcard->ocr, 3);
614 if (ret < 0)
615 goto card_init_error;
617 if (!sd_check_timeout(5000000, EC_POWER_UP))
619 ret = -EC_POWER_UP;
620 goto card_init_error;
624 ret = sd_command(SD_ALL_SEND_CID, 0, currcard->cid, 2);
625 if (ret < 0)
626 goto card_init_error;
628 ret = sd_command(SD_SEND_RELATIVE_ADDR, 0, &currcard->rca, 1);
629 if (ret < 0)
630 goto card_init_error;
632 ret = sd_command(SD_SEND_CSD, currcard->rca, currcard->csd, 2);
633 if (ret < 0)
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;
647 #ifdef HAVE_HOTSWAP
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 */
659 REG_1 = 0;
661 ret = sd_command(SD_SELECT_CARD, currcard->rca, NULL, 129);
662 if (ret < 0)
663 goto card_init_error;
665 ret = sd_command(SD_APP_CMD, currcard->rca, NULL, 1);
666 if (ret < 0)
667 goto card_init_error;
669 ret = sd_command(SD_SET_BUS_WIDTH, currcard->rca | 2, NULL, 1); /* 4 bit */
670 if (ret < 0)
671 goto card_init_error;
673 ret = sd_command(SD_SET_BLOCKLEN, currcard->block_size, NULL, 1);
674 if (ret < 0)
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;
684 BLOCK_COUNT_REG = 1;
686 ret = sd_command(SD_SWITCH_FUNC, 0x80ffffef, NULL, 0x1c05);
687 if (ret < 0)
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! */
693 dataptr = carddata;
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);
700 continue;
703 ret = -EC_FIFO_ENA_BANK_EMPTY;
704 goto card_init_error;
708 currcard->initialized = 1;
709 return;
711 /* Card failed to initialize so disable it */
712 card_init_error:
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];
721 if (card_no == 0)
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);
731 return;
734 if (currcard->initialized == 0)
736 /* Card needs (re)init */
737 sd_init_device(card_no);
741 /* API Functions */
743 static void sd_led(bool onoff)
745 led(onoff);
748 int sd_read_sectors(IF_MV2(int drive,) unsigned long start, int incount,
749 void* inbuf)
751 #ifndef HAVE_MULTIVOLUME
752 const int drive = 0;
753 #endif
754 int ret;
755 unsigned char *buf, *buf_end;
756 int bank;
758 /* TODO: Add DMA support. */
760 mutex_lock(&sd_mtx);
761 sd_enable(true);
762 sd_led(true);
764 sd_read_retry:
765 if (drive != 0 && !card_detect_target())
767 /* no external sd-card inserted */
768 ret = -EC_NOCARD;
769 goto sd_read_error;
772 sd_select_device(drive);
774 if (currcard->initialized < 0)
776 ret = currcard->initialized;
777 goto sd_read_error;
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);
790 if (ret < 0)
791 goto sd_read_error;
794 start -= bank * BLOCKS_PER_BANK;
797 ret = sd_wait_for_state(SD_TRAN, EC_TRAN_READ_ENTRY);
798 if (ret < 0)
799 goto sd_read_error;
801 BLOCK_COUNT_REG = incount;
803 #ifdef HAVE_HOTSWAP
804 if(currcard->ocr & (1<<30) )
806 /* SDHC */
807 ret = sd_command(SD_READ_MULTIPLE_BLOCK, start, NULL, 0x1c25);
809 else
810 #endif
812 ret = sd_command(SD_READ_MULTIPLE_BLOCK, start * BLOCK_SIZE, NULL, 0x1c25);
814 if (ret < 0)
815 goto sd_read_error;
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 */
827 continue;
830 ret = -EC_FIFO_READ_FULL;
831 goto sd_read_error;
834 last_disk_activity = current_tick;
836 ret = sd_command(SD_STOP_TRANSMISSION, 0, NULL, 1);
837 if (ret < 0)
838 goto sd_read_error;
840 ret = sd_wait_for_state(SD_TRAN, EC_TRAN_READ_EXIT);
841 if (ret < 0)
842 goto sd_read_error;
844 while (1)
846 sd_led(false);
847 sd_enable(false);
848 mutex_unlock(&sd_mtx);
850 return ret;
852 sd_read_error:
853 if (sd_status[drive].retry < sd_status[drive].retry_max
854 && ret != -EC_NOCARD)
856 sd_status[drive].retry++;
857 currcard->initialized = 0;
858 goto sd_read_retry;
863 int sd_write_sectors(IF_MV2(int drive,) unsigned long start, int count,
864 const void* outbuf)
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
870 const int drive = 0;
871 #endif
872 int ret;
873 const unsigned char *buf, *buf_end;
874 int bank;
876 mutex_lock(&sd_mtx);
877 sd_enable(true);
878 sd_led(true);
880 sd_write_retry:
881 if (drive != 0 && !card_detect_target())
883 /* no external sd-card inserted */
884 ret = -EC_NOCARD;
885 goto sd_write_error;
888 sd_select_device(drive);
890 if (currcard->initialized < 0)
892 ret = currcard->initialized;
893 goto sd_write_error;
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);
904 if (ret < 0)
905 goto sd_write_error;
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);
914 if (ret < 0)
915 goto sd_write_error;
917 BLOCK_COUNT_REG = count;
919 #ifdef HAVE_HOTSWAP
920 if(currcard->ocr & (1<<30) )
922 /* SDHC */
923 ret = sd_command(SD_WRITE_MULTIPLE_BLOCK, start, NULL, 0x1c2d);
925 else
926 #endif
928 ret = sd_command(SD_WRITE_MULTIPLE_BLOCK, start*BLOCK_SIZE, NULL, 0x1c2d);
930 if (ret < 0)
931 goto sd_write_error;
933 buf_end = outbuf + count * currcard->block_size - 2*FIFO_LEN;
935 for (buf = outbuf; buf <= buf_end;)
937 if (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 */
950 continue;
953 ret = -EC_FIFO_WR_EMPTY;
954 goto sd_write_error;
957 last_disk_activity = current_tick;
959 if (!sd_poll_status(DATA_DONE, 0x80000))
961 ret = -EC_FIFO_WR_DONE;
962 goto sd_write_error;
965 ret = sd_command(SD_STOP_TRANSMISSION, 0, NULL, 1);
966 if (ret < 0)
967 goto sd_write_error;
969 ret = sd_wait_for_state(SD_TRAN, EC_TRAN_WRITE_EXIT);
970 if (ret < 0)
971 goto sd_write_error;
973 while (1)
975 sd_led(false);
976 sd_enable(false);
977 mutex_unlock(&sd_mtx);
979 return ret;
981 sd_write_error:
982 if (sd_status[drive].retry < sd_status[drive].retry_max
983 && ret != -EC_NOCARD)
985 sd_status[drive].retry++;
986 currcard->initialized = 0;
987 goto sd_write_retry;
992 static void sd_thread(void) __attribute__((noreturn));
993 static void sd_thread(void)
995 struct queue_event ev;
996 bool idle_notified = false;
998 while (1)
1000 queue_wait_w_tmo(&sd_queue, &ev, HZ);
1002 switch ( ev.id )
1004 #ifdef HAVE_HOTSWAP
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
1010 another thread */
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)
1026 disk_mount(1);
1028 queue_broadcast(SYS_FS_CHANGED, 0);
1030 /* Access is now safe */
1031 mutex_unlock(&sd_mtx);
1032 fat_unlock();
1033 break;
1034 #endif
1035 case SYS_TIMEOUT:
1036 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
1038 idle_notified = false;
1040 else
1042 /* never let a timer wrap confuse us */
1043 next_yield = USEC_TIMER;
1045 if (!idle_notified)
1047 call_storage_idle_notifys(false);
1048 idle_notified = true;
1051 break;
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);
1057 break;
1058 case SYS_USB_DISCONNECTED:
1059 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
1060 break;
1065 void sd_enable(bool on)
1067 if(on)
1069 DEV_EN |= DEV_ATA; /* Enable controller */
1071 else
1073 DEV_EN &= ~DEV_ATA; /* Disable controller */
1077 #ifdef HAVE_HOTSWAP
1078 void card_enable_monitoring_target(bool on)
1080 if (on)
1082 #ifdef SANSA_E200
1083 GPIO_SET_BITWISE(GPIOA_INT_EN, 0x80);
1084 #elif defined(SANSA_C200)
1085 GPIO_SET_BITWISE(GPIOL_INT_EN, 0x08);
1086 #endif
1088 else
1090 #ifdef SANSA_E200
1091 GPIO_CLEAR_BITWISE(GPIOA_INT_EN, 0x80);
1092 #elif defined(SANSA_C200)
1093 GPIO_CLEAR_BITWISE(GPIOL_INT_EN, 0x08);
1094 #endif
1097 #endif
1099 int sd_init(void)
1101 int ret = 0;
1103 if (!initialized)
1104 mutex_init(&sd_mtx);
1106 mutex_lock(&sd_mtx);
1108 sd_led(false);
1110 if (!initialized)
1112 initialized = true;
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));
1123 #ifdef HAVE_HOTSWAP
1124 /* enable card detection port - mask interrupt first */
1125 #ifdef SANSA_E200
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);
1135 #endif
1136 #endif
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)
1145 IF_COP(, CPU));
1147 /* enable interupt for the mSD card */
1148 sleep(HZ/10);
1149 #ifdef HAVE_HOTSWAP
1150 #ifdef SANSA_E200
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;
1164 #endif
1165 #endif
1168 mutex_unlock(&sd_mtx);
1170 return ret;
1173 /* move the sd-card info to mmc struct */
1174 tCardInfo *card_get_info_target(int card_no)
1176 int i, temp;
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;
1201 return &card;
1204 bool card_detect_target(void)
1206 #ifdef HAVE_HOTSWAP
1207 #ifdef SANSA_E200
1208 return (GPIOA_INPUT_VAL & 0x80) == 0; /* low active */
1209 #elif defined SANSA_C200
1210 return (GPIOL_INPUT_VAL & 0x08) != 0; /* high active */
1211 #endif
1212 #else
1213 return false;
1214 #endif
1217 #ifdef HAVE_HOTSWAP
1218 static int sd1_oneshot_callback(struct timeout *tmo)
1220 (void)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);
1226 else
1227 queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
1229 return 0;
1232 /* called on insertion/removal interrupt */
1233 void microsd_int(void)
1235 static struct timeout sd1_oneshot;
1237 #ifdef SANSA_E200
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);
1248 #endif
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
1262 const int drive=0;
1263 #endif
1264 info->sector_size=card_info[drive].block_size;
1265 info->num_sectors=card_info[drive].numblocks;
1266 info->vendor="Rockbox";
1267 if(drive==0)
1269 info->product="Internal Storage";
1271 else
1273 info->product="SD Card Slot";
1275 info->revision="0.00";
1277 #endif
1279 #ifdef HAVE_HOTSWAP
1280 bool sd_removable(IF_MV_NONVOID(int drive))
1282 #ifndef HAVE_MULTIVOLUME
1283 const int drive=0;
1284 #endif
1285 return (drive==1);
1288 bool sd_present(IF_MV_NONVOID(int drive))
1290 #ifndef HAVE_MULTIVOLUME
1291 const int drive=0;
1292 #endif
1293 return (card_info[drive].initialized && card_info[drive].numblocks > 0);
1295 #endif
1297 void sd_sleep(void)
1301 void sd_spin(void)
1305 void sd_spindown(int seconds)
1307 (void)seconds;