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