static int sd_welcome_card(struct sd_host *host) { int retval; /* soft reset the card */ retval = sd_reset_sequence(host); if (retval < 0 || sd_card_is_bad(host)) goto out; /* read Operating Conditions Register */ retval = sd_read_ocr(host); if (retval < 0) goto err_bad_card; /* refuse to drive cards reporting voltage ranges out of scope */ if (!(host->ocr & host->ocr_avail)) { sd_printk(KERN_WARNING, "reported OCR (%08x)" " indicates that it is not safe to use this" " card with a GameCube\n", host->ocr); retval = -ENODEV; goto err_bad_card; } /* read and decode the Card Specific Data */ retval = sd_read_csd(host); if (retval < 0) goto err_bad_card; mmc_decode_csd(&host->card); /* calculate some card access related timeouts */ sd_calc_timeouts(host); /* read and decode the Card Identification Data */ retval = sd_read_cid(host); if (retval < 0) goto err_bad_card; mmc_decode_cid(&host->card); sd_printk(KERN_INFO, "slot%d: descr \"%s\", size %luk, block %ub," " serial %08x\n", to_channel(exi_get_exi_channel(host->exi_device)), host->card.cid.prod_name, (unsigned long)((host->card.csd.capacity * (1 << host->card.csd.read_blkbits)) / 1024), 1 << host->card.csd.read_blkbits, host->card.cid.serial); retval = 0; goto out; err_bad_card: sd_card_set_bad(host); out: return retval; }
static int sd_reset_sequence(struct sd_host *host) { struct sd_command *cmd = &host->cmd; u8 d; int i; int retval = 0; u32 cmd_ret = 0; host->card.state = 0; /* * Wait at least 80 dummy clock cycles with the card deselected * and with the MOSI line continuously high. */ exi_dev_take(host->exi_device); exi_dev_deselect(host->exi_device); for (i = 0; i < SD_IDLE_CYCLES; i++) { d = 0xff; exi_dev_write(host->exi_device, &d, sizeof(d)); } exi_dev_give(host->exi_device); /* * Send a CMD0, card must ack with "idle state" (0x01). * This puts the card into SPI mode and soft resets it. * CRC checking is disabled by default. */ for (i = 0; i < 255; i++) { /* CMD0 */ sd_cmd_go_idle_state(cmd); retval = sd_run_no_data_command(host, cmd); if (retval < 0) { retval = -ENODEV; goto out; } if (retval == R1_SPI_IDLE) { break; } } if (retval != R1_SPI_IDLE) { retval = -ENODEV; goto out; } /* * Send CMD8 and CMD58 for SDHC support */ for (i = 0; i < 8; i++) { sd_cmd_crc(cmd, SD_SEND_IF_COND, 0x01AA, 0x87); //CMD8 + Check and CRC retval = sd_start_command(host, cmd); if(retval==0x01) { memset(&cmd_ret, 0, sizeof(cmd_ret)); spi_read(host, &cmd_ret, sizeof(cmd_ret)); sd_end_command(host); if (cmd_ret == 0x01AA) { //Check if CMD8 is alright sd_cmd(cmd, MMC_SPI_READ_OCR, 0);//CMD58 retval = sd_start_command(host, cmd); memset(&cmd_ret, 0, sizeof(cmd_ret)); spi_read(host, &cmd_ret, sizeof(cmd_ret)); sd_end_command(host); if(retval==0x01) { //Everything is alright break; } } break; } else if(retval==0x05) { //NO SDHC-Card break; } } /* * Send a ACMD41 to activate card initialization process. * SD card must ack with "ok" (0x00). * MMC card will report "invalid command" (0x04). */ for (i = 0; i < 65535; i++) { /* ACMD41 = CMD55 + CMD41 */ sd_cmd(cmd, MMC_APP_CMD, 0); retval = sd_run_no_data_command(host, cmd); if (retval < 0) { retval = -ENODEV; goto out; } sd_cmd(cmd, SD_APP_OP_COND, (1<<30)|0x100000); retval = sd_run_no_data_command(host, cmd); if (retval < 0) { retval = -ENODEV; goto out; } if (retval == 0x00) { /* we found a SD card */ mmc_card_set_present(&host->card); host->card.type = MMC_TYPE_SD; break; } else if(retval != 0x01) { DBG("ACMD41 return: %d\n", retval); } if ((retval & R1_SPI_ILLEGAL_COMMAND)) { /* this looks like a MMC card */ break; } } /* * MMC cards require CMD1 to activate card initialization process. * MMC card must ack with "ok" (0x00) */ if (!mmc_card_sd(&host->card)) { for (i = 0; i < 65535; i++) { sd_cmd(cmd, MMC_SEND_OP_COND, 0); retval = sd_run_no_data_command(host, cmd); if (retval < 0) { retval = -ENODEV; goto out; } if (retval == 0x00) { /* we found a MMC card */ mmc_card_set_present(&host->card); break; } } if (retval != 0x00) { DBG("MMC card, bad, retval=%02x\n", retval); sd_card_set_bad(host); } } out: return retval; }