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