blob: 31cfda288587119cf65de3e483be69d6437bcedc [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleming272cc702008-10-30 16:41:01 -05002/*
3 * Copyright 2008, Freescale Semiconductor, Inc
Yangbo Lu39913ac2020-06-17 18:08:58 +08004 * Copyright 2020 NXP
Andy Fleming272cc702008-10-30 16:41:01 -05005 * Andy Fleming
6 *
7 * Based vaguely on the Linux code
Andy Fleming272cc702008-10-30 16:41:01 -05008 */
9
10#include <config.h>
11#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060012#include <blk.h>
Andy Fleming272cc702008-10-30 16:41:01 -050013#include <command.h>
Sjoerd Simons8e3332e2015-08-30 16:55:45 -060014#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Sjoerd Simons8e3332e2015-08-30 16:55:45 -060016#include <dm/device-internal.h>
Stephen Warrend4622df2014-05-23 12:47:06 -060017#include <errno.h>
Andy Fleming272cc702008-10-30 16:41:01 -050018#include <mmc.h>
19#include <part.h>
Simon Glasscd93d622020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060021#include <linux/delay.h>
Peng Fan2051aef2016-10-11 15:08:43 +080022#include <power/regulator.h>
Andy Fleming272cc702008-10-30 16:41:01 -050023#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060024#include <memalign.h>
Andy Fleming272cc702008-10-30 16:41:01 -050025#include <linux/list.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053026#include <div64.h>
Paul Burtonda61fa52013-09-09 15:30:26 +010027#include "mmc_private.h"
Andy Fleming272cc702008-10-30 16:41:01 -050028
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +020029#define DEFAULT_CMD6_TIMEOUT_MS 500
30
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +020031static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
Marek Vasutb5b838f2016-12-01 02:06:33 +010032
Simon Glasse7881d82017-07-29 11:35:31 -060033#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020034
Sam Protsenko6cf8a902019-08-14 22:52:51 +030035static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020036{
Loic Poulaind6ad5a02022-05-26 16:37:21 +020037 if (mmc->cfg->ops->wait_dat0)
38 return mmc->cfg->ops->wait_dat0(mmc, state, timeout_us);
39
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020040 return -ENOSYS;
41}
42
Jeroen Hofstee750121c2014-07-12 21:24:08 +020043__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000044{
45 return -1;
46}
47
48int mmc_getwp(struct mmc *mmc)
49{
50 int wp;
51
52 wp = board_mmc_getwp(mmc);
53
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000054 if (wp < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020055 if (mmc->cfg->ops->getwp)
56 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000057 else
58 wp = 0;
59 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000060
61 return wp;
62}
63
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +020064__weak int board_mmc_getcd(struct mmc *mmc)
65{
Stefano Babic11fdade2010-02-05 15:04:43 +010066 return -1;
67}
Simon Glass8ca51e52016-06-12 23:30:22 -060068#endif
Stefano Babic11fdade2010-02-05 15:04:43 +010069
Marek Vasut8635ff92012-03-15 18:41:35 +000070#ifdef CONFIG_MMC_TRACE
Simon Glassc0c76eb2016-06-12 23:30:20 -060071void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
72{
73 printf("CMD_SEND:%d\n", cmd->cmdidx);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010074 printf("\t\tARG\t\t\t 0x%08x\n", cmd->cmdarg);
Simon Glassc0c76eb2016-06-12 23:30:20 -060075}
76
77void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
78{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000079 int i;
80 u8 *ptr;
81
Bin Meng7863ce52016-03-17 21:53:14 -070082 if (ret) {
83 printf("\t\tRET\t\t\t %d\n", ret);
84 } else {
85 switch (cmd->resp_type) {
86 case MMC_RSP_NONE:
87 printf("\t\tMMC_RSP_NONE\n");
88 break;
89 case MMC_RSP_R1:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010090 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070091 cmd->response[0]);
92 break;
93 case MMC_RSP_R1b:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010094 printf("\t\tMMC_RSP_R1b\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070095 cmd->response[0]);
96 break;
97 case MMC_RSP_R2:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010098 printf("\t\tMMC_RSP_R2\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070099 cmd->response[0]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100100 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700101 cmd->response[1]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100102 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700103 cmd->response[2]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100104 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700105 cmd->response[3]);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000106 printf("\n");
Bin Meng7863ce52016-03-17 21:53:14 -0700107 printf("\t\t\t\t\tDUMPING DATA\n");
108 for (i = 0; i < 4; i++) {
109 int j;
110 printf("\t\t\t\t\t%03d - ", i*4);
111 ptr = (u8 *)&cmd->response[i];
112 ptr += 3;
113 for (j = 0; j < 4; j++)
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100114 printf("%02x ", *ptr--);
Bin Meng7863ce52016-03-17 21:53:14 -0700115 printf("\n");
116 }
117 break;
118 case MMC_RSP_R3:
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100119 printf("\t\tMMC_RSP_R3,4\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700120 cmd->response[0]);
121 break;
122 default:
123 printf("\t\tERROR MMC rsp not supported\n");
124 break;
Bin Meng53e8e402016-03-17 21:53:13 -0700125 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000126 }
Simon Glassc0c76eb2016-06-12 23:30:20 -0600127}
128
129void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
130{
131 int status;
132
133 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
134 printf("CURR STATE:%d\n", status);
135}
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000136#endif
Simon Glassc0c76eb2016-06-12 23:30:20 -0600137
Pali Rohár48467e42022-04-03 00:20:10 +0200138#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG) || CONFIG_VAL(LOGLEVEL) >= LOGL_DEBUG
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200139const char *mmc_mode_name(enum bus_mode mode)
140{
141 static const char *const names[] = {
142 [MMC_LEGACY] = "MMC legacy",
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200143 [MMC_HS] = "MMC High Speed (26MHz)",
144 [SD_HS] = "SD High Speed (50MHz)",
145 [UHS_SDR12] = "UHS SDR12 (25MHz)",
146 [UHS_SDR25] = "UHS SDR25 (50MHz)",
147 [UHS_SDR50] = "UHS SDR50 (100MHz)",
148 [UHS_SDR104] = "UHS SDR104 (208MHz)",
149 [UHS_DDR50] = "UHS DDR50 (50MHz)",
150 [MMC_HS_52] = "MMC High Speed (52MHz)",
151 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
152 [MMC_HS_200] = "HS200 (200MHz)",
Peng Fan3dd26262018-08-10 14:07:54 +0800153 [MMC_HS_400] = "HS400 (200MHz)",
Peng Fan44acd492019-07-10 14:43:07 +0800154 [MMC_HS_400_ES] = "HS400ES (200MHz)",
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200155 };
156
157 if (mode >= MMC_MODES_END)
158 return "Unknown mode";
159 else
160 return names[mode];
161}
162#endif
163
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200164static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
165{
166 static const int freqs[] = {
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900167 [MMC_LEGACY] = 25000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200168 [MMC_HS] = 26000000,
169 [SD_HS] = 50000000,
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900170 [MMC_HS_52] = 52000000,
171 [MMC_DDR_52] = 52000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200172 [UHS_SDR12] = 25000000,
173 [UHS_SDR25] = 50000000,
174 [UHS_SDR50] = 100000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200175 [UHS_DDR50] = 50000000,
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100176 [UHS_SDR104] = 208000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200177 [MMC_HS_200] = 200000000,
Peng Fan3dd26262018-08-10 14:07:54 +0800178 [MMC_HS_400] = 200000000,
Peng Fan44acd492019-07-10 14:43:07 +0800179 [MMC_HS_400_ES] = 200000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200180 };
181
182 if (mode == MMC_LEGACY)
183 return mmc->legacy_speed;
184 else if (mode >= MMC_MODES_END)
185 return 0;
186 else
187 return freqs[mode];
188}
189
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200190static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
191{
192 mmc->selected_mode = mode;
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200193 mmc->tran_speed = mmc_mode2freq(mmc, mode);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200194 mmc->ddr_mode = mmc_is_mode_ddr(mode);
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900195 pr_debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
196 mmc->tran_speed / 1000000);
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200197 return 0;
198}
199
Simon Glasse7881d82017-07-29 11:35:31 -0600200#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassc0c76eb2016-06-12 23:30:20 -0600201int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
202{
203 int ret;
204
205 mmmc_trace_before_send(mmc, cmd);
206 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
207 mmmc_trace_after_send(mmc, cmd, ret);
208
Marek Vasut8635ff92012-03-15 18:41:35 +0000209 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500210}
Simon Glass8ca51e52016-06-12 23:30:22 -0600211#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500212
Sean Andersonda129172020-10-17 08:36:27 -0400213/**
214 * mmc_send_cmd_retry() - send a command to the mmc device, retrying on error
215 *
216 * @dev: device to receive the command
217 * @cmd: command to send
218 * @data: additional data to send/receive
219 * @retries: how many times to retry; mmc_send_cmd is always called at least
220 * once
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100221 * Return: 0 if ok, -ve on error
Sean Andersonda129172020-10-17 08:36:27 -0400222 */
223static int mmc_send_cmd_retry(struct mmc *mmc, struct mmc_cmd *cmd,
224 struct mmc_data *data, uint retries)
225{
226 int ret;
227
228 do {
229 ret = mmc_send_cmd(mmc, cmd, data);
230 } while (ret && retries--);
231
232 return ret;
233}
234
235/**
236 * mmc_send_cmd_quirks() - send a command to the mmc device, retrying if a
237 * specific quirk is enabled
238 *
239 * @dev: device to receive the command
240 * @cmd: command to send
241 * @data: additional data to send/receive
242 * @quirk: retry only if this quirk is enabled
243 * @retries: how many times to retry; mmc_send_cmd is always called at least
244 * once
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100245 * Return: 0 if ok, -ve on error
Sean Andersonda129172020-10-17 08:36:27 -0400246 */
247static int mmc_send_cmd_quirks(struct mmc *mmc, struct mmc_cmd *cmd,
248 struct mmc_data *data, u32 quirk, uint retries)
249{
Simon Glass497b7c62023-02-05 15:40:16 -0700250 if (IS_ENABLED(CONFIG_MMC_QUIRKS) && mmc->quirks & quirk)
Sean Andersonda129172020-10-17 08:36:27 -0400251 return mmc_send_cmd_retry(mmc, cmd, data, retries);
252 else
253 return mmc_send_cmd(mmc, cmd, data);
254}
255
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200256int mmc_send_status(struct mmc *mmc, unsigned int *status)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000257{
258 struct mmc_cmd cmd;
Sean Andersonda129172020-10-17 08:36:27 -0400259 int ret;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000260
261 cmd.cmdidx = MMC_CMD_SEND_STATUS;
262 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200263 if (!mmc_host_is_spi(mmc))
264 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000265
Sean Andersonda129172020-10-17 08:36:27 -0400266 ret = mmc_send_cmd_retry(mmc, &cmd, NULL, 4);
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200267 mmc_trace_state(mmc, &cmd);
Sean Andersonda129172020-10-17 08:36:27 -0400268 if (!ret)
269 *status = cmd.response[0];
270
271 return ret;
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200272}
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200273
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300274int mmc_poll_for_busy(struct mmc *mmc, int timeout_ms)
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200275{
276 unsigned int status;
277 int err;
278
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300279 err = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotcd0b80e2019-07-02 10:53:53 +0200280 if (err != -ENOSYS)
281 return err;
282
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200283 while (1) {
284 err = mmc_send_status(mmc, &status);
285 if (err)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000286 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000287
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200288 if ((status & MMC_STATUS_RDY_FOR_DATA) &&
289 (status & MMC_STATUS_CURR_STATE) !=
290 MMC_STATE_PRG)
291 break;
292
293 if (status & MMC_STATUS_MASK) {
294#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
295 pr_err("Status Error: 0x%08x\n", status);
296#endif
297 return -ECOMM;
298 }
299
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300300 if (timeout_ms-- <= 0)
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500301 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000302
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500303 udelay(1000);
304 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000305
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300306 if (timeout_ms <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100307#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100308 pr_err("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100309#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900310 return -ETIMEDOUT;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000311 }
312
313 return 0;
314}
315
Paul Burtonda61fa52013-09-09 15:30:26 +0100316int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500317{
318 struct mmc_cmd cmd;
319
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600320 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900321 return 0;
322
Andy Fleming272cc702008-10-30 16:41:01 -0500323 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
324 cmd.resp_type = MMC_RSP_R1;
325 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500326
Sean Andersonda129172020-10-17 08:36:27 -0400327 return mmc_send_cmd_quirks(mmc, &cmd, NULL,
328 MMC_QUIRK_RETRY_SET_BLOCKLEN, 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500329}
330
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100331#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200332static const u8 tuning_blk_pattern_4bit[] = {
333 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
334 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
335 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
336 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
337 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
338 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
339 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
340 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
341};
342
343static const u8 tuning_blk_pattern_8bit[] = {
344 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
345 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
346 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
347 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
348 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
349 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
350 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
351 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
352 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
353 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
354 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
355 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
356 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
357 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
358 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
359 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
360};
361
362int mmc_send_tuning(struct mmc *mmc, u32 opcode, int *cmd_error)
363{
364 struct mmc_cmd cmd;
365 struct mmc_data data;
366 const u8 *tuning_block_pattern;
367 int size, err;
368
369 if (mmc->bus_width == 8) {
370 tuning_block_pattern = tuning_blk_pattern_8bit;
371 size = sizeof(tuning_blk_pattern_8bit);
372 } else if (mmc->bus_width == 4) {
373 tuning_block_pattern = tuning_blk_pattern_4bit;
374 size = sizeof(tuning_blk_pattern_4bit);
375 } else {
376 return -EINVAL;
377 }
378
379 ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size);
380
381 cmd.cmdidx = opcode;
382 cmd.cmdarg = 0;
383 cmd.resp_type = MMC_RSP_R1;
384
385 data.dest = (void *)data_buf;
386 data.blocks = 1;
387 data.blocksize = size;
388 data.flags = MMC_DATA_READ;
389
390 err = mmc_send_cmd(mmc, &cmd, &data);
391 if (err)
392 return err;
393
394 if (memcmp(data_buf, tuning_block_pattern, size))
395 return -EIO;
396
397 return 0;
398}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100399#endif
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200400
Hai Pham0ac2cca2023-06-20 00:38:24 +0200401int mmc_send_stop_transmission(struct mmc *mmc, bool write)
402{
403 struct mmc_cmd cmd;
404
405 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
406 cmd.cmdarg = 0;
407 /*
408 * JEDEC Standard No. 84-B51 Page 126
409 * CMD12 STOP_TRANSMISSION R1/R1b[3]
410 * NOTE 3 R1 for read cases and R1b for write cases.
411 *
412 * Physical Layer Simplified Specification Version 9.00
413 * 7.3.1.3 Detailed Command Description
414 * CMD12 R1b
415 */
416 cmd.resp_type = (IS_SD(mmc) || write) ? MMC_RSP_R1b : MMC_RSP_R1;
417
418 return mmc_send_cmd(mmc, &cmd, NULL);
419}
420
Sascha Silbeff8fef52013-06-14 13:07:25 +0200421static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000422 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500423{
424 struct mmc_cmd cmd;
425 struct mmc_data data;
426
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700427 if (blkcnt > 1)
428 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
429 else
430 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500431
432 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700433 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500434 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700435 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500436
437 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500438
439 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700440 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500441 data.blocksize = mmc->read_bl_len;
442 data.flags = MMC_DATA_READ;
443
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700444 if (mmc_send_cmd(mmc, &cmd, &data))
445 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500446
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700447 if (blkcnt > 1) {
Hai Pham0ac2cca2023-06-20 00:38:24 +0200448 if (mmc_send_stop_transmission(mmc, false)) {
Paul Burton56196822013-09-04 16:12:25 +0100449#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100450 pr_err("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100451#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700452 return 0;
453 }
Andy Fleming272cc702008-10-30 16:41:01 -0500454 }
455
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700456 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500457}
458
Marek Vasut145429a2020-04-04 12:45:05 +0200459#if !CONFIG_IS_ENABLED(DM_MMC)
460static int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
461{
462 if (mmc->cfg->ops->get_b_max)
463 return mmc->cfg->ops->get_b_max(mmc, dst, blkcnt);
464 else
465 return mmc->cfg->b_max;
466}
467#endif
468
Simon Glassc4d660d2017-07-04 13:31:19 -0600469#if CONFIG_IS_ENABLED(BLK)
Simon Glass7dba0b92016-06-12 23:30:15 -0600470ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600471#else
Simon Glass7dba0b92016-06-12 23:30:15 -0600472ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
473 void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600474#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500475{
Simon Glassc4d660d2017-07-04 13:31:19 -0600476#if CONFIG_IS_ENABLED(BLK)
Simon Glasscaa4daa2020-12-03 16:55:18 -0700477 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass33fb2112016-05-01 13:52:41 -0600478#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700479 int dev_num = block_dev->devnum;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700480 int err;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700481 lbaint_t cur, blocks_todo = blkcnt;
Marek Vasut145429a2020-04-04 12:45:05 +0200482 uint b_max;
Andy Fleming272cc702008-10-30 16:41:01 -0500483
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700484 if (blkcnt == 0)
485 return 0;
486
487 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500488 if (!mmc)
489 return 0;
490
Marek Vasutb5b838f2016-12-01 02:06:33 +0100491 if (CONFIG_IS_ENABLED(MMC_TINY))
492 err = mmc_switch_part(mmc, block_dev->hwpart);
493 else
494 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
495
Stephen Warren873cc1d2015-12-07 11:38:49 -0700496 if (err < 0)
497 return 0;
498
Simon Glassc40fdca2016-05-01 13:52:35 -0600499 if ((start + blkcnt) > block_dev->lba) {
Paul Burton56196822013-09-04 16:12:25 +0100500#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100501 pr_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
502 start + blkcnt, block_dev->lba);
Paul Burton56196822013-09-04 16:12:25 +0100503#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800504 return 0;
505 }
Andy Fleming272cc702008-10-30 16:41:01 -0500506
Simon Glass11692992015-06-23 15:38:50 -0600507 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900508 pr_debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500509 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600510 }
Andy Fleming272cc702008-10-30 16:41:01 -0500511
Marek Vasut145429a2020-04-04 12:45:05 +0200512 b_max = mmc_get_b_max(mmc, dst, blkcnt);
513
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700514 do {
Marek Vasut145429a2020-04-04 12:45:05 +0200515 cur = (blocks_todo > b_max) ? b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600516 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900517 pr_debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700518 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600519 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700520 blocks_todo -= cur;
521 start += cur;
522 dst += cur * mmc->read_bl_len;
523 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500524
525 return blkcnt;
526}
527
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000528static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500529{
530 struct mmc_cmd cmd;
531 int err;
532
533 udelay(1000);
534
535 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
536 cmd.cmdarg = 0;
537 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500538
539 err = mmc_send_cmd(mmc, &cmd, NULL);
540
541 if (err)
542 return err;
543
544 udelay(2000);
545
546 return 0;
547}
548
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100549#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200550static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
551{
552 struct mmc_cmd cmd;
553 int err = 0;
554
555 /*
556 * Send CMD11 only if the request is to switch the card to
557 * 1.8V signalling.
558 */
559 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
560 return mmc_set_signal_voltage(mmc, signal_voltage);
561
562 cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
563 cmd.cmdarg = 0;
564 cmd.resp_type = MMC_RSP_R1;
565
566 err = mmc_send_cmd(mmc, &cmd, NULL);
567 if (err)
568 return err;
569
570 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
571 return -EIO;
572
573 /*
574 * The card should drive cmd and dat[0:3] low immediately
575 * after the response of cmd11, but wait 100 us to be sure
576 */
577 err = mmc_wait_dat0(mmc, 0, 100);
578 if (err == -ENOSYS)
579 udelay(100);
580 else if (err)
581 return -ETIMEDOUT;
582
583 /*
584 * During a signal voltage level switch, the clock must be gated
585 * for 5 ms according to the SD spec
586 */
Jaehoon Chung65117182018-01-26 19:25:29 +0900587 mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200588
589 err = mmc_set_signal_voltage(mmc, signal_voltage);
590 if (err)
591 return err;
592
593 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
594 mdelay(10);
Jaehoon Chung65117182018-01-26 19:25:29 +0900595 mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200596
597 /*
598 * Failure to switch is indicated by the card holding
599 * dat[0:3] low. Wait for at least 1 ms according to spec
600 */
601 err = mmc_wait_dat0(mmc, 1, 1000);
602 if (err == -ENOSYS)
603 udelay(1000);
604 else if (err)
605 return -ETIMEDOUT;
606
607 return 0;
608}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100609#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200610
611static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
Andy Fleming272cc702008-10-30 16:41:01 -0500612{
613 int timeout = 1000;
614 int err;
615 struct mmc_cmd cmd;
616
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500617 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500618 cmd.cmdidx = MMC_CMD_APP_CMD;
619 cmd.resp_type = MMC_RSP_R1;
620 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500621
622 err = mmc_send_cmd(mmc, &cmd, NULL);
623
624 if (err)
625 return err;
626
627 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
628 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100629
630 /*
631 * Most cards do not answer if some reserved bits
632 * in the ocr are set. However, Some controller
633 * can set bit 7 (reserved for low voltages), but
634 * how to manage low voltages SD card is not yet
635 * specified.
636 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000637 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200638 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500639
640 if (mmc->version == SD_VERSION_2)
641 cmd.cmdarg |= OCR_HCS;
642
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200643 if (uhs_en)
644 cmd.cmdarg |= OCR_S18R;
645
Andy Fleming272cc702008-10-30 16:41:01 -0500646 err = mmc_send_cmd(mmc, &cmd, NULL);
647
648 if (err)
649 return err;
650
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500651 if (cmd.response[0] & OCR_BUSY)
652 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500653
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500654 if (timeout-- <= 0)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900655 return -EOPNOTSUPP;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500656
657 udelay(1000);
658 }
Andy Fleming272cc702008-10-30 16:41:01 -0500659
660 if (mmc->version != SD_VERSION_2)
661 mmc->version = SD_VERSION_1_0;
662
Thomas Choud52ebf12010-12-24 13:12:21 +0000663 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
664 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
665 cmd.resp_type = MMC_RSP_R3;
666 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000667
668 err = mmc_send_cmd(mmc, &cmd, NULL);
669
670 if (err)
671 return err;
672 }
673
Rabin Vincent998be3d2009-04-05 13:30:56 +0530674 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500675
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100676#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200677 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
678 == 0x41000000) {
679 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
680 if (err)
681 return err;
682 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100683#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200684
Andy Fleming272cc702008-10-30 16:41:01 -0500685 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
686 mmc->rca = 0;
687
688 return 0;
689}
690
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500691static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500692{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500693 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500694 int err;
695
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500696 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
697 cmd.resp_type = MMC_RSP_R3;
698 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500699 if (use_arg && !mmc_host_is_spi(mmc))
700 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200701 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500702 (mmc->ocr & OCR_VOLTAGE_MASK)) |
703 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000704
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500705 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000706 if (err)
707 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500708 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000709 return 0;
710}
711
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200712static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000713{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000714 int err, i;
Haibo Chenfe959052020-06-15 17:18:12 +0800715 int timeout = 1000;
716 uint start;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000717
Andy Fleming272cc702008-10-30 16:41:01 -0500718 /* Some cards seem to need this */
719 mmc_go_idle(mmc);
720
Haibo Chenfe959052020-06-15 17:18:12 +0800721 start = get_timer(0);
Wolfgang Denk0cf207e2021-09-27 17:42:39 +0200722 /* Asking to the card its capabilities */
Haibo Chenfe959052020-06-15 17:18:12 +0800723 for (i = 0; ; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500724 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500725 if (err)
726 return err;
727
Che-Liang Chioue9550442012-11-28 15:21:13 +0000728 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500729 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500730 break;
Haibo Chenfe959052020-06-15 17:18:12 +0800731
732 if (get_timer(start) > timeout)
733 return -ETIMEDOUT;
734 udelay(100);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000735 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500736 mmc->op_cond_pending = 1;
737 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000738}
Andy Fleming272cc702008-10-30 16:41:01 -0500739
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200740static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000741{
742 struct mmc_cmd cmd;
743 int timeout = 1000;
Vipul Kumar36332b62018-05-03 12:20:54 +0530744 ulong start;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000745 int err;
746
747 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500748 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lud188b112016-08-02 15:33:18 +0800749 /* Some cards seem to need this */
750 mmc_go_idle(mmc);
751
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500752 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500753 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500754 err = mmc_send_op_cond_iter(mmc, 1);
755 if (err)
756 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500757 if (mmc->ocr & OCR_BUSY)
758 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500759 if (get_timer(start) > timeout)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900760 return -EOPNOTSUPP;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500761 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500762 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500763 }
Andy Fleming272cc702008-10-30 16:41:01 -0500764
Thomas Choud52ebf12010-12-24 13:12:21 +0000765 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
766 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
767 cmd.resp_type = MMC_RSP_R3;
768 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000769
770 err = mmc_send_cmd(mmc, &cmd, NULL);
771
772 if (err)
773 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500774
775 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000776 }
777
Andy Fleming272cc702008-10-30 16:41:01 -0500778 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500779
780 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700781 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500782
783 return 0;
784}
785
786
Heinrich Schuchardt1601ea22020-03-30 07:24:17 +0200787int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500788{
789 struct mmc_cmd cmd;
790 struct mmc_data data;
791 int err;
792
793 /* Get the Card Status Register */
794 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
795 cmd.resp_type = MMC_RSP_R1;
796 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500797
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000798 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500799 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000800 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500801 data.flags = MMC_DATA_READ;
802
803 err = mmc_send_cmd(mmc, &cmd, &data);
804
805 return err;
806}
807
Marek Vasut68925502019-02-06 11:34:27 +0100808static int __mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value,
809 bool send_status)
Andy Fleming272cc702008-10-30 16:41:01 -0500810{
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200811 unsigned int status, start;
Andy Fleming272cc702008-10-30 16:41:01 -0500812 struct mmc_cmd cmd;
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300813 int timeout_ms = DEFAULT_CMD6_TIMEOUT_MS;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200814 bool is_part_switch = (set == EXT_CSD_CMD_SET_NORMAL) &&
815 (index == EXT_CSD_PART_CONF);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000816 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500817
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200818 if (mmc->gen_cmd6_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300819 timeout_ms = mmc->gen_cmd6_time * 10;
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200820
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200821 if (is_part_switch && mmc->part_switch_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300822 timeout_ms = mmc->part_switch_time * 10;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200823
Andy Fleming272cc702008-10-30 16:41:01 -0500824 cmd.cmdidx = MMC_CMD_SWITCH;
825 cmd.resp_type = MMC_RSP_R1b;
826 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000827 (index << 16) |
828 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500829
Sean Andersonda129172020-10-17 08:36:27 -0400830 ret = mmc_send_cmd_retry(mmc, &cmd, NULL, 3);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200831 if (ret)
832 return ret;
833
834 start = get_timer(0);
835
836 /* poll dat0 for rdy/buys status */
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300837 ret = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200838 if (ret && ret != -ENOSYS)
839 return ret;
840
841 /*
Kirill Kapranov44645f82021-10-09 23:49:59 +0300842 * In cases when neiter allowed to poll by using CMD13 nor we are
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200843 * capable of polling by using mmc_wait_dat0, then rely on waiting the
844 * stated timeout to be sufficient.
845 */
Kirill Kapranov44645f82021-10-09 23:49:59 +0300846 if (ret == -ENOSYS && !send_status) {
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300847 mdelay(timeout_ms);
Haibo Chenef5ab0d2020-09-22 18:11:42 +0800848 return 0;
849 }
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200850
Marek Vasut2349ecf2022-07-15 01:58:24 +0200851 if (!send_status)
852 return 0;
853
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200854 /* Finally wait until the card is ready or indicates a failure
855 * to switch. It doesn't hurt to use CMD13 here even if send_status
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300856 * is false, because by now (after 'timeout_ms' ms) the bus should be
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200857 * reliable.
858 */
859 do {
860 ret = mmc_send_status(mmc, &status);
861
862 if (!ret && (status & MMC_STATUS_SWITCH_ERROR)) {
863 pr_debug("switch failed %d/%d/0x%x !\n", set, index,
864 value);
865 return -EIO;
Maxime Riparda9003dc2016-11-04 16:18:08 +0100866 }
Stefan Bosch8e2b0af2021-01-23 13:37:41 +0100867 if (!ret && (status & MMC_STATUS_RDY_FOR_DATA) &&
868 (status & MMC_STATUS_CURR_STATE) == MMC_STATE_TRANS)
Marek Vasut68925502019-02-06 11:34:27 +0100869 return 0;
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200870 udelay(100);
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300871 } while (get_timer(start) < timeout_ms);
Marek Vasut68925502019-02-06 11:34:27 +0100872
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200873 return -ETIMEDOUT;
Andy Fleming272cc702008-10-30 16:41:01 -0500874}
875
Marek Vasut68925502019-02-06 11:34:27 +0100876int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
877{
878 return __mmc_switch(mmc, set, index, value, true);
879}
880
Heinrich Schuchardt0469d842020-03-30 07:24:19 +0200881int mmc_boot_wp(struct mmc *mmc)
882{
883 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, 1);
884}
885
Ying-Chun Liu (PaulLiu)19a29ff2022-04-25 21:59:02 +0800886int mmc_boot_wp_single_partition(struct mmc *mmc, int partition)
887{
888 u8 value;
889 int ret;
890
891 value = EXT_CSD_BOOT_WP_B_PWR_WP_EN;
892
893 if (partition == 0) {
894 value |= EXT_CSD_BOOT_WP_B_SEC_WP_SEL;
895 ret = mmc_switch(mmc,
896 EXT_CSD_CMD_SET_NORMAL,
897 EXT_CSD_BOOT_WP,
898 value);
899 } else if (partition == 1) {
900 value |= EXT_CSD_BOOT_WP_B_SEC_WP_SEL;
901 value |= EXT_CSD_BOOT_WP_B_PWR_WP_SEC_SEL;
902 ret = mmc_switch(mmc,
903 EXT_CSD_CMD_SET_NORMAL,
904 EXT_CSD_BOOT_WP,
905 value);
906 } else {
907 ret = mmc_boot_wp(mmc);
908 }
909
910 return ret;
911}
912
Marek Vasut62d77ce2018-04-15 00:37:11 +0200913#if !CONFIG_IS_ENABLED(MMC_TINY)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100914static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode,
915 bool hsdowngrade)
Andy Fleming272cc702008-10-30 16:41:01 -0500916{
Andy Fleming272cc702008-10-30 16:41:01 -0500917 int err;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200918 int speed_bits;
919
920 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
921
922 switch (mode) {
923 case MMC_HS:
924 case MMC_HS_52:
925 case MMC_DDR_52:
926 speed_bits = EXT_CSD_TIMING_HS;
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200927 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100928#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200929 case MMC_HS_200:
930 speed_bits = EXT_CSD_TIMING_HS200;
931 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100932#endif
Peng Fan3dd26262018-08-10 14:07:54 +0800933#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
934 case MMC_HS_400:
935 speed_bits = EXT_CSD_TIMING_HS400;
936 break;
937#endif
Peng Fan44acd492019-07-10 14:43:07 +0800938#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
939 case MMC_HS_400_ES:
940 speed_bits = EXT_CSD_TIMING_HS400;
941 break;
942#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200943 case MMC_LEGACY:
944 speed_bits = EXT_CSD_TIMING_LEGACY;
945 break;
946 default:
947 return -EINVAL;
948 }
Marek Vasut68925502019-02-06 11:34:27 +0100949
950 err = __mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
951 speed_bits, !hsdowngrade);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200952 if (err)
953 return err;
954
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100955#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
956 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
957 /*
958 * In case the eMMC is in HS200/HS400 mode and we are downgrading
959 * to HS mode, the card clock are still running much faster than
960 * the supported HS mode clock, so we can not reliably read out
961 * Extended CSD. Reconfigure the controller to run at HS mode.
962 */
963 if (hsdowngrade) {
964 mmc_select_mode(mmc, MMC_HS);
965 mmc_set_clock(mmc, mmc_mode2freq(mmc, MMC_HS), false);
966 }
967#endif
968
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200969 if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
970 /* Now check to see that it worked */
971 err = mmc_send_ext_csd(mmc, test_csd);
972 if (err)
973 return err;
974
975 /* No high-speed support */
976 if (!test_csd[EXT_CSD_HS_TIMING])
977 return -ENOTSUPP;
978 }
979
980 return 0;
981}
982
983static int mmc_get_capabilities(struct mmc *mmc)
984{
985 u8 *ext_csd = mmc->ext_csd;
986 char cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500987
Jean-Jacques Hiblot00e446f2017-11-30 17:43:56 +0100988 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -0500989
Thomas Choud52ebf12010-12-24 13:12:21 +0000990 if (mmc_host_is_spi(mmc))
991 return 0;
992
Andy Fleming272cc702008-10-30 16:41:01 -0500993 /* Only version 4 supports high-speed */
994 if (mmc->version < MMC_VERSION_4)
995 return 0;
996
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200997 if (!ext_csd) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100998 pr_err("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200999 return -ENOTSUPP;
1000 }
1001
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -06001002 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
1003
Peng Fan3dd26262018-08-10 14:07:54 +08001004 cardtype = ext_csd[EXT_CSD_CARD_TYPE];
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001005 mmc->cardtype = cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -05001006
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001007#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001008 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1009 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
1010 mmc->card_caps |= MMC_MODE_HS200;
1011 }
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001012#endif
Peng Fan44acd492019-07-10 14:43:07 +08001013#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
1014 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Peng Fan3dd26262018-08-10 14:07:54 +08001015 if (cardtype & (EXT_CSD_CARD_TYPE_HS400_1_2V |
1016 EXT_CSD_CARD_TYPE_HS400_1_8V)) {
1017 mmc->card_caps |= MMC_MODE_HS400;
1018 }
1019#endif
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001020 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001021 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001022 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001023 mmc->card_caps |= MMC_MODE_HS_52MHz;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001024 }
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001025 if (cardtype & EXT_CSD_CARD_TYPE_26)
1026 mmc->card_caps |= MMC_MODE_HS;
Andy Fleming272cc702008-10-30 16:41:01 -05001027
Peng Fan44acd492019-07-10 14:43:07 +08001028#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1029 if (ext_csd[EXT_CSD_STROBE_SUPPORT] &&
1030 (mmc->card_caps & MMC_MODE_HS400)) {
1031 mmc->card_caps |= MMC_MODE_HS400_ES;
1032 }
1033#endif
1034
Andy Fleming272cc702008-10-30 16:41:01 -05001035 return 0;
1036}
Marek Vasut62d77ce2018-04-15 00:37:11 +02001037#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001038
Stephen Warrenf866a462013-06-11 15:14:01 -06001039static int mmc_set_capacity(struct mmc *mmc, int part_num)
1040{
1041 switch (part_num) {
1042 case 0:
1043 mmc->capacity = mmc->capacity_user;
1044 break;
1045 case 1:
1046 case 2:
1047 mmc->capacity = mmc->capacity_boot;
1048 break;
1049 case 3:
1050 mmc->capacity = mmc->capacity_rpmb;
1051 break;
1052 case 4:
1053 case 5:
1054 case 6:
1055 case 7:
1056 mmc->capacity = mmc->capacity_gp[part_num - 4];
1057 break;
1058 default:
1059 return -1;
1060 }
1061
Simon Glassc40fdca2016-05-01 13:52:35 -06001062 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -06001063
1064 return 0;
1065}
1066
Simon Glass7dba0b92016-06-12 23:30:15 -06001067int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +00001068{
Stephen Warrenf866a462013-06-11 15:14:01 -06001069 int ret;
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +02001070 int retry = 3;
Lei Wenbc897b12011-05-02 16:26:26 +00001071
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +02001072 do {
1073 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1074 EXT_CSD_PART_CONF,
1075 (mmc->part_config & ~PART_ACCESS_MASK)
1076 | (part_num & PART_ACCESS_MASK));
1077 } while (ret && retry--);
Stephen Warrenf866a462013-06-11 15:14:01 -06001078
Peter Bigot6dc93e72014-09-02 18:31:23 -05001079 /*
1080 * Set the capacity if the switch succeeded or was intended
1081 * to return to representing the raw device.
1082 */
Stephen Warren873cc1d2015-12-07 11:38:49 -07001083 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -05001084 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -06001085 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -07001086 }
Peter Bigot6dc93e72014-09-02 18:31:23 -05001087
1088 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +00001089}
1090
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001091#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001092int mmc_hwpart_config(struct mmc *mmc,
1093 const struct mmc_hwpart_conf *conf,
1094 enum mmc_hwpart_conf_mode mode)
1095{
1096 u8 part_attrs = 0;
1097 u32 enh_size_mult;
1098 u32 enh_start_addr;
1099 u32 gp_size_mult[4];
1100 u32 max_enh_size_mult;
1101 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001102 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001103 int i, pidx, err;
1104 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1105
1106 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
1107 return -EINVAL;
1108
1109 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001110 pr_err("eMMC >= 4.4 required for enhanced user data area\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001111 return -EMEDIUMTYPE;
1112 }
1113
1114 if (!(mmc->part_support & PART_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001115 pr_err("Card does not support partitioning\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001116 return -EMEDIUMTYPE;
1117 }
1118
1119 if (!mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001120 pr_err("Card does not define HC WP group size\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001121 return -EMEDIUMTYPE;
1122 }
1123
1124 /* check partition alignment and total enhanced size */
1125 if (conf->user.enh_size) {
1126 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
1127 conf->user.enh_start % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001128 pr_err("User data enhanced area not HC WP group "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001129 "size aligned\n");
1130 return -EINVAL;
1131 }
1132 part_attrs |= EXT_CSD_ENH_USR;
1133 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
1134 if (mmc->high_capacity) {
1135 enh_start_addr = conf->user.enh_start;
1136 } else {
1137 enh_start_addr = (conf->user.enh_start << 9);
1138 }
1139 } else {
1140 enh_size_mult = 0;
1141 enh_start_addr = 0;
1142 }
1143 tot_enh_size_mult += enh_size_mult;
1144
1145 for (pidx = 0; pidx < 4; pidx++) {
1146 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001147 pr_err("GP%i partition not HC WP group size "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001148 "aligned\n", pidx+1);
1149 return -EINVAL;
1150 }
1151 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
1152 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
1153 part_attrs |= EXT_CSD_ENH_GP(pidx);
1154 tot_enh_size_mult += gp_size_mult[pidx];
1155 }
1156 }
1157
1158 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001159 pr_err("Card does not support enhanced attribute\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001160 return -EMEDIUMTYPE;
1161 }
1162
1163 err = mmc_send_ext_csd(mmc, ext_csd);
1164 if (err)
1165 return err;
1166
1167 max_enh_size_mult =
1168 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
1169 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
1170 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
1171 if (tot_enh_size_mult > max_enh_size_mult) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001172 pr_err("Total enhanced size exceeds maximum (%u > %u)\n",
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001173 tot_enh_size_mult, max_enh_size_mult);
1174 return -EMEDIUMTYPE;
1175 }
1176
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001177 /* The default value of EXT_CSD_WR_REL_SET is device
1178 * dependent, the values can only be changed if the
1179 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
1180 * changed only once and before partitioning is completed. */
1181 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1182 if (conf->user.wr_rel_change) {
1183 if (conf->user.wr_rel_set)
1184 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
1185 else
1186 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
1187 }
1188 for (pidx = 0; pidx < 4; pidx++) {
1189 if (conf->gp_part[pidx].wr_rel_change) {
1190 if (conf->gp_part[pidx].wr_rel_set)
1191 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
1192 else
1193 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
1194 }
1195 }
1196
1197 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
1198 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
1199 puts("Card does not support host controlled partition write "
1200 "reliability settings\n");
1201 return -EMEDIUMTYPE;
1202 }
1203
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001204 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
1205 EXT_CSD_PARTITION_SETTING_COMPLETED) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001206 pr_err("Card already partitioned\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001207 return -EPERM;
1208 }
1209
1210 if (mode == MMC_HWPART_CONF_CHECK)
1211 return 0;
1212
1213 /* Partitioning requires high-capacity size definitions */
1214 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
1215 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1216 EXT_CSD_ERASE_GROUP_DEF, 1);
1217
1218 if (err)
1219 return err;
1220
1221 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1222
Jaehoon Chung4af66592020-01-17 15:06:54 +09001223#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001224 /* update erase group size to be high-capacity */
1225 mmc->erase_grp_size =
1226 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jaehoon Chung4af66592020-01-17 15:06:54 +09001227#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001228
1229 }
1230
1231 /* all OK, write the configuration */
1232 for (i = 0; i < 4; i++) {
1233 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1234 EXT_CSD_ENH_START_ADDR+i,
1235 (enh_start_addr >> (i*8)) & 0xFF);
1236 if (err)
1237 return err;
1238 }
1239 for (i = 0; i < 3; i++) {
1240 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1241 EXT_CSD_ENH_SIZE_MULT+i,
1242 (enh_size_mult >> (i*8)) & 0xFF);
1243 if (err)
1244 return err;
1245 }
1246 for (pidx = 0; pidx < 4; pidx++) {
1247 for (i = 0; i < 3; i++) {
1248 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1249 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1250 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1251 if (err)
1252 return err;
1253 }
1254 }
1255 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1256 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1257 if (err)
1258 return err;
1259
1260 if (mode == MMC_HWPART_CONF_SET)
1261 return 0;
1262
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001263 /* The WR_REL_SET is a write-once register but shall be
1264 * written before setting PART_SETTING_COMPLETED. As it is
1265 * write-once we can only write it when completing the
1266 * partitioning. */
1267 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1268 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1269 EXT_CSD_WR_REL_SET, wr_rel_set);
1270 if (err)
1271 return err;
1272 }
1273
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001274 /* Setting PART_SETTING_COMPLETED confirms the partition
1275 * configuration but it only becomes effective after power
1276 * cycle, so we do not adjust the partition related settings
1277 * in the mmc struct. */
1278
1279 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1280 EXT_CSD_PARTITION_SETTING,
1281 EXT_CSD_PARTITION_SETTING_COMPLETED);
1282 if (err)
1283 return err;
1284
1285 return 0;
1286}
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001287#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001288
Simon Glasse7881d82017-07-29 11:35:31 -06001289#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +00001290int mmc_getcd(struct mmc *mmc)
1291{
1292 int cd;
1293
1294 cd = board_mmc_getcd(mmc);
1295
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001296 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001297 if (mmc->cfg->ops->getcd)
1298 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001299 else
1300 cd = 1;
1301 }
Thierry Reding48972d92012-01-02 01:15:37 +00001302
1303 return cd;
1304}
Simon Glass8ca51e52016-06-12 23:30:22 -06001305#endif
Thierry Reding48972d92012-01-02 01:15:37 +00001306
Marek Vasut62d77ce2018-04-15 00:37:11 +02001307#if !CONFIG_IS_ENABLED(MMC_TINY)
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001308static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -05001309{
1310 struct mmc_cmd cmd;
1311 struct mmc_data data;
1312
1313 /* Switch the frequency */
1314 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1315 cmd.resp_type = MMC_RSP_R1;
1316 cmd.cmdarg = (mode << 31) | 0xffffff;
1317 cmd.cmdarg &= ~(0xf << (group * 4));
1318 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -05001319
1320 data.dest = (char *)resp;
1321 data.blocksize = 64;
1322 data.blocks = 1;
1323 data.flags = MMC_DATA_READ;
1324
1325 return mmc_send_cmd(mmc, &cmd, &data);
1326}
1327
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001328static int sd_get_capabilities(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001329{
1330 int err;
1331 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +05301332 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1333 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -05001334 struct mmc_data data;
1335 int timeout;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001336#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001337 u32 sd3_bus_mode;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001338#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001339
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301340 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05001341
Thomas Choud52ebf12010-12-24 13:12:21 +00001342 if (mmc_host_is_spi(mmc))
1343 return 0;
1344
Andy Fleming272cc702008-10-30 16:41:01 -05001345 /* Read the SCR to find out if this card supports higher speeds */
1346 cmd.cmdidx = MMC_CMD_APP_CMD;
1347 cmd.resp_type = MMC_RSP_R1;
1348 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001349
1350 err = mmc_send_cmd(mmc, &cmd, NULL);
1351
1352 if (err)
1353 return err;
1354
1355 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1356 cmd.resp_type = MMC_RSP_R1;
1357 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001358
Anton staaff781dd32011-10-03 13:54:59 +00001359 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -05001360 data.blocksize = 8;
1361 data.blocks = 1;
1362 data.flags = MMC_DATA_READ;
1363
Sean Andersonda129172020-10-17 08:36:27 -04001364 err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);
Andy Fleming272cc702008-10-30 16:41:01 -05001365
Sean Andersonda129172020-10-17 08:36:27 -04001366 if (err)
Andy Fleming272cc702008-10-30 16:41:01 -05001367 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001368
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001369 mmc->scr[0] = __be32_to_cpu(scr[0]);
1370 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -05001371
1372 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -07001373 case 0:
1374 mmc->version = SD_VERSION_1_0;
1375 break;
1376 case 1:
1377 mmc->version = SD_VERSION_1_10;
1378 break;
1379 case 2:
1380 mmc->version = SD_VERSION_2;
1381 if ((mmc->scr[0] >> 15) & 0x1)
1382 mmc->version = SD_VERSION_3;
1383 break;
1384 default:
1385 mmc->version = SD_VERSION_1_0;
1386 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001387 }
1388
Alagu Sankarb44c7082010-05-12 15:08:24 +05301389 if (mmc->scr[0] & SD_DATA_4BIT)
1390 mmc->card_caps |= MMC_MODE_4BIT;
1391
Andy Fleming272cc702008-10-30 16:41:01 -05001392 /* Version 1.0 doesn't support switching */
1393 if (mmc->version == SD_VERSION_1_0)
1394 return 0;
1395
1396 timeout = 4;
1397 while (timeout--) {
1398 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +00001399 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001400
1401 if (err)
1402 return err;
1403
1404 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001405 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -05001406 break;
1407 }
1408
Andy Fleming272cc702008-10-30 16:41:01 -05001409 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001410 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1411 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Fleming272cc702008-10-30 16:41:01 -05001412
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001413#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001414 /* Version before 3.0 don't support UHS modes */
1415 if (mmc->version < SD_VERSION_3)
1416 return 0;
1417
1418 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1419 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1420 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1421 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1422 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1423 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1424 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1425 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1426 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1427 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1428 mmc->card_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001429#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001430
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001431 return 0;
1432}
1433
1434static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1435{
1436 int err;
1437
1438 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001439 int speed;
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001440
Marek Vasutcf345762018-11-18 03:25:08 +01001441 /* SD version 1.00 and 1.01 does not support CMD 6 */
1442 if (mmc->version == SD_VERSION_1_0)
1443 return 0;
1444
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001445 switch (mode) {
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301446 case MMC_LEGACY:
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001447 speed = UHS_SDR12_BUS_SPEED;
1448 break;
1449 case SD_HS:
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001450 speed = HIGH_SPEED_BUS_SPEED;
1451 break;
1452#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1453 case UHS_SDR12:
1454 speed = UHS_SDR12_BUS_SPEED;
1455 break;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001456 case UHS_SDR25:
1457 speed = UHS_SDR25_BUS_SPEED;
1458 break;
1459 case UHS_SDR50:
1460 speed = UHS_SDR50_BUS_SPEED;
1461 break;
1462 case UHS_DDR50:
1463 speed = UHS_DDR50_BUS_SPEED;
1464 break;
1465 case UHS_SDR104:
1466 speed = UHS_SDR104_BUS_SPEED;
1467 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001468#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001469 default:
1470 return -EINVAL;
1471 }
1472
1473 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001474 if (err)
1475 return err;
1476
Jean-Jacques Hiblota0276f32018-02-09 12:09:27 +01001477 if (((__be32_to_cpu(switch_status[4]) >> 24) & 0xF) != speed)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001478 return -ENOTSUPP;
1479
1480 return 0;
1481}
1482
Marek Vasutec360e62018-04-15 00:36:45 +02001483static int sd_select_bus_width(struct mmc *mmc, int w)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001484{
1485 int err;
1486 struct mmc_cmd cmd;
1487
1488 if ((w != 4) && (w != 1))
1489 return -EINVAL;
1490
1491 cmd.cmdidx = MMC_CMD_APP_CMD;
1492 cmd.resp_type = MMC_RSP_R1;
1493 cmd.cmdarg = mmc->rca << 16;
1494
1495 err = mmc_send_cmd(mmc, &cmd, NULL);
1496 if (err)
1497 return err;
1498
1499 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1500 cmd.resp_type = MMC_RSP_R1;
1501 if (w == 4)
1502 cmd.cmdarg = 2;
1503 else if (w == 1)
1504 cmd.cmdarg = 0;
1505 err = mmc_send_cmd(mmc, &cmd, NULL);
1506 if (err)
1507 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001508
1509 return 0;
1510}
Marek Vasut62d77ce2018-04-15 00:37:11 +02001511#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001512
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001513#if CONFIG_IS_ENABLED(MMC_WRITE)
Peng Fan3697e592016-09-01 11:13:38 +08001514static int sd_read_ssr(struct mmc *mmc)
1515{
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001516 static const unsigned int sd_au_size[] = {
1517 0, SZ_16K / 512, SZ_32K / 512,
1518 SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
1519 SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
1520 SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
1521 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512,
1522 SZ_64M / 512,
1523 };
Peng Fan3697e592016-09-01 11:13:38 +08001524 int err, i;
1525 struct mmc_cmd cmd;
1526 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1527 struct mmc_data data;
Peng Fan3697e592016-09-01 11:13:38 +08001528 unsigned int au, eo, et, es;
1529
1530 cmd.cmdidx = MMC_CMD_APP_CMD;
1531 cmd.resp_type = MMC_RSP_R1;
1532 cmd.cmdarg = mmc->rca << 16;
1533
Sean Andersonda129172020-10-17 08:36:27 -04001534 err = mmc_send_cmd_quirks(mmc, &cmd, NULL, MMC_QUIRK_RETRY_APP_CMD, 4);
Peng Fan3697e592016-09-01 11:13:38 +08001535 if (err)
1536 return err;
1537
1538 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1539 cmd.resp_type = MMC_RSP_R1;
1540 cmd.cmdarg = 0;
1541
Peng Fan3697e592016-09-01 11:13:38 +08001542 data.dest = (char *)ssr;
1543 data.blocksize = 64;
1544 data.blocks = 1;
1545 data.flags = MMC_DATA_READ;
1546
Sean Andersonda129172020-10-17 08:36:27 -04001547 err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);
1548 if (err)
Peng Fan3697e592016-09-01 11:13:38 +08001549 return err;
Peng Fan3697e592016-09-01 11:13:38 +08001550
1551 for (i = 0; i < 16; i++)
1552 ssr[i] = be32_to_cpu(ssr[i]);
1553
1554 au = (ssr[2] >> 12) & 0xF;
1555 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1556 mmc->ssr.au = sd_au_size[au];
1557 es = (ssr[3] >> 24) & 0xFF;
1558 es |= (ssr[2] & 0xFF) << 8;
1559 et = (ssr[3] >> 18) & 0x3F;
1560 if (es && et) {
1561 eo = (ssr[3] >> 16) & 0x3;
1562 mmc->ssr.erase_timeout = (et * 1000) / es;
1563 mmc->ssr.erase_offset = eo * 1000;
1564 }
1565 } else {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001566 pr_debug("Invalid Allocation Unit Size.\n");
Peng Fan3697e592016-09-01 11:13:38 +08001567 }
1568
1569 return 0;
1570}
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001571#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001572/* frequency bases */
1573/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001574static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001575 10000,
1576 100000,
1577 1000000,
1578 10000000,
1579};
1580
1581/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1582 * to platforms without floating point.
1583 */
Simon Glass61fe0762016-05-14 14:02:57 -06001584static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001585 0, /* reserved */
1586 10,
1587 12,
1588 13,
1589 15,
1590 20,
1591 25,
1592 30,
1593 35,
1594 40,
1595 45,
1596 50,
1597 55,
1598 60,
1599 70,
1600 80,
1601};
1602
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001603static inline int bus_width(uint cap)
1604{
1605 if (cap == MMC_MODE_8BIT)
1606 return 8;
1607 if (cap == MMC_MODE_4BIT)
1608 return 4;
1609 if (cap == MMC_MODE_1BIT)
1610 return 1;
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001611 pr_warn("invalid bus witdh capability 0x%x\n", cap);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001612 return 0;
1613}
1614
Simon Glasse7881d82017-07-29 11:35:31 -06001615#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001616#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001617static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1618{
1619 return -ENOTSUPP;
1620}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001621#endif
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001622
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001623static int mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001624{
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001625 int ret = 0;
1626
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001627 if (mmc->cfg->ops->set_ios)
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001628 ret = mmc->cfg->ops->set_ios(mmc);
1629
1630 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001631}
Yann Gautier3602a562019-09-19 17:56:12 +02001632
1633static int mmc_host_power_cycle(struct mmc *mmc)
1634{
1635 int ret = 0;
1636
1637 if (mmc->cfg->ops->host_power_cycle)
1638 ret = mmc->cfg->ops->host_power_cycle(mmc);
1639
1640 return ret;
1641}
Simon Glass8ca51e52016-06-12 23:30:22 -06001642#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001643
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001644int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
Andy Fleming272cc702008-10-30 16:41:01 -05001645{
Jaehoon Chungc0fafe62018-01-23 14:04:30 +09001646 if (!disable) {
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001647 if (clock > mmc->cfg->f_max)
1648 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001649
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001650 if (clock < mmc->cfg->f_min)
1651 clock = mmc->cfg->f_min;
1652 }
Andy Fleming272cc702008-10-30 16:41:01 -05001653
1654 mmc->clock = clock;
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001655 mmc->clk_disable = disable;
Andy Fleming272cc702008-10-30 16:41:01 -05001656
Jaehoon Chungd2faadb2018-01-26 19:25:30 +09001657 debug("clock is %s (%dHz)\n", disable ? "disabled" : "enabled", clock);
1658
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001659 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001660}
1661
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001662static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001663{
1664 mmc->bus_width = width;
1665
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001666 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001667}
1668
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001669#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1670/*
1671 * helper function to display the capabilities in a human
1672 * friendly manner. The capabilities include bus width and
1673 * supported modes.
1674 */
1675void mmc_dump_capabilities(const char *text, uint caps)
1676{
1677 enum bus_mode mode;
1678
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001679 pr_debug("%s: widths [", text);
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001680 if (caps & MMC_MODE_8BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001681 pr_debug("8, ");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001682 if (caps & MMC_MODE_4BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001683 pr_debug("4, ");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001684 if (caps & MMC_MODE_1BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001685 pr_debug("1, ");
1686 pr_debug("\b\b] modes [");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001687 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1688 if (MMC_CAP(mode) & caps)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001689 pr_debug("%s, ", mmc_mode_name(mode));
1690 pr_debug("\b\b]\n");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001691}
1692#endif
1693
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001694struct mode_width_tuning {
1695 enum bus_mode mode;
1696 uint widths;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001697#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001698 uint tuning;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001699#endif
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001700};
1701
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001702#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001703int mmc_voltage_to_mv(enum mmc_voltage voltage)
1704{
1705 switch (voltage) {
1706 case MMC_SIGNAL_VOLTAGE_000: return 0;
1707 case MMC_SIGNAL_VOLTAGE_330: return 3300;
1708 case MMC_SIGNAL_VOLTAGE_180: return 1800;
1709 case MMC_SIGNAL_VOLTAGE_120: return 1200;
1710 }
1711 return -EINVAL;
1712}
1713
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001714static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1715{
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001716 int err;
1717
1718 if (mmc->signal_voltage == signal_voltage)
1719 return 0;
1720
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001721 mmc->signal_voltage = signal_voltage;
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001722 err = mmc_set_ios(mmc);
1723 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001724 pr_debug("unable to set voltage (err %d)\n", err);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001725
1726 return err;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001727}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001728#else
1729static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1730{
1731 return 0;
1732}
1733#endif
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001734
Marek Vasut62d77ce2018-04-15 00:37:11 +02001735#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001736static const struct mode_width_tuning sd_modes_by_pref[] = {
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001737#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1738#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001739 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001740 .mode = UHS_SDR104,
1741 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1742 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1743 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001744#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001745 {
1746 .mode = UHS_SDR50,
1747 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1748 },
1749 {
1750 .mode = UHS_DDR50,
1751 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1752 },
1753 {
1754 .mode = UHS_SDR25,
1755 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1756 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001757#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001758 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001759 .mode = SD_HS,
1760 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1761 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001762#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001763 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001764 .mode = UHS_SDR12,
1765 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1766 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001767#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001768 {
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301769 .mode = MMC_LEGACY,
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001770 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1771 }
1772};
1773
1774#define for_each_sd_mode_by_pref(caps, mwt) \
1775 for (mwt = sd_modes_by_pref;\
1776 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1777 mwt++) \
1778 if (caps & MMC_CAP(mwt->mode))
1779
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001780static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001781{
1782 int err;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001783 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1784 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001785#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001786 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001787#else
1788 bool uhs_en = false;
1789#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001790 uint caps;
1791
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001792#ifdef DEBUG
1793 mmc_dump_capabilities("sd card", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001794 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001795#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001796
Anup Patelf49ff792019-07-08 04:10:43 +00001797 if (mmc_host_is_spi(mmc)) {
1798 mmc_set_bus_width(mmc, 1);
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301799 mmc_select_mode(mmc, MMC_LEGACY);
Anup Patelf49ff792019-07-08 04:10:43 +00001800 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
Pragnesh Patel810bc132020-06-29 15:17:26 +05301801#if CONFIG_IS_ENABLED(MMC_WRITE)
1802 err = sd_read_ssr(mmc);
1803 if (err)
1804 pr_warn("unable to read ssr\n");
1805#endif
Anup Patelf49ff792019-07-08 04:10:43 +00001806 return 0;
1807 }
1808
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001809 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001810 caps = card_caps & mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001811
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001812 if (!uhs_en)
1813 caps &= ~UHS_CAPS;
1814
1815 for_each_sd_mode_by_pref(caps, mwt) {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001816 uint *w;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001817
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001818 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001819 if (*w & caps & mwt->widths) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001820 pr_debug("trying mode %s width %d (at %d MHz)\n",
1821 mmc_mode_name(mwt->mode),
1822 bus_width(*w),
1823 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001824
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001825 /* configure the bus width (card + host) */
1826 err = sd_select_bus_width(mmc, bus_width(*w));
1827 if (err)
1828 goto error;
1829 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001830
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001831 /* configure the bus mode (card) */
1832 err = sd_set_card_speed(mmc, mwt->mode);
1833 if (err)
1834 goto error;
1835
1836 /* configure the bus mode (host) */
1837 mmc_select_mode(mmc, mwt->mode);
Jaehoon Chung65117182018-01-26 19:25:29 +09001838 mmc_set_clock(mmc, mmc->tran_speed,
1839 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001840
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001841#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001842 /* execute tuning if needed */
1843 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1844 err = mmc_execute_tuning(mmc,
1845 mwt->tuning);
1846 if (err) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001847 pr_debug("tuning failed\n");
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001848 goto error;
1849 }
1850 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001851#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001852
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001853#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001854 err = sd_read_ssr(mmc);
Peng Fan0a4c2b02018-03-05 16:20:40 +08001855 if (err)
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001856 pr_warn("unable to read ssr\n");
1857#endif
1858 if (!err)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001859 return 0;
1860
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001861error:
1862 /* revert to a safer bus speed */
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301863 mmc_select_mode(mmc, MMC_LEGACY);
Jaehoon Chung65117182018-01-26 19:25:29 +09001864 mmc_set_clock(mmc, mmc->tran_speed,
1865 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001866 }
1867 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001868 }
1869
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001870 pr_err("unable to select a mode\n");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001871 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001872}
1873
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001874/*
1875 * read the compare the part of ext csd that is constant.
1876 * This can be used to check that the transfer is working
1877 * as expected.
1878 */
1879static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1880{
1881 int err;
1882 const u8 *ext_csd = mmc->ext_csd;
1883 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1884
Jean-Jacques Hiblot1de06b92017-11-30 17:43:58 +01001885 if (mmc->version < MMC_VERSION_4)
1886 return 0;
1887
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001888 err = mmc_send_ext_csd(mmc, test_csd);
1889 if (err)
1890 return err;
1891
1892 /* Only compare read only fields */
1893 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1894 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1895 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1896 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1897 ext_csd[EXT_CSD_REV]
1898 == test_csd[EXT_CSD_REV] &&
1899 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1900 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1901 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1902 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1903 return 0;
1904
1905 return -EBADMSG;
1906}
1907
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001908#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001909static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1910 uint32_t allowed_mask)
1911{
1912 u32 card_mask = 0;
1913
1914 switch (mode) {
Peng Fan44acd492019-07-10 14:43:07 +08001915 case MMC_HS_400_ES:
Peng Fan3dd26262018-08-10 14:07:54 +08001916 case MMC_HS_400:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001917 case MMC_HS_200:
Peng Fan3dd26262018-08-10 14:07:54 +08001918 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_8V |
1919 EXT_CSD_CARD_TYPE_HS400_1_8V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001920 card_mask |= MMC_SIGNAL_VOLTAGE_180;
Peng Fan3dd26262018-08-10 14:07:54 +08001921 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1922 EXT_CSD_CARD_TYPE_HS400_1_2V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001923 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1924 break;
1925 case MMC_DDR_52:
1926 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
1927 card_mask |= MMC_SIGNAL_VOLTAGE_330 |
1928 MMC_SIGNAL_VOLTAGE_180;
1929 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
1930 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1931 break;
1932 default:
1933 card_mask |= MMC_SIGNAL_VOLTAGE_330;
1934 break;
1935 }
1936
1937 while (card_mask & allowed_mask) {
1938 enum mmc_voltage best_match;
1939
1940 best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
1941 if (!mmc_set_signal_voltage(mmc, best_match))
1942 return 0;
1943
1944 allowed_mask &= ~best_match;
1945 }
1946
1947 return -ENOTSUPP;
1948}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001949#else
1950static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1951 uint32_t allowed_mask)
1952{
1953 return 0;
1954}
1955#endif
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001956
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001957static const struct mode_width_tuning mmc_modes_by_pref[] = {
Peng Fan44acd492019-07-10 14:43:07 +08001958#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1959 {
1960 .mode = MMC_HS_400_ES,
1961 .widths = MMC_MODE_8BIT,
1962 },
1963#endif
Peng Fan3dd26262018-08-10 14:07:54 +08001964#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1965 {
1966 .mode = MMC_HS_400,
1967 .widths = MMC_MODE_8BIT,
1968 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
1969 },
1970#endif
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001971#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001972 {
1973 .mode = MMC_HS_200,
1974 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001975 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001976 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001977#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001978 {
1979 .mode = MMC_DDR_52,
1980 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1981 },
1982 {
1983 .mode = MMC_HS_52,
1984 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1985 },
1986 {
1987 .mode = MMC_HS,
1988 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1989 },
1990 {
1991 .mode = MMC_LEGACY,
1992 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1993 }
1994};
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001995
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001996#define for_each_mmc_mode_by_pref(caps, mwt) \
1997 for (mwt = mmc_modes_by_pref;\
1998 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
1999 mwt++) \
2000 if (caps & MMC_CAP(mwt->mode))
2001
2002static const struct ext_csd_bus_width {
2003 uint cap;
2004 bool is_ddr;
2005 uint ext_csd_bits;
2006} ext_csd_bus_width[] = {
2007 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
2008 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
2009 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
2010 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
2011 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
2012};
2013
Peng Fan3dd26262018-08-10 14:07:54 +08002014#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2015static int mmc_select_hs400(struct mmc *mmc)
2016{
2017 int err;
2018
2019 /* Set timing to HS200 for tuning */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002020 err = mmc_set_card_speed(mmc, MMC_HS_200, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002021 if (err)
2022 return err;
2023
2024 /* configure the bus mode (host) */
2025 mmc_select_mode(mmc, MMC_HS_200);
2026 mmc_set_clock(mmc, mmc->tran_speed, false);
2027
2028 /* execute tuning if needed */
Yangbo Lu8c968802020-09-01 16:58:03 +08002029 mmc->hs400_tuning = 1;
Peng Fan3dd26262018-08-10 14:07:54 +08002030 err = mmc_execute_tuning(mmc, MMC_CMD_SEND_TUNING_BLOCK_HS200);
Yangbo Lu8c968802020-09-01 16:58:03 +08002031 mmc->hs400_tuning = 0;
Peng Fan3dd26262018-08-10 14:07:54 +08002032 if (err) {
2033 debug("tuning failed\n");
2034 return err;
2035 }
2036
2037 /* Set back to HS */
BOUGH CHEN5cf12032019-03-26 06:24:17 +00002038 mmc_set_card_speed(mmc, MMC_HS, true);
Peng Fan3dd26262018-08-10 14:07:54 +08002039
Yangbo Lud271e102020-09-01 16:58:04 +08002040 err = mmc_hs400_prepare_ddr(mmc);
2041 if (err)
2042 return err;
2043
Peng Fan3dd26262018-08-10 14:07:54 +08002044 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2045 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG);
2046 if (err)
2047 return err;
2048
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002049 err = mmc_set_card_speed(mmc, MMC_HS_400, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002050 if (err)
2051 return err;
2052
2053 mmc_select_mode(mmc, MMC_HS_400);
2054 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2055 if (err)
2056 return err;
2057
2058 return 0;
2059}
2060#else
2061static int mmc_select_hs400(struct mmc *mmc)
2062{
2063 return -ENOTSUPP;
2064}
2065#endif
2066
Peng Fan44acd492019-07-10 14:43:07 +08002067#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
2068#if !CONFIG_IS_ENABLED(DM_MMC)
2069static int mmc_set_enhanced_strobe(struct mmc *mmc)
2070{
2071 return -ENOTSUPP;
2072}
2073#endif
2074static int mmc_select_hs400es(struct mmc *mmc)
2075{
2076 int err;
2077
2078 err = mmc_set_card_speed(mmc, MMC_HS, true);
2079 if (err)
2080 return err;
2081
2082 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2083 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG |
2084 EXT_CSD_BUS_WIDTH_STROBE);
2085 if (err) {
2086 printf("switch to bus width for hs400 failed\n");
2087 return err;
2088 }
2089 /* TODO: driver strength */
2090 err = mmc_set_card_speed(mmc, MMC_HS_400_ES, false);
2091 if (err)
2092 return err;
2093
2094 mmc_select_mode(mmc, MMC_HS_400_ES);
2095 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2096 if (err)
2097 return err;
2098
2099 return mmc_set_enhanced_strobe(mmc);
2100}
2101#else
2102static int mmc_select_hs400es(struct mmc *mmc)
2103{
2104 return -ENOTSUPP;
2105}
2106#endif
2107
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002108#define for_each_supported_width(caps, ddr, ecbv) \
2109 for (ecbv = ext_csd_bus_width;\
2110 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
2111 ecbv++) \
2112 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
2113
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002114static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002115{
Jaehoon Chung52ff04a2020-12-04 06:36:00 +09002116 int err = 0;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002117 const struct mode_width_tuning *mwt;
2118 const struct ext_csd_bus_width *ecbw;
2119
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002120#ifdef DEBUG
2121 mmc_dump_capabilities("mmc", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002122 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002123#endif
2124
Anup Patelf49ff792019-07-08 04:10:43 +00002125 if (mmc_host_is_spi(mmc)) {
2126 mmc_set_bus_width(mmc, 1);
2127 mmc_select_mode(mmc, MMC_LEGACY);
2128 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
2129 return 0;
2130 }
2131
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002132 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002133 card_caps &= mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002134
2135 /* Only version 4 of MMC supports wider bus widths */
2136 if (mmc->version < MMC_VERSION_4)
2137 return 0;
2138
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002139 if (!mmc->ext_csd) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002140 pr_debug("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002141 return -ENOTSUPP;
2142 }
2143
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002144#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
Ye Lifb8c2e82021-08-17 17:20:34 +08002145 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
2146 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002147 /*
2148 * In case the eMMC is in HS200/HS400 mode, downgrade to HS mode
2149 * before doing anything else, since a transition from either of
2150 * the HS200/HS400 mode directly to legacy mode is not supported.
2151 */
2152 if (mmc->selected_mode == MMC_HS_200 ||
Ye Lifb8c2e82021-08-17 17:20:34 +08002153 mmc->selected_mode == MMC_HS_400 ||
2154 mmc->selected_mode == MMC_HS_400_ES)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002155 mmc_set_card_speed(mmc, MMC_HS, true);
2156 else
2157#endif
2158 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002159
2160 for_each_mmc_mode_by_pref(card_caps, mwt) {
2161 for_each_supported_width(card_caps & mwt->widths,
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002162 mmc_is_mode_ddr(mwt->mode), ecbw) {
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002163 enum mmc_voltage old_voltage;
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002164 pr_debug("trying mode %s width %d (at %d MHz)\n",
2165 mmc_mode_name(mwt->mode),
2166 bus_width(ecbw->cap),
2167 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002168 old_voltage = mmc->signal_voltage;
2169 err = mmc_set_lowest_voltage(mmc, mwt->mode,
2170 MMC_ALL_SIGNAL_VOLTAGE);
2171 if (err)
2172 continue;
2173
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002174 /* configure the bus width (card + host) */
2175 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2176 EXT_CSD_BUS_WIDTH,
2177 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
2178 if (err)
2179 goto error;
2180 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
2181
Peng Fan3dd26262018-08-10 14:07:54 +08002182 if (mwt->mode == MMC_HS_400) {
2183 err = mmc_select_hs400(mmc);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002184 if (err) {
Peng Fan3dd26262018-08-10 14:07:54 +08002185 printf("Select HS400 failed %d\n", err);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002186 goto error;
2187 }
Peng Fan44acd492019-07-10 14:43:07 +08002188 } else if (mwt->mode == MMC_HS_400_ES) {
2189 err = mmc_select_hs400es(mmc);
2190 if (err) {
2191 printf("Select HS400ES failed %d\n",
2192 err);
2193 goto error;
2194 }
Peng Fan3dd26262018-08-10 14:07:54 +08002195 } else {
2196 /* configure the bus speed (card) */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002197 err = mmc_set_card_speed(mmc, mwt->mode, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002198 if (err)
2199 goto error;
2200
2201 /*
2202 * configure the bus width AND the ddr mode
2203 * (card). The host side will be taken care
2204 * of in the next step
2205 */
2206 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
2207 err = mmc_switch(mmc,
2208 EXT_CSD_CMD_SET_NORMAL,
2209 EXT_CSD_BUS_WIDTH,
2210 ecbw->ext_csd_bits);
2211 if (err)
2212 goto error;
2213 }
2214
2215 /* configure the bus mode (host) */
2216 mmc_select_mode(mmc, mwt->mode);
2217 mmc_set_clock(mmc, mmc->tran_speed,
2218 MMC_CLK_ENABLE);
2219#ifdef MMC_SUPPORTS_TUNING
2220
2221 /* execute tuning if needed */
2222 if (mwt->tuning) {
2223 err = mmc_execute_tuning(mmc,
2224 mwt->tuning);
2225 if (err) {
Jaehoon Chung58896452020-11-17 07:04:59 +09002226 pr_debug("tuning failed : %d\n", err);
Peng Fan3dd26262018-08-10 14:07:54 +08002227 goto error;
2228 }
2229 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01002230#endif
Peng Fan3dd26262018-08-10 14:07:54 +08002231 }
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002232
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002233 /* do a transfer to check the configuration */
2234 err = mmc_read_and_compare_ext_csd(mmc);
2235 if (!err)
2236 return 0;
2237error:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002238 mmc_set_signal_voltage(mmc, old_voltage);
Naoki Hayama64dbd862020-10-12 18:35:22 +09002239 /* if an error occurred, revert to a safer bus mode */
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002240 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2241 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
2242 mmc_select_mode(mmc, MMC_LEGACY);
Valentine Barshak50dee4f2023-06-10 13:22:33 +02002243 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002244 mmc_set_bus_width(mmc, 1);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002245 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002246 }
2247
Jaehoon Chung58896452020-11-17 07:04:59 +09002248 pr_err("unable to select a mode : %d\n", err);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002249
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002250 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002251}
Marek Vasut62d77ce2018-04-15 00:37:11 +02002252#endif
2253
2254#if CONFIG_IS_ENABLED(MMC_TINY)
2255DEFINE_CACHE_ALIGN_BUFFER(u8, ext_csd_bkup, MMC_MAX_BLOCK_LEN);
2256#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002257
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002258static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002259{
2260 int err, i;
2261 u64 capacity;
2262 bool has_parts = false;
2263 bool part_completed;
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002264 static const u32 mmc_versions[] = {
2265 MMC_VERSION_4,
2266 MMC_VERSION_4_1,
2267 MMC_VERSION_4_2,
2268 MMC_VERSION_4_3,
Jean-Jacques Hiblotace1bed2018-02-09 12:09:28 +01002269 MMC_VERSION_4_4,
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002270 MMC_VERSION_4_41,
2271 MMC_VERSION_4_5,
2272 MMC_VERSION_5_0,
2273 MMC_VERSION_5_1
2274 };
2275
Marek Vasut62d77ce2018-04-15 00:37:11 +02002276#if CONFIG_IS_ENABLED(MMC_TINY)
2277 u8 *ext_csd = ext_csd_bkup;
2278
2279 if (IS_SD(mmc) || mmc->version < MMC_VERSION_4)
2280 return 0;
2281
2282 if (!mmc->ext_csd)
Sam Edwards229d6892023-05-18 13:47:07 -06002283 memset(ext_csd_bkup, 0, MMC_MAX_BLOCK_LEN);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002284
2285 err = mmc_send_ext_csd(mmc, ext_csd);
2286 if (err)
2287 goto error;
2288
2289 /* store the ext csd for future reference */
2290 if (!mmc->ext_csd)
2291 mmc->ext_csd = ext_csd;
2292#else
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002293 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002294
2295 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
2296 return 0;
2297
2298 /* check ext_csd version and capacity */
2299 err = mmc_send_ext_csd(mmc, ext_csd);
2300 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002301 goto error;
2302
2303 /* store the ext csd for future reference */
2304 if (!mmc->ext_csd)
2305 mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN);
2306 if (!mmc->ext_csd)
2307 return -ENOMEM;
2308 memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002309#endif
Alexander Kochetkov76584e32018-02-20 14:35:55 +03002310 if (ext_csd[EXT_CSD_REV] >= ARRAY_SIZE(mmc_versions))
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002311 return -EINVAL;
2312
2313 mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]];
2314
2315 if (mmc->version >= MMC_VERSION_4_2) {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002316 /*
2317 * According to the JEDEC Standard, the value of
2318 * ext_csd's capacity is valid if the value is more
2319 * than 2GB
2320 */
2321 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
2322 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
2323 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
2324 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2325 capacity *= MMC_MAX_BLOCK_LEN;
2326 if ((capacity >> 20) > 2 * 1024)
2327 mmc->capacity_user = capacity;
2328 }
2329
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +02002330 if (mmc->version >= MMC_VERSION_4_5)
2331 mmc->gen_cmd6_time = ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
2332
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002333 /* The partition data may be non-zero but it is only
2334 * effective if PARTITION_SETTING_COMPLETED is set in
2335 * EXT_CSD, so ignore any data if this bit is not set,
2336 * except for enabling the high-capacity group size
2337 * definition (see below).
2338 */
2339 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
2340 EXT_CSD_PARTITION_SETTING_COMPLETED);
2341
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +02002342 mmc->part_switch_time = ext_csd[EXT_CSD_PART_SWITCH_TIME];
2343 /* Some eMMC set the value too low so set a minimum */
2344 if (mmc->part_switch_time < MMC_MIN_PART_SWITCH_TIME && mmc->part_switch_time)
2345 mmc->part_switch_time = MMC_MIN_PART_SWITCH_TIME;
2346
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002347 /* store the partition info of emmc */
2348 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
2349 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
2350 ext_csd[EXT_CSD_BOOT_MULT])
2351 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
2352 if (part_completed &&
2353 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
2354 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
2355
2356 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
2357
2358 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
2359
2360 for (i = 0; i < 4; i++) {
2361 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
2362 uint mult = (ext_csd[idx + 2] << 16) +
2363 (ext_csd[idx + 1] << 8) + ext_csd[idx];
2364 if (mult)
2365 has_parts = true;
2366 if (!part_completed)
2367 continue;
2368 mmc->capacity_gp[i] = mult;
2369 mmc->capacity_gp[i] *=
2370 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2371 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2372 mmc->capacity_gp[i] <<= 19;
2373 }
2374
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002375#ifndef CONFIG_SPL_BUILD
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002376 if (part_completed) {
2377 mmc->enh_user_size =
2378 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
2379 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
2380 ext_csd[EXT_CSD_ENH_SIZE_MULT];
2381 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2382 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2383 mmc->enh_user_size <<= 19;
2384 mmc->enh_user_start =
2385 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
2386 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
2387 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
2388 ext_csd[EXT_CSD_ENH_START_ADDR];
2389 if (mmc->high_capacity)
2390 mmc->enh_user_start <<= 9;
2391 }
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002392#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002393
2394 /*
2395 * Host needs to enable ERASE_GRP_DEF bit if device is
2396 * partitioned. This bit will be lost every time after a reset
2397 * or power off. This will affect erase size.
2398 */
2399 if (part_completed)
2400 has_parts = true;
2401 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
2402 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
2403 has_parts = true;
2404 if (has_parts) {
2405 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2406 EXT_CSD_ERASE_GROUP_DEF, 1);
2407
2408 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002409 goto error;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002410
2411 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
2412 }
2413
2414 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002415#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002416 /* Read out group size from ext_csd */
2417 mmc->erase_grp_size =
2418 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002419#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002420 /*
2421 * if high capacity and partition setting completed
2422 * SEC_COUNT is valid even if it is smaller than 2 GiB
2423 * JEDEC Standard JESD84-B45, 6.2.4
2424 */
2425 if (mmc->high_capacity && part_completed) {
2426 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
2427 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
2428 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
2429 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
2430 capacity *= MMC_MAX_BLOCK_LEN;
2431 mmc->capacity_user = capacity;
2432 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002433 }
2434#if CONFIG_IS_ENABLED(MMC_WRITE)
2435 else {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002436 /* Calculate the group size from the csd value. */
2437 int erase_gsz, erase_gmul;
2438
2439 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
2440 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
2441 mmc->erase_grp_size = (erase_gsz + 1)
2442 * (erase_gmul + 1);
2443 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002444#endif
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002445#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002446 mmc->hc_wp_grp_size = 1024
2447 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
2448 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002449#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002450
2451 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
2452
Loic Poulaineeb739a2023-01-26 10:24:17 +01002453 mmc->can_trim =
2454 !!(ext_csd[EXT_CSD_SEC_FEATURE] & EXT_CSD_SEC_FEATURE_TRIM_EN);
2455
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002456 return 0;
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002457error:
2458 if (mmc->ext_csd) {
Marek Vasut62d77ce2018-04-15 00:37:11 +02002459#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002460 free(mmc->ext_csd);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002461#endif
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002462 mmc->ext_csd = NULL;
2463 }
2464 return err;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002465}
2466
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002467static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002468{
Stephen Warrenf866a462013-06-11 15:14:01 -06002469 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05002470 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002471 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05002472 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06002473 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05002474
Thomas Choud52ebf12010-12-24 13:12:21 +00002475#ifdef CONFIG_MMC_SPI_CRC_ON
2476 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
2477 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
2478 cmd.resp_type = MMC_RSP_R1;
2479 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002480 err = mmc_send_cmd(mmc, &cmd, NULL);
Thomas Choud52ebf12010-12-24 13:12:21 +00002481 if (err)
2482 return err;
2483 }
2484#endif
2485
Andy Fleming272cc702008-10-30 16:41:01 -05002486 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002487 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
2488 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05002489 cmd.resp_type = MMC_RSP_R2;
2490 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002491
Sean Andersonda129172020-10-17 08:36:27 -04002492 err = mmc_send_cmd_quirks(mmc, &cmd, NULL, MMC_QUIRK_RETRY_SEND_CID, 4);
Andy Fleming272cc702008-10-30 16:41:01 -05002493 if (err)
2494 return err;
2495
2496 memcpy(mmc->cid, cmd.response, 16);
2497
2498 /*
2499 * For MMC cards, set the Relative Address.
2500 * For SD cards, get the Relatvie Address.
2501 * This also puts the cards into Standby State
2502 */
Thomas Choud52ebf12010-12-24 13:12:21 +00002503 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2504 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
2505 cmd.cmdarg = mmc->rca << 16;
2506 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05002507
Thomas Choud52ebf12010-12-24 13:12:21 +00002508 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002509
Thomas Choud52ebf12010-12-24 13:12:21 +00002510 if (err)
2511 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002512
Thomas Choud52ebf12010-12-24 13:12:21 +00002513 if (IS_SD(mmc))
2514 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
2515 }
Andy Fleming272cc702008-10-30 16:41:01 -05002516
2517 /* Get the Card-Specific Data */
2518 cmd.cmdidx = MMC_CMD_SEND_CSD;
2519 cmd.resp_type = MMC_RSP_R2;
2520 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05002521
2522 err = mmc_send_cmd(mmc, &cmd, NULL);
2523
2524 if (err)
2525 return err;
2526
Rabin Vincent998be3d2009-04-05 13:30:56 +05302527 mmc->csd[0] = cmd.response[0];
2528 mmc->csd[1] = cmd.response[1];
2529 mmc->csd[2] = cmd.response[2];
2530 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05002531
2532 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302533 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05002534
2535 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07002536 case 0:
2537 mmc->version = MMC_VERSION_1_2;
2538 break;
2539 case 1:
2540 mmc->version = MMC_VERSION_1_4;
2541 break;
2542 case 2:
2543 mmc->version = MMC_VERSION_2_2;
2544 break;
2545 case 3:
2546 mmc->version = MMC_VERSION_3;
2547 break;
2548 case 4:
2549 mmc->version = MMC_VERSION_4;
2550 break;
2551 default:
2552 mmc->version = MMC_VERSION_1_2;
2553 break;
Andy Fleming272cc702008-10-30 16:41:01 -05002554 }
2555 }
2556
2557 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302558 freq = fbase[(cmd.response[0] & 0x7)];
2559 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05002560
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002561 mmc->legacy_speed = freq * mult;
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002562 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05002563
Markus Niebelab711882013-12-16 13:40:46 +01002564 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05302565 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002566#if CONFIG_IS_ENABLED(MMC_WRITE)
Andy Fleming272cc702008-10-30 16:41:01 -05002567
2568 if (IS_SD(mmc))
2569 mmc->write_bl_len = mmc->read_bl_len;
2570 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05302571 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002572#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002573
2574 if (mmc->high_capacity) {
2575 csize = (mmc->csd[1] & 0x3f) << 16
2576 | (mmc->csd[2] & 0xffff0000) >> 16;
2577 cmult = 8;
2578 } else {
2579 csize = (mmc->csd[1] & 0x3ff) << 2
2580 | (mmc->csd[2] & 0xc0000000) >> 30;
2581 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2582 }
2583
Stephen Warrenf866a462013-06-11 15:14:01 -06002584 mmc->capacity_user = (csize + 1) << (cmult + 2);
2585 mmc->capacity_user *= mmc->read_bl_len;
2586 mmc->capacity_boot = 0;
2587 mmc->capacity_rpmb = 0;
2588 for (i = 0; i < 4; i++)
2589 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002590
Simon Glass8bfa1952013-04-03 08:54:30 +00002591 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2592 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05002593
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002594#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass8bfa1952013-04-03 08:54:30 +00002595 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2596 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002597#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002598
Markus Niebelab711882013-12-16 13:40:46 +01002599 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2600 cmd.cmdidx = MMC_CMD_SET_DSR;
2601 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2602 cmd.resp_type = MMC_RSP_NONE;
2603 if (mmc_send_cmd(mmc, &cmd, NULL))
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002604 pr_warn("MMC: SET_DSR failed\n");
Markus Niebelab711882013-12-16 13:40:46 +01002605 }
2606
Andy Fleming272cc702008-10-30 16:41:01 -05002607 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002608 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2609 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00002610 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002611 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00002612 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002613
Thomas Choud52ebf12010-12-24 13:12:21 +00002614 if (err)
2615 return err;
2616 }
Andy Fleming272cc702008-10-30 16:41:01 -05002617
Lei Wene6f99a52011-06-22 17:03:31 +00002618 /*
2619 * For SD, its erase group is always one sector
2620 */
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002621#if CONFIG_IS_ENABLED(MMC_WRITE)
Lei Wene6f99a52011-06-22 17:03:31 +00002622 mmc->erase_grp_size = 1;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002623#endif
Lei Wenbc897b12011-05-02 16:26:26 +00002624 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00002625
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002626 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002627 if (err)
2628 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05302629
Simon Glassc40fdca2016-05-01 13:52:35 -06002630 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06002631 if (err)
2632 return err;
2633
Marek Vasut62d77ce2018-04-15 00:37:11 +02002634#if CONFIG_IS_ENABLED(MMC_TINY)
2635 mmc_set_clock(mmc, mmc->legacy_speed, false);
Faiz Abbase8d5dde2020-02-26 13:44:32 +05302636 mmc_select_mode(mmc, MMC_LEGACY);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002637 mmc_set_bus_width(mmc, 1);
2638#else
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002639 if (IS_SD(mmc)) {
2640 err = sd_get_capabilities(mmc);
2641 if (err)
2642 return err;
2643 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2644 } else {
2645 err = mmc_get_capabilities(mmc);
2646 if (err)
2647 return err;
Masahiro Yamada8adf50e2020-01-23 14:31:12 +09002648 err = mmc_select_mode_and_width(mmc, mmc->card_caps);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002649 }
Marek Vasut62d77ce2018-04-15 00:37:11 +02002650#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002651 if (err)
2652 return err;
2653
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002654 mmc->best_mode = mmc->selected_mode;
Jaehoon Chungad5fd922012-03-26 21:16:03 +00002655
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002656 /* Fix the block length for DDR mode */
2657 if (mmc->ddr_mode) {
2658 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002659#if CONFIG_IS_ENABLED(MMC_WRITE)
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002660 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002661#endif
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002662 }
2663
Andy Fleming272cc702008-10-30 16:41:01 -05002664 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06002665 bdesc = mmc_get_blk_desc(mmc);
2666 bdesc->lun = 0;
2667 bdesc->hwpart = 0;
2668 bdesc->type = 0;
2669 bdesc->blksz = mmc->read_bl_len;
2670 bdesc->log2blksz = LOG2(bdesc->blksz);
2671 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01002672#if !defined(CONFIG_SPL_BUILD) || \
2673 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
Simon Glass27084c02019-09-25 08:56:27 -06002674 !CONFIG_IS_ENABLED(USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06002675 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00002676 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2677 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002678 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002679 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2680 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2681 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002682 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002683 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01002684#else
Simon Glassc40fdca2016-05-01 13:52:35 -06002685 bdesc->vendor[0] = 0;
2686 bdesc->product[0] = 0;
2687 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01002688#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002689
Andre Przywaraeef05fd2018-12-17 10:05:45 +00002690#if !defined(CONFIG_DM_MMC) && (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT))
2691 part_init(bdesc);
2692#endif
2693
Andy Fleming272cc702008-10-30 16:41:01 -05002694 return 0;
2695}
2696
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002697static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002698{
2699 struct mmc_cmd cmd;
2700 int err;
2701
2702 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2703 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002704 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05002705 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05002706
2707 err = mmc_send_cmd(mmc, &cmd, NULL);
2708
2709 if (err)
2710 return err;
2711
Rabin Vincent998be3d2009-04-05 13:30:56 +05302712 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002713 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002714 else
2715 mmc->version = SD_VERSION_2;
2716
2717 return 0;
2718}
2719
Simon Glassc4d660d2017-07-04 13:31:19 -06002720#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002721/* board-specific MMC power initializations. */
2722__weak void board_mmc_power_init(void)
2723{
2724}
Simon Glass05cbeb72017-04-22 19:10:56 -06002725#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002726
Peng Fan2051aef2016-10-11 15:08:43 +08002727static int mmc_power_init(struct mmc *mmc)
2728{
Simon Glassc4d660d2017-07-04 13:31:19 -06002729#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002730#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08002731 int ret;
2732
2733 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002734 &mmc->vmmc_supply);
2735 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002736 pr_debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002737
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002738 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2739 &mmc->vqmmc_supply);
2740 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002741 pr_debug("%s: No vqmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002742#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06002743#else /* !CONFIG_DM_MMC */
2744 /*
2745 * Driver model should use a regulator, as above, rather than calling
2746 * out to board code.
2747 */
2748 board_mmc_power_init();
2749#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002750 return 0;
2751}
2752
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002753/*
2754 * put the host in the initial state:
2755 * - turn on Vdd (card power supply)
2756 * - configure the bus width and clock to minimal values
2757 */
2758static void mmc_set_initial_state(struct mmc *mmc)
2759{
2760 int err;
2761
2762 /* First try to set 3.3V. If it fails set to 1.8V */
2763 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2764 if (err != 0)
2765 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2766 if (err != 0)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002767 pr_warn("mmc: failed to set signal voltage\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002768
2769 mmc_select_mode(mmc, MMC_LEGACY);
2770 mmc_set_bus_width(mmc, 1);
Jaehoon Chung65117182018-01-26 19:25:29 +09002771 mmc_set_clock(mmc, 0, MMC_CLK_ENABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002772}
2773
2774static int mmc_power_on(struct mmc *mmc)
2775{
2776#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2777 if (mmc->vmmc_supply) {
2778 int ret = regulator_set_enable(mmc->vmmc_supply, true);
2779
Jaehoon Chungb3dc0162020-11-06 20:30:41 +09002780 if (ret && ret != -EACCES) {
Jaehoon Chung58896452020-11-17 07:04:59 +09002781 printf("Error enabling VMMC supply : %d\n", ret);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002782 return ret;
2783 }
2784 }
2785#endif
2786 return 0;
2787}
2788
2789static int mmc_power_off(struct mmc *mmc)
2790{
Jaehoon Chung65117182018-01-26 19:25:29 +09002791 mmc_set_clock(mmc, 0, MMC_CLK_DISABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002792#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2793 if (mmc->vmmc_supply) {
2794 int ret = regulator_set_enable(mmc->vmmc_supply, false);
2795
Jaehoon Chungb3dc0162020-11-06 20:30:41 +09002796 if (ret && ret != -EACCES) {
Jaehoon Chung58896452020-11-17 07:04:59 +09002797 pr_debug("Error disabling VMMC supply : %d\n", ret);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002798 return ret;
2799 }
2800 }
2801#endif
2802 return 0;
2803}
2804
2805static int mmc_power_cycle(struct mmc *mmc)
2806{
2807 int ret;
2808
2809 ret = mmc_power_off(mmc);
2810 if (ret)
2811 return ret;
Yann Gautier3602a562019-09-19 17:56:12 +02002812
2813 ret = mmc_host_power_cycle(mmc);
2814 if (ret)
2815 return ret;
2816
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002817 /*
2818 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2819 * to be on the safer side.
2820 */
2821 udelay(2000);
2822 return mmc_power_on(mmc);
2823}
2824
Pali Rohára4c577f2021-07-14 16:37:29 +02002825int mmc_get_op_cond(struct mmc *mmc, bool quiet)
Andy Fleming272cc702008-10-30 16:41:01 -05002826{
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002827 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
Macpaul Linafd59322011-11-14 23:35:39 +00002828 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05002829
Lei Wenbc897b12011-05-02 16:26:26 +00002830 if (mmc->has_init)
2831 return 0;
2832
Peng Fan2051aef2016-10-11 15:08:43 +08002833 err = mmc_power_init(mmc);
2834 if (err)
2835 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002836
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002837#ifdef CONFIG_MMC_QUIRKS
2838 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
Joel Johnsond4a5fa32020-01-11 09:08:14 -07002839 MMC_QUIRK_RETRY_SEND_CID |
2840 MMC_QUIRK_RETRY_APP_CMD;
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002841#endif
2842
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002843 err = mmc_power_cycle(mmc);
2844 if (err) {
2845 /*
2846 * if power cycling is not supported, we should not try
2847 * to use the UHS modes, because we wouldn't be able to
2848 * recover from an error during the UHS initialization.
2849 */
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002850 pr_debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002851 uhs_en = false;
2852 mmc->host_caps &= ~UHS_CAPS;
2853 err = mmc_power_on(mmc);
2854 }
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002855 if (err)
2856 return err;
2857
Simon Glasse7881d82017-07-29 11:35:31 -06002858#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu390f9bd2020-09-01 16:57:59 +08002859 /*
2860 * Re-initialization is needed to clear old configuration for
2861 * mmc rescan.
2862 */
2863 err = mmc_reinit(mmc);
Simon Glass8ca51e52016-06-12 23:30:22 -06002864#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02002865 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002866 err = mmc->cfg->ops->init(mmc);
Yangbo Lu390f9bd2020-09-01 16:57:59 +08002867#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002868 if (err)
2869 return err;
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06002870 mmc->ddr_mode = 0;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02002871
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002872retry:
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002873 mmc_set_initial_state(mmc);
Jean-Jacques Hiblot318a7a52017-09-21 16:30:01 +02002874
Andy Fleming272cc702008-10-30 16:41:01 -05002875 /* Reset the Card */
2876 err = mmc_go_idle(mmc);
2877
2878 if (err)
2879 return err;
2880
Marcel Ziswilerf5624b12019-05-20 02:44:53 +02002881 /* The internal partition reset to user partition(0) at every CMD0 */
Simon Glassc40fdca2016-05-01 13:52:35 -06002882 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00002883
Andy Fleming272cc702008-10-30 16:41:01 -05002884 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00002885 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002886
Andy Fleming272cc702008-10-30 16:41:01 -05002887 /* Now try to get the SD card's operating condition */
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002888 err = sd_send_op_cond(mmc, uhs_en);
2889 if (err && uhs_en) {
2890 uhs_en = false;
2891 mmc_power_cycle(mmc);
2892 goto retry;
2893 }
Andy Fleming272cc702008-10-30 16:41:01 -05002894
2895 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002896 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05002897 err = mmc_send_op_cond(mmc);
2898
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002899 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01002900#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Pali Rohára4c577f2021-07-14 16:37:29 +02002901 if (!quiet)
2902 pr_err("Card did not respond to voltage select! : %d\n", err);
Paul Burton56196822013-09-04 16:12:25 +01002903#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002904 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002905 }
2906 }
2907
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002908 return err;
2909}
2910
2911int mmc_start_init(struct mmc *mmc)
2912{
2913 bool no_card;
2914 int err = 0;
2915
2916 /*
2917 * all hosts are capable of 1 bit bus-width and able to use the legacy
2918 * timings.
2919 */
Faiz Abbase8d5dde2020-02-26 13:44:32 +05302920 mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(MMC_LEGACY) |
Aswath Govindraju19f7a342021-08-13 23:04:41 +05302921 MMC_MODE_1BIT;
2922
2923 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET)) {
2924 if (mmc->user_speed_mode != MMC_MODES_END) {
2925 int i;
2926 /* set host caps */
2927 if (mmc->host_caps & MMC_CAP(mmc->user_speed_mode)) {
2928 /* Remove all existing speed capabilities */
2929 for (i = MMC_LEGACY; i < MMC_MODES_END; i++)
2930 mmc->host_caps &= ~MMC_CAP(i);
2931 mmc->host_caps |= (MMC_CAP(mmc->user_speed_mode)
2932 | MMC_CAP(MMC_LEGACY) |
2933 MMC_MODE_1BIT);
2934 } else {
2935 pr_err("bus_mode requested is not supported\n");
2936 return -EINVAL;
2937 }
2938 }
2939 }
Faiz Abbas32860bd2020-02-26 13:44:30 +05302940#if CONFIG_IS_ENABLED(DM_MMC)
2941 mmc_deferred_probe(mmc);
2942#endif
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002943#if !defined(CONFIG_MMC_BROKEN_CD)
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002944 no_card = mmc_getcd(mmc) == 0;
2945#else
2946 no_card = 0;
2947#endif
2948#if !CONFIG_IS_ENABLED(DM_MMC)
Baruch Siachfea39392019-07-22 15:52:12 +03002949 /* we pretend there's no card when init is NULL */
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002950 no_card = no_card || (mmc->cfg->ops->init == NULL);
2951#endif
2952 if (no_card) {
2953 mmc->has_init = 0;
2954#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2955 pr_err("MMC: no card present\n");
2956#endif
2957 return -ENOMEDIUM;
2958 }
2959
Pali Rohára4c577f2021-07-14 16:37:29 +02002960 err = mmc_get_op_cond(mmc, false);
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002961
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002962 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002963 mmc->init_in_progress = 1;
2964
2965 return err;
2966}
2967
2968static int mmc_complete_init(struct mmc *mmc)
2969{
2970 int err = 0;
2971
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002972 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002973 if (mmc->op_cond_pending)
2974 err = mmc_complete_op_cond(mmc);
2975
2976 if (!err)
2977 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00002978 if (err)
2979 mmc->has_init = 0;
2980 else
2981 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002982 return err;
2983}
2984
2985int mmc_init(struct mmc *mmc)
2986{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002987 int err = 0;
Vipul Kumar36332b62018-05-03 12:20:54 +05302988 __maybe_unused ulong start;
Simon Glassc4d660d2017-07-04 13:31:19 -06002989#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06002990 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00002991
Simon Glass33fb2112016-05-01 13:52:41 -06002992 upriv->mmc = mmc;
2993#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00002994 if (mmc->has_init)
2995 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02002996
2997 start = get_timer(0);
2998
Che-Liang Chioue9550442012-11-28 15:21:13 +00002999 if (!mmc->init_in_progress)
3000 err = mmc_start_init(mmc);
3001
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05003002 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00003003 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01003004 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09003005 pr_info("%s: %d, time %lu\n", __func__, err, get_timer(start));
Jagan Teki919b4852017-01-10 11:18:43 +01003006
Lei Wenbc897b12011-05-02 16:26:26 +00003007 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05003008}
3009
Marek Vasutfceea992019-01-29 04:45:51 +01003010#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
3011 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
3012 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
3013int mmc_deinit(struct mmc *mmc)
3014{
3015 u32 caps_filtered;
3016
3017 if (!mmc->has_init)
3018 return 0;
3019
3020 if (IS_SD(mmc)) {
3021 caps_filtered = mmc->card_caps &
3022 ~(MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25) |
3023 MMC_CAP(UHS_SDR50) | MMC_CAP(UHS_DDR50) |
3024 MMC_CAP(UHS_SDR104));
3025
3026 return sd_select_mode_and_width(mmc, caps_filtered);
3027 } else {
3028 caps_filtered = mmc->card_caps &
Ye Lifb8c2e82021-08-17 17:20:34 +08003029 ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_400_ES));
Marek Vasutfceea992019-01-29 04:45:51 +01003030
3031 return mmc_select_mode_and_width(mmc, caps_filtered);
3032 }
3033}
3034#endif
3035
Markus Niebelab711882013-12-16 13:40:46 +01003036int mmc_set_dsr(struct mmc *mmc, u16 val)
3037{
3038 mmc->dsr = val;
3039 return 0;
3040}
3041
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02003042/* CPU-specific MMC initializations */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003043__weak int cpu_mmc_init(struct bd_info *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05003044{
3045 return -1;
3046}
3047
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02003048/* board-specific MMC initializations. */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003049__weak int board_mmc_init(struct bd_info *bis)
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02003050{
3051 return -1;
3052}
Andy Fleming272cc702008-10-30 16:41:01 -05003053
Che-Liang Chioue9550442012-11-28 15:21:13 +00003054void mmc_set_preinit(struct mmc *mmc, int preinit)
3055{
3056 mmc->preinit = preinit;
3057}
3058
Faiz Abbas8a856db2018-02-12 19:35:24 +05303059#if CONFIG_IS_ENABLED(DM_MMC)
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003060static int mmc_probe(struct bd_info *bis)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003061{
Simon Glass4a1db6d2015-12-29 05:22:49 -07003062 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003063 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07003064 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003065
3066 ret = uclass_get(UCLASS_MMC, &uc);
3067 if (ret)
3068 return ret;
3069
Simon Glass4a1db6d2015-12-29 05:22:49 -07003070 /*
3071 * Try to add them in sequence order. Really with driver model we
3072 * should allow holes, but the current MMC list does not allow that.
3073 * So if we request 0, 1, 3 we will get 0, 1, 2.
3074 */
3075 for (i = 0; ; i++) {
3076 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
3077 if (ret == -ENODEV)
3078 break;
3079 }
3080 uclass_foreach_dev(dev, uc) {
3081 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003082 if (ret)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01003083 pr_err("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003084 }
3085
3086 return 0;
3087}
3088#else
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003089static int mmc_probe(struct bd_info *bis)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003090{
3091 if (board_mmc_init(bis) < 0)
3092 cpu_mmc_init(bis);
3093
3094 return 0;
3095}
3096#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00003097
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003098int mmc_initialize(struct bd_info *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05003099{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003100 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003101 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003102 if (initialized) /* Avoid initializing mmc multiple times */
3103 return 0;
3104 initialized = 1;
3105
Simon Glassc4d660d2017-07-04 13:31:19 -06003106#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01003107#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06003108 mmc_list_init();
3109#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01003110#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003111 ret = mmc_probe(bis);
3112 if (ret)
3113 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05003114
Ying Zhangbb0dc102013-08-16 15:16:11 +08003115#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05003116 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08003117#endif
Andy Fleming272cc702008-10-30 16:41:01 -05003118
Simon Glassc40fdca2016-05-01 13:52:35 -06003119 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05003120 return 0;
3121}
Tomas Melincd3d4882016-11-25 11:01:03 +02003122
Lokesh Vutla80f02012019-09-09 14:40:36 +05303123#if CONFIG_IS_ENABLED(DM_MMC)
3124int mmc_init_device(int num)
3125{
3126 struct udevice *dev;
3127 struct mmc *m;
3128 int ret;
3129
Aswath Govindraju2153a082021-03-25 12:48:47 +05303130 if (uclass_get_device_by_seq(UCLASS_MMC, num, &dev)) {
3131 ret = uclass_get_device(UCLASS_MMC, num, &dev);
3132 if (ret)
3133 return ret;
3134 }
Lokesh Vutla80f02012019-09-09 14:40:36 +05303135
3136 m = mmc_get_mmc_dev(dev);
3137 if (!m)
3138 return 0;
Venkatesh Yadav Abbarapu337af542022-09-29 10:22:49 +05303139
3140 /* Initialising user set speed mode */
3141 m->user_speed_mode = MMC_MODES_END;
3142
Lokesh Vutla80f02012019-09-09 14:40:36 +05303143 if (m->preinit)
3144 mmc_start_init(m);
3145
3146 return 0;
3147}
3148#endif
3149
Tomas Melincd3d4882016-11-25 11:01:03 +02003150#ifdef CONFIG_CMD_BKOPS_ENABLE
Marek Vasutcf1f7352023-01-05 15:19:08 +01003151int mmc_set_bkops_enable(struct mmc *mmc, bool autobkops, bool enable)
Tomas Melincd3d4882016-11-25 11:01:03 +02003152{
3153 int err;
Marek Vasutcf1f7352023-01-05 15:19:08 +01003154 u32 bit = autobkops ? BIT(1) : BIT(0);
Tomas Melincd3d4882016-11-25 11:01:03 +02003155 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
3156
3157 err = mmc_send_ext_csd(mmc, ext_csd);
3158 if (err) {
3159 puts("Could not get ext_csd register values\n");
3160 return err;
3161 }
3162
3163 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
3164 puts("Background operations not supported on device\n");
3165 return -EMEDIUMTYPE;
3166 }
3167
Marek Vasutcf1f7352023-01-05 15:19:08 +01003168 if (enable && (ext_csd[EXT_CSD_BKOPS_EN] & bit)) {
Tomas Melincd3d4882016-11-25 11:01:03 +02003169 puts("Background operations already enabled\n");
3170 return 0;
3171 }
3172
Marek Vasutcf1f7352023-01-05 15:19:08 +01003173 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN,
3174 enable ? bit : 0);
Tomas Melincd3d4882016-11-25 11:01:03 +02003175 if (err) {
Marek Vasutcf1f7352023-01-05 15:19:08 +01003176 printf("Failed to %sable manual background operations\n",
3177 enable ? "en" : "dis");
Tomas Melincd3d4882016-11-25 11:01:03 +02003178 return err;
3179 }
3180
Marek Vasutcf1f7352023-01-05 15:19:08 +01003181 printf("%sabled %s background operations\n",
3182 enable ? "En" : "Dis", autobkops ? "auto" : "manual");
Tomas Melincd3d4882016-11-25 11:01:03 +02003183
3184 return 0;
3185}
3186#endif
David Woodhouse4dee3f72020-08-04 10:05:46 +01003187
3188__weak int mmc_get_env_dev(void)
3189{
3190#ifdef CONFIG_SYS_MMC_ENV_DEV
3191 return CONFIG_SYS_MMC_ENV_DEV;
3192#else
3193 return 0;
3194#endif
3195}