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