Skip to content

Commit

Permalink
Fill numberOfSectors for SD Gecko
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed Dec 3, 2024
1 parent d72aa1f commit e38a505
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
7 changes: 5 additions & 2 deletions gc/sdcard/card_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
#define MANUFACTURER_ID(drv_no) ((u8)(g_CID[drv_no][0]))

/* CSD Register */
#define CSD_STRUCTURE(drv_no) ((u8)((g_CSD[drv_no][0]>>6)&0x03))
#define READ_BL_LEN(drv_no) ((u8)(g_CSD[drv_no][5]&0x0f))
#define WRITE_BL_LEN(drv_no) ((u8)((g_CSD[drv_no][12]&0x03)<<2)|((g_CSD[drv_no][13]>>6)&0x03))
#define WRITE_BL_LEN(drv_no) ((u8)(((g_CSD[drv_no][12]&0x03)<<2)|((g_CSD[drv_no][13]>>6)&0x03)))
#define C_SIZE(drv_no) ((u16)(((g_CSD[drv_no][6]&0x03)<<10)|(g_CSD[drv_no][7]<<2)|((g_CSD[drv_no][8]>>6)&0x03)))
#define C_SIZE_MULT(drv_no) ((u8)((g_CSD[drv_no][9]&0x03)<<1)|((g_CSD[drv_no][10]>>7)&0x01))
#define C_SIZE_MULT(drv_no) ((u8)(((g_CSD[drv_no][9]&0x03)<<1)|((g_CSD[drv_no][10]>>7)&0x01)))
#define C_SIZE1(drv_no) ((u32)(((g_CSD[drv_no][7]&0x3f)<<16)|(g_CSD[drv_no][8]<<8)|(g_CSD[drv_no][9])))
#define C_SIZE2(drv_no) ((u32)(((g_CSD[drv_no][6]&0x0f)<<24)|(g_CSD[drv_no][7]<<16)|(g_CSD[drv_no][8]<<8)|(g_CSD[drv_no][9])))

#ifdef __cplusplus
extern "C" {
Expand Down
21 changes: 18 additions & 3 deletions libogc/gcsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ static bool __gcsd_startup(DISC_INTERFACE *disc)
}
}

switch(CSD_STRUCTURE(chan)) {
case 0:
disc->numberOfSectors = ((C_SIZE(chan) + 1) << (C_SIZE_MULT(chan) + 2)) << (READ_BL_LEN(chan) - 9);
break;
case 1:
disc->numberOfSectors = (C_SIZE1(chan) + 1LL) << 10;
break;
case 2:
disc->numberOfSectors = (C_SIZE2(chan) + 1LL) << 10;
break;
default:
disc->numberOfSectors = 0;
break;
}

return true;
}

Expand Down Expand Up @@ -147,7 +162,7 @@ DISC_INTERFACE __io_gcsda = {
__gcsd_writeSectors,
__gcsd_clearStatus,
__gcsd_shutdown,
0x100000000,
0,
512
};

Expand All @@ -160,7 +175,7 @@ DISC_INTERFACE __io_gcsdb = {
__gcsd_writeSectors,
__gcsd_clearStatus,
__gcsd_shutdown,
0x100000000,
0,
512
};

Expand All @@ -173,6 +188,6 @@ DISC_INTERFACE __io_gcsd2 = {
__gcsd_writeSectors,
__gcsd_clearStatus,
__gcsd_shutdown,
0x100000000,
0,
512
};
22 changes: 15 additions & 7 deletions libogc/sdgecko_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ static s32 __card_setblocklen(s32 drv_no,u32 block_len)
static s32 __card_readcsd(s32 drv_no)
{
s32 ret;
u8 crc;

if(drv_no<0 || drv_no>=MAX_DRIVE) return CARDIO_ERROR_NOCARD;
#ifdef _CARDIO_DEBUG
Expand All @@ -1068,12 +1069,17 @@ static s32 __card_readcsd(s32 drv_no)
return ret;
}
if((ret=__card_response1(drv_no))!=0) return ret;
return __card_dataread(drv_no,g_CSD[drv_no],16);
if((ret=__card_dataread(drv_no,g_CSD[drv_no],16))==CARDIO_OP_IOERR_CRC) {
crc = __make_crc7(g_CSD[drv_no],15)|0x01;
ret = (crc!=g_CSD[drv_no][15])?CARDIO_ERROR_FATALERROR:CARDIO_ERROR_READY;
}
return ret;
}

static s32 __card_readcid(s32 drv_no)
{
s32 ret;
u8 crc;

if(drv_no<0 || drv_no>=MAX_DRIVE) return CARDIO_ERROR_NOCARD;
#ifdef _CARDIO_DEBUG
Expand All @@ -1086,7 +1092,11 @@ static s32 __card_readcid(s32 drv_no)
return ret;
}
if((ret=__card_response1(drv_no))!=0) return ret;
return __card_dataread(drv_no,g_CID[drv_no],16);
if((ret=__card_dataread(drv_no,g_CID[drv_no],16))==CARDIO_OP_IOERR_CRC) {
crc = __make_crc7(g_CID[drv_no],15)|0x01;
ret = (crc!=g_CID[drv_no][15])?CARDIO_ERROR_FATALERROR:CARDIO_ERROR_READY;
}
return ret;
}

static s32 __card_sd_status(s32 drv_no)
Expand All @@ -1097,10 +1107,6 @@ static s32 __card_sd_status(s32 drv_no)
#ifdef _CARDIO_DEBUG
printf("__card_sd_status(%d)\n",drv_no);
#endif
if(_ioPageSize[drv_no]!=64) {
_ioPageSize[drv_no] = 64;
if((ret=__card_setblocklen(drv_no,_ioPageSize[drv_no]))!=0) return ret;
}
if((ret=__card_sendappcmd(drv_no))!=0) return ret;
if((ret=__card_sendcmd(drv_no,0x0D,NULL))!=0) return ret;
if((ret=__card_response2(drv_no))!=0) return ret;
Expand Down Expand Up @@ -1262,8 +1268,10 @@ s32 sdgecko_initIO(s32 drv_no)
if(_ioResponse[drv_no][1]&0x40) _ioAddressingType[drv_no] = CARDIO_ADDRESSING_BLOCK;
}

if(__card_readcsd(drv_no)!=0) goto exit;

_ioPageSize[drv_no] = PAGE_SIZE512;
if(__card_setblocklen(drv_no,_ioPageSize[drv_no])!=0) goto exit;
if(__card_setblocklen(drv_no,_ioPageSize[drv_no])!=0) goto exit;

if(__card_sd_status(drv_no)!=0) goto exit;

Expand Down

0 comments on commit e38a505

Please sign in to comment.