1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Daniel Ankers
11 * Copyright © 2008-2009 Rafaël Carré
12 * Copyright (C) 2011 Marcin Bukat
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
24 #include "config.h" /* for HAVE_MULTIVOLUME */
27 #include "gcc_extensions.h"
38 #include "ata_idle_notify.h"
52 static tCardInfo card_info
;
54 /* for compatibility */
55 static long last_disk_activity
= -1;
57 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x200)/sizeof(long)];
58 static const char sd_thread_name
[] = "ata/sd";
59 static struct mutex sd_mtx SHAREDBSS_ATTR
;
60 static struct event_queue sd_queue
;
62 bool sd_enabled
= false;
65 static struct semaphore transfer_completion_signal
;
66 static struct semaphore command_completion_signal
;
67 static volatile bool retry
;
68 static volatile int cmd_error
;
70 /* interrupt handler for SD */
73 const int status
= SD_INT
;
75 SD_INT
= 0; /* disable sd interrupts, clear pending interrupts */
77 /* cmd and response status pending */
78 if(status
& CMD_RES_STAT
)
81 cmd_error
= SD_CMDRES
;
82 semaphore_release(&command_completion_signal
);
85 /* data transfer status pending */
86 if(status
& DATA_XFER_STAT
)
89 if (cmd_error
& DATA_XFER_ERR
)
92 semaphore_release(&transfer_completion_signal
);
95 SD_INT
= CMD_RES_INT_EN
| DATA_XFER_INT_EN
;
98 /* Exchange buffers - the one where SD module puts into/reads from
99 * data and the one controlled by MCU. This allows some overlap
100 * in transfer operations and should increase throuput.
102 static void mmu_switch_buff(void)
104 static unsigned int i
= 0;
108 MMU_CTRL
= MMU_MMU0_BUFII
| MMU_CPU_BUFI
| MMU_BUFII_RESET
|
109 MMU_BUFII_BYTE
| MMU_BUFI_RESET
| MMU_BUFI_WORD
;
113 MMU_CTRL
= MMU_MMU0_BUFI
| MMU_CPU_BUFII
| MMU_BUFII_RESET
|
114 MMU_BUFII_WORD
| MMU_BUFI_RESET
| MMU_BUFI_BYTE
;
118 /* Reset internal pointers of the MMU submodule */
119 static void mmu_buff_reset(void)
121 MMU_CTRL
|= MMU_BUFII_RESET
| MMU_BUFI_RESET
;
124 /* My generic device uses PC7 pin, active low */
125 static inline bool card_detect_target(void)
127 return !(GPIO_PCDR
& 0x80);
130 /* Send command to the SD card. Command finish is signaled in ISR */
131 static bool send_cmd(const int cmd
, const int arg
, const int res
,
132 unsigned long *response
)
137 SD_CMDREST
= CMD_XFER_START
| RES_XFER_START
| res
| cmd
;
139 SD_CMDREST
= CMD_XFER_START
| RES_XFER_END
| RES_R1
| cmd
;
141 semaphore_wait(&command_completion_signal
, TIMEOUT_BLOCK
);
143 /* Handle command responses & errors */
146 if(cmd_error
& STAT_CMD_RES_ERR
)
151 response
[0] = SD_RES3
;
152 response
[1] = SD_RES2
;
153 response
[2] = SD_RES1
;
154 response
[3] = SD_RES0
;
157 response
[0] = SD_RES3
;
163 /* for some misterious reason the card does not report itself as being in TRAN
164 * but transfers are successful. Rockchip OF does not check the card state
165 * after SELECT. I checked two different cards.
167 static void print_card_status(void)
169 unsigned long response
;
170 send_cmd(SD_SEND_STATUS
, card_info
.rca
, RES_R1
,
173 printf("card status: 0x%0x, state: 0x%0x", response
, (response
>>9)&0xf);
176 static int sd_wait_for_tran_state(void)
178 unsigned long response
;
179 unsigned int timeout
= current_tick
+ 5*HZ
;
184 while (!send_cmd(SD_SEND_STATUS
, card_info
.rca
, RES_R1
,
185 &response
) && cmd_retry
> 0)
195 if (((response
>> 9) & 0xf) == SD_TRAN
)
200 if(TIME_AFTER(current_tick
, timeout
))
202 return -10 * ((response
>> 9) & 0xf);
205 last_disk_activity
= current_tick
;
210 static bool sd_wait_card_busy(void)
212 unsigned int timeout
= current_tick
+ 5*HZ
;
214 while (!(SD_CARD
& SD_CARD_BSY
))
216 if(TIME_AFTER(current_tick
, timeout
))
223 static int sd_init_card(void)
225 unsigned long response
;
231 /* assume 50 MHz APB freq / 125 = 400 kHz */
232 SD_CTRL
= (SD_CTRL
& ~(0x7FF)) | 0x7D;
234 /* 100 - 400kHz clock required for Identification Mode */
235 /* Start of Card Identification Mode ************************************/
238 if(!send_cmd(SD_GO_IDLE_STATE
, 0, RES_NO
, NULL
))
243 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
244 Non v2 cards will not respond to this command*/
245 if(send_cmd(SD_SEND_IF_COND
, 0x1AA, RES_R6
, &response
))
246 if((response
& 0xFFF) == 0x1AA)
249 /* timeout for initialization is 1sec, from SD Specification 2.00 */
250 init_timeout
= current_tick
+ HZ
;
253 /* this timeout is the only valid error for this loop*/
254 if(TIME_AFTER(current_tick
, init_timeout
))
257 if(!send_cmd(SD_APP_CMD
, card_info
.rca
, RES_R1
, &response
))
260 sleep(1); /* bus conflict otherwise */
262 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
263 if(!send_cmd(SD_APP_OP_COND
, (0x00FF8000 | (sd_v2
? 1<<30 : 0)),
264 RES_R3
, &card_info
.ocr
))
266 } while(!(card_info
.ocr
& (1<<31)) );
269 if(!send_cmd(SD_ALL_SEND_CID
, 0, RES_R2
, card_info
.cid
))
273 if(!send_cmd(SD_SEND_RELATIVE_ADDR
, 0, RES_R6
, &card_info
.rca
))
276 /* End of Card Identification Mode ************************************/
278 /* Card back to full speed 25MHz*/
279 SD_CTRL
= (SD_CTRL
& ~0x7FF) | 1; /* FIXME check this divider - OF uses 0 here*/
282 if(!send_cmd(SD_SEND_CSD
, card_info
.rca
, RES_R2
, card_info
.csd
))
285 sd_parse_csd(&card_info
);
287 if(!send_cmd(SD_SELECT_CARD
, card_info
.rca
, RES_R1b
, &response
))
290 if (!sd_wait_card_busy())
293 card_info
.initialized
= 1;
298 static void sd_thread(void) NORETURN_ATTR
;
299 static void sd_thread(void)
301 struct queue_event ev
;
302 bool idle_notified
= false;
306 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
311 case SYS_HOTSWAP_INSERTED
:
312 case SYS_HOTSWAP_EXTRACTED
:
314 int microsd_init
= 1;
315 fat_lock(); /* lock-out FAT activity first -
316 prevent deadlocking via disk_mount that
317 would cause a reverse-order attempt with
319 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
320 into driver that bypass the fat cache */
322 /* We now have exclusive control of fat cache and ata */
324 disk_unmount(sd_first_drive
); /* release "by force", ensure file
325 descriptors aren't leaked and any busy
326 ones are invalid if mounting */
327 /* Force card init for new card, re-init for re-inserted one or
328 * clear if the last attempt to init failed with an error. */
329 card_info
.initialized
= 0;
331 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
334 microsd_init
= sd_init_card(sd_first_drive
);
335 if (microsd_init
< 0) /* initialisation failed */
336 panicf("microSD init failed : %d", microsd_init
);
338 microsd_init
= disk_mount(sd_first_drive
); /* 0 if fail */
342 * Mount succeeded, or this was an EXTRACTED event,
343 * in both cases notify the system about the changed filesystems
346 queue_broadcast(SYS_FS_CHANGED
, 0);
350 /* Access is now safe */
351 mutex_unlock(&sd_mtx
);
357 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
359 idle_notified
= false;
361 else if (!idle_notified
)
363 call_storage_idle_notifys(false);
364 idle_notified
= true;
368 case SYS_USB_CONNECTED
:
369 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
370 /* Wait until the USB cable is extracted again */
371 usb_wait_for_disconnect(&sd_queue
);
378 static void init_controller(void)
380 /* reset SD module */
381 SCU_RSTCFG
|= (1<<9);
383 SCU_RSTCFG
&= ~(1<<9);
385 /* set pins functions as SD signals */
386 SCU_IOMUXA_CON
|= IOMUX_SD
;
388 /* enable and unmask SD interrupts in interrupt controller */
390 INTC_IECR
|= (1<<10);
392 SD_CTRL
= SD_PWR_CPU
| SD_DETECT_MECH
| SD_CLOCK_EN
| 0x7D;
393 SD_INT
= CMD_RES_INT_EN
| DATA_XFER_INT_EN
;
394 SD_CARD
= SD_CARD_SELECT
| SD_CARD_PWR_EN
;
396 /* setup mmu buffers */
399 MMU_CTRL
= MMU_MMU0_BUFII
| MMU_CPU_BUFI
| MMU_BUFII_RESET
|
400 MMU_BUFII_BYTE
| MMU_BUFI_RESET
| MMU_BUFI_WORD
;
408 semaphore_init(&transfer_completion_signal
, 1, 0);
409 semaphore_init(&command_completion_signal
, 1, 0);
413 ret
= sd_init_card();
420 queue_init(&sd_queue
, true);
421 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
422 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
) IF_COP(, CPU
));
427 int sd_read_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
430 #ifdef HAVE_MULTIDRIVE
433 unsigned long response
;
434 unsigned int retry_cnt
= 0;
441 if (count
<= 0 || start
+ count
> card_info
.numblocks
)
444 if(!(card_info
.ocr
& (1<<30)))
445 start
<<= 9; /* not SDHC */
447 /* setup A2A DMA CH0 */
448 A2A_ISRC0
= (unsigned long)(&MMU_DATA
);
453 while (retry_cnt
++ < 20)
456 dst
= (unsigned char *)buf
;
459 retry
= false; /* reset retry flag */
460 mmu_buff_reset(); /* reset recive buff state */
462 /* issue read command to the card */
463 if (!send_cmd(SD_READ_MULTIPLE_BLOCK
, start
, RES_R1
, &response
))
473 /* last block to tranfer */
474 SD_DATAT
= DATA_XFER_START
| DATA_XFER_READ
|
475 DATA_BUS_1LINE
| DATA_XFER_DMA_DIS
|
480 /* more than one block to transfer */
481 SD_DATAT
= DATA_XFER_START
| DATA_XFER_READ
|
482 DATA_BUS_1LINE
| DATA_XFER_DMA_DIS
|
486 /* wait for transfer completion */
487 semaphore_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
491 /* data transfer error */
496 /* exchange buffers */
499 last_disk_activity
= current_tick
;
501 /* transfer data from receive buffer to the dest
502 * for (i=0; i<(512/4); i++)
505 * below is DMA version in software mode.
506 * SD module provides DMAreq signals and all this
507 * can be done in hardware in theory but I can't
508 * figure this out. OF doesn't use DMA at all.
510 A2A_IDST0
= (unsigned long)dst
;
511 A2A_CON0
= (3<<9) | (1<<6) | (1<<3) | (2<<1) | (1<<0);
513 /* wait for DMA engine to finish transfer */
514 while (A2A_DMA_STS
& 1);
518 } /* while (cnt > 0) */
520 if (!send_cmd(SD_STOP_TRANSMISSION
, 0, RES_R1b
, &response
))
523 /* transfer successfull - leave retry loop */
529 mutex_unlock(&sd_mtx
);
535 int sd_write_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
538 #ifdef HAVE_MULTIDRIVE
541 #if defined(BOOTLOADER) /* we don't need write support in bootloader */
547 unsigned long response
;
548 unsigned int retry_cnt
= 0;
551 bool card_selected
= false;
556 if (count
<= 0 || start
+ count
> card_info
.numblocks
)
559 if(!(card_info
.ocr
& (1<<30)))
560 start
<<= 9; /* not SDHC */
562 /* setup A2A DMA CH0 */
563 A2A_IDST0
= (unsigned long)(&MMU_DATA
);
568 while (retry_cnt
++ < 20)
571 src
= (unsigned char *)buf
;
574 retry
= false; /* reset retry flag */
575 mmu_buff_reset(); /* reset recive buff state */
577 if (!send_cmd(SD_WRITE_MULTIPLE_BLOCK
, start
, RES_R1
, &response
))
585 /* transfer data from receive buffer to the dest
586 * for (i=0; i<(512/4); i++)
589 * Below is DMA version in software mode.
592 A2A_ISRC0
= (unsigned long)src
;
593 A2A_CON0
= (3<<9) | (1<<5) | (1<<3) | (2<<1) | (1<<0);
595 while (A2A_DMA_STS
& 1);
599 /* exchange buffers */
604 /* last block to tranfer */
605 SD_DATAT
= DATA_XFER_START
| DATA_XFER_WRITE
|
606 DATA_BUS_1LINE
| DATA_XFER_DMA_DIS
|
612 /* more than one block to transfer */
613 SD_DATAT
= DATA_XFER_START
| DATA_XFER_WRITE
|
614 DATA_BUS_1LINE
| DATA_XFER_DMA_DIS
|
619 /* wait for transfer completion */
620 semaphore_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
624 /* data transfer error */
630 } /* while (cnt > 0) */
632 if (!send_cmd(SD_STOP_TRANSMISSION
, 0, RES_R1b
, &response
))
635 if (!sd_wait_card_busy())
638 /* transfer successfull - leave retry loop */
644 mutex_unlock(&sd_mtx
);
648 #endif /* defined(BOOTLOADER) */
651 void sd_enable(bool on
)
653 /* enable or disable clock signal for SD module */
656 SCU_CLKCFG
&= ~(1<<22);
660 SCU_CLKCFG
|= (1<<22);
665 long sd_last_disk_activity(void)
667 return last_disk_activity
;
670 tCardInfo
*card_get_info_target(int card_no
)
675 #endif /* BOOTLOADER */
678 /* Not complete and disabled in config */
679 bool sd_removable(IF_MD_NONVOID(int drive
))
685 bool sd_present(IF_MD_NONVOID(int drive
))
688 return card_detect_target();
691 static int sd_oneshot_callback(struct timeout
*tmo
)
695 /* This is called only if the state was stable for 300ms - check state
696 * and post appropriate event. */
697 if (card_detect_target())
699 queue_broadcast(SYS_HOTSWAP_INSERTED
, 0);
702 queue_broadcast(SYS_HOTSWAP_EXTRACTED
, 0);
707 /* interrupt handler for SD detect */
709 #endif /* HAVE_HOTSWAP */
711 #ifdef CONFIG_STORAGE_MULTI
712 int sd_num_drives(int first_drive
)
716 /* we have only one SD drive */
719 #endif /* CONFIG_STORAGE_MULTI */