Skip to content

Commit

Permalink
Initial support for semi-passive SD card adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed Jun 8, 2024
1 parent 91d7801 commit 232a13d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
6 changes: 6 additions & 0 deletions gc/sdcard/card_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ enum {
CARDIO_ADDRESSING_BLOCK
};

enum {
CARDIO_TRANSFER_IMM = 0,
CARDIO_TRANSFER_DMA
};

extern u8 g_CSD[MAX_DRIVE][16];
extern u8 g_CID[MAX_DRIVE][16];
extern u8 g_mCode[MAX_MI_NUM];
Expand Down Expand Up @@ -59,6 +64,7 @@ u32 sdgecko_getPageSize(s32 drv_no);
s32 sdgecko_setPageSize(s32 drv_no, u32 size);

u32 sdgecko_getAddressingType(s32 drv_no);
u32 sdgecko_getTransferMode(s32 drv_no);

bool sdgecko_isInserted(s32 drv_no);
bool sdgecko_isInitialized(s32 drv_no);
Expand Down
1 change: 1 addition & 0 deletions libogc/gcsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static bool __gcsd_readSectors(int n, sec_t sector, sec_t numSectors, void *buff

if((u32)sector != sector) return false;
if((u32)numSectors != numSectors) return false;
if(!SYS_IsDMAAddress(buffer)) return false;

if(numSectors == 1)
ret = sdgecko_readSector(n, buffer, sector);
Expand Down
59 changes: 46 additions & 13 deletions libogc/sdgecko_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ static u16 _ioCrc16Table[256];
// SDHC support
static u32 _initType[MAX_DRIVE];
static u32 _ioAddressingType[MAX_DRIVE];
static u32 _ioTransferMode[MAX_DRIVE];

extern unsigned long gettick(void);

Expand Down Expand Up @@ -399,7 +400,9 @@ static s32 __card_readresponse(s32 drv_no,void *buf,s32 len)

__exi_wait(drv_no);

if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
if((_ioTransferMode[drv_no]==CARDIO_TRANSFER_DMA?
EXI_SelectSD(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no]):
EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no]))==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -440,7 +443,9 @@ static s32 __card_stopreadresponse(s32 drv_no,void *buf,s32 len)

__exi_wait(drv_no);

if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
if((_ioTransferMode[drv_no]==CARDIO_TRANSFER_DMA?
EXI_SelectSD(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no]):
EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no]))==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -542,7 +547,9 @@ static s32 __card_datares(s32 drv_no,void *buf)

__exi_wait(drv_no);

if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
if((_ioTransferMode[drv_no]==CARDIO_TRANSFER_DMA?
EXI_SelectSD(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no]):
EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no]))==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -658,14 +665,16 @@ static s32 __card_dataread(s32 drv_no,void *buf,u32 len)

__exi_wait(drv_no);

if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
if((_ioTransferMode[drv_no]==CARDIO_TRANSFER_DMA?
EXI_SelectSD(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no]):
EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no]))==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}

ret = CARDIO_ERROR_READY;
ptr = buf;
for(cnt=0;cnt<len;cnt++) ptr[cnt] = 0xff;
*ptr = 0xff;
if(EXI_ImmEx(drv_no,ptr,1,EXI_READWRITE)==0) {
EXI_Deselect(drv_no);
EXI_Unlock(drv_no);
Expand Down Expand Up @@ -702,11 +711,19 @@ static s32 __card_dataread(s32 drv_no,void *buf,u32 len)
}
}

*ptr = 0xff;
if(EXI_ImmEx(drv_no,ptr,len,EXI_READWRITE)==0) {
EXI_Deselect(drv_no);
EXI_Unlock(drv_no);
return CARDIO_ERROR_IOERROR;
if(_ioTransferMode[drv_no]==CARDIO_TRANSFER_DMA) {
if(EXI_DmaEx(drv_no,ptr,len,EXI_READ)==0) {
EXI_Deselect(drv_no);
EXI_Unlock(drv_no);
return CARDIO_ERROR_IOERROR;
}
} else {
for(cnt=0;cnt<len;cnt++) ptr[cnt] = 0xff;
if(EXI_ImmEx(drv_no,ptr,len,EXI_READWRITE)==0) {
EXI_Deselect(drv_no);
EXI_Unlock(drv_no);
return CARDIO_ERROR_IOERROR;
}
}

/* sleep 1us*/
Expand Down Expand Up @@ -1239,8 +1256,9 @@ void sdgecko_initIODefault(void)
_ioError[i] = 0;
_ioCardInserted[i] = FALSE;
_ioFlag[i] = NOT_INITIALIZED;
_ioAddressingType[i] = CARDIO_ADDRESSING_BYTE;
_initType[i] = TYPE_SD;
_ioAddressingType[i] = CARDIO_ADDRESSING_BYTE;
_ioTransferMode[i] = CARDIO_TRANSFER_IMM;
_ioCardSelect[i] = EXI_DEVICE_0;
_ioCardFreq[i] = EXI_SPEED16MHZ;
LWP_InitQueue(&_ioEXILock[i]);
Expand All @@ -1259,10 +1277,20 @@ s32 sdgecko_initIO(s32 drv_no)
_ioCardInserted[drv_no] = __card_check(drv_no);

if(_ioCardInserted[drv_no]==TRUE) {
_initType[drv_no] = TYPE_SD;
_ioFlag[drv_no] = INITIALIZING;
_initType[drv_no] = TYPE_SD;
_ioAddressingType[drv_no] = CARDIO_ADDRESSING_BYTE;
if(__card_softreset(drv_no)!=0) goto exit;

if(drv_no!=0 && _ioCardSelect[drv_no]==EXI_DEVICE_0) {
_ioTransferMode[drv_no] = CARDIO_TRANSFER_DMA;
if(__card_softreset(drv_no)!=0) {
_ioTransferMode[drv_no] = CARDIO_TRANSFER_IMM;
if(__card_softreset(drv_no)!=0) goto exit;
}
} else {
_ioTransferMode[drv_no] = CARDIO_TRANSFER_IMM;
if(__card_softreset(drv_no)!=0) goto exit;
}

if(__card_sendCMD8(drv_no)!=0) goto exit;
#ifdef _CARDIO_DEBUG
Expand Down Expand Up @@ -1587,6 +1615,11 @@ u32 sdgecko_getAddressingType(s32 drv_no)
return _ioAddressingType[drv_no];
}

u32 sdgecko_getTransferMode(s32 drv_no)
{
return _ioTransferMode[drv_no];
}

bool sdgecko_isInserted(s32 drv_no)
{
return (_ioCardInserted[drv_no] = __card_check(drv_no));
Expand Down

0 comments on commit 232a13d

Please sign in to comment.