1
0
Fork 0
forked from len0rd/rockbox

SD driver cleanup by Thomas Martitz. Removes various magic numbers in favor of defines, increases a timeout in hopes of improving support for various SD cards, adds descriptive panic messages, and adds volatile were needed.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@19774 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Michael Giacomelli 2009-01-16 01:14:58 +00:00
parent 4544531585
commit 090535fcbd

View file

@ -110,7 +110,7 @@ static bool sd_enabled = false;
#endif #endif
static struct wakeup transfer_completion_signal; static struct wakeup transfer_completion_signal;
bool retry; static volatile bool retry;
static inline void mci_delay(void) { int i = 0xffff; while(i--) ; } static inline void mci_delay(void) { int i = 0xffff; while(i--) ; }
@ -270,6 +270,9 @@ static int sd_init_card(const int drive)
sdhc = true; sdhc = true;
do { do {
/* some MicroSD cards seems to need more delays, so play safe */
mci_delay();
mci_delay();
mci_delay(); mci_delay();
/* app_cmd */ /* app_cmd */
@ -286,7 +289,7 @@ static int sd_init_card(const int drive)
} while(!(card_info[drive].ocr & (1<<31)) && max_tries--); } while(!(card_info[drive].ocr & (1<<31)) && max_tries--);
if(!max_tries) if(max_tries < 0)
return -4; return -4;
/* send CID */ /* send CID */
@ -371,19 +374,20 @@ static void sd_thread(void)
/* We now have exclusive control of fat cache and ata */ /* We now have exclusive control of fat cache and ata */
disk_unmount(1); /* release "by force", ensure file disk_unmount(SD_SLOT_AS3525); /* release "by force", ensure file
descriptors aren't leaked and any busy descriptors aren't leaked and any busy
ones are invalid if mounting */ ones are invalid if mounting */
/* Force card init for new card, re-init for re-inserted one or /* Force card init for new card, re-init for re-inserted one or
* clear if the last attempt to init failed with an error. */ * clear if the last attempt to init failed with an error. */
card_info[1].initialized = 0; card_info[SD_SLOT_AS3525].initialized = 0;
if (ev.id == SYS_HOTSWAP_INSERTED) if (ev.id == SYS_HOTSWAP_INSERTED)
{ {
sd_enable(true); sd_enable(true);
init_pl180_controller(SD_SLOT_AS3525); init_pl180_controller(SD_SLOT_AS3525);
disk_mount(1); sd_init_card(SD_SLOT_AS3525);
disk_mount(SD_SLOT_AS3525);
} }
queue_broadcast(SYS_FS_CHANGED, 0); queue_broadcast(SYS_FS_CHANGED, 0);
@ -391,6 +395,7 @@ static void sd_thread(void)
/* Access is now safe */ /* Access is now safe */
mutex_unlock(&sd_mtx); mutex_unlock(&sd_mtx);
fat_unlock(); fat_unlock();
sd_enable(false);
break; break;
#endif #endif
case SYS_TIMEOUT: case SYS_TIMEOUT:
@ -445,6 +450,7 @@ static void init_pl180_controller(const int drive)
GPIOA_IS &= ~(1<<2); GPIOA_IS &= ~(1<<2);
/* detect both raising and falling edges */ /* detect both raising and falling edges */
GPIOA_IBE |= (1<<2); GPIOA_IBE |= (1<<2);
#endif #endif
#else #else
@ -470,43 +476,40 @@ static void init_pl180_controller(const int drive)
int sd_init(void) int sd_init(void)
{ {
int ret; int ret;
CGU_IDE = (1<<7) /* AHB interface enable */ |
CGU_IDE = (1<<7) /* AHB interface enable */ |
(1<<6) /* interface enable */ | (1<<6) /* interface enable */ |
((CLK_DIV(AS3525_PLLA_FREQ, AS3525_IDE_FREQ) - 1) << 2) | ((CLK_DIV(AS3525_PLLA_FREQ, AS3525_IDE_FREQ) - 1) << 2) |
1 /* clock source = PLLA */; 1 /* clock source = PLLA */;
CGU_PERI |= CGU_NAF_CLOCK_ENABLE; CGU_PERI |= CGU_NAF_CLOCK_ENABLE;
#ifdef HAVE_MULTIVOLUME #ifdef HAVE_MULTIVOLUME
CGU_PERI |= CGU_MCI_CLOCK_ENABLE; CGU_PERI |= CGU_MCI_CLOCK_ENABLE;
CCU_IO &= ~(1<<3); /* bits 3:2 = 01, xpd is SD interface */
CCU_IO |= (1<<2);
#endif #endif
wakeup_init(&transfer_completion_signal); wakeup_init(&transfer_completion_signal);
init_pl180_controller(INTERNAL_AS3525); init_pl180_controller(INTERNAL_AS3525);
ret = sd_init_card(INTERNAL_AS3525); ret = sd_init_card(INTERNAL_AS3525);
if(ret < 0) if(ret < 0)
return ret; return ret;
#ifdef HAVE_MULTIVOLUME #ifdef HAVE_MULTIVOLUME
CCU_IO &= ~8; /* bits 3:2 = 01, xpd is SD interface */
CCU_IO |= 4;
init_pl180_controller(SD_SLOT_AS3525); init_pl180_controller(SD_SLOT_AS3525);
sd_init_card(SD_SLOT_AS3525);
#endif #endif
/* init mutex */ /* init mutex */
#ifndef BOOTLOADER
sd_enable(false);
#endif
mutex_init(&sd_mtx); mutex_init(&sd_mtx);
queue_init(&sd_queue, true); queue_init(&sd_queue, true);
create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0, create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0,
sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU)); sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));
#ifndef BOOTLOADER
sd_enabled = true;
sd_enable(false);
#endif
return 0; return 0;
} }
@ -592,21 +595,12 @@ static int sd_transfer_sectors(IF_MV2(int drive,) unsigned long start,
sd_enable(true); sd_enable(true);
#endif #endif
#ifdef HAVE_MULTIVOLUME
if (drive != 0 && !card_detect_target())
{
/* no external sd-card inserted */
ret = -88;
goto sd_transfer_error;
}
#endif
if (card_info[drive].initialized <= 0) if (card_info[drive].initialized <= 0)
{ {
sd_init_card(drive); ret = sd_init_card(drive);
if (!(card_info[drive].initialized)) if (!(card_info[drive].initialized))
{ {
panicf("card not initialised"); panicf("card not initialised (%d)", ret);
goto sd_transfer_error; goto sd_transfer_error;
} }
} }
@ -623,6 +617,8 @@ static int sd_transfer_sectors(IF_MV2(int drive,) unsigned long start,
while(count) while(count)
{ {
/* Interrupt handler might set this to true during transfer */
retry = false;
/* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH /* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH
* register, so we have to transfer maximum 127 sectors at a time. */ * register, so we have to transfer maximum 127 sectors at a time. */
unsigned int transfer = (count >= 128) ? 127 : count; /* sectors */ unsigned int transfer = (count >= 128) ? 127 : count; /* sectors */
@ -636,10 +632,7 @@ static int sd_transfer_sectors(IF_MV2(int drive,) unsigned long start,
MCI_ARG, NULL); MCI_ARG, NULL);
if (ret < 0) if (ret < 0)
{ panicf("transfer multiple blocks failed (%d)", ret);
panicf("transfer multiple blocks failed");
goto sd_transfer_error;
}
if(write) if(write)
dma_enable_channel(0, buf, MCI_FIFO(drive), dma_enable_channel(0, buf, MCI_FIFO(drive),
@ -657,7 +650,7 @@ static int sd_transfer_sectors(IF_MV2(int drive,) unsigned long start,
(1<<3) /* DMA */ | (1<<3) /* DMA */ |
(9<<4) /* 2^9 = 512 */ ; (9<<4) /* 2^9 = 512 */ ;
retry = false;
wakeup_wait(&transfer_completion_signal, TIMEOUT_BLOCK); wakeup_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
if(!retry) if(!retry)
{ {
@ -678,26 +671,23 @@ static int sd_transfer_sectors(IF_MV2(int drive,) unsigned long start,
ret = sd_wait_for_state(drive, SD_TRAN); ret = sd_wait_for_state(drive, SD_TRAN);
if (ret < 0) if (ret < 0)
{ {
panicf(" wait for state TRAN failed"); panicf(" wait for state TRAN failed (%d)", ret);
goto sd_transfer_error; goto sd_transfer_error;
} }
} }
while (1) dma_release();
{
dma_release();
#ifndef BOOTLOADER #ifndef BOOTLOADER
sd_enable(false); sd_enable(false);
#endif #endif
mutex_unlock(&sd_mtx); mutex_unlock(&sd_mtx);
return 0;
return ret;
sd_transfer_error: sd_transfer_error:
panicf("transfer error : %d",ret); panicf("transfer error : %d",ret);
card_info[drive].initialized = 0; card_info[drive].initialized = 0;
} return ret;
} }
int sd_read_sectors(IF_MV2(int drive,) unsigned long start, int count, int sd_read_sectors(IF_MV2(int drive,) unsigned long start, int count,