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