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