rockbox/firmware/target/arm/tcc780x/sd-tcc780x.c
Michael Sevakis 7d1a47cf13 Rewrite filesystem code (WIP)
This patch redoes the filesystem code from the FAT driver up to the
clipboard code in onplay.c.

Not every aspect of this is finished therefore it is still "WIP". I
don't wish to do too much at once (haha!). What is left to do is get
dircache back in the sim and find an implementation for the dircache
indicies in the tagcache and playlist code or do something else that
has the same benefit. Leaving these out for now does not make anything
unusable. All the basics are done.

Phone app code should probably get vetted (and app path handling
just plain rewritten as environment expansions); the SDL app and
Android run well.

Main things addressed:
1) Thread safety: There is none right now in the trunk code. Most of
what currently works is luck when multiple threads are involved or
multiple descriptors to the same file are open.

2) POSIX compliance: Many of the functions behave nothing like their
counterparts on a host system. This leads to inconsistent code or very
different behavior from native to hosted. One huge offender was
rename(). Going point by point would fill a book.

3) Actual running RAM usage: Many targets will use less RAM and less
stack space (some more RAM because I upped the number of cache buffers
for large memory). There's very little memory lying fallow in rarely-used
areas (see 'Key core changes' below). Also, all targets may open the same
number of directory streams whereas before those with less than 8MB RAM
were limited to 8, not 12 implying those targets will save slightly
less.

4) Performance: The test_disk plugin shows markedly improved performance,
particularly in the area of (uncached) directory scanning, due partly to
more optimal directory reading and to a better sector cache algorithm.
Uncached times tend to be better while there is a bit of a slowdown in
dircache due to it being a bit heavier of an implementation. It's not
noticeable by a human as far as I can say.

Key core changes:
1) Files and directories share core code and data structures.

2) The filesystem code knows which descriptors refer to same file.
This ensures that changes from one stream are appropriately reflected
in every open descriptor for that file (fileobj_mgr.c).

3) File and directory cache buffers are borrowed from the main sector
cache. This means that when they are not in use by a file, they are not
wasted, but used for the cache. Most of the time, only a few of them
are needed. It also means that adding more file and directory handles
is less expensive. All one must do in ensure a large enough cache to
borrow from.

4) Relative path components are supported and the namespace is unified.
It does not support full relative paths to an implied current directory;
what is does support is use of "." and "..". Adding the former would
not be very difficult. The namespace is unified in the sense that
volumes may be specified several times along with relative parts, e.g.:
"/<0>/foo/../../<1>/bar" :<=> "/<1>/bar".

5) Stack usage is down due to sharing of data, static allocation and
less duplication of strings on the stack. This requires more
serialization than I would like but since the number of threads is
limited to a low number, the tradoff in favor of the stack seems
reasonable.

6) Separates and heirarchicalizes (sic) the SIM and APP filesystem
code. SIM path and volume handling is just like the target. Some
aspects of the APP file code get more straightforward (e.g. no path
hashing is needed).

Dircache:
Deserves its own section. Dircache is new but pays homage to the old.
The old one was not compatible and so it, since it got redone, does
all the stuff it always should have done such as:

1) It may be update and used at any time during the build process.
No longer has one to wait for it to finish building to do basic file
management (create, remove, rename, etc.).

2) It does not need to be either fully scanned or completely disabled;
it can be incomplete (i.e. overfilled, missing paths), still be
of benefit and be correct.

3) Handles mounting and dismounting of individual volumes which means
a full rebuild is not needed just because you pop a new SD card in the
slot. Now, because it reuses its freed entry data, may rebuild only
that volume.

4) Much more fundamental to the file code. When it is built, it is
the keeper of the master file list whether enabled or not ("disabled"
is just a state of the cache). Its must always to ready to be started
and bind all streams opened prior to being enabled.

5) Maintains any short filenames in OEM format which means that it does
not need to be rebuilt when changing the default codepage.

Miscellaneous Compatibility:
1) Update any other code that would otherwise not work such as the
hotswap mounting code in various card drivers.

2) File management: Clipboard needed updating because of the behavioral
changes. Still needs a little more work on some finer points.

3) Remove now-obsolete functionality such as the mutex's "no preempt"
flag (which was only for the prior FAT driver).

4) struct dirinfo uses time_t rather than raw FAT directory entry
time fields. I plan to follow up on genericizing everything there
(i.e. no FAT attributes).

5) unicode.c needed some redoing so that the file code does not try
try to load codepages during a scan, which is actually a problem with
the current code. The default codepage, if any is required, is now
kept in RAM separarately (bufalloced) from codepages specified to
iso_decode() (which must not be bufalloced because the conversion
may be done by playback threads).

Brings with it some additional reusable core code:
1) Revised file functions: Reusable code that does things such as
safe path concatenation and parsing without buffer limitations or
data duplication. Variants that copy or alter the input path may be
based off these.

To do:
1) Put dircache functionality back in the sim. Treating it internally
as a different kind of file system seems the best approach at this
time.

2) Restore use of dircache indexes in the playlist and database or
something effectively the same. Since the cache doesn't have to be
complete in order to be used, not getting a hit on the cache doesn't
unambiguously say if the path exists or not.

Change-Id: Ia30f3082a136253e3a0eae0784e3091d138915c8
Reviewed-on: http://gerrit.rockbox.org/566
Reviewed-by: Michael Sevakis <jethead71@rockbox.org>
Tested: Michael Sevakis <jethead71@rockbox.org>
2014-08-30 03:48:23 +02:00

820 lines
19 KiB
C

/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2006 Daniel Ankers
* Copyright (C) 2009 Rob Purchase
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "sd.h"
#include "system.h"
#include <string.h>
#include "gcc_extensions.h"
#include "sdmmc.h"
#include "storage.h"
#include "led.h"
#include "thread.h"
#include "disk.h"
#include "ata_idle_notify.h"
#include "usb.h"
#if defined(HAVE_INTERNAL_SD) && defined(HAVE_HOTSWAP)
#define CARD_NUM_INTERNAL 0
#define CARD_NUM_SLOT 1
#elif !defined(HAVE_INTERNAL_SD) && defined(HAVE_HOTSWAP)
#define CARD_NUM_SLOT 0
#endif
#define EC_OK 0
#define EC_FAILED 1
#define EC_NOCARD 2
#define EC_WAIT_STATE_FAILED 3
#define EC_POWER_UP 4
#define EC_FIFO_WR_EMPTY 5
#define EC_FIFO_WR_DONE 6
#define EC_TRAN_READ_ENTRY 7
#define EC_TRAN_READ_EXIT 8
#define EC_TRAN_WRITE_ENTRY 9
#define EC_TRAN_WRITE_EXIT 10
#define EC_COMMAND 11
#define EC_WRITE_PROTECT 12
/* for compatibility */
static long last_disk_activity = -1;
/** static, private data **/
static bool initialized = false;
static long next_yield = 0;
#define MIN_YIELD_PERIOD 1000
static tCardInfo card_info[NUM_DRIVES];
static tCardInfo *currcard = NULL; /* current active card */
struct sd_card_status
{
int retry;
int retry_max;
};
static struct sd_card_status sd_status[NUM_DRIVES] =
{
#ifdef HAVE_INTERNAL_SD
{ 0, 1 },
#endif
#ifdef HAVE_HOTSWAP
{ 0, 10 }
#endif
};
/* Shoot for around 75% usage */
static long sd_stack [(DEFAULT_STACK_SIZE*2 + 0x1c0)/sizeof(long)];
static const char sd_thread_name[] = "sd";
static struct mutex sd_mtx SHAREDBSS_ATTR;
static struct event_queue sd_queue;
static int sd_first_drive = 0;
static bool sd_poll_status(unsigned int trigger, long timeout)
{
long t = USEC_TIMER;
while ((SDISTATUS & trigger) != trigger)
{
long time = USEC_TIMER;
if (TIME_AFTER(time, next_yield))
{
long ty = USEC_TIMER;
yield();
timeout += USEC_TIMER - ty;
next_yield = ty + MIN_YIELD_PERIOD;
}
if (TIME_AFTER(time, t + timeout))
return false;
}
return true;
}
static int sd_command(unsigned int cmd, unsigned int arg,
unsigned long* response, unsigned int resp_type)
{
int sdi_cmd = cmd;
sdi_cmd |= (127<<12) | (1<<11); /* max wait time | enable */
if (resp_type)
{
/* response type & response required flag */
sdi_cmd |= (resp_type<<7) | (1<<6);
}
if (cmd == SD_READ_SINGLE_BLOCK ||
cmd == SD_READ_MULTIPLE_BLOCK ||
cmd == SD_WRITE_BLOCK ||
cmd == SD_WRITE_MULTIPLE_BLOCK)
{
sdi_cmd |= (1<<10); /* request data transfer */
}
if (!sd_poll_status(SDISTATUS_CMD_PATH_RDY, 100000))
return -EC_COMMAND;
SDIARGU = arg;
SDICMD = sdi_cmd;
udelay(10);
if (response == NULL)
return 0;
if (!sd_poll_status(SDISTATUS_RESP_RCVD, 100000))
return -EC_COMMAND;
if (resp_type == SDICMD_RES_TYPE2)
{
response[0] = SDIRSPARGU0;
response[1] = SDIRSPARGU1;
response[2] = SDIRSPARGU2;
response[3] = SDIRSPARGU3;
}
else
{
response[0] = SDIRSPARGU0;
}
return 0;
}
static int sd_wait_for_state(unsigned int state, int id)
{
unsigned long response = 0;
unsigned int timeout = 0x80000;
long start_time = USEC_TIMER;
while (1)
{
int ret = sd_command
(SD_SEND_STATUS, currcard->rca, &response, SDICMD_RES_TYPE1);
long us;
if (ret < 0)
return ret*100 - id;
if (((response >> 9) & 0xf) == state)
{
return 0;
}
if (TIME_AFTER(USEC_TIMER, start_time + timeout))
return -EC_WAIT_STATE_FAILED*100 - id;
us = USEC_TIMER;
if (TIME_AFTER(us, next_yield))
{
yield();
timeout += USEC_TIMER - us;
next_yield = us + MIN_YIELD_PERIOD;
}
}
}
static void sd_card_mux(int card_no)
{
/* We only support the default card */
(void)card_no;
}
#ifdef HAVE_HOTSWAP
static inline bool card_detect_target(void)
{
#ifdef HAVE_HOTSWAP
return (GPIOB & (1<<26)) == 0; /* low active */
#else
return false;
#endif
}
static int sd1_oneshot_callback(struct timeout *tmo)
{
(void)tmo;
/* This is called only if the state was stable for 300ms - check state
* and post appropriate event. */
if (card_detect_target())
queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
else
queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
return 0;
}
void EXT0(void)
{
static struct timeout sd1_oneshot;
timeout_register(&sd1_oneshot, sd1_oneshot_callback, (3*HZ/10), 0);
}
bool sd_removable(IF_MD_NONVOID(int card_no))
{
#ifndef HAVE_MULTIDRIVE
const int card_no = 0;
#endif
return (card_no == CARD_NUM_SLOT);
}
bool sd_present(IF_MD_NONVOID(int card_no))
{
#ifdef HAVE_MULTIDRIVE
(void)card_no;
#endif
return card_detect_target();
}
#else
bool sd_removable(IF_MD_NONVOID(int card_no))
{
#ifndef HAVE_MULTIDRIVE
const int card_no = 0;
#endif
(void)card_no;
return false;
}
#endif /* HAVE_HOTSWAP */
static void sd_init_device(int card_no)
{
int ret;
unsigned long response;
/* Initialise card data as blank */
memset(currcard, 0, sizeof(*currcard));
/* Switch card mux to card to initialize */
sd_card_mux(card_no);
#ifdef HAVE_HOTSWAP
/* Check card is inserted */
if (card_no == CARD_NUM_SLOT)
{
if (GPIOB & (1<<26))
{
ret = -EC_NOCARD;
goto card_init_error;
}
/* Card will not power up unless this is done */
GPIOC_CLEAR = (1<<24);
}
#endif
ret = sd_command(SD_GO_IDLE_STATE, 0, NULL, SDICMD_RES_TYPE1);
if (ret < 0)
goto card_init_error;
/* Use slow clock during identification (24MHz / 60 = 400kHz) */
SDICLK = (1<<12) | 59;
sd_command(SD_SEND_IF_COND, 0x1aa, &response, SDICMD_RES_TYPE3);
if (!sd_poll_status(SDISTATUS_CMD_PATH_RDY, 100000))
goto card_init_error;
currcard->ocr = 0;
long start_tick = current_tick;
while ((currcard->ocr & (1<<31)) == 0
&& TIME_BEFORE(current_tick, start_tick + HZ))
{
udelay(100);
sd_command(SD_APP_CMD, 0, NULL, SDICMD_RES_TYPE1);
int arg = 0x100000 | ((response == 0x1aa) ? (1<<30):0);
sd_command(SD_APP_OP_COND, arg, &currcard->ocr, SDICMD_RES_TYPE3);
}
if ((currcard->ocr & (1<<31)) == 0)
{
ret = -EC_POWER_UP;
goto card_init_error;
}
ret = sd_command
(SD_ALL_SEND_CID, 0, currcard->cid, SDICMD_RES_TYPE2);
if (ret < 0)
goto card_init_error;
ret = sd_command
(SD_SEND_RELATIVE_ADDR, 0, &currcard->rca, SDICMD_RES_TYPE1);
if (ret < 0)
goto card_init_error;
ret = sd_command
(SD_SEND_CSD, currcard->rca, currcard->csd, SDICMD_RES_TYPE2);
if (ret < 0)
goto card_init_error;
sd_parse_csd(currcard);
ret = sd_command
(SD_SELECT_CARD, currcard->rca, NULL, SDICMD_RES_TYPE1);
if (ret < 0)
goto card_init_error;
ret = sd_command
(SD_APP_CMD, currcard->rca, NULL, SDICMD_RES_TYPE1);
if (ret < 0)
goto card_init_error;
ret = sd_command /* 4 bit */
(SD_SET_BUS_WIDTH, currcard->rca | 2, NULL, SDICMD_RES_TYPE1);
if (ret < 0)
goto card_init_error;
ret = sd_command
(SD_SET_BLOCKLEN, currcard->blocksize, NULL, SDICMD_RES_TYPE1);
if (ret < 0)
goto card_init_error;
currcard->initialized = 1;
return;
/* Card failed to initialize so disable it */
card_init_error:
currcard->initialized = ret;
return;
}
/* lock must already be acquired */
static void sd_select_device(int card_no)
{
currcard = &card_info[card_no];
if (currcard->initialized > 0)
{
/* This card is already initialized - switch to it */
sd_card_mux(card_no);
return;
}
if (currcard->initialized == 0)
{
/* Card needs (re)init */
sd_init_device(card_no);
}
}
int sd_read_sectors(IF_MD(int card_no,) unsigned long start, int incount,
void* inbuf)
{
#ifndef HAVE_MULTIDRIVE
const int card_no = 0;
#endif
int ret = 0;
bool aligned;
unsigned char* buf_end;
mutex_lock(&sd_mtx);
sd_enable(true);
led(true);
sd_read_retry:
if ((card_no == CARD_NUM_SLOT) && !card_detect_target())
{
/* no external sd-card inserted */
ret = -EC_NOCARD;
goto sd_read_error;
}
sd_select_device(card_no);
if (currcard->initialized < 0)
{
ret = currcard->initialized;
goto sd_read_error;
}
last_disk_activity = current_tick;
ret = sd_wait_for_state(SD_TRAN, EC_TRAN_READ_ENTRY);
if (ret < 0)
goto sd_read_error;
/* Use full SD clock for data transfer (PCK_SDMMC) */
SDICLK = (1<<13) | (1<<12); /* bypass divider | enable */
/* Block count | FIFO count | Block size (2^9) | 4-bit bus */
SDIDCTRL = (incount << 13) | (4<<8) | (9<<4) | (1<<2);
SDIDCTRL |= (1<<12); /* nReset */
SDIDCTRL2 = (1<<2); /* multi block, read */
if (currcard->ocr & (1<<30))
ret = sd_command(SD_READ_MULTIPLE_BLOCK, start, NULL, SDICMD_RES_TYPE1);
else
ret = sd_command(SD_READ_MULTIPLE_BLOCK, start * 512, NULL, SDICMD_RES_TYPE1);
if (ret < 0)
goto sd_read_error;
aligned = (((int)inbuf & 3) == 0);
buf_end = (unsigned char *)inbuf + incount * currcard->blocksize;
while (inbuf < (void*)buf_end)
{
if (!sd_poll_status(SDISTATUS_FIFO_FETCH_REQ, 100000))
goto sd_read_error;
if (aligned)
{
unsigned int* ptr = (unsigned int*)inbuf;
*ptr++ = SDIRDATA;
*ptr++ = SDIRDATA;
*ptr++ = SDIRDATA;
*ptr = SDIRDATA;
}
else
{
int tmp_buf[4];
tmp_buf[0] = SDIRDATA;
tmp_buf[1] = SDIRDATA;
tmp_buf[2] = SDIRDATA;
tmp_buf[3] = SDIRDATA;
memcpy(inbuf, tmp_buf, 16);
}
inbuf += 16;
}
ret = sd_command(SD_STOP_TRANSMISSION, 0, NULL, SDICMD_RES_TYPE1);
if (ret < 0)
goto sd_read_error;
ret = sd_wait_for_state(SD_TRAN, EC_TRAN_READ_EXIT);
if (ret < 0)
goto sd_read_error;
while (1)
{
led(false);
sd_enable(false);
mutex_unlock(&sd_mtx);
return ret;
sd_read_error:
if (sd_status[card_no].retry < sd_status[card_no].retry_max
&& ret != -EC_NOCARD)
{
sd_status[card_no].retry++;
currcard->initialized = 0;
goto sd_read_retry;
}
}
}
int sd_write_sectors(IF_MD(int card_no,) unsigned long start, int count,
const void* outbuf)
{
/* Write support is not finished yet */
/* TODO: The standard suggests using ACMD23 prior to writing multiple blocks
to improve performance */
#ifndef HAVE_MULTIDRIVE
const int card_no = 0;
#endif
int ret;
const unsigned char *buf_end;
bool aligned;
if ((card_no == CARD_NUM_SLOT) && (GPIOA & 0x10))
{
/* write protect tab set */
return -EC_WRITE_PROTECT;
}
mutex_lock(&sd_mtx);
sd_enable(true);
led(true);
sd_write_retry:
if ((card_no == CARD_NUM_SLOT) && !card_detect_target())
{
/* no external sd-card inserted */
ret = -EC_NOCARD;
goto sd_write_error;
}
sd_select_device(card_no);
if (currcard->initialized < 0)
{
ret = currcard->initialized;
goto sd_write_error;
}
ret = sd_wait_for_state(SD_TRAN, EC_TRAN_WRITE_ENTRY);
if (ret < 0)
goto sd_write_error;
/* Use full SD clock for data transfer (PCK_SDMMC) */
SDICLK = (1<<13) | (1<<12); /* bypass divider | enable */
/* Block count | FIFO count | Block size (2^9) | 4-bit bus */
SDIDCTRL = (count<<13) | (4<<8) | (9<<4) | (1<<2);
SDIDCTRL |= (1<<12); /* nReset */
SDIDCTRL2 = (1<<2) | (1<<1); /* multi block, write */
if (currcard->ocr & (1<<30))
ret = sd_command(SD_WRITE_MULTIPLE_BLOCK, start, NULL, SDICMD_RES_TYPE1);
else
ret = sd_command(SD_WRITE_MULTIPLE_BLOCK, start * 512, NULL, SDICMD_RES_TYPE1);
if (ret < 0)
goto sd_write_error;
aligned = (((int)outbuf & 3) == 0);
buf_end = (unsigned char *)outbuf + count * currcard->blocksize;
while (outbuf < (void*)buf_end)
{
if (aligned)
{
unsigned int* ptr = (unsigned int*)outbuf;
SDIWDATA = *ptr++;
SDIWDATA = *ptr++;
SDIWDATA = *ptr++;
SDIWDATA = *ptr;
}
else
{
int tmp_buf[4];
memcpy(tmp_buf, outbuf, 16);
SDIWDATA = tmp_buf[0];
SDIWDATA = tmp_buf[1];
SDIWDATA = tmp_buf[2];
SDIWDATA = tmp_buf[3];
}
outbuf += 16;
/* Wait for the FIFO to empty */
if (!sd_poll_status(SDISTATUS_FIFO_LOAD_REQ, 0x80000))
{
ret = -EC_FIFO_WR_EMPTY;
goto sd_write_error;
}
}
last_disk_activity = current_tick;
if (!sd_poll_status(SDISTATUS_MULTIBLOCK_END, 0x80000))
{
ret = -EC_FIFO_WR_DONE;
goto sd_write_error;
}
ret = sd_command(SD_STOP_TRANSMISSION, 0, NULL, SDICMD_RES_TYPE1);
if (ret < 0)
goto sd_write_error;
ret = sd_wait_for_state(SD_TRAN, EC_TRAN_WRITE_EXIT);
if (ret < 0)
goto sd_write_error;
while (1)
{
led(false);
sd_enable(false);
mutex_unlock(&sd_mtx);
return ret;
sd_write_error:
if (sd_status[card_no].retry < sd_status[card_no].retry_max
&& ret != -EC_NOCARD)
{
sd_status[card_no].retry++;
currcard->initialized = 0;
goto sd_write_retry;
}
}
}
static void sd_thread(void) NORETURN_ATTR;
static void sd_thread(void)
{
struct queue_event ev;
bool idle_notified = false;
while (1)
{
queue_wait_w_tmo(&sd_queue, &ev, HZ);
switch ( ev.id )
{
#ifdef HAVE_HOTSWAP
case SYS_HOTSWAP_INSERTED:
case SYS_HOTSWAP_EXTRACTED:;
int success = 1;
/* Release "by force" */
disk_unmount(sd_first_drive + CARD_NUM_SLOT);
mutex_lock(&sd_mtx); /* lock-out card activity */
/* Force card init for new card, re-init for re-inserted one or
* clear if the last attempt to init failed with an error. */
card_info[CARD_NUM_SLOT].initialized = 0;
sd_status[CARD_NUM_SLOT].retry = 0;
mutex_unlock(&sd_mtx);
if (ev.id == SYS_HOTSWAP_INSERTED)
success = disk_mount(sd_first_drive + CARD_NUM_SLOT);
if (success)
queue_broadcast(SYS_FS_CHANGED, 0);
break;
#endif /* HAVE_HOTSWAP */
case SYS_TIMEOUT:
if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
{
idle_notified = false;
}
else
{
/* never let a timer wrap confuse us */
next_yield = USEC_TIMER;
if (!idle_notified)
{
call_storage_idle_notifys(false);
idle_notified = true;
}
}
break;
case SYS_USB_CONNECTED:
usb_acknowledge(SYS_USB_CONNECTED_ACK);
/* Wait until the USB cable is extracted again */
usb_wait_for_disconnect(&sd_queue);
break;
}
}
}
void sd_enable(bool on)
{
if(on)
{
/* Enable controller & clock */
BCLKCTR |= DEV_SDMMC;
PCLK_SDMMC = PCK_EN | (CKSEL_PLL0<<24) | 7; /* 192/8 = 24MHz */
}
else
{
/* Disable controller & clock */
BCLKCTR &= ~DEV_SDMMC;
PCLK_SDMMC &= ~PCK_EN;
}
}
int sd_init(void)
{
int ret = 0;
if (!initialized)
mutex_init(&sd_mtx);
mutex_lock(&sd_mtx);
led(false);
if (!initialized)
{
initialized = true;
SWRESET |= DEV_SDMMC;
SWRESET &= ~DEV_SDMMC;
/* Configure dual-purpose pins for SD usage */
PORTCFG0 &= ~(3<<16);
PORTCFG0 |= (1<<16); /* SD_D0 & SD_D1 */
PORTCFG2 &= ~((3<<2) | (3<<0));
PORTCFG2 |= ((1<<2) | (1<<0)); /* SD_D2/D3/CK/CMD */
/* Configure card detection GPIO as input */
GPIOB_DIR &= ~(1<<26);
/* Configure card power(?) GPIO as output */
GPIOC_DIR |= (1<<24);
queue_init(&sd_queue, true);
create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0,
sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE)
IF_COP(, CPU));
sleep(HZ/10);
#ifdef HAVE_HOTSWAP
/* Configure interrupts for the card slot */
TMODE &= ~EXT0_IRQ_MASK; /* edge-triggered */
TMODEA |= EXT0_IRQ_MASK; /* trigger on both edges */
IEN |= EXT0_IRQ_MASK; /* enable the interrupt */
#endif
}
mutex_unlock(&sd_mtx);
return ret;
}
long sd_last_disk_activity(void)
{
return last_disk_activity;
}
tCardInfo *card_get_info_target(int card_no)
{
return &card_info[card_no];
}
#ifdef CONFIG_STORAGE_MULTI
int sd_num_drives(int first_drive)
{
/* Store which logical drive number(s) we have been assigned */
sd_first_drive = first_drive;
#if defined(HAVE_INTERNAL_SD) && defined(HAVE_HOTSWAP)
return 2;
#else
return 1;
#endif
}
void sd_sleepnow(void)
{
}
bool sd_disk_is_active(void)
{
return false;
}
int sd_soft_reset(void)
{
return 0;
}
int sd_spinup_time(void)
{
return 0;
}
#endif /* CONFIG_STORAGE_MULTI */