Lines Matching full:mmc
15 #include <mmc.h>
24 static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
25 static int mmc_power_cycle(struct mmc *mmc);
27 static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps);
33 static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout) in mmc_wait_dat0() argument
39 __weak int board_mmc_getwp(struct mmc *mmc) in board_mmc_getwp() argument
44 int mmc_getwp(struct mmc *mmc) in mmc_getwp() argument
48 wp = board_mmc_getwp(mmc); in mmc_getwp()
51 if (mmc->cfg->ops->getwp) in mmc_getwp()
52 wp = mmc->cfg->ops->getwp(mmc); in mmc_getwp()
60 __weak int board_mmc_getcd(struct mmc *mmc) in board_mmc_getcd() argument
67 void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd) in mmmc_trace_before_send() argument
73 void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret) in mmmc_trace_after_send() argument
119 printf("\t\tERROR MMC rsp not supported\n"); in mmmc_trace_after_send()
125 void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd) in mmc_trace_state() argument
138 [MMC_LEGACY] = "MMC legacy", in mmc_mode_name()
140 [MMC_HS] = "MMC High Speed (26MHz)", in mmc_mode_name()
147 [MMC_HS_52] = "MMC High Speed (52MHz)", in mmc_mode_name()
148 [MMC_DDR_52] = "MMC DDR52 (52MHz)", in mmc_mode_name()
160 static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode) in mmc_mode2freq() argument
179 return mmc->legacy_speed; in mmc_mode2freq()
186 static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode) in mmc_select_mode() argument
188 mmc->selected_mode = mode; in mmc_select_mode()
189 mmc->tran_speed = mmc_mode2freq(mmc, mode); in mmc_select_mode()
190 mmc->ddr_mode = mmc_is_mode_ddr(mode); in mmc_select_mode()
192 mmc->tran_speed / 1000000); in mmc_select_mode()
197 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) in mmc_send_cmd() argument
201 mmmc_trace_before_send(mmc, cmd); in mmc_send_cmd()
202 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data); in mmc_send_cmd()
203 mmmc_trace_after_send(mmc, cmd, ret); in mmc_send_cmd()
209 int mmc_send_status(struct mmc *mmc, int timeout) in mmc_send_status() argument
216 if (!mmc_host_is_spi(mmc)) in mmc_send_status()
217 cmd.cmdarg = mmc->rca << 16; in mmc_send_status()
220 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_send_status()
243 mmc_trace_state(mmc, &cmd); in mmc_send_status()
254 int mmc_set_blocklen(struct mmc *mmc, int len) in mmc_set_blocklen() argument
259 if (mmc->ddr_mode) in mmc_set_blocklen()
266 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_set_blocklen()
269 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SET_BLOCKLEN)) { in mmc_set_blocklen()
276 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_set_blocklen()
317 int mmc_send_tuning(struct mmc *mmc, u32 opcode, int *cmd_error) in mmc_send_tuning() argument
324 if (mmc->bus_width == 8) { in mmc_send_tuning()
327 } else if (mmc->bus_width == 4) { in mmc_send_tuning()
345 err = mmc_send_cmd(mmc, &cmd, &data); in mmc_send_tuning()
356 static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start, in mmc_read_blocks() argument
367 if (mmc->high_capacity) in mmc_read_blocks()
370 cmd.cmdarg = start * mmc->read_bl_len; in mmc_read_blocks()
376 data.blocksize = mmc->read_bl_len; in mmc_read_blocks()
379 if (mmc_send_cmd(mmc, &cmd, &data)) in mmc_read_blocks()
386 if (mmc_send_cmd(mmc, &cmd, NULL)) { in mmc_read_blocks()
388 pr_err("mmc fail to send stop cmd\n"); in mmc_read_blocks()
414 struct mmc *mmc = find_mmc_device(dev_num); in mmc_bread() local
415 if (!mmc) in mmc_bread()
419 err = mmc_switch_part(mmc, block_dev->hwpart); in mmc_bread()
428 pr_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", in mmc_bread()
434 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) { in mmc_bread()
440 cur = (blocks_todo > mmc->cfg->b_max) ? in mmc_bread()
441 mmc->cfg->b_max : blocks_todo; in mmc_bread()
442 if (mmc_read_blocks(mmc, dst, start, cur) != cur) { in mmc_bread()
448 dst += cur * mmc->read_bl_len; in mmc_bread()
454 static int mmc_go_idle(struct mmc *mmc) in mmc_go_idle() argument
465 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_go_idle()
476 static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage) in mmc_switch_voltage() argument
486 return mmc_set_signal_voltage(mmc, signal_voltage); in mmc_switch_voltage()
492 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_switch_voltage()
496 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR)) in mmc_switch_voltage()
503 err = mmc_wait_dat0(mmc, 0, 100); in mmc_switch_voltage()
513 mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE); in mmc_switch_voltage()
515 err = mmc_set_signal_voltage(mmc, signal_voltage); in mmc_switch_voltage()
521 mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE); in mmc_switch_voltage()
527 err = mmc_wait_dat0(mmc, 1, 1000); in mmc_switch_voltage()
537 static int sd_send_op_cond(struct mmc *mmc, bool uhs_en) in sd_send_op_cond() argument
548 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_send_op_cond()
563 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : in sd_send_op_cond()
564 (mmc->cfg->voltages & 0xff8000); in sd_send_op_cond()
566 if (mmc->version == SD_VERSION_2) in sd_send_op_cond()
572 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_send_op_cond()
586 if (mmc->version != SD_VERSION_2) in sd_send_op_cond()
587 mmc->version = SD_VERSION_1_0; in sd_send_op_cond()
589 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ in sd_send_op_cond()
594 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_send_op_cond()
600 mmc->ocr = cmd.response[0]; in sd_send_op_cond()
603 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000) in sd_send_op_cond()
605 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180); in sd_send_op_cond()
611 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); in sd_send_op_cond()
612 mmc->rca = 0; in sd_send_op_cond()
617 static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg) in mmc_send_op_cond_iter() argument
625 if (use_arg && !mmc_host_is_spi(mmc)) in mmc_send_op_cond_iter()
627 (mmc->cfg->voltages & in mmc_send_op_cond_iter()
628 (mmc->ocr & OCR_VOLTAGE_MASK)) | in mmc_send_op_cond_iter()
629 (mmc->ocr & OCR_ACCESS_MODE); in mmc_send_op_cond_iter()
631 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_send_op_cond_iter()
634 mmc->ocr = cmd.response[0]; in mmc_send_op_cond_iter()
638 static int mmc_send_op_cond(struct mmc *mmc) in mmc_send_op_cond() argument
643 mmc_go_idle(mmc); in mmc_send_op_cond()
647 err = mmc_send_op_cond_iter(mmc, i != 0); in mmc_send_op_cond()
652 if (mmc->ocr & OCR_BUSY) in mmc_send_op_cond()
655 mmc->op_cond_pending = 1; in mmc_send_op_cond()
659 static int mmc_complete_op_cond(struct mmc *mmc) in mmc_complete_op_cond() argument
666 mmc->op_cond_pending = 0; in mmc_complete_op_cond()
667 if (!(mmc->ocr & OCR_BUSY)) { in mmc_complete_op_cond()
669 mmc_go_idle(mmc); in mmc_complete_op_cond()
673 err = mmc_send_op_cond_iter(mmc, 1); in mmc_complete_op_cond()
676 if (mmc->ocr & OCR_BUSY) in mmc_complete_op_cond()
684 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ in mmc_complete_op_cond()
689 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_complete_op_cond()
694 mmc->ocr = cmd.response[0]; in mmc_complete_op_cond()
697 mmc->version = MMC_VERSION_UNKNOWN; in mmc_complete_op_cond()
699 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); in mmc_complete_op_cond()
700 mmc->rca = 1; in mmc_complete_op_cond()
706 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd) in mmc_send_ext_csd() argument
722 err = mmc_send_cmd(mmc, &cmd, &data); in mmc_send_ext_csd()
727 static int __mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value, in __mmc_switch() argument
742 ret = mmc_send_cmd(mmc, &cmd, NULL); in __mmc_switch()
755 return mmc_send_status(mmc, timeout); in __mmc_switch()
762 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) in mmc_switch() argument
764 return __mmc_switch(mmc, set, index, value, true); in mmc_switch()
768 static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode, in mmc_set_card_speed() argument
799 err = __mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, in mmc_set_card_speed()
813 mmc_select_mode(mmc, MMC_HS); in mmc_set_card_speed()
814 mmc_set_clock(mmc, mmc_mode2freq(mmc, MMC_HS), false); in mmc_set_card_speed()
820 err = mmc_send_ext_csd(mmc, test_csd); in mmc_set_card_speed()
832 static int mmc_get_capabilities(struct mmc *mmc) in mmc_get_capabilities() argument
834 u8 *ext_csd = mmc->ext_csd; in mmc_get_capabilities()
837 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY); in mmc_get_capabilities()
839 if (mmc_host_is_spi(mmc)) in mmc_get_capabilities()
843 if (mmc->version < MMC_VERSION_4) in mmc_get_capabilities()
851 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT; in mmc_get_capabilities()
854 mmc->cardtype = cardtype; in mmc_get_capabilities()
859 mmc->card_caps |= MMC_MODE_HS200; in mmc_get_capabilities()
865 mmc->card_caps |= MMC_MODE_HS400; in mmc_get_capabilities()
870 mmc->card_caps |= MMC_MODE_DDR_52MHz; in mmc_get_capabilities()
871 mmc->card_caps |= MMC_MODE_HS_52MHz; in mmc_get_capabilities()
874 mmc->card_caps |= MMC_MODE_HS; in mmc_get_capabilities()
880 static int mmc_set_capacity(struct mmc *mmc, int part_num) in mmc_set_capacity() argument
884 mmc->capacity = mmc->capacity_user; in mmc_set_capacity()
888 mmc->capacity = mmc->capacity_boot; in mmc_set_capacity()
891 mmc->capacity = mmc->capacity_rpmb; in mmc_set_capacity()
897 mmc->capacity = mmc->capacity_gp[part_num - 4]; in mmc_set_capacity()
903 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len); in mmc_set_capacity()
909 static int mmc_boot_part_access_chk(struct mmc *mmc, unsigned int part_num) in mmc_boot_part_access_chk() argument
917 if (MMC_CAP(mmc->selected_mode) & forbidden) { in mmc_boot_part_access_chk()
919 mmc_mode_name(mmc->selected_mode), part_num); in mmc_boot_part_access_chk()
921 } else if (mmc->selected_mode != mmc->best_mode) { in mmc_boot_part_access_chk()
927 return mmc_select_mode_and_width(mmc, in mmc_boot_part_access_chk()
928 mmc->card_caps & ~forbidden); in mmc_boot_part_access_chk()
933 static inline int mmc_boot_part_access_chk(struct mmc *mmc, in mmc_boot_part_access_chk() argument
940 int mmc_switch_part(struct mmc *mmc, unsigned int part_num) in mmc_switch_part() argument
944 ret = mmc_boot_part_access_chk(mmc, part_num); in mmc_switch_part()
948 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, in mmc_switch_part()
949 (mmc->part_config & ~PART_ACCESS_MASK) in mmc_switch_part()
957 ret = mmc_set_capacity(mmc, part_num); in mmc_switch_part()
958 mmc_get_blk_desc(mmc)->hwpart = part_num; in mmc_switch_part()
965 int mmc_hwpart_config(struct mmc *mmc, in mmc_hwpart_config() argument
982 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) { in mmc_hwpart_config()
987 if (!(mmc->part_support & PART_SUPPORT)) { in mmc_hwpart_config()
992 if (!mmc->hc_wp_grp_size) { in mmc_hwpart_config()
999 if (conf->user.enh_size % mmc->hc_wp_grp_size || in mmc_hwpart_config()
1000 conf->user.enh_start % mmc->hc_wp_grp_size) { in mmc_hwpart_config()
1006 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size; in mmc_hwpart_config()
1007 if (mmc->high_capacity) { in mmc_hwpart_config()
1019 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) { in mmc_hwpart_config()
1024 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size; in mmc_hwpart_config()
1031 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) { in mmc_hwpart_config()
1036 err = mmc_send_ext_csd(mmc, ext_csd); in mmc_hwpart_config()
1088 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1097 mmc->erase_grp_size = in mmc_hwpart_config()
1104 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1111 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1119 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1126 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1139 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1148 * in the mmc struct. */ in mmc_hwpart_config()
1150 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1161 int mmc_getcd(struct mmc *mmc) in mmc_getcd() argument
1165 cd = board_mmc_getcd(mmc); in mmc_getcd()
1168 if (mmc->cfg->ops->getcd) in mmc_getcd()
1169 cd = mmc->cfg->ops->getcd(mmc); in mmc_getcd()
1179 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) in sd_switch() argument
1196 return mmc_send_cmd(mmc, &cmd, &data); in sd_switch()
1199 static int sd_get_capabilities(struct mmc *mmc) in sd_get_capabilities() argument
1211 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(SD_LEGACY); in sd_get_capabilities()
1213 if (mmc_host_is_spi(mmc)) in sd_get_capabilities()
1219 cmd.cmdarg = mmc->rca << 16; in sd_get_capabilities()
1221 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_get_capabilities()
1238 err = mmc_send_cmd(mmc, &cmd, &data); in sd_get_capabilities()
1247 mmc->scr[0] = __be32_to_cpu(scr[0]); in sd_get_capabilities()
1248 mmc->scr[1] = __be32_to_cpu(scr[1]); in sd_get_capabilities()
1250 switch ((mmc->scr[0] >> 24) & 0xf) { in sd_get_capabilities()
1252 mmc->version = SD_VERSION_1_0; in sd_get_capabilities()
1255 mmc->version = SD_VERSION_1_10; in sd_get_capabilities()
1258 mmc->version = SD_VERSION_2; in sd_get_capabilities()
1259 if ((mmc->scr[0] >> 15) & 0x1) in sd_get_capabilities()
1260 mmc->version = SD_VERSION_3; in sd_get_capabilities()
1263 mmc->version = SD_VERSION_1_0; in sd_get_capabilities()
1267 if (mmc->scr[0] & SD_DATA_4BIT) in sd_get_capabilities()
1268 mmc->card_caps |= MMC_MODE_4BIT; in sd_get_capabilities()
1271 if (mmc->version == SD_VERSION_1_0) in sd_get_capabilities()
1276 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, in sd_get_capabilities()
1289 mmc->card_caps |= MMC_CAP(SD_HS); in sd_get_capabilities()
1293 if (mmc->version < SD_VERSION_3) in sd_get_capabilities()
1298 mmc->card_caps |= MMC_CAP(UHS_SDR104); in sd_get_capabilities()
1300 mmc->card_caps |= MMC_CAP(UHS_SDR50); in sd_get_capabilities()
1302 mmc->card_caps |= MMC_CAP(UHS_SDR25); in sd_get_capabilities()
1304 mmc->card_caps |= MMC_CAP(UHS_SDR12); in sd_get_capabilities()
1306 mmc->card_caps |= MMC_CAP(UHS_DDR50); in sd_get_capabilities()
1312 static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode) in sd_set_card_speed() argument
1320 if (mmc->version == SD_VERSION_1_0) in sd_set_card_speed()
1351 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status); in sd_set_card_speed()
1361 static int sd_select_bus_width(struct mmc *mmc, int w) in sd_select_bus_width() argument
1371 cmd.cmdarg = mmc->rca << 16; in sd_select_bus_width()
1373 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_select_bus_width()
1383 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_select_bus_width()
1392 static int sd_read_ssr(struct mmc *mmc) in sd_read_ssr() argument
1411 cmd.cmdarg = mmc->rca << 16; in sd_read_ssr()
1413 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_read_ssr()
1427 err = mmc_send_cmd(mmc, &cmd, &data); in sd_read_ssr()
1439 if ((au <= 9) || (mmc->version == SD_VERSION_3)) { in sd_read_ssr()
1440 mmc->ssr.au = sd_au_size[au]; in sd_read_ssr()
1446 mmc->ssr.erase_timeout = (et * 1000) / es; in sd_read_ssr()
1447 mmc->ssr.erase_offset = eo * 1000; in sd_read_ssr()
1501 static int mmc_execute_tuning(struct mmc *mmc, uint opcode) in mmc_execute_tuning() argument
1507 static void mmc_send_init_stream(struct mmc *mmc) in mmc_send_init_stream() argument
1511 static int mmc_set_ios(struct mmc *mmc) in mmc_set_ios() argument
1515 if (mmc->cfg->ops->set_ios) in mmc_set_ios()
1516 ret = mmc->cfg->ops->set_ios(mmc); in mmc_set_ios()
1522 int mmc_set_clock(struct mmc *mmc, uint clock, bool disable) in mmc_set_clock() argument
1525 if (clock > mmc->cfg->f_max) in mmc_set_clock()
1526 clock = mmc->cfg->f_max; in mmc_set_clock()
1528 if (clock < mmc->cfg->f_min) in mmc_set_clock()
1529 clock = mmc->cfg->f_min; in mmc_set_clock()
1532 mmc->clock = clock; in mmc_set_clock()
1533 mmc->clk_disable = disable; in mmc_set_clock()
1537 return mmc_set_ios(mmc); in mmc_set_clock()
1540 static int mmc_set_bus_width(struct mmc *mmc, uint width) in mmc_set_bus_width() argument
1542 mmc->bus_width = width; in mmc_set_bus_width()
1544 return mmc_set_ios(mmc); in mmc_set_bus_width()
1592 static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage) in mmc_set_signal_voltage() argument
1596 if (mmc->signal_voltage == signal_voltage) in mmc_set_signal_voltage()
1599 mmc->signal_voltage = signal_voltage; in mmc_set_signal_voltage()
1600 err = mmc_set_ios(mmc); in mmc_set_signal_voltage()
1607 static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage) in mmc_set_signal_voltage() argument
1658 static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps) in sd_select_mode_and_width() argument
1664 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false; in sd_select_mode_and_width()
1672 mmc_dump_capabilities("host", mmc->host_caps); in sd_select_mode_and_width()
1676 caps = card_caps & mmc->host_caps; in sd_select_mode_and_width()
1689 mmc_mode2freq(mmc, mwt->mode) / 1000000); in sd_select_mode_and_width()
1692 err = sd_select_bus_width(mmc, bus_width(*w)); in sd_select_mode_and_width()
1695 mmc_set_bus_width(mmc, bus_width(*w)); in sd_select_mode_and_width()
1698 err = sd_set_card_speed(mmc, mwt->mode); in sd_select_mode_and_width()
1703 mmc_select_mode(mmc, mwt->mode); in sd_select_mode_and_width()
1704 mmc_set_clock(mmc, mmc->tran_speed, in sd_select_mode_and_width()
1709 if (mwt->tuning && !mmc_host_is_spi(mmc)) { in sd_select_mode_and_width()
1710 err = mmc_execute_tuning(mmc, in sd_select_mode_and_width()
1720 err = sd_read_ssr(mmc); in sd_select_mode_and_width()
1729 mmc_select_mode(mmc, SD_LEGACY); in sd_select_mode_and_width()
1730 mmc_set_clock(mmc, mmc->tran_speed, in sd_select_mode_and_width()
1745 static int mmc_read_and_compare_ext_csd(struct mmc *mmc) in mmc_read_and_compare_ext_csd() argument
1748 const u8 *ext_csd = mmc->ext_csd; in mmc_read_and_compare_ext_csd()
1751 if (mmc->version < MMC_VERSION_4) in mmc_read_and_compare_ext_csd()
1754 err = mmc_send_ext_csd(mmc, test_csd); in mmc_read_and_compare_ext_csd()
1775 static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode, in mmc_set_lowest_voltage() argument
1783 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_8V | in mmc_set_lowest_voltage()
1786 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V | in mmc_set_lowest_voltage()
1791 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V) in mmc_set_lowest_voltage()
1794 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V) in mmc_set_lowest_voltage()
1806 if (!mmc_set_signal_voltage(mmc, best_match)) in mmc_set_lowest_voltage()
1815 static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode, in mmc_set_lowest_voltage() argument
1874 static int mmc_select_hs400(struct mmc *mmc) in mmc_select_hs400() argument
1879 err = mmc_set_card_speed(mmc, MMC_HS_200, false); in mmc_select_hs400()
1884 mmc_select_mode(mmc, MMC_HS_200); in mmc_select_hs400()
1885 mmc_set_clock(mmc, mmc->tran_speed, false); in mmc_select_hs400()
1888 err = mmc_execute_tuning(mmc, MMC_CMD_SEND_TUNING_BLOCK_HS200); in mmc_select_hs400()
1895 mmc_set_card_speed(mmc, MMC_HS, true); in mmc_select_hs400()
1897 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, in mmc_select_hs400()
1902 err = mmc_set_card_speed(mmc, MMC_HS_400, false); in mmc_select_hs400()
1906 mmc_select_mode(mmc, MMC_HS_400); in mmc_select_hs400()
1907 err = mmc_set_clock(mmc, mmc->tran_speed, false); in mmc_select_hs400()
1914 static int mmc_select_hs400(struct mmc *mmc) in mmc_select_hs400() argument
1926 static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps) in mmc_select_mode_and_width() argument
1933 mmc_dump_capabilities("mmc", card_caps); in mmc_select_mode_and_width()
1934 mmc_dump_capabilities("host", mmc->host_caps); in mmc_select_mode_and_width()
1938 card_caps &= mmc->host_caps; in mmc_select_mode_and_width()
1940 /* Only version 4 of MMC supports wider bus widths */ in mmc_select_mode_and_width()
1941 if (mmc->version < MMC_VERSION_4) in mmc_select_mode_and_width()
1944 if (!mmc->ext_csd) { in mmc_select_mode_and_width()
1956 if (mmc->selected_mode == MMC_HS_200 || in mmc_select_mode_and_width()
1957 mmc->selected_mode == MMC_HS_400) in mmc_select_mode_and_width()
1958 mmc_set_card_speed(mmc, MMC_HS, true); in mmc_select_mode_and_width()
1961 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE); in mmc_select_mode_and_width()
1970 mmc_mode2freq(mmc, mwt->mode) / 1000000); in mmc_select_mode_and_width()
1971 old_voltage = mmc->signal_voltage; in mmc_select_mode_and_width()
1972 err = mmc_set_lowest_voltage(mmc, mwt->mode, in mmc_select_mode_and_width()
1978 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_select_mode_and_width()
1983 mmc_set_bus_width(mmc, bus_width(ecbw->cap)); in mmc_select_mode_and_width()
1986 err = mmc_select_hs400(mmc); in mmc_select_mode_and_width()
1993 err = mmc_set_card_speed(mmc, mwt->mode, false); in mmc_select_mode_and_width()
2003 err = mmc_switch(mmc, in mmc_select_mode_and_width()
2012 mmc_select_mode(mmc, mwt->mode); in mmc_select_mode_and_width()
2013 mmc_set_clock(mmc, mmc->tran_speed, in mmc_select_mode_and_width()
2019 err = mmc_execute_tuning(mmc, in mmc_select_mode_and_width()
2030 err = mmc_read_and_compare_ext_csd(mmc); in mmc_select_mode_and_width()
2034 mmc_set_signal_voltage(mmc, old_voltage); in mmc_select_mode_and_width()
2036 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_select_mode_and_width()
2038 mmc_select_mode(mmc, MMC_LEGACY); in mmc_select_mode_and_width()
2039 mmc_set_bus_width(mmc, 1); in mmc_select_mode_and_width()
2053 static int mmc_startup_v4(struct mmc *mmc) in mmc_startup_v4() argument
2074 if (IS_SD(mmc) || mmc->version < MMC_VERSION_4) in mmc_startup_v4()
2077 if (!mmc->ext_csd) in mmc_startup_v4()
2080 err = mmc_send_ext_csd(mmc, ext_csd); in mmc_startup_v4()
2085 if (!mmc->ext_csd) in mmc_startup_v4()
2086 mmc->ext_csd = ext_csd; in mmc_startup_v4()
2090 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4)) in mmc_startup_v4()
2094 err = mmc_send_ext_csd(mmc, ext_csd); in mmc_startup_v4()
2099 if (!mmc->ext_csd) in mmc_startup_v4()
2100 mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN); in mmc_startup_v4()
2101 if (!mmc->ext_csd) in mmc_startup_v4()
2103 memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN); in mmc_startup_v4()
2108 mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]]; in mmc_startup_v4()
2110 if (mmc->version >= MMC_VERSION_4_2) { in mmc_startup_v4()
2122 mmc->capacity_user = capacity; in mmc_startup_v4()
2135 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT]; in mmc_startup_v4()
2138 mmc->part_config = ext_csd[EXT_CSD_PART_CONF]; in mmc_startup_v4()
2141 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE]; in mmc_startup_v4()
2143 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17; in mmc_startup_v4()
2145 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17; in mmc_startup_v4()
2155 mmc->capacity_gp[i] = mult; in mmc_startup_v4()
2156 mmc->capacity_gp[i] *= in mmc_startup_v4()
2158 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; in mmc_startup_v4()
2159 mmc->capacity_gp[i] <<= 19; in mmc_startup_v4()
2164 mmc->enh_user_size = in mmc_startup_v4()
2168 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; in mmc_startup_v4()
2169 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; in mmc_startup_v4()
2170 mmc->enh_user_size <<= 19; in mmc_startup_v4()
2171 mmc->enh_user_start = in mmc_startup_v4()
2176 if (mmc->high_capacity) in mmc_startup_v4()
2177 mmc->enh_user_start <<= 9; in mmc_startup_v4()
2192 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_startup_v4()
2204 mmc->erase_grp_size = in mmc_startup_v4()
2212 if (mmc->high_capacity && part_completed) { in mmc_startup_v4()
2218 mmc->capacity_user = capacity; in mmc_startup_v4()
2226 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10; in mmc_startup_v4()
2227 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5; in mmc_startup_v4()
2228 mmc->erase_grp_size = (erase_gsz + 1) in mmc_startup_v4()
2233 mmc->hc_wp_grp_size = 1024 in mmc_startup_v4()
2238 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; in mmc_startup_v4()
2242 if (mmc->ext_csd) { in mmc_startup_v4()
2244 free(mmc->ext_csd); in mmc_startup_v4()
2246 mmc->ext_csd = NULL; in mmc_startup_v4()
2251 static int mmc_startup(struct mmc *mmc) in mmc_startup() argument
2260 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ in mmc_startup()
2264 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2271 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : in mmc_startup()
2276 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2279 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SEND_CID)) { in mmc_startup()
2286 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2296 memcpy(mmc->cid, cmd.response, 16); in mmc_startup()
2299 * For MMC cards, set the Relative Address. in mmc_startup()
2303 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ in mmc_startup()
2305 cmd.cmdarg = mmc->rca << 16; in mmc_startup()
2308 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2313 if (IS_SD(mmc)) in mmc_startup()
2314 mmc->rca = (cmd.response[0] >> 16) & 0xffff; in mmc_startup()
2320 cmd.cmdarg = mmc->rca << 16; in mmc_startup()
2322 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2327 mmc->csd[0] = cmd.response[0]; in mmc_startup()
2328 mmc->csd[1] = cmd.response[1]; in mmc_startup()
2329 mmc->csd[2] = cmd.response[2]; in mmc_startup()
2330 mmc->csd[3] = cmd.response[3]; in mmc_startup()
2332 if (mmc->version == MMC_VERSION_UNKNOWN) { in mmc_startup()
2337 mmc->version = MMC_VERSION_1_2; in mmc_startup()
2340 mmc->version = MMC_VERSION_1_4; in mmc_startup()
2343 mmc->version = MMC_VERSION_2_2; in mmc_startup()
2346 mmc->version = MMC_VERSION_3; in mmc_startup()
2349 mmc->version = MMC_VERSION_4; in mmc_startup()
2352 mmc->version = MMC_VERSION_1_2; in mmc_startup()
2361 mmc->legacy_speed = freq * mult; in mmc_startup()
2362 mmc_select_mode(mmc, MMC_LEGACY); in mmc_startup()
2364 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1); in mmc_startup()
2365 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); in mmc_startup()
2368 if (IS_SD(mmc)) in mmc_startup()
2369 mmc->write_bl_len = mmc->read_bl_len; in mmc_startup()
2371 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); in mmc_startup()
2374 if (mmc->high_capacity) { in mmc_startup()
2375 csize = (mmc->csd[1] & 0x3f) << 16 in mmc_startup()
2376 | (mmc->csd[2] & 0xffff0000) >> 16; in mmc_startup()
2379 csize = (mmc->csd[1] & 0x3ff) << 2 in mmc_startup()
2380 | (mmc->csd[2] & 0xc0000000) >> 30; in mmc_startup()
2381 cmult = (mmc->csd[2] & 0x00038000) >> 15; in mmc_startup()
2384 mmc->capacity_user = (csize + 1) << (cmult + 2); in mmc_startup()
2385 mmc->capacity_user *= mmc->read_bl_len; in mmc_startup()
2386 mmc->capacity_boot = 0; in mmc_startup()
2387 mmc->capacity_rpmb = 0; in mmc_startup()
2389 mmc->capacity_gp[i] = 0; in mmc_startup()
2391 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN) in mmc_startup()
2392 mmc->read_bl_len = MMC_MAX_BLOCK_LEN; in mmc_startup()
2395 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN) in mmc_startup()
2396 mmc->write_bl_len = MMC_MAX_BLOCK_LEN; in mmc_startup()
2399 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) { in mmc_startup()
2401 cmd.cmdarg = (mmc->dsr & 0xffff) << 16; in mmc_startup()
2403 if (mmc_send_cmd(mmc, &cmd, NULL)) in mmc_startup()
2404 pr_warn("MMC: SET_DSR failed\n"); in mmc_startup()
2408 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ in mmc_startup()
2411 cmd.cmdarg = mmc->rca << 16; in mmc_startup()
2412 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2422 mmc->erase_grp_size = 1; in mmc_startup()
2424 mmc->part_config = MMCPART_NOAVAILABLE; in mmc_startup()
2426 err = mmc_startup_v4(mmc); in mmc_startup()
2430 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart); in mmc_startup()
2435 mmc_set_clock(mmc, mmc->legacy_speed, false); in mmc_startup()
2436 mmc_select_mode(mmc, IS_SD(mmc) ? SD_LEGACY : MMC_LEGACY); in mmc_startup()
2437 mmc_set_bus_width(mmc, 1); in mmc_startup()
2439 if (IS_SD(mmc)) { in mmc_startup()
2440 err = sd_get_capabilities(mmc); in mmc_startup()
2443 err = sd_select_mode_and_width(mmc, mmc->card_caps); in mmc_startup()
2445 err = mmc_get_capabilities(mmc); in mmc_startup()
2448 mmc_select_mode_and_width(mmc, mmc->card_caps); in mmc_startup()
2454 mmc->best_mode = mmc->selected_mode; in mmc_startup()
2457 if (mmc->ddr_mode) { in mmc_startup()
2458 mmc->read_bl_len = MMC_MAX_BLOCK_LEN; in mmc_startup()
2460 mmc->write_bl_len = MMC_MAX_BLOCK_LEN; in mmc_startup()
2465 bdesc = mmc_get_blk_desc(mmc); in mmc_startup()
2469 bdesc->blksz = mmc->read_bl_len; in mmc_startup()
2471 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len); in mmc_startup()
2476 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff), in mmc_startup()
2477 (mmc->cid[3] >> 16) & 0xffff); in mmc_startup()
2478 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff, in mmc_startup()
2479 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, in mmc_startup()
2480 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff, in mmc_startup()
2481 (mmc->cid[2] >> 24) & 0xff); in mmc_startup()
2482 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf, in mmc_startup()
2483 (mmc->cid[2] >> 16) & 0xf); in mmc_startup()
2497 static int mmc_send_if_cond(struct mmc *mmc) in mmc_send_if_cond() argument
2504 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa; in mmc_send_if_cond()
2507 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_send_if_cond()
2515 mmc->version = SD_VERSION_2; in mmc_send_if_cond()
2521 /* board-specific MMC power initializations. */
2527 static int mmc_power_init(struct mmc *mmc) in mmc_power_init() argument
2533 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply", in mmc_power_init()
2534 &mmc->vmmc_supply); in mmc_power_init()
2536 pr_debug("%s: No vmmc supply\n", mmc->dev->name); in mmc_power_init()
2538 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply", in mmc_power_init()
2539 &mmc->vqmmc_supply); in mmc_power_init()
2541 pr_debug("%s: No vqmmc supply\n", mmc->dev->name); in mmc_power_init()
2558 static void mmc_set_initial_state(struct mmc *mmc) in mmc_set_initial_state() argument
2563 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330); in mmc_set_initial_state()
2565 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180); in mmc_set_initial_state()
2567 pr_warn("mmc: failed to set signal voltage\n"); in mmc_set_initial_state()
2569 mmc_select_mode(mmc, MMC_LEGACY); in mmc_set_initial_state()
2570 mmc_set_bus_width(mmc, 1); in mmc_set_initial_state()
2571 mmc_set_clock(mmc, 0, MMC_CLK_ENABLE); in mmc_set_initial_state()
2574 static int mmc_power_on(struct mmc *mmc) in mmc_power_on() argument
2577 if (mmc->vmmc_supply) { in mmc_power_on()
2578 int ret = regulator_set_enable(mmc->vmmc_supply, true); in mmc_power_on()
2589 static int mmc_power_off(struct mmc *mmc) in mmc_power_off() argument
2591 mmc_set_clock(mmc, 0, MMC_CLK_DISABLE); in mmc_power_off()
2593 if (mmc->vmmc_supply) { in mmc_power_off()
2594 int ret = regulator_set_enable(mmc->vmmc_supply, false); in mmc_power_off()
2605 static int mmc_power_cycle(struct mmc *mmc) in mmc_power_cycle() argument
2609 ret = mmc_power_off(mmc); in mmc_power_cycle()
2617 return mmc_power_on(mmc); in mmc_power_cycle()
2620 int mmc_get_op_cond(struct mmc *mmc) in mmc_get_op_cond() argument
2622 bool uhs_en = supports_uhs(mmc->cfg->host_caps); in mmc_get_op_cond()
2625 if (mmc->has_init) in mmc_get_op_cond()
2631 err = mmc_power_init(mmc); in mmc_get_op_cond()
2636 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN | in mmc_get_op_cond()
2640 err = mmc_power_cycle(mmc); in mmc_get_op_cond()
2649 mmc->host_caps &= ~UHS_CAPS; in mmc_get_op_cond()
2650 err = mmc_power_on(mmc); in mmc_get_op_cond()
2659 err = mmc->cfg->ops->init(mmc); in mmc_get_op_cond()
2663 mmc->ddr_mode = 0; in mmc_get_op_cond()
2666 mmc_set_initial_state(mmc); in mmc_get_op_cond()
2667 mmc_send_init_stream(mmc); in mmc_get_op_cond()
2670 err = mmc_go_idle(mmc); in mmc_get_op_cond()
2676 mmc_get_blk_desc(mmc)->hwpart = 0; in mmc_get_op_cond()
2679 err = mmc_send_if_cond(mmc); in mmc_get_op_cond()
2682 err = sd_send_op_cond(mmc, uhs_en); in mmc_get_op_cond()
2685 mmc_power_cycle(mmc); in mmc_get_op_cond()
2689 /* If the command timed out, we check for an MMC card */ in mmc_get_op_cond()
2691 err = mmc_send_op_cond(mmc); in mmc_get_op_cond()
2704 int mmc_start_init(struct mmc *mmc) in mmc_start_init() argument
2713 mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(SD_LEGACY) | in mmc_start_init()
2718 no_card = mmc_getcd(mmc) == 0; in mmc_start_init()
2723 no_card = no_card || (mmc->cfg->ops->init == NULL); in mmc_start_init()
2726 mmc->has_init = 0; in mmc_start_init()
2728 pr_err("MMC: no card present\n"); in mmc_start_init()
2733 err = mmc_get_op_cond(mmc); in mmc_start_init()
2736 mmc->init_in_progress = 1; in mmc_start_init()
2741 static int mmc_complete_init(struct mmc *mmc) in mmc_complete_init() argument
2745 mmc->init_in_progress = 0; in mmc_complete_init()
2746 if (mmc->op_cond_pending) in mmc_complete_init()
2747 err = mmc_complete_op_cond(mmc); in mmc_complete_init()
2750 err = mmc_startup(mmc); in mmc_complete_init()
2752 mmc->has_init = 0; in mmc_complete_init()
2754 mmc->has_init = 1; in mmc_complete_init()
2758 int mmc_init(struct mmc *mmc) in mmc_init() argument
2763 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev); in mmc_init()
2765 upriv->mmc = mmc; in mmc_init()
2767 if (mmc->has_init) in mmc_init()
2772 if (!mmc->init_in_progress) in mmc_init()
2773 err = mmc_start_init(mmc); in mmc_init()
2776 err = mmc_complete_init(mmc); in mmc_init()
2786 int mmc_deinit(struct mmc *mmc) in mmc_deinit() argument
2790 if (!mmc->has_init) in mmc_deinit()
2793 if (IS_SD(mmc)) { in mmc_deinit()
2794 caps_filtered = mmc->card_caps & in mmc_deinit()
2799 return sd_select_mode_and_width(mmc, caps_filtered); in mmc_deinit()
2801 caps_filtered = mmc->card_caps & in mmc_deinit()
2804 return mmc_select_mode_and_width(mmc, caps_filtered); in mmc_deinit()
2809 int mmc_set_dsr(struct mmc *mmc, u16 val) in mmc_set_dsr() argument
2811 mmc->dsr = val; in mmc_set_dsr()
2815 /* CPU-specific MMC initializations */
2821 /* board-specific MMC initializations. */
2827 void mmc_set_preinit(struct mmc *mmc, int preinit) in mmc_set_preinit() argument
2829 mmc->preinit = preinit; in mmc_set_preinit()
2845 * should allow holes, but the current MMC list does not allow that. in mmc_probe()
2875 if (initialized) /* Avoid initializing mmc multiple times */ in mmc_initialize()
2897 int mmc_set_bkops_enable(struct mmc *mmc) in mmc_set_bkops_enable() argument
2902 err = mmc_send_ext_csd(mmc, ext_csd); in mmc_set_bkops_enable()
2918 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1); in mmc_set_bkops_enable()