Updated sdmmc.c / sdmmc.h

Thanks to @Gemarcano
This commit is contained in:
d0k3 2016-07-12 18:11:05 +02:00
parent 93153f010e
commit c14b49f341
2 changed files with 145 additions and 272 deletions

View File

@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2014, Normmatt
* Copyright (c) 2014-2015, Normmatt
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU General Public License Version 2, as described below:
@ -35,19 +35,13 @@
#include "sdmmc.h"
//#include "DrawCharacter.h"
//Uncomment to enable 32bit fifo support?
//not currently working
//#define DATA32_SUPPORT
#define DATA32_SUPPORT
#define TRUE 1
#define FALSE 0
#define bool int
#define NO_INLINE __attribute__ ((noinline))
#define RGB(r,g,b) (r<<24|b<<16|g<<8|r)
#ifdef __cplusplus
extern "C" {
#endif
@ -56,23 +50,6 @@ extern "C" {
};
#endif
//#define DEBUG_SDMMC
#ifdef DEBUG_SDMMC
extern uint8_t* topScreen;
extern void DrawHexWithName(unsigned char *screen, const char *str, unsigned int hex, int x, int y, int color, int bgcolor);
#define DEBUGPRINT(scr,str,hex,x,y,color,bg) DrawHexWithName(scr,str,hex,x,y,color,bg)
#else
#define DEBUGPRINT(...)
#endif
//extern "C" void sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args);
//extern "C" void inittarget(struct mmcdevice *ctx);
//extern "C" int SD_Init();
//extern "C" int SD_Init2();
//extern "C" int Nand_Init2();
//extern "C" void InitSD();
struct mmcdevice handelNAND;
struct mmcdevice handelSD;
@ -82,13 +59,13 @@ mmcdevice *getMMCDevice(int drive)
return &handelSD;
}
int geterror(struct mmcdevice *ctx)
static int geterror(struct mmcdevice *ctx)
{
return (ctx->error << 29) >> 31;
return (int)((ctx->error << 29) >> 31);
}
void inittarget(struct mmcdevice *ctx)
static void inittarget(struct mmcdevice *ctx)
{
sdmmc_mask16(REG_SDPORTSEL,0x3,(uint16_t)ctx->devicenumber);
setckl(ctx->clk);
@ -100,50 +77,39 @@ void inittarget(struct mmcdevice *ctx)
{
sdmmc_mask16(REG_SDOPT,0x8000,0);
}
}
void NO_INLINE sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args)
static void NO_INLINE sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args)
{
bool getSDRESP = (cmd << 15) >> 31;
uint32_t getSDRESP = (cmd << 15) >> 31;
uint16_t flags = (cmd << 15) >> 31;
const bool readdata = cmd & 0x20000;
const bool writedata = cmd & 0x40000;
const int readdata = cmd & 0x20000;
const int 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_DATACTL32,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;
#ifdef DATA32_SUPPORT
uint32_t *dataPtr32 = (uint32_t*)ctx->data;
#endif
bool useBuf = ( NULL != dataPtr );
#ifdef DATA32_SUPPORT
bool useBuf32 = (useBuf && (0 == (3 & ((uint32_t)dataPtr))));
#endif
uint8_t *rDataPtr = ctx->rData;
const uint8_t *tDataPtr = ctx->tData;
int rUseBuf = ( NULL != rDataPtr );
int tUseBuf = ( NULL != tDataPtr );
uint16_t status0 = 0;
while(1)
{
@ -157,34 +123,34 @@ void NO_INLINE sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t
{
if(readdata)
{
if(useBuf)
if(rUseBuf)
{
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0);
//sdmmc_write16(REG_SDSTATUS1,~TMIO_STAT1_RXRDY);
if(size > 0x1FF)
{
#ifdef DATA32_SUPPORT
if(useBuf32)
//Gabriel Marcano: This implementation doesn't assume alignment.
//I've removed the alignment check doen with former rUseBuf32 as a result
for(int i = 0; i<0x200; i+=4)
{
for(int i = 0; i<0x200; i+=4)
{
*dataPtr32++ = sdmmc_read32(REG_SDFIFO32);
}
uint32_t data = sdmmc_read32(REG_SDFIFO32);
*rDataPtr++ = data;
*rDataPtr++ = data >> 8;
*rDataPtr++ = data >> 16;
*rDataPtr++ = data >> 24;
}
else
#else
for(int i = 0; i<0x200; i+=2)
{
#endif
for(int i = 0; i<0x200; i+=2)
{
*dataPtr++ = sdmmc_read16(REG_SDFIFO);
}
#ifdef DATA32_SUPPORT
uint16_t data = sdmmc_read16(REG_SDFIFO);
*rDataPtr++ = data;
*rDataPtr++ = data >> 8;
}
#endif
size -= 0x200;
}
}
sdmmc_mask16(REG_DATACTL32, 0x800, 0);
}
}
@ -196,27 +162,32 @@ void NO_INLINE sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t
{
if(writedata)
{
if(useBuf)
if(tUseBuf)
{
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0);
//sdmmc_write16(REG_SDSTATUS1,~TMIO_STAT1_TXRQ);
if(size > 0x1FF)
{
#ifdef DATA32_SUPPORT
for(int i = 0; i<0x200; i+=4)
{
sdmmc_write32(REG_SDFIFO32,*dataPtr32++);
uint32_t data = *tDataPtr++;
data |= (uint32_t)*tDataPtr++ << 8;
data |= (uint32_t)*tDataPtr++ << 16;
data |= (uint32_t)*tDataPtr++ << 24;
sdmmc_write32(REG_SDFIFO32, data);
}
#else
for(int i = 0; i<0x200; i+=2)
{
sdmmc_write16(REG_SDFIFO,*dataPtr++);
uint16_t data = *tDataPtr++;
data |= (uint8_t)(*tDataPtr++ << 8);
sdmmc_write16(REG_SDFIFO, data);
}
#endif
size -= 0x200;
}
}
sdmmc_mask16(REG_DATACTL32, 0x1000, 0);
}
}
@ -225,7 +196,7 @@ void NO_INLINE sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t
ctx->error |= 4;
break;
}
if(!(status1 & TMIO_STAT1_CMD_BUSY))
{
status0 = sdmmc_read16(REG_SDSTATUS0);
@ -237,7 +208,7 @@ void NO_INLINE sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t
{
ctx->error |= 0x2;
}
if((status0 & flags) == flags)
break;
}
@ -246,17 +217,17 @@ void NO_INLINE sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t
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);
ctx->ret[0] = (uint32_t)(sdmmc_read16(REG_SDRESP0) | (sdmmc_read16(REG_SDRESP1) << 16));
ctx->ret[1] = (uint32_t)(sdmmc_read16(REG_SDRESP2) | (sdmmc_read16(REG_SDRESP3) << 16));
ctx->ret[2] = (uint32_t)(sdmmc_read16(REG_SDRESP4) | (sdmmc_read16(REG_SDRESP5) << 16));
ctx->ret[3] = (uint32_t)(sdmmc_read16(REG_SDRESP6) | (sdmmc_read16(REG_SDRESP7) << 16));
}
}
int NO_INLINE sdmmc_sdcard_writesectors(uint32_t sector_no, uint32_t numsectors, uint8_t *in)
int NO_INLINE sdmmc_sdcard_writesectors(uint32_t sector_no, uint32_t numsectors, const uint8_t *in)
{
if(handelSD.isSDHC == 0) sector_no <<= 9;
inittarget(&handelSD);
@ -266,7 +237,7 @@ int NO_INLINE sdmmc_sdcard_writesectors(uint32_t sector_no, uint32_t numsectors,
sdmmc_write16(REG_SDBLKLEN32,0x200);
#endif
sdmmc_write16(REG_SDBLKCOUNT,numsectors);
handelSD.data = in;
handelSD.tData = in;
handelSD.size = numsectors << 9;
sdmmc_send_command(&handelSD,0x52C19,sector_no);
return geterror(&handelSD);
@ -282,7 +253,7 @@ int NO_INLINE sdmmc_sdcard_readsectors(uint32_t sector_no, uint32_t numsectors,
sdmmc_write16(REG_SDBLKLEN32,0x200);
#endif
sdmmc_write16(REG_SDBLKCOUNT,numsectors);
handelSD.data = out;
handelSD.rData = out;
handelSD.size = numsectors << 9;
sdmmc_send_command(&handelSD,0x33C12,sector_no);
return geterror(&handelSD);
@ -300,14 +271,14 @@ int NO_INLINE sdmmc_nand_readsectors(uint32_t sector_no, uint32_t numsectors, ui
sdmmc_write16(REG_SDBLKLEN32,0x200);
#endif
sdmmc_write16(REG_SDBLKCOUNT,numsectors);
handelNAND.data = out;
handelNAND.rData = out;
handelNAND.size = numsectors << 9;
sdmmc_send_command(&handelNAND,0x33C12,sector_no);
inittarget(&handelSD);
return geterror(&handelNAND);
}
int NO_INLINE sdmmc_nand_writesectors(uint32_t sector_no, uint32_t numsectors, uint8_t *in) //experimental
int NO_INLINE sdmmc_nand_writesectors(uint32_t sector_no, uint32_t numsectors, const uint8_t *in) //experimental
{
if(handelNAND.isSDHC == 0) sector_no <<= 9;
inittarget(&handelNAND);
@ -317,7 +288,7 @@ int NO_INLINE sdmmc_nand_writesectors(uint32_t sector_no, uint32_t numsectors, u
sdmmc_write16(REG_SDBLKLEN32,0x200);
#endif
sdmmc_write16(REG_SDBLKCOUNT,numsectors);
handelNAND.data = in;
handelNAND.tData = in;
handelNAND.size = numsectors << 9;
sdmmc_send_command(&handelNAND,0x52C19,sector_no);
inittarget(&handelSD);
@ -326,16 +297,16 @@ int NO_INLINE sdmmc_nand_writesectors(uint32_t sector_no, uint32_t numsectors, u
static uint32_t calcSDSize(uint8_t* csd, int type)
{
uint32_t result=0;
uint32_t result = 0;
if(type == -1) type = csd[14] >> 6;
switch(type)
{
case 0:
{
uint32_t block_len=csd[9]&0xf;
block_len=1<<block_len;
uint32_t mult=(csd[4]>>7)|((csd[5]&3)<<1);
mult=1<<(mult+2);
block_len=1u<<block_len;
uint32_t mult=( uint32_t)((csd[4]>>7)|((csd[5]&3)<<1));
mult=1u<<(mult+2);
result=csd[8]&3;
result=(result<<8)|csd[7];
result=(result<<2)|(csd[6]>>6);
@ -348,28 +319,14 @@ static uint32_t calcSDSize(uint8_t* csd, int type)
result=(result<<8)|csd[5];
result=(result+1)*1024;
break;
default:
break; //Do nothing otherwise FIXME perhaps return some error?
}
return result;
}
void InitSD()
{
//NAND
handelNAND.isSDHC = 0;
handelNAND.SDOPT = 0;
handelNAND.res = 0;
handelNAND.initarg = 1;
handelNAND.clk = 0x80;
handelNAND.devicenumber = 1;
//SD
handelSD.isSDHC = 0;
handelSD.SDOPT = 0;
handelSD.res = 0;
handelSD.initarg = 0;
handelSD.clk = 0x80;
handelSD.devicenumber = 0;
//sdmmc_mask16(0x100,0x800,0);
//sdmmc_mask16(0x100,0x1000,0);
//sdmmc_mask16(0x100,0x0,0x402);
@ -392,7 +349,7 @@ void InitSD()
//sdmmc_mask16(0x02,0x3,0);
//sdmmc_write16(REG_SDBLKLEN,0x200);
//sdmmc_write16(REG_SDSTOP,0);
*(volatile uint16_t*)0x10006100 &= 0xF7FFu; //SDDATACTL32
*(volatile uint16_t*)0x10006100 &= 0xEFFFu; //SDDATACTL32
#ifdef DATA32_SUPPORT
@ -428,107 +385,90 @@ void InitSD()
*(volatile uint16_t*)0x10006002 &= 0xFFFCu; ////SDPORTSEL
*(volatile uint16_t*)0x10006026 = 512; //SDBLKLEN
*(volatile uint16_t*)0x10006008 = 0; //SDSTOP
inittarget(&handelSD);
}
int Nand_Init()
{
//NAND
handelNAND.isSDHC = 0;
handelNAND.SDOPT = 0;
handelNAND.res = 0;
handelNAND.initarg = 1;
handelNAND.clk = 0x80;
handelNAND.devicenumber = 1;
inittarget(&handelNAND);
waitcycles(0xF000);
DEBUGPRINT(topScreen, "0x00000 ", handelNAND.error, 10, 20 + 13*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelNAND,0,0);
DEBUGPRINT(topScreen, "0x10701 ", handelNAND.error, 10, 20 + 13*8, RGB(40, 40, 40), RGB(208, 208, 208));
do
{
do
{
sdmmc_send_command(&handelNAND,0x10701,0x100000);
DEBUGPRINT(topScreen, "error ", handelNAND.error, 10, 20 + 17*8, RGB(40, 40, 40), RGB(208, 208, 208));
DEBUGPRINT(topScreen, "ret: ", handelNAND.ret[0], 10, 20 + 18*8, RGB(40, 40, 40), RGB(208, 208, 208));
DEBUGPRINT(topScreen, "test ", 3, 10, 20 + 19*8, RGB(40, 40, 40), RGB(208, 208, 208));
} while ( !(handelNAND.error & 1) );
}
while((handelNAND.ret[0] & 0x80000000) == 0);
DEBUGPRINT(topScreen, "0x10602 ", handelNAND.error, 10, 20 + 13*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelNAND,0x10602,0x0);
if((handelNAND.error & 0x4))return -1;
DEBUGPRINT(topScreen, "0x10403 ", handelNAND.error, 10, 20 + 13*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelNAND,0x10403,handelNAND.initarg << 0x10);
if((handelNAND.error & 0x4))return -1;
DEBUGPRINT(topScreen, "0x10609 ", handelNAND.error, 10, 20 + 13*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelNAND,0x10609,handelNAND.initarg << 0x10);
if((handelNAND.error & 0x4))return -1;
DEBUGPRINT(topScreen, "0x10407 ", handelNAND.error, 10, 20 + 13*8, RGB(40, 40, 40), RGB(208, 208, 208));
handelNAND.total_size = calcSDSize((uint8_t*)&handelNAND.ret[0],0);
handelNAND.clk = 1;
setckl(1);
sdmmc_send_command(&handelNAND,0x10407,handelNAND.initarg << 0x10);
if((handelNAND.error & 0x4))return -1;
DEBUGPRINT(topScreen, "0x10506 ", handelNAND.error, 10, 20 + 13*8, RGB(40, 40, 40), RGB(208, 208, 208));
handelNAND.SDOPT = 1;
sdmmc_send_command(&handelNAND,0x10506,0x3B70100);
if((handelNAND.error & 0x4))return -1;
DEBUGPRINT(topScreen, "0x10506 ", handelNAND.error, 10, 20 + 13*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelNAND,0x10506,0x3B90100);
if((handelNAND.error & 0x4))return -1;
DEBUGPRINT(topScreen, "0x1040D ", handelNAND.error, 10, 20 + 13*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelNAND,0x1040D,handelNAND.initarg << 0x10);
if((handelNAND.error & 0x4))return -1;
DEBUGPRINT(topScreen, "0x10410 ", handelNAND.error, 10, 20 + 13*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelNAND,0x10410,0x200);
if((handelNAND.error & 0x4))return -1;
handelNAND.clk |= 0x200;
handelNAND.clk |= 0x200;
inittarget(&handelSD);
return 0;
}
int SD_Init()
{
//SD
handelSD.isSDHC = 0;
handelSD.SDOPT = 0;
handelSD.res = 0;
handelSD.initarg = 0;
handelSD.clk = 0x80;
handelSD.devicenumber = 0;
inittarget(&handelSD);
//waitcycles(0x3E8);
//waitcycles(0xF000);
waitcycles(1u << 19); //Card needs a little bit of time to be detected, it seems
if (!(*((volatile uint16_t*)0x1000601c) & TMIO_STAT0_SIGSTATE)) return -1; // check if card inserted
DEBUGPRINT(topScreen, "0x00000 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
waitcycles(1u << 22); //Card needs a little bit of time to be detected, it seems FIXME test again to see what a good number is for the delay
//If not inserted
if (!(*((volatile uint16_t*)(SDMMC_BASE + REG_SDSTATUS0)) & TMIO_STAT0_SIGSTATE)) return 5;
sdmmc_send_command(&handelSD,0,0);
DEBUGPRINT(topScreen, "0x10408 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelSD,0x10408,0x1AA);
//uint32_t temp = (handelSD.ret[0] == 0x1AA) << 0x1E;
uint32_t temp = (handelSD.error & 0x1) << 0x1E;
DEBUGPRINT(topScreen, "0x10769 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
DEBUGPRINT(topScreen, "sd ret: ", handelSD.ret[0], 10, 20 + 15*8, RGB(40, 40, 40), RGB(208, 208, 208));
DEBUGPRINT(topScreen, "temp: ", temp, 10, 20 + 16*8, RGB(40, 40, 40), RGB(208, 208, 208));
//int count = 0;
uint32_t temp2 = 0;
do
{
@ -538,101 +478,56 @@ int SD_Init()
sdmmc_send_command(&handelSD,0x10769,0x00FF8000 | temp);
temp2 = 1;
} while ( !(handelSD.error & 1) );
//DEBUGPRINT(topScreen, "sd error ", handelSD.error, 10, 20 + 17*8, RGB(40, 40, 40), RGB(208, 208, 208));
//DEBUGPRINT(topScreen, "sd ret: ", handelSD.ret[0], 10, 20 + 18*8, RGB(40, 40, 40), RGB(208, 208, 208));
//DEBUGPRINT(topScreen, "count: ", count++, 10, 20 + 19*8, RGB(40, 40, 40), RGB(208, 208, 208));
}
while((handelSD.ret[0] & 0x80000000) == 0);
//do
//{
// sdmmc_send_command(&handelSD,0x10437,handelSD.initarg << 0x10);
// sdmmc_send_command(&handelSD,0x10769,0x00FF8000 | temp);
//
// DEBUGPRINT(topScreen, "sd error ", handelSD.error, 10, 20 + 17*8, RGB(40, 40, 40), RGB(208, 208, 208));
// DEBUGPRINT(topScreen, "sd ret: ", handelSD.ret[0], 10, 20 + 18*8, RGB(40, 40, 40), RGB(208, 208, 208));
// DEBUGPRINT(topScreen, "count: ", count++, 10, 20 + 19*8, RGB(40, 40, 40), RGB(208, 208, 208));
//}
//while(!(handelSD.ret[0] & 0x80000000));
if(!((handelSD.ret[0] >> 30) & 1) || !temp)
temp2 = 0;
handelSD.isSDHC = temp2;
//handelSD.isSDHC = (handelSD.ret[0] & 0x40000000);
DEBUGPRINT(topScreen, "0x10602 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelSD,0x10602,0);
if((handelSD.error & 0x4)) return -1;
DEBUGPRINT(topScreen, "0x10403 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelSD,0x10403,0);
if((handelSD.error & 0x4)) return -1;
if((handelSD.error & 0x4)) return -2;
handelSD.initarg = handelSD.ret[0] >> 0x10;
DEBUGPRINT(topScreen, "0x10609 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelSD,0x10609,handelSD.initarg << 0x10);
if((handelSD.error & 0x4)) return -1;
if((handelSD.error & 0x4)) return -3;
handelSD.total_size = calcSDSize((uint8_t*)&handelSD.ret[0],-1);
handelSD.clk = 1;
setckl(1);
DEBUGPRINT(topScreen, "0x10507 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelSD,0x10507,handelSD.initarg << 0x10);
if((handelSD.error & 0x4)) return -1;
DEBUGPRINT(topScreen, "0x10437 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
sdmmc_send_command(&handelSD,0x10507,handelSD.initarg << 0x10);
if((handelSD.error & 0x4)) return -4;
sdmmc_send_command(&handelSD,0x10437,handelSD.initarg << 0x10);
if((handelSD.error & 0x4)) return -1;
DEBUGPRINT(topScreen, "0x10446 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
if((handelSD.error & 0x4)) return -5;
handelSD.SDOPT = 1;
sdmmc_send_command(&handelSD,0x10446,0x2);
if((handelSD.error & 0x4)) return -1;
DEBUGPRINT(topScreen, "0x1040D ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
if((handelSD.error & 0x4)) return -6;
sdmmc_send_command(&handelSD,0x1040D,handelSD.initarg << 0x10);
if((handelSD.error & 0x4)) return -1;
DEBUGPRINT(topScreen, "0x10410 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208));
if((handelSD.error & 0x4)) return -7;
sdmmc_send_command(&handelSD,0x10410,0x200);
if((handelSD.error & 0x4)) return -1;
if((handelSD.error & 0x4)) return -8;
handelSD.clk |= 0x200;
return 0;
}
int sdmmc_sdcard_init()
{
DEBUGPRINT(topScreen, "sdmmc_sdcard_init ", handelSD.error, 10, 20 + 2*8, RGB(40, 40, 40), RGB(208, 208, 208));
InitSD();
//SD_Init2();
//Nand_Init();
Nand_Init();
DEBUGPRINT(topScreen, "nand_res ", nand_res, 10, 20 + 3*8, RGB(40, 40, 40), RGB(208, 208, 208));
if (SD_Init() != 0) return FALSE;
DEBUGPRINT(topScreen, "sd_res ", sd_res, 10, 20 + 4*8, RGB(40, 40, 40), RGB(208, 208, 208));
return TRUE;
}
int sdmmc_get_cid( int isNand, uint32_t *info)
int sdmmc_get_cid(bool isNand, uint32_t *info)
{
struct mmcdevice *device;
if(isNand)
device = &handelNAND;
else
device = &handelSD;
inittarget(device);
// use cmd7 to put sd card in standby mode
// CMD7
@ -659,10 +554,13 @@ int sdmmc_get_cid( int isNand, uint32_t *info)
//if((device->error & 0x4)) return -3;
}
if(isNand)
{
inittarget(&handelSD);
}
return 0;
}
int sdmmc_sdcard_init()
{
InitSD();
int nand_res = Nand_Init();
int sd_res = SD_Init();
return nand_res | sd_res;
}

View File

@ -1,10 +1,8 @@
#ifndef __SDMMC_H__
#define __SDMMC_H__
#define TRUE 1
#define FALSE 0
#include <stdint.h>
#include <stdbool.h>
#define SDMMC_BASE 0x10006000
@ -30,8 +28,8 @@
#define REG_SDIRMASK0 0x20
#define REG_SDIRMASK1 0x22
#define REG_SDCLKCTL 0x24
#define REG_SDCLKCTL 0x24
#define REG_SDBLKLEN 0x26
#define REG_SDOPT 0x28
#define REG_SDFIFO 0x30
@ -70,30 +68,6 @@
#define TMIO_STAT1_CMD_BUSY 0x4000
#define TMIO_STAT1_ILL_ACCESS 0x8000
//Comes from TWLSDK mongoose.tef DWARF info
#define SDMC_NORMAL 0x00000000
#define SDMC_ERR_COMMAND 0x00000001
#define SDMC_ERR_CRC 0x00000002
#define SDMC_ERR_END 0x00000004
#define SDMC_ERR_TIMEOUT 0x00000008
#define SDMC_ERR_FIFO_OVF 0x00000010
#define SDMC_ERR_FIFO_UDF 0x00000020
#define SDMC_ERR_WP 0x00000040
#define SDMC_ERR_ABORT 0x00000080
#define SDMC_ERR_FPGA_TIMEOUT 0x00000100
#define SDMC_ERR_PARAM 0x00000200
#define SDMC_ERR_R1_STATUS 0x00000800
#define SDMC_ERR_NUM_WR_SECTORS 0x00001000
#define SDMC_ERR_RESET 0x00002000
#define SDMC_ERR_ILA 0x00004000
#define SDMC_ERR_INFO_DETECT 0x00008000
#define SDMC_STAT_ERR_UNKNOWN 0x00080000
#define SDMC_STAT_ERR_CC 0x00100000
#define SDMC_STAT_ERR_ECC_FAILED 0x00200000
#define SDMC_STAT_ERR_CRC 0x00800000
#define SDMC_STAT_ERR_OTHER 0xf9c70008
#define TMIO_MASK_ALL 0x837f031d
#define TMIO_MASK_GW (TMIO_STAT1_ILL_ACCESS | TMIO_STAT1_CMDTIMEOUT | TMIO_STAT1_TXUNDERRUN | TMIO_STAT1_RXOVERFLOW | \
@ -107,7 +81,8 @@ extern "C" {
#endif
typedef struct mmcdevice {
uint8_t* data;
uint8_t* rData;
const uint8_t* tData;
uint32_t size;
uint32_t error;
uint16_t stat0;
@ -121,20 +96,20 @@ extern "C" {
uint32_t total_size; //size in sectors of the device
uint32_t res;
} mmcdevice;
int sdmmc_sdcard_init();
int sdmmc_sdcard_readsector(uint32_t sector_no, uint8_t *out);
int sdmmc_sdcard_readsectors(uint32_t sector_no, uint32_t numsectors, uint8_t *out);
int sdmmc_sdcard_writesector(uint32_t sector_no, uint8_t *in);
int sdmmc_sdcard_writesectors(uint32_t sector_no, uint32_t numsectors, uint8_t *in);
int sdmmc_sdcard_writesector(uint32_t sector_no, const uint8_t *in);
int sdmmc_sdcard_writesectors(uint32_t sector_no, uint32_t numsectors, const uint8_t *in);
int sdmmc_nand_readsectors(uint32_t sector_no, uint32_t numsectors, uint8_t *out);
int sdmmc_nand_writesectors(uint32_t sector_no, uint32_t numsectors, uint8_t *in);
int sdmmc_get_cid( int isNand, uint32_t *info);
int sdmmc_nand_writesectors(uint32_t sector_no, uint32_t numsectors, const uint8_t *in);
int sdmmc_get_cid(bool isNand, uint32_t *info);
mmcdevice *getMMCDevice(int drive);
void InitSD();
int Nand_Init();
int SD_Init();