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