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