Skip to content

Commit

Permalink
Initial support for multiplexed SD cards
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed May 24, 2024
1 parent 8d2b894 commit 935bd73
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
3 changes: 3 additions & 0 deletions gc/sdcard/card_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ s32 sdgecko_doUnmount(s32 drv_no);
void sdgecko_insertedCB(s32 drv_no);
void sdgecko_ejectedCB(s32 drv_no);

u32 sdgecko_getDevice(s32 drv_no);
void sdgecko_setDevice(s32 drv_no, u32 dev);

u32 sdgecko_getSpeed(s32 drv_no);
void sdgecko_setSpeed(s32 drv_no, u32 freq);

Expand Down
3 changes: 2 additions & 1 deletion libogc/exi.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ s32 EXI_GetIDEx(s32 nChn,s32 nDev,u32 *nId)
s32 ret;
u32 reg;

if(nDev==EXI_DEVICE_0 && sdgecko_isInitialized(nChn)) {
if(nDev==sdgecko_getDevice(nChn)
&& sdgecko_isInitialized(nChn)) {
*nId = 0xffffffff;
return 1;
}
Expand Down
13 changes: 13 additions & 0 deletions libogc/gcsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,30 @@ static int __gcsd_init = 0;
static bool __gcsd_startup(int n)
{
s32 ret;
u32 dev;

if(!__gcsd_init) {
sdgecko_initBufferPool();
sdgecko_initIODefault();
__gcsd_init = 1;
}

dev = sdgecko_getDevice(n);

ret = sdgecko_preIO(n);
if(ret == CARDIO_ERROR_READY)
return true;

if(dev == EXI_DEVICE_0)
sdgecko_setDevice(n, EXI_DEVICE_2);
else
sdgecko_setDevice(n, EXI_DEVICE_0);

ret = sdgecko_preIO(n);
if(ret == CARDIO_ERROR_READY)
return true;

sdgecko_setDevice(n, dev);
return false;
}

Expand Down
42 changes: 30 additions & 12 deletions libogc/sdgecko_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ u16 g_dCode[MAX_MI_NUM][MAX_DI_NUM] =
}
};

static u32 _ioCardSelect[MAX_DRIVE];
static u32 _ioCardFreq[MAX_DRIVE];
static u32 _ioRetryCnt;
static cardiocallback _ioRetryCB = NULL;
Expand Down Expand Up @@ -269,7 +270,7 @@ static void __exi_wait(s32 drv_no)
u32 ret;

do {
if((ret=EXI_Lock(drv_no,EXI_DEVICE_0,__exi_unlock))==1) break;
if((ret=EXI_Lock(drv_no,_ioCardSelect[drv_no],__exi_unlock))==1) break;
LWP_ThreadSleep(_ioEXILock[drv_no]);
} while(ret==0);
}
Expand Down Expand Up @@ -298,7 +299,7 @@ static s32 __card_writecmd0(s32 drv_no)

__exi_wait(drv_no);

if(EXI_SelectSD(drv_no,EXI_DEVICE_0,_ioCardFreq[drv_no])==0) {
if(EXI_SelectSD(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand All @@ -315,7 +316,7 @@ static s32 __card_writecmd0(s32 drv_no)
}
EXI_Deselect(drv_no);

if(EXI_Select(drv_no,EXI_DEVICE_0,_ioCardFreq[drv_no])==0) {
if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -355,7 +356,7 @@ static s32 __card_writecmd(s32 drv_no,void *buf,s32 len)

__exi_wait(drv_no);

if(EXI_Select(drv_no,EXI_DEVICE_0,_ioCardFreq[drv_no])==0) {
if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -398,7 +399,7 @@ static s32 __card_readresponse(s32 drv_no,void *buf,s32 len)

__exi_wait(drv_no);

if(EXI_Select(drv_no,EXI_DEVICE_0,_ioCardFreq[drv_no])==0) {
if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -439,7 +440,7 @@ static s32 __card_stopreadresponse(s32 drv_no,void *buf,s32 len)

__exi_wait(drv_no);

if(EXI_Select(drv_no,EXI_DEVICE_0,_ioCardFreq[drv_no])==0) {
if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -541,7 +542,7 @@ static s32 __card_datares(s32 drv_no,void *buf)

__exi_wait(drv_no);

if(EXI_Select(drv_no,EXI_DEVICE_0,_ioCardFreq[drv_no])==0) {
if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -657,7 +658,7 @@ static s32 __card_dataread(s32 drv_no,void *buf,u32 len)

__exi_wait(drv_no);

if(EXI_Select(drv_no,EXI_DEVICE_0,_ioCardFreq[drv_no])==0) {
if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -747,7 +748,7 @@ static s32 __card_datawrite(s32 drv_no,void *buf,u32 len)

__exi_wait(drv_no);

if(EXI_Select(drv_no,EXI_DEVICE_0,_ioCardFreq[drv_no])==0) {
if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -791,7 +792,7 @@ static s32 __card_multidatawrite(s32 drv_no,void *buf,u32 len)

__exi_wait(drv_no);

if(EXI_Select(drv_no,EXI_DEVICE_0,_ioCardFreq[drv_no])==0) {
if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -832,7 +833,7 @@ static s32 __card_multiwritestop(s32 drv_no)

__exi_wait(drv_no);

if(EXI_Select(drv_no,EXI_DEVICE_0,_ioCardFreq[drv_no])==0) {
if(EXI_Select(drv_no,_ioCardSelect[drv_no],_ioCardFreq[drv_no])==0) {
EXI_Unlock(drv_no);
return CARDIO_ERROR_NOCARD;
}
Expand Down Expand Up @@ -1180,6 +1181,11 @@ static bool __card_check(s32 drv_no)
#ifdef _CARDIO_DEBUG
printf("__card_check(%d)\n",drv_no);
#endif
if(_ioCardSelect[drv_no]!=EXI_DEVICE_0) {
if(EXI_GetID(drv_no,_ioCardSelect[drv_no],&id)==0) return FALSE;
if(id!=0xffffffff) return FALSE;
return TRUE;
}
while((ret=EXI_ProbeEx(drv_no))==0);
if(ret!=1) return FALSE;
if(EXI_GetID(drv_no,EXI_DEVICE_0,&id)==0) return FALSE;
Expand Down Expand Up @@ -1235,6 +1241,7 @@ void sdgecko_initIODefault(void)
_ioFlag[i] = NOT_INITIALIZED;
_ioAddressingType[i] = CARDIO_ADDRESSING_BYTE;
_initType[i] = TYPE_SD;
_ioCardSelect[i] = EXI_DEVICE_0;
_ioCardFreq[i] = EXI_SPEED16MHZ;
LWP_InitQueue(&_ioEXILock[i]);
}
Expand Down Expand Up @@ -1292,7 +1299,7 @@ s32 sdgecko_preIO(s32 drv_no)
{
s32 ret;

if(_ioFlag[drv_no]!=INITIALIZED) {
if(_ioFlag[drv_no]==NOT_INITIALIZED) {
ret = sdgecko_initIO(drv_no);
if(ret!=CARDIO_ERROR_READY) {
#ifdef _CARDIO_DEBUG
Expand Down Expand Up @@ -1535,6 +1542,17 @@ void sdgecko_ejectedCB(s32 drv_no)
pfCallbackOUT[drv_no](drv_no);
}

u32 sdgecko_getDevice(s32 drv_no)
{
return _ioCardSelect[drv_no];
}

void sdgecko_setDevice(s32 drv_no, u32 dev)
{
if(_ioFlag[drv_no]==NOT_INITIALIZED)
_ioCardSelect[drv_no] = dev;
}

u32 sdgecko_getSpeed(s32 drv_no)
{
return _ioCardFreq[drv_no];
Expand Down
2 changes: 1 addition & 1 deletion lwip/netif/enc28j60if.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ static bool ENC28J60_Init(s32 chan, s32 dev, struct enc28j60if *enc28j60if)
while (!EXI_ProbeEx(chan));
if (!EXI_Attach(chan, ExtHandler))
return false;
} else if (dev == EXI_DEVICE_0 && sdgecko_isInitialized(chan))
} else if (dev == sdgecko_getDevice(chan) && sdgecko_isInitialized(chan))
return false;

u32 level = IRQ_Disable();
Expand Down

0 comments on commit 935bd73

Please sign in to comment.