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