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