Lines Matching refs:mmc
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
125 void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd) in mmc_trace_state() argument
159 static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode) in mmc_mode2freq() argument
177 return mmc->legacy_speed; in mmc_mode2freq()
184 static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode) in mmc_select_mode() argument
186 mmc->selected_mode = mode; in mmc_select_mode()
187 mmc->tran_speed = mmc_mode2freq(mmc, mode); in mmc_select_mode()
188 mmc->ddr_mode = mmc_is_mode_ddr(mode); in mmc_select_mode()
190 mmc->tran_speed / 1000000); in mmc_select_mode()
195 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) in mmc_send_cmd() argument
199 mmmc_trace_before_send(mmc, cmd); in mmc_send_cmd()
200 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data); in mmc_send_cmd()
201 mmmc_trace_after_send(mmc, cmd, ret); in mmc_send_cmd()
207 int mmc_send_status(struct mmc *mmc, int timeout) in mmc_send_status() argument
214 if (!mmc_host_is_spi(mmc)) in mmc_send_status()
215 cmd.cmdarg = mmc->rca << 16; in mmc_send_status()
218 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_send_status()
241 mmc_trace_state(mmc, &cmd); in mmc_send_status()
252 int mmc_set_blocklen(struct mmc *mmc, int len) in mmc_set_blocklen() argument
257 if (mmc->ddr_mode) in mmc_set_blocklen()
264 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_set_blocklen()
267 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SET_BLOCKLEN)) { in mmc_set_blocklen()
274 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_set_blocklen()
315 int mmc_send_tuning(struct mmc *mmc, u32 opcode, int *cmd_error) in mmc_send_tuning() argument
322 if (mmc->bus_width == 8) { in mmc_send_tuning()
325 } else if (mmc->bus_width == 4) { in mmc_send_tuning()
343 err = mmc_send_cmd(mmc, &cmd, &data); in mmc_send_tuning()
354 static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start, in mmc_read_blocks() argument
365 if (mmc->high_capacity) in mmc_read_blocks()
368 cmd.cmdarg = start * mmc->read_bl_len; in mmc_read_blocks()
374 data.blocksize = mmc->read_bl_len; in mmc_read_blocks()
377 if (mmc_send_cmd(mmc, &cmd, &data)) in mmc_read_blocks()
384 if (mmc_send_cmd(mmc, &cmd, NULL)) { in mmc_read_blocks()
412 struct mmc *mmc = find_mmc_device(dev_num); in mmc_bread() local
413 if (!mmc) in mmc_bread()
417 err = mmc_switch_part(mmc, block_dev->hwpart); in mmc_bread()
432 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) { in mmc_bread()
438 cur = (blocks_todo > mmc->cfg->b_max) ? in mmc_bread()
439 mmc->cfg->b_max : blocks_todo; in mmc_bread()
440 if (mmc_read_blocks(mmc, dst, start, cur) != cur) { in mmc_bread()
446 dst += cur * mmc->read_bl_len; in mmc_bread()
452 static int mmc_go_idle(struct mmc *mmc) in mmc_go_idle() argument
463 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_go_idle()
474 static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage) in mmc_switch_voltage() argument
484 return mmc_set_signal_voltage(mmc, signal_voltage); in mmc_switch_voltage()
490 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_switch_voltage()
494 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR)) in mmc_switch_voltage()
501 err = mmc_wait_dat0(mmc, 0, 100); in mmc_switch_voltage()
511 mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE); in mmc_switch_voltage()
513 err = mmc_set_signal_voltage(mmc, signal_voltage); in mmc_switch_voltage()
519 mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE); in mmc_switch_voltage()
525 err = mmc_wait_dat0(mmc, 1, 1000); in mmc_switch_voltage()
535 static int sd_send_op_cond(struct mmc *mmc, bool uhs_en) in sd_send_op_cond() argument
546 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_send_op_cond()
561 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : in sd_send_op_cond()
562 (mmc->cfg->voltages & 0xff8000); in sd_send_op_cond()
564 if (mmc->version == SD_VERSION_2) in sd_send_op_cond()
570 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_send_op_cond()
584 if (mmc->version != SD_VERSION_2) in sd_send_op_cond()
585 mmc->version = SD_VERSION_1_0; in sd_send_op_cond()
587 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ in sd_send_op_cond()
592 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_send_op_cond()
598 mmc->ocr = cmd.response[0]; in sd_send_op_cond()
601 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000) in sd_send_op_cond()
603 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180); in sd_send_op_cond()
609 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); in sd_send_op_cond()
610 mmc->rca = 0; in sd_send_op_cond()
615 static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg) in mmc_send_op_cond_iter() argument
623 if (use_arg && !mmc_host_is_spi(mmc)) in mmc_send_op_cond_iter()
625 (mmc->cfg->voltages & in mmc_send_op_cond_iter()
626 (mmc->ocr & OCR_VOLTAGE_MASK)) | in mmc_send_op_cond_iter()
627 (mmc->ocr & OCR_ACCESS_MODE); in mmc_send_op_cond_iter()
629 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_send_op_cond_iter()
632 mmc->ocr = cmd.response[0]; in mmc_send_op_cond_iter()
636 static int mmc_send_op_cond(struct mmc *mmc) in mmc_send_op_cond() argument
641 mmc_go_idle(mmc); in mmc_send_op_cond()
645 err = mmc_send_op_cond_iter(mmc, i != 0); in mmc_send_op_cond()
650 if (mmc->ocr & OCR_BUSY) in mmc_send_op_cond()
653 mmc->op_cond_pending = 1; in mmc_send_op_cond()
657 static int mmc_complete_op_cond(struct mmc *mmc) in mmc_complete_op_cond() argument
664 mmc->op_cond_pending = 0; in mmc_complete_op_cond()
665 if (!(mmc->ocr & OCR_BUSY)) { in mmc_complete_op_cond()
667 mmc_go_idle(mmc); in mmc_complete_op_cond()
671 err = mmc_send_op_cond_iter(mmc, 1); in mmc_complete_op_cond()
674 if (mmc->ocr & OCR_BUSY) in mmc_complete_op_cond()
682 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ in mmc_complete_op_cond()
687 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_complete_op_cond()
692 mmc->ocr = cmd.response[0]; in mmc_complete_op_cond()
695 mmc->version = MMC_VERSION_UNKNOWN; in mmc_complete_op_cond()
697 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); in mmc_complete_op_cond()
698 mmc->rca = 1; in mmc_complete_op_cond()
704 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd) in mmc_send_ext_csd() argument
720 err = mmc_send_cmd(mmc, &cmd, &data); in mmc_send_ext_csd()
725 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) in mmc_switch() argument
739 ret = mmc_send_cmd(mmc, &cmd, NULL); in mmc_switch()
743 ret = mmc_send_status(mmc, timeout); in mmc_switch()
755 static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode) in mmc_set_card_speed() argument
779 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, in mmc_set_card_speed()
786 err = mmc_send_ext_csd(mmc, test_csd); in mmc_set_card_speed()
798 static int mmc_get_capabilities(struct mmc *mmc) in mmc_get_capabilities() argument
800 u8 *ext_csd = mmc->ext_csd; in mmc_get_capabilities()
803 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY); in mmc_get_capabilities()
805 if (mmc_host_is_spi(mmc)) in mmc_get_capabilities()
809 if (mmc->version < MMC_VERSION_4) in mmc_get_capabilities()
817 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT; in mmc_get_capabilities()
820 mmc->cardtype = cardtype; in mmc_get_capabilities()
825 mmc->card_caps |= MMC_MODE_HS200; in mmc_get_capabilities()
830 mmc->card_caps |= MMC_MODE_DDR_52MHz; in mmc_get_capabilities()
831 mmc->card_caps |= MMC_MODE_HS_52MHz; in mmc_get_capabilities()
834 mmc->card_caps |= MMC_MODE_HS; in mmc_get_capabilities()
840 static int mmc_set_capacity(struct mmc *mmc, int part_num) in mmc_set_capacity() argument
844 mmc->capacity = mmc->capacity_user; in mmc_set_capacity()
848 mmc->capacity = mmc->capacity_boot; in mmc_set_capacity()
851 mmc->capacity = mmc->capacity_rpmb; in mmc_set_capacity()
857 mmc->capacity = mmc->capacity_gp[part_num - 4]; in mmc_set_capacity()
863 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len); in mmc_set_capacity()
869 static int mmc_boot_part_access_chk(struct mmc *mmc, unsigned int part_num) in mmc_boot_part_access_chk() argument
877 if (MMC_CAP(mmc->selected_mode) & forbidden) { in mmc_boot_part_access_chk()
879 mmc_mode_name(mmc->selected_mode), part_num); in mmc_boot_part_access_chk()
881 } else if (mmc->selected_mode != mmc->best_mode) { in mmc_boot_part_access_chk()
887 return mmc_select_mode_and_width(mmc, in mmc_boot_part_access_chk()
888 mmc->card_caps & ~forbidden); in mmc_boot_part_access_chk()
893 static inline int mmc_boot_part_access_chk(struct mmc *mmc, in mmc_boot_part_access_chk() argument
900 int mmc_switch_part(struct mmc *mmc, unsigned int part_num) in mmc_switch_part() argument
904 ret = mmc_boot_part_access_chk(mmc, part_num); in mmc_switch_part()
908 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, in mmc_switch_part()
909 (mmc->part_config & ~PART_ACCESS_MASK) in mmc_switch_part()
917 ret = mmc_set_capacity(mmc, part_num); in mmc_switch_part()
918 mmc_get_blk_desc(mmc)->hwpart = part_num; in mmc_switch_part()
925 int mmc_hwpart_config(struct mmc *mmc, in mmc_hwpart_config() argument
942 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) { in mmc_hwpart_config()
947 if (!(mmc->part_support & PART_SUPPORT)) { in mmc_hwpart_config()
952 if (!mmc->hc_wp_grp_size) { in mmc_hwpart_config()
959 if (conf->user.enh_size % mmc->hc_wp_grp_size || in mmc_hwpart_config()
960 conf->user.enh_start % mmc->hc_wp_grp_size) { in mmc_hwpart_config()
966 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size; in mmc_hwpart_config()
967 if (mmc->high_capacity) { in mmc_hwpart_config()
979 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) { in mmc_hwpart_config()
984 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size; in mmc_hwpart_config()
991 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) { in mmc_hwpart_config()
996 err = mmc_send_ext_csd(mmc, ext_csd); in mmc_hwpart_config()
1048 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1057 mmc->erase_grp_size = in mmc_hwpart_config()
1064 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1071 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1079 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1086 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1099 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1110 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_hwpart_config()
1121 int mmc_getcd(struct mmc *mmc) in mmc_getcd() argument
1125 cd = board_mmc_getcd(mmc); in mmc_getcd()
1128 if (mmc->cfg->ops->getcd) in mmc_getcd()
1129 cd = mmc->cfg->ops->getcd(mmc); in mmc_getcd()
1139 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) in sd_switch() argument
1156 return mmc_send_cmd(mmc, &cmd, &data); in sd_switch()
1159 static int sd_get_capabilities(struct mmc *mmc) in sd_get_capabilities() argument
1171 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(SD_LEGACY); in sd_get_capabilities()
1173 if (mmc_host_is_spi(mmc)) in sd_get_capabilities()
1179 cmd.cmdarg = mmc->rca << 16; in sd_get_capabilities()
1181 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_get_capabilities()
1198 err = mmc_send_cmd(mmc, &cmd, &data); in sd_get_capabilities()
1207 mmc->scr[0] = __be32_to_cpu(scr[0]); in sd_get_capabilities()
1208 mmc->scr[1] = __be32_to_cpu(scr[1]); in sd_get_capabilities()
1210 switch ((mmc->scr[0] >> 24) & 0xf) { in sd_get_capabilities()
1212 mmc->version = SD_VERSION_1_0; in sd_get_capabilities()
1215 mmc->version = SD_VERSION_1_10; in sd_get_capabilities()
1218 mmc->version = SD_VERSION_2; in sd_get_capabilities()
1219 if ((mmc->scr[0] >> 15) & 0x1) in sd_get_capabilities()
1220 mmc->version = SD_VERSION_3; in sd_get_capabilities()
1223 mmc->version = SD_VERSION_1_0; in sd_get_capabilities()
1227 if (mmc->scr[0] & SD_DATA_4BIT) in sd_get_capabilities()
1228 mmc->card_caps |= MMC_MODE_4BIT; in sd_get_capabilities()
1231 if (mmc->version == SD_VERSION_1_0) in sd_get_capabilities()
1236 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, in sd_get_capabilities()
1249 mmc->card_caps |= MMC_CAP(SD_HS); in sd_get_capabilities()
1253 if (mmc->version < SD_VERSION_3) in sd_get_capabilities()
1258 mmc->card_caps |= MMC_CAP(UHS_SDR104); in sd_get_capabilities()
1260 mmc->card_caps |= MMC_CAP(UHS_SDR50); in sd_get_capabilities()
1262 mmc->card_caps |= MMC_CAP(UHS_SDR25); in sd_get_capabilities()
1264 mmc->card_caps |= MMC_CAP(UHS_SDR12); in sd_get_capabilities()
1266 mmc->card_caps |= MMC_CAP(UHS_DDR50); in sd_get_capabilities()
1272 static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode) in sd_set_card_speed() argument
1307 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status); in sd_set_card_speed()
1317 static int sd_select_bus_width(struct mmc *mmc, int w) in sd_select_bus_width() argument
1327 cmd.cmdarg = mmc->rca << 16; in sd_select_bus_width()
1329 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_select_bus_width()
1339 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_select_bus_width()
1348 static int sd_read_ssr(struct mmc *mmc) in sd_read_ssr() argument
1367 cmd.cmdarg = mmc->rca << 16; in sd_read_ssr()
1369 err = mmc_send_cmd(mmc, &cmd, NULL); in sd_read_ssr()
1383 err = mmc_send_cmd(mmc, &cmd, &data); in sd_read_ssr()
1395 if ((au <= 9) || (mmc->version == SD_VERSION_3)) { in sd_read_ssr()
1396 mmc->ssr.au = sd_au_size[au]; in sd_read_ssr()
1402 mmc->ssr.erase_timeout = (et * 1000) / es; in sd_read_ssr()
1403 mmc->ssr.erase_offset = eo * 1000; in sd_read_ssr()
1457 static int mmc_execute_tuning(struct mmc *mmc, uint opcode) in mmc_execute_tuning() argument
1463 static void mmc_send_init_stream(struct mmc *mmc) in mmc_send_init_stream() argument
1467 static int mmc_set_ios(struct mmc *mmc) in mmc_set_ios() argument
1471 if (mmc->cfg->ops->set_ios) in mmc_set_ios()
1472 ret = mmc->cfg->ops->set_ios(mmc); in mmc_set_ios()
1478 int mmc_set_clock(struct mmc *mmc, uint clock, bool disable) in mmc_set_clock() argument
1481 if (clock > mmc->cfg->f_max) in mmc_set_clock()
1482 clock = mmc->cfg->f_max; in mmc_set_clock()
1484 if (clock < mmc->cfg->f_min) in mmc_set_clock()
1485 clock = mmc->cfg->f_min; in mmc_set_clock()
1488 mmc->clock = clock; in mmc_set_clock()
1489 mmc->clk_disable = disable; in mmc_set_clock()
1493 return mmc_set_ios(mmc); in mmc_set_clock()
1496 static int mmc_set_bus_width(struct mmc *mmc, uint width) in mmc_set_bus_width() argument
1498 mmc->bus_width = width; in mmc_set_bus_width()
1500 return mmc_set_ios(mmc); in mmc_set_bus_width()
1548 static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage) in mmc_set_signal_voltage() argument
1552 if (mmc->signal_voltage == signal_voltage) in mmc_set_signal_voltage()
1555 mmc->signal_voltage = signal_voltage; in mmc_set_signal_voltage()
1556 err = mmc_set_ios(mmc); in mmc_set_signal_voltage()
1563 static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage) in mmc_set_signal_voltage() argument
1614 static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps) in sd_select_mode_and_width() argument
1620 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false; in sd_select_mode_and_width()
1628 mmc_dump_capabilities("host", mmc->host_caps); in sd_select_mode_and_width()
1632 caps = card_caps & mmc->host_caps; in sd_select_mode_and_width()
1645 mmc_mode2freq(mmc, mwt->mode) / 1000000); in sd_select_mode_and_width()
1648 err = sd_select_bus_width(mmc, bus_width(*w)); in sd_select_mode_and_width()
1651 mmc_set_bus_width(mmc, bus_width(*w)); in sd_select_mode_and_width()
1654 err = sd_set_card_speed(mmc, mwt->mode); in sd_select_mode_and_width()
1659 mmc_select_mode(mmc, mwt->mode); in sd_select_mode_and_width()
1660 mmc_set_clock(mmc, mmc->tran_speed, in sd_select_mode_and_width()
1665 if (mwt->tuning && !mmc_host_is_spi(mmc)) { in sd_select_mode_and_width()
1666 err = mmc_execute_tuning(mmc, in sd_select_mode_and_width()
1676 err = sd_read_ssr(mmc); in sd_select_mode_and_width()
1685 mmc_select_mode(mmc, SD_LEGACY); in sd_select_mode_and_width()
1686 mmc_set_clock(mmc, mmc->tran_speed, in sd_select_mode_and_width()
1701 static int mmc_read_and_compare_ext_csd(struct mmc *mmc) in mmc_read_and_compare_ext_csd() argument
1704 const u8 *ext_csd = mmc->ext_csd; in mmc_read_and_compare_ext_csd()
1707 if (mmc->version < MMC_VERSION_4) in mmc_read_and_compare_ext_csd()
1710 err = mmc_send_ext_csd(mmc, test_csd); in mmc_read_and_compare_ext_csd()
1731 static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode, in mmc_set_lowest_voltage() argument
1738 if (mmc->cardtype & EXT_CSD_CARD_TYPE_HS200_1_8V) in mmc_set_lowest_voltage()
1740 if (mmc->cardtype & EXT_CSD_CARD_TYPE_HS200_1_2V) in mmc_set_lowest_voltage()
1744 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V) in mmc_set_lowest_voltage()
1747 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V) in mmc_set_lowest_voltage()
1759 if (!mmc_set_signal_voltage(mmc, best_match)) in mmc_set_lowest_voltage()
1768 static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode, in mmc_set_lowest_voltage() argument
1825 static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps) in mmc_select_mode_and_width() argument
1833 mmc_dump_capabilities("host", mmc->host_caps); in mmc_select_mode_and_width()
1837 card_caps &= mmc->host_caps; in mmc_select_mode_and_width()
1840 if (mmc->version < MMC_VERSION_4) in mmc_select_mode_and_width()
1843 if (!mmc->ext_csd) { in mmc_select_mode_and_width()
1848 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE); in mmc_select_mode_and_width()
1857 mmc_mode2freq(mmc, mwt->mode) / 1000000); in mmc_select_mode_and_width()
1858 old_voltage = mmc->signal_voltage; in mmc_select_mode_and_width()
1859 err = mmc_set_lowest_voltage(mmc, mwt->mode, in mmc_select_mode_and_width()
1865 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_select_mode_and_width()
1870 mmc_set_bus_width(mmc, bus_width(ecbw->cap)); in mmc_select_mode_and_width()
1873 err = mmc_set_card_speed(mmc, mwt->mode); in mmc_select_mode_and_width()
1882 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_select_mode_and_width()
1890 mmc_select_mode(mmc, mwt->mode); in mmc_select_mode_and_width()
1891 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE); in mmc_select_mode_and_width()
1896 err = mmc_execute_tuning(mmc, mwt->tuning); in mmc_select_mode_and_width()
1905 err = mmc_read_and_compare_ext_csd(mmc); in mmc_select_mode_and_width()
1909 mmc_set_signal_voltage(mmc, old_voltage); in mmc_select_mode_and_width()
1911 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_select_mode_and_width()
1913 mmc_select_mode(mmc, MMC_LEGACY); in mmc_select_mode_and_width()
1914 mmc_set_bus_width(mmc, 1); in mmc_select_mode_and_width()
1928 static int mmc_startup_v4(struct mmc *mmc) in mmc_startup_v4() argument
1949 if (IS_SD(mmc) || mmc->version < MMC_VERSION_4) in mmc_startup_v4()
1952 if (!mmc->ext_csd) in mmc_startup_v4()
1955 err = mmc_send_ext_csd(mmc, ext_csd); in mmc_startup_v4()
1960 if (!mmc->ext_csd) in mmc_startup_v4()
1961 mmc->ext_csd = ext_csd; in mmc_startup_v4()
1965 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4)) in mmc_startup_v4()
1969 err = mmc_send_ext_csd(mmc, ext_csd); in mmc_startup_v4()
1974 if (!mmc->ext_csd) in mmc_startup_v4()
1975 mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN); in mmc_startup_v4()
1976 if (!mmc->ext_csd) in mmc_startup_v4()
1978 memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN); in mmc_startup_v4()
1983 mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]]; in mmc_startup_v4()
1985 if (mmc->version >= MMC_VERSION_4_2) { in mmc_startup_v4()
1997 mmc->capacity_user = capacity; in mmc_startup_v4()
2010 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT]; in mmc_startup_v4()
2013 mmc->part_config = ext_csd[EXT_CSD_PART_CONF]; in mmc_startup_v4()
2016 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE]; in mmc_startup_v4()
2018 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17; in mmc_startup_v4()
2020 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17; in mmc_startup_v4()
2030 mmc->capacity_gp[i] = mult; in mmc_startup_v4()
2031 mmc->capacity_gp[i] *= in mmc_startup_v4()
2033 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; in mmc_startup_v4()
2034 mmc->capacity_gp[i] <<= 19; in mmc_startup_v4()
2039 mmc->enh_user_size = in mmc_startup_v4()
2043 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; in mmc_startup_v4()
2044 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; in mmc_startup_v4()
2045 mmc->enh_user_size <<= 19; in mmc_startup_v4()
2046 mmc->enh_user_start = in mmc_startup_v4()
2051 if (mmc->high_capacity) in mmc_startup_v4()
2052 mmc->enh_user_start <<= 9; in mmc_startup_v4()
2067 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, in mmc_startup_v4()
2079 mmc->erase_grp_size = in mmc_startup_v4()
2087 if (mmc->high_capacity && part_completed) { in mmc_startup_v4()
2093 mmc->capacity_user = capacity; in mmc_startup_v4()
2101 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10; in mmc_startup_v4()
2102 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5; in mmc_startup_v4()
2103 mmc->erase_grp_size = (erase_gsz + 1) in mmc_startup_v4()
2108 mmc->hc_wp_grp_size = 1024 in mmc_startup_v4()
2113 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; in mmc_startup_v4()
2117 if (mmc->ext_csd) { in mmc_startup_v4()
2119 free(mmc->ext_csd); in mmc_startup_v4()
2121 mmc->ext_csd = NULL; in mmc_startup_v4()
2126 static int mmc_startup(struct mmc *mmc) in mmc_startup() argument
2135 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ in mmc_startup()
2139 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2146 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : in mmc_startup()
2151 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2154 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SEND_CID)) { in mmc_startup()
2161 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2171 memcpy(mmc->cid, cmd.response, 16); in mmc_startup()
2178 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ in mmc_startup()
2180 cmd.cmdarg = mmc->rca << 16; in mmc_startup()
2183 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2188 if (IS_SD(mmc)) in mmc_startup()
2189 mmc->rca = (cmd.response[0] >> 16) & 0xffff; in mmc_startup()
2195 cmd.cmdarg = mmc->rca << 16; in mmc_startup()
2197 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2202 mmc->csd[0] = cmd.response[0]; in mmc_startup()
2203 mmc->csd[1] = cmd.response[1]; in mmc_startup()
2204 mmc->csd[2] = cmd.response[2]; in mmc_startup()
2205 mmc->csd[3] = cmd.response[3]; in mmc_startup()
2207 if (mmc->version == MMC_VERSION_UNKNOWN) { in mmc_startup()
2212 mmc->version = MMC_VERSION_1_2; in mmc_startup()
2215 mmc->version = MMC_VERSION_1_4; in mmc_startup()
2218 mmc->version = MMC_VERSION_2_2; in mmc_startup()
2221 mmc->version = MMC_VERSION_3; in mmc_startup()
2224 mmc->version = MMC_VERSION_4; in mmc_startup()
2227 mmc->version = MMC_VERSION_1_2; in mmc_startup()
2236 mmc->legacy_speed = freq * mult; in mmc_startup()
2237 mmc_select_mode(mmc, MMC_LEGACY); in mmc_startup()
2239 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1); in mmc_startup()
2240 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); in mmc_startup()
2243 if (IS_SD(mmc)) in mmc_startup()
2244 mmc->write_bl_len = mmc->read_bl_len; in mmc_startup()
2246 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); in mmc_startup()
2249 if (mmc->high_capacity) { in mmc_startup()
2250 csize = (mmc->csd[1] & 0x3f) << 16 in mmc_startup()
2251 | (mmc->csd[2] & 0xffff0000) >> 16; in mmc_startup()
2254 csize = (mmc->csd[1] & 0x3ff) << 2 in mmc_startup()
2255 | (mmc->csd[2] & 0xc0000000) >> 30; in mmc_startup()
2256 cmult = (mmc->csd[2] & 0x00038000) >> 15; in mmc_startup()
2259 mmc->capacity_user = (csize + 1) << (cmult + 2); in mmc_startup()
2260 mmc->capacity_user *= mmc->read_bl_len; in mmc_startup()
2261 mmc->capacity_boot = 0; in mmc_startup()
2262 mmc->capacity_rpmb = 0; in mmc_startup()
2264 mmc->capacity_gp[i] = 0; in mmc_startup()
2266 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN) in mmc_startup()
2267 mmc->read_bl_len = MMC_MAX_BLOCK_LEN; in mmc_startup()
2270 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN) in mmc_startup()
2271 mmc->write_bl_len = MMC_MAX_BLOCK_LEN; in mmc_startup()
2274 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) { in mmc_startup()
2276 cmd.cmdarg = (mmc->dsr & 0xffff) << 16; in mmc_startup()
2278 if (mmc_send_cmd(mmc, &cmd, NULL)) in mmc_startup()
2283 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ in mmc_startup()
2286 cmd.cmdarg = mmc->rca << 16; in mmc_startup()
2287 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_startup()
2297 mmc->erase_grp_size = 1; in mmc_startup()
2299 mmc->part_config = MMCPART_NOAVAILABLE; in mmc_startup()
2301 err = mmc_startup_v4(mmc); in mmc_startup()
2305 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart); in mmc_startup()
2310 mmc_set_clock(mmc, mmc->legacy_speed, false); in mmc_startup()
2311 mmc_select_mode(mmc, IS_SD(mmc) ? SD_LEGACY : MMC_LEGACY); in mmc_startup()
2312 mmc_set_bus_width(mmc, 1); in mmc_startup()
2314 if (IS_SD(mmc)) { in mmc_startup()
2315 err = sd_get_capabilities(mmc); in mmc_startup()
2318 err = sd_select_mode_and_width(mmc, mmc->card_caps); in mmc_startup()
2320 err = mmc_get_capabilities(mmc); in mmc_startup()
2323 mmc_select_mode_and_width(mmc, mmc->card_caps); in mmc_startup()
2329 mmc->best_mode = mmc->selected_mode; in mmc_startup()
2332 if (mmc->ddr_mode) { in mmc_startup()
2333 mmc->read_bl_len = MMC_MAX_BLOCK_LEN; in mmc_startup()
2335 mmc->write_bl_len = MMC_MAX_BLOCK_LEN; in mmc_startup()
2340 bdesc = mmc_get_blk_desc(mmc); in mmc_startup()
2344 bdesc->blksz = mmc->read_bl_len; in mmc_startup()
2346 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len); in mmc_startup()
2351 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff), in mmc_startup()
2352 (mmc->cid[3] >> 16) & 0xffff); in mmc_startup()
2353 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff, in mmc_startup()
2354 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, in mmc_startup()
2355 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff, in mmc_startup()
2356 (mmc->cid[2] >> 24) & 0xff); in mmc_startup()
2357 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf, in mmc_startup()
2358 (mmc->cid[2] >> 16) & 0xf); in mmc_startup()
2371 static int mmc_send_if_cond(struct mmc *mmc) in mmc_send_if_cond() argument
2378 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa; in mmc_send_if_cond()
2381 err = mmc_send_cmd(mmc, &cmd, NULL); in mmc_send_if_cond()
2389 mmc->version = SD_VERSION_2; in mmc_send_if_cond()
2401 static int mmc_power_init(struct mmc *mmc) in mmc_power_init() argument
2407 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply", in mmc_power_init()
2408 &mmc->vmmc_supply); in mmc_power_init()
2410 pr_debug("%s: No vmmc supply\n", mmc->dev->name); in mmc_power_init()
2412 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply", in mmc_power_init()
2413 &mmc->vqmmc_supply); in mmc_power_init()
2415 pr_debug("%s: No vqmmc supply\n", mmc->dev->name); in mmc_power_init()
2432 static void mmc_set_initial_state(struct mmc *mmc) in mmc_set_initial_state() argument
2437 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330); in mmc_set_initial_state()
2439 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180); in mmc_set_initial_state()
2443 mmc_select_mode(mmc, MMC_LEGACY); in mmc_set_initial_state()
2444 mmc_set_bus_width(mmc, 1); in mmc_set_initial_state()
2445 mmc_set_clock(mmc, 0, MMC_CLK_ENABLE); in mmc_set_initial_state()
2448 static int mmc_power_on(struct mmc *mmc) in mmc_power_on() argument
2451 if (mmc->vmmc_supply) { in mmc_power_on()
2452 int ret = regulator_set_enable(mmc->vmmc_supply, true); in mmc_power_on()
2463 static int mmc_power_off(struct mmc *mmc) in mmc_power_off() argument
2465 mmc_set_clock(mmc, 0, MMC_CLK_DISABLE); in mmc_power_off()
2467 if (mmc->vmmc_supply) { in mmc_power_off()
2468 int ret = regulator_set_enable(mmc->vmmc_supply, false); in mmc_power_off()
2479 static int mmc_power_cycle(struct mmc *mmc) in mmc_power_cycle() argument
2483 ret = mmc_power_off(mmc); in mmc_power_cycle()
2491 return mmc_power_on(mmc); in mmc_power_cycle()
2494 int mmc_start_init(struct mmc *mmc) in mmc_start_init() argument
2497 bool uhs_en = supports_uhs(mmc->cfg->host_caps); in mmc_start_init()
2504 mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(SD_LEGACY) | in mmc_start_init()
2509 no_card = mmc_getcd(mmc) == 0; in mmc_start_init()
2514 no_card = no_card || (mmc->cfg->ops->init == NULL); in mmc_start_init()
2517 mmc->has_init = 0; in mmc_start_init()
2524 if (mmc->has_init) in mmc_start_init()
2530 err = mmc_power_init(mmc); in mmc_start_init()
2535 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN | in mmc_start_init()
2539 err = mmc_power_cycle(mmc); in mmc_start_init()
2548 mmc->host_caps &= ~UHS_CAPS; in mmc_start_init()
2549 err = mmc_power_on(mmc); in mmc_start_init()
2558 err = mmc->cfg->ops->init(mmc); in mmc_start_init()
2562 mmc->ddr_mode = 0; in mmc_start_init()
2565 mmc_set_initial_state(mmc); in mmc_start_init()
2566 mmc_send_init_stream(mmc); in mmc_start_init()
2569 err = mmc_go_idle(mmc); in mmc_start_init()
2575 mmc_get_blk_desc(mmc)->hwpart = 0; in mmc_start_init()
2578 err = mmc_send_if_cond(mmc); in mmc_start_init()
2581 err = sd_send_op_cond(mmc, uhs_en); in mmc_start_init()
2584 mmc_power_cycle(mmc); in mmc_start_init()
2590 err = mmc_send_op_cond(mmc); in mmc_start_init()
2601 mmc->init_in_progress = 1; in mmc_start_init()
2606 static int mmc_complete_init(struct mmc *mmc) in mmc_complete_init() argument
2610 mmc->init_in_progress = 0; in mmc_complete_init()
2611 if (mmc->op_cond_pending) in mmc_complete_init()
2612 err = mmc_complete_op_cond(mmc); in mmc_complete_init()
2615 err = mmc_startup(mmc); in mmc_complete_init()
2617 mmc->has_init = 0; in mmc_complete_init()
2619 mmc->has_init = 1; in mmc_complete_init()
2623 int mmc_init(struct mmc *mmc) in mmc_init() argument
2628 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev); in mmc_init()
2630 upriv->mmc = mmc; in mmc_init()
2632 if (mmc->has_init) in mmc_init()
2637 if (!mmc->init_in_progress) in mmc_init()
2638 err = mmc_start_init(mmc); in mmc_init()
2641 err = mmc_complete_init(mmc); in mmc_init()
2648 int mmc_set_dsr(struct mmc *mmc, u16 val) in mmc_set_dsr() argument
2650 mmc->dsr = val; in mmc_set_dsr()
2666 void mmc_set_preinit(struct mmc *mmc, int preinit) in mmc_set_preinit() argument
2668 mmc->preinit = preinit; in mmc_set_preinit()
2736 int mmc_set_bkops_enable(struct mmc *mmc) in mmc_set_bkops_enable() argument
2741 err = mmc_send_ext_csd(mmc, ext_csd); in mmc_set_bkops_enable()
2757 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1); in mmc_set_bkops_enable()