blob: b18dc331f785a08d9080eef3325c52aeab6e0f4e [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>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <blk.h>
Andy Fleming272cc702008-10-30 16:41:01 -050012#include <command.h>
Sjoerd Simons8e3332e2015-08-30 16:55:45 -060013#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Sjoerd Simons8e3332e2015-08-30 16:55:45 -060015#include <dm/device-internal.h>
Stephen Warrend4622df2014-05-23 12:47:06 -060016#include <errno.h>
Andy Fleming272cc702008-10-30 16:41:01 -050017#include <mmc.h>
18#include <part.h>
Tom Rini03de3052024-05-20 13:35:03 -060019#include <time.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>
Simon Glass1e94b462023-09-14 18:21:46 -060022#include <linux/printk.h>
Peng Fan2051aef2016-10-11 15:08:43 +080023#include <power/regulator.h>
Andy Fleming272cc702008-10-30 16:41:01 -050024#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060025#include <memalign.h>
Andy Fleming272cc702008-10-30 16:41:01 -050026#include <linux/list.h>
Tom Rini03de3052024-05-20 13:35:03 -060027#include <linux/printk.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053028#include <div64.h>
Paul Burtonda61fa52013-09-09 15:30:26 +010029#include "mmc_private.h"
Andy Fleming272cc702008-10-30 16:41:01 -050030
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +020031#define DEFAULT_CMD6_TIMEOUT_MS 500
32
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +020033static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
Marek Vasutb5b838f2016-12-01 02:06:33 +010034
Simon Glasse7881d82017-07-29 11:35:31 -060035#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020036
Sam Protsenko6cf8a902019-08-14 22:52:51 +030037static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020038{
Loic Poulaind6ad5a02022-05-26 16:37:21 +020039 if (mmc->cfg->ops->wait_dat0)
40 return mmc->cfg->ops->wait_dat0(mmc, state, timeout_us);
41
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020042 return -ENOSYS;
43}
44
Jeroen Hofstee750121c2014-07-12 21:24:08 +020045__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000046{
47 return -1;
48}
49
50int mmc_getwp(struct mmc *mmc)
51{
52 int wp;
53
54 wp = board_mmc_getwp(mmc);
55
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000056 if (wp < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020057 if (mmc->cfg->ops->getwp)
58 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000059 else
60 wp = 0;
61 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000062
63 return wp;
64}
65
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +020066__weak int board_mmc_getcd(struct mmc *mmc)
67{
Stefano Babic11fdade2010-02-05 15:04:43 +010068 return -1;
69}
Simon Glass8ca51e52016-06-12 23:30:22 -060070#endif
Stefano Babic11fdade2010-02-05 15:04:43 +010071
Marek Vasut8635ff92012-03-15 18:41:35 +000072#ifdef CONFIG_MMC_TRACE
Simon Glassc0c76eb2016-06-12 23:30:20 -060073void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
74{
75 printf("CMD_SEND:%d\n", cmd->cmdidx);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010076 printf("\t\tARG\t\t\t 0x%08x\n", cmd->cmdarg);
Simon Glassc0c76eb2016-06-12 23:30:20 -060077}
78
79void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
80{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000081 int i;
82 u8 *ptr;
83
Bin Meng7863ce52016-03-17 21:53:14 -070084 if (ret) {
85 printf("\t\tRET\t\t\t %d\n", ret);
86 } else {
87 switch (cmd->resp_type) {
88 case MMC_RSP_NONE:
89 printf("\t\tMMC_RSP_NONE\n");
90 break;
91 case MMC_RSP_R1:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010092 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070093 cmd->response[0]);
94 break;
95 case MMC_RSP_R1b:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010096 printf("\t\tMMC_RSP_R1b\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070097 cmd->response[0]);
98 break;
99 case MMC_RSP_R2:
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100100 printf("\t\tMMC_RSP_R2\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700101 cmd->response[0]);
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[1]);
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[2]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100106 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700107 cmd->response[3]);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000108 printf("\n");
Bin Meng7863ce52016-03-17 21:53:14 -0700109 printf("\t\t\t\t\tDUMPING DATA\n");
110 for (i = 0; i < 4; i++) {
111 int j;
112 printf("\t\t\t\t\t%03d - ", i*4);
113 ptr = (u8 *)&cmd->response[i];
114 ptr += 3;
115 for (j = 0; j < 4; j++)
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100116 printf("%02x ", *ptr--);
Bin Meng7863ce52016-03-17 21:53:14 -0700117 printf("\n");
118 }
119 break;
120 case MMC_RSP_R3:
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100121 printf("\t\tMMC_RSP_R3,4\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700122 cmd->response[0]);
123 break;
124 default:
125 printf("\t\tERROR MMC rsp not supported\n");
126 break;
Bin Meng53e8e402016-03-17 21:53:13 -0700127 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000128 }
Simon Glassc0c76eb2016-06-12 23:30:20 -0600129}
130
131void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
132{
133 int status;
134
135 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
136 printf("CURR STATE:%d\n", status);
137}
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000138#endif
Simon Glassc0c76eb2016-06-12 23:30:20 -0600139
Pali Rohár48467e42022-04-03 00:20:10 +0200140#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG) || CONFIG_VAL(LOGLEVEL) >= LOGL_DEBUG
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200141const char *mmc_mode_name(enum bus_mode mode)
142{
143 static const char *const names[] = {
144 [MMC_LEGACY] = "MMC legacy",
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200145 [MMC_HS] = "MMC High Speed (26MHz)",
146 [SD_HS] = "SD High Speed (50MHz)",
147 [UHS_SDR12] = "UHS SDR12 (25MHz)",
148 [UHS_SDR25] = "UHS SDR25 (50MHz)",
149 [UHS_SDR50] = "UHS SDR50 (100MHz)",
150 [UHS_SDR104] = "UHS SDR104 (208MHz)",
151 [UHS_DDR50] = "UHS DDR50 (50MHz)",
152 [MMC_HS_52] = "MMC High Speed (52MHz)",
153 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
154 [MMC_HS_200] = "HS200 (200MHz)",
Peng Fan3dd26262018-08-10 14:07:54 +0800155 [MMC_HS_400] = "HS400 (200MHz)",
Peng Fan44acd492019-07-10 14:43:07 +0800156 [MMC_HS_400_ES] = "HS400ES (200MHz)",
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200157 };
158
159 if (mode >= MMC_MODES_END)
160 return "Unknown mode";
161 else
162 return names[mode];
163}
164#endif
165
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200166static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
167{
168 static const int freqs[] = {
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900169 [MMC_LEGACY] = 25000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200170 [MMC_HS] = 26000000,
171 [SD_HS] = 50000000,
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900172 [MMC_HS_52] = 52000000,
173 [MMC_DDR_52] = 52000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200174 [UHS_SDR12] = 25000000,
175 [UHS_SDR25] = 50000000,
176 [UHS_SDR50] = 100000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200177 [UHS_DDR50] = 50000000,
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100178 [UHS_SDR104] = 208000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200179 [MMC_HS_200] = 200000000,
Peng Fan3dd26262018-08-10 14:07:54 +0800180 [MMC_HS_400] = 200000000,
Peng Fan44acd492019-07-10 14:43:07 +0800181 [MMC_HS_400_ES] = 200000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200182 };
183
184 if (mode == MMC_LEGACY)
185 return mmc->legacy_speed;
186 else if (mode >= MMC_MODES_END)
187 return 0;
188 else
189 return freqs[mode];
190}
191
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200192static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
193{
194 mmc->selected_mode = mode;
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200195 mmc->tran_speed = mmc_mode2freq(mmc, mode);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200196 mmc->ddr_mode = mmc_is_mode_ddr(mode);
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900197 pr_debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
198 mmc->tran_speed / 1000000);
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200199 return 0;
200}
201
Simon Glasse7881d82017-07-29 11:35:31 -0600202#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassc0c76eb2016-06-12 23:30:20 -0600203int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
204{
205 int ret;
206
207 mmmc_trace_before_send(mmc, cmd);
208 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
209 mmmc_trace_after_send(mmc, cmd, ret);
210
Marek Vasut8635ff92012-03-15 18:41:35 +0000211 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500212}
Simon Glass8ca51e52016-06-12 23:30:22 -0600213#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500214
Sean Andersonda129172020-10-17 08:36:27 -0400215/**
216 * mmc_send_cmd_retry() - send a command to the mmc device, retrying on error
217 *
218 * @dev: device to receive the command
219 * @cmd: command to send
220 * @data: additional data to send/receive
221 * @retries: how many times to retry; mmc_send_cmd is always called at least
222 * once
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100223 * Return: 0 if ok, -ve on error
Sean Andersonda129172020-10-17 08:36:27 -0400224 */
225static int mmc_send_cmd_retry(struct mmc *mmc, struct mmc_cmd *cmd,
226 struct mmc_data *data, uint retries)
227{
228 int ret;
229
230 do {
231 ret = mmc_send_cmd(mmc, cmd, data);
232 } while (ret && retries--);
233
234 return ret;
235}
236
237/**
238 * mmc_send_cmd_quirks() - send a command to the mmc device, retrying if a
239 * specific quirk is enabled
240 *
241 * @dev: device to receive the command
242 * @cmd: command to send
243 * @data: additional data to send/receive
244 * @quirk: retry only if this quirk is enabled
245 * @retries: how many times to retry; mmc_send_cmd is always called at least
246 * once
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100247 * Return: 0 if ok, -ve on error
Sean Andersonda129172020-10-17 08:36:27 -0400248 */
249static int mmc_send_cmd_quirks(struct mmc *mmc, struct mmc_cmd *cmd,
250 struct mmc_data *data, u32 quirk, uint retries)
251{
Simon Glass497b7c62023-02-05 15:40:16 -0700252 if (IS_ENABLED(CONFIG_MMC_QUIRKS) && mmc->quirks & quirk)
Sean Andersonda129172020-10-17 08:36:27 -0400253 return mmc_send_cmd_retry(mmc, cmd, data, retries);
254 else
255 return mmc_send_cmd(mmc, cmd, data);
256}
257
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200258int mmc_send_status(struct mmc *mmc, unsigned int *status)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000259{
260 struct mmc_cmd cmd;
Sean Andersonda129172020-10-17 08:36:27 -0400261 int ret;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000262
263 cmd.cmdidx = MMC_CMD_SEND_STATUS;
264 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200265 if (!mmc_host_is_spi(mmc))
266 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000267
Sean Andersonda129172020-10-17 08:36:27 -0400268 ret = mmc_send_cmd_retry(mmc, &cmd, NULL, 4);
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200269 mmc_trace_state(mmc, &cmd);
Sean Andersonda129172020-10-17 08:36:27 -0400270 if (!ret)
271 *status = cmd.response[0];
272
273 return ret;
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200274}
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200275
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300276int mmc_poll_for_busy(struct mmc *mmc, int timeout_ms)
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200277{
278 unsigned int status;
279 int err;
280
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300281 err = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotcd0b80e2019-07-02 10:53:53 +0200282 if (err != -ENOSYS)
283 return err;
284
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200285 while (1) {
286 err = mmc_send_status(mmc, &status);
287 if (err)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000288 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000289
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200290 if ((status & MMC_STATUS_RDY_FOR_DATA) &&
291 (status & MMC_STATUS_CURR_STATE) !=
292 MMC_STATE_PRG)
293 break;
294
295 if (status & MMC_STATUS_MASK) {
296#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
297 pr_err("Status Error: 0x%08x\n", status);
298#endif
299 return -ECOMM;
300 }
301
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300302 if (timeout_ms-- <= 0)
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500303 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000304
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500305 udelay(1000);
306 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000307
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300308 if (timeout_ms <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100309#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100310 pr_err("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100311#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900312 return -ETIMEDOUT;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000313 }
314
315 return 0;
316}
317
Paul Burtonda61fa52013-09-09 15:30:26 +0100318int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500319{
320 struct mmc_cmd cmd;
321
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600322 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900323 return 0;
324
Andy Fleming272cc702008-10-30 16:41:01 -0500325 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
326 cmd.resp_type = MMC_RSP_R1;
327 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500328
Sean Andersonda129172020-10-17 08:36:27 -0400329 return mmc_send_cmd_quirks(mmc, &cmd, NULL,
330 MMC_QUIRK_RETRY_SET_BLOCKLEN, 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500331}
332
Tom Rini03de3052024-05-20 13:35:03 -0600333#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200334static const u8 tuning_blk_pattern_4bit[] = {
335 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
336 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
337 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
338 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
339 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
340 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
341 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
342 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
343};
344
345static const u8 tuning_blk_pattern_8bit[] = {
346 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
347 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
348 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
349 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
350 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
351 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
352 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
353 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
354 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
355 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
356 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
357 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
358 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
359 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
360 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
361 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
362};
363
Marek Vasuta3b27862024-02-20 09:36:23 +0100364int mmc_send_tuning(struct mmc *mmc, u32 opcode)
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200365{
366 struct mmc_cmd cmd;
367 struct mmc_data data;
368 const u8 *tuning_block_pattern;
369 int size, err;
370
371 if (mmc->bus_width == 8) {
372 tuning_block_pattern = tuning_blk_pattern_8bit;
373 size = sizeof(tuning_blk_pattern_8bit);
374 } else if (mmc->bus_width == 4) {
375 tuning_block_pattern = tuning_blk_pattern_4bit;
376 size = sizeof(tuning_blk_pattern_4bit);
377 } else {
378 return -EINVAL;
379 }
380
381 ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size);
382
383 cmd.cmdidx = opcode;
384 cmd.cmdarg = 0;
385 cmd.resp_type = MMC_RSP_R1;
386
387 data.dest = (void *)data_buf;
388 data.blocks = 1;
389 data.blocksize = size;
390 data.flags = MMC_DATA_READ;
391
392 err = mmc_send_cmd(mmc, &cmd, &data);
393 if (err)
394 return err;
395
396 if (memcmp(data_buf, tuning_block_pattern, size))
397 return -EIO;
398
399 return 0;
400}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100401#endif
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200402
Hai Pham0ac2cca2023-06-20 00:38:24 +0200403int mmc_send_stop_transmission(struct mmc *mmc, bool write)
404{
405 struct mmc_cmd cmd;
406
407 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
408 cmd.cmdarg = 0;
409 /*
410 * JEDEC Standard No. 84-B51 Page 126
411 * CMD12 STOP_TRANSMISSION R1/R1b[3]
412 * NOTE 3 R1 for read cases and R1b for write cases.
413 *
414 * Physical Layer Simplified Specification Version 9.00
415 * 7.3.1.3 Detailed Command Description
416 * CMD12 R1b
417 */
418 cmd.resp_type = (IS_SD(mmc) || write) ? MMC_RSP_R1b : MMC_RSP_R1;
419
420 return mmc_send_cmd(mmc, &cmd, NULL);
421}
422
Sascha Silbeff8fef52013-06-14 13:07:25 +0200423static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000424 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500425{
426 struct mmc_cmd cmd;
427 struct mmc_data data;
428
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700429 if (blkcnt > 1)
430 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
431 else
432 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500433
434 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700435 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500436 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700437 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500438
439 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500440
441 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700442 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500443 data.blocksize = mmc->read_bl_len;
444 data.flags = MMC_DATA_READ;
445
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700446 if (mmc_send_cmd(mmc, &cmd, &data))
447 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500448
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700449 if (blkcnt > 1) {
Hai Pham0ac2cca2023-06-20 00:38:24 +0200450 if (mmc_send_stop_transmission(mmc, false)) {
Paul Burton56196822013-09-04 16:12:25 +0100451#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100452 pr_err("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100453#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700454 return 0;
455 }
Andy Fleming272cc702008-10-30 16:41:01 -0500456 }
457
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700458 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500459}
460
Marek Vasut145429a2020-04-04 12:45:05 +0200461#if !CONFIG_IS_ENABLED(DM_MMC)
462static int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
463{
464 if (mmc->cfg->ops->get_b_max)
465 return mmc->cfg->ops->get_b_max(mmc, dst, blkcnt);
466 else
467 return mmc->cfg->b_max;
468}
469#endif
470
Simon Glassc4d660d2017-07-04 13:31:19 -0600471#if CONFIG_IS_ENABLED(BLK)
Simon Glass7dba0b92016-06-12 23:30:15 -0600472ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600473#else
Simon Glass7dba0b92016-06-12 23:30:15 -0600474ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
475 void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600476#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500477{
Simon Glassc4d660d2017-07-04 13:31:19 -0600478#if CONFIG_IS_ENABLED(BLK)
Simon Glasscaa4daa2020-12-03 16:55:18 -0700479 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass33fb2112016-05-01 13:52:41 -0600480#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700481 int dev_num = block_dev->devnum;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700482 int err;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700483 lbaint_t cur, blocks_todo = blkcnt;
Marek Vasut145429a2020-04-04 12:45:05 +0200484 uint b_max;
Andy Fleming272cc702008-10-30 16:41:01 -0500485
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700486 if (blkcnt == 0)
487 return 0;
488
489 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500490 if (!mmc)
491 return 0;
492
Marek Vasutb5b838f2016-12-01 02:06:33 +0100493 if (CONFIG_IS_ENABLED(MMC_TINY))
494 err = mmc_switch_part(mmc, block_dev->hwpart);
495 else
496 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
497
Stephen Warren873cc1d2015-12-07 11:38:49 -0700498 if (err < 0)
499 return 0;
500
Simon Glassc40fdca2016-05-01 13:52:35 -0600501 if ((start + blkcnt) > block_dev->lba) {
Paul Burton56196822013-09-04 16:12:25 +0100502#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100503 pr_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
504 start + blkcnt, block_dev->lba);
Paul Burton56196822013-09-04 16:12:25 +0100505#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800506 return 0;
507 }
Andy Fleming272cc702008-10-30 16:41:01 -0500508
Simon Glass11692992015-06-23 15:38:50 -0600509 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900510 pr_debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500511 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600512 }
Andy Fleming272cc702008-10-30 16:41:01 -0500513
Marek Vasut145429a2020-04-04 12:45:05 +0200514 b_max = mmc_get_b_max(mmc, dst, blkcnt);
515
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700516 do {
Marek Vasut145429a2020-04-04 12:45:05 +0200517 cur = (blocks_todo > b_max) ? b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600518 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900519 pr_debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700520 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600521 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700522 blocks_todo -= cur;
523 start += cur;
524 dst += cur * mmc->read_bl_len;
525 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500526
527 return blkcnt;
528}
529
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000530static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500531{
532 struct mmc_cmd cmd;
533 int err;
534
535 udelay(1000);
536
537 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
538 cmd.cmdarg = 0;
539 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500540
541 err = mmc_send_cmd(mmc, &cmd, NULL);
542
543 if (err)
544 return err;
545
546 udelay(2000);
547
548 return 0;
549}
550
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100551#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200552static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
553{
554 struct mmc_cmd cmd;
555 int err = 0;
556
557 /*
558 * Send CMD11 only if the request is to switch the card to
559 * 1.8V signalling.
560 */
561 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
562 return mmc_set_signal_voltage(mmc, signal_voltage);
563
564 cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
565 cmd.cmdarg = 0;
566 cmd.resp_type = MMC_RSP_R1;
567
568 err = mmc_send_cmd(mmc, &cmd, NULL);
569 if (err)
570 return err;
571
572 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
573 return -EIO;
574
575 /*
576 * The card should drive cmd and dat[0:3] low immediately
577 * after the response of cmd11, but wait 100 us to be sure
578 */
579 err = mmc_wait_dat0(mmc, 0, 100);
580 if (err == -ENOSYS)
581 udelay(100);
582 else if (err)
583 return -ETIMEDOUT;
584
585 /*
586 * During a signal voltage level switch, the clock must be gated
587 * for 5 ms according to the SD spec
588 */
Jaehoon Chung65117182018-01-26 19:25:29 +0900589 mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200590
591 err = mmc_set_signal_voltage(mmc, signal_voltage);
592 if (err)
593 return err;
594
595 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
596 mdelay(10);
Jaehoon Chung65117182018-01-26 19:25:29 +0900597 mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200598
599 /*
600 * Failure to switch is indicated by the card holding
601 * dat[0:3] low. Wait for at least 1 ms according to spec
602 */
603 err = mmc_wait_dat0(mmc, 1, 1000);
604 if (err == -ENOSYS)
605 udelay(1000);
606 else if (err)
607 return -ETIMEDOUT;
608
609 return 0;
610}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100611#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200612
613static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
Andy Fleming272cc702008-10-30 16:41:01 -0500614{
615 int timeout = 1000;
616 int err;
617 struct mmc_cmd cmd;
618
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500619 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500620 cmd.cmdidx = MMC_CMD_APP_CMD;
621 cmd.resp_type = MMC_RSP_R1;
622 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500623
624 err = mmc_send_cmd(mmc, &cmd, NULL);
625
626 if (err)
627 return err;
628
629 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
630 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100631
632 /*
633 * Most cards do not answer if some reserved bits
634 * in the ocr are set. However, Some controller
635 * can set bit 7 (reserved for low voltages), but
636 * how to manage low voltages SD card is not yet
637 * specified.
638 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000639 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200640 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500641
642 if (mmc->version == SD_VERSION_2)
643 cmd.cmdarg |= OCR_HCS;
644
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200645 if (uhs_en)
646 cmd.cmdarg |= OCR_S18R;
647
Andy Fleming272cc702008-10-30 16:41:01 -0500648 err = mmc_send_cmd(mmc, &cmd, NULL);
649
650 if (err)
651 return err;
652
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500653 if (cmd.response[0] & OCR_BUSY)
654 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500655
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500656 if (timeout-- <= 0)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900657 return -EOPNOTSUPP;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500658
659 udelay(1000);
660 }
Andy Fleming272cc702008-10-30 16:41:01 -0500661
662 if (mmc->version != SD_VERSION_2)
663 mmc->version = SD_VERSION_1_0;
664
Thomas Choud52ebf12010-12-24 13:12:21 +0000665 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
666 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
667 cmd.resp_type = MMC_RSP_R3;
668 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000669
670 err = mmc_send_cmd(mmc, &cmd, NULL);
671
672 if (err)
673 return err;
674 }
675
Rabin Vincent998be3d2009-04-05 13:30:56 +0530676 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500677
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100678#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200679 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
680 == 0x41000000) {
681 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
682 if (err)
683 return err;
684 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100685#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200686
Andy Fleming272cc702008-10-30 16:41:01 -0500687 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
688 mmc->rca = 0;
689
690 return 0;
691}
692
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500693static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500694{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500695 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500696 int err;
697
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500698 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
699 cmd.resp_type = MMC_RSP_R3;
700 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500701 if (use_arg && !mmc_host_is_spi(mmc))
702 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200703 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500704 (mmc->ocr & OCR_VOLTAGE_MASK)) |
705 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000706
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500707 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000708 if (err)
709 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500710 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000711 return 0;
712}
713
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200714static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000715{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000716 int err, i;
Haibo Chenfe959052020-06-15 17:18:12 +0800717 int timeout = 1000;
718 uint start;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000719
Andy Fleming272cc702008-10-30 16:41:01 -0500720 /* Some cards seem to need this */
721 mmc_go_idle(mmc);
722
Haibo Chenfe959052020-06-15 17:18:12 +0800723 start = get_timer(0);
Wolfgang Denk0cf207e2021-09-27 17:42:39 +0200724 /* Asking to the card its capabilities */
Haibo Chenfe959052020-06-15 17:18:12 +0800725 for (i = 0; ; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500726 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500727 if (err)
728 return err;
729
Che-Liang Chioue9550442012-11-28 15:21:13 +0000730 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500731 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500732 break;
Haibo Chenfe959052020-06-15 17:18:12 +0800733
734 if (get_timer(start) > timeout)
735 return -ETIMEDOUT;
736 udelay(100);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000737 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500738 mmc->op_cond_pending = 1;
739 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000740}
Andy Fleming272cc702008-10-30 16:41:01 -0500741
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200742static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000743{
744 struct mmc_cmd cmd;
745 int timeout = 1000;
Vipul Kumar36332b62018-05-03 12:20:54 +0530746 ulong start;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000747 int err;
748
749 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500750 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lud188b112016-08-02 15:33:18 +0800751 /* Some cards seem to need this */
752 mmc_go_idle(mmc);
753
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500754 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500755 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500756 err = mmc_send_op_cond_iter(mmc, 1);
757 if (err)
758 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500759 if (mmc->ocr & OCR_BUSY)
760 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500761 if (get_timer(start) > timeout)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900762 return -EOPNOTSUPP;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500763 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500764 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500765 }
Andy Fleming272cc702008-10-30 16:41:01 -0500766
Thomas Choud52ebf12010-12-24 13:12:21 +0000767 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
768 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
769 cmd.resp_type = MMC_RSP_R3;
770 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000771
772 err = mmc_send_cmd(mmc, &cmd, NULL);
773
774 if (err)
775 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500776
777 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000778 }
779
Andy Fleming272cc702008-10-30 16:41:01 -0500780 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500781
782 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700783 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500784
785 return 0;
786}
787
788
Heinrich Schuchardt1601ea22020-03-30 07:24:17 +0200789int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500790{
791 struct mmc_cmd cmd;
792 struct mmc_data data;
793 int err;
794
795 /* Get the Card Status Register */
796 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
797 cmd.resp_type = MMC_RSP_R1;
798 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500799
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000800 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500801 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000802 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500803 data.flags = MMC_DATA_READ;
804
805 err = mmc_send_cmd(mmc, &cmd, &data);
806
807 return err;
808}
809
Marek Vasut68925502019-02-06 11:34:27 +0100810static int __mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value,
811 bool send_status)
Andy Fleming272cc702008-10-30 16:41:01 -0500812{
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200813 unsigned int status, start;
Andy Fleming272cc702008-10-30 16:41:01 -0500814 struct mmc_cmd cmd;
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300815 int timeout_ms = DEFAULT_CMD6_TIMEOUT_MS;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200816 bool is_part_switch = (set == EXT_CSD_CMD_SET_NORMAL) &&
817 (index == EXT_CSD_PART_CONF);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000818 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500819
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200820 if (mmc->gen_cmd6_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300821 timeout_ms = mmc->gen_cmd6_time * 10;
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200822
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200823 if (is_part_switch && mmc->part_switch_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300824 timeout_ms = mmc->part_switch_time * 10;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200825
Andy Fleming272cc702008-10-30 16:41:01 -0500826 cmd.cmdidx = MMC_CMD_SWITCH;
827 cmd.resp_type = MMC_RSP_R1b;
828 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000829 (index << 16) |
830 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500831
Sean Andersonda129172020-10-17 08:36:27 -0400832 ret = mmc_send_cmd_retry(mmc, &cmd, NULL, 3);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200833 if (ret)
834 return ret;
835
836 start = get_timer(0);
837
838 /* poll dat0 for rdy/buys status */
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300839 ret = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200840 if (ret && ret != -ENOSYS)
841 return ret;
842
843 /*
Kirill Kapranov44645f82021-10-09 23:49:59 +0300844 * In cases when neiter allowed to poll by using CMD13 nor we are
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200845 * capable of polling by using mmc_wait_dat0, then rely on waiting the
846 * stated timeout to be sufficient.
847 */
Kirill Kapranov44645f82021-10-09 23:49:59 +0300848 if (ret == -ENOSYS && !send_status) {
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300849 mdelay(timeout_ms);
Haibo Chenef5ab0d2020-09-22 18:11:42 +0800850 return 0;
851 }
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200852
Marek Vasut2349ecf2022-07-15 01:58:24 +0200853 if (!send_status)
854 return 0;
855
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200856 /* Finally wait until the card is ready or indicates a failure
857 * to switch. It doesn't hurt to use CMD13 here even if send_status
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300858 * is false, because by now (after 'timeout_ms' ms) the bus should be
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200859 * reliable.
860 */
861 do {
862 ret = mmc_send_status(mmc, &status);
863
864 if (!ret && (status & MMC_STATUS_SWITCH_ERROR)) {
865 pr_debug("switch failed %d/%d/0x%x !\n", set, index,
866 value);
867 return -EIO;
Maxime Riparda9003dc2016-11-04 16:18:08 +0100868 }
Stefan Bosch8e2b0af2021-01-23 13:37:41 +0100869 if (!ret && (status & MMC_STATUS_RDY_FOR_DATA) &&
870 (status & MMC_STATUS_CURR_STATE) == MMC_STATE_TRANS)
Marek Vasut68925502019-02-06 11:34:27 +0100871 return 0;
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200872 udelay(100);
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300873 } while (get_timer(start) < timeout_ms);
Marek Vasut68925502019-02-06 11:34:27 +0100874
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200875 return -ETIMEDOUT;
Andy Fleming272cc702008-10-30 16:41:01 -0500876}
877
Marek Vasut68925502019-02-06 11:34:27 +0100878int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
879{
880 return __mmc_switch(mmc, set, index, value, true);
881}
882
Heinrich Schuchardt0469d842020-03-30 07:24:19 +0200883int mmc_boot_wp(struct mmc *mmc)
884{
885 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, 1);
886}
887
Ying-Chun Liu (PaulLiu)19a29ff2022-04-25 21:59:02 +0800888int mmc_boot_wp_single_partition(struct mmc *mmc, int partition)
889{
890 u8 value;
891 int ret;
892
893 value = EXT_CSD_BOOT_WP_B_PWR_WP_EN;
894
895 if (partition == 0) {
896 value |= EXT_CSD_BOOT_WP_B_SEC_WP_SEL;
897 ret = mmc_switch(mmc,
898 EXT_CSD_CMD_SET_NORMAL,
899 EXT_CSD_BOOT_WP,
900 value);
901 } else if (partition == 1) {
902 value |= EXT_CSD_BOOT_WP_B_SEC_WP_SEL;
903 value |= EXT_CSD_BOOT_WP_B_PWR_WP_SEC_SEL;
904 ret = mmc_switch(mmc,
905 EXT_CSD_CMD_SET_NORMAL,
906 EXT_CSD_BOOT_WP,
907 value);
908 } else {
909 ret = mmc_boot_wp(mmc);
910 }
911
912 return ret;
913}
914
Marek Vasut62d77ce2018-04-15 00:37:11 +0200915#if !CONFIG_IS_ENABLED(MMC_TINY)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100916static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode,
917 bool hsdowngrade)
Andy Fleming272cc702008-10-30 16:41:01 -0500918{
Andy Fleming272cc702008-10-30 16:41:01 -0500919 int err;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200920 int speed_bits;
921
922 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
923
924 switch (mode) {
925 case MMC_HS:
926 case MMC_HS_52:
927 case MMC_DDR_52:
928 speed_bits = EXT_CSD_TIMING_HS;
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200929 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100930#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200931 case MMC_HS_200:
932 speed_bits = EXT_CSD_TIMING_HS200;
933 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100934#endif
Peng Fan3dd26262018-08-10 14:07:54 +0800935#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
936 case MMC_HS_400:
937 speed_bits = EXT_CSD_TIMING_HS400;
938 break;
939#endif
Peng Fan44acd492019-07-10 14:43:07 +0800940#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
941 case MMC_HS_400_ES:
942 speed_bits = EXT_CSD_TIMING_HS400;
943 break;
944#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200945 case MMC_LEGACY:
946 speed_bits = EXT_CSD_TIMING_LEGACY;
947 break;
948 default:
949 return -EINVAL;
950 }
Marek Vasut68925502019-02-06 11:34:27 +0100951
952 err = __mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
953 speed_bits, !hsdowngrade);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200954 if (err)
955 return err;
956
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100957#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
958 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
959 /*
960 * In case the eMMC is in HS200/HS400 mode and we are downgrading
961 * to HS mode, the card clock are still running much faster than
962 * the supported HS mode clock, so we can not reliably read out
963 * Extended CSD. Reconfigure the controller to run at HS mode.
964 */
965 if (hsdowngrade) {
966 mmc_select_mode(mmc, MMC_HS);
967 mmc_set_clock(mmc, mmc_mode2freq(mmc, MMC_HS), false);
968 }
969#endif
970
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200971 if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
972 /* Now check to see that it worked */
973 err = mmc_send_ext_csd(mmc, test_csd);
974 if (err)
975 return err;
976
977 /* No high-speed support */
978 if (!test_csd[EXT_CSD_HS_TIMING])
979 return -ENOTSUPP;
980 }
981
982 return 0;
983}
984
985static int mmc_get_capabilities(struct mmc *mmc)
986{
987 u8 *ext_csd = mmc->ext_csd;
988 char cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500989
Jean-Jacques Hiblot00e446f2017-11-30 17:43:56 +0100990 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -0500991
Thomas Choud52ebf12010-12-24 13:12:21 +0000992 if (mmc_host_is_spi(mmc))
993 return 0;
994
Andy Fleming272cc702008-10-30 16:41:01 -0500995 /* Only version 4 supports high-speed */
996 if (mmc->version < MMC_VERSION_4)
997 return 0;
998
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200999 if (!ext_csd) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001000 pr_err("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001001 return -ENOTSUPP;
1002 }
1003
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -06001004 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
1005
Peng Fan3dd26262018-08-10 14:07:54 +08001006 cardtype = ext_csd[EXT_CSD_CARD_TYPE];
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001007 mmc->cardtype = cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -05001008
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001009#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001010 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1011 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
1012 mmc->card_caps |= MMC_MODE_HS200;
1013 }
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001014#endif
Peng Fan44acd492019-07-10 14:43:07 +08001015#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
1016 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Peng Fan3dd26262018-08-10 14:07:54 +08001017 if (cardtype & (EXT_CSD_CARD_TYPE_HS400_1_2V |
1018 EXT_CSD_CARD_TYPE_HS400_1_8V)) {
1019 mmc->card_caps |= MMC_MODE_HS400;
1020 }
1021#endif
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001022 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001023 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001024 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001025 mmc->card_caps |= MMC_MODE_HS_52MHz;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001026 }
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001027 if (cardtype & EXT_CSD_CARD_TYPE_26)
1028 mmc->card_caps |= MMC_MODE_HS;
Andy Fleming272cc702008-10-30 16:41:01 -05001029
Peng Fan44acd492019-07-10 14:43:07 +08001030#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1031 if (ext_csd[EXT_CSD_STROBE_SUPPORT] &&
1032 (mmc->card_caps & MMC_MODE_HS400)) {
1033 mmc->card_caps |= MMC_MODE_HS400_ES;
1034 }
1035#endif
1036
Andy Fleming272cc702008-10-30 16:41:01 -05001037 return 0;
1038}
Marek Vasut62d77ce2018-04-15 00:37:11 +02001039#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001040
Stephen Warrenf866a462013-06-11 15:14:01 -06001041static int mmc_set_capacity(struct mmc *mmc, int part_num)
1042{
1043 switch (part_num) {
1044 case 0:
1045 mmc->capacity = mmc->capacity_user;
1046 break;
1047 case 1:
1048 case 2:
1049 mmc->capacity = mmc->capacity_boot;
1050 break;
1051 case 3:
1052 mmc->capacity = mmc->capacity_rpmb;
1053 break;
1054 case 4:
1055 case 5:
1056 case 6:
1057 case 7:
1058 mmc->capacity = mmc->capacity_gp[part_num - 4];
1059 break;
1060 default:
1061 return -1;
1062 }
1063
Simon Glassc40fdca2016-05-01 13:52:35 -06001064 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -06001065
1066 return 0;
1067}
1068
Simon Glass7dba0b92016-06-12 23:30:15 -06001069int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +00001070{
Stephen Warrenf866a462013-06-11 15:14:01 -06001071 int ret;
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +02001072 int retry = 3;
Lei Wenbc897b12011-05-02 16:26:26 +00001073
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +02001074 do {
1075 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1076 EXT_CSD_PART_CONF,
1077 (mmc->part_config & ~PART_ACCESS_MASK)
1078 | (part_num & PART_ACCESS_MASK));
1079 } while (ret && retry--);
Stephen Warrenf866a462013-06-11 15:14:01 -06001080
Peter Bigot6dc93e72014-09-02 18:31:23 -05001081 /*
1082 * Set the capacity if the switch succeeded or was intended
1083 * to return to representing the raw device.
1084 */
Stephen Warren873cc1d2015-12-07 11:38:49 -07001085 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -05001086 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -06001087 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -07001088 }
Peter Bigot6dc93e72014-09-02 18:31:23 -05001089
1090 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +00001091}
1092
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001093#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001094int mmc_hwpart_config(struct mmc *mmc,
1095 const struct mmc_hwpart_conf *conf,
1096 enum mmc_hwpart_conf_mode mode)
1097{
1098 u8 part_attrs = 0;
1099 u32 enh_size_mult;
1100 u32 enh_start_addr;
1101 u32 gp_size_mult[4];
1102 u32 max_enh_size_mult;
1103 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001104 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001105 int i, pidx, err;
1106 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1107
1108 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
1109 return -EINVAL;
1110
1111 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001112 pr_err("eMMC >= 4.4 required for enhanced user data area\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001113 return -EMEDIUMTYPE;
1114 }
1115
1116 if (!(mmc->part_support & PART_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001117 pr_err("Card does not support partitioning\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001118 return -EMEDIUMTYPE;
1119 }
1120
1121 if (!mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001122 pr_err("Card does not define HC WP group size\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001123 return -EMEDIUMTYPE;
1124 }
1125
1126 /* check partition alignment and total enhanced size */
1127 if (conf->user.enh_size) {
1128 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
1129 conf->user.enh_start % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001130 pr_err("User data enhanced area not HC WP group "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001131 "size aligned\n");
1132 return -EINVAL;
1133 }
1134 part_attrs |= EXT_CSD_ENH_USR;
1135 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
1136 if (mmc->high_capacity) {
1137 enh_start_addr = conf->user.enh_start;
1138 } else {
1139 enh_start_addr = (conf->user.enh_start << 9);
1140 }
1141 } else {
1142 enh_size_mult = 0;
1143 enh_start_addr = 0;
1144 }
1145 tot_enh_size_mult += enh_size_mult;
1146
1147 for (pidx = 0; pidx < 4; pidx++) {
1148 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001149 pr_err("GP%i partition not HC WP group size "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001150 "aligned\n", pidx+1);
1151 return -EINVAL;
1152 }
1153 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
1154 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
1155 part_attrs |= EXT_CSD_ENH_GP(pidx);
1156 tot_enh_size_mult += gp_size_mult[pidx];
1157 }
1158 }
1159
1160 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001161 pr_err("Card does not support enhanced attribute\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001162 return -EMEDIUMTYPE;
1163 }
1164
1165 err = mmc_send_ext_csd(mmc, ext_csd);
1166 if (err)
1167 return err;
1168
1169 max_enh_size_mult =
1170 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
1171 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
1172 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
1173 if (tot_enh_size_mult > max_enh_size_mult) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001174 pr_err("Total enhanced size exceeds maximum (%u > %u)\n",
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001175 tot_enh_size_mult, max_enh_size_mult);
1176 return -EMEDIUMTYPE;
1177 }
1178
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001179 /* The default value of EXT_CSD_WR_REL_SET is device
1180 * dependent, the values can only be changed if the
1181 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
1182 * changed only once and before partitioning is completed. */
1183 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1184 if (conf->user.wr_rel_change) {
1185 if (conf->user.wr_rel_set)
1186 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
1187 else
1188 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
1189 }
1190 for (pidx = 0; pidx < 4; pidx++) {
1191 if (conf->gp_part[pidx].wr_rel_change) {
1192 if (conf->gp_part[pidx].wr_rel_set)
1193 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
1194 else
1195 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
1196 }
1197 }
1198
1199 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
1200 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
1201 puts("Card does not support host controlled partition write "
1202 "reliability settings\n");
1203 return -EMEDIUMTYPE;
1204 }
1205
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001206 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
1207 EXT_CSD_PARTITION_SETTING_COMPLETED) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001208 pr_err("Card already partitioned\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001209 return -EPERM;
1210 }
1211
1212 if (mode == MMC_HWPART_CONF_CHECK)
1213 return 0;
1214
1215 /* Partitioning requires high-capacity size definitions */
1216 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
1217 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1218 EXT_CSD_ERASE_GROUP_DEF, 1);
1219
1220 if (err)
1221 return err;
1222
1223 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1224
Jaehoon Chung4af66592020-01-17 15:06:54 +09001225#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001226 /* update erase group size to be high-capacity */
1227 mmc->erase_grp_size =
1228 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jaehoon Chung4af66592020-01-17 15:06:54 +09001229#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001230
1231 }
1232
1233 /* all OK, write the configuration */
1234 for (i = 0; i < 4; i++) {
1235 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1236 EXT_CSD_ENH_START_ADDR+i,
1237 (enh_start_addr >> (i*8)) & 0xFF);
1238 if (err)
1239 return err;
1240 }
1241 for (i = 0; i < 3; i++) {
1242 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1243 EXT_CSD_ENH_SIZE_MULT+i,
1244 (enh_size_mult >> (i*8)) & 0xFF);
1245 if (err)
1246 return err;
1247 }
1248 for (pidx = 0; pidx < 4; pidx++) {
1249 for (i = 0; i < 3; i++) {
1250 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1251 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1252 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1253 if (err)
1254 return err;
1255 }
1256 }
1257 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1258 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1259 if (err)
1260 return err;
1261
1262 if (mode == MMC_HWPART_CONF_SET)
1263 return 0;
1264
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001265 /* The WR_REL_SET is a write-once register but shall be
1266 * written before setting PART_SETTING_COMPLETED. As it is
1267 * write-once we can only write it when completing the
1268 * partitioning. */
1269 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1270 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1271 EXT_CSD_WR_REL_SET, wr_rel_set);
1272 if (err)
1273 return err;
1274 }
1275
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001276 /* Setting PART_SETTING_COMPLETED confirms the partition
1277 * configuration but it only becomes effective after power
1278 * cycle, so we do not adjust the partition related settings
1279 * in the mmc struct. */
1280
1281 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1282 EXT_CSD_PARTITION_SETTING,
1283 EXT_CSD_PARTITION_SETTING_COMPLETED);
1284 if (err)
1285 return err;
1286
1287 return 0;
1288}
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001289#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001290
Simon Glasse7881d82017-07-29 11:35:31 -06001291#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +00001292int mmc_getcd(struct mmc *mmc)
1293{
1294 int cd;
1295
1296 cd = board_mmc_getcd(mmc);
1297
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001298 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001299 if (mmc->cfg->ops->getcd)
1300 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001301 else
1302 cd = 1;
1303 }
Thierry Reding48972d92012-01-02 01:15:37 +00001304
1305 return cd;
1306}
Simon Glass8ca51e52016-06-12 23:30:22 -06001307#endif
Thierry Reding48972d92012-01-02 01:15:37 +00001308
Marek Vasut62d77ce2018-04-15 00:37:11 +02001309#if !CONFIG_IS_ENABLED(MMC_TINY)
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001310static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -05001311{
1312 struct mmc_cmd cmd;
1313 struct mmc_data data;
1314
1315 /* Switch the frequency */
1316 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1317 cmd.resp_type = MMC_RSP_R1;
1318 cmd.cmdarg = (mode << 31) | 0xffffff;
1319 cmd.cmdarg &= ~(0xf << (group * 4));
1320 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -05001321
1322 data.dest = (char *)resp;
1323 data.blocksize = 64;
1324 data.blocks = 1;
1325 data.flags = MMC_DATA_READ;
1326
1327 return mmc_send_cmd(mmc, &cmd, &data);
1328}
1329
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001330static int sd_get_capabilities(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001331{
1332 int err;
1333 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +05301334 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1335 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -05001336 struct mmc_data data;
1337 int timeout;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001338#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001339 u32 sd3_bus_mode;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001340#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001341
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301342 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05001343
Thomas Choud52ebf12010-12-24 13:12:21 +00001344 if (mmc_host_is_spi(mmc))
1345 return 0;
1346
Andy Fleming272cc702008-10-30 16:41:01 -05001347 /* Read the SCR to find out if this card supports higher speeds */
1348 cmd.cmdidx = MMC_CMD_APP_CMD;
1349 cmd.resp_type = MMC_RSP_R1;
1350 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001351
1352 err = mmc_send_cmd(mmc, &cmd, NULL);
1353
1354 if (err)
1355 return err;
1356
1357 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1358 cmd.resp_type = MMC_RSP_R1;
1359 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001360
Anton staaff781dd32011-10-03 13:54:59 +00001361 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -05001362 data.blocksize = 8;
1363 data.blocks = 1;
1364 data.flags = MMC_DATA_READ;
1365
Sean Andersonda129172020-10-17 08:36:27 -04001366 err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);
Andy Fleming272cc702008-10-30 16:41:01 -05001367
Sean Andersonda129172020-10-17 08:36:27 -04001368 if (err)
Andy Fleming272cc702008-10-30 16:41:01 -05001369 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001370
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001371 mmc->scr[0] = __be32_to_cpu(scr[0]);
1372 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -05001373
1374 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -07001375 case 0:
1376 mmc->version = SD_VERSION_1_0;
1377 break;
1378 case 1:
1379 mmc->version = SD_VERSION_1_10;
1380 break;
1381 case 2:
1382 mmc->version = SD_VERSION_2;
1383 if ((mmc->scr[0] >> 15) & 0x1)
1384 mmc->version = SD_VERSION_3;
1385 break;
1386 default:
1387 mmc->version = SD_VERSION_1_0;
1388 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001389 }
1390
Alagu Sankarb44c7082010-05-12 15:08:24 +05301391 if (mmc->scr[0] & SD_DATA_4BIT)
1392 mmc->card_caps |= MMC_MODE_4BIT;
1393
Andy Fleming272cc702008-10-30 16:41:01 -05001394 /* Version 1.0 doesn't support switching */
1395 if (mmc->version == SD_VERSION_1_0)
1396 return 0;
1397
1398 timeout = 4;
1399 while (timeout--) {
1400 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +00001401 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001402
1403 if (err)
1404 return err;
1405
1406 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001407 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -05001408 break;
1409 }
1410
Andy Fleming272cc702008-10-30 16:41:01 -05001411 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001412 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1413 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Fleming272cc702008-10-30 16:41:01 -05001414
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001415#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001416 /* Version before 3.0 don't support UHS modes */
1417 if (mmc->version < SD_VERSION_3)
1418 return 0;
1419
1420 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1421 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1422 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1423 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1424 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1425 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1426 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1427 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1428 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1429 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1430 mmc->card_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001431#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001432
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001433 return 0;
1434}
1435
1436static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1437{
1438 int err;
1439
1440 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001441 int speed;
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001442
Marek Vasutcf345762018-11-18 03:25:08 +01001443 /* SD version 1.00 and 1.01 does not support CMD 6 */
1444 if (mmc->version == SD_VERSION_1_0)
1445 return 0;
1446
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001447 switch (mode) {
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301448 case MMC_LEGACY:
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001449 speed = UHS_SDR12_BUS_SPEED;
1450 break;
1451 case SD_HS:
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001452 speed = HIGH_SPEED_BUS_SPEED;
1453 break;
1454#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1455 case UHS_SDR12:
1456 speed = UHS_SDR12_BUS_SPEED;
1457 break;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001458 case UHS_SDR25:
1459 speed = UHS_SDR25_BUS_SPEED;
1460 break;
1461 case UHS_SDR50:
1462 speed = UHS_SDR50_BUS_SPEED;
1463 break;
1464 case UHS_DDR50:
1465 speed = UHS_DDR50_BUS_SPEED;
1466 break;
1467 case UHS_SDR104:
1468 speed = UHS_SDR104_BUS_SPEED;
1469 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001470#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001471 default:
1472 return -EINVAL;
1473 }
1474
1475 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001476 if (err)
1477 return err;
1478
Jean-Jacques Hiblota0276f32018-02-09 12:09:27 +01001479 if (((__be32_to_cpu(switch_status[4]) >> 24) & 0xF) != speed)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001480 return -ENOTSUPP;
1481
1482 return 0;
1483}
1484
Marek Vasutec360e62018-04-15 00:36:45 +02001485static int sd_select_bus_width(struct mmc *mmc, int w)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001486{
1487 int err;
1488 struct mmc_cmd cmd;
1489
1490 if ((w != 4) && (w != 1))
1491 return -EINVAL;
1492
1493 cmd.cmdidx = MMC_CMD_APP_CMD;
1494 cmd.resp_type = MMC_RSP_R1;
1495 cmd.cmdarg = mmc->rca << 16;
1496
1497 err = mmc_send_cmd(mmc, &cmd, NULL);
1498 if (err)
1499 return err;
1500
1501 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1502 cmd.resp_type = MMC_RSP_R1;
1503 if (w == 4)
1504 cmd.cmdarg = 2;
1505 else if (w == 1)
1506 cmd.cmdarg = 0;
1507 err = mmc_send_cmd(mmc, &cmd, NULL);
1508 if (err)
1509 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001510
1511 return 0;
1512}
Marek Vasut62d77ce2018-04-15 00:37:11 +02001513#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001514
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001515#if CONFIG_IS_ENABLED(MMC_WRITE)
Peng Fan3697e592016-09-01 11:13:38 +08001516static int sd_read_ssr(struct mmc *mmc)
1517{
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001518 static const unsigned int sd_au_size[] = {
1519 0, SZ_16K / 512, SZ_32K / 512,
1520 SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
1521 SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
1522 SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
1523 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512,
1524 SZ_64M / 512,
1525 };
Peng Fan3697e592016-09-01 11:13:38 +08001526 int err, i;
1527 struct mmc_cmd cmd;
1528 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1529 struct mmc_data data;
Peng Fan3697e592016-09-01 11:13:38 +08001530 unsigned int au, eo, et, es;
1531
1532 cmd.cmdidx = MMC_CMD_APP_CMD;
1533 cmd.resp_type = MMC_RSP_R1;
1534 cmd.cmdarg = mmc->rca << 16;
1535
Sean Andersonda129172020-10-17 08:36:27 -04001536 err = mmc_send_cmd_quirks(mmc, &cmd, NULL, MMC_QUIRK_RETRY_APP_CMD, 4);
Peng Fan3697e592016-09-01 11:13:38 +08001537 if (err)
1538 return err;
1539
1540 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1541 cmd.resp_type = MMC_RSP_R1;
1542 cmd.cmdarg = 0;
1543
Peng Fan3697e592016-09-01 11:13:38 +08001544 data.dest = (char *)ssr;
1545 data.blocksize = 64;
1546 data.blocks = 1;
1547 data.flags = MMC_DATA_READ;
1548
Sean Andersonda129172020-10-17 08:36:27 -04001549 err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);
1550 if (err)
Peng Fan3697e592016-09-01 11:13:38 +08001551 return err;
Peng Fan3697e592016-09-01 11:13:38 +08001552
1553 for (i = 0; i < 16; i++)
1554 ssr[i] = be32_to_cpu(ssr[i]);
1555
1556 au = (ssr[2] >> 12) & 0xF;
1557 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1558 mmc->ssr.au = sd_au_size[au];
1559 es = (ssr[3] >> 24) & 0xFF;
1560 es |= (ssr[2] & 0xFF) << 8;
1561 et = (ssr[3] >> 18) & 0x3F;
1562 if (es && et) {
1563 eo = (ssr[3] >> 16) & 0x3;
1564 mmc->ssr.erase_timeout = (et * 1000) / es;
1565 mmc->ssr.erase_offset = eo * 1000;
1566 }
1567 } else {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001568 pr_debug("Invalid Allocation Unit Size.\n");
Peng Fan3697e592016-09-01 11:13:38 +08001569 }
1570
1571 return 0;
1572}
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001573#endif
Heinrich Schuchardtf9a86fb2024-01-04 04:49:42 +01001574/*
1575 * TRAN_SPEED bits 0:2 encode the frequency unit:
1576 * 0 = 100KHz, 1 = 1MHz, 2 = 10MHz, 3 = 100MHz, values 4 - 7 are reserved.
1577 * The values in fbase[] are divided by 10 to avoid floats in multiplier[].
1578 */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001579static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001580 10000,
1581 100000,
1582 1000000,
1583 10000000,
Heinrich Schuchardtf9a86fb2024-01-04 04:49:42 +01001584 0, /* reserved */
1585 0, /* reserved */
1586 0, /* reserved */
1587 0, /* reserved */
Andy Fleming272cc702008-10-30 16:41:01 -05001588};
1589
1590/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1591 * to platforms without floating point.
1592 */
Simon Glass61fe0762016-05-14 14:02:57 -06001593static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001594 0, /* reserved */
1595 10,
1596 12,
1597 13,
1598 15,
1599 20,
1600 25,
1601 30,
1602 35,
1603 40,
1604 45,
1605 50,
1606 55,
1607 60,
1608 70,
1609 80,
1610};
1611
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001612static inline int bus_width(uint cap)
1613{
1614 if (cap == MMC_MODE_8BIT)
1615 return 8;
1616 if (cap == MMC_MODE_4BIT)
1617 return 4;
1618 if (cap == MMC_MODE_1BIT)
1619 return 1;
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001620 pr_warn("invalid bus witdh capability 0x%x\n", cap);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001621 return 0;
1622}
1623
Simon Glasse7881d82017-07-29 11:35:31 -06001624#if !CONFIG_IS_ENABLED(DM_MMC)
Tom Rini03de3052024-05-20 13:35:03 -06001625#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001626static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1627{
1628 return -ENOTSUPP;
1629}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001630#endif
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001631
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001632static int mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001633{
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001634 int ret = 0;
1635
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001636 if (mmc->cfg->ops->set_ios)
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001637 ret = mmc->cfg->ops->set_ios(mmc);
1638
1639 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001640}
Yann Gautier3602a562019-09-19 17:56:12 +02001641
1642static int mmc_host_power_cycle(struct mmc *mmc)
1643{
1644 int ret = 0;
1645
1646 if (mmc->cfg->ops->host_power_cycle)
1647 ret = mmc->cfg->ops->host_power_cycle(mmc);
1648
1649 return ret;
1650}
Simon Glass8ca51e52016-06-12 23:30:22 -06001651#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001652
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001653int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
Andy Fleming272cc702008-10-30 16:41:01 -05001654{
Jaehoon Chungc0fafe62018-01-23 14:04:30 +09001655 if (!disable) {
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001656 if (clock > mmc->cfg->f_max)
1657 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001658
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001659 if (clock < mmc->cfg->f_min)
1660 clock = mmc->cfg->f_min;
1661 }
Andy Fleming272cc702008-10-30 16:41:01 -05001662
1663 mmc->clock = clock;
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001664 mmc->clk_disable = disable;
Andy Fleming272cc702008-10-30 16:41:01 -05001665
Jaehoon Chungd2faadb2018-01-26 19:25:30 +09001666 debug("clock is %s (%dHz)\n", disable ? "disabled" : "enabled", clock);
1667
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001668 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001669}
1670
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001671static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001672{
1673 mmc->bus_width = width;
1674
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001675 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001676}
1677
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001678#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1679/*
1680 * helper function to display the capabilities in a human
1681 * friendly manner. The capabilities include bus width and
1682 * supported modes.
1683 */
1684void mmc_dump_capabilities(const char *text, uint caps)
1685{
1686 enum bus_mode mode;
1687
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001688 pr_debug("%s: widths [", text);
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001689 if (caps & MMC_MODE_8BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001690 pr_debug("8, ");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001691 if (caps & MMC_MODE_4BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001692 pr_debug("4, ");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001693 if (caps & MMC_MODE_1BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001694 pr_debug("1, ");
1695 pr_debug("\b\b] modes [");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001696 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1697 if (MMC_CAP(mode) & caps)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001698 pr_debug("%s, ", mmc_mode_name(mode));
1699 pr_debug("\b\b]\n");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001700}
1701#endif
1702
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001703struct mode_width_tuning {
1704 enum bus_mode mode;
1705 uint widths;
Tom Rini03de3052024-05-20 13:35:03 -06001706#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001707 uint tuning;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001708#endif
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001709};
1710
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001711#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001712int mmc_voltage_to_mv(enum mmc_voltage voltage)
1713{
1714 switch (voltage) {
1715 case MMC_SIGNAL_VOLTAGE_000: return 0;
1716 case MMC_SIGNAL_VOLTAGE_330: return 3300;
1717 case MMC_SIGNAL_VOLTAGE_180: return 1800;
1718 case MMC_SIGNAL_VOLTAGE_120: return 1200;
1719 }
1720 return -EINVAL;
1721}
1722
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001723static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1724{
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001725 int err;
1726
1727 if (mmc->signal_voltage == signal_voltage)
1728 return 0;
1729
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001730 mmc->signal_voltage = signal_voltage;
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001731 err = mmc_set_ios(mmc);
1732 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001733 pr_debug("unable to set voltage (err %d)\n", err);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001734
1735 return err;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001736}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001737#else
1738static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1739{
1740 return 0;
1741}
1742#endif
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001743
Marek Vasut62d77ce2018-04-15 00:37:11 +02001744#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001745static const struct mode_width_tuning sd_modes_by_pref[] = {
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001746#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Tom Rini03de3052024-05-20 13:35:03 -06001747#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001748 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001749 .mode = UHS_SDR104,
1750 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1751 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1752 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001753#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001754 {
1755 .mode = UHS_SDR50,
1756 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1757 },
1758 {
1759 .mode = UHS_DDR50,
1760 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1761 },
1762 {
1763 .mode = UHS_SDR25,
1764 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1765 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001766#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001767 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001768 .mode = SD_HS,
1769 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1770 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001771#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001772 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001773 .mode = UHS_SDR12,
1774 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1775 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001776#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001777 {
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301778 .mode = MMC_LEGACY,
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001779 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1780 }
1781};
1782
1783#define for_each_sd_mode_by_pref(caps, mwt) \
1784 for (mwt = sd_modes_by_pref;\
1785 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1786 mwt++) \
1787 if (caps & MMC_CAP(mwt->mode))
1788
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001789static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001790{
1791 int err;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001792 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1793 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001794#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001795 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001796#else
1797 bool uhs_en = false;
1798#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001799 uint caps;
1800
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001801#ifdef DEBUG
1802 mmc_dump_capabilities("sd card", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001803 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001804#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001805
Anup Patelf49ff792019-07-08 04:10:43 +00001806 if (mmc_host_is_spi(mmc)) {
1807 mmc_set_bus_width(mmc, 1);
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301808 mmc_select_mode(mmc, MMC_LEGACY);
Anup Patelf49ff792019-07-08 04:10:43 +00001809 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
Pragnesh Patel810bc132020-06-29 15:17:26 +05301810#if CONFIG_IS_ENABLED(MMC_WRITE)
1811 err = sd_read_ssr(mmc);
1812 if (err)
1813 pr_warn("unable to read ssr\n");
1814#endif
Anup Patelf49ff792019-07-08 04:10:43 +00001815 return 0;
1816 }
1817
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001818 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001819 caps = card_caps & mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001820
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001821 if (!uhs_en)
1822 caps &= ~UHS_CAPS;
1823
1824 for_each_sd_mode_by_pref(caps, mwt) {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001825 uint *w;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001826
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001827 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001828 if (*w & caps & mwt->widths) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001829 pr_debug("trying mode %s width %d (at %d MHz)\n",
1830 mmc_mode_name(mwt->mode),
1831 bus_width(*w),
1832 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001833
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001834 /* configure the bus width (card + host) */
1835 err = sd_select_bus_width(mmc, bus_width(*w));
1836 if (err)
1837 goto error;
1838 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001839
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001840 /* configure the bus mode (card) */
1841 err = sd_set_card_speed(mmc, mwt->mode);
1842 if (err)
1843 goto error;
1844
1845 /* configure the bus mode (host) */
1846 mmc_select_mode(mmc, mwt->mode);
Jaehoon Chung65117182018-01-26 19:25:29 +09001847 mmc_set_clock(mmc, mmc->tran_speed,
1848 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001849
Tom Rini03de3052024-05-20 13:35:03 -06001850#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001851 /* execute tuning if needed */
1852 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1853 err = mmc_execute_tuning(mmc,
1854 mwt->tuning);
1855 if (err) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001856 pr_debug("tuning failed\n");
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001857 goto error;
1858 }
1859 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001860#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001861
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001862#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001863 err = sd_read_ssr(mmc);
Peng Fan0a4c2b02018-03-05 16:20:40 +08001864 if (err)
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001865 pr_warn("unable to read ssr\n");
1866#endif
1867 if (!err)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001868 return 0;
1869
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001870error:
1871 /* revert to a safer bus speed */
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301872 mmc_select_mode(mmc, MMC_LEGACY);
Jaehoon Chung65117182018-01-26 19:25:29 +09001873 mmc_set_clock(mmc, mmc->tran_speed,
1874 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001875 }
1876 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001877 }
1878
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001879 pr_err("unable to select a mode\n");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001880 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001881}
1882
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001883/*
1884 * read the compare the part of ext csd that is constant.
1885 * This can be used to check that the transfer is working
1886 * as expected.
1887 */
1888static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1889{
1890 int err;
1891 const u8 *ext_csd = mmc->ext_csd;
1892 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1893
Jean-Jacques Hiblot1de06b92017-11-30 17:43:58 +01001894 if (mmc->version < MMC_VERSION_4)
1895 return 0;
1896
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001897 err = mmc_send_ext_csd(mmc, test_csd);
1898 if (err)
1899 return err;
1900
1901 /* Only compare read only fields */
1902 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1903 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1904 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1905 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1906 ext_csd[EXT_CSD_REV]
1907 == test_csd[EXT_CSD_REV] &&
1908 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1909 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1910 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1911 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1912 return 0;
1913
1914 return -EBADMSG;
1915}
1916
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001917#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001918static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1919 uint32_t allowed_mask)
1920{
1921 u32 card_mask = 0;
1922
1923 switch (mode) {
Peng Fan44acd492019-07-10 14:43:07 +08001924 case MMC_HS_400_ES:
Peng Fan3dd26262018-08-10 14:07:54 +08001925 case MMC_HS_400:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001926 case MMC_HS_200:
Peng Fan3dd26262018-08-10 14:07:54 +08001927 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_8V |
1928 EXT_CSD_CARD_TYPE_HS400_1_8V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001929 card_mask |= MMC_SIGNAL_VOLTAGE_180;
Peng Fan3dd26262018-08-10 14:07:54 +08001930 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1931 EXT_CSD_CARD_TYPE_HS400_1_2V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001932 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1933 break;
1934 case MMC_DDR_52:
1935 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
1936 card_mask |= MMC_SIGNAL_VOLTAGE_330 |
1937 MMC_SIGNAL_VOLTAGE_180;
1938 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
1939 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1940 break;
1941 default:
1942 card_mask |= MMC_SIGNAL_VOLTAGE_330;
1943 break;
1944 }
1945
1946 while (card_mask & allowed_mask) {
1947 enum mmc_voltage best_match;
1948
1949 best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
1950 if (!mmc_set_signal_voltage(mmc, best_match))
1951 return 0;
1952
1953 allowed_mask &= ~best_match;
1954 }
1955
1956 return -ENOTSUPP;
1957}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001958#else
1959static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1960 uint32_t allowed_mask)
1961{
1962 return 0;
1963}
1964#endif
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001965
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001966static const struct mode_width_tuning mmc_modes_by_pref[] = {
Peng Fan44acd492019-07-10 14:43:07 +08001967#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1968 {
1969 .mode = MMC_HS_400_ES,
1970 .widths = MMC_MODE_8BIT,
1971 },
1972#endif
Peng Fan3dd26262018-08-10 14:07:54 +08001973#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1974 {
1975 .mode = MMC_HS_400,
1976 .widths = MMC_MODE_8BIT,
1977 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
1978 },
1979#endif
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001980#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001981 {
1982 .mode = MMC_HS_200,
1983 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001984 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001985 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001986#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001987 {
1988 .mode = MMC_DDR_52,
1989 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1990 },
1991 {
1992 .mode = MMC_HS_52,
1993 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1994 },
1995 {
1996 .mode = MMC_HS,
1997 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1998 },
1999 {
2000 .mode = MMC_LEGACY,
2001 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
2002 }
2003};
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002004
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002005#define for_each_mmc_mode_by_pref(caps, mwt) \
2006 for (mwt = mmc_modes_by_pref;\
2007 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
2008 mwt++) \
2009 if (caps & MMC_CAP(mwt->mode))
2010
2011static const struct ext_csd_bus_width {
2012 uint cap;
2013 bool is_ddr;
2014 uint ext_csd_bits;
2015} ext_csd_bus_width[] = {
2016 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
2017 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
2018 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
2019 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
2020 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
2021};
2022
Peng Fan3dd26262018-08-10 14:07:54 +08002023#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2024static int mmc_select_hs400(struct mmc *mmc)
2025{
2026 int err;
2027
2028 /* Set timing to HS200 for tuning */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002029 err = mmc_set_card_speed(mmc, MMC_HS_200, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002030 if (err)
2031 return err;
2032
2033 /* configure the bus mode (host) */
2034 mmc_select_mode(mmc, MMC_HS_200);
2035 mmc_set_clock(mmc, mmc->tran_speed, false);
2036
2037 /* execute tuning if needed */
Marek Vasutd1343522024-02-24 23:32:09 +01002038 mmc->hs400_tuning = true;
Peng Fan3dd26262018-08-10 14:07:54 +08002039 err = mmc_execute_tuning(mmc, MMC_CMD_SEND_TUNING_BLOCK_HS200);
Marek Vasutd1343522024-02-24 23:32:09 +01002040 mmc->hs400_tuning = false;
Peng Fan3dd26262018-08-10 14:07:54 +08002041 if (err) {
2042 debug("tuning failed\n");
2043 return err;
2044 }
2045
2046 /* Set back to HS */
BOUGH CHEN5cf12032019-03-26 06:24:17 +00002047 mmc_set_card_speed(mmc, MMC_HS, true);
Peng Fan3dd26262018-08-10 14:07:54 +08002048
Yangbo Lud271e102020-09-01 16:58:04 +08002049 err = mmc_hs400_prepare_ddr(mmc);
2050 if (err)
2051 return err;
2052
Peng Fan3dd26262018-08-10 14:07:54 +08002053 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2054 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG);
2055 if (err)
2056 return err;
2057
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002058 err = mmc_set_card_speed(mmc, MMC_HS_400, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002059 if (err)
2060 return err;
2061
2062 mmc_select_mode(mmc, MMC_HS_400);
2063 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2064 if (err)
2065 return err;
2066
2067 return 0;
2068}
2069#else
2070static int mmc_select_hs400(struct mmc *mmc)
2071{
2072 return -ENOTSUPP;
2073}
2074#endif
2075
Peng Fan44acd492019-07-10 14:43:07 +08002076#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
2077#if !CONFIG_IS_ENABLED(DM_MMC)
2078static int mmc_set_enhanced_strobe(struct mmc *mmc)
2079{
2080 return -ENOTSUPP;
2081}
2082#endif
2083static int mmc_select_hs400es(struct mmc *mmc)
2084{
2085 int err;
2086
2087 err = mmc_set_card_speed(mmc, MMC_HS, true);
2088 if (err)
2089 return err;
2090
2091 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2092 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG |
2093 EXT_CSD_BUS_WIDTH_STROBE);
2094 if (err) {
2095 printf("switch to bus width for hs400 failed\n");
2096 return err;
2097 }
2098 /* TODO: driver strength */
2099 err = mmc_set_card_speed(mmc, MMC_HS_400_ES, false);
2100 if (err)
2101 return err;
2102
2103 mmc_select_mode(mmc, MMC_HS_400_ES);
2104 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2105 if (err)
2106 return err;
2107
2108 return mmc_set_enhanced_strobe(mmc);
2109}
2110#else
2111static int mmc_select_hs400es(struct mmc *mmc)
2112{
2113 return -ENOTSUPP;
2114}
2115#endif
2116
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002117#define for_each_supported_width(caps, ddr, ecbv) \
2118 for (ecbv = ext_csd_bus_width;\
2119 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
2120 ecbv++) \
2121 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
2122
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002123static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002124{
Jaehoon Chung52ff04a2020-12-04 06:36:00 +09002125 int err = 0;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002126 const struct mode_width_tuning *mwt;
2127 const struct ext_csd_bus_width *ecbw;
2128
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002129#ifdef DEBUG
2130 mmc_dump_capabilities("mmc", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002131 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002132#endif
2133
Anup Patelf49ff792019-07-08 04:10:43 +00002134 if (mmc_host_is_spi(mmc)) {
2135 mmc_set_bus_width(mmc, 1);
2136 mmc_select_mode(mmc, MMC_LEGACY);
2137 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
2138 return 0;
2139 }
2140
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002141 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002142 card_caps &= mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002143
2144 /* Only version 4 of MMC supports wider bus widths */
2145 if (mmc->version < MMC_VERSION_4)
2146 return 0;
2147
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002148 if (!mmc->ext_csd) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002149 pr_debug("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002150 return -ENOTSUPP;
2151 }
2152
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002153#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
Ye Lifb8c2e82021-08-17 17:20:34 +08002154 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
2155 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002156 /*
2157 * In case the eMMC is in HS200/HS400 mode, downgrade to HS mode
2158 * before doing anything else, since a transition from either of
2159 * the HS200/HS400 mode directly to legacy mode is not supported.
2160 */
2161 if (mmc->selected_mode == MMC_HS_200 ||
Ye Lifb8c2e82021-08-17 17:20:34 +08002162 mmc->selected_mode == MMC_HS_400 ||
2163 mmc->selected_mode == MMC_HS_400_ES)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002164 mmc_set_card_speed(mmc, MMC_HS, true);
2165 else
2166#endif
2167 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002168
2169 for_each_mmc_mode_by_pref(card_caps, mwt) {
2170 for_each_supported_width(card_caps & mwt->widths,
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002171 mmc_is_mode_ddr(mwt->mode), ecbw) {
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002172 enum mmc_voltage old_voltage;
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002173 pr_debug("trying mode %s width %d (at %d MHz)\n",
2174 mmc_mode_name(mwt->mode),
2175 bus_width(ecbw->cap),
2176 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002177 old_voltage = mmc->signal_voltage;
2178 err = mmc_set_lowest_voltage(mmc, mwt->mode,
2179 MMC_ALL_SIGNAL_VOLTAGE);
2180 if (err)
2181 continue;
2182
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002183 /* configure the bus width (card + host) */
2184 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2185 EXT_CSD_BUS_WIDTH,
2186 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
2187 if (err)
2188 goto error;
2189 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
2190
Peng Fan3dd26262018-08-10 14:07:54 +08002191 if (mwt->mode == MMC_HS_400) {
2192 err = mmc_select_hs400(mmc);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002193 if (err) {
Peng Fan3dd26262018-08-10 14:07:54 +08002194 printf("Select HS400 failed %d\n", err);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002195 goto error;
2196 }
Peng Fan44acd492019-07-10 14:43:07 +08002197 } else if (mwt->mode == MMC_HS_400_ES) {
2198 err = mmc_select_hs400es(mmc);
2199 if (err) {
2200 printf("Select HS400ES failed %d\n",
2201 err);
2202 goto error;
2203 }
Peng Fan3dd26262018-08-10 14:07:54 +08002204 } else {
2205 /* configure the bus speed (card) */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002206 err = mmc_set_card_speed(mmc, mwt->mode, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002207 if (err)
2208 goto error;
2209
2210 /*
2211 * configure the bus width AND the ddr mode
2212 * (card). The host side will be taken care
2213 * of in the next step
2214 */
2215 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
2216 err = mmc_switch(mmc,
2217 EXT_CSD_CMD_SET_NORMAL,
2218 EXT_CSD_BUS_WIDTH,
2219 ecbw->ext_csd_bits);
2220 if (err)
2221 goto error;
2222 }
2223
2224 /* configure the bus mode (host) */
2225 mmc_select_mode(mmc, mwt->mode);
2226 mmc_set_clock(mmc, mmc->tran_speed,
2227 MMC_CLK_ENABLE);
Tom Rini03de3052024-05-20 13:35:03 -06002228#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Peng Fan3dd26262018-08-10 14:07:54 +08002229
2230 /* execute tuning if needed */
2231 if (mwt->tuning) {
2232 err = mmc_execute_tuning(mmc,
2233 mwt->tuning);
2234 if (err) {
Jaehoon Chung58896452020-11-17 07:04:59 +09002235 pr_debug("tuning failed : %d\n", err);
Peng Fan3dd26262018-08-10 14:07:54 +08002236 goto error;
2237 }
2238 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01002239#endif
Peng Fan3dd26262018-08-10 14:07:54 +08002240 }
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002241
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002242 /* do a transfer to check the configuration */
2243 err = mmc_read_and_compare_ext_csd(mmc);
2244 if (!err)
2245 return 0;
2246error:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002247 mmc_set_signal_voltage(mmc, old_voltage);
Naoki Hayama64dbd862020-10-12 18:35:22 +09002248 /* if an error occurred, revert to a safer bus mode */
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002249 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2250 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
2251 mmc_select_mode(mmc, MMC_LEGACY);
Valentine Barshak50dee4f2023-06-10 13:22:33 +02002252 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002253 mmc_set_bus_width(mmc, 1);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002254 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002255 }
2256
Jaehoon Chung58896452020-11-17 07:04:59 +09002257 pr_err("unable to select a mode : %d\n", err);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002258
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002259 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002260}
Marek Vasut27ba82c2024-03-17 04:01:22 +01002261#else
2262static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
2263{
2264 return 0;
2265};
2266
2267static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
2268{
2269 return 0;
2270};
Marek Vasut62d77ce2018-04-15 00:37:11 +02002271#endif
2272
2273#if CONFIG_IS_ENABLED(MMC_TINY)
2274DEFINE_CACHE_ALIGN_BUFFER(u8, ext_csd_bkup, MMC_MAX_BLOCK_LEN);
2275#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002276
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002277static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002278{
2279 int err, i;
2280 u64 capacity;
2281 bool has_parts = false;
2282 bool part_completed;
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002283 static const u32 mmc_versions[] = {
2284 MMC_VERSION_4,
2285 MMC_VERSION_4_1,
2286 MMC_VERSION_4_2,
2287 MMC_VERSION_4_3,
Jean-Jacques Hiblotace1bed2018-02-09 12:09:28 +01002288 MMC_VERSION_4_4,
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002289 MMC_VERSION_4_41,
2290 MMC_VERSION_4_5,
2291 MMC_VERSION_5_0,
2292 MMC_VERSION_5_1
2293 };
2294
Marek Vasut62d77ce2018-04-15 00:37:11 +02002295#if CONFIG_IS_ENABLED(MMC_TINY)
2296 u8 *ext_csd = ext_csd_bkup;
2297
2298 if (IS_SD(mmc) || mmc->version < MMC_VERSION_4)
2299 return 0;
2300
2301 if (!mmc->ext_csd)
Sam Edwards229d6892023-05-18 13:47:07 -06002302 memset(ext_csd_bkup, 0, MMC_MAX_BLOCK_LEN);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002303
2304 err = mmc_send_ext_csd(mmc, ext_csd);
2305 if (err)
2306 goto error;
2307
2308 /* store the ext csd for future reference */
2309 if (!mmc->ext_csd)
2310 mmc->ext_csd = ext_csd;
2311#else
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002312 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002313
2314 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
2315 return 0;
2316
2317 /* check ext_csd version and capacity */
2318 err = mmc_send_ext_csd(mmc, ext_csd);
2319 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002320 goto error;
2321
2322 /* store the ext csd for future reference */
2323 if (!mmc->ext_csd)
2324 mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN);
2325 if (!mmc->ext_csd)
2326 return -ENOMEM;
2327 memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002328#endif
Alexander Kochetkov76584e32018-02-20 14:35:55 +03002329 if (ext_csd[EXT_CSD_REV] >= ARRAY_SIZE(mmc_versions))
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002330 return -EINVAL;
2331
2332 mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]];
2333
2334 if (mmc->version >= MMC_VERSION_4_2) {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002335 /*
2336 * According to the JEDEC Standard, the value of
2337 * ext_csd's capacity is valid if the value is more
2338 * than 2GB
2339 */
2340 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
2341 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
2342 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
2343 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2344 capacity *= MMC_MAX_BLOCK_LEN;
2345 if ((capacity >> 20) > 2 * 1024)
2346 mmc->capacity_user = capacity;
2347 }
2348
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +02002349 if (mmc->version >= MMC_VERSION_4_5)
2350 mmc->gen_cmd6_time = ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
2351
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002352 /* The partition data may be non-zero but it is only
2353 * effective if PARTITION_SETTING_COMPLETED is set in
2354 * EXT_CSD, so ignore any data if this bit is not set,
2355 * except for enabling the high-capacity group size
2356 * definition (see below).
2357 */
2358 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
2359 EXT_CSD_PARTITION_SETTING_COMPLETED);
2360
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +02002361 mmc->part_switch_time = ext_csd[EXT_CSD_PART_SWITCH_TIME];
2362 /* Some eMMC set the value too low so set a minimum */
2363 if (mmc->part_switch_time < MMC_MIN_PART_SWITCH_TIME && mmc->part_switch_time)
2364 mmc->part_switch_time = MMC_MIN_PART_SWITCH_TIME;
2365
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002366 /* store the partition info of emmc */
2367 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
2368 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
2369 ext_csd[EXT_CSD_BOOT_MULT])
2370 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
2371 if (part_completed &&
2372 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
2373 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
2374
2375 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
2376
2377 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
2378
2379 for (i = 0; i < 4; i++) {
2380 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
2381 uint mult = (ext_csd[idx + 2] << 16) +
2382 (ext_csd[idx + 1] << 8) + ext_csd[idx];
2383 if (mult)
2384 has_parts = true;
2385 if (!part_completed)
2386 continue;
2387 mmc->capacity_gp[i] = mult;
2388 mmc->capacity_gp[i] *=
2389 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2390 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2391 mmc->capacity_gp[i] <<= 19;
2392 }
2393
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002394#ifndef CONFIG_SPL_BUILD
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002395 if (part_completed) {
2396 mmc->enh_user_size =
2397 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
2398 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
2399 ext_csd[EXT_CSD_ENH_SIZE_MULT];
2400 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2401 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2402 mmc->enh_user_size <<= 19;
2403 mmc->enh_user_start =
2404 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
2405 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
2406 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
2407 ext_csd[EXT_CSD_ENH_START_ADDR];
2408 if (mmc->high_capacity)
2409 mmc->enh_user_start <<= 9;
2410 }
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002411#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002412
2413 /*
2414 * Host needs to enable ERASE_GRP_DEF bit if device is
2415 * partitioned. This bit will be lost every time after a reset
2416 * or power off. This will affect erase size.
2417 */
2418 if (part_completed)
2419 has_parts = true;
2420 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
2421 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
2422 has_parts = true;
2423 if (has_parts) {
2424 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2425 EXT_CSD_ERASE_GROUP_DEF, 1);
2426
2427 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002428 goto error;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002429
2430 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
2431 }
2432
2433 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002434#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002435 /* Read out group size from ext_csd */
2436 mmc->erase_grp_size =
2437 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002438#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002439 /*
2440 * if high capacity and partition setting completed
2441 * SEC_COUNT is valid even if it is smaller than 2 GiB
2442 * JEDEC Standard JESD84-B45, 6.2.4
2443 */
2444 if (mmc->high_capacity && part_completed) {
2445 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
2446 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
2447 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
2448 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
2449 capacity *= MMC_MAX_BLOCK_LEN;
2450 mmc->capacity_user = capacity;
2451 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002452 }
2453#if CONFIG_IS_ENABLED(MMC_WRITE)
2454 else {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002455 /* Calculate the group size from the csd value. */
2456 int erase_gsz, erase_gmul;
2457
2458 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
2459 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
2460 mmc->erase_grp_size = (erase_gsz + 1)
2461 * (erase_gmul + 1);
2462 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002463#endif
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002464#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002465 mmc->hc_wp_grp_size = 1024
2466 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
2467 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002468#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002469
2470 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
2471
Loic Poulaineeb739a2023-01-26 10:24:17 +01002472 mmc->can_trim =
2473 !!(ext_csd[EXT_CSD_SEC_FEATURE] & EXT_CSD_SEC_FEATURE_TRIM_EN);
2474
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002475 return 0;
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002476error:
2477 if (mmc->ext_csd) {
Marek Vasut62d77ce2018-04-15 00:37:11 +02002478#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002479 free(mmc->ext_csd);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002480#endif
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002481 mmc->ext_csd = NULL;
2482 }
2483 return err;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002484}
2485
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002486static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002487{
Stephen Warrenf866a462013-06-11 15:14:01 -06002488 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05002489 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002490 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05002491 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06002492 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05002493
Thomas Choud52ebf12010-12-24 13:12:21 +00002494#ifdef CONFIG_MMC_SPI_CRC_ON
2495 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
2496 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
2497 cmd.resp_type = MMC_RSP_R1;
2498 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002499 err = mmc_send_cmd(mmc, &cmd, NULL);
Thomas Choud52ebf12010-12-24 13:12:21 +00002500 if (err)
2501 return err;
2502 }
2503#endif
2504
Andy Fleming272cc702008-10-30 16:41:01 -05002505 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002506 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
2507 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05002508 cmd.resp_type = MMC_RSP_R2;
2509 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002510
Sean Andersonda129172020-10-17 08:36:27 -04002511 err = mmc_send_cmd_quirks(mmc, &cmd, NULL, MMC_QUIRK_RETRY_SEND_CID, 4);
Andy Fleming272cc702008-10-30 16:41:01 -05002512 if (err)
2513 return err;
2514
2515 memcpy(mmc->cid, cmd.response, 16);
2516
2517 /*
2518 * For MMC cards, set the Relative Address.
2519 * For SD cards, get the Relatvie Address.
2520 * This also puts the cards into Standby State
2521 */
Thomas Choud52ebf12010-12-24 13:12:21 +00002522 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2523 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
2524 cmd.cmdarg = mmc->rca << 16;
2525 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05002526
Thomas Choud52ebf12010-12-24 13:12:21 +00002527 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002528
Thomas Choud52ebf12010-12-24 13:12:21 +00002529 if (err)
2530 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002531
Thomas Choud52ebf12010-12-24 13:12:21 +00002532 if (IS_SD(mmc))
2533 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
2534 }
Andy Fleming272cc702008-10-30 16:41:01 -05002535
2536 /* Get the Card-Specific Data */
2537 cmd.cmdidx = MMC_CMD_SEND_CSD;
2538 cmd.resp_type = MMC_RSP_R2;
2539 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05002540
2541 err = mmc_send_cmd(mmc, &cmd, NULL);
2542
2543 if (err)
2544 return err;
2545
Rabin Vincent998be3d2009-04-05 13:30:56 +05302546 mmc->csd[0] = cmd.response[0];
2547 mmc->csd[1] = cmd.response[1];
2548 mmc->csd[2] = cmd.response[2];
2549 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05002550
2551 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302552 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05002553
2554 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07002555 case 0:
2556 mmc->version = MMC_VERSION_1_2;
2557 break;
2558 case 1:
2559 mmc->version = MMC_VERSION_1_4;
2560 break;
2561 case 2:
2562 mmc->version = MMC_VERSION_2_2;
2563 break;
2564 case 3:
2565 mmc->version = MMC_VERSION_3;
2566 break;
2567 case 4:
2568 mmc->version = MMC_VERSION_4;
2569 break;
2570 default:
2571 mmc->version = MMC_VERSION_1_2;
2572 break;
Andy Fleming272cc702008-10-30 16:41:01 -05002573 }
2574 }
2575
2576 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302577 freq = fbase[(cmd.response[0] & 0x7)];
2578 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05002579
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002580 mmc->legacy_speed = freq * mult;
Heinrich Schuchardtf9a86fb2024-01-04 04:49:42 +01002581 if (!mmc->legacy_speed)
2582 log_debug("TRAN_SPEED: reserved value");
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002583 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05002584
Markus Niebelab711882013-12-16 13:40:46 +01002585 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05302586 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002587#if CONFIG_IS_ENABLED(MMC_WRITE)
Andy Fleming272cc702008-10-30 16:41:01 -05002588
2589 if (IS_SD(mmc))
2590 mmc->write_bl_len = mmc->read_bl_len;
2591 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05302592 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002593#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002594
2595 if (mmc->high_capacity) {
2596 csize = (mmc->csd[1] & 0x3f) << 16
2597 | (mmc->csd[2] & 0xffff0000) >> 16;
2598 cmult = 8;
2599 } else {
2600 csize = (mmc->csd[1] & 0x3ff) << 2
2601 | (mmc->csd[2] & 0xc0000000) >> 30;
2602 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2603 }
2604
Stephen Warrenf866a462013-06-11 15:14:01 -06002605 mmc->capacity_user = (csize + 1) << (cmult + 2);
2606 mmc->capacity_user *= mmc->read_bl_len;
2607 mmc->capacity_boot = 0;
2608 mmc->capacity_rpmb = 0;
2609 for (i = 0; i < 4; i++)
2610 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002611
Simon Glass8bfa1952013-04-03 08:54:30 +00002612 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2613 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05002614
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002615#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass8bfa1952013-04-03 08:54:30 +00002616 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2617 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002618#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002619
Markus Niebelab711882013-12-16 13:40:46 +01002620 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2621 cmd.cmdidx = MMC_CMD_SET_DSR;
2622 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2623 cmd.resp_type = MMC_RSP_NONE;
2624 if (mmc_send_cmd(mmc, &cmd, NULL))
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002625 pr_warn("MMC: SET_DSR failed\n");
Markus Niebelab711882013-12-16 13:40:46 +01002626 }
2627
Andy Fleming272cc702008-10-30 16:41:01 -05002628 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002629 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2630 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00002631 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002632 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00002633 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002634
Thomas Choud52ebf12010-12-24 13:12:21 +00002635 if (err)
2636 return err;
2637 }
Andy Fleming272cc702008-10-30 16:41:01 -05002638
Lei Wene6f99a52011-06-22 17:03:31 +00002639 /*
2640 * For SD, its erase group is always one sector
2641 */
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002642#if CONFIG_IS_ENABLED(MMC_WRITE)
Lei Wene6f99a52011-06-22 17:03:31 +00002643 mmc->erase_grp_size = 1;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002644#endif
Lei Wenbc897b12011-05-02 16:26:26 +00002645 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00002646
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002647 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002648 if (err)
2649 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05302650
Simon Glassc40fdca2016-05-01 13:52:35 -06002651 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06002652 if (err)
2653 return err;
2654
Marek Vasut62d77ce2018-04-15 00:37:11 +02002655#if CONFIG_IS_ENABLED(MMC_TINY)
2656 mmc_set_clock(mmc, mmc->legacy_speed, false);
Faiz Abbase8d5dde2020-02-26 13:44:32 +05302657 mmc_select_mode(mmc, MMC_LEGACY);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002658 mmc_set_bus_width(mmc, 1);
2659#else
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002660 if (IS_SD(mmc)) {
2661 err = sd_get_capabilities(mmc);
2662 if (err)
2663 return err;
2664 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2665 } else {
2666 err = mmc_get_capabilities(mmc);
2667 if (err)
2668 return err;
Masahiro Yamada8adf50e2020-01-23 14:31:12 +09002669 err = mmc_select_mode_and_width(mmc, mmc->card_caps);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002670 }
Marek Vasut62d77ce2018-04-15 00:37:11 +02002671#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002672 if (err)
2673 return err;
2674
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002675 mmc->best_mode = mmc->selected_mode;
Jaehoon Chungad5fd922012-03-26 21:16:03 +00002676
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002677 /* Fix the block length for DDR mode */
2678 if (mmc->ddr_mode) {
2679 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002680#if CONFIG_IS_ENABLED(MMC_WRITE)
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002681 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002682#endif
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002683 }
2684
Andy Fleming272cc702008-10-30 16:41:01 -05002685 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06002686 bdesc = mmc_get_blk_desc(mmc);
2687 bdesc->lun = 0;
2688 bdesc->hwpart = 0;
2689 bdesc->type = 0;
2690 bdesc->blksz = mmc->read_bl_len;
2691 bdesc->log2blksz = LOG2(bdesc->blksz);
2692 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01002693#if !defined(CONFIG_SPL_BUILD) || \
2694 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
Simon Glass27084c02019-09-25 08:56:27 -06002695 !CONFIG_IS_ENABLED(USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06002696 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00002697 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2698 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002699 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002700 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2701 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2702 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002703 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002704 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01002705#else
Simon Glassc40fdca2016-05-01 13:52:35 -06002706 bdesc->vendor[0] = 0;
2707 bdesc->product[0] = 0;
2708 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01002709#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002710
Andre Przywaraeef05fd2018-12-17 10:05:45 +00002711#if !defined(CONFIG_DM_MMC) && (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT))
2712 part_init(bdesc);
2713#endif
2714
Andy Fleming272cc702008-10-30 16:41:01 -05002715 return 0;
2716}
2717
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002718static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002719{
2720 struct mmc_cmd cmd;
2721 int err;
2722
2723 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2724 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002725 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05002726 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05002727
2728 err = mmc_send_cmd(mmc, &cmd, NULL);
2729
2730 if (err)
2731 return err;
2732
Rabin Vincent998be3d2009-04-05 13:30:56 +05302733 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002734 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002735 else
2736 mmc->version = SD_VERSION_2;
2737
2738 return 0;
2739}
2740
Simon Glassc4d660d2017-07-04 13:31:19 -06002741#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002742/* board-specific MMC power initializations. */
2743__weak void board_mmc_power_init(void)
2744{
2745}
Simon Glass05cbeb72017-04-22 19:10:56 -06002746#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002747
Peng Fan2051aef2016-10-11 15:08:43 +08002748static int mmc_power_init(struct mmc *mmc)
2749{
Simon Glassc4d660d2017-07-04 13:31:19 -06002750#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002751#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08002752 int ret;
2753
2754 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002755 &mmc->vmmc_supply);
2756 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002757 pr_debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002758
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002759 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2760 &mmc->vqmmc_supply);
2761 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002762 pr_debug("%s: No vqmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002763#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06002764#else /* !CONFIG_DM_MMC */
2765 /*
2766 * Driver model should use a regulator, as above, rather than calling
2767 * out to board code.
2768 */
2769 board_mmc_power_init();
2770#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002771 return 0;
2772}
2773
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002774/*
2775 * put the host in the initial state:
2776 * - turn on Vdd (card power supply)
2777 * - configure the bus width and clock to minimal values
2778 */
2779static void mmc_set_initial_state(struct mmc *mmc)
2780{
2781 int err;
2782
2783 /* First try to set 3.3V. If it fails set to 1.8V */
2784 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2785 if (err != 0)
2786 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2787 if (err != 0)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002788 pr_warn("mmc: failed to set signal voltage\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002789
2790 mmc_select_mode(mmc, MMC_LEGACY);
2791 mmc_set_bus_width(mmc, 1);
Jaehoon Chung65117182018-01-26 19:25:29 +09002792 mmc_set_clock(mmc, 0, MMC_CLK_ENABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002793}
2794
2795static int mmc_power_on(struct mmc *mmc)
2796{
2797#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2798 if (mmc->vmmc_supply) {
Jonas Karlman9a2e9cc2023-07-19 21:20:59 +00002799 int ret = regulator_set_enable_if_allowed(mmc->vmmc_supply,
2800 true);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002801
Jonas Karlman9a2e9cc2023-07-19 21:20:59 +00002802 if (ret && ret != -ENOSYS) {
Jaehoon Chung58896452020-11-17 07:04:59 +09002803 printf("Error enabling VMMC supply : %d\n", ret);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002804 return ret;
2805 }
2806 }
2807#endif
2808 return 0;
2809}
2810
2811static int mmc_power_off(struct mmc *mmc)
2812{
Jaehoon Chung65117182018-01-26 19:25:29 +09002813 mmc_set_clock(mmc, 0, MMC_CLK_DISABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002814#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2815 if (mmc->vmmc_supply) {
Jonas Karlman9a2e9cc2023-07-19 21:20:59 +00002816 int ret = regulator_set_enable_if_allowed(mmc->vmmc_supply,
2817 false);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002818
Jonas Karlman9a2e9cc2023-07-19 21:20:59 +00002819 if (ret && ret != -ENOSYS) {
Jaehoon Chung58896452020-11-17 07:04:59 +09002820 pr_debug("Error disabling VMMC supply : %d\n", ret);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002821 return ret;
2822 }
2823 }
2824#endif
2825 return 0;
2826}
2827
2828static int mmc_power_cycle(struct mmc *mmc)
2829{
2830 int ret;
2831
2832 ret = mmc_power_off(mmc);
2833 if (ret)
2834 return ret;
Yann Gautier3602a562019-09-19 17:56:12 +02002835
2836 ret = mmc_host_power_cycle(mmc);
2837 if (ret)
2838 return ret;
2839
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002840 /*
2841 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2842 * to be on the safer side.
2843 */
2844 udelay(2000);
2845 return mmc_power_on(mmc);
2846}
2847
Pali Rohára4c577f2021-07-14 16:37:29 +02002848int mmc_get_op_cond(struct mmc *mmc, bool quiet)
Andy Fleming272cc702008-10-30 16:41:01 -05002849{
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002850 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
Macpaul Linafd59322011-11-14 23:35:39 +00002851 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05002852
Lei Wenbc897b12011-05-02 16:26:26 +00002853 if (mmc->has_init)
2854 return 0;
2855
Peng Fan2051aef2016-10-11 15:08:43 +08002856 err = mmc_power_init(mmc);
2857 if (err)
2858 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002859
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002860#ifdef CONFIG_MMC_QUIRKS
2861 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
Joel Johnsond4a5fa32020-01-11 09:08:14 -07002862 MMC_QUIRK_RETRY_SEND_CID |
2863 MMC_QUIRK_RETRY_APP_CMD;
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002864#endif
2865
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002866 err = mmc_power_cycle(mmc);
2867 if (err) {
2868 /*
2869 * if power cycling is not supported, we should not try
2870 * to use the UHS modes, because we wouldn't be able to
2871 * recover from an error during the UHS initialization.
2872 */
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002873 pr_debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002874 uhs_en = false;
2875 mmc->host_caps &= ~UHS_CAPS;
2876 err = mmc_power_on(mmc);
2877 }
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002878 if (err)
2879 return err;
2880
Simon Glasse7881d82017-07-29 11:35:31 -06002881#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu390f9bd2020-09-01 16:57:59 +08002882 /*
2883 * Re-initialization is needed to clear old configuration for
2884 * mmc rescan.
2885 */
2886 err = mmc_reinit(mmc);
Simon Glass8ca51e52016-06-12 23:30:22 -06002887#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02002888 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002889 err = mmc->cfg->ops->init(mmc);
Yangbo Lu390f9bd2020-09-01 16:57:59 +08002890#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002891 if (err)
2892 return err;
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06002893 mmc->ddr_mode = 0;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02002894
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002895retry:
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002896 mmc_set_initial_state(mmc);
Jean-Jacques Hiblot318a7a52017-09-21 16:30:01 +02002897
Andy Fleming272cc702008-10-30 16:41:01 -05002898 /* Reset the Card */
2899 err = mmc_go_idle(mmc);
2900
2901 if (err)
2902 return err;
2903
Marcel Ziswilerf5624b12019-05-20 02:44:53 +02002904 /* The internal partition reset to user partition(0) at every CMD0 */
Simon Glassc40fdca2016-05-01 13:52:35 -06002905 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00002906
Andy Fleming272cc702008-10-30 16:41:01 -05002907 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00002908 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002909
Andy Fleming272cc702008-10-30 16:41:01 -05002910 /* Now try to get the SD card's operating condition */
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002911 err = sd_send_op_cond(mmc, uhs_en);
2912 if (err && uhs_en) {
2913 uhs_en = false;
2914 mmc_power_cycle(mmc);
2915 goto retry;
2916 }
Andy Fleming272cc702008-10-30 16:41:01 -05002917
2918 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002919 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05002920 err = mmc_send_op_cond(mmc);
2921
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002922 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01002923#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Pali Rohára4c577f2021-07-14 16:37:29 +02002924 if (!quiet)
2925 pr_err("Card did not respond to voltage select! : %d\n", err);
Paul Burton56196822013-09-04 16:12:25 +01002926#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002927 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002928 }
2929 }
2930
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002931 return err;
2932}
2933
2934int mmc_start_init(struct mmc *mmc)
2935{
2936 bool no_card;
2937 int err = 0;
2938
2939 /*
2940 * all hosts are capable of 1 bit bus-width and able to use the legacy
2941 * timings.
2942 */
Faiz Abbase8d5dde2020-02-26 13:44:32 +05302943 mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(MMC_LEGACY) |
Aswath Govindraju19f7a342021-08-13 23:04:41 +05302944 MMC_MODE_1BIT;
2945
2946 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET)) {
2947 if (mmc->user_speed_mode != MMC_MODES_END) {
2948 int i;
2949 /* set host caps */
2950 if (mmc->host_caps & MMC_CAP(mmc->user_speed_mode)) {
2951 /* Remove all existing speed capabilities */
2952 for (i = MMC_LEGACY; i < MMC_MODES_END; i++)
2953 mmc->host_caps &= ~MMC_CAP(i);
2954 mmc->host_caps |= (MMC_CAP(mmc->user_speed_mode)
2955 | MMC_CAP(MMC_LEGACY) |
2956 MMC_MODE_1BIT);
2957 } else {
2958 pr_err("bus_mode requested is not supported\n");
2959 return -EINVAL;
2960 }
2961 }
2962 }
Faiz Abbas32860bd2020-02-26 13:44:30 +05302963#if CONFIG_IS_ENABLED(DM_MMC)
2964 mmc_deferred_probe(mmc);
2965#endif
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002966#if !defined(CONFIG_MMC_BROKEN_CD)
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002967 no_card = mmc_getcd(mmc) == 0;
2968#else
2969 no_card = 0;
2970#endif
2971#if !CONFIG_IS_ENABLED(DM_MMC)
Baruch Siachfea39392019-07-22 15:52:12 +03002972 /* we pretend there's no card when init is NULL */
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002973 no_card = no_card || (mmc->cfg->ops->init == NULL);
2974#endif
2975 if (no_card) {
2976 mmc->has_init = 0;
2977#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2978 pr_err("MMC: no card present\n");
2979#endif
2980 return -ENOMEDIUM;
2981 }
2982
Pali Rohára4c577f2021-07-14 16:37:29 +02002983 err = mmc_get_op_cond(mmc, false);
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002984
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002985 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002986 mmc->init_in_progress = 1;
2987
2988 return err;
2989}
2990
2991static int mmc_complete_init(struct mmc *mmc)
2992{
2993 int err = 0;
2994
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002995 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002996 if (mmc->op_cond_pending)
2997 err = mmc_complete_op_cond(mmc);
2998
2999 if (!err)
3000 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00003001 if (err)
3002 mmc->has_init = 0;
3003 else
3004 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00003005 return err;
3006}
3007
3008int mmc_init(struct mmc *mmc)
3009{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05003010 int err = 0;
Vipul Kumar36332b62018-05-03 12:20:54 +05303011 __maybe_unused ulong start;
Simon Glassc4d660d2017-07-04 13:31:19 -06003012#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06003013 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00003014
Simon Glass33fb2112016-05-01 13:52:41 -06003015 upriv->mmc = mmc;
3016#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00003017 if (mmc->has_init)
3018 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02003019
3020 start = get_timer(0);
3021
Che-Liang Chioue9550442012-11-28 15:21:13 +00003022 if (!mmc->init_in_progress)
3023 err = mmc_start_init(mmc);
3024
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05003025 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00003026 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01003027 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09003028 pr_info("%s: %d, time %lu\n", __func__, err, get_timer(start));
Jagan Teki919b4852017-01-10 11:18:43 +01003029
Lei Wenbc897b12011-05-02 16:26:26 +00003030 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05003031}
3032
Marek Vasutfceea992019-01-29 04:45:51 +01003033int mmc_deinit(struct mmc *mmc)
3034{
3035 u32 caps_filtered;
3036
Marek Vasut27ba82c2024-03-17 04:01:22 +01003037 if (!CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) &&
3038 !CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) &&
3039 !CONFIG_IS_ENABLED(MMC_HS400_SUPPORT))
3040 return 0;
3041
Marek Vasutfceea992019-01-29 04:45:51 +01003042 if (!mmc->has_init)
3043 return 0;
3044
3045 if (IS_SD(mmc)) {
3046 caps_filtered = mmc->card_caps &
3047 ~(MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25) |
3048 MMC_CAP(UHS_SDR50) | MMC_CAP(UHS_DDR50) |
3049 MMC_CAP(UHS_SDR104));
3050
3051 return sd_select_mode_and_width(mmc, caps_filtered);
3052 } else {
3053 caps_filtered = mmc->card_caps &
Ye Lifb8c2e82021-08-17 17:20:34 +08003054 ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_400_ES));
Marek Vasutfceea992019-01-29 04:45:51 +01003055
3056 return mmc_select_mode_and_width(mmc, caps_filtered);
3057 }
3058}
Marek Vasutfceea992019-01-29 04:45:51 +01003059
Markus Niebelab711882013-12-16 13:40:46 +01003060int mmc_set_dsr(struct mmc *mmc, u16 val)
3061{
3062 mmc->dsr = val;
3063 return 0;
3064}
3065
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02003066/* CPU-specific MMC initializations */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003067__weak int cpu_mmc_init(struct bd_info *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05003068{
3069 return -1;
3070}
3071
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02003072/* board-specific MMC initializations. */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003073__weak int board_mmc_init(struct bd_info *bis)
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02003074{
3075 return -1;
3076}
Andy Fleming272cc702008-10-30 16:41:01 -05003077
Che-Liang Chioue9550442012-11-28 15:21:13 +00003078void mmc_set_preinit(struct mmc *mmc, int preinit)
3079{
3080 mmc->preinit = preinit;
3081}
3082
Faiz Abbas8a856db2018-02-12 19:35:24 +05303083#if CONFIG_IS_ENABLED(DM_MMC)
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003084static int mmc_probe(struct bd_info *bis)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003085{
Simon Glass4a1db6d2015-12-29 05:22:49 -07003086 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003087 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07003088 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003089
3090 ret = uclass_get(UCLASS_MMC, &uc);
3091 if (ret)
3092 return ret;
3093
Simon Glass4a1db6d2015-12-29 05:22:49 -07003094 /*
3095 * Try to add them in sequence order. Really with driver model we
3096 * should allow holes, but the current MMC list does not allow that.
3097 * So if we request 0, 1, 3 we will get 0, 1, 2.
3098 */
3099 for (i = 0; ; i++) {
3100 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
3101 if (ret == -ENODEV)
3102 break;
3103 }
3104 uclass_foreach_dev(dev, uc) {
3105 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003106 if (ret)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01003107 pr_err("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003108 }
3109
3110 return 0;
3111}
3112#else
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003113static int mmc_probe(struct bd_info *bis)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003114{
3115 if (board_mmc_init(bis) < 0)
3116 cpu_mmc_init(bis);
3117
3118 return 0;
3119}
3120#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00003121
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003122int mmc_initialize(struct bd_info *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05003123{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003124 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003125 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003126 if (initialized) /* Avoid initializing mmc multiple times */
3127 return 0;
3128 initialized = 1;
3129
Simon Glassc4d660d2017-07-04 13:31:19 -06003130#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01003131#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06003132 mmc_list_init();
3133#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01003134#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003135 ret = mmc_probe(bis);
3136 if (ret)
3137 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05003138
Ying Zhangbb0dc102013-08-16 15:16:11 +08003139#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05003140 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08003141#endif
Andy Fleming272cc702008-10-30 16:41:01 -05003142
Simon Glassc40fdca2016-05-01 13:52:35 -06003143 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05003144 return 0;
3145}
Tomas Melincd3d4882016-11-25 11:01:03 +02003146
Lokesh Vutla80f02012019-09-09 14:40:36 +05303147#if CONFIG_IS_ENABLED(DM_MMC)
3148int mmc_init_device(int num)
3149{
3150 struct udevice *dev;
3151 struct mmc *m;
3152 int ret;
3153
Aswath Govindraju2153a082021-03-25 12:48:47 +05303154 if (uclass_get_device_by_seq(UCLASS_MMC, num, &dev)) {
3155 ret = uclass_get_device(UCLASS_MMC, num, &dev);
3156 if (ret)
3157 return ret;
3158 }
Lokesh Vutla80f02012019-09-09 14:40:36 +05303159
3160 m = mmc_get_mmc_dev(dev);
3161 if (!m)
3162 return 0;
Venkatesh Yadav Abbarapu337af542022-09-29 10:22:49 +05303163
3164 /* Initialising user set speed mode */
3165 m->user_speed_mode = MMC_MODES_END;
3166
Lokesh Vutla80f02012019-09-09 14:40:36 +05303167 if (m->preinit)
3168 mmc_start_init(m);
3169
3170 return 0;
3171}
3172#endif
3173
Tomas Melincd3d4882016-11-25 11:01:03 +02003174#ifdef CONFIG_CMD_BKOPS_ENABLE
Marek Vasutcf1f7352023-01-05 15:19:08 +01003175int mmc_set_bkops_enable(struct mmc *mmc, bool autobkops, bool enable)
Tomas Melincd3d4882016-11-25 11:01:03 +02003176{
3177 int err;
Marek Vasutcf1f7352023-01-05 15:19:08 +01003178 u32 bit = autobkops ? BIT(1) : BIT(0);
Tomas Melincd3d4882016-11-25 11:01:03 +02003179 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
3180
3181 err = mmc_send_ext_csd(mmc, ext_csd);
3182 if (err) {
3183 puts("Could not get ext_csd register values\n");
3184 return err;
3185 }
3186
3187 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
3188 puts("Background operations not supported on device\n");
3189 return -EMEDIUMTYPE;
3190 }
3191
Marek Vasutcf1f7352023-01-05 15:19:08 +01003192 if (enable && (ext_csd[EXT_CSD_BKOPS_EN] & bit)) {
Tomas Melincd3d4882016-11-25 11:01:03 +02003193 puts("Background operations already enabled\n");
3194 return 0;
3195 }
3196
Marek Vasutcf1f7352023-01-05 15:19:08 +01003197 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN,
3198 enable ? bit : 0);
Tomas Melincd3d4882016-11-25 11:01:03 +02003199 if (err) {
Marek Vasutcf1f7352023-01-05 15:19:08 +01003200 printf("Failed to %sable manual background operations\n",
3201 enable ? "en" : "dis");
Tomas Melincd3d4882016-11-25 11:01:03 +02003202 return err;
3203 }
3204
Marek Vasutcf1f7352023-01-05 15:19:08 +01003205 printf("%sabled %s background operations\n",
3206 enable ? "En" : "Dis", autobkops ? "auto" : "manual");
Tomas Melincd3d4882016-11-25 11:01:03 +02003207
3208 return 0;
3209}
3210#endif
David Woodhouse4dee3f72020-08-04 10:05:46 +01003211
3212__weak int mmc_get_env_dev(void)
3213{
3214#ifdef CONFIG_SYS_MMC_ENV_DEV
3215 return CONFIG_SYS_MMC_ENV_DEV;
3216#else
3217 return 0;
3218#endif
3219}