imx233: completely rework the sd driver

Change-Id: I456df0d9f5eaee435bdfd5a3f667055715e53d2a
This commit is contained in:
Amaury Pouly 2012-05-19 13:45:29 +02:00
parent 3f59cf9708
commit 8b53c0f9a9
3 changed files with 246 additions and 125 deletions

View file

@ -24,62 +24,107 @@
#include "sdmmc.h" #include "sdmmc.h"
#include "ssp-imx233.h" #include "ssp-imx233.h"
#include "pinctrl-imx233.h" #include "pinctrl-imx233.h"
#include "partitions-imx233.h"
#include "button-target.h" #include "button-target.h"
#include "fat.h" #include "fat.h"
#include "disk.h" #include "disk.h"
#include "usb.h" #include "usb.h"
#include "debug.h" #include "debug.h"
/** struct sd_config_t
* This code assumes a single SD card slot {
*/ const char *name; /* name(for debug) */
int flags; /* flags */
int power_pin; /* power pin */
int power_delay; /* extra power up delay */
int ssp; /* associated ssp block */
};
/* flags */
#define POWER_PIN (1 << 0)
#define POWER_INVERTED (1 << 1)
#define REMOVABLE (1 << 2)
#define DETECT_INVERTED (1 << 3)
#define POWER_DELAY (1 << 4)
#define WINDOW (1 << 5)
#define PIN(bank,pin) ((bank) << 5 | (pin))
#define PIN2BANK(v) ((v) >> 5)
#define PIN2PIN(v) ((v) & 0x1f)
struct sd_config_t sd_config[] =
{
#ifdef SANSA_FUZEPLUS #ifdef SANSA_FUZEPLUS
#define SD_SSP 1 /* The Fuze+ uses pin #B0P8 for power */
{
.name = "microSD",
.flags = POWER_PIN | POWER_INVERTED | REMOVABLE,
.power_pin = PIN(0, 8),
.ssp = 1
},
#else #else
#error You need to configure the ssp to use #error You need to write the sd config!
#endif #endif
};
static tCardInfo card_info; #define SD_NUM_DRIVES (sizeof(sd_config) / sizeof(sd_config[0]))
static long sd_stack [(DEFAULT_STACK_SIZE*2 + 0x200)/sizeof(long)];
#define SD_CONF(drive) sd_config[drive]
#define SD_FLAGS(drive) SD_CONF(drive).flags
#define SD_SSP(drive) SD_CONF(drive).ssp
#define IF_FIRST_DRIVE(drive) if((drive) == 0)
#define IF_SECOND_DRIVE(drive) if((drive) == 1)
static tCardInfo card_info[SD_NUM_DRIVES];
static long sd_stack[(DEFAULT_STACK_SIZE*2 + 0x200)/sizeof(long)];
static struct mutex sd_mutex; static struct mutex sd_mutex;
static const char sd_thread_name[] = "sd"; static const char sd_thread_name[] = "sd";
static struct event_queue sd_queue; static struct event_queue sd_queue;
static int sd_first_drive; static int sd_first_drive;
static int last_disk_activity; static int last_disk_activity;
static unsigned sd_window_start[SD_NUM_DRIVES];
static unsigned sd_window_end[SD_NUM_DRIVES];
static void sd_detect_callback(int ssp) static void sd_detect_callback(int ssp)
{ {
(void)ssp;
/* This is called only if the state was stable for 300ms - check state /* This is called only if the state was stable for 300ms - check state
* and post appropriate event. */ * and post appropriate event. */
if(imx233_ssp_sdmmc_detect(SD_SSP)) if(imx233_ssp_sdmmc_detect(ssp))
queue_broadcast(SYS_HOTSWAP_INSERTED, 0); queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
else else
queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0); queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
imx233_ssp_sdmmc_setup_detect(SD_SSP, true, sd_detect_callback, false); imx233_ssp_sdmmc_setup_detect(ssp, true, sd_detect_callback, false,
imx233_ssp_sdmmc_is_detect_inverted(ssp));
} }
void sd_power(bool on) void sd_power(int drive, bool on)
{ {
#ifdef SANSA_FUZEPLUS /* power chip if needed */
/* The Fuze+ uses pin B0P8 for whatever reason, power ? */ if(SD_FLAGS(drive) & POWER_PIN)
imx233_pinctrl_acquire_pin(0, 8, "sd power"); {
imx233_set_pin_function(0, 8, PINCTRL_FUNCTION_GPIO); int bank = PIN2BANK(SD_CONF(drive).power_pin);
imx233_enable_gpio_output(0, 8, true); int pin = PIN2PIN(SD_CONF(drive).power_pin);
imx233_set_gpio_output(0, 8, !on); imx233_pinctrl_acquire_pin(bank, pin, "sd power");
/* disable pull ups when not needed to save power */ imx233_set_pin_function(bank, pin, PINCTRL_FUNCTION_GPIO);
imx233_ssp_setup_ssp1_sd_mmc_pins(on, 4, PINCTRL_DRIVE_4mA, false); imx233_enable_gpio_output(bank, pin, true);
#endif if(SD_FLAGS(drive) & POWER_INVERTED)
imx233_set_gpio_output(bank, pin, !on);
else
imx233_set_gpio_output(bank, pin, on);
}
if(SD_FLAGS(drive) & POWER_DELAY)
sleep(SD_CONF(drive).power_delay);
/* setup pins, never use alternatives pin on SSP1 because these are force
* bus width >= 4 and SD cannot use more than 4 data lines. */
if(SD_SSP(drive) == 1)
imx233_ssp_setup_ssp1_sd_mmc_pins(on, 4, PINCTRL_DRIVE_4mA, false);
else
imx233_ssp_setup_ssp2_sd_mmc_pins(on, 4, PINCTRL_DRIVE_4mA);
} }
void sd_enable(bool on) void sd_enable(bool on)
{ {
static int sd_enable = 2; /* 2 means not on and not off, for init purpose */ (void) on;
if(sd_enable == on)
return;
sd_enable = on;
} }
#define MCI_NO_RESP 0 #define MCI_NO_RESP 0
@ -89,15 +134,15 @@ void sd_enable(bool on)
#define MCI_NOCRC (1<<3) #define MCI_NOCRC (1<<3)
#define MCI_BUSY (1<<4) #define MCI_BUSY (1<<4)
static bool send_cmd(uint8_t cmd, uint32_t arg, uint32_t flags, uint32_t *resp) static bool send_cmd(int drive, uint8_t cmd, uint32_t arg, uint32_t flags, uint32_t *resp)
{ {
if((flags & MCI_ACMD) && !send_cmd(SD_APP_CMD, card_info.rca, MCI_RESP, resp)) if((flags & MCI_ACMD) && !send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MCI_RESP, resp))
return false; return false;
enum imx233_ssp_resp_t resp_type = (flags & MCI_LONG_RESP) ? SSP_LONG_RESP : enum imx233_ssp_resp_t resp_type = (flags & MCI_LONG_RESP) ? SSP_LONG_RESP :
(flags & MCI_RESP) ? SSP_SHORT_RESP : SSP_NO_RESP; (flags & MCI_RESP) ? SSP_SHORT_RESP : SSP_NO_RESP;
enum imx233_ssp_error_t ret = imx233_ssp_sd_mmc_transfer(SD_SSP, cmd, arg, enum imx233_ssp_error_t ret = imx233_ssp_sd_mmc_transfer(SD_SSP(drive), cmd,
resp_type, NULL, 0, !!(flags & MCI_BUSY), false, resp); arg, resp_type, NULL, 0, !!(flags & MCI_BUSY), false, resp);
if(resp_type == SSP_LONG_RESP) if(resp_type == SSP_LONG_RESP)
{ {
/* Our SD codes assume most significant word first, so reverse resp */ /* Our SD codes assume most significant word first, so reverse resp */
@ -111,7 +156,7 @@ static bool send_cmd(uint8_t cmd, uint32_t arg, uint32_t flags, uint32_t *resp)
return ret == SSP_SUCCESS; return ret == SSP_SUCCESS;
} }
static int sd_wait_for_tran_state(void) static int sd_wait_for_tran_state(int drive)
{ {
unsigned long response; unsigned long response;
unsigned int timeout = current_tick + 5*HZ; unsigned int timeout = current_tick + 5*HZ;
@ -119,7 +164,7 @@ static int sd_wait_for_tran_state(void)
while (1) while (1)
{ {
while(!send_cmd(SD_SEND_STATUS, card_info.rca, MCI_RESP, &response) && cmd_retry > 0) while(!send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca, MCI_RESP, &response) && cmd_retry > 0)
cmd_retry--; cmd_retry--;
if(cmd_retry <= 0) if(cmd_retry <= 0)
@ -135,33 +180,35 @@ static int sd_wait_for_tran_state(void)
} }
} }
static int sd_init_card(void) static int sd_init_card(int drive)
{ {
sd_enable(false); /* sanity check against bad configuration of SD_NUM_DRIVES/NUM_DRIVES */
sd_power(false); if((unsigned)drive >= SD_NUM_DRIVES)
sd_power(true); panicf("drive >= SD_NUM_DRIVES in sd_init_card!");
sd_enable(true); int ssp = SD_SSP(drive);
imx233_ssp_start(SD_SSP); sd_power(drive, false);
imx233_ssp_softreset(SD_SSP); sd_power(drive, true);
imx233_ssp_set_mode(SD_SSP, HW_SSP_CTRL1__SSP_MODE__SD_MMC); imx233_ssp_start(ssp);
imx233_ssp_softreset(ssp);
imx233_ssp_set_mode(ssp, HW_SSP_CTRL1__SSP_MODE__SD_MMC);
/* SSPCLK @ 96MHz /* SSPCLK @ 96MHz
* gives bitrate of 96000 / 240 / 1 = 400kHz */ * gives bitrate of 96000 / 240 / 1 = 400kHz */
imx233_ssp_set_timings(SD_SSP, 240, 0, 0xffff); imx233_ssp_set_timings(ssp, 240, 0, 0xffff);
imx233_ssp_sd_mmc_power_up_sequence(SD_SSP); imx233_ssp_sd_mmc_power_up_sequence(ssp);
imx233_ssp_set_bus_width(SD_SSP, 1); imx233_ssp_set_bus_width(ssp, 1);
imx233_ssp_set_block_size(SD_SSP, 9); imx233_ssp_set_block_size(ssp, 9);
card_info.rca = 0; card_info[drive].rca = 0;
bool sd_v2 = false; bool sd_v2 = false;
uint32_t resp; uint32_t resp;
long init_timeout; long init_timeout;
/* go to idle state */ /* go to idle state */
if(!send_cmd(SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL)) if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
return -1; return -1;
/* CMD8 Check for v2 sd card. Must be sent before using ACMD41 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
Non v2 cards will not respond to this command */ Non v2 cards will not respond to this command */
if(send_cmd(SD_SEND_IF_COND, 0x1AA, MCI_RESP, &resp)) if(send_cmd(drive, SD_SEND_IF_COND, 0x1AA, MCI_RESP, &resp))
if((resp & 0xFFF) == 0x1AA) if((resp & 0xFFF) == 0x1AA)
sd_v2 = true; sd_v2 = true;
/* timeout for initialization is 1sec, from SD Specification 2.00 */ /* timeout for initialization is 1sec, from SD Specification 2.00 */
@ -173,67 +220,84 @@ static int sd_init_card(void)
return -2; return -2;
/* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */ /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
if(!send_cmd(SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)), if(!send_cmd(drive, SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)),
MCI_ACMD|MCI_NOCRC|MCI_RESP, &card_info.ocr)) MCI_ACMD|MCI_NOCRC|MCI_RESP, &card_info[drive].ocr))
return -100; return -100;
} while(!(card_info.ocr & (1<<31))); } while(!(card_info[drive].ocr & (1<<31)));
/* CMD2 send CID */ /* CMD2 send CID */
if(!send_cmd(SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP, card_info.cid)) if(!send_cmd(drive, SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP, card_info[drive].cid))
return -3; return -3;
/* CMD3 send RCA */ /* CMD3 send RCA */
if(!send_cmd(SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &card_info.rca)) if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &card_info[drive].rca))
return -4; return -4;
/* Try to switch V2 cards to HS timings, non HS seem to ignore this */ /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
if(sd_v2) if(sd_v2)
{ {
/* CMD7 w/rca: Select card to put it in TRAN state */ /* CMD7 w/rca: Select card to put it in TRAN state */
if(!send_cmd(SD_SELECT_CARD, card_info.rca, MCI_RESP, NULL)) if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_RESP, NULL))
return -5; return -5;
if(sd_wait_for_tran_state()) if(sd_wait_for_tran_state(drive))
return -6; return -6;
/* CMD6 */ /* CMD6 */
if(!send_cmd(SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL)) if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL))
return -7; return -7;
sleep(HZ/10); sleep(HZ/10);
/* go back to STBY state so we can read csd */ /* go back to STBY state so we can read csd */
/* CMD7 w/rca=0: Deselect card to put it in STBY state */ /* CMD7 w/rca=0: Deselect card to put it in STBY state */
if(!send_cmd(SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL)) if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
return -8; return -8;
} }
/* CMD9 send CSD */ /* CMD9 send CSD */
if(!send_cmd(SD_SEND_CSD, card_info.rca, MCI_RESP|MCI_LONG_RESP, card_info.csd)) if(!send_cmd(drive, SD_SEND_CSD, card_info[drive].rca, MCI_RESP|MCI_LONG_RESP, card_info[drive].csd))
return -9; return -9;
sd_parse_csd(&card_info); sd_parse_csd(&card_info[drive]);
/* SSPCLK @ 96MHz /* SSPCLK @ 96MHz
* gives bitrate of 96 / 4 / 1 = 24MHz */ * gives bitrate of 96 / 4 / 1 = 24MHz */
imx233_ssp_set_timings(SD_SSP, 4, 0, 0xffff); imx233_ssp_set_timings(ssp, 4, 0, 0xffff);
/* CMD7 w/rca: Select card to put it in TRAN state */ /* CMD7 w/rca: Select card to put it in TRAN state */
if(!send_cmd(SD_SELECT_CARD, card_info.rca, MCI_RESP, &resp)) if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_RESP, &resp))
return -12; return -12;
if(sd_wait_for_tran_state() < 0) if(sd_wait_for_tran_state(drive) < 0)
return -13; return -13;
/* ACMD6: set bus width to 4-bit */ /* ACMD6: set bus width to 4-bit */
if(!send_cmd(SD_SET_BUS_WIDTH, 2, MCI_RESP|MCI_ACMD, &resp)) if(!send_cmd(drive, SD_SET_BUS_WIDTH, 2, MCI_RESP|MCI_ACMD, &resp))
return -15; return -15;
/* ACMD42: disconnect the pull-up resistor on CD/DAT3 */ /* ACMD42: disconnect the pull-up resistor on CD/DAT3 */
if(!send_cmd(SD_SET_CLR_CARD_DETECT, 0, MCI_RESP|MCI_ACMD, &resp)) if(!send_cmd(drive, SD_SET_CLR_CARD_DETECT, 0, MCI_RESP|MCI_ACMD, &resp))
return -17; return -17;
/* Switch to 4-bit */ /* Switch to 4-bit */
imx233_ssp_set_bus_width(SD_SSP, 4); imx233_ssp_set_bus_width(ssp, 4);
card_info.initialized = 1; card_info[drive].initialized = 1;
/* compute window */
sd_window_start[drive] = 0;
sd_window_end[drive] = card_info[drive].numblocks;
if((SD_FLAGS(drive) & WINDOW) && imx233_partitions_is_window_enabled())
{
/* WARNING: sd_first_drive is not set at this point */
uint8_t mbr[512];
int ret = sd_read_sectors(IF_MD2(drive,) 0, 1, mbr);
if(ret)
panicf("Cannot read MBR: %d", ret);
ret = imx233_partitions_compute_window(mbr, &sd_window_start[drive],
&sd_window_end[drive]);
if(ret)
panicf("cannot compute partitions window: %d", ret);
card_info[drive].numblocks = sd_window_end[drive] - sd_window_start[drive];
}
return 0; return 0;
} }
@ -253,45 +317,51 @@ static void sd_thread(void)
case SYS_HOTSWAP_EXTRACTED: case SYS_HOTSWAP_EXTRACTED:
{ {
int microsd_init = 1; int microsd_init = 1;
fat_lock(); /* lock-out FAT activity first - /* lock-out FAT activity first -
prevent deadlocking via disk_mount that * prevent deadlocking via disk_mount that
would cause a reverse-order attempt with * would cause a reverse-order attempt with
another thread */ * another thread */
mutex_lock(&sd_mutex); /* lock-out card activity - direct calls fat_lock();
into driver that bypass the fat cache */ /* lock-out card activity - direct calls
* into driver that bypass the fat cache */
mutex_lock(&sd_mutex);
/* We now have exclusive control of fat cache and sd */ /* We now have exclusive control of fat cache and sd.
* Release "by force", ensure file
disk_unmount(sd_first_drive); /* 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 */ for(unsigned drive = 0; drive < SD_NUM_DRIVES; drive++)
/* 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.initialized = 0;
if(ev.id == SYS_HOTSWAP_INSERTED)
{ {
microsd_init = sd_init_card(); /* Skip non-removable drivers */
if(microsd_init < 0) /* initialisation failed */ if(!sd_removable(drive))
panicf("microSD init failed : %d", microsd_init); continue;
disk_unmount(sd_first_drive + drive);
/* 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[drive].initialized = 0;
microsd_init = disk_mount(sd_first_drive); /* 0 if fail */ if(ev.id == SYS_HOTSWAP_INSERTED)
{
microsd_init = sd_init_card(drive);
if(microsd_init < 0) /* initialisation failed */
panicf("%s init failed : %d", SD_CONF(drive).name, microsd_init);
microsd_init = disk_mount(sd_first_drive + drive); /* 0 if fail */
}
/*
* Mount succeeded, or this was an EXTRACTED event,
* in both cases notify the system about the changed filesystems
*/
if(microsd_init)
queue_broadcast(SYS_FS_CHANGED, 0);
} }
/*
* Mount succeeded, or this was an EXTRACTED event,
* in both cases notify the system about the changed filesystems
*/
if(microsd_init)
queue_broadcast(SYS_FS_CHANGED, 0);
sd_enable(false);
/* Access is now safe */ /* Access is now safe */
mutex_unlock(&sd_mutex); mutex_unlock(&sd_mutex);
fat_unlock(); fat_unlock();
break; break;
} }
case SYS_TIMEOUT: case SYS_TIMEOUT:
if(!TIME_BEFORE(current_tick, last_disk_activity+(3*HZ))) if(!TIME_BEFORE(current_tick, last_disk_activity +3 * HZ))
sd_enable(false); sd_enable(false);
break; break;
case SYS_USB_CONNECTED: case SYS_USB_CONNECTED:
@ -309,8 +379,13 @@ int sd_init(void)
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));
sd_enable(false);
imx233_ssp_sdmmc_setup_detect(SD_SSP, true, sd_detect_callback, false); for(unsigned drive = 0; drive < SD_NUM_DRIVES; drive++)
{
if(SD_FLAGS(drive) & REMOVABLE)
imx233_ssp_sdmmc_setup_detect(SD_SSP(drive), true, sd_detect_callback,
false, SD_FLAGS(drive) & DETECT_INVERTED);
}
return 0; return 0;
} }
@ -324,21 +399,28 @@ static int transfer_sectors(IF_MD2(int drive,) unsigned long start, int count, v
last_disk_activity = current_tick; last_disk_activity = current_tick;
mutex_lock(&sd_mutex); mutex_lock(&sd_mutex);
sd_enable(true);
if(card_info.initialized <= 0) if(card_info[drive].initialized <= 0)
{ {
ret = sd_init_card(); ret = sd_init_card(drive);
if(card_info.initialized <= 0) if(card_info[drive].initialized <= 0)
goto Lend; goto Lend;
} }
if(!send_cmd(SD_SELECT_CARD, card_info.rca, MCI_NO_RESP, NULL)) /* check window */
start += sd_window_start[drive];
if((start + count) > sd_window_end[drive])
{
ret = -201;
goto Lend;
}
if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_NO_RESP, NULL))
{ {
ret = -20; ret = -20;
goto Lend; goto Lend;
} }
ret = sd_wait_for_tran_state(); ret = sd_wait_for_tran_state(drive);
if(ret < 0) if(ret < 0)
goto Ldeselect; goto Ldeselect;
while(count != 0) while(count != 0)
@ -346,13 +428,14 @@ static int transfer_sectors(IF_MD2(int drive,) unsigned long start, int count, v
int this_count = MIN(count, IMX233_MAX_SSP_XFER_SIZE / 512); int this_count = MIN(count, IMX233_MAX_SSP_XFER_SIZE / 512);
/* Set bank_start to the correct unit (blocks or bytes) */ /* Set bank_start to the correct unit (blocks or bytes) */
int bank_start = start; int bank_start = start;
if(!(card_info.ocr & (1<<30))) /* not SDHC */ if(!(card_info[drive].ocr & (1<<30))) /* not SDHC */
bank_start *= SD_BLOCK_SIZE; bank_start *= SD_BLOCK_SIZE;
ret = imx233_ssp_sd_mmc_transfer(SD_SSP, read ? SD_READ_MULTIPLE_BLOCK : SD_WRITE_MULTIPLE_BLOCK, ret = imx233_ssp_sd_mmc_transfer(SD_SSP(drive),
read ? SD_READ_MULTIPLE_BLOCK : SD_WRITE_MULTIPLE_BLOCK,
bank_start, SSP_SHORT_RESP, buf, this_count, false, read, &resp); bank_start, SSP_SHORT_RESP, buf, this_count, false, read, &resp);
if(ret != SSP_SUCCESS) if(ret != SSP_SUCCESS)
break; break;
if(!send_cmd(SD_STOP_TRANSMISSION, 0, MCI_RESP|MCI_BUSY, &resp)) if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MCI_RESP|MCI_BUSY, &resp))
{ {
ret = -15; ret = -15;
break; break;
@ -364,51 +447,48 @@ static int transfer_sectors(IF_MD2(int drive,) unsigned long start, int count, v
Ldeselect: Ldeselect:
/* CMD7 w/rca =0 : deselects card & puts it in STBY state */ /* CMD7 w/rca =0 : deselects card & puts it in STBY state */
if(!send_cmd(SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL)) if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
ret = -23; ret = -23;
Lend: Lend:
mutex_unlock(&sd_mutex); mutex_unlock(&sd_mutex);
return ret; return ret;
} }
int sd_read_sectors(IF_MD2(int drive,) unsigned long start, int count, int sd_read_sectors(IF_MD2(int drive,) unsigned long start, int count, void* buf)
void* buf)
{ {
return transfer_sectors(IF_MD2(drive,) start, count, buf, true); return transfer_sectors(IF_MD2(drive,) start, count, buf, true);
} }
int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count, int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count, const void* buf)
const void* buf)
{ {
return transfer_sectors(IF_MD2(drive,) start, count, (void *)buf, false); return transfer_sectors(IF_MD2(drive,) start, count, (void *)buf, false);
} }
tCardInfo *card_get_info_target(int card_no) tCardInfo *card_get_info_target(int card_no)
{ {
(void)card_no; return &card_info[card_no];
return &card_info;
} }
int sd_num_drives(int first_drive) int sd_num_drives(int first_drive)
{ {
sd_first_drive = first_drive; sd_first_drive = first_drive;
return 1; return SD_NUM_DRIVES;
} }
bool sd_present(IF_MD(int drive)) bool sd_present(IF_MV_NONVOID(int drive))
{ {
IF_MD((void) drive); if(SD_FLAGS(drive) & REMOVABLE)
return imx233_ssp_sdmmc_detect(SD_SSP); return imx233_ssp_sdmmc_detect(SD_SSP(drive));
else
return true;
} }
bool sd_removable(IF_MD(int drive)) bool sd_removable(IF_MV_NONVOID(int drive))
{ {
IF_MD((void) drive); return SD_FLAGS(drive) & REMOVABLE;
return true;
} }
long sd_last_disk_activity(void) long sd_last_disk_activity(void)
{ {
return last_disk_activity; return last_disk_activity;
} }

View file

@ -28,6 +28,13 @@
#include "pinctrl-imx233.h" #include "pinctrl-imx233.h"
#include "dma-imx233.h" #include "dma-imx233.h"
/* for debug purpose */
#if 0
#define ASSERT_SSP(ssp) if(ssp < 1 || ssp > 2) panicf("ssp=%d in %s", ssp, __func__);
#else
#define ASSERT_SSP(ssp)
#endif
/* Used for DMA */ /* Used for DMA */
struct ssp_dma_command_t struct ssp_dma_command_t
{ {
@ -46,6 +53,7 @@ static struct ssp_dma_command_t ssp_dma_cmd[2];
static uint32_t ssp_bus_width[2]; static uint32_t ssp_bus_width[2];
static unsigned ssp_log_block_size[2]; static unsigned ssp_log_block_size[2];
static ssp_detect_cb_t ssp_detect_cb[2]; static ssp_detect_cb_t ssp_detect_cb[2];
static bool ssp_detect_invert[2];
void INT_SSP(int ssp) void INT_SSP(int ssp)
{ {
@ -93,6 +101,7 @@ void imx233_ssp_init(void)
void imx233_ssp_start(int ssp) void imx233_ssp_start(int ssp)
{ {
ASSERT_SSP(ssp)
if(ssp_in_use[ssp - 1]) if(ssp_in_use[ssp - 1])
return; return;
ssp_in_use[ssp - 1] = true; ssp_in_use[ssp - 1] = true;
@ -117,6 +126,7 @@ void imx233_ssp_start(int ssp)
void imx233_ssp_stop(int ssp) void imx233_ssp_stop(int ssp)
{ {
ASSERT_SSP(ssp)
if(!ssp_in_use[ssp - 1]) if(!ssp_in_use[ssp - 1])
return; return;
ssp_in_use[ssp - 1] = false; ssp_in_use[ssp - 1] = false;
@ -135,11 +145,13 @@ void imx233_ssp_stop(int ssp)
void imx233_ssp_softreset(int ssp) void imx233_ssp_softreset(int ssp)
{ {
ASSERT_SSP(ssp)
imx233_reset_block(&HW_SSP_CTRL0(ssp)); imx233_reset_block(&HW_SSP_CTRL0(ssp));
} }
void imx233_ssp_set_timings(int ssp, int divide, int rate, int timeout) void imx233_ssp_set_timings(int ssp, int divide, int rate, int timeout)
{ {
ASSERT_SSP(ssp)
HW_SSP_TIMING(ssp) = divide << HW_SSP_TIMING__CLOCK_DIVIDE_BP | rate | HW_SSP_TIMING(ssp) = divide << HW_SSP_TIMING__CLOCK_DIVIDE_BP | rate |
timeout << HW_SSP_TIMING__CLOCK_TIMEOUT_BP; timeout << HW_SSP_TIMING__CLOCK_TIMEOUT_BP;
} }
@ -209,6 +221,7 @@ void imx233_ssp_setup_ssp2_sd_mmc_pins(bool enable_pullups, unsigned bus_width,
void imx233_ssp_set_mode(int ssp, unsigned mode) void imx233_ssp_set_mode(int ssp, unsigned mode)
{ {
ASSERT_SSP(ssp)
switch(mode) switch(mode)
{ {
case HW_SSP_CTRL1__SSP_MODE__SD_MMC: case HW_SSP_CTRL1__SSP_MODE__SD_MMC:
@ -226,6 +239,7 @@ void imx233_ssp_set_mode(int ssp, unsigned mode)
void imx233_ssp_set_bus_width(int ssp, unsigned width) void imx233_ssp_set_bus_width(int ssp, unsigned width)
{ {
ASSERT_SSP(ssp)
switch(width) switch(width)
{ {
case 1: ssp_bus_width[ssp - 1] = HW_SSP_CTRL0__BUS_WIDTH__ONE_BIT; break; case 1: ssp_bus_width[ssp - 1] = HW_SSP_CTRL0__BUS_WIDTH__ONE_BIT; break;
@ -236,6 +250,7 @@ void imx233_ssp_set_bus_width(int ssp, unsigned width)
void imx233_ssp_set_block_size(int ssp, unsigned log_block_size) void imx233_ssp_set_block_size(int ssp, unsigned log_block_size)
{ {
ASSERT_SSP(ssp)
ssp_log_block_size[ssp - 1] = log_block_size; ssp_log_block_size[ssp - 1] = log_block_size;
} }
@ -243,6 +258,7 @@ enum imx233_ssp_error_t imx233_ssp_sd_mmc_transfer(int ssp, uint8_t cmd,
uint32_t cmd_arg, enum imx233_ssp_resp_t resp, void *buffer, unsigned block_count, uint32_t cmd_arg, enum imx233_ssp_resp_t resp, void *buffer, unsigned block_count,
bool wait4irq, bool read, uint32_t *resp_ptr) bool wait4irq, bool read, uint32_t *resp_ptr)
{ {
ASSERT_SSP(ssp)
mutex_lock(&ssp_mutex[ssp - 1]); mutex_lock(&ssp_mutex[ssp - 1]);
/* Enable all interrupts */ /* Enable all interrupts */
imx233_icoll_enable_interrupt(INT_SRC_SSP_DMA(ssp), true); imx233_icoll_enable_interrupt(INT_SRC_SSP_DMA(ssp), true);
@ -312,6 +328,7 @@ enum imx233_ssp_error_t imx233_ssp_sd_mmc_transfer(int ssp, uint8_t cmd,
void imx233_ssp_sd_mmc_power_up_sequence(int ssp) void imx233_ssp_sd_mmc_power_up_sequence(int ssp)
{ {
ASSERT_SSP(ssp)
__REG_CLR(HW_SSP_CMD0(ssp)) = HW_SSP_CMD0__SLOW_CLKING_EN; __REG_CLR(HW_SSP_CMD0(ssp)) = HW_SSP_CMD0__SLOW_CLKING_EN;
__REG_SET(HW_SSP_CMD0(ssp)) = HW_SSP_CMD0__CONT_CLKING_EN; __REG_SET(HW_SSP_CMD0(ssp)) = HW_SSP_CMD0__CONT_CLKING_EN;
mdelay(1); mdelay(1);
@ -320,6 +337,7 @@ void imx233_ssp_sd_mmc_power_up_sequence(int ssp)
static int ssp_detect_oneshot_callback(int ssp) static int ssp_detect_oneshot_callback(int ssp)
{ {
ASSERT_SSP(ssp)
if(ssp_detect_cb[ssp - 1]) if(ssp_detect_cb[ssp - 1])
ssp_detect_cb[ssp - 1](ssp); ssp_detect_cb[ssp - 1](ssp);
@ -348,11 +366,14 @@ static void detect_irq(int bank, int pin)
timeout_register(&ssp2_detect_oneshot, ssp2_detect_oneshot_callback, (3*HZ/10), 0); timeout_register(&ssp2_detect_oneshot, ssp2_detect_oneshot_callback, (3*HZ/10), 0);
} }
void imx233_ssp_sdmmc_setup_detect(int ssp, bool enable, ssp_detect_cb_t fn, bool first_time) void imx233_ssp_sdmmc_setup_detect(int ssp, bool enable, ssp_detect_cb_t fn,
bool first_time, bool invert)
{ {
ASSERT_SSP(ssp)
int bank = ssp == 1 ? 2 : 0; int bank = ssp == 1 ? 2 : 0;
int pin = ssp == 1 ? 1 : 19; int pin = ssp == 1 ? 1 : 19;
ssp_detect_cb[ssp - 1] = fn; ssp_detect_cb[ssp - 1] = fn;
ssp_detect_invert[ssp - 1] = invert;
if(enable) if(enable)
{ {
imx233_pinctrl_acquire_pin(bank, pin, ssp == 1 ? "ssp1 detect" : "ssp2 detect"); imx233_pinctrl_acquire_pin(bank, pin, ssp == 1 ? "ssp1 detect" : "ssp2 detect");
@ -361,10 +382,23 @@ void imx233_ssp_sdmmc_setup_detect(int ssp, bool enable, ssp_detect_cb_t fn, boo
} }
if(first_time && imx233_ssp_sdmmc_detect(ssp)) if(first_time && imx233_ssp_sdmmc_detect(ssp))
detect_irq(bank, pin); detect_irq(bank, pin);
imx233_setup_pin_irq(bank, pin, enable, true, !imx233_ssp_sdmmc_detect(ssp), detect_irq); imx233_setup_pin_irq(bank, pin, enable, true, !imx233_ssp_sdmmc_detect_raw(ssp), detect_irq);
}
bool imx233_ssp_sdmmc_is_detect_inverted(int ssp)
{
ASSERT_SSP(ssp)
return ssp_detect_invert[ssp - 1];
}
bool imx233_ssp_sdmmc_detect_raw(int ssp)
{
ASSERT_SSP(ssp)
return !!(HW_SSP_STATUS(ssp) & HW_SSP_STATUS__CARD_DETECT);
} }
bool imx233_ssp_sdmmc_detect(int ssp) bool imx233_ssp_sdmmc_detect(int ssp)
{ {
return !!(HW_SSP_STATUS(ssp) & HW_SSP_STATUS__CARD_DETECT); ASSERT_SSP(ssp)
return imx233_ssp_sdmmc_detect_raw(ssp) != ssp_detect_invert[ssp - 1];
} }

View file

@ -7,7 +7,7 @@
* \/ \/ \/ \/ \/ * \/ \/ \/ \/ \/
* $Id$ * $Id$
* *
* Copyright (C) 2011 by amaury Pouly * Copyright (C) 2011 by Amaury Pouly
* *
* Based on Rockbox iriver bootloader by Linus Nielsen Feltzing * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
* and the ipodlinux bootloader by Daniel Palffy and Bernard Leach * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
@ -170,8 +170,15 @@ void imx233_ssp_setup_ssp2_sd_mmc_pins(bool enable_pullups, unsigned bus_width,
/* after callback is fired, imx233_ssp_sdmmc_setup_detect needs to be called /* after callback is fired, imx233_ssp_sdmmc_setup_detect needs to be called
* to enable detection again. If first_time is true, the callback will * to enable detection again. If first_time is true, the callback will
* be called if the sd card is inserted when the function is called, otherwise * be called if the sd card is inserted when the function is called, otherwise
* it will be called on the next insertion change. */ * it will be called on the next insertion change.
void imx233_ssp_sdmmc_setup_detect(int ssp, bool enable, ssp_detect_cb_t fn, bool first_time); * By default, sd_detect=1 means sd inserted; invert reverses this behaviour */
void imx233_ssp_sdmmc_setup_detect(int ssp, bool enable, ssp_detect_cb_t fn,
bool first_time, bool invert);
/* needs prior setup with imx233_ssp_sdmmc_setup_detect */
bool imx233_ssp_sdmmc_is_detect_inverted(int spp);
/* raw value of the detect pin */
bool imx233_ssp_sdmmc_detect_raw(int ssp);
/* corrected value given the invert setting */
bool imx233_ssp_sdmmc_detect(int ssp); bool imx233_ssp_sdmmc_detect(int ssp);
/* SD/MMC requires that the card be provided the clock during an init sequence of /* SD/MMC requires that the card be provided the clock during an init sequence of
* at least 1msec (or 74 clocks). Does NOT touch the clock so it has to be correct. */ * at least 1msec (or 74 clocks). Does NOT touch the clock so it has to be correct. */