Skip to content

Commit

Permalink
Add WIP NDMA code
Browse files Browse the repository at this point in the history
  • Loading branch information
shutterbug2000 committed Aug 26, 2017
1 parent 5522e1f commit ee3d52c
Showing 1 changed file with 154 additions and 8 deletions.
162 changes: 154 additions & 8 deletions cardengine_arm7/source/sdmmc.c
Expand Up @@ -163,6 +163,151 @@ void __attribute__((noinline)) sdmmc_send_command(struct mmcdevice *ctx, uint32_
}
}

//---------------------------------------------------------------------------------
void __attribute__((noinline)) sdmmc_send_command_ndma(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
//---------------------------------------------------------------------------------

*(u32*)(0x4004104) = 0x0400490C;
*(u32*)(0x4004108) = ctx->data;

*(u32*)(0x400410C) = ctx->size;

*(u32*)(0x4004110) = 0x80;

*(u32*)(0x4004114) = 0x1;

*(u32*)(0x400411C) = 0xC8004000;


int i;
bool getSDRESP = (cmd << 15) >> 31;
uint16_t flags = (cmd << 15) >> 31;
const bool readdata = cmd & 0x20000;
const bool writedata = cmd & 0x40000;

if(readdata || writedata)
{
flags |= TMIO_STAT0_DATAEND;
}

ctx->error = 0;
while((sdmmc_read16(REG_SDSTATUS1) & TMIO_STAT1_CMD_BUSY)); //mmc working?
sdmmc_write16(REG_SDIRMASK0,0);
sdmmc_write16(REG_SDIRMASK1,0);
sdmmc_write16(REG_SDSTATUS0,0);
sdmmc_write16(REG_SDSTATUS1,0);
#ifdef DATA32_SUPPORT
// if(readdata)sdmmc_mask16(REG_DATACTL32, 0x1000, 0x800);
// if(writedata)sdmmc_mask16(REG_DATACTL32, 0x800, 0x1000);
// sdmmc_mask16(REG_DATACTL32,0x1800,2);
#else
sdmmc_mask16(REG_SDDATACTL32,0x1800,0);
#endif
sdmmc_write16(REG_SDCMDARG0,args &0xFFFF);
sdmmc_write16(REG_SDCMDARG1,args >> 16);
sdmmc_write16(REG_SDCMD,cmd &0xFFFF);

uint32_t size = ctx->size;
uint16_t *dataPtr = (uint16_t*)ctx->data;
uint32_t *dataPtr32 = (uint32_t*)ctx->data;

bool useBuf = ( 0 != dataPtr );
bool useBuf32 = (useBuf && (0 == (3 & ((uint32_t)dataPtr))));

uint16_t status0 = 0;

while(1) {
volatile uint16_t status1 = sdmmc_read16(REG_SDSTATUS1);
#ifdef DATA32_SUPPORT
volatile uint16_t ctl32 = sdmmc_read16(REG_SDDATACTL32);
if((ctl32 & 0x100))
#else
if((status1 & TMIO_STAT1_RXRDY))
#endif
{
if(readdata) {
if(useBuf) {
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0);
if(size > 0x1FF) {
#ifdef DATA32_SUPPORT
if(useBuf32) {
//for(i = 0; i<0x200; i+=4) {
// *dataPtr32++ = sdmmc_read32(REG_SDFIFO32);
//}
} else {
#endif
//for(i = 0; i<0x200; i+=2) {
// *dataPtr++ = sdmmc_read16(REG_SDFIFO);
//}
#ifdef DATA32_SUPPORT
}
#endif
size -= 0x200;
}
}

sdmmc_mask16(REG_SDDATACTL32, 0x800, 0);
}
}
#ifdef DATA32_SUPPORT
if(!(ctl32 & 0x200))
#else
if((status1 & TMIO_STAT1_TXRQ))
#endif
{
if(writedata) {
if(useBuf) {
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0);
//sdmmc_write16(REG_SDSTATUS1,~TMIO_STAT1_TXRQ);
if(size > 0x1FF) {
#ifdef DATA32_SUPPORT
for(i = 0; i<0x200; i+=4) {
sdmmc_write32(REG_SDFIFO32,*dataPtr32++);
}
#else
for(i = 0; i<0x200; i+=2) {
sdmmc_write16(REG_SDFIFO,*dataPtr++);
}
#endif
size -= 0x200;
}
}

sdmmc_mask16(REG_SDDATACTL32, 0x1000, 0);
}
}
if(status1 & TMIO_MASK_GW) {
ctx->error |= 4;
break;
}

if(!(status1 & TMIO_STAT1_CMD_BUSY)) {
status0 = sdmmc_read16(REG_SDSTATUS0);
if(sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND) {
ctx->error |= 0x1;
}
if(status0 & TMIO_STAT0_DATAEND) {
ctx->error |= 0x2;
}

if((status0 & flags) == flags){
break;
}
}
}
ctx->stat0 = sdmmc_read16(REG_SDSTATUS0);
ctx->stat1 = sdmmc_read16(REG_SDSTATUS1);
sdmmc_write16(REG_SDSTATUS0,0);
sdmmc_write16(REG_SDSTATUS1,0);

if(getSDRESP != 0) {
ctx->ret[0] = sdmmc_read16(REG_SDRESP0) | (sdmmc_read16(REG_SDRESP1) << 16);
ctx->ret[1] = sdmmc_read16(REG_SDRESP2) | (sdmmc_read16(REG_SDRESP3) << 16);
ctx->ret[2] = sdmmc_read16(REG_SDRESP4) | (sdmmc_read16(REG_SDRESP5) << 16);
ctx->ret[3] = sdmmc_read16(REG_SDRESP6) | (sdmmc_read16(REG_SDRESP7) << 16);
}
*(u32*)(0x400411C) = 0x48004000;
}

//---------------------------------------------------------------------------------
int sdmmc_cardinserted() {
Expand Down Expand Up @@ -320,7 +465,8 @@ int __attribute__((noinline)) sdmmc_sdcard_readsectors(u32 sector_no, u32 numsec
sdmmc_write16(REG_SDBLKCOUNT,numsectors);
deviceSD.data = out;
deviceSD.size = numsectors << 9;
sdmmc_send_command(&deviceSD,0x33C12,sector_no);
*(u32*)(0x2000000) = sector_no;
sdmmc_send_command_ndma(&deviceSD,0x33C12,sector_no);
return geterror(&deviceSD);
}

Expand All @@ -347,11 +493,11 @@ startUp
Initialize the interface, geting it into an idle, ready state
returns true if successful, otherwise returns false
-----------------------------------------------------------------*/
bool startup(void) {
bool startup(void) {
#ifdef DEBUG
nocashMessage("startup internal");
#endif
return true;
return true;
}

/*-----------------------------------------------------------------
Expand All @@ -375,7 +521,7 @@ return true if the card is idle and ready
bool clearStatus (void) {
#ifdef DEBUG
nocashMessage("clearStatus internal");
#endif
#endif
return true;
}

Expand All @@ -390,7 +536,7 @@ return true if it was successful, false if it failed for any reason
bool readSectors (u32 sector, u32 numSectors, void* buffer) {
#ifdef DEBUG
nocashMessage("readSectors internal");
#endif
#endif
//dbg_printf("readSectors internal");
return sdmmc_sdcard_readsectors(sector,numSectors,buffer)==0;
}
Expand All @@ -407,7 +553,7 @@ return true if it was successful, false if it failed for any reason
bool writeSectors (u32 sector, u32 numSectors, void* buffer) {
#ifdef DEBUG
nocashMessage("writeSectors internal");
#endif
#endif
//dbg_printf("writeSectors internal");
return sdmmc_sdcard_writesectors(sector,numSectors,buffer)==0;
}
Expand All @@ -421,9 +567,9 @@ it is merely for disabling the card.
return true if the card is no longer active
-----------------------------------------------------------------*/
bool shutdown(void) {
#ifdef DEBUG
#ifdef DEBUG
nocashMessage("shutdown internal");
#endif
#endif
return true;
}

Expand Down

0 comments on commit ee3d52c

Please sign in to comment.