static void inittarget(enum tmio_dev_id target) { uint32_t status; if(waitDataend) { tmio_write32(REG_SDIRMASK, ~(TMIO_STAT_DATAEND | TMIO_MASK_GW)); do { tmio_wfi(); tmio_write32(REG_SDSTATUS, ~TMIO_STAT_DATAEND); status = tmio_read32(REG_SDSTATUS); if((status & TMIO_MASK_GW)) break; } while ((status & TMIO_STAT_CMD_BUSY)); waitDataend = 0; } tmio_mask16(REG_SDPORTSEL,0x3,target); setckl(tmio_dev[target].clk); if(tmio_dev[target].SDOPT == 0) { tmio_mask16(REG_SDOPT,0,0x8000); } else { tmio_mask16(REG_SDOPT,0x8000,0); } }
static void __attribute__((noinline)) inittarget(struct mmcdevice *ctx) { sdmmc_mask16(REG_SDPORTSEL,0x3,(u16)ctx->devicenumber); setckl(ctx->clk); if (ctx->SDOPT == 0) { sdmmc_mask16(REG_SDOPT, 0, 0x8000); } else { sdmmc_mask16(REG_SDOPT, 0x8000, 0); } }
static void inittarget(struct mmcdevice *ctx) { sdmmc_mask16(REG_SDPORTSEL,0x3,(uint16_t)ctx->devicenumber); setckl(ctx->clk); if(ctx->SDOPT == 0) { sdmmc_mask16(REG_SDOPT,0,0x8000); } else { sdmmc_mask16(REG_SDOPT,0x8000,0); } }
int SD_Init() { inittarget(&handelSD); //waitcycles(0x3E8); waitcycles(0xF000); DEBUGPRINT(topScreen, "0x00000 ", handelSD.error, 10, 20 + 14*8, RGB(40, 40, 40), RGB(208, 208, 208)); 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 { do { sdmmc_send_command(&handelSD,0x10437,handelSD.initarg << 0x10); 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; 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; 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,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)); 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)); 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)); sdmmc_send_command(&handelSD,0x10410,0x200); if((handelSD.error & 0x4)) return -1; handelSD.clk |= 0x200; return 0; }
int Nand_Init() { 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; inittarget(&handelSD); return 0; }
uint32_t tmio_init_sdmc() { uint32_t resp; uint32_t r; if (tmio_dev[TMIO_DEV_SDMC].total_size > 0) return 0; //SD tmio_dev[TMIO_DEV_SDMC].isSDHC = 0; tmio_dev[TMIO_DEV_SDMC].SDOPT = 0; tmio_dev[TMIO_DEV_SDMC].res = 0; tmio_dev[TMIO_DEV_SDMC].initarg = 0; tmio_dev[TMIO_DEV_SDMC].clk = 0x80; inittarget(TMIO_DEV_SDMC); waitcycles(0xF000); tmio_send_command(MMC_IDLE,0,0); r = tmio_send_command(SD_SEND_IF_COND | TMIO_CMD_RESP_R1,0x1AA,1); if(r) return r; uint32_t temp = tmio_wait_respend() ? 0 : 0x1 << 0x1E; uint32_t temp2 = 0; do { while(1) { tmio_send_command(MMC_APP_CMD | TMIO_CMD_RESP_R1, tmio_dev[TMIO_DEV_SDMC].initarg << 0x10,0); temp2 = 1; if(tmio_send_command(SD_APP_OP_COND | TMIO_CMD_APP | TMIO_CMD_RESP_R3, 0x00FF8000 | temp,1)) continue; if(!tmio_wait_respend()) break; } resp = tmio_read32(REG_SDRESP0); } while((resp & 0x80000000) == 0); if(!((resp >> 30) & 1) || !temp) temp2 = 0; tmio_dev[TMIO_DEV_SDMC].isSDHC = temp2; tmio_send_command(MMC_ALL_SEND_CID | TMIO_CMD_RESP_R2,0,0); r = tmio_send_command(MMC_SET_RELATIVE_ADDR | TMIO_CMD_RESP_R1,0,1); if(r) return r; r = tmio_wait_respend(); if(r) return r; tmio_dev[TMIO_DEV_SDMC].initarg = tmio_read32(REG_SDRESP0) >> 0x10; tmio_send_command(MMC_SEND_CSD | TMIO_CMD_RESP_R2, tmio_dev[TMIO_DEV_SDMC].initarg << 0x10,0); r = tmio_wait_respend(); if(r) return r; tmio_dev[TMIO_DEV_SDMC].total_size = calcSDSize((uint8_t*)TMIO_BASE + REG_SDRESP0,-1); tmio_dev[TMIO_DEV_SDMC].clk = 1; setckl(1); tmio_send_command(MMC_SELECT_CARD | TMIO_CMD_RESP_R1B, tmio_dev[TMIO_DEV_SDMC].initarg << 0x10,0); r = tmio_send_command(MMC_APP_CMD | TMIO_CMD_RESP_R1, tmio_dev[TMIO_DEV_SDMC].initarg << 0x10,1); if(r) return r; tmio_dev[TMIO_DEV_SDMC].SDOPT = 1; r = tmio_send_command(MMC_SWITCH | TMIO_CMD_APP | TMIO_CMD_RESP_R1, 0x2,1); if(r) return r; r = tmio_send_command(MMC_SEND_STATUS | TMIO_CMD_RESP_R1, tmio_dev[TMIO_DEV_SDMC].initarg << 0x10,1); if(r) return r; r = tmio_send_command(MMC_SET_BLOCKLEN | TMIO_CMD_RESP_R1,TMIO_BBS,1); if(r) return r; tmio_dev[TMIO_DEV_SDMC].clk |= 0x200; return 0; }
uint32_t tmio_init_nand() { uint32_t r; if (tmio_dev[TMIO_DEV_NAND].total_size > 0) return 0; //NAND tmio_dev[TMIO_DEV_NAND].isSDHC = 0; tmio_dev[TMIO_DEV_NAND].SDOPT = 0; tmio_dev[TMIO_DEV_NAND].res = 0; tmio_dev[TMIO_DEV_NAND].initarg = 1; tmio_dev[TMIO_DEV_NAND].clk = 0x80; inittarget(TMIO_DEV_NAND); waitcycles(0xF000); tmio_send_command(MMC_IDLE,0,0); do { do tmio_send_command(MMC_SEND_OP_COND | TMIO_CMD_RESP_R3, 0x100000,0); while (tmio_wait_respend()); } while((tmio_read32(REG_SDRESP0) & 0x80000000) == 0); tmio_send_command(MMC_ALL_SEND_CID | TMIO_CMD_RESP_R2,0x0,0); r = tmio_send_command(MMC_SET_RELATIVE_ADDR | TMIO_CMD_RESP_R1, tmio_dev[TMIO_DEV_NAND].initarg << 0x10,1); if(r) return r; r = tmio_send_command(MMC_SEND_CSD | TMIO_CMD_RESP_R2, tmio_dev[TMIO_DEV_NAND].initarg << 0x10,1); if(r) return r; r = tmio_wait_respend(); if(r) return r; tmio_dev[TMIO_DEV_NAND].total_size = calcSDSize((uint8_t*)TMIO_BASE + REG_SDRESP0,0); tmio_dev[TMIO_DEV_NAND].clk = 1; setckl(1); tmio_send_command(MMC_SELECT_CARD | TMIO_CMD_RESP_R1, tmio_dev[TMIO_DEV_NAND].initarg << 0x10,0); tmio_dev[TMIO_DEV_NAND].SDOPT = 1; r = tmio_send_command(MMC_SWITCH | TMIO_CMD_RESP_R1B,0x3B70100,1); if(r) return r; r = tmio_send_command(MMC_SWITCH | TMIO_CMD_RESP_R1B,0x3B90100,1); if(r) return r; r = tmio_send_command(MMC_SEND_STATUS | TMIO_CMD_RESP_R1, tmio_dev[TMIO_DEV_NAND].initarg << 0x10,1); if(r) return r; r = tmio_send_command(MMC_SET_BLOCKLEN | TMIO_CMD_RESP_R1,TMIO_BBS,1); if(r) return r; tmio_dev[TMIO_DEV_NAND].clk |= 0x200; inittarget(TMIO_DEV_SDMC); return 0; }