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