Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008, Freescale Semiconductor, Inc |
Yangbo Lu | f9049b2 | 2020-06-17 18:08:58 +0800 | [diff] [blame] | 4 | * Copyright 2020 NXP |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 5 | * Andy Fleming |
| 6 | * |
| 7 | * Based vaguely on the Linux code |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <config.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 11 | #include <blk.h> |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 12 | #include <command.h> |
Sjoerd Simons | df8aa52 | 2015-08-30 16:55:45 -0600 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Sjoerd Simons | df8aa52 | 2015-08-30 16:55:45 -0600 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
Stephen Warren | bf0c785 | 2014-05-23 12:47:06 -0600 | [diff] [blame] | 16 | #include <errno.h> |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 17 | #include <mmc.h> |
| 18 | #include <part.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 19 | #include <time.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 20 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 22 | #include <linux/printk.h> |
Peng Fan | 1530596 | 2016-10-11 15:08:43 +0800 | [diff] [blame] | 23 | #include <power/regulator.h> |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 24 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 25 | #include <memalign.h> |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 26 | #include <linux/list.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 27 | #include <linux/printk.h> |
Rabin Vincent | 69d4e2c | 2009-04-05 13:30:54 +0530 | [diff] [blame] | 28 | #include <div64.h> |
Paul Burton | 8d30cc9 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 29 | #include "mmc_private.h" |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 30 | |
Jean-Jacques Hiblot | 201559c | 2019-07-02 10:53:54 +0200 | [diff] [blame] | 31 | #define DEFAULT_CMD6_TIMEOUT_MS 500 |
| 32 | |
Tim Harvey | 728cbde | 2024-05-31 08:36:34 -0700 | [diff] [blame] | 33 | /** |
| 34 | * names of emmc BOOT_PARTITION_ENABLE values |
| 35 | * |
| 36 | * Boot Area Partitions - name consistent with Linux |
| 37 | */ |
| 38 | const char *emmc_boot_part_names[] = { |
| 39 | "default", /* EMMC_BOOT_PART_DEFAULT */ |
| 40 | "boot0", /* EMMC_BOOT_PART_BOOT1 */ |
| 41 | "boot1", /* EMMC_BOOT_PART_BOOT2 */ |
| 42 | "", |
| 43 | "", |
| 44 | "", |
| 45 | "", |
| 46 | "user", /* EMMC_BOOT_PART_USER */ |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * names of emmc 'hardware partitions' consistent with: |
| 51 | * - value used in mmc_switch() |
| 52 | * - value used by PARTITION_CONFIG PARTITION_ACCESS field |
| 53 | * |
| 54 | * Boot Area Partitions - name consistent with Linux |
| 55 | * General Perpose Partitions - name consistent with 'mmc hwpartition' usage |
| 56 | */ |
| 57 | const char *emmc_hwpart_names[] = { |
| 58 | "user", /* EMMC_HWPART_DEFAULT */ |
| 59 | "boot0", /* EMMC_HWPART_BOOT1 */ |
| 60 | "boot1", /* EMMC_HWPART_BOOT2 */ |
| 61 | "rpmb", /* EMMC_HWPART_RPMB */ |
| 62 | "gp1", /* EMMC_HWPART_GP1 */ |
| 63 | "gp2", /* EMMC_HWPART_GP2 */ |
| 64 | "gp3", /* EMMC_HWPART_GP3 */ |
| 65 | "gp4", /* EMMC_HWPART_GP4 */ |
| 66 | }; |
| 67 | |
Kishon Vijay Abraham I | 4afb12b | 2017-09-21 16:30:00 +0200 | [diff] [blame] | 68 | static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage); |
Marek Vasut | f537e39 | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 69 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 70 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 71 | |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 72 | static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us) |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 73 | { |
Loic Poulain | 9c32f4f | 2022-05-26 16:37:21 +0200 | [diff] [blame] | 74 | if (mmc->cfg->ops->wait_dat0) |
| 75 | return mmc->cfg->ops->wait_dat0(mmc, state, timeout_us); |
| 76 | |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 77 | return -ENOSYS; |
| 78 | } |
| 79 | |
Jeroen Hofstee | aedeeaa | 2014-07-12 21:24:08 +0200 | [diff] [blame] | 80 | __weak int board_mmc_getwp(struct mmc *mmc) |
Nikita Kiryanov | 020f261 | 2012-12-03 02:19:46 +0000 | [diff] [blame] | 81 | { |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | int mmc_getwp(struct mmc *mmc) |
| 86 | { |
| 87 | int wp; |
| 88 | |
| 89 | wp = board_mmc_getwp(mmc); |
| 90 | |
Peter Korsgaard | f7b1510 | 2013-03-21 04:00:03 +0000 | [diff] [blame] | 91 | if (wp < 0) { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 92 | if (mmc->cfg->ops->getwp) |
| 93 | wp = mmc->cfg->ops->getwp(mmc); |
Peter Korsgaard | f7b1510 | 2013-03-21 04:00:03 +0000 | [diff] [blame] | 94 | else |
| 95 | wp = 0; |
| 96 | } |
Nikita Kiryanov | 020f261 | 2012-12-03 02:19:46 +0000 | [diff] [blame] | 97 | |
| 98 | return wp; |
| 99 | } |
| 100 | |
Jeroen Hofstee | 4772630 | 2014-07-10 22:46:28 +0200 | [diff] [blame] | 101 | __weak int board_mmc_getcd(struct mmc *mmc) |
| 102 | { |
Stefano Babic | 6e00edf | 2010-02-05 15:04:43 +0100 | [diff] [blame] | 103 | return -1; |
| 104 | } |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 105 | #endif |
Stefano Babic | 6e00edf | 2010-02-05 15:04:43 +0100 | [diff] [blame] | 106 | |
Simon Glass | b23d96e | 2016-06-12 23:30:20 -0600 | [diff] [blame] | 107 | #ifdef CONFIG_MMC_TRACE |
| 108 | void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 109 | { |
Simon Glass | b23d96e | 2016-06-12 23:30:20 -0600 | [diff] [blame] | 110 | printf("CMD_SEND:%d\n", cmd->cmdidx); |
Marek Vasut | 6eeee30 | 2019-03-23 18:54:45 +0100 | [diff] [blame] | 111 | printf("\t\tARG\t\t\t 0x%08x\n", cmd->cmdarg); |
Simon Glass | b23d96e | 2016-06-12 23:30:20 -0600 | [diff] [blame] | 112 | } |
Marek Vasut | dccb608 | 2012-03-15 18:41:35 +0000 | [diff] [blame] | 113 | |
Simon Glass | b23d96e | 2016-06-12 23:30:20 -0600 | [diff] [blame] | 114 | void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret) |
| 115 | { |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 116 | int i; |
| 117 | u8 *ptr; |
| 118 | |
Bin Meng | 8d1ad1e | 2016-03-17 21:53:14 -0700 | [diff] [blame] | 119 | if (ret) { |
| 120 | printf("\t\tRET\t\t\t %d\n", ret); |
| 121 | } else { |
| 122 | switch (cmd->resp_type) { |
| 123 | case MMC_RSP_NONE: |
| 124 | printf("\t\tMMC_RSP_NONE\n"); |
| 125 | break; |
| 126 | case MMC_RSP_R1: |
Marek Vasut | 6eeee30 | 2019-03-23 18:54:45 +0100 | [diff] [blame] | 127 | printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08x \n", |
Bin Meng | 8d1ad1e | 2016-03-17 21:53:14 -0700 | [diff] [blame] | 128 | cmd->response[0]); |
| 129 | break; |
| 130 | case MMC_RSP_R1b: |
Marek Vasut | 6eeee30 | 2019-03-23 18:54:45 +0100 | [diff] [blame] | 131 | printf("\t\tMMC_RSP_R1b\t\t 0x%08x \n", |
Bin Meng | 8d1ad1e | 2016-03-17 21:53:14 -0700 | [diff] [blame] | 132 | cmd->response[0]); |
| 133 | break; |
| 134 | case MMC_RSP_R2: |
Marek Vasut | 6eeee30 | 2019-03-23 18:54:45 +0100 | [diff] [blame] | 135 | printf("\t\tMMC_RSP_R2\t\t 0x%08x \n", |
Bin Meng | 8d1ad1e | 2016-03-17 21:53:14 -0700 | [diff] [blame] | 136 | cmd->response[0]); |
Marek Vasut | 6eeee30 | 2019-03-23 18:54:45 +0100 | [diff] [blame] | 137 | printf("\t\t \t\t 0x%08x \n", |
Bin Meng | 8d1ad1e | 2016-03-17 21:53:14 -0700 | [diff] [blame] | 138 | cmd->response[1]); |
Marek Vasut | 6eeee30 | 2019-03-23 18:54:45 +0100 | [diff] [blame] | 139 | printf("\t\t \t\t 0x%08x \n", |
Bin Meng | 8d1ad1e | 2016-03-17 21:53:14 -0700 | [diff] [blame] | 140 | cmd->response[2]); |
Marek Vasut | 6eeee30 | 2019-03-23 18:54:45 +0100 | [diff] [blame] | 141 | printf("\t\t \t\t 0x%08x \n", |
Bin Meng | 8d1ad1e | 2016-03-17 21:53:14 -0700 | [diff] [blame] | 142 | cmd->response[3]); |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 143 | printf("\n"); |
Bin Meng | 8d1ad1e | 2016-03-17 21:53:14 -0700 | [diff] [blame] | 144 | printf("\t\t\t\t\tDUMPING DATA\n"); |
| 145 | for (i = 0; i < 4; i++) { |
| 146 | int j; |
| 147 | printf("\t\t\t\t\t%03d - ", i*4); |
| 148 | ptr = (u8 *)&cmd->response[i]; |
| 149 | ptr += 3; |
| 150 | for (j = 0; j < 4; j++) |
Marek Vasut | 6eeee30 | 2019-03-23 18:54:45 +0100 | [diff] [blame] | 151 | printf("%02x ", *ptr--); |
Bin Meng | 8d1ad1e | 2016-03-17 21:53:14 -0700 | [diff] [blame] | 152 | printf("\n"); |
| 153 | } |
| 154 | break; |
| 155 | case MMC_RSP_R3: |
Marek Vasut | 6eeee30 | 2019-03-23 18:54:45 +0100 | [diff] [blame] | 156 | printf("\t\tMMC_RSP_R3,4\t\t 0x%08x \n", |
Bin Meng | 8d1ad1e | 2016-03-17 21:53:14 -0700 | [diff] [blame] | 157 | cmd->response[0]); |
| 158 | break; |
| 159 | default: |
| 160 | printf("\t\tERROR MMC rsp not supported\n"); |
| 161 | break; |
Bin Meng | 4a4ef87 | 2016-03-17 21:53:13 -0700 | [diff] [blame] | 162 | } |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 163 | } |
Simon Glass | b23d96e | 2016-06-12 23:30:20 -0600 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd) |
| 167 | { |
| 168 | int status; |
| 169 | |
| 170 | status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9; |
| 171 | printf("CURR STATE:%d\n", status); |
| 172 | } |
Raffaele Recalcati | 894b1e2 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 173 | #endif |
Simon Glass | b23d96e | 2016-06-12 23:30:20 -0600 | [diff] [blame] | 174 | |
Pali Rohár | 377ecee | 2022-04-03 00:20:10 +0200 | [diff] [blame] | 175 | #if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG) || CONFIG_VAL(LOGLEVEL) >= LOGL_DEBUG |
Jean-Jacques Hiblot | a94fb41 | 2017-09-21 16:29:53 +0200 | [diff] [blame] | 176 | const char *mmc_mode_name(enum bus_mode mode) |
| 177 | { |
| 178 | static const char *const names[] = { |
| 179 | [MMC_LEGACY] = "MMC legacy", |
Jean-Jacques Hiblot | a94fb41 | 2017-09-21 16:29:53 +0200 | [diff] [blame] | 180 | [MMC_HS] = "MMC High Speed (26MHz)", |
| 181 | [SD_HS] = "SD High Speed (50MHz)", |
| 182 | [UHS_SDR12] = "UHS SDR12 (25MHz)", |
| 183 | [UHS_SDR25] = "UHS SDR25 (50MHz)", |
| 184 | [UHS_SDR50] = "UHS SDR50 (100MHz)", |
| 185 | [UHS_SDR104] = "UHS SDR104 (208MHz)", |
| 186 | [UHS_DDR50] = "UHS DDR50 (50MHz)", |
| 187 | [MMC_HS_52] = "MMC High Speed (52MHz)", |
| 188 | [MMC_DDR_52] = "MMC DDR52 (52MHz)", |
| 189 | [MMC_HS_200] = "HS200 (200MHz)", |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 190 | [MMC_HS_400] = "HS400 (200MHz)", |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 191 | [MMC_HS_400_ES] = "HS400ES (200MHz)", |
Jean-Jacques Hiblot | a94fb41 | 2017-09-21 16:29:53 +0200 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | if (mode >= MMC_MODES_END) |
| 195 | return "Unknown mode"; |
| 196 | else |
| 197 | return names[mode]; |
| 198 | } |
| 199 | #endif |
| 200 | |
Jean-Jacques Hiblot | 7842231 | 2017-09-21 16:29:55 +0200 | [diff] [blame] | 201 | static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode) |
| 202 | { |
| 203 | static const int freqs[] = { |
Jaehoon Chung | 7c5c730 | 2018-01-30 14:10:16 +0900 | [diff] [blame] | 204 | [MMC_LEGACY] = 25000000, |
Jean-Jacques Hiblot | 7842231 | 2017-09-21 16:29:55 +0200 | [diff] [blame] | 205 | [MMC_HS] = 26000000, |
| 206 | [SD_HS] = 50000000, |
Jaehoon Chung | 7c5c730 | 2018-01-30 14:10:16 +0900 | [diff] [blame] | 207 | [MMC_HS_52] = 52000000, |
| 208 | [MMC_DDR_52] = 52000000, |
Jean-Jacques Hiblot | 7842231 | 2017-09-21 16:29:55 +0200 | [diff] [blame] | 209 | [UHS_SDR12] = 25000000, |
| 210 | [UHS_SDR25] = 50000000, |
| 211 | [UHS_SDR50] = 100000000, |
Jean-Jacques Hiblot | 7842231 | 2017-09-21 16:29:55 +0200 | [diff] [blame] | 212 | [UHS_DDR50] = 50000000, |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 213 | [UHS_SDR104] = 208000000, |
Jean-Jacques Hiblot | 7842231 | 2017-09-21 16:29:55 +0200 | [diff] [blame] | 214 | [MMC_HS_200] = 200000000, |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 215 | [MMC_HS_400] = 200000000, |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 216 | [MMC_HS_400_ES] = 200000000, |
Jean-Jacques Hiblot | 7842231 | 2017-09-21 16:29:55 +0200 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | if (mode == MMC_LEGACY) |
| 220 | return mmc->legacy_speed; |
| 221 | else if (mode >= MMC_MODES_END) |
| 222 | return 0; |
| 223 | else |
| 224 | return freqs[mode]; |
| 225 | } |
| 226 | |
Jean-Jacques Hiblot | a94fb41 | 2017-09-21 16:29:53 +0200 | [diff] [blame] | 227 | static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode) |
| 228 | { |
| 229 | mmc->selected_mode = mode; |
Jean-Jacques Hiblot | 7842231 | 2017-09-21 16:29:55 +0200 | [diff] [blame] | 230 | mmc->tran_speed = mmc_mode2freq(mmc, mode); |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 231 | mmc->ddr_mode = mmc_is_mode_ddr(mode); |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 232 | pr_debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode), |
| 233 | mmc->tran_speed / 1000000); |
Jean-Jacques Hiblot | a94fb41 | 2017-09-21 16:29:53 +0200 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 237 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Simon Glass | b23d96e | 2016-06-12 23:30:20 -0600 | [diff] [blame] | 238 | int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 239 | { |
| 240 | int ret; |
| 241 | |
| 242 | mmmc_trace_before_send(mmc, cmd); |
| 243 | ret = mmc->cfg->ops->send_cmd(mmc, cmd, data); |
| 244 | mmmc_trace_after_send(mmc, cmd, ret); |
| 245 | |
Marek Vasut | dccb608 | 2012-03-15 18:41:35 +0000 | [diff] [blame] | 246 | return ret; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 247 | } |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 248 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 249 | |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 250 | /** |
| 251 | * mmc_send_cmd_retry() - send a command to the mmc device, retrying on error |
| 252 | * |
| 253 | * @dev: device to receive the command |
| 254 | * @cmd: command to send |
| 255 | * @data: additional data to send/receive |
| 256 | * @retries: how many times to retry; mmc_send_cmd is always called at least |
| 257 | * once |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 258 | * Return: 0 if ok, -ve on error |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 259 | */ |
| 260 | static int mmc_send_cmd_retry(struct mmc *mmc, struct mmc_cmd *cmd, |
| 261 | struct mmc_data *data, uint retries) |
| 262 | { |
| 263 | int ret; |
| 264 | |
| 265 | do { |
| 266 | ret = mmc_send_cmd(mmc, cmd, data); |
| 267 | } while (ret && retries--); |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * mmc_send_cmd_quirks() - send a command to the mmc device, retrying if a |
| 274 | * specific quirk is enabled |
| 275 | * |
| 276 | * @dev: device to receive the command |
| 277 | * @cmd: command to send |
| 278 | * @data: additional data to send/receive |
| 279 | * @quirk: retry only if this quirk is enabled |
| 280 | * @retries: how many times to retry; mmc_send_cmd is always called at least |
| 281 | * once |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 282 | * Return: 0 if ok, -ve on error |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 283 | */ |
| 284 | static int mmc_send_cmd_quirks(struct mmc *mmc, struct mmc_cmd *cmd, |
| 285 | struct mmc_data *data, u32 quirk, uint retries) |
| 286 | { |
Simon Glass | 68f3ced | 2023-02-05 15:40:16 -0700 | [diff] [blame] | 287 | if (IS_ENABLED(CONFIG_MMC_QUIRKS) && mmc->quirks & quirk) |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 288 | return mmc_send_cmd_retry(mmc, cmd, data, retries); |
| 289 | else |
| 290 | return mmc_send_cmd(mmc, cmd, data); |
| 291 | } |
| 292 | |
Jean-Jacques Hiblot | 443edbe | 2019-07-02 10:53:52 +0200 | [diff] [blame] | 293 | int mmc_send_status(struct mmc *mmc, unsigned int *status) |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 294 | { |
| 295 | struct mmc_cmd cmd; |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 296 | int ret; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 297 | |
| 298 | cmd.cmdidx = MMC_CMD_SEND_STATUS; |
| 299 | cmd.resp_type = MMC_RSP_R1; |
Marek Vasut | c442739 | 2011-08-10 09:24:48 +0200 | [diff] [blame] | 300 | if (!mmc_host_is_spi(mmc)) |
| 301 | cmd.cmdarg = mmc->rca << 16; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 302 | |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 303 | ret = mmc_send_cmd_retry(mmc, &cmd, NULL, 4); |
Jean-Jacques Hiblot | 443edbe | 2019-07-02 10:53:52 +0200 | [diff] [blame] | 304 | mmc_trace_state(mmc, &cmd); |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 305 | if (!ret) |
| 306 | *status = cmd.response[0]; |
| 307 | |
| 308 | return ret; |
Jean-Jacques Hiblot | 443edbe | 2019-07-02 10:53:52 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 311 | int mmc_poll_for_busy(struct mmc *mmc, int timeout_ms) |
Jean-Jacques Hiblot | 443edbe | 2019-07-02 10:53:52 +0200 | [diff] [blame] | 312 | { |
| 313 | unsigned int status; |
| 314 | int err; |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 315 | |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 316 | err = mmc_wait_dat0(mmc, 1, timeout_ms * 1000); |
Jean-Jacques Hiblot | 4f04a32 | 2019-07-02 10:53:53 +0200 | [diff] [blame] | 317 | if (err != -ENOSYS) |
| 318 | return err; |
| 319 | |
Jean-Jacques Hiblot | 443edbe | 2019-07-02 10:53:52 +0200 | [diff] [blame] | 320 | while (1) { |
| 321 | err = mmc_send_status(mmc, &status); |
| 322 | if (err) |
| 323 | return err; |
| 324 | |
| 325 | if ((status & MMC_STATUS_RDY_FOR_DATA) && |
| 326 | (status & MMC_STATUS_CURR_STATE) != |
| 327 | MMC_STATE_PRG) |
| 328 | break; |
| 329 | |
| 330 | if (status & MMC_STATUS_MASK) { |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 331 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 332 | log_err("Status Error: %#08x\n", status); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 333 | #endif |
Jean-Jacques Hiblot | 443edbe | 2019-07-02 10:53:52 +0200 | [diff] [blame] | 334 | return -ECOMM; |
| 335 | } |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 336 | |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 337 | if (timeout_ms-- <= 0) |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 338 | break; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 339 | |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 340 | udelay(1000); |
| 341 | } |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 342 | |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 343 | if (timeout_ms <= 0) { |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 344 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 345 | log_err("Timeout waiting card ready\n"); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 346 | #endif |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 347 | return -ETIMEDOUT; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
Paul Burton | 8d30cc9 | 2013-09-09 15:30:26 +0100 | [diff] [blame] | 353 | int mmc_set_blocklen(struct mmc *mmc, int len) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 354 | { |
| 355 | struct mmc_cmd cmd; |
| 356 | |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 357 | if (mmc->ddr_mode) |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 358 | return 0; |
| 359 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 360 | cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; |
| 361 | cmd.resp_type = MMC_RSP_R1; |
| 362 | cmd.cmdarg = len; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 363 | |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 364 | return mmc_send_cmd_quirks(mmc, &cmd, NULL, |
| 365 | MMC_QUIRK_RETRY_SET_BLOCKLEN, 4); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 368 | #if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING) |
Jean-Jacques Hiblot | 71264bb | 2017-09-21 16:30:12 +0200 | [diff] [blame] | 369 | static const u8 tuning_blk_pattern_4bit[] = { |
| 370 | 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc, |
| 371 | 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef, |
| 372 | 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb, |
| 373 | 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef, |
| 374 | 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c, |
| 375 | 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee, |
| 376 | 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff, |
| 377 | 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde, |
| 378 | }; |
| 379 | |
| 380 | static const u8 tuning_blk_pattern_8bit[] = { |
| 381 | 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 382 | 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc, |
| 383 | 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff, |
| 384 | 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff, |
| 385 | 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd, |
| 386 | 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, |
| 387 | 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff, |
| 388 | 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff, |
| 389 | 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, |
| 390 | 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, |
| 391 | 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, |
| 392 | 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, |
| 393 | 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, |
| 394 | 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, |
| 395 | 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, |
| 396 | 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, |
| 397 | }; |
| 398 | |
Marek Vasut | dad81fb | 2024-02-20 09:36:23 +0100 | [diff] [blame] | 399 | int mmc_send_tuning(struct mmc *mmc, u32 opcode) |
Jean-Jacques Hiblot | 71264bb | 2017-09-21 16:30:12 +0200 | [diff] [blame] | 400 | { |
| 401 | struct mmc_cmd cmd; |
| 402 | struct mmc_data data; |
| 403 | const u8 *tuning_block_pattern; |
| 404 | int size, err; |
| 405 | |
| 406 | if (mmc->bus_width == 8) { |
| 407 | tuning_block_pattern = tuning_blk_pattern_8bit; |
| 408 | size = sizeof(tuning_blk_pattern_8bit); |
| 409 | } else if (mmc->bus_width == 4) { |
| 410 | tuning_block_pattern = tuning_blk_pattern_4bit; |
| 411 | size = sizeof(tuning_blk_pattern_4bit); |
| 412 | } else { |
| 413 | return -EINVAL; |
| 414 | } |
| 415 | |
| 416 | ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size); |
| 417 | |
| 418 | cmd.cmdidx = opcode; |
| 419 | cmd.cmdarg = 0; |
| 420 | cmd.resp_type = MMC_RSP_R1; |
| 421 | |
| 422 | data.dest = (void *)data_buf; |
| 423 | data.blocks = 1; |
| 424 | data.blocksize = size; |
| 425 | data.flags = MMC_DATA_READ; |
| 426 | |
| 427 | err = mmc_send_cmd(mmc, &cmd, &data); |
| 428 | if (err) |
| 429 | return err; |
| 430 | |
| 431 | if (memcmp(data_buf, tuning_block_pattern, size)) |
| 432 | return -EIO; |
| 433 | |
| 434 | return 0; |
| 435 | } |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 436 | #endif |
Jean-Jacques Hiblot | 71264bb | 2017-09-21 16:30:12 +0200 | [diff] [blame] | 437 | |
Hai Pham | 27abf9f | 2023-06-20 00:38:24 +0200 | [diff] [blame] | 438 | int mmc_send_stop_transmission(struct mmc *mmc, bool write) |
| 439 | { |
| 440 | struct mmc_cmd cmd; |
| 441 | |
| 442 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
| 443 | cmd.cmdarg = 0; |
| 444 | /* |
| 445 | * JEDEC Standard No. 84-B51 Page 126 |
| 446 | * CMD12 STOP_TRANSMISSION R1/R1b[3] |
| 447 | * NOTE 3 R1 for read cases and R1b for write cases. |
| 448 | * |
| 449 | * Physical Layer Simplified Specification Version 9.00 |
| 450 | * 7.3.1.3 Detailed Command Description |
| 451 | * CMD12 R1b |
| 452 | */ |
| 453 | cmd.resp_type = (IS_SD(mmc) || write) ? MMC_RSP_R1b : MMC_RSP_R1; |
| 454 | |
| 455 | return mmc_send_cmd(mmc, &cmd, NULL); |
| 456 | } |
| 457 | |
Sascha Silbe | 4bdf6fd | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 458 | static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start, |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 459 | lbaint_t blkcnt) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 460 | { |
| 461 | struct mmc_cmd cmd; |
| 462 | struct mmc_data data; |
| 463 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 464 | if (blkcnt > 1) |
| 465 | cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; |
| 466 | else |
| 467 | cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 468 | |
| 469 | if (mmc->high_capacity) |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 470 | cmd.cmdarg = start; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 471 | else |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 472 | cmd.cmdarg = start * mmc->read_bl_len; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 473 | |
| 474 | cmd.resp_type = MMC_RSP_R1; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 475 | |
| 476 | data.dest = dst; |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 477 | data.blocks = blkcnt; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 478 | data.blocksize = mmc->read_bl_len; |
| 479 | data.flags = MMC_DATA_READ; |
| 480 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 481 | if (mmc_send_cmd(mmc, &cmd, &data)) |
| 482 | return 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 483 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 484 | if (blkcnt > 1) { |
Hai Pham | 27abf9f | 2023-06-20 00:38:24 +0200 | [diff] [blame] | 485 | if (mmc_send_stop_transmission(mmc, false)) { |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 486 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 487 | log_err("mmc fail to send stop cmd\n"); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 488 | #endif |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 489 | return 0; |
| 490 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 491 | } |
| 492 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 493 | return blkcnt; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 494 | } |
| 495 | |
Marek Vasut | 31976d9 | 2020-04-04 12:45:05 +0200 | [diff] [blame] | 496 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 497 | static int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt) |
| 498 | { |
| 499 | if (mmc->cfg->ops->get_b_max) |
| 500 | return mmc->cfg->ops->get_b_max(mmc, dst, blkcnt); |
| 501 | else |
| 502 | return mmc->cfg->b_max; |
| 503 | } |
| 504 | #endif |
| 505 | |
Simon Glass | 5f4bd8c | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 506 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | 62e293a | 2016-06-12 23:30:15 -0600 | [diff] [blame] | 507 | ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst) |
Simon Glass | 59bc6f2 | 2016-05-01 13:52:41 -0600 | [diff] [blame] | 508 | #else |
Simon Glass | 62e293a | 2016-06-12 23:30:15 -0600 | [diff] [blame] | 509 | ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt, |
| 510 | void *dst) |
Simon Glass | 59bc6f2 | 2016-05-01 13:52:41 -0600 | [diff] [blame] | 511 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 512 | { |
Simon Glass | 5f4bd8c | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 513 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 514 | struct blk_desc *block_dev = dev_get_uclass_plat(dev); |
Simon Glass | 59bc6f2 | 2016-05-01 13:52:41 -0600 | [diff] [blame] | 515 | #endif |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 516 | int dev_num = block_dev->devnum; |
Stephen Warren | 1e0f92a | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 517 | int err; |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 518 | lbaint_t cur, blocks_todo = blkcnt; |
Marek Vasut | 31976d9 | 2020-04-04 12:45:05 +0200 | [diff] [blame] | 519 | uint b_max; |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 520 | |
| 521 | if (blkcnt == 0) |
| 522 | return 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 523 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 524 | struct mmc *mmc = find_mmc_device(dev_num); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 525 | if (!mmc) |
| 526 | return 0; |
| 527 | |
Marek Vasut | f537e39 | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 528 | if (CONFIG_IS_ENABLED(MMC_TINY)) |
| 529 | err = mmc_switch_part(mmc, block_dev->hwpart); |
| 530 | else |
| 531 | err = blk_dselect_hwpart(block_dev, block_dev->hwpart); |
| 532 | |
Stephen Warren | 1e0f92a | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 533 | if (err < 0) |
| 534 | return 0; |
| 535 | |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 536 | if ((start + blkcnt) > block_dev->lba) { |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 537 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 538 | log_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", |
| 539 | start + blkcnt, block_dev->lba); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 540 | #endif |
Lei Wen | e1cc9c8 | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 541 | return 0; |
| 542 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 543 | |
Simon Glass | a4343c4 | 2015-06-23 15:38:50 -0600 | [diff] [blame] | 544 | if (mmc_set_blocklen(mmc, mmc->read_bl_len)) { |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 545 | pr_debug("%s: Failed to set blocklen\n", __func__); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 546 | return 0; |
Simon Glass | a4343c4 | 2015-06-23 15:38:50 -0600 | [diff] [blame] | 547 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 548 | |
Marek Vasut | 31976d9 | 2020-04-04 12:45:05 +0200 | [diff] [blame] | 549 | b_max = mmc_get_b_max(mmc, dst, blkcnt); |
| 550 | |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 551 | do { |
Marek Vasut | 31976d9 | 2020-04-04 12:45:05 +0200 | [diff] [blame] | 552 | cur = (blocks_todo > b_max) ? b_max : blocks_todo; |
Simon Glass | a4343c4 | 2015-06-23 15:38:50 -0600 | [diff] [blame] | 553 | if (mmc_read_blocks(mmc, dst, start, cur) != cur) { |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 554 | pr_debug("%s: Failed to read blocks\n", __func__); |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 555 | return 0; |
Simon Glass | a4343c4 | 2015-06-23 15:38:50 -0600 | [diff] [blame] | 556 | } |
Alagu Sankar | c25d1b9 | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 557 | blocks_todo -= cur; |
| 558 | start += cur; |
| 559 | dst += cur * mmc->read_bl_len; |
| 560 | } while (blocks_todo > 0); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 561 | |
| 562 | return blkcnt; |
| 563 | } |
| 564 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 565 | static int mmc_go_idle(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 566 | { |
| 567 | struct mmc_cmd cmd; |
| 568 | int err; |
| 569 | |
| 570 | udelay(1000); |
| 571 | |
| 572 | cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; |
| 573 | cmd.cmdarg = 0; |
| 574 | cmd.resp_type = MMC_RSP_NONE; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 575 | |
| 576 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 577 | |
| 578 | if (err) |
| 579 | return err; |
| 580 | |
| 581 | udelay(2000); |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 586 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 587 | static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage) |
| 588 | { |
| 589 | struct mmc_cmd cmd; |
| 590 | int err = 0; |
| 591 | |
| 592 | /* |
| 593 | * Send CMD11 only if the request is to switch the card to |
| 594 | * 1.8V signalling. |
| 595 | */ |
| 596 | if (signal_voltage == MMC_SIGNAL_VOLTAGE_330) |
| 597 | return mmc_set_signal_voltage(mmc, signal_voltage); |
| 598 | |
| 599 | cmd.cmdidx = SD_CMD_SWITCH_UHS18V; |
| 600 | cmd.cmdarg = 0; |
| 601 | cmd.resp_type = MMC_RSP_R1; |
| 602 | |
| 603 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 604 | if (err) |
| 605 | return err; |
| 606 | |
| 607 | if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR)) |
| 608 | return -EIO; |
| 609 | |
| 610 | /* |
| 611 | * The card should drive cmd and dat[0:3] low immediately |
| 612 | * after the response of cmd11, but wait 100 us to be sure |
| 613 | */ |
| 614 | err = mmc_wait_dat0(mmc, 0, 100); |
| 615 | if (err == -ENOSYS) |
| 616 | udelay(100); |
| 617 | else if (err) |
| 618 | return -ETIMEDOUT; |
| 619 | |
| 620 | /* |
| 621 | * During a signal voltage level switch, the clock must be gated |
| 622 | * for 5 ms according to the SD spec |
| 623 | */ |
Jaehoon Chung | 239cb2f | 2018-01-26 19:25:29 +0900 | [diff] [blame] | 624 | mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE); |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 625 | |
| 626 | err = mmc_set_signal_voltage(mmc, signal_voltage); |
| 627 | if (err) |
| 628 | return err; |
| 629 | |
| 630 | /* Keep clock gated for at least 10 ms, though spec only says 5 ms */ |
| 631 | mdelay(10); |
Jaehoon Chung | 239cb2f | 2018-01-26 19:25:29 +0900 | [diff] [blame] | 632 | mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE); |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 633 | |
| 634 | /* |
| 635 | * Failure to switch is indicated by the card holding |
| 636 | * dat[0:3] low. Wait for at least 1 ms according to spec |
| 637 | */ |
| 638 | err = mmc_wait_dat0(mmc, 1, 1000); |
| 639 | if (err == -ENOSYS) |
| 640 | udelay(1000); |
| 641 | else if (err) |
| 642 | return -ETIMEDOUT; |
| 643 | |
| 644 | return 0; |
| 645 | } |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 646 | #endif |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 647 | |
| 648 | static int sd_send_op_cond(struct mmc *mmc, bool uhs_en) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 649 | { |
| 650 | int timeout = 1000; |
| 651 | int err; |
| 652 | struct mmc_cmd cmd; |
| 653 | |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 654 | while (1) { |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 655 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 656 | cmd.resp_type = MMC_RSP_R1; |
| 657 | cmd.cmdarg = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 658 | |
| 659 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 660 | |
| 661 | if (err) |
| 662 | return err; |
| 663 | |
| 664 | cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; |
| 665 | cmd.resp_type = MMC_RSP_R3; |
Stefano Babic | f8e9a21 | 2010-01-20 18:20:39 +0100 | [diff] [blame] | 666 | |
| 667 | /* |
| 668 | * Most cards do not answer if some reserved bits |
| 669 | * in the ocr are set. However, Some controller |
| 670 | * can set bit 7 (reserved for low voltages), but |
| 671 | * how to manage low voltages SD card is not yet |
| 672 | * specified. |
| 673 | */ |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 674 | cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 675 | (mmc->cfg->voltages & 0xff8000); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 676 | |
| 677 | if (mmc->version == SD_VERSION_2) |
| 678 | cmd.cmdarg |= OCR_HCS; |
| 679 | |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 680 | if (uhs_en) |
| 681 | cmd.cmdarg |= OCR_S18R; |
| 682 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 683 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 684 | |
| 685 | if (err) |
| 686 | return err; |
| 687 | |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 688 | if (cmd.response[0] & OCR_BUSY) |
| 689 | break; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 690 | |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 691 | if (timeout-- <= 0) |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 692 | return -EOPNOTSUPP; |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 693 | |
| 694 | udelay(1000); |
| 695 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 696 | |
| 697 | if (mmc->version != SD_VERSION_2) |
| 698 | mmc->version = SD_VERSION_1_0; |
| 699 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 700 | if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ |
| 701 | cmd.cmdidx = MMC_CMD_SPI_READ_OCR; |
| 702 | cmd.resp_type = MMC_RSP_R3; |
| 703 | cmd.cmdarg = 0; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 704 | |
| 705 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 706 | |
| 707 | if (err) |
| 708 | return err; |
| 709 | } |
| 710 | |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 711 | mmc->ocr = cmd.response[0]; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 712 | |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 713 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 714 | if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000) |
| 715 | == 0x41000000) { |
| 716 | err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180); |
| 717 | if (err) |
| 718 | return err; |
| 719 | } |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 720 | #endif |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 721 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 722 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); |
| 723 | mmc->rca = 0; |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 728 | static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 729 | { |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 730 | struct mmc_cmd cmd; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 731 | int err; |
| 732 | |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 733 | cmd.cmdidx = MMC_CMD_SEND_OP_COND; |
| 734 | cmd.resp_type = MMC_RSP_R3; |
| 735 | cmd.cmdarg = 0; |
Rob Herring | 5fd3edd | 2015-03-23 17:56:59 -0500 | [diff] [blame] | 736 | if (use_arg && !mmc_host_is_spi(mmc)) |
| 737 | cmd.cmdarg = OCR_HCS | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 738 | (mmc->cfg->voltages & |
Andrew Gabbasov | ec600d1 | 2015-03-19 07:44:03 -0500 | [diff] [blame] | 739 | (mmc->ocr & OCR_VOLTAGE_MASK)) | |
| 740 | (mmc->ocr & OCR_ACCESS_MODE); |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 741 | |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 742 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 743 | if (err) |
| 744 | return err; |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 745 | mmc->ocr = cmd.response[0]; |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 746 | return 0; |
| 747 | } |
| 748 | |
Jeroen Hofstee | aedeeaa | 2014-07-12 21:24:08 +0200 | [diff] [blame] | 749 | static int mmc_send_op_cond(struct mmc *mmc) |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 750 | { |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 751 | int err, i; |
Haibo Chen | 7194951 | 2020-06-15 17:18:12 +0800 | [diff] [blame] | 752 | int timeout = 1000; |
| 753 | uint start; |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 754 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 755 | /* Some cards seem to need this */ |
| 756 | mmc_go_idle(mmc); |
| 757 | |
Haibo Chen | 7194951 | 2020-06-15 17:18:12 +0800 | [diff] [blame] | 758 | start = get_timer(0); |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 759 | /* Asking to the card its capabilities */ |
Haibo Chen | 7194951 | 2020-06-15 17:18:12 +0800 | [diff] [blame] | 760 | for (i = 0; ; i++) { |
Andrew Gabbasov | fafa6a0 | 2015-03-19 07:44:04 -0500 | [diff] [blame] | 761 | err = mmc_send_op_cond_iter(mmc, i != 0); |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 762 | if (err) |
| 763 | return err; |
Wolfgang Denk | 80f7021 | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 764 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 765 | /* exit if not busy (flag seems to be inverted) */ |
Andrew Gabbasov | ec600d1 | 2015-03-19 07:44:03 -0500 | [diff] [blame] | 766 | if (mmc->ocr & OCR_BUSY) |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 767 | break; |
Haibo Chen | 7194951 | 2020-06-15 17:18:12 +0800 | [diff] [blame] | 768 | |
| 769 | if (get_timer(start) > timeout) |
| 770 | return -ETIMEDOUT; |
| 771 | udelay(100); |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 772 | } |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 773 | mmc->op_cond_pending = 1; |
| 774 | return 0; |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 775 | } |
Wolfgang Denk | 80f7021 | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 776 | |
Jeroen Hofstee | aedeeaa | 2014-07-12 21:24:08 +0200 | [diff] [blame] | 777 | static int mmc_complete_op_cond(struct mmc *mmc) |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 778 | { |
| 779 | struct mmc_cmd cmd; |
| 780 | int timeout = 1000; |
Vipul Kumar | dbad7b4 | 2018-05-03 12:20:54 +0530 | [diff] [blame] | 781 | ulong start; |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 782 | int err; |
Wolfgang Denk | 80f7021 | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 783 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 784 | mmc->op_cond_pending = 0; |
Andrew Gabbasov | 5a513ca | 2015-03-19 07:44:05 -0500 | [diff] [blame] | 785 | if (!(mmc->ocr & OCR_BUSY)) { |
Yangbo Lu | 9c72061 | 2016-08-02 15:33:18 +0800 | [diff] [blame] | 786 | /* Some cards seem to need this */ |
| 787 | mmc_go_idle(mmc); |
| 788 | |
Andrew Gabbasov | 5a513ca | 2015-03-19 07:44:05 -0500 | [diff] [blame] | 789 | start = get_timer(0); |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 790 | while (1) { |
Andrew Gabbasov | 5a513ca | 2015-03-19 07:44:05 -0500 | [diff] [blame] | 791 | err = mmc_send_op_cond_iter(mmc, 1); |
| 792 | if (err) |
| 793 | return err; |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 794 | if (mmc->ocr & OCR_BUSY) |
| 795 | break; |
Andrew Gabbasov | 5a513ca | 2015-03-19 07:44:05 -0500 | [diff] [blame] | 796 | if (get_timer(start) > timeout) |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 797 | return -EOPNOTSUPP; |
Andrew Gabbasov | 5a513ca | 2015-03-19 07:44:05 -0500 | [diff] [blame] | 798 | udelay(100); |
Andrew Gabbasov | 034857c | 2015-03-19 07:44:06 -0500 | [diff] [blame] | 799 | } |
Andrew Gabbasov | 5a513ca | 2015-03-19 07:44:05 -0500 | [diff] [blame] | 800 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 801 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 802 | if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ |
| 803 | cmd.cmdidx = MMC_CMD_SPI_READ_OCR; |
| 804 | cmd.resp_type = MMC_RSP_R3; |
| 805 | cmd.cmdarg = 0; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 806 | |
| 807 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 808 | |
| 809 | if (err) |
| 810 | return err; |
Andrew Gabbasov | ec600d1 | 2015-03-19 07:44:03 -0500 | [diff] [blame] | 811 | |
| 812 | mmc->ocr = cmd.response[0]; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 815 | mmc->version = MMC_VERSION_UNKNOWN; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 816 | |
| 817 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); |
Stephen Warren | f6545f1 | 2014-01-30 16:11:12 -0700 | [diff] [blame] | 818 | mmc->rca = 1; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
Heinrich Schuchardt | bf230e1 | 2020-03-30 07:24:17 +0200 | [diff] [blame] | 823 | int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 824 | { |
| 825 | struct mmc_cmd cmd; |
| 826 | struct mmc_data data; |
| 827 | int err; |
| 828 | |
| 829 | /* Get the Card Status Register */ |
| 830 | cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; |
| 831 | cmd.resp_type = MMC_RSP_R1; |
| 832 | cmd.cmdarg = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 833 | |
Yoshihiro Shimoda | f6bec73 | 2012-06-07 19:09:11 +0000 | [diff] [blame] | 834 | data.dest = (char *)ext_csd; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 835 | data.blocks = 1; |
Simon Glass | a09c2b7 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 836 | data.blocksize = MMC_MAX_BLOCK_LEN; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 837 | data.flags = MMC_DATA_READ; |
| 838 | |
| 839 | err = mmc_send_cmd(mmc, &cmd, &data); |
| 840 | |
| 841 | return err; |
| 842 | } |
| 843 | |
Marek Vasut | 8a96647 | 2019-02-06 11:34:27 +0100 | [diff] [blame] | 844 | static int __mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value, |
| 845 | bool send_status) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 846 | { |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 847 | unsigned int status, start; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 848 | struct mmc_cmd cmd; |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 849 | int timeout_ms = DEFAULT_CMD6_TIMEOUT_MS; |
Jean-Jacques Hiblot | 7f5b169 | 2019-07-02 10:53:55 +0200 | [diff] [blame] | 850 | bool is_part_switch = (set == EXT_CSD_CMD_SET_NORMAL) && |
| 851 | (index == EXT_CSD_PART_CONF); |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 852 | int ret; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 853 | |
Jean-Jacques Hiblot | 201559c | 2019-07-02 10:53:54 +0200 | [diff] [blame] | 854 | if (mmc->gen_cmd6_time) |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 855 | timeout_ms = mmc->gen_cmd6_time * 10; |
Jean-Jacques Hiblot | 201559c | 2019-07-02 10:53:54 +0200 | [diff] [blame] | 856 | |
Jean-Jacques Hiblot | 7f5b169 | 2019-07-02 10:53:55 +0200 | [diff] [blame] | 857 | if (is_part_switch && mmc->part_switch_time) |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 858 | timeout_ms = mmc->part_switch_time * 10; |
Jean-Jacques Hiblot | 7f5b169 | 2019-07-02 10:53:55 +0200 | [diff] [blame] | 859 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 860 | cmd.cmdidx = MMC_CMD_SWITCH; |
| 861 | cmd.resp_type = MMC_RSP_R1b; |
| 862 | cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 863 | (index << 16) | |
| 864 | (value << 8); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 865 | |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 866 | ret = mmc_send_cmd_retry(mmc, &cmd, NULL, 3); |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 867 | if (ret) |
| 868 | return ret; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 869 | |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 870 | start = get_timer(0); |
Marek Vasut | 8a96647 | 2019-02-06 11:34:27 +0100 | [diff] [blame] | 871 | |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 872 | /* poll dat0 for rdy/buys status */ |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 873 | ret = mmc_wait_dat0(mmc, 1, timeout_ms * 1000); |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 874 | if (ret && ret != -ENOSYS) |
| 875 | return ret; |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 876 | |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 877 | /* |
Kirill Kapranov | cd9ea64 | 2021-10-09 23:49:59 +0300 | [diff] [blame] | 878 | * In cases when neiter allowed to poll by using CMD13 nor we are |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 879 | * capable of polling by using mmc_wait_dat0, then rely on waiting the |
| 880 | * stated timeout to be sufficient. |
| 881 | */ |
Kirill Kapranov | cd9ea64 | 2021-10-09 23:49:59 +0300 | [diff] [blame] | 882 | if (ret == -ENOSYS && !send_status) { |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 883 | mdelay(timeout_ms); |
Haibo Chen | d8de5e4 | 2020-09-22 18:11:42 +0800 | [diff] [blame] | 884 | return 0; |
| 885 | } |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 886 | |
Marek Vasut | 946e06f | 2022-07-15 01:58:24 +0200 | [diff] [blame] | 887 | if (!send_status) |
| 888 | return 0; |
| 889 | |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 890 | /* Finally wait until the card is ready or indicates a failure |
| 891 | * to switch. It doesn't hurt to use CMD13 here even if send_status |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 892 | * is false, because by now (after 'timeout_ms' ms) the bus should be |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 893 | * reliable. |
| 894 | */ |
| 895 | do { |
| 896 | ret = mmc_send_status(mmc, &status); |
| 897 | |
| 898 | if (!ret && (status & MMC_STATUS_SWITCH_ERROR)) { |
| 899 | pr_debug("switch failed %d/%d/0x%x !\n", set, index, |
| 900 | value); |
| 901 | return -EIO; |
| 902 | } |
Stefan Bosch | a463bbe | 2021-01-23 13:37:41 +0100 | [diff] [blame] | 903 | if (!ret && (status & MMC_STATUS_RDY_FOR_DATA) && |
| 904 | (status & MMC_STATUS_CURR_STATE) == MMC_STATE_TRANS) |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 905 | return 0; |
| 906 | udelay(100); |
Sam Protsenko | db174c6 | 2019-08-14 22:52:51 +0300 | [diff] [blame] | 907 | } while (get_timer(start) < timeout_ms); |
Raffaele Recalcati | 01a0dc6 | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 908 | |
Jean-Jacques Hiblot | 5a7cf40 | 2019-07-02 10:53:56 +0200 | [diff] [blame] | 909 | return -ETIMEDOUT; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 910 | } |
| 911 | |
Marek Vasut | 8a96647 | 2019-02-06 11:34:27 +0100 | [diff] [blame] | 912 | int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) |
| 913 | { |
| 914 | return __mmc_switch(mmc, set, index, value, true); |
| 915 | } |
| 916 | |
Heinrich Schuchardt | 75e5a64 | 2020-03-30 07:24:19 +0200 | [diff] [blame] | 917 | int mmc_boot_wp(struct mmc *mmc) |
| 918 | { |
| 919 | return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, 1); |
| 920 | } |
| 921 | |
Ying-Chun Liu (PaulLiu) | 4493cb5 | 2022-04-25 21:59:02 +0800 | [diff] [blame] | 922 | int mmc_boot_wp_single_partition(struct mmc *mmc, int partition) |
| 923 | { |
| 924 | u8 value; |
| 925 | int ret; |
| 926 | |
| 927 | value = EXT_CSD_BOOT_WP_B_PWR_WP_EN; |
| 928 | |
| 929 | if (partition == 0) { |
| 930 | value |= EXT_CSD_BOOT_WP_B_SEC_WP_SEL; |
| 931 | ret = mmc_switch(mmc, |
| 932 | EXT_CSD_CMD_SET_NORMAL, |
| 933 | EXT_CSD_BOOT_WP, |
| 934 | value); |
| 935 | } else if (partition == 1) { |
| 936 | value |= EXT_CSD_BOOT_WP_B_SEC_WP_SEL; |
| 937 | value |= EXT_CSD_BOOT_WP_B_PWR_WP_SEC_SEL; |
| 938 | ret = mmc_switch(mmc, |
| 939 | EXT_CSD_CMD_SET_NORMAL, |
| 940 | EXT_CSD_BOOT_WP, |
| 941 | value); |
| 942 | } else { |
| 943 | ret = mmc_boot_wp(mmc); |
| 944 | } |
| 945 | |
| 946 | return ret; |
| 947 | } |
| 948 | |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 949 | #if !CONFIG_IS_ENABLED(MMC_TINY) |
Marek Vasut | 111572f | 2019-01-03 21:19:24 +0100 | [diff] [blame] | 950 | static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode, |
| 951 | bool hsdowngrade) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 952 | { |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 953 | int err; |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 954 | int speed_bits; |
| 955 | |
| 956 | ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN); |
| 957 | |
| 958 | switch (mode) { |
| 959 | case MMC_HS: |
| 960 | case MMC_HS_52: |
| 961 | case MMC_DDR_52: |
| 962 | speed_bits = EXT_CSD_TIMING_HS; |
Kishon Vijay Abraham I | 210369f | 2017-09-21 16:30:06 +0200 | [diff] [blame] | 963 | break; |
Jean-Jacques Hiblot | 74c98b2 | 2018-01-04 15:23:30 +0100 | [diff] [blame] | 964 | #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) |
Kishon Vijay Abraham I | 210369f | 2017-09-21 16:30:06 +0200 | [diff] [blame] | 965 | case MMC_HS_200: |
| 966 | speed_bits = EXT_CSD_TIMING_HS200; |
| 967 | break; |
Jean-Jacques Hiblot | 74c98b2 | 2018-01-04 15:23:30 +0100 | [diff] [blame] | 968 | #endif |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 969 | #if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 970 | case MMC_HS_400: |
| 971 | speed_bits = EXT_CSD_TIMING_HS400; |
| 972 | break; |
| 973 | #endif |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 974 | #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
| 975 | case MMC_HS_400_ES: |
| 976 | speed_bits = EXT_CSD_TIMING_HS400; |
| 977 | break; |
| 978 | #endif |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 979 | case MMC_LEGACY: |
| 980 | speed_bits = EXT_CSD_TIMING_LEGACY; |
| 981 | break; |
| 982 | default: |
| 983 | return -EINVAL; |
| 984 | } |
Marek Vasut | 8a96647 | 2019-02-06 11:34:27 +0100 | [diff] [blame] | 985 | |
| 986 | err = __mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, |
| 987 | speed_bits, !hsdowngrade); |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 988 | if (err) |
| 989 | return err; |
| 990 | |
Marek Vasut | 111572f | 2019-01-03 21:19:24 +0100 | [diff] [blame] | 991 | #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
| 992 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 993 | /* |
| 994 | * In case the eMMC is in HS200/HS400 mode and we are downgrading |
| 995 | * to HS mode, the card clock are still running much faster than |
| 996 | * the supported HS mode clock, so we can not reliably read out |
| 997 | * Extended CSD. Reconfigure the controller to run at HS mode. |
| 998 | */ |
| 999 | if (hsdowngrade) { |
Venkatesh Yadav Abbarapu | 10f9def | 2024-04-23 11:00:57 +0530 | [diff] [blame] | 1000 | mmc_select_mode(mmc, MMC_HS_52); |
| 1001 | mmc_set_clock(mmc, mmc_mode2freq(mmc, MMC_HS_52), false); |
Marek Vasut | 111572f | 2019-01-03 21:19:24 +0100 | [diff] [blame] | 1002 | } |
| 1003 | #endif |
| 1004 | |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 1005 | if ((mode == MMC_HS) || (mode == MMC_HS_52)) { |
| 1006 | /* Now check to see that it worked */ |
| 1007 | err = mmc_send_ext_csd(mmc, test_csd); |
| 1008 | if (err) |
| 1009 | return err; |
| 1010 | |
| 1011 | /* No high-speed support */ |
| 1012 | if (!test_csd[EXT_CSD_HS_TIMING]) |
| 1013 | return -ENOTSUPP; |
| 1014 | } |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | static int mmc_get_capabilities(struct mmc *mmc) |
| 1020 | { |
| 1021 | u8 *ext_csd = mmc->ext_csd; |
| 1022 | char cardtype; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1023 | |
Jean-Jacques Hiblot | 3f2ffc2 | 2017-11-30 17:43:56 +0100 | [diff] [blame] | 1024 | mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1025 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1026 | if (mmc_host_is_spi(mmc)) |
| 1027 | return 0; |
| 1028 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1029 | /* Only version 4 supports high-speed */ |
| 1030 | if (mmc->version < MMC_VERSION_4) |
| 1031 | return 0; |
| 1032 | |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 1033 | if (!ext_csd) { |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 1034 | log_err("No ext_csd found!\n"); /* this should never happen */ |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 1035 | return -ENOTSUPP; |
| 1036 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1037 | |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 1038 | mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1039 | |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 1040 | cardtype = ext_csd[EXT_CSD_CARD_TYPE]; |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 1041 | mmc->cardtype = cardtype; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1042 | |
Jean-Jacques Hiblot | 74c98b2 | 2018-01-04 15:23:30 +0100 | [diff] [blame] | 1043 | #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) |
Kishon Vijay Abraham I | 210369f | 2017-09-21 16:30:06 +0200 | [diff] [blame] | 1044 | if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V | |
| 1045 | EXT_CSD_CARD_TYPE_HS200_1_8V)) { |
| 1046 | mmc->card_caps |= MMC_MODE_HS200; |
| 1047 | } |
Jean-Jacques Hiblot | 74c98b2 | 2018-01-04 15:23:30 +0100 | [diff] [blame] | 1048 | #endif |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 1049 | #if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \ |
| 1050 | CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 1051 | if (cardtype & (EXT_CSD_CARD_TYPE_HS400_1_2V | |
| 1052 | EXT_CSD_CARD_TYPE_HS400_1_8V)) { |
| 1053 | mmc->card_caps |= MMC_MODE_HS400; |
| 1054 | } |
| 1055 | #endif |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 1056 | if (cardtype & EXT_CSD_CARD_TYPE_52) { |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 1057 | if (cardtype & EXT_CSD_CARD_TYPE_DDR_52) |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 1058 | mmc->card_caps |= MMC_MODE_DDR_52MHz; |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 1059 | mmc->card_caps |= MMC_MODE_HS_52MHz; |
Jaehoon Chung | 38ce30b | 2014-05-16 13:59:54 +0900 | [diff] [blame] | 1060 | } |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 1061 | if (cardtype & EXT_CSD_CARD_TYPE_26) |
| 1062 | mmc->card_caps |= MMC_MODE_HS; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1063 | |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 1064 | #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
| 1065 | if (ext_csd[EXT_CSD_STROBE_SUPPORT] && |
| 1066 | (mmc->card_caps & MMC_MODE_HS400)) { |
| 1067 | mmc->card_caps |= MMC_MODE_HS400_ES; |
| 1068 | } |
| 1069 | #endif |
| 1070 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1071 | return 0; |
| 1072 | } |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 1073 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1074 | |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1075 | static int mmc_set_capacity(struct mmc *mmc, int part_num) |
| 1076 | { |
| 1077 | switch (part_num) { |
| 1078 | case 0: |
| 1079 | mmc->capacity = mmc->capacity_user; |
| 1080 | break; |
| 1081 | case 1: |
| 1082 | case 2: |
| 1083 | mmc->capacity = mmc->capacity_boot; |
| 1084 | break; |
| 1085 | case 3: |
| 1086 | mmc->capacity = mmc->capacity_rpmb; |
| 1087 | break; |
| 1088 | case 4: |
| 1089 | case 5: |
| 1090 | case 6: |
| 1091 | case 7: |
| 1092 | mmc->capacity = mmc->capacity_gp[part_num - 4]; |
| 1093 | break; |
| 1094 | default: |
| 1095 | return -1; |
| 1096 | } |
| 1097 | |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 1098 | mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len); |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
Simon Glass | 62e293a | 2016-06-12 23:30:15 -0600 | [diff] [blame] | 1103 | int mmc_switch_part(struct mmc *mmc, unsigned int part_num) |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1104 | { |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1105 | int ret; |
Jean-Jacques Hiblot | faf5c95 | 2019-07-02 10:53:58 +0200 | [diff] [blame] | 1106 | int retry = 3; |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1107 | |
Jean-Jacques Hiblot | faf5c95 | 2019-07-02 10:53:58 +0200 | [diff] [blame] | 1108 | do { |
| 1109 | ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 1110 | EXT_CSD_PART_CONF, |
| 1111 | (mmc->part_config & ~PART_ACCESS_MASK) |
| 1112 | | (part_num & PART_ACCESS_MASK)); |
| 1113 | } while (ret && retry--); |
Peter Bigot | 45fde89 | 2014-09-02 18:31:23 -0500 | [diff] [blame] | 1114 | |
| 1115 | /* |
| 1116 | * Set the capacity if the switch succeeded or was intended |
| 1117 | * to return to representing the raw device. |
| 1118 | */ |
Stephen Warren | 1e0f92a | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 1119 | if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) { |
Peter Bigot | 45fde89 | 2014-09-02 18:31:23 -0500 | [diff] [blame] | 1120 | ret = mmc_set_capacity(mmc, part_num); |
Simon Glass | 984db5d | 2016-05-01 13:52:37 -0600 | [diff] [blame] | 1121 | mmc_get_blk_desc(mmc)->hwpart = part_num; |
Stephen Warren | 1e0f92a | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 1122 | } |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1123 | |
Peter Bigot | 45fde89 | 2014-09-02 18:31:23 -0500 | [diff] [blame] | 1124 | return ret; |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Jean-Jacques Hiblot | 1d7769a | 2017-11-30 17:44:02 +0100 | [diff] [blame] | 1127 | #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING) |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1128 | int mmc_hwpart_config(struct mmc *mmc, |
| 1129 | const struct mmc_hwpart_conf *conf, |
| 1130 | enum mmc_hwpart_conf_mode mode) |
| 1131 | { |
| 1132 | u8 part_attrs = 0; |
| 1133 | u32 enh_size_mult; |
| 1134 | u32 enh_start_addr; |
| 1135 | u32 gp_size_mult[4]; |
| 1136 | u32 max_enh_size_mult; |
| 1137 | u32 tot_enh_size_mult = 0; |
Diego Santa Cruz | 8020027 | 2014-12-23 10:50:31 +0100 | [diff] [blame] | 1138 | u8 wr_rel_set; |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1139 | int i, pidx, err; |
| 1140 | ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); |
| 1141 | |
| 1142 | if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE) |
| 1143 | return -EINVAL; |
| 1144 | |
| 1145 | if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) { |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 1146 | log_err("eMMC >= 4.4 required for enhanced user data area\n"); |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1147 | return -EMEDIUMTYPE; |
| 1148 | } |
| 1149 | |
| 1150 | if (!(mmc->part_support & PART_SUPPORT)) { |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 1151 | log_err("Card does not support partitioning\n"); |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1152 | return -EMEDIUMTYPE; |
| 1153 | } |
| 1154 | |
| 1155 | if (!mmc->hc_wp_grp_size) { |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 1156 | log_err("Card does not define HC WP group size\n"); |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1157 | return -EMEDIUMTYPE; |
| 1158 | } |
| 1159 | |
| 1160 | /* check partition alignment and total enhanced size */ |
| 1161 | if (conf->user.enh_size) { |
| 1162 | if (conf->user.enh_size % mmc->hc_wp_grp_size || |
| 1163 | conf->user.enh_start % mmc->hc_wp_grp_size) { |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 1164 | log_err("User data enhanced area not HC WP group size aligned\n"); |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1165 | return -EINVAL; |
| 1166 | } |
| 1167 | part_attrs |= EXT_CSD_ENH_USR; |
| 1168 | enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size; |
| 1169 | if (mmc->high_capacity) { |
| 1170 | enh_start_addr = conf->user.enh_start; |
| 1171 | } else { |
| 1172 | enh_start_addr = (conf->user.enh_start << 9); |
| 1173 | } |
| 1174 | } else { |
| 1175 | enh_size_mult = 0; |
| 1176 | enh_start_addr = 0; |
| 1177 | } |
| 1178 | tot_enh_size_mult += enh_size_mult; |
| 1179 | |
| 1180 | for (pidx = 0; pidx < 4; pidx++) { |
| 1181 | if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) { |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 1182 | log_err("GP%i partition not HC WP group-size aligned\n", |
| 1183 | pidx + 1); |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1184 | return -EINVAL; |
| 1185 | } |
| 1186 | gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size; |
| 1187 | if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) { |
| 1188 | part_attrs |= EXT_CSD_ENH_GP(pidx); |
| 1189 | tot_enh_size_mult += gp_size_mult[pidx]; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) { |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 1194 | log_err("Card does not support enhanced attribute\n"); |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1195 | return -EMEDIUMTYPE; |
| 1196 | } |
| 1197 | |
| 1198 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 1199 | if (err) |
| 1200 | return err; |
| 1201 | |
| 1202 | max_enh_size_mult = |
| 1203 | (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) + |
| 1204 | (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) + |
| 1205 | ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]; |
| 1206 | if (tot_enh_size_mult > max_enh_size_mult) { |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 1207 | log_err("Total enhanced size exceeds maximum (%#x > %#x)\n", |
| 1208 | tot_enh_size_mult, max_enh_size_mult); |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1209 | return -EMEDIUMTYPE; |
| 1210 | } |
| 1211 | |
Diego Santa Cruz | 8020027 | 2014-12-23 10:50:31 +0100 | [diff] [blame] | 1212 | /* The default value of EXT_CSD_WR_REL_SET is device |
| 1213 | * dependent, the values can only be changed if the |
| 1214 | * EXT_CSD_HS_CTRL_REL bit is set. The values can be |
| 1215 | * changed only once and before partitioning is completed. */ |
| 1216 | wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; |
| 1217 | if (conf->user.wr_rel_change) { |
| 1218 | if (conf->user.wr_rel_set) |
| 1219 | wr_rel_set |= EXT_CSD_WR_DATA_REL_USR; |
| 1220 | else |
| 1221 | wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR; |
| 1222 | } |
| 1223 | for (pidx = 0; pidx < 4; pidx++) { |
| 1224 | if (conf->gp_part[pidx].wr_rel_change) { |
| 1225 | if (conf->gp_part[pidx].wr_rel_set) |
| 1226 | wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx); |
| 1227 | else |
| 1228 | wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx); |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] && |
| 1233 | !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) { |
| 1234 | puts("Card does not support host controlled partition write " |
| 1235 | "reliability settings\n"); |
| 1236 | return -EMEDIUMTYPE; |
| 1237 | } |
| 1238 | |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1239 | if (ext_csd[EXT_CSD_PARTITION_SETTING] & |
| 1240 | EXT_CSD_PARTITION_SETTING_COMPLETED) { |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 1241 | log_err("Card already partitioned\n"); |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1242 | return -EPERM; |
| 1243 | } |
| 1244 | |
| 1245 | if (mode == MMC_HWPART_CONF_CHECK) |
| 1246 | return 0; |
| 1247 | |
| 1248 | /* Partitioning requires high-capacity size definitions */ |
| 1249 | if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) { |
| 1250 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 1251 | EXT_CSD_ERASE_GROUP_DEF, 1); |
| 1252 | |
| 1253 | if (err) |
| 1254 | return err; |
| 1255 | |
| 1256 | ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1; |
| 1257 | |
Jaehoon Chung | 58b9eb8 | 2020-01-17 15:06:54 +0900 | [diff] [blame] | 1258 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1259 | /* update erase group size to be high-capacity */ |
| 1260 | mmc->erase_grp_size = |
| 1261 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024; |
Jaehoon Chung | 58b9eb8 | 2020-01-17 15:06:54 +0900 | [diff] [blame] | 1262 | #endif |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1263 | |
| 1264 | } |
| 1265 | |
| 1266 | /* all OK, write the configuration */ |
| 1267 | for (i = 0; i < 4; i++) { |
| 1268 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 1269 | EXT_CSD_ENH_START_ADDR+i, |
| 1270 | (enh_start_addr >> (i*8)) & 0xFF); |
| 1271 | if (err) |
| 1272 | return err; |
| 1273 | } |
| 1274 | for (i = 0; i < 3; i++) { |
| 1275 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 1276 | EXT_CSD_ENH_SIZE_MULT+i, |
| 1277 | (enh_size_mult >> (i*8)) & 0xFF); |
| 1278 | if (err) |
| 1279 | return err; |
| 1280 | } |
| 1281 | for (pidx = 0; pidx < 4; pidx++) { |
| 1282 | for (i = 0; i < 3; i++) { |
| 1283 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 1284 | EXT_CSD_GP_SIZE_MULT+pidx*3+i, |
| 1285 | (gp_size_mult[pidx] >> (i*8)) & 0xFF); |
| 1286 | if (err) |
| 1287 | return err; |
| 1288 | } |
| 1289 | } |
| 1290 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 1291 | EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs); |
| 1292 | if (err) |
| 1293 | return err; |
| 1294 | |
| 1295 | if (mode == MMC_HWPART_CONF_SET) |
| 1296 | return 0; |
| 1297 | |
Diego Santa Cruz | 8020027 | 2014-12-23 10:50:31 +0100 | [diff] [blame] | 1298 | /* The WR_REL_SET is a write-once register but shall be |
| 1299 | * written before setting PART_SETTING_COMPLETED. As it is |
| 1300 | * write-once we can only write it when completing the |
| 1301 | * partitioning. */ |
| 1302 | if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) { |
| 1303 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 1304 | EXT_CSD_WR_REL_SET, wr_rel_set); |
| 1305 | if (err) |
| 1306 | return err; |
| 1307 | } |
| 1308 | |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1309 | /* Setting PART_SETTING_COMPLETED confirms the partition |
| 1310 | * configuration but it only becomes effective after power |
| 1311 | * cycle, so we do not adjust the partition related settings |
| 1312 | * in the mmc struct. */ |
| 1313 | |
| 1314 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 1315 | EXT_CSD_PARTITION_SETTING, |
| 1316 | EXT_CSD_PARTITION_SETTING_COMPLETED); |
| 1317 | if (err) |
| 1318 | return err; |
| 1319 | |
| 1320 | return 0; |
| 1321 | } |
Jean-Jacques Hiblot | 1d7769a | 2017-11-30 17:44:02 +0100 | [diff] [blame] | 1322 | #endif |
Diego Santa Cruz | 69eb71a0 | 2014-12-23 10:50:29 +0100 | [diff] [blame] | 1323 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 1324 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Thierry Reding | b9c8b77 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 1325 | int mmc_getcd(struct mmc *mmc) |
| 1326 | { |
| 1327 | int cd; |
| 1328 | |
| 1329 | cd = board_mmc_getcd(mmc); |
| 1330 | |
Peter Korsgaard | f7b1510 | 2013-03-21 04:00:03 +0000 | [diff] [blame] | 1331 | if (cd < 0) { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1332 | if (mmc->cfg->ops->getcd) |
| 1333 | cd = mmc->cfg->ops->getcd(mmc); |
Peter Korsgaard | f7b1510 | 2013-03-21 04:00:03 +0000 | [diff] [blame] | 1334 | else |
| 1335 | cd = 1; |
| 1336 | } |
Thierry Reding | b9c8b77 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 1337 | |
| 1338 | return cd; |
| 1339 | } |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 1340 | #endif |
Thierry Reding | b9c8b77 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 1341 | |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 1342 | #if !CONFIG_IS_ENABLED(MMC_TINY) |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 1343 | static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1344 | { |
| 1345 | struct mmc_cmd cmd; |
| 1346 | struct mmc_data data; |
| 1347 | |
| 1348 | /* Switch the frequency */ |
| 1349 | cmd.cmdidx = SD_CMD_SWITCH_FUNC; |
| 1350 | cmd.resp_type = MMC_RSP_R1; |
| 1351 | cmd.cmdarg = (mode << 31) | 0xffffff; |
| 1352 | cmd.cmdarg &= ~(0xf << (group * 4)); |
| 1353 | cmd.cmdarg |= value << (group * 4); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1354 | |
| 1355 | data.dest = (char *)resp; |
| 1356 | data.blocksize = 64; |
| 1357 | data.blocks = 1; |
| 1358 | data.flags = MMC_DATA_READ; |
| 1359 | |
| 1360 | return mmc_send_cmd(mmc, &cmd, &data); |
| 1361 | } |
| 1362 | |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1363 | static int sd_get_capabilities(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1364 | { |
| 1365 | int err; |
| 1366 | struct mmc_cmd cmd; |
Suniel Mahesh | 2f423da | 2017-10-05 11:32:00 +0530 | [diff] [blame] | 1367 | ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2); |
| 1368 | ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1369 | struct mmc_data data; |
| 1370 | int timeout; |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1371 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1372 | u32 sd3_bus_mode; |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1373 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1374 | |
Faiz Abbas | 01db77e | 2020-02-26 13:44:32 +0530 | [diff] [blame] | 1375 | mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1376 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1377 | if (mmc_host_is_spi(mmc)) |
| 1378 | return 0; |
| 1379 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1380 | /* Read the SCR to find out if this card supports higher speeds */ |
| 1381 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 1382 | cmd.resp_type = MMC_RSP_R1; |
| 1383 | cmd.cmdarg = mmc->rca << 16; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1384 | |
| 1385 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1386 | |
| 1387 | if (err) |
| 1388 | return err; |
| 1389 | |
| 1390 | cmd.cmdidx = SD_CMD_APP_SEND_SCR; |
| 1391 | cmd.resp_type = MMC_RSP_R1; |
| 1392 | cmd.cmdarg = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1393 | |
Anton staaf | 9b00f0d | 2011-10-03 13:54:59 +0000 | [diff] [blame] | 1394 | data.dest = (char *)scr; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1395 | data.blocksize = 8; |
| 1396 | data.blocks = 1; |
| 1397 | data.flags = MMC_DATA_READ; |
| 1398 | |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 1399 | err = mmc_send_cmd_retry(mmc, &cmd, &data, 3); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1400 | |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 1401 | if (err) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1402 | return err; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1403 | |
Yauhen Kharuzhy | 6e8edf4 | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 1404 | mmc->scr[0] = __be32_to_cpu(scr[0]); |
| 1405 | mmc->scr[1] = __be32_to_cpu(scr[1]); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1406 | |
| 1407 | switch ((mmc->scr[0] >> 24) & 0xf) { |
Bin Meng | 4a4ef87 | 2016-03-17 21:53:13 -0700 | [diff] [blame] | 1408 | case 0: |
| 1409 | mmc->version = SD_VERSION_1_0; |
| 1410 | break; |
| 1411 | case 1: |
| 1412 | mmc->version = SD_VERSION_1_10; |
| 1413 | break; |
| 1414 | case 2: |
| 1415 | mmc->version = SD_VERSION_2; |
| 1416 | if ((mmc->scr[0] >> 15) & 0x1) |
| 1417 | mmc->version = SD_VERSION_3; |
| 1418 | break; |
| 1419 | default: |
| 1420 | mmc->version = SD_VERSION_1_0; |
| 1421 | break; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1422 | } |
| 1423 | |
Alagu Sankar | 24bb5ab | 2010-05-12 15:08:24 +0530 | [diff] [blame] | 1424 | if (mmc->scr[0] & SD_DATA_4BIT) |
| 1425 | mmc->card_caps |= MMC_MODE_4BIT; |
| 1426 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1427 | /* Version 1.0 doesn't support switching */ |
| 1428 | if (mmc->version == SD_VERSION_1_0) |
| 1429 | return 0; |
| 1430 | |
| 1431 | timeout = 4; |
| 1432 | while (timeout--) { |
| 1433 | err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, |
Anton staaf | 9b00f0d | 2011-10-03 13:54:59 +0000 | [diff] [blame] | 1434 | (u8 *)switch_status); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1435 | |
| 1436 | if (err) |
| 1437 | return err; |
| 1438 | |
| 1439 | /* The high-speed function is busy. Try again */ |
Yauhen Kharuzhy | 6e8edf4 | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 1440 | if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1441 | break; |
| 1442 | } |
| 1443 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1444 | /* If high-speed isn't supported, we return */ |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1445 | if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED) |
| 1446 | mmc->card_caps |= MMC_CAP(SD_HS); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1447 | |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1448 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1449 | /* Version before 3.0 don't support UHS modes */ |
| 1450 | if (mmc->version < SD_VERSION_3) |
| 1451 | return 0; |
| 1452 | |
| 1453 | sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f; |
| 1454 | if (sd3_bus_mode & SD_MODE_UHS_SDR104) |
| 1455 | mmc->card_caps |= MMC_CAP(UHS_SDR104); |
| 1456 | if (sd3_bus_mode & SD_MODE_UHS_SDR50) |
| 1457 | mmc->card_caps |= MMC_CAP(UHS_SDR50); |
| 1458 | if (sd3_bus_mode & SD_MODE_UHS_SDR25) |
| 1459 | mmc->card_caps |= MMC_CAP(UHS_SDR25); |
| 1460 | if (sd3_bus_mode & SD_MODE_UHS_SDR12) |
| 1461 | mmc->card_caps |= MMC_CAP(UHS_SDR12); |
| 1462 | if (sd3_bus_mode & SD_MODE_UHS_DDR50) |
| 1463 | mmc->card_caps |= MMC_CAP(UHS_DDR50); |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1464 | #endif |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1465 | |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode) |
| 1470 | { |
| 1471 | int err; |
| 1472 | |
| 1473 | ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16); |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1474 | int speed; |
Macpaul Lin | 24e92ec | 2011-11-28 16:31:09 +0000 | [diff] [blame] | 1475 | |
Marek Vasut | 4105e97 | 2018-11-18 03:25:08 +0100 | [diff] [blame] | 1476 | /* SD version 1.00 and 1.01 does not support CMD 6 */ |
| 1477 | if (mmc->version == SD_VERSION_1_0) |
| 1478 | return 0; |
| 1479 | |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1480 | switch (mode) { |
Faiz Abbas | 01db77e | 2020-02-26 13:44:32 +0530 | [diff] [blame] | 1481 | case MMC_LEGACY: |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1482 | speed = UHS_SDR12_BUS_SPEED; |
| 1483 | break; |
| 1484 | case SD_HS: |
Jean-Jacques Hiblot | 74c98b2 | 2018-01-04 15:23:30 +0100 | [diff] [blame] | 1485 | speed = HIGH_SPEED_BUS_SPEED; |
| 1486 | break; |
| 1487 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
| 1488 | case UHS_SDR12: |
| 1489 | speed = UHS_SDR12_BUS_SPEED; |
| 1490 | break; |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1491 | case UHS_SDR25: |
| 1492 | speed = UHS_SDR25_BUS_SPEED; |
| 1493 | break; |
| 1494 | case UHS_SDR50: |
| 1495 | speed = UHS_SDR50_BUS_SPEED; |
| 1496 | break; |
| 1497 | case UHS_DDR50: |
| 1498 | speed = UHS_DDR50_BUS_SPEED; |
| 1499 | break; |
| 1500 | case UHS_SDR104: |
| 1501 | speed = UHS_SDR104_BUS_SPEED; |
| 1502 | break; |
Jean-Jacques Hiblot | 74c98b2 | 2018-01-04 15:23:30 +0100 | [diff] [blame] | 1503 | #endif |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1504 | default: |
| 1505 | return -EINVAL; |
| 1506 | } |
| 1507 | |
| 1508 | err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status); |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1509 | if (err) |
| 1510 | return err; |
| 1511 | |
Jean-Jacques Hiblot | e7f664e | 2018-02-09 12:09:27 +0100 | [diff] [blame] | 1512 | if (((__be32_to_cpu(switch_status[4]) >> 24) & 0xF) != speed) |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1513 | return -ENOTSUPP; |
| 1514 | |
| 1515 | return 0; |
| 1516 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1517 | |
Marek Vasut | 8ff55fb | 2018-04-15 00:36:45 +0200 | [diff] [blame] | 1518 | static int sd_select_bus_width(struct mmc *mmc, int w) |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1519 | { |
| 1520 | int err; |
| 1521 | struct mmc_cmd cmd; |
| 1522 | |
| 1523 | if ((w != 4) && (w != 1)) |
| 1524 | return -EINVAL; |
| 1525 | |
| 1526 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 1527 | cmd.resp_type = MMC_RSP_R1; |
| 1528 | cmd.cmdarg = mmc->rca << 16; |
| 1529 | |
| 1530 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1531 | if (err) |
| 1532 | return err; |
| 1533 | |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1534 | cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; |
| 1535 | cmd.resp_type = MMC_RSP_R1; |
| 1536 | if (w == 4) |
| 1537 | cmd.cmdarg = 2; |
| 1538 | else if (w == 1) |
| 1539 | cmd.cmdarg = 0; |
| 1540 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1541 | if (err) |
| 1542 | return err; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1543 | |
| 1544 | return 0; |
| 1545 | } |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 1546 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1547 | |
Jean-Jacques Hiblot | cb534f0 | 2018-01-04 15:23:33 +0100 | [diff] [blame] | 1548 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Peng Fan | b3fcf1e | 2016-09-01 11:13:38 +0800 | [diff] [blame] | 1549 | static int sd_read_ssr(struct mmc *mmc) |
| 1550 | { |
Jean-Jacques Hiblot | cb534f0 | 2018-01-04 15:23:33 +0100 | [diff] [blame] | 1551 | static const unsigned int sd_au_size[] = { |
| 1552 | 0, SZ_16K / 512, SZ_32K / 512, |
| 1553 | SZ_64K / 512, SZ_128K / 512, SZ_256K / 512, |
| 1554 | SZ_512K / 512, SZ_1M / 512, SZ_2M / 512, |
| 1555 | SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512, |
| 1556 | SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512, |
| 1557 | SZ_64M / 512, |
| 1558 | }; |
Peng Fan | b3fcf1e | 2016-09-01 11:13:38 +0800 | [diff] [blame] | 1559 | int err, i; |
| 1560 | struct mmc_cmd cmd; |
| 1561 | ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16); |
| 1562 | struct mmc_data data; |
Peng Fan | b3fcf1e | 2016-09-01 11:13:38 +0800 | [diff] [blame] | 1563 | unsigned int au, eo, et, es; |
| 1564 | |
| 1565 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 1566 | cmd.resp_type = MMC_RSP_R1; |
| 1567 | cmd.cmdarg = mmc->rca << 16; |
| 1568 | |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 1569 | err = mmc_send_cmd_quirks(mmc, &cmd, NULL, MMC_QUIRK_RETRY_APP_CMD, 4); |
Peng Fan | b3fcf1e | 2016-09-01 11:13:38 +0800 | [diff] [blame] | 1570 | if (err) |
| 1571 | return err; |
| 1572 | |
| 1573 | cmd.cmdidx = SD_CMD_APP_SD_STATUS; |
| 1574 | cmd.resp_type = MMC_RSP_R1; |
| 1575 | cmd.cmdarg = 0; |
| 1576 | |
Peng Fan | b3fcf1e | 2016-09-01 11:13:38 +0800 | [diff] [blame] | 1577 | data.dest = (char *)ssr; |
| 1578 | data.blocksize = 64; |
| 1579 | data.blocks = 1; |
| 1580 | data.flags = MMC_DATA_READ; |
| 1581 | |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 1582 | err = mmc_send_cmd_retry(mmc, &cmd, &data, 3); |
| 1583 | if (err) |
Peng Fan | b3fcf1e | 2016-09-01 11:13:38 +0800 | [diff] [blame] | 1584 | return err; |
Peng Fan | b3fcf1e | 2016-09-01 11:13:38 +0800 | [diff] [blame] | 1585 | |
| 1586 | for (i = 0; i < 16; i++) |
| 1587 | ssr[i] = be32_to_cpu(ssr[i]); |
| 1588 | |
| 1589 | au = (ssr[2] >> 12) & 0xF; |
| 1590 | if ((au <= 9) || (mmc->version == SD_VERSION_3)) { |
| 1591 | mmc->ssr.au = sd_au_size[au]; |
| 1592 | es = (ssr[3] >> 24) & 0xFF; |
| 1593 | es |= (ssr[2] & 0xFF) << 8; |
| 1594 | et = (ssr[3] >> 18) & 0x3F; |
| 1595 | if (es && et) { |
| 1596 | eo = (ssr[3] >> 16) & 0x3; |
| 1597 | mmc->ssr.erase_timeout = (et * 1000) / es; |
| 1598 | mmc->ssr.erase_offset = eo * 1000; |
| 1599 | } |
| 1600 | } else { |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 1601 | pr_debug("Invalid Allocation Unit Size.\n"); |
Peng Fan | b3fcf1e | 2016-09-01 11:13:38 +0800 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | return 0; |
| 1605 | } |
Jean-Jacques Hiblot | cb534f0 | 2018-01-04 15:23:33 +0100 | [diff] [blame] | 1606 | #endif |
Heinrich Schuchardt | 82479d4 | 2024-01-04 04:49:42 +0100 | [diff] [blame] | 1607 | /* |
| 1608 | * TRAN_SPEED bits 0:2 encode the frequency unit: |
| 1609 | * 0 = 100KHz, 1 = 1MHz, 2 = 10MHz, 3 = 100MHz, values 4 - 7 are reserved. |
| 1610 | * The values in fbase[] are divided by 10 to avoid floats in multiplier[]. |
| 1611 | */ |
Mike Frysinger | b588caf | 2010-10-20 01:15:53 +0000 | [diff] [blame] | 1612 | static const int fbase[] = { |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1613 | 10000, |
| 1614 | 100000, |
| 1615 | 1000000, |
| 1616 | 10000000, |
Heinrich Schuchardt | 82479d4 | 2024-01-04 04:49:42 +0100 | [diff] [blame] | 1617 | 0, /* reserved */ |
| 1618 | 0, /* reserved */ |
| 1619 | 0, /* reserved */ |
| 1620 | 0, /* reserved */ |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1621 | }; |
| 1622 | |
| 1623 | /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice |
| 1624 | * to platforms without floating point. |
| 1625 | */ |
Simon Glass | 03317cc | 2016-05-14 14:02:57 -0600 | [diff] [blame] | 1626 | static const u8 multipliers[] = { |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1627 | 0, /* reserved */ |
| 1628 | 10, |
| 1629 | 12, |
| 1630 | 13, |
| 1631 | 15, |
| 1632 | 20, |
| 1633 | 25, |
| 1634 | 30, |
| 1635 | 35, |
| 1636 | 40, |
| 1637 | 45, |
| 1638 | 50, |
| 1639 | 55, |
| 1640 | 60, |
| 1641 | 70, |
| 1642 | 80, |
| 1643 | }; |
| 1644 | |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1645 | static inline int bus_width(uint cap) |
| 1646 | { |
| 1647 | if (cap == MMC_MODE_8BIT) |
| 1648 | return 8; |
| 1649 | if (cap == MMC_MODE_4BIT) |
| 1650 | return 4; |
| 1651 | if (cap == MMC_MODE_1BIT) |
| 1652 | return 1; |
Jean-Jacques Hiblot | 678b608 | 2017-11-30 17:44:00 +0100 | [diff] [blame] | 1653 | pr_warn("invalid bus witdh capability 0x%x\n", cap); |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1654 | return 0; |
| 1655 | } |
| 1656 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 1657 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 1658 | #if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING) |
Kishon Vijay Abraham I | ae7174f | 2017-09-21 16:30:05 +0200 | [diff] [blame] | 1659 | static int mmc_execute_tuning(struct mmc *mmc, uint opcode) |
| 1660 | { |
| 1661 | return -ENOTSUPP; |
| 1662 | } |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1663 | #endif |
Kishon Vijay Abraham I | ae7174f | 2017-09-21 16:30:05 +0200 | [diff] [blame] | 1664 | |
Kishon Vijay Abraham I | e178c11 | 2017-09-21 16:29:59 +0200 | [diff] [blame] | 1665 | static int mmc_set_ios(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1666 | { |
Kishon Vijay Abraham I | e178c11 | 2017-09-21 16:29:59 +0200 | [diff] [blame] | 1667 | int ret = 0; |
| 1668 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1669 | if (mmc->cfg->ops->set_ios) |
Kishon Vijay Abraham I | e178c11 | 2017-09-21 16:29:59 +0200 | [diff] [blame] | 1670 | ret = mmc->cfg->ops->set_ios(mmc); |
| 1671 | |
| 1672 | return ret; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1673 | } |
Yann Gautier | 6f55833 | 2019-09-19 17:56:12 +0200 | [diff] [blame] | 1674 | |
| 1675 | static int mmc_host_power_cycle(struct mmc *mmc) |
| 1676 | { |
| 1677 | int ret = 0; |
| 1678 | |
| 1679 | if (mmc->cfg->ops->host_power_cycle) |
| 1680 | ret = mmc->cfg->ops->host_power_cycle(mmc); |
| 1681 | |
| 1682 | return ret; |
| 1683 | } |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 1684 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1685 | |
Kishon Vijay Abraham I | d6246bf | 2017-09-21 16:30:03 +0200 | [diff] [blame] | 1686 | int mmc_set_clock(struct mmc *mmc, uint clock, bool disable) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1687 | { |
Jaehoon Chung | ab4d405 | 2018-01-23 14:04:30 +0900 | [diff] [blame] | 1688 | if (!disable) { |
Jaehoon Chung | 8a93329 | 2018-01-17 19:36:58 +0900 | [diff] [blame] | 1689 | if (clock > mmc->cfg->f_max) |
| 1690 | clock = mmc->cfg->f_max; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1691 | |
Jaehoon Chung | 8a93329 | 2018-01-17 19:36:58 +0900 | [diff] [blame] | 1692 | if (clock < mmc->cfg->f_min) |
| 1693 | clock = mmc->cfg->f_min; |
| 1694 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1695 | |
| 1696 | mmc->clock = clock; |
Kishon Vijay Abraham I | d6246bf | 2017-09-21 16:30:03 +0200 | [diff] [blame] | 1697 | mmc->clk_disable = disable; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1698 | |
Jaehoon Chung | c8477d6 | 2018-01-26 19:25:30 +0900 | [diff] [blame] | 1699 | debug("clock is %s (%dHz)\n", disable ? "disabled" : "enabled", clock); |
| 1700 | |
Kishon Vijay Abraham I | e178c11 | 2017-09-21 16:29:59 +0200 | [diff] [blame] | 1701 | return mmc_set_ios(mmc); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1702 | } |
| 1703 | |
Kishon Vijay Abraham I | e178c11 | 2017-09-21 16:29:59 +0200 | [diff] [blame] | 1704 | static int mmc_set_bus_width(struct mmc *mmc, uint width) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1705 | { |
| 1706 | mmc->bus_width = width; |
| 1707 | |
Kishon Vijay Abraham I | e178c11 | 2017-09-21 16:29:59 +0200 | [diff] [blame] | 1708 | return mmc_set_ios(mmc); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1709 | } |
| 1710 | |
Jean-Jacques Hiblot | 00de504 | 2017-09-21 16:29:54 +0200 | [diff] [blame] | 1711 | #if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG) |
| 1712 | /* |
| 1713 | * helper function to display the capabilities in a human |
| 1714 | * friendly manner. The capabilities include bus width and |
| 1715 | * supported modes. |
| 1716 | */ |
| 1717 | void mmc_dump_capabilities(const char *text, uint caps) |
| 1718 | { |
| 1719 | enum bus_mode mode; |
| 1720 | |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 1721 | pr_debug("%s: widths [", text); |
Jean-Jacques Hiblot | 00de504 | 2017-09-21 16:29:54 +0200 | [diff] [blame] | 1722 | if (caps & MMC_MODE_8BIT) |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 1723 | pr_debug("8, "); |
Jean-Jacques Hiblot | 00de504 | 2017-09-21 16:29:54 +0200 | [diff] [blame] | 1724 | if (caps & MMC_MODE_4BIT) |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 1725 | pr_debug("4, "); |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1726 | if (caps & MMC_MODE_1BIT) |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 1727 | pr_debug("1, "); |
| 1728 | pr_debug("\b\b] modes ["); |
Jean-Jacques Hiblot | 00de504 | 2017-09-21 16:29:54 +0200 | [diff] [blame] | 1729 | for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++) |
| 1730 | if (MMC_CAP(mode) & caps) |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 1731 | pr_debug("%s, ", mmc_mode_name(mode)); |
| 1732 | pr_debug("\b\b]\n"); |
Jean-Jacques Hiblot | 00de504 | 2017-09-21 16:29:54 +0200 | [diff] [blame] | 1733 | } |
| 1734 | #endif |
| 1735 | |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1736 | struct mode_width_tuning { |
| 1737 | enum bus_mode mode; |
| 1738 | uint widths; |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 1739 | #if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING) |
Kishon Vijay Abraham I | 210369f | 2017-09-21 16:30:06 +0200 | [diff] [blame] | 1740 | uint tuning; |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1741 | #endif |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1742 | }; |
| 1743 | |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1744 | #if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE) |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 1745 | int mmc_voltage_to_mv(enum mmc_voltage voltage) |
| 1746 | { |
| 1747 | switch (voltage) { |
| 1748 | case MMC_SIGNAL_VOLTAGE_000: return 0; |
| 1749 | case MMC_SIGNAL_VOLTAGE_330: return 3300; |
| 1750 | case MMC_SIGNAL_VOLTAGE_180: return 1800; |
| 1751 | case MMC_SIGNAL_VOLTAGE_120: return 1200; |
| 1752 | } |
| 1753 | return -EINVAL; |
| 1754 | } |
| 1755 | |
Kishon Vijay Abraham I | 4afb12b | 2017-09-21 16:30:00 +0200 | [diff] [blame] | 1756 | static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage) |
| 1757 | { |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 1758 | int err; |
| 1759 | |
| 1760 | if (mmc->signal_voltage == signal_voltage) |
| 1761 | return 0; |
| 1762 | |
Kishon Vijay Abraham I | 4afb12b | 2017-09-21 16:30:00 +0200 | [diff] [blame] | 1763 | mmc->signal_voltage = signal_voltage; |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 1764 | err = mmc_set_ios(mmc); |
| 1765 | if (err) |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 1766 | pr_debug("unable to set voltage (err %d)\n", err); |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 1767 | |
| 1768 | return err; |
Kishon Vijay Abraham I | 4afb12b | 2017-09-21 16:30:00 +0200 | [diff] [blame] | 1769 | } |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1770 | #else |
| 1771 | static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage) |
| 1772 | { |
| 1773 | return 0; |
| 1774 | } |
| 1775 | #endif |
Kishon Vijay Abraham I | 4afb12b | 2017-09-21 16:30:00 +0200 | [diff] [blame] | 1776 | |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 1777 | #if !CONFIG_IS_ENABLED(MMC_TINY) |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1778 | static const struct mode_width_tuning sd_modes_by_pref[] = { |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1779 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 1780 | #if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING) |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1781 | { |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1782 | .mode = UHS_SDR104, |
| 1783 | .widths = MMC_MODE_4BIT | MMC_MODE_1BIT, |
| 1784 | .tuning = MMC_CMD_SEND_TUNING_BLOCK |
| 1785 | }, |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1786 | #endif |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1787 | { |
| 1788 | .mode = UHS_SDR50, |
| 1789 | .widths = MMC_MODE_4BIT | MMC_MODE_1BIT, |
| 1790 | }, |
| 1791 | { |
| 1792 | .mode = UHS_DDR50, |
| 1793 | .widths = MMC_MODE_4BIT | MMC_MODE_1BIT, |
| 1794 | }, |
| 1795 | { |
| 1796 | .mode = UHS_SDR25, |
| 1797 | .widths = MMC_MODE_4BIT | MMC_MODE_1BIT, |
| 1798 | }, |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1799 | #endif |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1800 | { |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1801 | .mode = SD_HS, |
| 1802 | .widths = MMC_MODE_4BIT | MMC_MODE_1BIT, |
| 1803 | }, |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1804 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1805 | { |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1806 | .mode = UHS_SDR12, |
| 1807 | .widths = MMC_MODE_4BIT | MMC_MODE_1BIT, |
| 1808 | }, |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1809 | #endif |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1810 | { |
Faiz Abbas | 01db77e | 2020-02-26 13:44:32 +0530 | [diff] [blame] | 1811 | .mode = MMC_LEGACY, |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1812 | .widths = MMC_MODE_4BIT | MMC_MODE_1BIT, |
| 1813 | } |
| 1814 | }; |
| 1815 | |
| 1816 | #define for_each_sd_mode_by_pref(caps, mwt) \ |
| 1817 | for (mwt = sd_modes_by_pref;\ |
| 1818 | mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\ |
| 1819 | mwt++) \ |
| 1820 | if (caps & MMC_CAP(mwt->mode)) |
| 1821 | |
Jean-Jacques Hiblot | 3d30972b | 2017-09-21 16:30:09 +0200 | [diff] [blame] | 1822 | static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps) |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1823 | { |
| 1824 | int err; |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1825 | uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT}; |
| 1826 | const struct mode_width_tuning *mwt; |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1827 | #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1828 | bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false; |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1829 | #else |
| 1830 | bool uhs_en = false; |
| 1831 | #endif |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1832 | uint caps; |
| 1833 | |
Jean-Jacques Hiblot | 93c31d1 | 2017-11-30 17:43:54 +0100 | [diff] [blame] | 1834 | #ifdef DEBUG |
| 1835 | mmc_dump_capabilities("sd card", card_caps); |
Jean-Jacques Hiblot | d7e5e03 | 2017-11-30 17:43:57 +0100 | [diff] [blame] | 1836 | mmc_dump_capabilities("host", mmc->host_caps); |
Jean-Jacques Hiblot | 93c31d1 | 2017-11-30 17:43:54 +0100 | [diff] [blame] | 1837 | #endif |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1838 | |
Anup Patel | d9c92c7 | 2019-07-08 04:10:43 +0000 | [diff] [blame] | 1839 | if (mmc_host_is_spi(mmc)) { |
| 1840 | mmc_set_bus_width(mmc, 1); |
Faiz Abbas | 01db77e | 2020-02-26 13:44:32 +0530 | [diff] [blame] | 1841 | mmc_select_mode(mmc, MMC_LEGACY); |
Anup Patel | d9c92c7 | 2019-07-08 04:10:43 +0000 | [diff] [blame] | 1842 | mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE); |
Pragnesh Patel | a01f57e | 2020-06-29 15:17:26 +0530 | [diff] [blame] | 1843 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
| 1844 | err = sd_read_ssr(mmc); |
| 1845 | if (err) |
| 1846 | pr_warn("unable to read ssr\n"); |
| 1847 | #endif |
Anup Patel | d9c92c7 | 2019-07-08 04:10:43 +0000 | [diff] [blame] | 1848 | return 0; |
| 1849 | } |
| 1850 | |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1851 | /* Restrict card's capabilities by what the host can do */ |
Jean-Jacques Hiblot | d7e5e03 | 2017-11-30 17:43:57 +0100 | [diff] [blame] | 1852 | caps = card_caps & mmc->host_caps; |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1853 | |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1854 | if (!uhs_en) |
| 1855 | caps &= ~UHS_CAPS; |
| 1856 | |
| 1857 | for_each_sd_mode_by_pref(caps, mwt) { |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1858 | uint *w; |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1859 | |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1860 | for (w = widths; w < widths + ARRAY_SIZE(widths); w++) { |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1861 | if (*w & caps & mwt->widths) { |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 1862 | pr_debug("trying mode %s width %d (at %d MHz)\n", |
| 1863 | mmc_mode_name(mwt->mode), |
| 1864 | bus_width(*w), |
| 1865 | mmc_mode2freq(mmc, mwt->mode) / 1000000); |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1866 | |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1867 | /* configure the bus width (card + host) */ |
| 1868 | err = sd_select_bus_width(mmc, bus_width(*w)); |
| 1869 | if (err) |
| 1870 | goto error; |
| 1871 | mmc_set_bus_width(mmc, bus_width(*w)); |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1872 | |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1873 | /* configure the bus mode (card) */ |
| 1874 | err = sd_set_card_speed(mmc, mwt->mode); |
| 1875 | if (err) |
| 1876 | goto error; |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1877 | |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1878 | /* configure the bus mode (host) */ |
| 1879 | mmc_select_mode(mmc, mwt->mode); |
Jaehoon Chung | 239cb2f | 2018-01-26 19:25:29 +0900 | [diff] [blame] | 1880 | mmc_set_clock(mmc, mmc->tran_speed, |
| 1881 | MMC_CLK_ENABLE); |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1882 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 1883 | #if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING) |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1884 | /* execute tuning if needed */ |
| 1885 | if (mwt->tuning && !mmc_host_is_spi(mmc)) { |
| 1886 | err = mmc_execute_tuning(mmc, |
| 1887 | mwt->tuning); |
| 1888 | if (err) { |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 1889 | pr_debug("tuning failed\n"); |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1890 | goto error; |
| 1891 | } |
| 1892 | } |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1893 | #endif |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 1894 | |
Jean-Jacques Hiblot | cb534f0 | 2018-01-04 15:23:33 +0100 | [diff] [blame] | 1895 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1896 | err = sd_read_ssr(mmc); |
Peng Fan | 2d2fe8e | 2018-03-05 16:20:40 +0800 | [diff] [blame] | 1897 | if (err) |
Jean-Jacques Hiblot | cb534f0 | 2018-01-04 15:23:33 +0100 | [diff] [blame] | 1898 | pr_warn("unable to read ssr\n"); |
| 1899 | #endif |
| 1900 | if (!err) |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1901 | return 0; |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1902 | |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1903 | error: |
| 1904 | /* revert to a safer bus speed */ |
Faiz Abbas | 01db77e | 2020-02-26 13:44:32 +0530 | [diff] [blame] | 1905 | mmc_select_mode(mmc, MMC_LEGACY); |
Jaehoon Chung | 239cb2f | 2018-01-26 19:25:29 +0900 | [diff] [blame] | 1906 | mmc_set_clock(mmc, mmc->tran_speed, |
| 1907 | MMC_CLK_ENABLE); |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 1912 | log_err("unable to select a mode\n"); |
Jean-Jacques Hiblot | 5b1a4d9 | 2017-09-21 16:29:57 +0200 | [diff] [blame] | 1913 | return -ENOTSUPP; |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1914 | } |
| 1915 | |
Jean-Jacques Hiblot | 933d126 | 2017-09-21 16:29:52 +0200 | [diff] [blame] | 1916 | /* |
| 1917 | * read the compare the part of ext csd that is constant. |
| 1918 | * This can be used to check that the transfer is working |
| 1919 | * as expected. |
| 1920 | */ |
| 1921 | static int mmc_read_and_compare_ext_csd(struct mmc *mmc) |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 1922 | { |
Jean-Jacques Hiblot | 933d126 | 2017-09-21 16:29:52 +0200 | [diff] [blame] | 1923 | int err; |
Jean-Jacques Hiblot | ed9506b | 2017-09-21 16:29:51 +0200 | [diff] [blame] | 1924 | const u8 *ext_csd = mmc->ext_csd; |
Jean-Jacques Hiblot | 933d126 | 2017-09-21 16:29:52 +0200 | [diff] [blame] | 1925 | ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN); |
| 1926 | |
Jean-Jacques Hiblot | 7ab1b62 | 2017-11-30 17:43:58 +0100 | [diff] [blame] | 1927 | if (mmc->version < MMC_VERSION_4) |
| 1928 | return 0; |
| 1929 | |
Jean-Jacques Hiblot | 933d126 | 2017-09-21 16:29:52 +0200 | [diff] [blame] | 1930 | err = mmc_send_ext_csd(mmc, test_csd); |
| 1931 | if (err) |
| 1932 | return err; |
| 1933 | |
| 1934 | /* Only compare read only fields */ |
| 1935 | if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] |
| 1936 | == test_csd[EXT_CSD_PARTITIONING_SUPPORT] && |
| 1937 | ext_csd[EXT_CSD_HC_WP_GRP_SIZE] |
| 1938 | == test_csd[EXT_CSD_HC_WP_GRP_SIZE] && |
| 1939 | ext_csd[EXT_CSD_REV] |
| 1940 | == test_csd[EXT_CSD_REV] && |
| 1941 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] |
| 1942 | == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] && |
| 1943 | memcmp(&ext_csd[EXT_CSD_SEC_CNT], |
| 1944 | &test_csd[EXT_CSD_SEC_CNT], 4) == 0) |
| 1945 | return 0; |
| 1946 | |
| 1947 | return -EBADMSG; |
| 1948 | } |
| 1949 | |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1950 | #if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE) |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 1951 | static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode, |
| 1952 | uint32_t allowed_mask) |
| 1953 | { |
| 1954 | u32 card_mask = 0; |
| 1955 | |
| 1956 | switch (mode) { |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 1957 | case MMC_HS_400_ES: |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 1958 | case MMC_HS_400: |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 1959 | case MMC_HS_200: |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 1960 | if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_8V | |
| 1961 | EXT_CSD_CARD_TYPE_HS400_1_8V)) |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 1962 | card_mask |= MMC_SIGNAL_VOLTAGE_180; |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 1963 | if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V | |
| 1964 | EXT_CSD_CARD_TYPE_HS400_1_2V)) |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 1965 | card_mask |= MMC_SIGNAL_VOLTAGE_120; |
| 1966 | break; |
| 1967 | case MMC_DDR_52: |
| 1968 | if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V) |
| 1969 | card_mask |= MMC_SIGNAL_VOLTAGE_330 | |
| 1970 | MMC_SIGNAL_VOLTAGE_180; |
| 1971 | if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V) |
| 1972 | card_mask |= MMC_SIGNAL_VOLTAGE_120; |
| 1973 | break; |
| 1974 | default: |
| 1975 | card_mask |= MMC_SIGNAL_VOLTAGE_330; |
| 1976 | break; |
| 1977 | } |
| 1978 | |
| 1979 | while (card_mask & allowed_mask) { |
| 1980 | enum mmc_voltage best_match; |
| 1981 | |
| 1982 | best_match = 1 << (ffs(card_mask & allowed_mask) - 1); |
| 1983 | if (!mmc_set_signal_voltage(mmc, best_match)) |
| 1984 | return 0; |
| 1985 | |
| 1986 | allowed_mask &= ~best_match; |
| 1987 | } |
| 1988 | |
| 1989 | return -ENOTSUPP; |
| 1990 | } |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 1991 | #else |
| 1992 | static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode, |
| 1993 | uint32_t allowed_mask) |
| 1994 | { |
| 1995 | return 0; |
| 1996 | } |
| 1997 | #endif |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 1998 | |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 1999 | static const struct mode_width_tuning mmc_modes_by_pref[] = { |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 2000 | #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
| 2001 | { |
| 2002 | .mode = MMC_HS_400_ES, |
| 2003 | .widths = MMC_MODE_8BIT, |
| 2004 | }, |
| 2005 | #endif |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2006 | #if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 2007 | { |
| 2008 | .mode = MMC_HS_400, |
| 2009 | .widths = MMC_MODE_8BIT, |
| 2010 | .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200 |
| 2011 | }, |
| 2012 | #endif |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 2013 | #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2014 | { |
| 2015 | .mode = MMC_HS_200, |
| 2016 | .widths = MMC_MODE_8BIT | MMC_MODE_4BIT, |
Kishon Vijay Abraham I | 210369f | 2017-09-21 16:30:06 +0200 | [diff] [blame] | 2017 | .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200 |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2018 | }, |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 2019 | #endif |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2020 | { |
| 2021 | .mode = MMC_DDR_52, |
| 2022 | .widths = MMC_MODE_8BIT | MMC_MODE_4BIT, |
| 2023 | }, |
| 2024 | { |
| 2025 | .mode = MMC_HS_52, |
| 2026 | .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT, |
| 2027 | }, |
| 2028 | { |
| 2029 | .mode = MMC_HS, |
| 2030 | .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT, |
| 2031 | }, |
| 2032 | { |
| 2033 | .mode = MMC_LEGACY, |
| 2034 | .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT, |
| 2035 | } |
| 2036 | }; |
| 2037 | |
| 2038 | #define for_each_mmc_mode_by_pref(caps, mwt) \ |
| 2039 | for (mwt = mmc_modes_by_pref;\ |
| 2040 | mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\ |
| 2041 | mwt++) \ |
| 2042 | if (caps & MMC_CAP(mwt->mode)) |
| 2043 | |
| 2044 | static const struct ext_csd_bus_width { |
| 2045 | uint cap; |
| 2046 | bool is_ddr; |
| 2047 | uint ext_csd_bits; |
| 2048 | } ext_csd_bus_width[] = { |
| 2049 | {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8}, |
| 2050 | {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4}, |
| 2051 | {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8}, |
| 2052 | {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4}, |
| 2053 | {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1}, |
| 2054 | }; |
| 2055 | |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2056 | #if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) |
| 2057 | static int mmc_select_hs400(struct mmc *mmc) |
| 2058 | { |
| 2059 | int err; |
| 2060 | |
| 2061 | /* Set timing to HS200 for tuning */ |
Marek Vasut | 111572f | 2019-01-03 21:19:24 +0100 | [diff] [blame] | 2062 | err = mmc_set_card_speed(mmc, MMC_HS_200, false); |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2063 | if (err) |
| 2064 | return err; |
| 2065 | |
| 2066 | /* configure the bus mode (host) */ |
| 2067 | mmc_select_mode(mmc, MMC_HS_200); |
| 2068 | mmc_set_clock(mmc, mmc->tran_speed, false); |
| 2069 | |
| 2070 | /* execute tuning if needed */ |
Marek Vasut | 259cc63 | 2024-02-24 23:32:09 +0100 | [diff] [blame] | 2071 | mmc->hs400_tuning = true; |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2072 | err = mmc_execute_tuning(mmc, MMC_CMD_SEND_TUNING_BLOCK_HS200); |
Marek Vasut | 259cc63 | 2024-02-24 23:32:09 +0100 | [diff] [blame] | 2073 | mmc->hs400_tuning = false; |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2074 | if (err) { |
| 2075 | debug("tuning failed\n"); |
| 2076 | return err; |
| 2077 | } |
| 2078 | |
| 2079 | /* Set back to HS */ |
Venkatesh Yadav Abbarapu | 10f9def | 2024-04-23 11:00:57 +0530 | [diff] [blame] | 2080 | mmc_set_card_speed(mmc, MMC_HS_52, true); |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2081 | |
Yangbo Lu | 5347aea | 2020-09-01 16:58:04 +0800 | [diff] [blame] | 2082 | err = mmc_hs400_prepare_ddr(mmc); |
| 2083 | if (err) |
| 2084 | return err; |
| 2085 | |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2086 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, |
| 2087 | EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG); |
| 2088 | if (err) |
| 2089 | return err; |
| 2090 | |
Marek Vasut | 111572f | 2019-01-03 21:19:24 +0100 | [diff] [blame] | 2091 | err = mmc_set_card_speed(mmc, MMC_HS_400, false); |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2092 | if (err) |
| 2093 | return err; |
| 2094 | |
| 2095 | mmc_select_mode(mmc, MMC_HS_400); |
| 2096 | err = mmc_set_clock(mmc, mmc->tran_speed, false); |
| 2097 | if (err) |
| 2098 | return err; |
| 2099 | |
| 2100 | return 0; |
| 2101 | } |
| 2102 | #else |
| 2103 | static int mmc_select_hs400(struct mmc *mmc) |
| 2104 | { |
| 2105 | return -ENOTSUPP; |
| 2106 | } |
| 2107 | #endif |
| 2108 | |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 2109 | #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
| 2110 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 2111 | static int mmc_set_enhanced_strobe(struct mmc *mmc) |
| 2112 | { |
| 2113 | return -ENOTSUPP; |
| 2114 | } |
| 2115 | #endif |
| 2116 | static int mmc_select_hs400es(struct mmc *mmc) |
| 2117 | { |
| 2118 | int err; |
| 2119 | |
| 2120 | err = mmc_set_card_speed(mmc, MMC_HS, true); |
| 2121 | if (err) |
| 2122 | return err; |
| 2123 | |
| 2124 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, |
| 2125 | EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG | |
| 2126 | EXT_CSD_BUS_WIDTH_STROBE); |
| 2127 | if (err) { |
| 2128 | printf("switch to bus width for hs400 failed\n"); |
| 2129 | return err; |
| 2130 | } |
| 2131 | /* TODO: driver strength */ |
| 2132 | err = mmc_set_card_speed(mmc, MMC_HS_400_ES, false); |
| 2133 | if (err) |
| 2134 | return err; |
| 2135 | |
| 2136 | mmc_select_mode(mmc, MMC_HS_400_ES); |
| 2137 | err = mmc_set_clock(mmc, mmc->tran_speed, false); |
| 2138 | if (err) |
| 2139 | return err; |
| 2140 | |
| 2141 | return mmc_set_enhanced_strobe(mmc); |
| 2142 | } |
| 2143 | #else |
| 2144 | static int mmc_select_hs400es(struct mmc *mmc) |
| 2145 | { |
| 2146 | return -ENOTSUPP; |
| 2147 | } |
| 2148 | #endif |
| 2149 | |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2150 | #define for_each_supported_width(caps, ddr, ecbv) \ |
| 2151 | for (ecbv = ext_csd_bus_width;\ |
| 2152 | ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\ |
| 2153 | ecbv++) \ |
| 2154 | if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap)) |
| 2155 | |
Jean-Jacques Hiblot | 3d30972b | 2017-09-21 16:30:09 +0200 | [diff] [blame] | 2156 | static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps) |
Jean-Jacques Hiblot | 933d126 | 2017-09-21 16:29:52 +0200 | [diff] [blame] | 2157 | { |
Jaehoon Chung | 6b3431c | 2020-12-04 06:36:00 +0900 | [diff] [blame] | 2158 | int err = 0; |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2159 | const struct mode_width_tuning *mwt; |
| 2160 | const struct ext_csd_bus_width *ecbw; |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 2161 | |
Jean-Jacques Hiblot | 93c31d1 | 2017-11-30 17:43:54 +0100 | [diff] [blame] | 2162 | #ifdef DEBUG |
| 2163 | mmc_dump_capabilities("mmc", card_caps); |
Jean-Jacques Hiblot | d7e5e03 | 2017-11-30 17:43:57 +0100 | [diff] [blame] | 2164 | mmc_dump_capabilities("host", mmc->host_caps); |
Jean-Jacques Hiblot | 93c31d1 | 2017-11-30 17:43:54 +0100 | [diff] [blame] | 2165 | #endif |
| 2166 | |
Anup Patel | d9c92c7 | 2019-07-08 04:10:43 +0000 | [diff] [blame] | 2167 | if (mmc_host_is_spi(mmc)) { |
| 2168 | mmc_set_bus_width(mmc, 1); |
| 2169 | mmc_select_mode(mmc, MMC_LEGACY); |
| 2170 | mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE); |
| 2171 | return 0; |
| 2172 | } |
| 2173 | |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 2174 | /* Restrict card's capabilities by what the host can do */ |
Jean-Jacques Hiblot | d7e5e03 | 2017-11-30 17:43:57 +0100 | [diff] [blame] | 2175 | card_caps &= mmc->host_caps; |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 2176 | |
| 2177 | /* Only version 4 of MMC supports wider bus widths */ |
| 2178 | if (mmc->version < MMC_VERSION_4) |
| 2179 | return 0; |
| 2180 | |
Jean-Jacques Hiblot | ed9506b | 2017-09-21 16:29:51 +0200 | [diff] [blame] | 2181 | if (!mmc->ext_csd) { |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 2182 | pr_debug("No ext_csd found!\n"); /* this should enver happen */ |
Jean-Jacques Hiblot | ed9506b | 2017-09-21 16:29:51 +0200 | [diff] [blame] | 2183 | return -ENOTSUPP; |
| 2184 | } |
| 2185 | |
Marek Vasut | 111572f | 2019-01-03 21:19:24 +0100 | [diff] [blame] | 2186 | #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \ |
Ye Li | 3679e80 | 2021-08-17 17:20:34 +0800 | [diff] [blame] | 2187 | CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \ |
| 2188 | CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
Marek Vasut | 111572f | 2019-01-03 21:19:24 +0100 | [diff] [blame] | 2189 | /* |
| 2190 | * In case the eMMC is in HS200/HS400 mode, downgrade to HS mode |
| 2191 | * before doing anything else, since a transition from either of |
| 2192 | * the HS200/HS400 mode directly to legacy mode is not supported. |
| 2193 | */ |
| 2194 | if (mmc->selected_mode == MMC_HS_200 || |
Ye Li | 3679e80 | 2021-08-17 17:20:34 +0800 | [diff] [blame] | 2195 | mmc->selected_mode == MMC_HS_400 || |
| 2196 | mmc->selected_mode == MMC_HS_400_ES) |
Marek Vasut | 111572f | 2019-01-03 21:19:24 +0100 | [diff] [blame] | 2197 | mmc_set_card_speed(mmc, MMC_HS, true); |
| 2198 | else |
| 2199 | #endif |
| 2200 | mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE); |
Jean-Jacques Hiblot | 3d30972b | 2017-09-21 16:30:09 +0200 | [diff] [blame] | 2201 | |
| 2202 | for_each_mmc_mode_by_pref(card_caps, mwt) { |
| 2203 | for_each_supported_width(card_caps & mwt->widths, |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2204 | mmc_is_mode_ddr(mwt->mode), ecbw) { |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 2205 | enum mmc_voltage old_voltage; |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 2206 | pr_debug("trying mode %s width %d (at %d MHz)\n", |
| 2207 | mmc_mode_name(mwt->mode), |
| 2208 | bus_width(ecbw->cap), |
| 2209 | mmc_mode2freq(mmc, mwt->mode) / 1000000); |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 2210 | old_voltage = mmc->signal_voltage; |
| 2211 | err = mmc_set_lowest_voltage(mmc, mwt->mode, |
| 2212 | MMC_ALL_SIGNAL_VOLTAGE); |
| 2213 | if (err) |
| 2214 | continue; |
| 2215 | |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2216 | /* configure the bus width (card + host) */ |
| 2217 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 2218 | EXT_CSD_BUS_WIDTH, |
| 2219 | ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG); |
| 2220 | if (err) |
| 2221 | goto error; |
| 2222 | mmc_set_bus_width(mmc, bus_width(ecbw->cap)); |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 2223 | |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2224 | if (mwt->mode == MMC_HS_400) { |
| 2225 | err = mmc_select_hs400(mmc); |
| 2226 | if (err) { |
| 2227 | printf("Select HS400 failed %d\n", err); |
| 2228 | goto error; |
| 2229 | } |
Peng Fan | eede83b | 2019-07-10 14:43:07 +0800 | [diff] [blame] | 2230 | } else if (mwt->mode == MMC_HS_400_ES) { |
| 2231 | err = mmc_select_hs400es(mmc); |
| 2232 | if (err) { |
| 2233 | printf("Select HS400ES failed %d\n", |
| 2234 | err); |
| 2235 | goto error; |
| 2236 | } |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2237 | } else { |
| 2238 | /* configure the bus speed (card) */ |
Marek Vasut | 111572f | 2019-01-03 21:19:24 +0100 | [diff] [blame] | 2239 | err = mmc_set_card_speed(mmc, mwt->mode, false); |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2240 | if (err) |
| 2241 | goto error; |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2242 | |
| 2243 | /* |
| 2244 | * configure the bus width AND the ddr mode |
| 2245 | * (card). The host side will be taken care |
| 2246 | * of in the next step |
| 2247 | */ |
| 2248 | if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) { |
| 2249 | err = mmc_switch(mmc, |
| 2250 | EXT_CSD_CMD_SET_NORMAL, |
| 2251 | EXT_CSD_BUS_WIDTH, |
| 2252 | ecbw->ext_csd_bits); |
| 2253 | if (err) |
| 2254 | goto error; |
| 2255 | } |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 2256 | |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2257 | /* configure the bus mode (host) */ |
| 2258 | mmc_select_mode(mmc, mwt->mode); |
| 2259 | mmc_set_clock(mmc, mmc->tran_speed, |
| 2260 | MMC_CLK_ENABLE); |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 2261 | #if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING) |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 2262 | |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2263 | /* execute tuning if needed */ |
| 2264 | if (mwt->tuning) { |
| 2265 | err = mmc_execute_tuning(mmc, |
| 2266 | mwt->tuning); |
| 2267 | if (err) { |
Jaehoon Chung | ad9f7ce | 2020-11-17 07:04:59 +0900 | [diff] [blame] | 2268 | pr_debug("tuning failed : %d\n", err); |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2269 | goto error; |
| 2270 | } |
Kishon Vijay Abraham I | 210369f | 2017-09-21 16:30:06 +0200 | [diff] [blame] | 2271 | } |
Jean-Jacques Hiblot | 6051e78 | 2017-11-30 17:44:01 +0100 | [diff] [blame] | 2272 | #endif |
Peng Fan | 4680125 | 2018-08-10 14:07:54 +0800 | [diff] [blame] | 2273 | } |
Kishon Vijay Abraham I | 210369f | 2017-09-21 16:30:06 +0200 | [diff] [blame] | 2274 | |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2275 | /* do a transfer to check the configuration */ |
| 2276 | err = mmc_read_and_compare_ext_csd(mmc); |
| 2277 | if (!err) |
| 2278 | return 0; |
| 2279 | error: |
Jean-Jacques Hiblot | b6937d6 | 2017-09-21 16:30:11 +0200 | [diff] [blame] | 2280 | mmc_set_signal_voltage(mmc, old_voltage); |
Naoki Hayama | 3110dcb | 2020-10-12 18:35:22 +0900 | [diff] [blame] | 2281 | /* if an error occurred, revert to a safer bus mode */ |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2282 | mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 2283 | EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1); |
| 2284 | mmc_select_mode(mmc, MMC_LEGACY); |
Valentine Barshak | 29f8d07 | 2023-06-10 13:22:33 +0200 | [diff] [blame] | 2285 | mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE); |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2286 | mmc_set_bus_width(mmc, 1); |
| 2287 | } |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 2288 | } |
| 2289 | |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 2290 | log_err("unable to select a mode: %d\n", err); |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 2291 | |
Jean-Jacques Hiblot | ec34683 | 2017-09-21 16:29:58 +0200 | [diff] [blame] | 2292 | return -ENOTSUPP; |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 2293 | } |
Marek Vasut | 67c77f9 | 2024-03-17 04:01:22 +0100 | [diff] [blame] | 2294 | #else |
| 2295 | static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps) |
| 2296 | { |
| 2297 | return 0; |
| 2298 | }; |
| 2299 | |
| 2300 | static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps) |
| 2301 | { |
| 2302 | return 0; |
| 2303 | }; |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 2304 | #endif |
| 2305 | |
| 2306 | #if CONFIG_IS_ENABLED(MMC_TINY) |
| 2307 | DEFINE_CACHE_ALIGN_BUFFER(u8, ext_csd_bkup, MMC_MAX_BLOCK_LEN); |
| 2308 | #endif |
Jean-Jacques Hiblot | 31e7cf3 | 2017-09-21 16:29:49 +0200 | [diff] [blame] | 2309 | |
Jean-Jacques Hiblot | ed9506b | 2017-09-21 16:29:51 +0200 | [diff] [blame] | 2310 | static int mmc_startup_v4(struct mmc *mmc) |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2311 | { |
| 2312 | int err, i; |
| 2313 | u64 capacity; |
| 2314 | bool has_parts = false; |
| 2315 | bool part_completed; |
Jean-Jacques Hiblot | fa6c577 | 2018-01-04 15:23:31 +0100 | [diff] [blame] | 2316 | static const u32 mmc_versions[] = { |
| 2317 | MMC_VERSION_4, |
| 2318 | MMC_VERSION_4_1, |
| 2319 | MMC_VERSION_4_2, |
| 2320 | MMC_VERSION_4_3, |
Jean-Jacques Hiblot | c64862b | 2018-02-09 12:09:28 +0100 | [diff] [blame] | 2321 | MMC_VERSION_4_4, |
Jean-Jacques Hiblot | fa6c577 | 2018-01-04 15:23:31 +0100 | [diff] [blame] | 2322 | MMC_VERSION_4_41, |
| 2323 | MMC_VERSION_4_5, |
| 2324 | MMC_VERSION_5_0, |
| 2325 | MMC_VERSION_5_1 |
| 2326 | }; |
| 2327 | |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 2328 | #if CONFIG_IS_ENABLED(MMC_TINY) |
| 2329 | u8 *ext_csd = ext_csd_bkup; |
| 2330 | |
| 2331 | if (IS_SD(mmc) || mmc->version < MMC_VERSION_4) |
| 2332 | return 0; |
| 2333 | |
| 2334 | if (!mmc->ext_csd) |
Sam Edwards | ed8339f | 2023-05-18 13:47:07 -0600 | [diff] [blame] | 2335 | memset(ext_csd_bkup, 0, MMC_MAX_BLOCK_LEN); |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 2336 | |
| 2337 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 2338 | if (err) |
| 2339 | goto error; |
| 2340 | |
| 2341 | /* store the ext csd for future reference */ |
| 2342 | if (!mmc->ext_csd) |
| 2343 | mmc->ext_csd = ext_csd; |
| 2344 | #else |
Jean-Jacques Hiblot | 06976eb | 2017-11-30 17:43:59 +0100 | [diff] [blame] | 2345 | ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2346 | |
| 2347 | if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4)) |
| 2348 | return 0; |
| 2349 | |
| 2350 | /* check ext_csd version and capacity */ |
| 2351 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 2352 | if (err) |
Jean-Jacques Hiblot | 06976eb | 2017-11-30 17:43:59 +0100 | [diff] [blame] | 2353 | goto error; |
| 2354 | |
| 2355 | /* store the ext csd for future reference */ |
| 2356 | if (!mmc->ext_csd) |
| 2357 | mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN); |
| 2358 | if (!mmc->ext_csd) |
| 2359 | return -ENOMEM; |
| 2360 | memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN); |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 2361 | #endif |
Alexander Kochetkov | f1133c9 | 2018-02-20 14:35:55 +0300 | [diff] [blame] | 2362 | if (ext_csd[EXT_CSD_REV] >= ARRAY_SIZE(mmc_versions)) |
Jean-Jacques Hiblot | fa6c577 | 2018-01-04 15:23:31 +0100 | [diff] [blame] | 2363 | return -EINVAL; |
| 2364 | |
| 2365 | mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]]; |
| 2366 | |
| 2367 | if (mmc->version >= MMC_VERSION_4_2) { |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2368 | /* |
| 2369 | * According to the JEDEC Standard, the value of |
| 2370 | * ext_csd's capacity is valid if the value is more |
| 2371 | * than 2GB |
| 2372 | */ |
| 2373 | capacity = ext_csd[EXT_CSD_SEC_CNT] << 0 |
| 2374 | | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
| 2375 | | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
| 2376 | | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; |
| 2377 | capacity *= MMC_MAX_BLOCK_LEN; |
| 2378 | if ((capacity >> 20) > 2 * 1024) |
| 2379 | mmc->capacity_user = capacity; |
| 2380 | } |
| 2381 | |
Jean-Jacques Hiblot | 201559c | 2019-07-02 10:53:54 +0200 | [diff] [blame] | 2382 | if (mmc->version >= MMC_VERSION_4_5) |
| 2383 | mmc->gen_cmd6_time = ext_csd[EXT_CSD_GENERIC_CMD6_TIME]; |
| 2384 | |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2385 | /* The partition data may be non-zero but it is only |
| 2386 | * effective if PARTITION_SETTING_COMPLETED is set in |
| 2387 | * EXT_CSD, so ignore any data if this bit is not set, |
| 2388 | * except for enabling the high-capacity group size |
| 2389 | * definition (see below). |
| 2390 | */ |
| 2391 | part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] & |
| 2392 | EXT_CSD_PARTITION_SETTING_COMPLETED); |
| 2393 | |
Jean-Jacques Hiblot | 7f5b169 | 2019-07-02 10:53:55 +0200 | [diff] [blame] | 2394 | mmc->part_switch_time = ext_csd[EXT_CSD_PART_SWITCH_TIME]; |
| 2395 | /* Some eMMC set the value too low so set a minimum */ |
| 2396 | if (mmc->part_switch_time < MMC_MIN_PART_SWITCH_TIME && mmc->part_switch_time) |
| 2397 | mmc->part_switch_time = MMC_MIN_PART_SWITCH_TIME; |
| 2398 | |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2399 | /* store the partition info of emmc */ |
| 2400 | mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT]; |
| 2401 | if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) || |
| 2402 | ext_csd[EXT_CSD_BOOT_MULT]) |
| 2403 | mmc->part_config = ext_csd[EXT_CSD_PART_CONF]; |
| 2404 | if (part_completed && |
| 2405 | (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT)) |
| 2406 | mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE]; |
| 2407 | |
| 2408 | mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17; |
| 2409 | |
| 2410 | mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17; |
| 2411 | |
| 2412 | for (i = 0; i < 4; i++) { |
| 2413 | int idx = EXT_CSD_GP_SIZE_MULT + i * 3; |
| 2414 | uint mult = (ext_csd[idx + 2] << 16) + |
| 2415 | (ext_csd[idx + 1] << 8) + ext_csd[idx]; |
| 2416 | if (mult) |
| 2417 | has_parts = true; |
| 2418 | if (!part_completed) |
| 2419 | continue; |
| 2420 | mmc->capacity_gp[i] = mult; |
| 2421 | mmc->capacity_gp[i] *= |
| 2422 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
| 2423 | mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
| 2424 | mmc->capacity_gp[i] <<= 19; |
| 2425 | } |
| 2426 | |
Jean-Jacques Hiblot | c94c547 | 2018-01-04 15:23:35 +0100 | [diff] [blame] | 2427 | #ifndef CONFIG_SPL_BUILD |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2428 | if (part_completed) { |
| 2429 | mmc->enh_user_size = |
| 2430 | (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) + |
| 2431 | (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) + |
| 2432 | ext_csd[EXT_CSD_ENH_SIZE_MULT]; |
| 2433 | mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
| 2434 | mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
| 2435 | mmc->enh_user_size <<= 19; |
| 2436 | mmc->enh_user_start = |
| 2437 | (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) + |
| 2438 | (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) + |
| 2439 | (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) + |
| 2440 | ext_csd[EXT_CSD_ENH_START_ADDR]; |
| 2441 | if (mmc->high_capacity) |
| 2442 | mmc->enh_user_start <<= 9; |
| 2443 | } |
Jean-Jacques Hiblot | c94c547 | 2018-01-04 15:23:35 +0100 | [diff] [blame] | 2444 | #endif |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2445 | |
| 2446 | /* |
| 2447 | * Host needs to enable ERASE_GRP_DEF bit if device is |
| 2448 | * partitioned. This bit will be lost every time after a reset |
| 2449 | * or power off. This will affect erase size. |
| 2450 | */ |
| 2451 | if (part_completed) |
| 2452 | has_parts = true; |
| 2453 | if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) && |
| 2454 | (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) |
| 2455 | has_parts = true; |
| 2456 | if (has_parts) { |
| 2457 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 2458 | EXT_CSD_ERASE_GROUP_DEF, 1); |
| 2459 | |
| 2460 | if (err) |
Jean-Jacques Hiblot | 06976eb | 2017-11-30 17:43:59 +0100 | [diff] [blame] | 2461 | goto error; |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2462 | |
| 2463 | ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1; |
| 2464 | } |
| 2465 | |
| 2466 | if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) { |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2467 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2468 | /* Read out group size from ext_csd */ |
| 2469 | mmc->erase_grp_size = |
| 2470 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024; |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2471 | #endif |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2472 | /* |
| 2473 | * if high capacity and partition setting completed |
| 2474 | * SEC_COUNT is valid even if it is smaller than 2 GiB |
| 2475 | * JEDEC Standard JESD84-B45, 6.2.4 |
| 2476 | */ |
| 2477 | if (mmc->high_capacity && part_completed) { |
| 2478 | capacity = (ext_csd[EXT_CSD_SEC_CNT]) | |
| 2479 | (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) | |
| 2480 | (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) | |
| 2481 | (ext_csd[EXT_CSD_SEC_CNT + 3] << 24); |
| 2482 | capacity *= MMC_MAX_BLOCK_LEN; |
| 2483 | mmc->capacity_user = capacity; |
| 2484 | } |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2485 | } |
| 2486 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
| 2487 | else { |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2488 | /* Calculate the group size from the csd value. */ |
| 2489 | int erase_gsz, erase_gmul; |
| 2490 | |
| 2491 | erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10; |
| 2492 | erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5; |
| 2493 | mmc->erase_grp_size = (erase_gsz + 1) |
| 2494 | * (erase_gmul + 1); |
| 2495 | } |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2496 | #endif |
Jean-Jacques Hiblot | ba54ab8 | 2018-01-04 15:23:36 +0100 | [diff] [blame] | 2497 | #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING) |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2498 | mmc->hc_wp_grp_size = 1024 |
| 2499 | * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] |
| 2500 | * ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
Jean-Jacques Hiblot | ba54ab8 | 2018-01-04 15:23:36 +0100 | [diff] [blame] | 2501 | #endif |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2502 | |
| 2503 | mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; |
| 2504 | |
Loic Poulain | c0aadbb | 2023-01-26 10:24:17 +0100 | [diff] [blame] | 2505 | mmc->can_trim = |
| 2506 | !!(ext_csd[EXT_CSD_SEC_FEATURE] & EXT_CSD_SEC_FEATURE_TRIM_EN); |
| 2507 | |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2508 | return 0; |
Jean-Jacques Hiblot | 06976eb | 2017-11-30 17:43:59 +0100 | [diff] [blame] | 2509 | error: |
| 2510 | if (mmc->ext_csd) { |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 2511 | #if !CONFIG_IS_ENABLED(MMC_TINY) |
Jean-Jacques Hiblot | 06976eb | 2017-11-30 17:43:59 +0100 | [diff] [blame] | 2512 | free(mmc->ext_csd); |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 2513 | #endif |
Jean-Jacques Hiblot | 06976eb | 2017-11-30 17:43:59 +0100 | [diff] [blame] | 2514 | mmc->ext_csd = NULL; |
| 2515 | } |
| 2516 | return err; |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2517 | } |
| 2518 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 2519 | static int mmc_startup(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2520 | { |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 2521 | int err, i; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2522 | uint mult, freq; |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2523 | u64 cmult, csize; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2524 | struct mmc_cmd cmd; |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 2525 | struct blk_desc *bdesc; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2526 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2527 | #ifdef CONFIG_MMC_SPI_CRC_ON |
| 2528 | if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ |
| 2529 | cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; |
| 2530 | cmd.resp_type = MMC_RSP_R1; |
| 2531 | cmd.cmdarg = 1; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2532 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2533 | if (err) |
| 2534 | return err; |
| 2535 | } |
| 2536 | #endif |
| 2537 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2538 | /* Put the Card in Identify Mode */ |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2539 | cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : |
| 2540 | MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */ |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2541 | cmd.resp_type = MMC_RSP_R2; |
| 2542 | cmd.cmdarg = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2543 | |
Sean Anderson | 8632509 | 2020-10-17 08:36:27 -0400 | [diff] [blame] | 2544 | err = mmc_send_cmd_quirks(mmc, &cmd, NULL, MMC_QUIRK_RETRY_SEND_CID, 4); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2545 | if (err) |
| 2546 | return err; |
| 2547 | |
| 2548 | memcpy(mmc->cid, cmd.response, 16); |
| 2549 | |
| 2550 | /* |
| 2551 | * For MMC cards, set the Relative Address. |
| 2552 | * For SD cards, get the Relatvie Address. |
| 2553 | * This also puts the cards into Standby State |
| 2554 | */ |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2555 | if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ |
| 2556 | cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; |
| 2557 | cmd.cmdarg = mmc->rca << 16; |
| 2558 | cmd.resp_type = MMC_RSP_R6; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2559 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2560 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2561 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2562 | if (err) |
| 2563 | return err; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2564 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2565 | if (IS_SD(mmc)) |
| 2566 | mmc->rca = (cmd.response[0] >> 16) & 0xffff; |
| 2567 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2568 | |
| 2569 | /* Get the Card-Specific Data */ |
| 2570 | cmd.cmdidx = MMC_CMD_SEND_CSD; |
| 2571 | cmd.resp_type = MMC_RSP_R2; |
| 2572 | cmd.cmdarg = mmc->rca << 16; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2573 | |
| 2574 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 2575 | |
| 2576 | if (err) |
| 2577 | return err; |
| 2578 | |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 2579 | mmc->csd[0] = cmd.response[0]; |
| 2580 | mmc->csd[1] = cmd.response[1]; |
| 2581 | mmc->csd[2] = cmd.response[2]; |
| 2582 | mmc->csd[3] = cmd.response[3]; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2583 | |
| 2584 | if (mmc->version == MMC_VERSION_UNKNOWN) { |
Rabin Vincent | bdf7a68 | 2009-04-05 13:30:55 +0530 | [diff] [blame] | 2585 | int version = (cmd.response[0] >> 26) & 0xf; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2586 | |
| 2587 | switch (version) { |
Bin Meng | 4a4ef87 | 2016-03-17 21:53:13 -0700 | [diff] [blame] | 2588 | case 0: |
| 2589 | mmc->version = MMC_VERSION_1_2; |
| 2590 | break; |
| 2591 | case 1: |
| 2592 | mmc->version = MMC_VERSION_1_4; |
| 2593 | break; |
| 2594 | case 2: |
| 2595 | mmc->version = MMC_VERSION_2_2; |
| 2596 | break; |
| 2597 | case 3: |
| 2598 | mmc->version = MMC_VERSION_3; |
| 2599 | break; |
| 2600 | case 4: |
| 2601 | mmc->version = MMC_VERSION_4; |
| 2602 | break; |
| 2603 | default: |
| 2604 | mmc->version = MMC_VERSION_1_2; |
| 2605 | break; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | /* divide frequency by 10, since the mults are 10x bigger */ |
Rabin Vincent | bdf7a68 | 2009-04-05 13:30:55 +0530 | [diff] [blame] | 2610 | freq = fbase[(cmd.response[0] & 0x7)]; |
| 2611 | mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2612 | |
Jean-Jacques Hiblot | a94fb41 | 2017-09-21 16:29:53 +0200 | [diff] [blame] | 2613 | mmc->legacy_speed = freq * mult; |
Heinrich Schuchardt | 82479d4 | 2024-01-04 04:49:42 +0100 | [diff] [blame] | 2614 | if (!mmc->legacy_speed) |
| 2615 | log_debug("TRAN_SPEED: reserved value"); |
Jean-Jacques Hiblot | a94fb41 | 2017-09-21 16:29:53 +0200 | [diff] [blame] | 2616 | mmc_select_mode(mmc, MMC_LEGACY); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2617 | |
Markus Niebel | 0395141 | 2013-12-16 13:40:46 +0100 | [diff] [blame] | 2618 | mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1); |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 2619 | mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2620 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2621 | |
| 2622 | if (IS_SD(mmc)) |
| 2623 | mmc->write_bl_len = mmc->read_bl_len; |
| 2624 | else |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 2625 | mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2626 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2627 | |
| 2628 | if (mmc->high_capacity) { |
| 2629 | csize = (mmc->csd[1] & 0x3f) << 16 |
| 2630 | | (mmc->csd[2] & 0xffff0000) >> 16; |
| 2631 | cmult = 8; |
| 2632 | } else { |
| 2633 | csize = (mmc->csd[1] & 0x3ff) << 2 |
| 2634 | | (mmc->csd[2] & 0xc0000000) >> 30; |
| 2635 | cmult = (mmc->csd[2] & 0x00038000) >> 15; |
| 2636 | } |
| 2637 | |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 2638 | mmc->capacity_user = (csize + 1) << (cmult + 2); |
| 2639 | mmc->capacity_user *= mmc->read_bl_len; |
| 2640 | mmc->capacity_boot = 0; |
| 2641 | mmc->capacity_rpmb = 0; |
| 2642 | for (i = 0; i < 4; i++) |
| 2643 | mmc->capacity_gp[i] = 0; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2644 | |
Simon Glass | a09c2b7 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 2645 | if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN) |
| 2646 | mmc->read_bl_len = MMC_MAX_BLOCK_LEN; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2647 | |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2648 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Simon Glass | a09c2b7 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 2649 | if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN) |
| 2650 | mmc->write_bl_len = MMC_MAX_BLOCK_LEN; |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2651 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2652 | |
Markus Niebel | 0395141 | 2013-12-16 13:40:46 +0100 | [diff] [blame] | 2653 | if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) { |
| 2654 | cmd.cmdidx = MMC_CMD_SET_DSR; |
| 2655 | cmd.cmdarg = (mmc->dsr & 0xffff) << 16; |
| 2656 | cmd.resp_type = MMC_RSP_NONE; |
| 2657 | if (mmc_send_cmd(mmc, &cmd, NULL)) |
Jean-Jacques Hiblot | 678b608 | 2017-11-30 17:44:00 +0100 | [diff] [blame] | 2658 | pr_warn("MMC: SET_DSR failed\n"); |
Markus Niebel | 0395141 | 2013-12-16 13:40:46 +0100 | [diff] [blame] | 2659 | } |
| 2660 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2661 | /* Select the card, and put it into Transfer Mode */ |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2662 | if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ |
| 2663 | cmd.cmdidx = MMC_CMD_SELECT_CARD; |
Ajay Bhargav | 4a32fba | 2011-10-05 03:13:23 +0000 | [diff] [blame] | 2664 | cmd.resp_type = MMC_RSP_R1; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2665 | cmd.cmdarg = mmc->rca << 16; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2666 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2667 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 2668 | if (err) |
| 2669 | return err; |
| 2670 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2671 | |
Lei Wen | ea52676 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 2672 | /* |
| 2673 | * For SD, its erase group is always one sector |
| 2674 | */ |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2675 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Lei Wen | ea52676 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 2676 | mmc->erase_grp_size = 1; |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2677 | #endif |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 2678 | mmc->part_config = MMCPART_NOAVAILABLE; |
Diego Santa Cruz | a7a7599 | 2014-12-23 10:50:27 +0100 | [diff] [blame] | 2679 | |
Jean-Jacques Hiblot | ed9506b | 2017-09-21 16:29:51 +0200 | [diff] [blame] | 2680 | err = mmc_startup_v4(mmc); |
Jean-Jacques Hiblot | e84459c | 2017-09-21 16:29:50 +0200 | [diff] [blame] | 2681 | if (err) |
| 2682 | return err; |
Sukumar Ghorai | 232293c | 2010-09-20 18:29:29 +0530 | [diff] [blame] | 2683 | |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 2684 | err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart); |
Stephen Warren | e315ae8 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 2685 | if (err) |
| 2686 | return err; |
| 2687 | |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 2688 | #if CONFIG_IS_ENABLED(MMC_TINY) |
| 2689 | mmc_set_clock(mmc, mmc->legacy_speed, false); |
Faiz Abbas | 01db77e | 2020-02-26 13:44:32 +0530 | [diff] [blame] | 2690 | mmc_select_mode(mmc, MMC_LEGACY); |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 2691 | mmc_set_bus_width(mmc, 1); |
| 2692 | #else |
Jean-Jacques Hiblot | 3d30972b | 2017-09-21 16:30:09 +0200 | [diff] [blame] | 2693 | if (IS_SD(mmc)) { |
| 2694 | err = sd_get_capabilities(mmc); |
| 2695 | if (err) |
| 2696 | return err; |
| 2697 | err = sd_select_mode_and_width(mmc, mmc->card_caps); |
| 2698 | } else { |
| 2699 | err = mmc_get_capabilities(mmc); |
| 2700 | if (err) |
| 2701 | return err; |
Masahiro Yamada | bf1f25c | 2020-01-23 14:31:12 +0900 | [diff] [blame] | 2702 | err = mmc_select_mode_and_width(mmc, mmc->card_caps); |
Jean-Jacques Hiblot | 3d30972b | 2017-09-21 16:30:09 +0200 | [diff] [blame] | 2703 | } |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 2704 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2705 | if (err) |
| 2706 | return err; |
| 2707 | |
Jean-Jacques Hiblot | 3d30972b | 2017-09-21 16:30:09 +0200 | [diff] [blame] | 2708 | mmc->best_mode = mmc->selected_mode; |
Jaehoon Chung | e1d4c7b | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 2709 | |
Andrew Gabbasov | 532663b | 2014-12-01 06:59:11 -0600 | [diff] [blame] | 2710 | /* Fix the block length for DDR mode */ |
| 2711 | if (mmc->ddr_mode) { |
| 2712 | mmc->read_bl_len = MMC_MAX_BLOCK_LEN; |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2713 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Andrew Gabbasov | 532663b | 2014-12-01 06:59:11 -0600 | [diff] [blame] | 2714 | mmc->write_bl_len = MMC_MAX_BLOCK_LEN; |
Jean-Jacques Hiblot | 27edffe | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 2715 | #endif |
Andrew Gabbasov | 532663b | 2014-12-01 06:59:11 -0600 | [diff] [blame] | 2716 | } |
| 2717 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2718 | /* fill in device description */ |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 2719 | bdesc = mmc_get_blk_desc(mmc); |
| 2720 | bdesc->lun = 0; |
| 2721 | bdesc->hwpart = 0; |
| 2722 | bdesc->type = 0; |
| 2723 | bdesc->blksz = mmc->read_bl_len; |
| 2724 | bdesc->log2blksz = LOG2(bdesc->blksz); |
| 2725 | bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len); |
Sjoerd Simons | d67754f | 2015-12-04 23:27:40 +0100 | [diff] [blame] | 2726 | #if !defined(CONFIG_SPL_BUILD) || \ |
| 2727 | (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \ |
Simon Glass | 7611ac6 | 2019-09-25 08:56:27 -0600 | [diff] [blame] | 2728 | !CONFIG_IS_ENABLED(USE_TINY_PRINTF)) |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 2729 | sprintf(bdesc->vendor, "Man %06x Snr %04x%04x", |
Taylor Hutt | 7367ec2 | 2012-10-20 17:15:59 +0000 | [diff] [blame] | 2730 | mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff), |
| 2731 | (mmc->cid[3] >> 16) & 0xffff); |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 2732 | sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff, |
Taylor Hutt | 7367ec2 | 2012-10-20 17:15:59 +0000 | [diff] [blame] | 2733 | (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, |
| 2734 | (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff, |
| 2735 | (mmc->cid[2] >> 24) & 0xff); |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 2736 | sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf, |
Taylor Hutt | 7367ec2 | 2012-10-20 17:15:59 +0000 | [diff] [blame] | 2737 | (mmc->cid[2] >> 16) & 0xf); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 2738 | #else |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 2739 | bdesc->vendor[0] = 0; |
| 2740 | bdesc->product[0] = 0; |
| 2741 | bdesc->revision[0] = 0; |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 2742 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2743 | |
Andre Przywara | 1779804 | 2018-12-17 10:05:45 +0000 | [diff] [blame] | 2744 | #if !defined(CONFIG_DM_MMC) && (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)) |
| 2745 | part_init(bdesc); |
| 2746 | #endif |
| 2747 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2748 | return 0; |
| 2749 | } |
| 2750 | |
Kim Phillips | 87ea389 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 2751 | static int mmc_send_if_cond(struct mmc *mmc) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2752 | { |
| 2753 | struct mmc_cmd cmd; |
| 2754 | int err; |
| 2755 | |
| 2756 | cmd.cmdidx = SD_CMD_SEND_IF_COND; |
| 2757 | /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 2758 | cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2759 | cmd.resp_type = MMC_RSP_R7; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2760 | |
| 2761 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 2762 | |
| 2763 | if (err) |
| 2764 | return err; |
| 2765 | |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 2766 | if ((cmd.response[0] & 0xff) != 0xaa) |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 2767 | return -EOPNOTSUPP; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2768 | else |
| 2769 | mmc->version = SD_VERSION_2; |
| 2770 | |
| 2771 | return 0; |
| 2772 | } |
| 2773 | |
Simon Glass | 5f4bd8c | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 2774 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Paul Kocialkowski | 2439fe9 | 2014-11-08 20:55:45 +0100 | [diff] [blame] | 2775 | /* board-specific MMC power initializations. */ |
| 2776 | __weak void board_mmc_power_init(void) |
| 2777 | { |
| 2778 | } |
Simon Glass | 833b80d | 2017-04-22 19:10:56 -0600 | [diff] [blame] | 2779 | #endif |
Paul Kocialkowski | 2439fe9 | 2014-11-08 20:55:45 +0100 | [diff] [blame] | 2780 | |
Peng Fan | 1530596 | 2016-10-11 15:08:43 +0800 | [diff] [blame] | 2781 | static int mmc_power_init(struct mmc *mmc) |
| 2782 | { |
Simon Glass | 5f4bd8c | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 2783 | #if CONFIG_IS_ENABLED(DM_MMC) |
Jean-Jacques Hiblot | a49ffa1 | 2017-09-21 16:29:48 +0200 | [diff] [blame] | 2784 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Peng Fan | 1530596 | 2016-10-11 15:08:43 +0800 | [diff] [blame] | 2785 | int ret; |
| 2786 | |
| 2787 | ret = device_get_supply_regulator(mmc->dev, "vmmc-supply", |
Jean-Jacques Hiblot | a49ffa1 | 2017-09-21 16:29:48 +0200 | [diff] [blame] | 2788 | &mmc->vmmc_supply); |
| 2789 | if (ret) |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 2790 | pr_debug("%s: No vmmc supply\n", mmc->dev->name); |
Jean-Jacques Hiblot | a49ffa1 | 2017-09-21 16:29:48 +0200 | [diff] [blame] | 2791 | |
| 2792 | ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply", |
| 2793 | &mmc->vqmmc_supply); |
| 2794 | if (ret) |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 2795 | pr_debug("%s: No vqmmc supply\n", mmc->dev->name); |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2796 | #endif |
| 2797 | #else /* !CONFIG_DM_MMC */ |
| 2798 | /* |
| 2799 | * Driver model should use a regulator, as above, rather than calling |
| 2800 | * out to board code. |
| 2801 | */ |
| 2802 | board_mmc_power_init(); |
| 2803 | #endif |
| 2804 | return 0; |
| 2805 | } |
| 2806 | |
| 2807 | /* |
| 2808 | * put the host in the initial state: |
| 2809 | * - turn on Vdd (card power supply) |
| 2810 | * - configure the bus width and clock to minimal values |
| 2811 | */ |
| 2812 | static void mmc_set_initial_state(struct mmc *mmc) |
| 2813 | { |
| 2814 | int err; |
| 2815 | |
| 2816 | /* First try to set 3.3V. If it fails set to 1.8V */ |
| 2817 | err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330); |
| 2818 | if (err != 0) |
| 2819 | err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180); |
| 2820 | if (err != 0) |
Jean-Jacques Hiblot | 678b608 | 2017-11-30 17:44:00 +0100 | [diff] [blame] | 2821 | pr_warn("mmc: failed to set signal voltage\n"); |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2822 | |
| 2823 | mmc_select_mode(mmc, MMC_LEGACY); |
| 2824 | mmc_set_bus_width(mmc, 1); |
Jaehoon Chung | 239cb2f | 2018-01-26 19:25:29 +0900 | [diff] [blame] | 2825 | mmc_set_clock(mmc, 0, MMC_CLK_ENABLE); |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2826 | } |
Peng Fan | 1530596 | 2016-10-11 15:08:43 +0800 | [diff] [blame] | 2827 | |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2828 | static int mmc_power_on(struct mmc *mmc) |
| 2829 | { |
| 2830 | #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR) |
Jean-Jacques Hiblot | a49ffa1 | 2017-09-21 16:29:48 +0200 | [diff] [blame] | 2831 | if (mmc->vmmc_supply) { |
Jonas Karlman | 0f28e18 | 2023-07-19 21:20:59 +0000 | [diff] [blame] | 2832 | int ret = regulator_set_enable_if_allowed(mmc->vmmc_supply, |
| 2833 | true); |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2834 | |
Jonas Karlman | 0f28e18 | 2023-07-19 21:20:59 +0000 | [diff] [blame] | 2835 | if (ret && ret != -ENOSYS) { |
Jaehoon Chung | ad9f7ce | 2020-11-17 07:04:59 +0900 | [diff] [blame] | 2836 | printf("Error enabling VMMC supply : %d\n", ret); |
Jean-Jacques Hiblot | a49ffa1 | 2017-09-21 16:29:48 +0200 | [diff] [blame] | 2837 | return ret; |
| 2838 | } |
Peng Fan | 1530596 | 2016-10-11 15:08:43 +0800 | [diff] [blame] | 2839 | } |
| 2840 | #endif |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2841 | return 0; |
| 2842 | } |
| 2843 | |
| 2844 | static int mmc_power_off(struct mmc *mmc) |
| 2845 | { |
Jaehoon Chung | 239cb2f | 2018-01-26 19:25:29 +0900 | [diff] [blame] | 2846 | mmc_set_clock(mmc, 0, MMC_CLK_DISABLE); |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2847 | #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR) |
| 2848 | if (mmc->vmmc_supply) { |
Jonas Karlman | 0f28e18 | 2023-07-19 21:20:59 +0000 | [diff] [blame] | 2849 | int ret = regulator_set_enable_if_allowed(mmc->vmmc_supply, |
| 2850 | false); |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2851 | |
Jonas Karlman | 0f28e18 | 2023-07-19 21:20:59 +0000 | [diff] [blame] | 2852 | if (ret && ret != -ENOSYS) { |
Jaehoon Chung | ad9f7ce | 2020-11-17 07:04:59 +0900 | [diff] [blame] | 2853 | pr_debug("Error disabling VMMC supply : %d\n", ret); |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2854 | return ret; |
| 2855 | } |
| 2856 | } |
Simon Glass | 833b80d | 2017-04-22 19:10:56 -0600 | [diff] [blame] | 2857 | #endif |
Peng Fan | 1530596 | 2016-10-11 15:08:43 +0800 | [diff] [blame] | 2858 | return 0; |
| 2859 | } |
| 2860 | |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2861 | static int mmc_power_cycle(struct mmc *mmc) |
| 2862 | { |
| 2863 | int ret; |
| 2864 | |
| 2865 | ret = mmc_power_off(mmc); |
| 2866 | if (ret) |
| 2867 | return ret; |
Yann Gautier | 6f55833 | 2019-09-19 17:56:12 +0200 | [diff] [blame] | 2868 | |
| 2869 | ret = mmc_host_power_cycle(mmc); |
| 2870 | if (ret) |
| 2871 | return ret; |
| 2872 | |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2873 | /* |
| 2874 | * SD spec recommends at least 1ms of delay. Let's wait for 2ms |
| 2875 | * to be on the safer side. |
| 2876 | */ |
| 2877 | udelay(2000); |
| 2878 | return mmc_power_on(mmc); |
| 2879 | } |
| 2880 | |
Pali Rohár | 7c63962 | 2021-07-14 16:37:29 +0200 | [diff] [blame] | 2881 | int mmc_get_op_cond(struct mmc *mmc, bool quiet) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2882 | { |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 2883 | bool uhs_en = supports_uhs(mmc->cfg->host_caps); |
Macpaul Lin | 028bde1 | 2011-11-14 23:35:39 +0000 | [diff] [blame] | 2884 | int err; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2885 | |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 2886 | if (mmc->has_init) |
| 2887 | return 0; |
| 2888 | |
Peng Fan | 1530596 | 2016-10-11 15:08:43 +0800 | [diff] [blame] | 2889 | err = mmc_power_init(mmc); |
| 2890 | if (err) |
| 2891 | return err; |
Paul Kocialkowski | 2439fe9 | 2014-11-08 20:55:45 +0100 | [diff] [blame] | 2892 | |
Kishon Vijay Abraham I | 07baaa6 | 2017-09-21 16:30:10 +0200 | [diff] [blame] | 2893 | #ifdef CONFIG_MMC_QUIRKS |
| 2894 | mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN | |
Joel Johnson | 5ea041b | 2020-01-11 09:08:14 -0700 | [diff] [blame] | 2895 | MMC_QUIRK_RETRY_SEND_CID | |
| 2896 | MMC_QUIRK_RETRY_APP_CMD; |
Kishon Vijay Abraham I | 07baaa6 | 2017-09-21 16:30:10 +0200 | [diff] [blame] | 2897 | #endif |
| 2898 | |
Jean-Jacques Hiblot | dc030fb | 2017-09-21 16:30:08 +0200 | [diff] [blame] | 2899 | err = mmc_power_cycle(mmc); |
| 2900 | if (err) { |
| 2901 | /* |
| 2902 | * if power cycling is not supported, we should not try |
| 2903 | * to use the UHS modes, because we wouldn't be able to |
| 2904 | * recover from an error during the UHS initialization. |
| 2905 | */ |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 2906 | pr_debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n"); |
Jean-Jacques Hiblot | dc030fb | 2017-09-21 16:30:08 +0200 | [diff] [blame] | 2907 | uhs_en = false; |
| 2908 | mmc->host_caps &= ~UHS_CAPS; |
| 2909 | err = mmc_power_on(mmc); |
| 2910 | } |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2911 | if (err) |
| 2912 | return err; |
| 2913 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 2914 | #if CONFIG_IS_ENABLED(DM_MMC) |
Yangbo Lu | c46f5d7 | 2020-09-01 16:57:59 +0800 | [diff] [blame] | 2915 | /* |
| 2916 | * Re-initialization is needed to clear old configuration for |
| 2917 | * mmc rescan. |
| 2918 | */ |
| 2919 | err = mmc_reinit(mmc); |
Simon Glass | 394dfc0 | 2016-06-12 23:30:22 -0600 | [diff] [blame] | 2920 | #else |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 2921 | /* made sure it's not NULL earlier */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 2922 | err = mmc->cfg->ops->init(mmc); |
Yangbo Lu | c46f5d7 | 2020-09-01 16:57:59 +0800 | [diff] [blame] | 2923 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2924 | if (err) |
| 2925 | return err; |
Andrew Gabbasov | 9fc2a41 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 2926 | mmc->ddr_mode = 0; |
Kishon Vijay Abraham I | 4afb12b | 2017-09-21 16:30:00 +0200 | [diff] [blame] | 2927 | |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 2928 | retry: |
Kishon Vijay Abraham I | 80b87e1 | 2017-09-21 16:30:02 +0200 | [diff] [blame] | 2929 | mmc_set_initial_state(mmc); |
Jean-Jacques Hiblot | 5f23d87 | 2017-09-21 16:30:01 +0200 | [diff] [blame] | 2930 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2931 | /* Reset the Card */ |
| 2932 | err = mmc_go_idle(mmc); |
| 2933 | |
| 2934 | if (err) |
| 2935 | return err; |
| 2936 | |
Marcel Ziswiler | b2b7fc8 | 2019-05-20 02:44:53 +0200 | [diff] [blame] | 2937 | /* The internal partition reset to user partition(0) at every CMD0 */ |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 2938 | mmc_get_blk_desc(mmc)->hwpart = 0; |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 2939 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2940 | /* Test for SD version 2 */ |
Macpaul Lin | 028bde1 | 2011-11-14 23:35:39 +0000 | [diff] [blame] | 2941 | err = mmc_send_if_cond(mmc); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2942 | |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2943 | /* Now try to get the SD card's operating condition */ |
Jean-Jacques Hiblot | f4d5b3e | 2017-09-21 16:30:07 +0200 | [diff] [blame] | 2944 | err = sd_send_op_cond(mmc, uhs_en); |
| 2945 | if (err && uhs_en) { |
| 2946 | uhs_en = false; |
| 2947 | mmc_power_cycle(mmc); |
| 2948 | goto retry; |
| 2949 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2950 | |
| 2951 | /* If the command timed out, we check for an MMC card */ |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 2952 | if (err == -ETIMEDOUT) { |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2953 | err = mmc_send_op_cond(mmc); |
| 2954 | |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 2955 | if (err) { |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 2956 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Pali Rohár | 7c63962 | 2021-07-14 16:37:29 +0200 | [diff] [blame] | 2957 | if (!quiet) |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 2958 | log_err("Card did not respond to voltage select! : %d\n", |
| 2959 | err); |
Paul Burton | 6a7c5ba | 2013-09-04 16:12:25 +0100 | [diff] [blame] | 2960 | #endif |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 2961 | return -EOPNOTSUPP; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 2962 | } |
| 2963 | } |
| 2964 | |
Jon Nettleton | 2663fe4 | 2018-06-11 15:26:19 +0300 | [diff] [blame] | 2965 | return err; |
| 2966 | } |
| 2967 | |
| 2968 | int mmc_start_init(struct mmc *mmc) |
| 2969 | { |
| 2970 | bool no_card; |
| 2971 | int err = 0; |
| 2972 | |
| 2973 | /* |
| 2974 | * all hosts are capable of 1 bit bus-width and able to use the legacy |
| 2975 | * timings. |
| 2976 | */ |
Faiz Abbas | 01db77e | 2020-02-26 13:44:32 +0530 | [diff] [blame] | 2977 | mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(MMC_LEGACY) | |
Aswath Govindraju | bb5b9fe | 2021-08-13 23:04:41 +0530 | [diff] [blame] | 2978 | MMC_MODE_1BIT; |
| 2979 | |
| 2980 | if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET)) { |
| 2981 | if (mmc->user_speed_mode != MMC_MODES_END) { |
| 2982 | int i; |
| 2983 | /* set host caps */ |
| 2984 | if (mmc->host_caps & MMC_CAP(mmc->user_speed_mode)) { |
| 2985 | /* Remove all existing speed capabilities */ |
| 2986 | for (i = MMC_LEGACY; i < MMC_MODES_END; i++) |
| 2987 | mmc->host_caps &= ~MMC_CAP(i); |
| 2988 | mmc->host_caps |= (MMC_CAP(mmc->user_speed_mode) |
| 2989 | | MMC_CAP(MMC_LEGACY) | |
| 2990 | MMC_MODE_1BIT); |
| 2991 | } else { |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 2992 | log_err("bus_mode requested is not supported\n"); |
Aswath Govindraju | bb5b9fe | 2021-08-13 23:04:41 +0530 | [diff] [blame] | 2993 | return -EINVAL; |
| 2994 | } |
| 2995 | } |
| 2996 | } |
Faiz Abbas | f6fd4ec | 2020-02-26 13:44:30 +0530 | [diff] [blame] | 2997 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 2998 | mmc_deferred_probe(mmc); |
| 2999 | #endif |
Jon Nettleton | 2663fe4 | 2018-06-11 15:26:19 +0300 | [diff] [blame] | 3000 | #if !defined(CONFIG_MMC_BROKEN_CD) |
Jon Nettleton | 2663fe4 | 2018-06-11 15:26:19 +0300 | [diff] [blame] | 3001 | no_card = mmc_getcd(mmc) == 0; |
| 3002 | #else |
| 3003 | no_card = 0; |
| 3004 | #endif |
| 3005 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Baruch Siach | 0448ce6 | 2019-07-22 15:52:12 +0300 | [diff] [blame] | 3006 | /* we pretend there's no card when init is NULL */ |
Jon Nettleton | 2663fe4 | 2018-06-11 15:26:19 +0300 | [diff] [blame] | 3007 | no_card = no_card || (mmc->cfg->ops->init == NULL); |
| 3008 | #endif |
| 3009 | if (no_card) { |
| 3010 | mmc->has_init = 0; |
| 3011 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 3012 | log_err("MMC: no card present\n"); |
Jon Nettleton | 2663fe4 | 2018-06-11 15:26:19 +0300 | [diff] [blame] | 3013 | #endif |
| 3014 | return -ENOMEDIUM; |
| 3015 | } |
| 3016 | |
Pali Rohár | 7c63962 | 2021-07-14 16:37:29 +0200 | [diff] [blame] | 3017 | err = mmc_get_op_cond(mmc, false); |
Jon Nettleton | 2663fe4 | 2018-06-11 15:26:19 +0300 | [diff] [blame] | 3018 | |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 3019 | if (!err) |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 3020 | mmc->init_in_progress = 1; |
| 3021 | |
| 3022 | return err; |
| 3023 | } |
| 3024 | |
| 3025 | static int mmc_complete_init(struct mmc *mmc) |
| 3026 | { |
| 3027 | int err = 0; |
| 3028 | |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 3029 | mmc->init_in_progress = 0; |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 3030 | if (mmc->op_cond_pending) |
| 3031 | err = mmc_complete_op_cond(mmc); |
| 3032 | |
| 3033 | if (!err) |
| 3034 | err = mmc_startup(mmc); |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 3035 | if (err) |
| 3036 | mmc->has_init = 0; |
| 3037 | else |
| 3038 | mmc->has_init = 1; |
| 3039 | return err; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 3040 | } |
| 3041 | |
Marek Vasut | b096740 | 2024-09-06 19:10:42 +0200 | [diff] [blame] | 3042 | static void __maybe_unused mmc_cyclic_cd_poll(struct cyclic_info *c) |
| 3043 | { |
| 3044 | struct mmc *m = CONFIG_IS_ENABLED(CYCLIC, (container_of(c, struct mmc, cyclic)), (NULL)); |
| 3045 | |
| 3046 | if (!m->has_init) |
| 3047 | return; |
| 3048 | |
| 3049 | if (mmc_getcd(m)) |
| 3050 | return; |
| 3051 | |
| 3052 | mmc_deinit(m); |
| 3053 | m->has_init = 0; |
| 3054 | } |
| 3055 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 3056 | int mmc_init(struct mmc *mmc) |
| 3057 | { |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 3058 | int err = 0; |
Vipul Kumar | dbad7b4 | 2018-05-03 12:20:54 +0530 | [diff] [blame] | 3059 | __maybe_unused ulong start; |
Simon Glass | 5f4bd8c | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 3060 | #if CONFIG_IS_ENABLED(DM_MMC) |
Simon Glass | 59bc6f2 | 2016-05-01 13:52:41 -0600 | [diff] [blame] | 3061 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev); |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 3062 | |
Simon Glass | 59bc6f2 | 2016-05-01 13:52:41 -0600 | [diff] [blame] | 3063 | upriv->mmc = mmc; |
| 3064 | #endif |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 3065 | if (mmc->has_init) |
| 3066 | return 0; |
Mateusz Zalega | da35178 | 2014-04-29 20:15:30 +0200 | [diff] [blame] | 3067 | |
| 3068 | start = get_timer(0); |
| 3069 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 3070 | if (!mmc->init_in_progress) |
| 3071 | err = mmc_start_init(mmc); |
| 3072 | |
Andrew Gabbasov | 3a669bc | 2015-03-19 07:44:07 -0500 | [diff] [blame] | 3073 | if (!err) |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 3074 | err = mmc_complete_init(mmc); |
Jagan Teki | 9bee2b5 | 2017-01-10 11:18:43 +0100 | [diff] [blame] | 3075 | if (err) |
Masahiro Yamada | f97b148 | 2018-01-28 19:11:42 +0900 | [diff] [blame] | 3076 | pr_info("%s: %d, time %lu\n", __func__, err, get_timer(start)); |
Jagan Teki | 9bee2b5 | 2017-01-10 11:18:43 +0100 | [diff] [blame] | 3077 | |
Marek Vasut | b096740 | 2024-09-06 19:10:42 +0200 | [diff] [blame] | 3078 | if (CONFIG_IS_ENABLED(CYCLIC, (!mmc->cyclic.func), (NULL))) { |
| 3079 | /* Register cyclic function for card detect polling */ |
| 3080 | CONFIG_IS_ENABLED(CYCLIC, (cyclic_register(&mmc->cyclic, |
| 3081 | mmc_cyclic_cd_poll, |
| 3082 | 100 * 1000, |
| 3083 | mmc->cfg->name))); |
| 3084 | } |
| 3085 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 3086 | return err; |
| 3087 | } |
| 3088 | |
Marek Vasut | a4773fc | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 3089 | int mmc_deinit(struct mmc *mmc) |
| 3090 | { |
| 3091 | u32 caps_filtered; |
| 3092 | |
Marek Vasut | b096740 | 2024-09-06 19:10:42 +0200 | [diff] [blame] | 3093 | if (CONFIG_IS_ENABLED(CYCLIC, (mmc->cyclic.func), (NULL))) |
| 3094 | CONFIG_IS_ENABLED(CYCLIC, (cyclic_unregister(&mmc->cyclic))); |
| 3095 | |
Marek Vasut | 67c77f9 | 2024-03-17 04:01:22 +0100 | [diff] [blame] | 3096 | if (!CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) && |
| 3097 | !CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) && |
| 3098 | !CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)) |
| 3099 | return 0; |
| 3100 | |
Marek Vasut | a4773fc | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 3101 | if (!mmc->has_init) |
| 3102 | return 0; |
| 3103 | |
| 3104 | if (IS_SD(mmc)) { |
| 3105 | caps_filtered = mmc->card_caps & |
| 3106 | ~(MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25) | |
| 3107 | MMC_CAP(UHS_SDR50) | MMC_CAP(UHS_DDR50) | |
| 3108 | MMC_CAP(UHS_SDR104)); |
| 3109 | |
| 3110 | return sd_select_mode_and_width(mmc, caps_filtered); |
| 3111 | } else { |
| 3112 | caps_filtered = mmc->card_caps & |
Ye Li | 3679e80 | 2021-08-17 17:20:34 +0800 | [diff] [blame] | 3113 | ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_400_ES)); |
Marek Vasut | a4773fc | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 3114 | |
| 3115 | return mmc_select_mode_and_width(mmc, caps_filtered); |
| 3116 | } |
| 3117 | } |
Marek Vasut | a4773fc | 2019-01-29 04:45:51 +0100 | [diff] [blame] | 3118 | |
Markus Niebel | 0395141 | 2013-12-16 13:40:46 +0100 | [diff] [blame] | 3119 | int mmc_set_dsr(struct mmc *mmc, u16 val) |
| 3120 | { |
| 3121 | mmc->dsr = val; |
| 3122 | return 0; |
| 3123 | } |
| 3124 | |
Jeroen Hofstee | 4772630 | 2014-07-10 22:46:28 +0200 | [diff] [blame] | 3125 | /* CPU-specific MMC initializations */ |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 3126 | __weak int cpu_mmc_init(struct bd_info *bis) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 3127 | { |
| 3128 | return -1; |
| 3129 | } |
| 3130 | |
Jeroen Hofstee | 4772630 | 2014-07-10 22:46:28 +0200 | [diff] [blame] | 3131 | /* board-specific MMC initializations. */ |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 3132 | __weak int board_mmc_init(struct bd_info *bis) |
Jeroen Hofstee | 4772630 | 2014-07-10 22:46:28 +0200 | [diff] [blame] | 3133 | { |
| 3134 | return -1; |
| 3135 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 3136 | |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 3137 | void mmc_set_preinit(struct mmc *mmc, int preinit) |
| 3138 | { |
| 3139 | mmc->preinit = preinit; |
| 3140 | } |
| 3141 | |
Faiz Abbas | b3857fd | 2018-02-12 19:35:24 +0530 | [diff] [blame] | 3142 | #if CONFIG_IS_ENABLED(DM_MMC) |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 3143 | static int mmc_probe(struct bd_info *bis) |
Sjoerd Simons | df8aa52 | 2015-08-30 16:55:45 -0600 | [diff] [blame] | 3144 | { |
Simon Glass | 547cb34 | 2015-12-29 05:22:49 -0700 | [diff] [blame] | 3145 | int ret, i; |
Sjoerd Simons | df8aa52 | 2015-08-30 16:55:45 -0600 | [diff] [blame] | 3146 | struct uclass *uc; |
Simon Glass | 547cb34 | 2015-12-29 05:22:49 -0700 | [diff] [blame] | 3147 | struct udevice *dev; |
Sjoerd Simons | df8aa52 | 2015-08-30 16:55:45 -0600 | [diff] [blame] | 3148 | |
| 3149 | ret = uclass_get(UCLASS_MMC, &uc); |
| 3150 | if (ret) |
| 3151 | return ret; |
| 3152 | |
Simon Glass | 547cb34 | 2015-12-29 05:22:49 -0700 | [diff] [blame] | 3153 | /* |
| 3154 | * Try to add them in sequence order. Really with driver model we |
| 3155 | * should allow holes, but the current MMC list does not allow that. |
| 3156 | * So if we request 0, 1, 3 we will get 0, 1, 2. |
| 3157 | */ |
| 3158 | for (i = 0; ; i++) { |
| 3159 | ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev); |
| 3160 | if (ret == -ENODEV) |
| 3161 | break; |
| 3162 | } |
| 3163 | uclass_foreach_dev(dev, uc) { |
| 3164 | ret = device_probe(dev); |
Sjoerd Simons | df8aa52 | 2015-08-30 16:55:45 -0600 | [diff] [blame] | 3165 | if (ret) |
Simon Glass | 367e385 | 2024-08-22 07:54:55 -0600 | [diff] [blame] | 3166 | log_err("%s - probe failed: %d\n", dev->name, ret); |
Sjoerd Simons | df8aa52 | 2015-08-30 16:55:45 -0600 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | return 0; |
| 3170 | } |
| 3171 | #else |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 3172 | static int mmc_probe(struct bd_info *bis) |
Sjoerd Simons | df8aa52 | 2015-08-30 16:55:45 -0600 | [diff] [blame] | 3173 | { |
| 3174 | if (board_mmc_init(bis) < 0) |
| 3175 | cpu_mmc_init(bis); |
| 3176 | |
| 3177 | return 0; |
| 3178 | } |
| 3179 | #endif |
Che-Liang Chiou | 4a2c7d7 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 3180 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 3181 | int mmc_initialize(struct bd_info *bis) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 3182 | { |
Daniel Kochmański | 13df57b | 2015-05-29 16:55:43 +0200 | [diff] [blame] | 3183 | static int initialized = 0; |
Sjoerd Simons | df8aa52 | 2015-08-30 16:55:45 -0600 | [diff] [blame] | 3184 | int ret; |
Daniel Kochmański | 13df57b | 2015-05-29 16:55:43 +0200 | [diff] [blame] | 3185 | if (initialized) /* Avoid initializing mmc multiple times */ |
| 3186 | return 0; |
| 3187 | initialized = 1; |
| 3188 | |
Simon Glass | 5f4bd8c | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 3189 | #if !CONFIG_IS_ENABLED(BLK) |
Marek Vasut | f537e39 | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 3190 | #if !CONFIG_IS_ENABLED(MMC_TINY) |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 3191 | mmc_list_init(); |
| 3192 | #endif |
Marek Vasut | f537e39 | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 3193 | #endif |
Sjoerd Simons | df8aa52 | 2015-08-30 16:55:45 -0600 | [diff] [blame] | 3194 | ret = mmc_probe(bis); |
| 3195 | if (ret) |
| 3196 | return ret; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 3197 | |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 3198 | #ifndef CONFIG_SPL_BUILD |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 3199 | print_mmc_devices(','); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 3200 | #endif |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 3201 | |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 3202 | mmc_do_preinit(); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 3203 | return 0; |
| 3204 | } |
Tomas Melin | c17dae5 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 3205 | |
Lokesh Vutla | c59b41c | 2019-09-09 14:40:36 +0530 | [diff] [blame] | 3206 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 3207 | int mmc_init_device(int num) |
| 3208 | { |
| 3209 | struct udevice *dev; |
| 3210 | struct mmc *m; |
| 3211 | int ret; |
| 3212 | |
Aswath Govindraju | 57e2ccb | 2021-03-25 12:48:47 +0530 | [diff] [blame] | 3213 | if (uclass_get_device_by_seq(UCLASS_MMC, num, &dev)) { |
| 3214 | ret = uclass_get_device(UCLASS_MMC, num, &dev); |
| 3215 | if (ret) |
Simon Glass | 9c3e926 | 2024-09-20 09:24:38 +0200 | [diff] [blame] | 3216 | return log_msg_ret("ini", ret); |
Aswath Govindraju | 57e2ccb | 2021-03-25 12:48:47 +0530 | [diff] [blame] | 3217 | } |
Lokesh Vutla | c59b41c | 2019-09-09 14:40:36 +0530 | [diff] [blame] | 3218 | |
| 3219 | m = mmc_get_mmc_dev(dev); |
| 3220 | if (!m) |
| 3221 | return 0; |
Venkatesh Yadav Abbarapu | 6738fcb | 2022-09-29 10:22:49 +0530 | [diff] [blame] | 3222 | |
| 3223 | /* Initialising user set speed mode */ |
| 3224 | m->user_speed_mode = MMC_MODES_END; |
| 3225 | |
Lokesh Vutla | c59b41c | 2019-09-09 14:40:36 +0530 | [diff] [blame] | 3226 | if (m->preinit) |
| 3227 | mmc_start_init(m); |
| 3228 | |
| 3229 | return 0; |
| 3230 | } |
| 3231 | #endif |
| 3232 | |
Tomas Melin | c17dae5 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 3233 | #ifdef CONFIG_CMD_BKOPS_ENABLE |
Marek Vasut | efdeed6 | 2023-01-05 15:19:08 +0100 | [diff] [blame] | 3234 | int mmc_set_bkops_enable(struct mmc *mmc, bool autobkops, bool enable) |
Tomas Melin | c17dae5 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 3235 | { |
| 3236 | int err; |
Marek Vasut | efdeed6 | 2023-01-05 15:19:08 +0100 | [diff] [blame] | 3237 | u32 bit = autobkops ? BIT(1) : BIT(0); |
Tomas Melin | c17dae5 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 3238 | ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); |
| 3239 | |
| 3240 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 3241 | if (err) { |
| 3242 | puts("Could not get ext_csd register values\n"); |
| 3243 | return err; |
| 3244 | } |
| 3245 | |
| 3246 | if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) { |
| 3247 | puts("Background operations not supported on device\n"); |
| 3248 | return -EMEDIUMTYPE; |
| 3249 | } |
| 3250 | |
Marek Vasut | efdeed6 | 2023-01-05 15:19:08 +0100 | [diff] [blame] | 3251 | if (enable && (ext_csd[EXT_CSD_BKOPS_EN] & bit)) { |
Tomas Melin | c17dae5 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 3252 | puts("Background operations already enabled\n"); |
| 3253 | return 0; |
| 3254 | } |
| 3255 | |
Marek Vasut | efdeed6 | 2023-01-05 15:19:08 +0100 | [diff] [blame] | 3256 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, |
| 3257 | enable ? bit : 0); |
Tomas Melin | c17dae5 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 3258 | if (err) { |
Marek Vasut | efdeed6 | 2023-01-05 15:19:08 +0100 | [diff] [blame] | 3259 | printf("Failed to %sable manual background operations\n", |
| 3260 | enable ? "en" : "dis"); |
Tomas Melin | c17dae5 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 3261 | return err; |
| 3262 | } |
| 3263 | |
Marek Vasut | efdeed6 | 2023-01-05 15:19:08 +0100 | [diff] [blame] | 3264 | printf("%sabled %s background operations\n", |
| 3265 | enable ? "En" : "Dis", autobkops ? "auto" : "manual"); |
Tomas Melin | c17dae5 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 3266 | |
| 3267 | return 0; |
| 3268 | } |
| 3269 | #endif |
David Woodhouse | 49fee03 | 2020-08-04 10:05:46 +0100 | [diff] [blame] | 3270 | |
| 3271 | __weak int mmc_get_env_dev(void) |
| 3272 | { |
| 3273 | #ifdef CONFIG_SYS_MMC_ENV_DEV |
| 3274 | return CONFIG_SYS_MMC_ENV_DEV; |
| 3275 | #else |
| 3276 | return 0; |
| 3277 | #endif |
| 3278 | } |