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