blob: 55987f329ef15e3b262afa13ee2051ebf87f0a11 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleming272cc702008-10-30 16:41:01 -05002/*
3 * Copyright 2008, Freescale Semiconductor, Inc
Yangbo Lu39913ac2020-06-17 18:08:58 +08004 * Copyright 2020 NXP
Andy Fleming272cc702008-10-30 16:41:01 -05005 * Andy Fleming
6 *
7 * Based vaguely on the Linux code
Andy Fleming272cc702008-10-30 16:41:01 -05008 */
9
10#include <config.h>
11#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060012#include <blk.h>
Andy Fleming272cc702008-10-30 16:41:01 -050013#include <command.h>
Sjoerd Simons8e3332e2015-08-30 16:55:45 -060014#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Sjoerd Simons8e3332e2015-08-30 16:55:45 -060016#include <dm/device-internal.h>
Stephen Warrend4622df2014-05-23 12:47:06 -060017#include <errno.h>
Andy Fleming272cc702008-10-30 16:41:01 -050018#include <mmc.h>
19#include <part.h>
Simon Glasscd93d622020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060021#include <linux/delay.h>
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>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053027#include <div64.h>
Paul Burtonda61fa52013-09-09 15:30:26 +010028#include "mmc_private.h"
Andy Fleming272cc702008-10-30 16:41:01 -050029
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +020030#define DEFAULT_CMD6_TIMEOUT_MS 500
31
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +020032static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
Marek Vasutb5b838f2016-12-01 02:06:33 +010033
Simon Glasse7881d82017-07-29 11:35:31 -060034#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020035
Sam Protsenko6cf8a902019-08-14 22:52:51 +030036static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020037{
Loic Poulaind6ad5a02022-05-26 16:37:21 +020038 if (mmc->cfg->ops->wait_dat0)
39 return mmc->cfg->ops->wait_dat0(mmc, state, timeout_us);
40
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020041 return -ENOSYS;
42}
43
Jeroen Hofstee750121c2014-07-12 21:24:08 +020044__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000045{
46 return -1;
47}
48
49int mmc_getwp(struct mmc *mmc)
50{
51 int wp;
52
53 wp = board_mmc_getwp(mmc);
54
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000055 if (wp < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020056 if (mmc->cfg->ops->getwp)
57 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000058 else
59 wp = 0;
60 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000061
62 return wp;
63}
64
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +020065__weak int board_mmc_getcd(struct mmc *mmc)
66{
Stefano Babic11fdade2010-02-05 15:04:43 +010067 return -1;
68}
Simon Glass8ca51e52016-06-12 23:30:22 -060069#endif
Stefano Babic11fdade2010-02-05 15:04:43 +010070
Marek Vasut8635ff92012-03-15 18:41:35 +000071#ifdef CONFIG_MMC_TRACE
Simon Glassc0c76eb2016-06-12 23:30:20 -060072void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
73{
74 printf("CMD_SEND:%d\n", cmd->cmdidx);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010075 printf("\t\tARG\t\t\t 0x%08x\n", cmd->cmdarg);
Simon Glassc0c76eb2016-06-12 23:30:20 -060076}
77
78void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
79{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000080 int i;
81 u8 *ptr;
82
Bin Meng7863ce52016-03-17 21:53:14 -070083 if (ret) {
84 printf("\t\tRET\t\t\t %d\n", ret);
85 } else {
86 switch (cmd->resp_type) {
87 case MMC_RSP_NONE:
88 printf("\t\tMMC_RSP_NONE\n");
89 break;
90 case MMC_RSP_R1:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010091 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070092 cmd->response[0]);
93 break;
94 case MMC_RSP_R1b:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010095 printf("\t\tMMC_RSP_R1b\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070096 cmd->response[0]);
97 break;
98 case MMC_RSP_R2:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010099 printf("\t\tMMC_RSP_R2\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700100 cmd->response[0]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100101 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700102 cmd->response[1]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100103 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700104 cmd->response[2]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100105 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700106 cmd->response[3]);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000107 printf("\n");
Bin Meng7863ce52016-03-17 21:53:14 -0700108 printf("\t\t\t\t\tDUMPING DATA\n");
109 for (i = 0; i < 4; i++) {
110 int j;
111 printf("\t\t\t\t\t%03d - ", i*4);
112 ptr = (u8 *)&cmd->response[i];
113 ptr += 3;
114 for (j = 0; j < 4; j++)
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100115 printf("%02x ", *ptr--);
Bin Meng7863ce52016-03-17 21:53:14 -0700116 printf("\n");
117 }
118 break;
119 case MMC_RSP_R3:
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100120 printf("\t\tMMC_RSP_R3,4\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700121 cmd->response[0]);
122 break;
123 default:
124 printf("\t\tERROR MMC rsp not supported\n");
125 break;
Bin Meng53e8e402016-03-17 21:53:13 -0700126 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000127 }
Simon Glassc0c76eb2016-06-12 23:30:20 -0600128}
129
130void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
131{
132 int status;
133
134 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
135 printf("CURR STATE:%d\n", status);
136}
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000137#endif
Simon Glassc0c76eb2016-06-12 23:30:20 -0600138
Pali Rohár48467e42022-04-03 00:20:10 +0200139#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG) || CONFIG_VAL(LOGLEVEL) >= LOGL_DEBUG
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200140const char *mmc_mode_name(enum bus_mode mode)
141{
142 static const char *const names[] = {
143 [MMC_LEGACY] = "MMC legacy",
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200144 [MMC_HS] = "MMC High Speed (26MHz)",
145 [SD_HS] = "SD High Speed (50MHz)",
146 [UHS_SDR12] = "UHS SDR12 (25MHz)",
147 [UHS_SDR25] = "UHS SDR25 (50MHz)",
148 [UHS_SDR50] = "UHS SDR50 (100MHz)",
149 [UHS_SDR104] = "UHS SDR104 (208MHz)",
150 [UHS_DDR50] = "UHS DDR50 (50MHz)",
151 [MMC_HS_52] = "MMC High Speed (52MHz)",
152 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
153 [MMC_HS_200] = "HS200 (200MHz)",
Peng Fan3dd26262018-08-10 14:07:54 +0800154 [MMC_HS_400] = "HS400 (200MHz)",
Peng Fan44acd492019-07-10 14:43:07 +0800155 [MMC_HS_400_ES] = "HS400ES (200MHz)",
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200156 };
157
158 if (mode >= MMC_MODES_END)
159 return "Unknown mode";
160 else
161 return names[mode];
162}
163#endif
164
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200165static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
166{
167 static const int freqs[] = {
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900168 [MMC_LEGACY] = 25000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200169 [MMC_HS] = 26000000,
170 [SD_HS] = 50000000,
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900171 [MMC_HS_52] = 52000000,
172 [MMC_DDR_52] = 52000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200173 [UHS_SDR12] = 25000000,
174 [UHS_SDR25] = 50000000,
175 [UHS_SDR50] = 100000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200176 [UHS_DDR50] = 50000000,
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100177 [UHS_SDR104] = 208000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200178 [MMC_HS_200] = 200000000,
Peng Fan3dd26262018-08-10 14:07:54 +0800179 [MMC_HS_400] = 200000000,
Peng Fan44acd492019-07-10 14:43:07 +0800180 [MMC_HS_400_ES] = 200000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200181 };
182
183 if (mode == MMC_LEGACY)
184 return mmc->legacy_speed;
185 else if (mode >= MMC_MODES_END)
186 return 0;
187 else
188 return freqs[mode];
189}
190
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200191static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
192{
193 mmc->selected_mode = mode;
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200194 mmc->tran_speed = mmc_mode2freq(mmc, mode);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200195 mmc->ddr_mode = mmc_is_mode_ddr(mode);
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900196 pr_debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
197 mmc->tran_speed / 1000000);
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200198 return 0;
199}
200
Simon Glasse7881d82017-07-29 11:35:31 -0600201#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassc0c76eb2016-06-12 23:30:20 -0600202int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
203{
204 int ret;
205
206 mmmc_trace_before_send(mmc, cmd);
207 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
208 mmmc_trace_after_send(mmc, cmd, ret);
209
Marek Vasut8635ff92012-03-15 18:41:35 +0000210 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500211}
Simon Glass8ca51e52016-06-12 23:30:22 -0600212#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500213
Sean Andersonda129172020-10-17 08:36:27 -0400214/**
215 * mmc_send_cmd_retry() - send a command to the mmc device, retrying on error
216 *
217 * @dev: device to receive the command
218 * @cmd: command to send
219 * @data: additional data to send/receive
220 * @retries: how many times to retry; mmc_send_cmd is always called at least
221 * once
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100222 * Return: 0 if ok, -ve on error
Sean Andersonda129172020-10-17 08:36:27 -0400223 */
224static int mmc_send_cmd_retry(struct mmc *mmc, struct mmc_cmd *cmd,
225 struct mmc_data *data, uint retries)
226{
227 int ret;
228
229 do {
230 ret = mmc_send_cmd(mmc, cmd, data);
231 } while (ret && retries--);
232
233 return ret;
234}
235
236/**
237 * mmc_send_cmd_quirks() - send a command to the mmc device, retrying if a
238 * specific quirk is enabled
239 *
240 * @dev: device to receive the command
241 * @cmd: command to send
242 * @data: additional data to send/receive
243 * @quirk: retry only if this quirk is enabled
244 * @retries: how many times to retry; mmc_send_cmd is always called at least
245 * once
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100246 * Return: 0 if ok, -ve on error
Sean Andersonda129172020-10-17 08:36:27 -0400247 */
248static int mmc_send_cmd_quirks(struct mmc *mmc, struct mmc_cmd *cmd,
249 struct mmc_data *data, u32 quirk, uint retries)
250{
Simon Glass497b7c62023-02-05 15:40:16 -0700251 if (IS_ENABLED(CONFIG_MMC_QUIRKS) && mmc->quirks & quirk)
Sean Andersonda129172020-10-17 08:36:27 -0400252 return mmc_send_cmd_retry(mmc, cmd, data, retries);
253 else
254 return mmc_send_cmd(mmc, cmd, data);
255}
256
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200257int mmc_send_status(struct mmc *mmc, unsigned int *status)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000258{
259 struct mmc_cmd cmd;
Sean Andersonda129172020-10-17 08:36:27 -0400260 int ret;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000261
262 cmd.cmdidx = MMC_CMD_SEND_STATUS;
263 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200264 if (!mmc_host_is_spi(mmc))
265 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000266
Sean Andersonda129172020-10-17 08:36:27 -0400267 ret = mmc_send_cmd_retry(mmc, &cmd, NULL, 4);
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200268 mmc_trace_state(mmc, &cmd);
Sean Andersonda129172020-10-17 08:36:27 -0400269 if (!ret)
270 *status = cmd.response[0];
271
272 return ret;
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200273}
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200274
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300275int mmc_poll_for_busy(struct mmc *mmc, int timeout_ms)
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200276{
277 unsigned int status;
278 int err;
279
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300280 err = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotcd0b80e2019-07-02 10:53:53 +0200281 if (err != -ENOSYS)
282 return err;
283
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200284 while (1) {
285 err = mmc_send_status(mmc, &status);
286 if (err)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000287 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000288
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200289 if ((status & MMC_STATUS_RDY_FOR_DATA) &&
290 (status & MMC_STATUS_CURR_STATE) !=
291 MMC_STATE_PRG)
292 break;
293
294 if (status & MMC_STATUS_MASK) {
295#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
296 pr_err("Status Error: 0x%08x\n", status);
297#endif
298 return -ECOMM;
299 }
300
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300301 if (timeout_ms-- <= 0)
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500302 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000303
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500304 udelay(1000);
305 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000306
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300307 if (timeout_ms <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100308#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100309 pr_err("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100310#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900311 return -ETIMEDOUT;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000312 }
313
314 return 0;
315}
316
Paul Burtonda61fa52013-09-09 15:30:26 +0100317int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500318{
319 struct mmc_cmd cmd;
320
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600321 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900322 return 0;
323
Andy Fleming272cc702008-10-30 16:41:01 -0500324 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
325 cmd.resp_type = MMC_RSP_R1;
326 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500327
Sean Andersonda129172020-10-17 08:36:27 -0400328 return mmc_send_cmd_quirks(mmc, &cmd, NULL,
329 MMC_QUIRK_RETRY_SET_BLOCKLEN, 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500330}
331
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100332#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200333static const u8 tuning_blk_pattern_4bit[] = {
334 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
335 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
336 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
337 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
338 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
339 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
340 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
341 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
342};
343
344static const u8 tuning_blk_pattern_8bit[] = {
345 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
346 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
347 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
348 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
349 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
350 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
351 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
352 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
353 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
354 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
355 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
356 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
357 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
358 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
359 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
360 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
361};
362
Marek Vasuta3b27862024-02-20 09:36:23 +0100363int mmc_send_tuning(struct mmc *mmc, u32 opcode)
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200364{
365 struct mmc_cmd cmd;
366 struct mmc_data data;
367 const u8 *tuning_block_pattern;
368 int size, err;
369
370 if (mmc->bus_width == 8) {
371 tuning_block_pattern = tuning_blk_pattern_8bit;
372 size = sizeof(tuning_blk_pattern_8bit);
373 } else if (mmc->bus_width == 4) {
374 tuning_block_pattern = tuning_blk_pattern_4bit;
375 size = sizeof(tuning_blk_pattern_4bit);
376 } else {
377 return -EINVAL;
378 }
379
380 ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size);
381
382 cmd.cmdidx = opcode;
383 cmd.cmdarg = 0;
384 cmd.resp_type = MMC_RSP_R1;
385
386 data.dest = (void *)data_buf;
387 data.blocks = 1;
388 data.blocksize = size;
389 data.flags = MMC_DATA_READ;
390
391 err = mmc_send_cmd(mmc, &cmd, &data);
392 if (err)
393 return err;
394
395 if (memcmp(data_buf, tuning_block_pattern, size))
396 return -EIO;
397
398 return 0;
399}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100400#endif
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200401
Hai Pham0ac2cca2023-06-20 00:38:24 +0200402int mmc_send_stop_transmission(struct mmc *mmc, bool write)
403{
404 struct mmc_cmd cmd;
405
406 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
407 cmd.cmdarg = 0;
408 /*
409 * JEDEC Standard No. 84-B51 Page 126
410 * CMD12 STOP_TRANSMISSION R1/R1b[3]
411 * NOTE 3 R1 for read cases and R1b for write cases.
412 *
413 * Physical Layer Simplified Specification Version 9.00
414 * 7.3.1.3 Detailed Command Description
415 * CMD12 R1b
416 */
417 cmd.resp_type = (IS_SD(mmc) || write) ? MMC_RSP_R1b : MMC_RSP_R1;
418
419 return mmc_send_cmd(mmc, &cmd, NULL);
420}
421
Sascha Silbeff8fef52013-06-14 13:07:25 +0200422static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000423 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500424{
425 struct mmc_cmd cmd;
426 struct mmc_data data;
427
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700428 if (blkcnt > 1)
429 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
430 else
431 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500432
433 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700434 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500435 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700436 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500437
438 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500439
440 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700441 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500442 data.blocksize = mmc->read_bl_len;
443 data.flags = MMC_DATA_READ;
444
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700445 if (mmc_send_cmd(mmc, &cmd, &data))
446 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500447
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700448 if (blkcnt > 1) {
Hai Pham0ac2cca2023-06-20 00:38:24 +0200449 if (mmc_send_stop_transmission(mmc, false)) {
Paul Burton56196822013-09-04 16:12:25 +0100450#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100451 pr_err("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100452#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700453 return 0;
454 }
Andy Fleming272cc702008-10-30 16:41:01 -0500455 }
456
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700457 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500458}
459
Marek Vasut145429a2020-04-04 12:45:05 +0200460#if !CONFIG_IS_ENABLED(DM_MMC)
461static int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
462{
463 if (mmc->cfg->ops->get_b_max)
464 return mmc->cfg->ops->get_b_max(mmc, dst, blkcnt);
465 else
466 return mmc->cfg->b_max;
467}
468#endif
469
Simon Glassc4d660d2017-07-04 13:31:19 -0600470#if CONFIG_IS_ENABLED(BLK)
Simon Glass7dba0b92016-06-12 23:30:15 -0600471ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600472#else
Simon Glass7dba0b92016-06-12 23:30:15 -0600473ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
474 void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600475#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500476{
Simon Glassc4d660d2017-07-04 13:31:19 -0600477#if CONFIG_IS_ENABLED(BLK)
Simon Glasscaa4daa2020-12-03 16:55:18 -0700478 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass33fb2112016-05-01 13:52:41 -0600479#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700480 int dev_num = block_dev->devnum;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700481 int err;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700482 lbaint_t cur, blocks_todo = blkcnt;
Marek Vasut145429a2020-04-04 12:45:05 +0200483 uint b_max;
Andy Fleming272cc702008-10-30 16:41:01 -0500484
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700485 if (blkcnt == 0)
486 return 0;
487
488 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500489 if (!mmc)
490 return 0;
491
Marek Vasutb5b838f2016-12-01 02:06:33 +0100492 if (CONFIG_IS_ENABLED(MMC_TINY))
493 err = mmc_switch_part(mmc, block_dev->hwpart);
494 else
495 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
496
Stephen Warren873cc1d2015-12-07 11:38:49 -0700497 if (err < 0)
498 return 0;
499
Simon Glassc40fdca2016-05-01 13:52:35 -0600500 if ((start + blkcnt) > block_dev->lba) {
Paul Burton56196822013-09-04 16:12:25 +0100501#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100502 pr_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
503 start + blkcnt, block_dev->lba);
Paul Burton56196822013-09-04 16:12:25 +0100504#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800505 return 0;
506 }
Andy Fleming272cc702008-10-30 16:41:01 -0500507
Simon Glass11692992015-06-23 15:38:50 -0600508 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900509 pr_debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500510 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600511 }
Andy Fleming272cc702008-10-30 16:41:01 -0500512
Marek Vasut145429a2020-04-04 12:45:05 +0200513 b_max = mmc_get_b_max(mmc, dst, blkcnt);
514
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700515 do {
Marek Vasut145429a2020-04-04 12:45:05 +0200516 cur = (blocks_todo > b_max) ? b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600517 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900518 pr_debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700519 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600520 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700521 blocks_todo -= cur;
522 start += cur;
523 dst += cur * mmc->read_bl_len;
524 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500525
526 return blkcnt;
527}
528
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000529static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500530{
531 struct mmc_cmd cmd;
532 int err;
533
534 udelay(1000);
535
536 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
537 cmd.cmdarg = 0;
538 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500539
540 err = mmc_send_cmd(mmc, &cmd, NULL);
541
542 if (err)
543 return err;
544
545 udelay(2000);
546
547 return 0;
548}
549
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100550#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200551static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
552{
553 struct mmc_cmd cmd;
554 int err = 0;
555
556 /*
557 * Send CMD11 only if the request is to switch the card to
558 * 1.8V signalling.
559 */
560 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
561 return mmc_set_signal_voltage(mmc, signal_voltage);
562
563 cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
564 cmd.cmdarg = 0;
565 cmd.resp_type = MMC_RSP_R1;
566
567 err = mmc_send_cmd(mmc, &cmd, NULL);
568 if (err)
569 return err;
570
571 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
572 return -EIO;
573
574 /*
575 * The card should drive cmd and dat[0:3] low immediately
576 * after the response of cmd11, but wait 100 us to be sure
577 */
578 err = mmc_wait_dat0(mmc, 0, 100);
579 if (err == -ENOSYS)
580 udelay(100);
581 else if (err)
582 return -ETIMEDOUT;
583
584 /*
585 * During a signal voltage level switch, the clock must be gated
586 * for 5 ms according to the SD spec
587 */
Jaehoon Chung65117182018-01-26 19:25:29 +0900588 mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200589
590 err = mmc_set_signal_voltage(mmc, signal_voltage);
591 if (err)
592 return err;
593
594 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
595 mdelay(10);
Jaehoon Chung65117182018-01-26 19:25:29 +0900596 mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200597
598 /*
599 * Failure to switch is indicated by the card holding
600 * dat[0:3] low. Wait for at least 1 ms according to spec
601 */
602 err = mmc_wait_dat0(mmc, 1, 1000);
603 if (err == -ENOSYS)
604 udelay(1000);
605 else if (err)
606 return -ETIMEDOUT;
607
608 return 0;
609}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100610#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200611
612static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
Andy Fleming272cc702008-10-30 16:41:01 -0500613{
614 int timeout = 1000;
615 int err;
616 struct mmc_cmd cmd;
617
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500618 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500619 cmd.cmdidx = MMC_CMD_APP_CMD;
620 cmd.resp_type = MMC_RSP_R1;
621 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500622
623 err = mmc_send_cmd(mmc, &cmd, NULL);
624
625 if (err)
626 return err;
627
628 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
629 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100630
631 /*
632 * Most cards do not answer if some reserved bits
633 * in the ocr are set. However, Some controller
634 * can set bit 7 (reserved for low voltages), but
635 * how to manage low voltages SD card is not yet
636 * specified.
637 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000638 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200639 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500640
641 if (mmc->version == SD_VERSION_2)
642 cmd.cmdarg |= OCR_HCS;
643
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200644 if (uhs_en)
645 cmd.cmdarg |= OCR_S18R;
646
Andy Fleming272cc702008-10-30 16:41:01 -0500647 err = mmc_send_cmd(mmc, &cmd, NULL);
648
649 if (err)
650 return err;
651
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500652 if (cmd.response[0] & OCR_BUSY)
653 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500654
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500655 if (timeout-- <= 0)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900656 return -EOPNOTSUPP;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500657
658 udelay(1000);
659 }
Andy Fleming272cc702008-10-30 16:41:01 -0500660
661 if (mmc->version != SD_VERSION_2)
662 mmc->version = SD_VERSION_1_0;
663
Thomas Choud52ebf12010-12-24 13:12:21 +0000664 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
665 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
666 cmd.resp_type = MMC_RSP_R3;
667 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000668
669 err = mmc_send_cmd(mmc, &cmd, NULL);
670
671 if (err)
672 return err;
673 }
674
Rabin Vincent998be3d2009-04-05 13:30:56 +0530675 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500676
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100677#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200678 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
679 == 0x41000000) {
680 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
681 if (err)
682 return err;
683 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100684#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200685
Andy Fleming272cc702008-10-30 16:41:01 -0500686 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
687 mmc->rca = 0;
688
689 return 0;
690}
691
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500692static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500693{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500694 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500695 int err;
696
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500697 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
698 cmd.resp_type = MMC_RSP_R3;
699 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500700 if (use_arg && !mmc_host_is_spi(mmc))
701 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200702 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500703 (mmc->ocr & OCR_VOLTAGE_MASK)) |
704 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000705
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500706 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000707 if (err)
708 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500709 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000710 return 0;
711}
712
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200713static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000714{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000715 int err, i;
Haibo Chenfe959052020-06-15 17:18:12 +0800716 int timeout = 1000;
717 uint start;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000718
Andy Fleming272cc702008-10-30 16:41:01 -0500719 /* Some cards seem to need this */
720 mmc_go_idle(mmc);
721
Haibo Chenfe959052020-06-15 17:18:12 +0800722 start = get_timer(0);
Wolfgang Denk0cf207e2021-09-27 17:42:39 +0200723 /* Asking to the card its capabilities */
Haibo Chenfe959052020-06-15 17:18:12 +0800724 for (i = 0; ; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500725 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500726 if (err)
727 return err;
728
Che-Liang Chioue9550442012-11-28 15:21:13 +0000729 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500730 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500731 break;
Haibo Chenfe959052020-06-15 17:18:12 +0800732
733 if (get_timer(start) > timeout)
734 return -ETIMEDOUT;
735 udelay(100);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000736 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500737 mmc->op_cond_pending = 1;
738 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000739}
Andy Fleming272cc702008-10-30 16:41:01 -0500740
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200741static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000742{
743 struct mmc_cmd cmd;
744 int timeout = 1000;
Vipul Kumar36332b62018-05-03 12:20:54 +0530745 ulong start;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000746 int err;
747
748 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500749 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lud188b112016-08-02 15:33:18 +0800750 /* Some cards seem to need this */
751 mmc_go_idle(mmc);
752
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500753 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500754 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500755 err = mmc_send_op_cond_iter(mmc, 1);
756 if (err)
757 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500758 if (mmc->ocr & OCR_BUSY)
759 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500760 if (get_timer(start) > timeout)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900761 return -EOPNOTSUPP;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500762 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500763 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500764 }
Andy Fleming272cc702008-10-30 16:41:01 -0500765
Thomas Choud52ebf12010-12-24 13:12:21 +0000766 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
767 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
768 cmd.resp_type = MMC_RSP_R3;
769 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000770
771 err = mmc_send_cmd(mmc, &cmd, NULL);
772
773 if (err)
774 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500775
776 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000777 }
778
Andy Fleming272cc702008-10-30 16:41:01 -0500779 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500780
781 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700782 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500783
784 return 0;
785}
786
787
Heinrich Schuchardt1601ea22020-03-30 07:24:17 +0200788int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500789{
790 struct mmc_cmd cmd;
791 struct mmc_data data;
792 int err;
793
794 /* Get the Card Status Register */
795 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
796 cmd.resp_type = MMC_RSP_R1;
797 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500798
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000799 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500800 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000801 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500802 data.flags = MMC_DATA_READ;
803
804 err = mmc_send_cmd(mmc, &cmd, &data);
805
806 return err;
807}
808
Marek Vasut68925502019-02-06 11:34:27 +0100809static int __mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value,
810 bool send_status)
Andy Fleming272cc702008-10-30 16:41:01 -0500811{
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200812 unsigned int status, start;
Andy Fleming272cc702008-10-30 16:41:01 -0500813 struct mmc_cmd cmd;
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300814 int timeout_ms = DEFAULT_CMD6_TIMEOUT_MS;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200815 bool is_part_switch = (set == EXT_CSD_CMD_SET_NORMAL) &&
816 (index == EXT_CSD_PART_CONF);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000817 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500818
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200819 if (mmc->gen_cmd6_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300820 timeout_ms = mmc->gen_cmd6_time * 10;
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200821
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200822 if (is_part_switch && mmc->part_switch_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300823 timeout_ms = mmc->part_switch_time * 10;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200824
Andy Fleming272cc702008-10-30 16:41:01 -0500825 cmd.cmdidx = MMC_CMD_SWITCH;
826 cmd.resp_type = MMC_RSP_R1b;
827 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000828 (index << 16) |
829 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500830
Sean Andersonda129172020-10-17 08:36:27 -0400831 ret = mmc_send_cmd_retry(mmc, &cmd, NULL, 3);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200832 if (ret)
833 return ret;
834
835 start = get_timer(0);
836
837 /* poll dat0 for rdy/buys status */
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300838 ret = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200839 if (ret && ret != -ENOSYS)
840 return ret;
841
842 /*
Kirill Kapranov44645f82021-10-09 23:49:59 +0300843 * In cases when neiter allowed to poll by using CMD13 nor we are
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200844 * capable of polling by using mmc_wait_dat0, then rely on waiting the
845 * stated timeout to be sufficient.
846 */
Kirill Kapranov44645f82021-10-09 23:49:59 +0300847 if (ret == -ENOSYS && !send_status) {
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300848 mdelay(timeout_ms);
Haibo Chenef5ab0d2020-09-22 18:11:42 +0800849 return 0;
850 }
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200851
Marek Vasut2349ecf2022-07-15 01:58:24 +0200852 if (!send_status)
853 return 0;
854
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200855 /* Finally wait until the card is ready or indicates a failure
856 * to switch. It doesn't hurt to use CMD13 here even if send_status
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300857 * is false, because by now (after 'timeout_ms' ms) the bus should be
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200858 * reliable.
859 */
860 do {
861 ret = mmc_send_status(mmc, &status);
862
863 if (!ret && (status & MMC_STATUS_SWITCH_ERROR)) {
864 pr_debug("switch failed %d/%d/0x%x !\n", set, index,
865 value);
866 return -EIO;
Maxime Riparda9003dc2016-11-04 16:18:08 +0100867 }
Stefan Bosch8e2b0af2021-01-23 13:37:41 +0100868 if (!ret && (status & MMC_STATUS_RDY_FOR_DATA) &&
869 (status & MMC_STATUS_CURR_STATE) == MMC_STATE_TRANS)
Marek Vasut68925502019-02-06 11:34:27 +0100870 return 0;
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200871 udelay(100);
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300872 } while (get_timer(start) < timeout_ms);
Marek Vasut68925502019-02-06 11:34:27 +0100873
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200874 return -ETIMEDOUT;
Andy Fleming272cc702008-10-30 16:41:01 -0500875}
876
Marek Vasut68925502019-02-06 11:34:27 +0100877int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
878{
879 return __mmc_switch(mmc, set, index, value, true);
880}
881
Heinrich Schuchardt0469d842020-03-30 07:24:19 +0200882int mmc_boot_wp(struct mmc *mmc)
883{
884 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, 1);
885}
886
Ying-Chun Liu (PaulLiu)19a29ff2022-04-25 21:59:02 +0800887int mmc_boot_wp_single_partition(struct mmc *mmc, int partition)
888{
889 u8 value;
890 int ret;
891
892 value = EXT_CSD_BOOT_WP_B_PWR_WP_EN;
893
894 if (partition == 0) {
895 value |= EXT_CSD_BOOT_WP_B_SEC_WP_SEL;
896 ret = mmc_switch(mmc,
897 EXT_CSD_CMD_SET_NORMAL,
898 EXT_CSD_BOOT_WP,
899 value);
900 } else if (partition == 1) {
901 value |= EXT_CSD_BOOT_WP_B_SEC_WP_SEL;
902 value |= EXT_CSD_BOOT_WP_B_PWR_WP_SEC_SEL;
903 ret = mmc_switch(mmc,
904 EXT_CSD_CMD_SET_NORMAL,
905 EXT_CSD_BOOT_WP,
906 value);
907 } else {
908 ret = mmc_boot_wp(mmc);
909 }
910
911 return ret;
912}
913
Marek Vasut62d77ce2018-04-15 00:37:11 +0200914#if !CONFIG_IS_ENABLED(MMC_TINY)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100915static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode,
916 bool hsdowngrade)
Andy Fleming272cc702008-10-30 16:41:01 -0500917{
Andy Fleming272cc702008-10-30 16:41:01 -0500918 int err;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200919 int speed_bits;
920
921 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
922
923 switch (mode) {
924 case MMC_HS:
925 case MMC_HS_52:
926 case MMC_DDR_52:
927 speed_bits = EXT_CSD_TIMING_HS;
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200928 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100929#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200930 case MMC_HS_200:
931 speed_bits = EXT_CSD_TIMING_HS200;
932 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100933#endif
Peng Fan3dd26262018-08-10 14:07:54 +0800934#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
935 case MMC_HS_400:
936 speed_bits = EXT_CSD_TIMING_HS400;
937 break;
938#endif
Peng Fan44acd492019-07-10 14:43:07 +0800939#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
940 case MMC_HS_400_ES:
941 speed_bits = EXT_CSD_TIMING_HS400;
942 break;
943#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200944 case MMC_LEGACY:
945 speed_bits = EXT_CSD_TIMING_LEGACY;
946 break;
947 default:
948 return -EINVAL;
949 }
Marek Vasut68925502019-02-06 11:34:27 +0100950
951 err = __mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
952 speed_bits, !hsdowngrade);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200953 if (err)
954 return err;
955
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100956#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
957 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
958 /*
959 * In case the eMMC is in HS200/HS400 mode and we are downgrading
960 * to HS mode, the card clock are still running much faster than
961 * the supported HS mode clock, so we can not reliably read out
962 * Extended CSD. Reconfigure the controller to run at HS mode.
963 */
964 if (hsdowngrade) {
965 mmc_select_mode(mmc, MMC_HS);
966 mmc_set_clock(mmc, mmc_mode2freq(mmc, MMC_HS), false);
967 }
968#endif
969
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200970 if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
971 /* Now check to see that it worked */
972 err = mmc_send_ext_csd(mmc, test_csd);
973 if (err)
974 return err;
975
976 /* No high-speed support */
977 if (!test_csd[EXT_CSD_HS_TIMING])
978 return -ENOTSUPP;
979 }
980
981 return 0;
982}
983
984static int mmc_get_capabilities(struct mmc *mmc)
985{
986 u8 *ext_csd = mmc->ext_csd;
987 char cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500988
Jean-Jacques Hiblot00e446f2017-11-30 17:43:56 +0100989 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -0500990
Thomas Choud52ebf12010-12-24 13:12:21 +0000991 if (mmc_host_is_spi(mmc))
992 return 0;
993
Andy Fleming272cc702008-10-30 16:41:01 -0500994 /* Only version 4 supports high-speed */
995 if (mmc->version < MMC_VERSION_4)
996 return 0;
997
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200998 if (!ext_csd) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100999 pr_err("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001000 return -ENOTSUPP;
1001 }
1002
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -06001003 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
1004
Peng Fan3dd26262018-08-10 14:07:54 +08001005 cardtype = ext_csd[EXT_CSD_CARD_TYPE];
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001006 mmc->cardtype = cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -05001007
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001008#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001009 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1010 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
1011 mmc->card_caps |= MMC_MODE_HS200;
1012 }
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001013#endif
Peng Fan44acd492019-07-10 14:43:07 +08001014#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
1015 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Peng Fan3dd26262018-08-10 14:07:54 +08001016 if (cardtype & (EXT_CSD_CARD_TYPE_HS400_1_2V |
1017 EXT_CSD_CARD_TYPE_HS400_1_8V)) {
1018 mmc->card_caps |= MMC_MODE_HS400;
1019 }
1020#endif
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001021 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001022 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001023 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001024 mmc->card_caps |= MMC_MODE_HS_52MHz;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001025 }
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001026 if (cardtype & EXT_CSD_CARD_TYPE_26)
1027 mmc->card_caps |= MMC_MODE_HS;
Andy Fleming272cc702008-10-30 16:41:01 -05001028
Peng Fan44acd492019-07-10 14:43:07 +08001029#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1030 if (ext_csd[EXT_CSD_STROBE_SUPPORT] &&
1031 (mmc->card_caps & MMC_MODE_HS400)) {
1032 mmc->card_caps |= MMC_MODE_HS400_ES;
1033 }
1034#endif
1035
Andy Fleming272cc702008-10-30 16:41:01 -05001036 return 0;
1037}
Marek Vasut62d77ce2018-04-15 00:37:11 +02001038#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001039
Stephen Warrenf866a462013-06-11 15:14:01 -06001040static int mmc_set_capacity(struct mmc *mmc, int part_num)
1041{
1042 switch (part_num) {
1043 case 0:
1044 mmc->capacity = mmc->capacity_user;
1045 break;
1046 case 1:
1047 case 2:
1048 mmc->capacity = mmc->capacity_boot;
1049 break;
1050 case 3:
1051 mmc->capacity = mmc->capacity_rpmb;
1052 break;
1053 case 4:
1054 case 5:
1055 case 6:
1056 case 7:
1057 mmc->capacity = mmc->capacity_gp[part_num - 4];
1058 break;
1059 default:
1060 return -1;
1061 }
1062
Simon Glassc40fdca2016-05-01 13:52:35 -06001063 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -06001064
1065 return 0;
1066}
1067
Simon Glass7dba0b92016-06-12 23:30:15 -06001068int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +00001069{
Stephen Warrenf866a462013-06-11 15:14:01 -06001070 int ret;
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +02001071 int retry = 3;
Lei Wenbc897b12011-05-02 16:26:26 +00001072
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +02001073 do {
1074 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1075 EXT_CSD_PART_CONF,
1076 (mmc->part_config & ~PART_ACCESS_MASK)
1077 | (part_num & PART_ACCESS_MASK));
1078 } while (ret && retry--);
Stephen Warrenf866a462013-06-11 15:14:01 -06001079
Peter Bigot6dc93e72014-09-02 18:31:23 -05001080 /*
1081 * Set the capacity if the switch succeeded or was intended
1082 * to return to representing the raw device.
1083 */
Stephen Warren873cc1d2015-12-07 11:38:49 -07001084 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -05001085 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -06001086 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -07001087 }
Peter Bigot6dc93e72014-09-02 18:31:23 -05001088
1089 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +00001090}
1091
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001092#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001093int mmc_hwpart_config(struct mmc *mmc,
1094 const struct mmc_hwpart_conf *conf,
1095 enum mmc_hwpart_conf_mode mode)
1096{
1097 u8 part_attrs = 0;
1098 u32 enh_size_mult;
1099 u32 enh_start_addr;
1100 u32 gp_size_mult[4];
1101 u32 max_enh_size_mult;
1102 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001103 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001104 int i, pidx, err;
1105 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1106
1107 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
1108 return -EINVAL;
1109
1110 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001111 pr_err("eMMC >= 4.4 required for enhanced user data area\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001112 return -EMEDIUMTYPE;
1113 }
1114
1115 if (!(mmc->part_support & PART_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001116 pr_err("Card does not support partitioning\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001117 return -EMEDIUMTYPE;
1118 }
1119
1120 if (!mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001121 pr_err("Card does not define HC WP group size\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001122 return -EMEDIUMTYPE;
1123 }
1124
1125 /* check partition alignment and total enhanced size */
1126 if (conf->user.enh_size) {
1127 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
1128 conf->user.enh_start % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001129 pr_err("User data enhanced area not HC WP group "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001130 "size aligned\n");
1131 return -EINVAL;
1132 }
1133 part_attrs |= EXT_CSD_ENH_USR;
1134 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
1135 if (mmc->high_capacity) {
1136 enh_start_addr = conf->user.enh_start;
1137 } else {
1138 enh_start_addr = (conf->user.enh_start << 9);
1139 }
1140 } else {
1141 enh_size_mult = 0;
1142 enh_start_addr = 0;
1143 }
1144 tot_enh_size_mult += enh_size_mult;
1145
1146 for (pidx = 0; pidx < 4; pidx++) {
1147 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001148 pr_err("GP%i partition not HC WP group size "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001149 "aligned\n", pidx+1);
1150 return -EINVAL;
1151 }
1152 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
1153 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
1154 part_attrs |= EXT_CSD_ENH_GP(pidx);
1155 tot_enh_size_mult += gp_size_mult[pidx];
1156 }
1157 }
1158
1159 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001160 pr_err("Card does not support enhanced attribute\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001161 return -EMEDIUMTYPE;
1162 }
1163
1164 err = mmc_send_ext_csd(mmc, ext_csd);
1165 if (err)
1166 return err;
1167
1168 max_enh_size_mult =
1169 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
1170 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
1171 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
1172 if (tot_enh_size_mult > max_enh_size_mult) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001173 pr_err("Total enhanced size exceeds maximum (%u > %u)\n",
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001174 tot_enh_size_mult, max_enh_size_mult);
1175 return -EMEDIUMTYPE;
1176 }
1177
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001178 /* The default value of EXT_CSD_WR_REL_SET is device
1179 * dependent, the values can only be changed if the
1180 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
1181 * changed only once and before partitioning is completed. */
1182 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1183 if (conf->user.wr_rel_change) {
1184 if (conf->user.wr_rel_set)
1185 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
1186 else
1187 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
1188 }
1189 for (pidx = 0; pidx < 4; pidx++) {
1190 if (conf->gp_part[pidx].wr_rel_change) {
1191 if (conf->gp_part[pidx].wr_rel_set)
1192 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
1193 else
1194 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
1195 }
1196 }
1197
1198 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
1199 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
1200 puts("Card does not support host controlled partition write "
1201 "reliability settings\n");
1202 return -EMEDIUMTYPE;
1203 }
1204
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001205 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
1206 EXT_CSD_PARTITION_SETTING_COMPLETED) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001207 pr_err("Card already partitioned\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001208 return -EPERM;
1209 }
1210
1211 if (mode == MMC_HWPART_CONF_CHECK)
1212 return 0;
1213
1214 /* Partitioning requires high-capacity size definitions */
1215 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
1216 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1217 EXT_CSD_ERASE_GROUP_DEF, 1);
1218
1219 if (err)
1220 return err;
1221
1222 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1223
Jaehoon Chung4af66592020-01-17 15:06:54 +09001224#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001225 /* update erase group size to be high-capacity */
1226 mmc->erase_grp_size =
1227 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jaehoon Chung4af66592020-01-17 15:06:54 +09001228#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001229
1230 }
1231
1232 /* all OK, write the configuration */
1233 for (i = 0; i < 4; i++) {
1234 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1235 EXT_CSD_ENH_START_ADDR+i,
1236 (enh_start_addr >> (i*8)) & 0xFF);
1237 if (err)
1238 return err;
1239 }
1240 for (i = 0; i < 3; i++) {
1241 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1242 EXT_CSD_ENH_SIZE_MULT+i,
1243 (enh_size_mult >> (i*8)) & 0xFF);
1244 if (err)
1245 return err;
1246 }
1247 for (pidx = 0; pidx < 4; pidx++) {
1248 for (i = 0; i < 3; i++) {
1249 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1250 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1251 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1252 if (err)
1253 return err;
1254 }
1255 }
1256 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1257 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1258 if (err)
1259 return err;
1260
1261 if (mode == MMC_HWPART_CONF_SET)
1262 return 0;
1263
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001264 /* The WR_REL_SET is a write-once register but shall be
1265 * written before setting PART_SETTING_COMPLETED. As it is
1266 * write-once we can only write it when completing the
1267 * partitioning. */
1268 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1269 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1270 EXT_CSD_WR_REL_SET, wr_rel_set);
1271 if (err)
1272 return err;
1273 }
1274
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001275 /* Setting PART_SETTING_COMPLETED confirms the partition
1276 * configuration but it only becomes effective after power
1277 * cycle, so we do not adjust the partition related settings
1278 * in the mmc struct. */
1279
1280 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1281 EXT_CSD_PARTITION_SETTING,
1282 EXT_CSD_PARTITION_SETTING_COMPLETED);
1283 if (err)
1284 return err;
1285
1286 return 0;
1287}
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001288#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001289
Simon Glasse7881d82017-07-29 11:35:31 -06001290#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +00001291int mmc_getcd(struct mmc *mmc)
1292{
1293 int cd;
1294
1295 cd = board_mmc_getcd(mmc);
1296
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001297 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001298 if (mmc->cfg->ops->getcd)
1299 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001300 else
1301 cd = 1;
1302 }
Thierry Reding48972d92012-01-02 01:15:37 +00001303
1304 return cd;
1305}
Simon Glass8ca51e52016-06-12 23:30:22 -06001306#endif
Thierry Reding48972d92012-01-02 01:15:37 +00001307
Marek Vasut62d77ce2018-04-15 00:37:11 +02001308#if !CONFIG_IS_ENABLED(MMC_TINY)
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001309static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -05001310{
1311 struct mmc_cmd cmd;
1312 struct mmc_data data;
1313
1314 /* Switch the frequency */
1315 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1316 cmd.resp_type = MMC_RSP_R1;
1317 cmd.cmdarg = (mode << 31) | 0xffffff;
1318 cmd.cmdarg &= ~(0xf << (group * 4));
1319 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -05001320
1321 data.dest = (char *)resp;
1322 data.blocksize = 64;
1323 data.blocks = 1;
1324 data.flags = MMC_DATA_READ;
1325
1326 return mmc_send_cmd(mmc, &cmd, &data);
1327}
1328
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001329static int sd_get_capabilities(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001330{
1331 int err;
1332 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +05301333 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1334 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -05001335 struct mmc_data data;
1336 int timeout;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001337#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001338 u32 sd3_bus_mode;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001339#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001340
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301341 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05001342
Thomas Choud52ebf12010-12-24 13:12:21 +00001343 if (mmc_host_is_spi(mmc))
1344 return 0;
1345
Andy Fleming272cc702008-10-30 16:41:01 -05001346 /* Read the SCR to find out if this card supports higher speeds */
1347 cmd.cmdidx = MMC_CMD_APP_CMD;
1348 cmd.resp_type = MMC_RSP_R1;
1349 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001350
1351 err = mmc_send_cmd(mmc, &cmd, NULL);
1352
1353 if (err)
1354 return err;
1355
1356 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1357 cmd.resp_type = MMC_RSP_R1;
1358 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001359
Anton staaff781dd32011-10-03 13:54:59 +00001360 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -05001361 data.blocksize = 8;
1362 data.blocks = 1;
1363 data.flags = MMC_DATA_READ;
1364
Sean Andersonda129172020-10-17 08:36:27 -04001365 err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);
Andy Fleming272cc702008-10-30 16:41:01 -05001366
Sean Andersonda129172020-10-17 08:36:27 -04001367 if (err)
Andy Fleming272cc702008-10-30 16:41:01 -05001368 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001369
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001370 mmc->scr[0] = __be32_to_cpu(scr[0]);
1371 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -05001372
1373 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -07001374 case 0:
1375 mmc->version = SD_VERSION_1_0;
1376 break;
1377 case 1:
1378 mmc->version = SD_VERSION_1_10;
1379 break;
1380 case 2:
1381 mmc->version = SD_VERSION_2;
1382 if ((mmc->scr[0] >> 15) & 0x1)
1383 mmc->version = SD_VERSION_3;
1384 break;
1385 default:
1386 mmc->version = SD_VERSION_1_0;
1387 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001388 }
1389
Alagu Sankarb44c7082010-05-12 15:08:24 +05301390 if (mmc->scr[0] & SD_DATA_4BIT)
1391 mmc->card_caps |= MMC_MODE_4BIT;
1392
Andy Fleming272cc702008-10-30 16:41:01 -05001393 /* Version 1.0 doesn't support switching */
1394 if (mmc->version == SD_VERSION_1_0)
1395 return 0;
1396
1397 timeout = 4;
1398 while (timeout--) {
1399 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +00001400 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001401
1402 if (err)
1403 return err;
1404
1405 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001406 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -05001407 break;
1408 }
1409
Andy Fleming272cc702008-10-30 16:41:01 -05001410 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001411 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1412 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Fleming272cc702008-10-30 16:41:01 -05001413
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001414#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001415 /* Version before 3.0 don't support UHS modes */
1416 if (mmc->version < SD_VERSION_3)
1417 return 0;
1418
1419 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1420 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1421 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1422 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1423 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1424 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1425 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1426 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1427 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1428 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1429 mmc->card_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001430#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001431
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001432 return 0;
1433}
1434
1435static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1436{
1437 int err;
1438
1439 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001440 int speed;
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001441
Marek Vasutcf345762018-11-18 03:25:08 +01001442 /* SD version 1.00 and 1.01 does not support CMD 6 */
1443 if (mmc->version == SD_VERSION_1_0)
1444 return 0;
1445
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001446 switch (mode) {
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301447 case MMC_LEGACY:
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001448 speed = UHS_SDR12_BUS_SPEED;
1449 break;
1450 case SD_HS:
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001451 speed = HIGH_SPEED_BUS_SPEED;
1452 break;
1453#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1454 case UHS_SDR12:
1455 speed = UHS_SDR12_BUS_SPEED;
1456 break;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001457 case UHS_SDR25:
1458 speed = UHS_SDR25_BUS_SPEED;
1459 break;
1460 case UHS_SDR50:
1461 speed = UHS_SDR50_BUS_SPEED;
1462 break;
1463 case UHS_DDR50:
1464 speed = UHS_DDR50_BUS_SPEED;
1465 break;
1466 case UHS_SDR104:
1467 speed = UHS_SDR104_BUS_SPEED;
1468 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001469#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001470 default:
1471 return -EINVAL;
1472 }
1473
1474 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001475 if (err)
1476 return err;
1477
Jean-Jacques Hiblota0276f32018-02-09 12:09:27 +01001478 if (((__be32_to_cpu(switch_status[4]) >> 24) & 0xF) != speed)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001479 return -ENOTSUPP;
1480
1481 return 0;
1482}
1483
Marek Vasutec360e62018-04-15 00:36:45 +02001484static int sd_select_bus_width(struct mmc *mmc, int w)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001485{
1486 int err;
1487 struct mmc_cmd cmd;
1488
1489 if ((w != 4) && (w != 1))
1490 return -EINVAL;
1491
1492 cmd.cmdidx = MMC_CMD_APP_CMD;
1493 cmd.resp_type = MMC_RSP_R1;
1494 cmd.cmdarg = mmc->rca << 16;
1495
1496 err = mmc_send_cmd(mmc, &cmd, NULL);
1497 if (err)
1498 return err;
1499
1500 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1501 cmd.resp_type = MMC_RSP_R1;
1502 if (w == 4)
1503 cmd.cmdarg = 2;
1504 else if (w == 1)
1505 cmd.cmdarg = 0;
1506 err = mmc_send_cmd(mmc, &cmd, NULL);
1507 if (err)
1508 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001509
1510 return 0;
1511}
Marek Vasut62d77ce2018-04-15 00:37:11 +02001512#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001513
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001514#if CONFIG_IS_ENABLED(MMC_WRITE)
Peng Fan3697e592016-09-01 11:13:38 +08001515static int sd_read_ssr(struct mmc *mmc)
1516{
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001517 static const unsigned int sd_au_size[] = {
1518 0, SZ_16K / 512, SZ_32K / 512,
1519 SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
1520 SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
1521 SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
1522 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512,
1523 SZ_64M / 512,
1524 };
Peng Fan3697e592016-09-01 11:13:38 +08001525 int err, i;
1526 struct mmc_cmd cmd;
1527 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1528 struct mmc_data data;
Peng Fan3697e592016-09-01 11:13:38 +08001529 unsigned int au, eo, et, es;
1530
1531 cmd.cmdidx = MMC_CMD_APP_CMD;
1532 cmd.resp_type = MMC_RSP_R1;
1533 cmd.cmdarg = mmc->rca << 16;
1534
Sean Andersonda129172020-10-17 08:36:27 -04001535 err = mmc_send_cmd_quirks(mmc, &cmd, NULL, MMC_QUIRK_RETRY_APP_CMD, 4);
Peng Fan3697e592016-09-01 11:13:38 +08001536 if (err)
1537 return err;
1538
1539 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1540 cmd.resp_type = MMC_RSP_R1;
1541 cmd.cmdarg = 0;
1542
Peng Fan3697e592016-09-01 11:13:38 +08001543 data.dest = (char *)ssr;
1544 data.blocksize = 64;
1545 data.blocks = 1;
1546 data.flags = MMC_DATA_READ;
1547
Sean Andersonda129172020-10-17 08:36:27 -04001548 err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);
1549 if (err)
Peng Fan3697e592016-09-01 11:13:38 +08001550 return err;
Peng Fan3697e592016-09-01 11:13:38 +08001551
1552 for (i = 0; i < 16; i++)
1553 ssr[i] = be32_to_cpu(ssr[i]);
1554
1555 au = (ssr[2] >> 12) & 0xF;
1556 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1557 mmc->ssr.au = sd_au_size[au];
1558 es = (ssr[3] >> 24) & 0xFF;
1559 es |= (ssr[2] & 0xFF) << 8;
1560 et = (ssr[3] >> 18) & 0x3F;
1561 if (es && et) {
1562 eo = (ssr[3] >> 16) & 0x3;
1563 mmc->ssr.erase_timeout = (et * 1000) / es;
1564 mmc->ssr.erase_offset = eo * 1000;
1565 }
1566 } else {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001567 pr_debug("Invalid Allocation Unit Size.\n");
Peng Fan3697e592016-09-01 11:13:38 +08001568 }
1569
1570 return 0;
1571}
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001572#endif
Heinrich Schuchardtf9a86fb2024-01-04 04:49:42 +01001573/*
1574 * TRAN_SPEED bits 0:2 encode the frequency unit:
1575 * 0 = 100KHz, 1 = 1MHz, 2 = 10MHz, 3 = 100MHz, values 4 - 7 are reserved.
1576 * The values in fbase[] are divided by 10 to avoid floats in multiplier[].
1577 */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001578static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001579 10000,
1580 100000,
1581 1000000,
1582 10000000,
Heinrich Schuchardtf9a86fb2024-01-04 04:49:42 +01001583 0, /* reserved */
1584 0, /* reserved */
1585 0, /* reserved */
1586 0, /* reserved */
Andy Fleming272cc702008-10-30 16:41:01 -05001587};
1588
1589/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1590 * to platforms without floating point.
1591 */
Simon Glass61fe0762016-05-14 14:02:57 -06001592static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001593 0, /* reserved */
1594 10,
1595 12,
1596 13,
1597 15,
1598 20,
1599 25,
1600 30,
1601 35,
1602 40,
1603 45,
1604 50,
1605 55,
1606 60,
1607 70,
1608 80,
1609};
1610
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001611static inline int bus_width(uint cap)
1612{
1613 if (cap == MMC_MODE_8BIT)
1614 return 8;
1615 if (cap == MMC_MODE_4BIT)
1616 return 4;
1617 if (cap == MMC_MODE_1BIT)
1618 return 1;
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001619 pr_warn("invalid bus witdh capability 0x%x\n", cap);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001620 return 0;
1621}
1622
Simon Glasse7881d82017-07-29 11:35:31 -06001623#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001624#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001625static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1626{
1627 return -ENOTSUPP;
1628}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001629#endif
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001630
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001631static int mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001632{
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001633 int ret = 0;
1634
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001635 if (mmc->cfg->ops->set_ios)
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001636 ret = mmc->cfg->ops->set_ios(mmc);
1637
1638 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001639}
Yann Gautier3602a562019-09-19 17:56:12 +02001640
1641static int mmc_host_power_cycle(struct mmc *mmc)
1642{
1643 int ret = 0;
1644
1645 if (mmc->cfg->ops->host_power_cycle)
1646 ret = mmc->cfg->ops->host_power_cycle(mmc);
1647
1648 return ret;
1649}
Simon Glass8ca51e52016-06-12 23:30:22 -06001650#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001651
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001652int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
Andy Fleming272cc702008-10-30 16:41:01 -05001653{
Jaehoon Chungc0fafe62018-01-23 14:04:30 +09001654 if (!disable) {
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001655 if (clock > mmc->cfg->f_max)
1656 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001657
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001658 if (clock < mmc->cfg->f_min)
1659 clock = mmc->cfg->f_min;
1660 }
Andy Fleming272cc702008-10-30 16:41:01 -05001661
1662 mmc->clock = clock;
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001663 mmc->clk_disable = disable;
Andy Fleming272cc702008-10-30 16:41:01 -05001664
Jaehoon Chungd2faadb2018-01-26 19:25:30 +09001665 debug("clock is %s (%dHz)\n", disable ? "disabled" : "enabled", clock);
1666
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001667 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001668}
1669
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001670static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001671{
1672 mmc->bus_width = width;
1673
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001674 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001675}
1676
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001677#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1678/*
1679 * helper function to display the capabilities in a human
1680 * friendly manner. The capabilities include bus width and
1681 * supported modes.
1682 */
1683void mmc_dump_capabilities(const char *text, uint caps)
1684{
1685 enum bus_mode mode;
1686
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001687 pr_debug("%s: widths [", text);
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001688 if (caps & MMC_MODE_8BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001689 pr_debug("8, ");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001690 if (caps & MMC_MODE_4BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001691 pr_debug("4, ");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001692 if (caps & MMC_MODE_1BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001693 pr_debug("1, ");
1694 pr_debug("\b\b] modes [");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001695 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1696 if (MMC_CAP(mode) & caps)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001697 pr_debug("%s, ", mmc_mode_name(mode));
1698 pr_debug("\b\b]\n");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001699}
1700#endif
1701
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001702struct mode_width_tuning {
1703 enum bus_mode mode;
1704 uint widths;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001705#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001706 uint tuning;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001707#endif
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001708};
1709
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001710#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001711int mmc_voltage_to_mv(enum mmc_voltage voltage)
1712{
1713 switch (voltage) {
1714 case MMC_SIGNAL_VOLTAGE_000: return 0;
1715 case MMC_SIGNAL_VOLTAGE_330: return 3300;
1716 case MMC_SIGNAL_VOLTAGE_180: return 1800;
1717 case MMC_SIGNAL_VOLTAGE_120: return 1200;
1718 }
1719 return -EINVAL;
1720}
1721
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001722static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1723{
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001724 int err;
1725
1726 if (mmc->signal_voltage == signal_voltage)
1727 return 0;
1728
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001729 mmc->signal_voltage = signal_voltage;
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001730 err = mmc_set_ios(mmc);
1731 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001732 pr_debug("unable to set voltage (err %d)\n", err);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001733
1734 return err;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001735}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001736#else
1737static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1738{
1739 return 0;
1740}
1741#endif
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001742
Marek Vasut62d77ce2018-04-15 00:37:11 +02001743#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001744static const struct mode_width_tuning sd_modes_by_pref[] = {
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001745#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1746#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001747 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001748 .mode = UHS_SDR104,
1749 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1750 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1751 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001752#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001753 {
1754 .mode = UHS_SDR50,
1755 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1756 },
1757 {
1758 .mode = UHS_DDR50,
1759 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1760 },
1761 {
1762 .mode = UHS_SDR25,
1763 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1764 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001765#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001766 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001767 .mode = SD_HS,
1768 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1769 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001770#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001771 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001772 .mode = UHS_SDR12,
1773 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1774 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001775#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001776 {
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301777 .mode = MMC_LEGACY,
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001778 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1779 }
1780};
1781
1782#define for_each_sd_mode_by_pref(caps, mwt) \
1783 for (mwt = sd_modes_by_pref;\
1784 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1785 mwt++) \
1786 if (caps & MMC_CAP(mwt->mode))
1787
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001788static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001789{
1790 int err;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001791 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1792 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001793#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001794 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001795#else
1796 bool uhs_en = false;
1797#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001798 uint caps;
1799
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001800#ifdef DEBUG
1801 mmc_dump_capabilities("sd card", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001802 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001803#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001804
Anup Patelf49ff792019-07-08 04:10:43 +00001805 if (mmc_host_is_spi(mmc)) {
1806 mmc_set_bus_width(mmc, 1);
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301807 mmc_select_mode(mmc, MMC_LEGACY);
Anup Patelf49ff792019-07-08 04:10:43 +00001808 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
Pragnesh Patel810bc132020-06-29 15:17:26 +05301809#if CONFIG_IS_ENABLED(MMC_WRITE)
1810 err = sd_read_ssr(mmc);
1811 if (err)
1812 pr_warn("unable to read ssr\n");
1813#endif
Anup Patelf49ff792019-07-08 04:10:43 +00001814 return 0;
1815 }
1816
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001817 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001818 caps = card_caps & mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001819
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001820 if (!uhs_en)
1821 caps &= ~UHS_CAPS;
1822
1823 for_each_sd_mode_by_pref(caps, mwt) {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001824 uint *w;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001825
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001826 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001827 if (*w & caps & mwt->widths) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001828 pr_debug("trying mode %s width %d (at %d MHz)\n",
1829 mmc_mode_name(mwt->mode),
1830 bus_width(*w),
1831 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001832
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001833 /* configure the bus width (card + host) */
1834 err = sd_select_bus_width(mmc, bus_width(*w));
1835 if (err)
1836 goto error;
1837 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001838
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001839 /* configure the bus mode (card) */
1840 err = sd_set_card_speed(mmc, mwt->mode);
1841 if (err)
1842 goto error;
1843
1844 /* configure the bus mode (host) */
1845 mmc_select_mode(mmc, mwt->mode);
Jaehoon Chung65117182018-01-26 19:25:29 +09001846 mmc_set_clock(mmc, mmc->tran_speed,
1847 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001848
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001849#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001850 /* execute tuning if needed */
1851 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1852 err = mmc_execute_tuning(mmc,
1853 mwt->tuning);
1854 if (err) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001855 pr_debug("tuning failed\n");
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001856 goto error;
1857 }
1858 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001859#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001860
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001861#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001862 err = sd_read_ssr(mmc);
Peng Fan0a4c2b02018-03-05 16:20:40 +08001863 if (err)
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001864 pr_warn("unable to read ssr\n");
1865#endif
1866 if (!err)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001867 return 0;
1868
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001869error:
1870 /* revert to a safer bus speed */
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301871 mmc_select_mode(mmc, MMC_LEGACY);
Jaehoon Chung65117182018-01-26 19:25:29 +09001872 mmc_set_clock(mmc, mmc->tran_speed,
1873 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001874 }
1875 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001876 }
1877
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001878 pr_err("unable to select a mode\n");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001879 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001880}
1881
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001882/*
1883 * read the compare the part of ext csd that is constant.
1884 * This can be used to check that the transfer is working
1885 * as expected.
1886 */
1887static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1888{
1889 int err;
1890 const u8 *ext_csd = mmc->ext_csd;
1891 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1892
Jean-Jacques Hiblot1de06b92017-11-30 17:43:58 +01001893 if (mmc->version < MMC_VERSION_4)
1894 return 0;
1895
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001896 err = mmc_send_ext_csd(mmc, test_csd);
1897 if (err)
1898 return err;
1899
1900 /* Only compare read only fields */
1901 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1902 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1903 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1904 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1905 ext_csd[EXT_CSD_REV]
1906 == test_csd[EXT_CSD_REV] &&
1907 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1908 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1909 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1910 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1911 return 0;
1912
1913 return -EBADMSG;
1914}
1915
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001916#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001917static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1918 uint32_t allowed_mask)
1919{
1920 u32 card_mask = 0;
1921
1922 switch (mode) {
Peng Fan44acd492019-07-10 14:43:07 +08001923 case MMC_HS_400_ES:
Peng Fan3dd26262018-08-10 14:07:54 +08001924 case MMC_HS_400:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001925 case MMC_HS_200:
Peng Fan3dd26262018-08-10 14:07:54 +08001926 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_8V |
1927 EXT_CSD_CARD_TYPE_HS400_1_8V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001928 card_mask |= MMC_SIGNAL_VOLTAGE_180;
Peng Fan3dd26262018-08-10 14:07:54 +08001929 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1930 EXT_CSD_CARD_TYPE_HS400_1_2V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001931 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1932 break;
1933 case MMC_DDR_52:
1934 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
1935 card_mask |= MMC_SIGNAL_VOLTAGE_330 |
1936 MMC_SIGNAL_VOLTAGE_180;
1937 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
1938 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1939 break;
1940 default:
1941 card_mask |= MMC_SIGNAL_VOLTAGE_330;
1942 break;
1943 }
1944
1945 while (card_mask & allowed_mask) {
1946 enum mmc_voltage best_match;
1947
1948 best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
1949 if (!mmc_set_signal_voltage(mmc, best_match))
1950 return 0;
1951
1952 allowed_mask &= ~best_match;
1953 }
1954
1955 return -ENOTSUPP;
1956}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001957#else
1958static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1959 uint32_t allowed_mask)
1960{
1961 return 0;
1962}
1963#endif
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001964
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001965static const struct mode_width_tuning mmc_modes_by_pref[] = {
Peng Fan44acd492019-07-10 14:43:07 +08001966#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1967 {
1968 .mode = MMC_HS_400_ES,
1969 .widths = MMC_MODE_8BIT,
1970 },
1971#endif
Peng Fan3dd26262018-08-10 14:07:54 +08001972#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1973 {
1974 .mode = MMC_HS_400,
1975 .widths = MMC_MODE_8BIT,
1976 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
1977 },
1978#endif
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001979#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001980 {
1981 .mode = MMC_HS_200,
1982 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001983 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001984 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001985#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001986 {
1987 .mode = MMC_DDR_52,
1988 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1989 },
1990 {
1991 .mode = MMC_HS_52,
1992 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1993 },
1994 {
1995 .mode = MMC_HS,
1996 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1997 },
1998 {
1999 .mode = MMC_LEGACY,
2000 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
2001 }
2002};
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002003
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002004#define for_each_mmc_mode_by_pref(caps, mwt) \
2005 for (mwt = mmc_modes_by_pref;\
2006 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
2007 mwt++) \
2008 if (caps & MMC_CAP(mwt->mode))
2009
2010static const struct ext_csd_bus_width {
2011 uint cap;
2012 bool is_ddr;
2013 uint ext_csd_bits;
2014} ext_csd_bus_width[] = {
2015 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
2016 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
2017 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
2018 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
2019 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
2020};
2021
Peng Fan3dd26262018-08-10 14:07:54 +08002022#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2023static int mmc_select_hs400(struct mmc *mmc)
2024{
2025 int err;
2026
2027 /* Set timing to HS200 for tuning */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002028 err = mmc_set_card_speed(mmc, MMC_HS_200, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002029 if (err)
2030 return err;
2031
2032 /* configure the bus mode (host) */
2033 mmc_select_mode(mmc, MMC_HS_200);
2034 mmc_set_clock(mmc, mmc->tran_speed, false);
2035
2036 /* execute tuning if needed */
Marek Vasutd1343522024-02-24 23:32:09 +01002037 mmc->hs400_tuning = true;
Peng Fan3dd26262018-08-10 14:07:54 +08002038 err = mmc_execute_tuning(mmc, MMC_CMD_SEND_TUNING_BLOCK_HS200);
Marek Vasutd1343522024-02-24 23:32:09 +01002039 mmc->hs400_tuning = false;
Peng Fan3dd26262018-08-10 14:07:54 +08002040 if (err) {
2041 debug("tuning failed\n");
2042 return err;
2043 }
2044
2045 /* Set back to HS */
BOUGH CHEN5cf12032019-03-26 06:24:17 +00002046 mmc_set_card_speed(mmc, MMC_HS, true);
Peng Fan3dd26262018-08-10 14:07:54 +08002047
Yangbo Lud271e102020-09-01 16:58:04 +08002048 err = mmc_hs400_prepare_ddr(mmc);
2049 if (err)
2050 return err;
2051
Peng Fan3dd26262018-08-10 14:07:54 +08002052 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2053 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG);
2054 if (err)
2055 return err;
2056
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002057 err = mmc_set_card_speed(mmc, MMC_HS_400, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002058 if (err)
2059 return err;
2060
2061 mmc_select_mode(mmc, MMC_HS_400);
2062 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2063 if (err)
2064 return err;
2065
2066 return 0;
2067}
2068#else
2069static int mmc_select_hs400(struct mmc *mmc)
2070{
2071 return -ENOTSUPP;
2072}
2073#endif
2074
Peng Fan44acd492019-07-10 14:43:07 +08002075#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
2076#if !CONFIG_IS_ENABLED(DM_MMC)
2077static int mmc_set_enhanced_strobe(struct mmc *mmc)
2078{
2079 return -ENOTSUPP;
2080}
2081#endif
2082static int mmc_select_hs400es(struct mmc *mmc)
2083{
2084 int err;
2085
2086 err = mmc_set_card_speed(mmc, MMC_HS, true);
2087 if (err)
2088 return err;
2089
2090 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2091 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG |
2092 EXT_CSD_BUS_WIDTH_STROBE);
2093 if (err) {
2094 printf("switch to bus width for hs400 failed\n");
2095 return err;
2096 }
2097 /* TODO: driver strength */
2098 err = mmc_set_card_speed(mmc, MMC_HS_400_ES, false);
2099 if (err)
2100 return err;
2101
2102 mmc_select_mode(mmc, MMC_HS_400_ES);
2103 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2104 if (err)
2105 return err;
2106
2107 return mmc_set_enhanced_strobe(mmc);
2108}
2109#else
2110static int mmc_select_hs400es(struct mmc *mmc)
2111{
2112 return -ENOTSUPP;
2113}
2114#endif
2115
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002116#define for_each_supported_width(caps, ddr, ecbv) \
2117 for (ecbv = ext_csd_bus_width;\
2118 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
2119 ecbv++) \
2120 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
2121
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002122static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002123{
Jaehoon Chung52ff04a2020-12-04 06:36:00 +09002124 int err = 0;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002125 const struct mode_width_tuning *mwt;
2126 const struct ext_csd_bus_width *ecbw;
2127
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002128#ifdef DEBUG
2129 mmc_dump_capabilities("mmc", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002130 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002131#endif
2132
Anup Patelf49ff792019-07-08 04:10:43 +00002133 if (mmc_host_is_spi(mmc)) {
2134 mmc_set_bus_width(mmc, 1);
2135 mmc_select_mode(mmc, MMC_LEGACY);
2136 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
2137 return 0;
2138 }
2139
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002140 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002141 card_caps &= mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002142
2143 /* Only version 4 of MMC supports wider bus widths */
2144 if (mmc->version < MMC_VERSION_4)
2145 return 0;
2146
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002147 if (!mmc->ext_csd) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002148 pr_debug("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002149 return -ENOTSUPP;
2150 }
2151
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002152#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
Ye Lifb8c2e82021-08-17 17:20:34 +08002153 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
2154 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002155 /*
2156 * In case the eMMC is in HS200/HS400 mode, downgrade to HS mode
2157 * before doing anything else, since a transition from either of
2158 * the HS200/HS400 mode directly to legacy mode is not supported.
2159 */
2160 if (mmc->selected_mode == MMC_HS_200 ||
Ye Lifb8c2e82021-08-17 17:20:34 +08002161 mmc->selected_mode == MMC_HS_400 ||
2162 mmc->selected_mode == MMC_HS_400_ES)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002163 mmc_set_card_speed(mmc, MMC_HS, true);
2164 else
2165#endif
2166 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002167
2168 for_each_mmc_mode_by_pref(card_caps, mwt) {
2169 for_each_supported_width(card_caps & mwt->widths,
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002170 mmc_is_mode_ddr(mwt->mode), ecbw) {
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002171 enum mmc_voltage old_voltage;
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002172 pr_debug("trying mode %s width %d (at %d MHz)\n",
2173 mmc_mode_name(mwt->mode),
2174 bus_width(ecbw->cap),
2175 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002176 old_voltage = mmc->signal_voltage;
2177 err = mmc_set_lowest_voltage(mmc, mwt->mode,
2178 MMC_ALL_SIGNAL_VOLTAGE);
2179 if (err)
2180 continue;
2181
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002182 /* configure the bus width (card + host) */
2183 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2184 EXT_CSD_BUS_WIDTH,
2185 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
2186 if (err)
2187 goto error;
2188 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
2189
Peng Fan3dd26262018-08-10 14:07:54 +08002190 if (mwt->mode == MMC_HS_400) {
2191 err = mmc_select_hs400(mmc);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002192 if (err) {
Peng Fan3dd26262018-08-10 14:07:54 +08002193 printf("Select HS400 failed %d\n", err);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002194 goto error;
2195 }
Peng Fan44acd492019-07-10 14:43:07 +08002196 } else if (mwt->mode == MMC_HS_400_ES) {
2197 err = mmc_select_hs400es(mmc);
2198 if (err) {
2199 printf("Select HS400ES failed %d\n",
2200 err);
2201 goto error;
2202 }
Peng Fan3dd26262018-08-10 14:07:54 +08002203 } else {
2204 /* configure the bus speed (card) */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002205 err = mmc_set_card_speed(mmc, mwt->mode, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002206 if (err)
2207 goto error;
2208
2209 /*
2210 * configure the bus width AND the ddr mode
2211 * (card). The host side will be taken care
2212 * of in the next step
2213 */
2214 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
2215 err = mmc_switch(mmc,
2216 EXT_CSD_CMD_SET_NORMAL,
2217 EXT_CSD_BUS_WIDTH,
2218 ecbw->ext_csd_bits);
2219 if (err)
2220 goto error;
2221 }
2222
2223 /* configure the bus mode (host) */
2224 mmc_select_mode(mmc, mwt->mode);
2225 mmc_set_clock(mmc, mmc->tran_speed,
2226 MMC_CLK_ENABLE);
2227#ifdef MMC_SUPPORTS_TUNING
2228
2229 /* execute tuning if needed */
2230 if (mwt->tuning) {
2231 err = mmc_execute_tuning(mmc,
2232 mwt->tuning);
2233 if (err) {
Jaehoon Chung58896452020-11-17 07:04:59 +09002234 pr_debug("tuning failed : %d\n", err);
Peng Fan3dd26262018-08-10 14:07:54 +08002235 goto error;
2236 }
2237 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01002238#endif
Peng Fan3dd26262018-08-10 14:07:54 +08002239 }
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002240
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002241 /* do a transfer to check the configuration */
2242 err = mmc_read_and_compare_ext_csd(mmc);
2243 if (!err)
2244 return 0;
2245error:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002246 mmc_set_signal_voltage(mmc, old_voltage);
Naoki Hayama64dbd862020-10-12 18:35:22 +09002247 /* if an error occurred, revert to a safer bus mode */
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002248 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2249 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
2250 mmc_select_mode(mmc, MMC_LEGACY);
Valentine Barshak50dee4f2023-06-10 13:22:33 +02002251 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002252 mmc_set_bus_width(mmc, 1);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002253 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002254 }
2255
Jaehoon Chung58896452020-11-17 07:04:59 +09002256 pr_err("unable to select a mode : %d\n", err);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002257
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002258 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002259}
Marek Vasut62d77ce2018-04-15 00:37:11 +02002260#endif
2261
2262#if CONFIG_IS_ENABLED(MMC_TINY)
2263DEFINE_CACHE_ALIGN_BUFFER(u8, ext_csd_bkup, MMC_MAX_BLOCK_LEN);
2264#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002265
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002266static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002267{
2268 int err, i;
2269 u64 capacity;
2270 bool has_parts = false;
2271 bool part_completed;
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002272 static const u32 mmc_versions[] = {
2273 MMC_VERSION_4,
2274 MMC_VERSION_4_1,
2275 MMC_VERSION_4_2,
2276 MMC_VERSION_4_3,
Jean-Jacques Hiblotace1bed2018-02-09 12:09:28 +01002277 MMC_VERSION_4_4,
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002278 MMC_VERSION_4_41,
2279 MMC_VERSION_4_5,
2280 MMC_VERSION_5_0,
2281 MMC_VERSION_5_1
2282 };
2283
Marek Vasut62d77ce2018-04-15 00:37:11 +02002284#if CONFIG_IS_ENABLED(MMC_TINY)
2285 u8 *ext_csd = ext_csd_bkup;
2286
2287 if (IS_SD(mmc) || mmc->version < MMC_VERSION_4)
2288 return 0;
2289
2290 if (!mmc->ext_csd)
Sam Edwards229d6892023-05-18 13:47:07 -06002291 memset(ext_csd_bkup, 0, MMC_MAX_BLOCK_LEN);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002292
2293 err = mmc_send_ext_csd(mmc, ext_csd);
2294 if (err)
2295 goto error;
2296
2297 /* store the ext csd for future reference */
2298 if (!mmc->ext_csd)
2299 mmc->ext_csd = ext_csd;
2300#else
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002301 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002302
2303 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
2304 return 0;
2305
2306 /* check ext_csd version and capacity */
2307 err = mmc_send_ext_csd(mmc, ext_csd);
2308 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002309 goto error;
2310
2311 /* store the ext csd for future reference */
2312 if (!mmc->ext_csd)
2313 mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN);
2314 if (!mmc->ext_csd)
2315 return -ENOMEM;
2316 memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002317#endif
Alexander Kochetkov76584e32018-02-20 14:35:55 +03002318 if (ext_csd[EXT_CSD_REV] >= ARRAY_SIZE(mmc_versions))
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002319 return -EINVAL;
2320
2321 mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]];
2322
2323 if (mmc->version >= MMC_VERSION_4_2) {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002324 /*
2325 * According to the JEDEC Standard, the value of
2326 * ext_csd's capacity is valid if the value is more
2327 * than 2GB
2328 */
2329 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
2330 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
2331 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
2332 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2333 capacity *= MMC_MAX_BLOCK_LEN;
2334 if ((capacity >> 20) > 2 * 1024)
2335 mmc->capacity_user = capacity;
2336 }
2337
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +02002338 if (mmc->version >= MMC_VERSION_4_5)
2339 mmc->gen_cmd6_time = ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
2340
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002341 /* The partition data may be non-zero but it is only
2342 * effective if PARTITION_SETTING_COMPLETED is set in
2343 * EXT_CSD, so ignore any data if this bit is not set,
2344 * except for enabling the high-capacity group size
2345 * definition (see below).
2346 */
2347 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
2348 EXT_CSD_PARTITION_SETTING_COMPLETED);
2349
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +02002350 mmc->part_switch_time = ext_csd[EXT_CSD_PART_SWITCH_TIME];
2351 /* Some eMMC set the value too low so set a minimum */
2352 if (mmc->part_switch_time < MMC_MIN_PART_SWITCH_TIME && mmc->part_switch_time)
2353 mmc->part_switch_time = MMC_MIN_PART_SWITCH_TIME;
2354
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002355 /* store the partition info of emmc */
2356 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
2357 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
2358 ext_csd[EXT_CSD_BOOT_MULT])
2359 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
2360 if (part_completed &&
2361 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
2362 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
2363
2364 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
2365
2366 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
2367
2368 for (i = 0; i < 4; i++) {
2369 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
2370 uint mult = (ext_csd[idx + 2] << 16) +
2371 (ext_csd[idx + 1] << 8) + ext_csd[idx];
2372 if (mult)
2373 has_parts = true;
2374 if (!part_completed)
2375 continue;
2376 mmc->capacity_gp[i] = mult;
2377 mmc->capacity_gp[i] *=
2378 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2379 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2380 mmc->capacity_gp[i] <<= 19;
2381 }
2382
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002383#ifndef CONFIG_SPL_BUILD
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002384 if (part_completed) {
2385 mmc->enh_user_size =
2386 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
2387 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
2388 ext_csd[EXT_CSD_ENH_SIZE_MULT];
2389 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2390 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2391 mmc->enh_user_size <<= 19;
2392 mmc->enh_user_start =
2393 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
2394 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
2395 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
2396 ext_csd[EXT_CSD_ENH_START_ADDR];
2397 if (mmc->high_capacity)
2398 mmc->enh_user_start <<= 9;
2399 }
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002400#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002401
2402 /*
2403 * Host needs to enable ERASE_GRP_DEF bit if device is
2404 * partitioned. This bit will be lost every time after a reset
2405 * or power off. This will affect erase size.
2406 */
2407 if (part_completed)
2408 has_parts = true;
2409 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
2410 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
2411 has_parts = true;
2412 if (has_parts) {
2413 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2414 EXT_CSD_ERASE_GROUP_DEF, 1);
2415
2416 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002417 goto error;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002418
2419 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
2420 }
2421
2422 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002423#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002424 /* Read out group size from ext_csd */
2425 mmc->erase_grp_size =
2426 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002427#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002428 /*
2429 * if high capacity and partition setting completed
2430 * SEC_COUNT is valid even if it is smaller than 2 GiB
2431 * JEDEC Standard JESD84-B45, 6.2.4
2432 */
2433 if (mmc->high_capacity && part_completed) {
2434 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
2435 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
2436 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
2437 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
2438 capacity *= MMC_MAX_BLOCK_LEN;
2439 mmc->capacity_user = capacity;
2440 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002441 }
2442#if CONFIG_IS_ENABLED(MMC_WRITE)
2443 else {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002444 /* Calculate the group size from the csd value. */
2445 int erase_gsz, erase_gmul;
2446
2447 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
2448 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
2449 mmc->erase_grp_size = (erase_gsz + 1)
2450 * (erase_gmul + 1);
2451 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002452#endif
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002453#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002454 mmc->hc_wp_grp_size = 1024
2455 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
2456 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002457#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002458
2459 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
2460
Loic Poulaineeb739a2023-01-26 10:24:17 +01002461 mmc->can_trim =
2462 !!(ext_csd[EXT_CSD_SEC_FEATURE] & EXT_CSD_SEC_FEATURE_TRIM_EN);
2463
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002464 return 0;
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002465error:
2466 if (mmc->ext_csd) {
Marek Vasut62d77ce2018-04-15 00:37:11 +02002467#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002468 free(mmc->ext_csd);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002469#endif
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002470 mmc->ext_csd = NULL;
2471 }
2472 return err;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002473}
2474
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002475static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002476{
Stephen Warrenf866a462013-06-11 15:14:01 -06002477 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05002478 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002479 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05002480 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06002481 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05002482
Thomas Choud52ebf12010-12-24 13:12:21 +00002483#ifdef CONFIG_MMC_SPI_CRC_ON
2484 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
2485 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
2486 cmd.resp_type = MMC_RSP_R1;
2487 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002488 err = mmc_send_cmd(mmc, &cmd, NULL);
Thomas Choud52ebf12010-12-24 13:12:21 +00002489 if (err)
2490 return err;
2491 }
2492#endif
2493
Andy Fleming272cc702008-10-30 16:41:01 -05002494 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002495 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
2496 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05002497 cmd.resp_type = MMC_RSP_R2;
2498 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002499
Sean Andersonda129172020-10-17 08:36:27 -04002500 err = mmc_send_cmd_quirks(mmc, &cmd, NULL, MMC_QUIRK_RETRY_SEND_CID, 4);
Andy Fleming272cc702008-10-30 16:41:01 -05002501 if (err)
2502 return err;
2503
2504 memcpy(mmc->cid, cmd.response, 16);
2505
2506 /*
2507 * For MMC cards, set the Relative Address.
2508 * For SD cards, get the Relatvie Address.
2509 * This also puts the cards into Standby State
2510 */
Thomas Choud52ebf12010-12-24 13:12:21 +00002511 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2512 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
2513 cmd.cmdarg = mmc->rca << 16;
2514 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05002515
Thomas Choud52ebf12010-12-24 13:12:21 +00002516 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002517
Thomas Choud52ebf12010-12-24 13:12:21 +00002518 if (err)
2519 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002520
Thomas Choud52ebf12010-12-24 13:12:21 +00002521 if (IS_SD(mmc))
2522 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
2523 }
Andy Fleming272cc702008-10-30 16:41:01 -05002524
2525 /* Get the Card-Specific Data */
2526 cmd.cmdidx = MMC_CMD_SEND_CSD;
2527 cmd.resp_type = MMC_RSP_R2;
2528 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05002529
2530 err = mmc_send_cmd(mmc, &cmd, NULL);
2531
2532 if (err)
2533 return err;
2534
Rabin Vincent998be3d2009-04-05 13:30:56 +05302535 mmc->csd[0] = cmd.response[0];
2536 mmc->csd[1] = cmd.response[1];
2537 mmc->csd[2] = cmd.response[2];
2538 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05002539
2540 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302541 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05002542
2543 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07002544 case 0:
2545 mmc->version = MMC_VERSION_1_2;
2546 break;
2547 case 1:
2548 mmc->version = MMC_VERSION_1_4;
2549 break;
2550 case 2:
2551 mmc->version = MMC_VERSION_2_2;
2552 break;
2553 case 3:
2554 mmc->version = MMC_VERSION_3;
2555 break;
2556 case 4:
2557 mmc->version = MMC_VERSION_4;
2558 break;
2559 default:
2560 mmc->version = MMC_VERSION_1_2;
2561 break;
Andy Fleming272cc702008-10-30 16:41:01 -05002562 }
2563 }
2564
2565 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302566 freq = fbase[(cmd.response[0] & 0x7)];
2567 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05002568
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002569 mmc->legacy_speed = freq * mult;
Heinrich Schuchardtf9a86fb2024-01-04 04:49:42 +01002570 if (!mmc->legacy_speed)
2571 log_debug("TRAN_SPEED: reserved value");
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002572 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05002573
Markus Niebelab711882013-12-16 13:40:46 +01002574 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05302575 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002576#if CONFIG_IS_ENABLED(MMC_WRITE)
Andy Fleming272cc702008-10-30 16:41:01 -05002577
2578 if (IS_SD(mmc))
2579 mmc->write_bl_len = mmc->read_bl_len;
2580 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05302581 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002582#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002583
2584 if (mmc->high_capacity) {
2585 csize = (mmc->csd[1] & 0x3f) << 16
2586 | (mmc->csd[2] & 0xffff0000) >> 16;
2587 cmult = 8;
2588 } else {
2589 csize = (mmc->csd[1] & 0x3ff) << 2
2590 | (mmc->csd[2] & 0xc0000000) >> 30;
2591 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2592 }
2593
Stephen Warrenf866a462013-06-11 15:14:01 -06002594 mmc->capacity_user = (csize + 1) << (cmult + 2);
2595 mmc->capacity_user *= mmc->read_bl_len;
2596 mmc->capacity_boot = 0;
2597 mmc->capacity_rpmb = 0;
2598 for (i = 0; i < 4; i++)
2599 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002600
Simon Glass8bfa1952013-04-03 08:54:30 +00002601 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2602 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05002603
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002604#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass8bfa1952013-04-03 08:54:30 +00002605 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2606 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002607#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002608
Markus Niebelab711882013-12-16 13:40:46 +01002609 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2610 cmd.cmdidx = MMC_CMD_SET_DSR;
2611 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2612 cmd.resp_type = MMC_RSP_NONE;
2613 if (mmc_send_cmd(mmc, &cmd, NULL))
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002614 pr_warn("MMC: SET_DSR failed\n");
Markus Niebelab711882013-12-16 13:40:46 +01002615 }
2616
Andy Fleming272cc702008-10-30 16:41:01 -05002617 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002618 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2619 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00002620 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002621 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00002622 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002623
Thomas Choud52ebf12010-12-24 13:12:21 +00002624 if (err)
2625 return err;
2626 }
Andy Fleming272cc702008-10-30 16:41:01 -05002627
Lei Wene6f99a52011-06-22 17:03:31 +00002628 /*
2629 * For SD, its erase group is always one sector
2630 */
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002631#if CONFIG_IS_ENABLED(MMC_WRITE)
Lei Wene6f99a52011-06-22 17:03:31 +00002632 mmc->erase_grp_size = 1;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002633#endif
Lei Wenbc897b12011-05-02 16:26:26 +00002634 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00002635
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002636 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002637 if (err)
2638 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05302639
Simon Glassc40fdca2016-05-01 13:52:35 -06002640 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06002641 if (err)
2642 return err;
2643
Marek Vasut62d77ce2018-04-15 00:37:11 +02002644#if CONFIG_IS_ENABLED(MMC_TINY)
2645 mmc_set_clock(mmc, mmc->legacy_speed, false);
Faiz Abbase8d5dde2020-02-26 13:44:32 +05302646 mmc_select_mode(mmc, MMC_LEGACY);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002647 mmc_set_bus_width(mmc, 1);
2648#else
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002649 if (IS_SD(mmc)) {
2650 err = sd_get_capabilities(mmc);
2651 if (err)
2652 return err;
2653 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2654 } else {
2655 err = mmc_get_capabilities(mmc);
2656 if (err)
2657 return err;
Masahiro Yamada8adf50e2020-01-23 14:31:12 +09002658 err = mmc_select_mode_and_width(mmc, mmc->card_caps);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002659 }
Marek Vasut62d77ce2018-04-15 00:37:11 +02002660#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002661 if (err)
2662 return err;
2663
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002664 mmc->best_mode = mmc->selected_mode;
Jaehoon Chungad5fd922012-03-26 21:16:03 +00002665
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002666 /* Fix the block length for DDR mode */
2667 if (mmc->ddr_mode) {
2668 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002669#if CONFIG_IS_ENABLED(MMC_WRITE)
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002670 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002671#endif
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002672 }
2673
Andy Fleming272cc702008-10-30 16:41:01 -05002674 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06002675 bdesc = mmc_get_blk_desc(mmc);
2676 bdesc->lun = 0;
2677 bdesc->hwpart = 0;
2678 bdesc->type = 0;
2679 bdesc->blksz = mmc->read_bl_len;
2680 bdesc->log2blksz = LOG2(bdesc->blksz);
2681 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01002682#if !defined(CONFIG_SPL_BUILD) || \
2683 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
Simon Glass27084c02019-09-25 08:56:27 -06002684 !CONFIG_IS_ENABLED(USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06002685 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00002686 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2687 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002688 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002689 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2690 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2691 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002692 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002693 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01002694#else
Simon Glassc40fdca2016-05-01 13:52:35 -06002695 bdesc->vendor[0] = 0;
2696 bdesc->product[0] = 0;
2697 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01002698#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002699
Andre Przywaraeef05fd2018-12-17 10:05:45 +00002700#if !defined(CONFIG_DM_MMC) && (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT))
2701 part_init(bdesc);
2702#endif
2703
Andy Fleming272cc702008-10-30 16:41:01 -05002704 return 0;
2705}
2706
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002707static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002708{
2709 struct mmc_cmd cmd;
2710 int err;
2711
2712 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2713 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002714 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05002715 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05002716
2717 err = mmc_send_cmd(mmc, &cmd, NULL);
2718
2719 if (err)
2720 return err;
2721
Rabin Vincent998be3d2009-04-05 13:30:56 +05302722 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002723 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002724 else
2725 mmc->version = SD_VERSION_2;
2726
2727 return 0;
2728}
2729
Simon Glassc4d660d2017-07-04 13:31:19 -06002730#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002731/* board-specific MMC power initializations. */
2732__weak void board_mmc_power_init(void)
2733{
2734}
Simon Glass05cbeb72017-04-22 19:10:56 -06002735#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002736
Peng Fan2051aef2016-10-11 15:08:43 +08002737static int mmc_power_init(struct mmc *mmc)
2738{
Simon Glassc4d660d2017-07-04 13:31:19 -06002739#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002740#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08002741 int ret;
2742
2743 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002744 &mmc->vmmc_supply);
2745 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002746 pr_debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002747
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002748 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2749 &mmc->vqmmc_supply);
2750 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002751 pr_debug("%s: No vqmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002752#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06002753#else /* !CONFIG_DM_MMC */
2754 /*
2755 * Driver model should use a regulator, as above, rather than calling
2756 * out to board code.
2757 */
2758 board_mmc_power_init();
2759#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002760 return 0;
2761}
2762
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002763/*
2764 * put the host in the initial state:
2765 * - turn on Vdd (card power supply)
2766 * - configure the bus width and clock to minimal values
2767 */
2768static void mmc_set_initial_state(struct mmc *mmc)
2769{
2770 int err;
2771
2772 /* First try to set 3.3V. If it fails set to 1.8V */
2773 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2774 if (err != 0)
2775 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2776 if (err != 0)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002777 pr_warn("mmc: failed to set signal voltage\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002778
2779 mmc_select_mode(mmc, MMC_LEGACY);
2780 mmc_set_bus_width(mmc, 1);
Jaehoon Chung65117182018-01-26 19:25:29 +09002781 mmc_set_clock(mmc, 0, MMC_CLK_ENABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002782}
2783
2784static int mmc_power_on(struct mmc *mmc)
2785{
2786#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2787 if (mmc->vmmc_supply) {
Jonas Karlman9a2e9cc2023-07-19 21:20:59 +00002788 int ret = regulator_set_enable_if_allowed(mmc->vmmc_supply,
2789 true);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002790
Jonas Karlman9a2e9cc2023-07-19 21:20:59 +00002791 if (ret && ret != -ENOSYS) {
Jaehoon Chung58896452020-11-17 07:04:59 +09002792 printf("Error enabling VMMC supply : %d\n", ret);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002793 return ret;
2794 }
2795 }
2796#endif
2797 return 0;
2798}
2799
2800static int mmc_power_off(struct mmc *mmc)
2801{
Jaehoon Chung65117182018-01-26 19:25:29 +09002802 mmc_set_clock(mmc, 0, MMC_CLK_DISABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002803#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2804 if (mmc->vmmc_supply) {
Jonas Karlman9a2e9cc2023-07-19 21:20:59 +00002805 int ret = regulator_set_enable_if_allowed(mmc->vmmc_supply,
2806 false);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002807
Jonas Karlman9a2e9cc2023-07-19 21:20:59 +00002808 if (ret && ret != -ENOSYS) {
Jaehoon Chung58896452020-11-17 07:04:59 +09002809 pr_debug("Error disabling VMMC supply : %d\n", ret);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002810 return ret;
2811 }
2812 }
2813#endif
2814 return 0;
2815}
2816
2817static int mmc_power_cycle(struct mmc *mmc)
2818{
2819 int ret;
2820
2821 ret = mmc_power_off(mmc);
2822 if (ret)
2823 return ret;
Yann Gautier3602a562019-09-19 17:56:12 +02002824
2825 ret = mmc_host_power_cycle(mmc);
2826 if (ret)
2827 return ret;
2828
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002829 /*
2830 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2831 * to be on the safer side.
2832 */
2833 udelay(2000);
2834 return mmc_power_on(mmc);
2835}
2836
Pali Rohára4c577f2021-07-14 16:37:29 +02002837int mmc_get_op_cond(struct mmc *mmc, bool quiet)
Andy Fleming272cc702008-10-30 16:41:01 -05002838{
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002839 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
Macpaul Linafd59322011-11-14 23:35:39 +00002840 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05002841
Lei Wenbc897b12011-05-02 16:26:26 +00002842 if (mmc->has_init)
2843 return 0;
2844
Peng Fan2051aef2016-10-11 15:08:43 +08002845 err = mmc_power_init(mmc);
2846 if (err)
2847 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002848
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002849#ifdef CONFIG_MMC_QUIRKS
2850 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
Joel Johnsond4a5fa32020-01-11 09:08:14 -07002851 MMC_QUIRK_RETRY_SEND_CID |
2852 MMC_QUIRK_RETRY_APP_CMD;
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002853#endif
2854
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002855 err = mmc_power_cycle(mmc);
2856 if (err) {
2857 /*
2858 * if power cycling is not supported, we should not try
2859 * to use the UHS modes, because we wouldn't be able to
2860 * recover from an error during the UHS initialization.
2861 */
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002862 pr_debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002863 uhs_en = false;
2864 mmc->host_caps &= ~UHS_CAPS;
2865 err = mmc_power_on(mmc);
2866 }
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002867 if (err)
2868 return err;
2869
Simon Glasse7881d82017-07-29 11:35:31 -06002870#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Lu390f9bd2020-09-01 16:57:59 +08002871 /*
2872 * Re-initialization is needed to clear old configuration for
2873 * mmc rescan.
2874 */
2875 err = mmc_reinit(mmc);
Simon Glass8ca51e52016-06-12 23:30:22 -06002876#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02002877 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002878 err = mmc->cfg->ops->init(mmc);
Yangbo Lu390f9bd2020-09-01 16:57:59 +08002879#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002880 if (err)
2881 return err;
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06002882 mmc->ddr_mode = 0;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02002883
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002884retry:
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002885 mmc_set_initial_state(mmc);
Jean-Jacques Hiblot318a7a52017-09-21 16:30:01 +02002886
Andy Fleming272cc702008-10-30 16:41:01 -05002887 /* Reset the Card */
2888 err = mmc_go_idle(mmc);
2889
2890 if (err)
2891 return err;
2892
Marcel Ziswilerf5624b12019-05-20 02:44:53 +02002893 /* The internal partition reset to user partition(0) at every CMD0 */
Simon Glassc40fdca2016-05-01 13:52:35 -06002894 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00002895
Andy Fleming272cc702008-10-30 16:41:01 -05002896 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00002897 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002898
Andy Fleming272cc702008-10-30 16:41:01 -05002899 /* Now try to get the SD card's operating condition */
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002900 err = sd_send_op_cond(mmc, uhs_en);
2901 if (err && uhs_en) {
2902 uhs_en = false;
2903 mmc_power_cycle(mmc);
2904 goto retry;
2905 }
Andy Fleming272cc702008-10-30 16:41:01 -05002906
2907 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002908 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05002909 err = mmc_send_op_cond(mmc);
2910
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002911 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01002912#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Pali Rohára4c577f2021-07-14 16:37:29 +02002913 if (!quiet)
2914 pr_err("Card did not respond to voltage select! : %d\n", err);
Paul Burton56196822013-09-04 16:12:25 +01002915#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002916 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002917 }
2918 }
2919
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002920 return err;
2921}
2922
2923int mmc_start_init(struct mmc *mmc)
2924{
2925 bool no_card;
2926 int err = 0;
2927
2928 /*
2929 * all hosts are capable of 1 bit bus-width and able to use the legacy
2930 * timings.
2931 */
Faiz Abbase8d5dde2020-02-26 13:44:32 +05302932 mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(MMC_LEGACY) |
Aswath Govindraju19f7a342021-08-13 23:04:41 +05302933 MMC_MODE_1BIT;
2934
2935 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET)) {
2936 if (mmc->user_speed_mode != MMC_MODES_END) {
2937 int i;
2938 /* set host caps */
2939 if (mmc->host_caps & MMC_CAP(mmc->user_speed_mode)) {
2940 /* Remove all existing speed capabilities */
2941 for (i = MMC_LEGACY; i < MMC_MODES_END; i++)
2942 mmc->host_caps &= ~MMC_CAP(i);
2943 mmc->host_caps |= (MMC_CAP(mmc->user_speed_mode)
2944 | MMC_CAP(MMC_LEGACY) |
2945 MMC_MODE_1BIT);
2946 } else {
2947 pr_err("bus_mode requested is not supported\n");
2948 return -EINVAL;
2949 }
2950 }
2951 }
Faiz Abbas32860bd2020-02-26 13:44:30 +05302952#if CONFIG_IS_ENABLED(DM_MMC)
2953 mmc_deferred_probe(mmc);
2954#endif
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002955#if !defined(CONFIG_MMC_BROKEN_CD)
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002956 no_card = mmc_getcd(mmc) == 0;
2957#else
2958 no_card = 0;
2959#endif
2960#if !CONFIG_IS_ENABLED(DM_MMC)
Baruch Siachfea39392019-07-22 15:52:12 +03002961 /* we pretend there's no card when init is NULL */
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002962 no_card = no_card || (mmc->cfg->ops->init == NULL);
2963#endif
2964 if (no_card) {
2965 mmc->has_init = 0;
2966#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2967 pr_err("MMC: no card present\n");
2968#endif
2969 return -ENOMEDIUM;
2970 }
2971
Pali Rohára4c577f2021-07-14 16:37:29 +02002972 err = mmc_get_op_cond(mmc, false);
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002973
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002974 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002975 mmc->init_in_progress = 1;
2976
2977 return err;
2978}
2979
2980static int mmc_complete_init(struct mmc *mmc)
2981{
2982 int err = 0;
2983
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002984 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002985 if (mmc->op_cond_pending)
2986 err = mmc_complete_op_cond(mmc);
2987
2988 if (!err)
2989 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00002990 if (err)
2991 mmc->has_init = 0;
2992 else
2993 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002994 return err;
2995}
2996
2997int mmc_init(struct mmc *mmc)
2998{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002999 int err = 0;
Vipul Kumar36332b62018-05-03 12:20:54 +05303000 __maybe_unused ulong start;
Simon Glassc4d660d2017-07-04 13:31:19 -06003001#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06003002 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00003003
Simon Glass33fb2112016-05-01 13:52:41 -06003004 upriv->mmc = mmc;
3005#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00003006 if (mmc->has_init)
3007 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02003008
3009 start = get_timer(0);
3010
Che-Liang Chioue9550442012-11-28 15:21:13 +00003011 if (!mmc->init_in_progress)
3012 err = mmc_start_init(mmc);
3013
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05003014 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00003015 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01003016 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09003017 pr_info("%s: %d, time %lu\n", __func__, err, get_timer(start));
Jagan Teki919b4852017-01-10 11:18:43 +01003018
Lei Wenbc897b12011-05-02 16:26:26 +00003019 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05003020}
3021
Marek Vasutfceea992019-01-29 04:45:51 +01003022#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
3023 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
3024 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
3025int mmc_deinit(struct mmc *mmc)
3026{
3027 u32 caps_filtered;
3028
3029 if (!mmc->has_init)
3030 return 0;
3031
3032 if (IS_SD(mmc)) {
3033 caps_filtered = mmc->card_caps &
3034 ~(MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25) |
3035 MMC_CAP(UHS_SDR50) | MMC_CAP(UHS_DDR50) |
3036 MMC_CAP(UHS_SDR104));
3037
3038 return sd_select_mode_and_width(mmc, caps_filtered);
3039 } else {
3040 caps_filtered = mmc->card_caps &
Ye Lifb8c2e82021-08-17 17:20:34 +08003041 ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_400_ES));
Marek Vasutfceea992019-01-29 04:45:51 +01003042
3043 return mmc_select_mode_and_width(mmc, caps_filtered);
3044 }
3045}
3046#endif
3047
Markus Niebelab711882013-12-16 13:40:46 +01003048int mmc_set_dsr(struct mmc *mmc, u16 val)
3049{
3050 mmc->dsr = val;
3051 return 0;
3052}
3053
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02003054/* CPU-specific MMC initializations */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003055__weak int cpu_mmc_init(struct bd_info *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05003056{
3057 return -1;
3058}
3059
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02003060/* board-specific MMC initializations. */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003061__weak int board_mmc_init(struct bd_info *bis)
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02003062{
3063 return -1;
3064}
Andy Fleming272cc702008-10-30 16:41:01 -05003065
Che-Liang Chioue9550442012-11-28 15:21:13 +00003066void mmc_set_preinit(struct mmc *mmc, int preinit)
3067{
3068 mmc->preinit = preinit;
3069}
3070
Faiz Abbas8a856db2018-02-12 19:35:24 +05303071#if CONFIG_IS_ENABLED(DM_MMC)
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003072static int mmc_probe(struct bd_info *bis)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003073{
Simon Glass4a1db6d2015-12-29 05:22:49 -07003074 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003075 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07003076 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003077
3078 ret = uclass_get(UCLASS_MMC, &uc);
3079 if (ret)
3080 return ret;
3081
Simon Glass4a1db6d2015-12-29 05:22:49 -07003082 /*
3083 * Try to add them in sequence order. Really with driver model we
3084 * should allow holes, but the current MMC list does not allow that.
3085 * So if we request 0, 1, 3 we will get 0, 1, 2.
3086 */
3087 for (i = 0; ; i++) {
3088 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
3089 if (ret == -ENODEV)
3090 break;
3091 }
3092 uclass_foreach_dev(dev, uc) {
3093 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003094 if (ret)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01003095 pr_err("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003096 }
3097
3098 return 0;
3099}
3100#else
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003101static int mmc_probe(struct bd_info *bis)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003102{
3103 if (board_mmc_init(bis) < 0)
3104 cpu_mmc_init(bis);
3105
3106 return 0;
3107}
3108#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00003109
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09003110int mmc_initialize(struct bd_info *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05003111{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003112 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003113 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003114 if (initialized) /* Avoid initializing mmc multiple times */
3115 return 0;
3116 initialized = 1;
3117
Simon Glassc4d660d2017-07-04 13:31:19 -06003118#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01003119#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06003120 mmc_list_init();
3121#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01003122#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003123 ret = mmc_probe(bis);
3124 if (ret)
3125 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05003126
Ying Zhangbb0dc102013-08-16 15:16:11 +08003127#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05003128 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08003129#endif
Andy Fleming272cc702008-10-30 16:41:01 -05003130
Simon Glassc40fdca2016-05-01 13:52:35 -06003131 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05003132 return 0;
3133}
Tomas Melincd3d4882016-11-25 11:01:03 +02003134
Lokesh Vutla80f02012019-09-09 14:40:36 +05303135#if CONFIG_IS_ENABLED(DM_MMC)
3136int mmc_init_device(int num)
3137{
3138 struct udevice *dev;
3139 struct mmc *m;
3140 int ret;
3141
Aswath Govindraju2153a082021-03-25 12:48:47 +05303142 if (uclass_get_device_by_seq(UCLASS_MMC, num, &dev)) {
3143 ret = uclass_get_device(UCLASS_MMC, num, &dev);
3144 if (ret)
3145 return ret;
3146 }
Lokesh Vutla80f02012019-09-09 14:40:36 +05303147
3148 m = mmc_get_mmc_dev(dev);
3149 if (!m)
3150 return 0;
Venkatesh Yadav Abbarapu337af542022-09-29 10:22:49 +05303151
3152 /* Initialising user set speed mode */
3153 m->user_speed_mode = MMC_MODES_END;
3154
Lokesh Vutla80f02012019-09-09 14:40:36 +05303155 if (m->preinit)
3156 mmc_start_init(m);
3157
3158 return 0;
3159}
3160#endif
3161
Tomas Melincd3d4882016-11-25 11:01:03 +02003162#ifdef CONFIG_CMD_BKOPS_ENABLE
Marek Vasutcf1f7352023-01-05 15:19:08 +01003163int mmc_set_bkops_enable(struct mmc *mmc, bool autobkops, bool enable)
Tomas Melincd3d4882016-11-25 11:01:03 +02003164{
3165 int err;
Marek Vasutcf1f7352023-01-05 15:19:08 +01003166 u32 bit = autobkops ? BIT(1) : BIT(0);
Tomas Melincd3d4882016-11-25 11:01:03 +02003167 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
3168
3169 err = mmc_send_ext_csd(mmc, ext_csd);
3170 if (err) {
3171 puts("Could not get ext_csd register values\n");
3172 return err;
3173 }
3174
3175 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
3176 puts("Background operations not supported on device\n");
3177 return -EMEDIUMTYPE;
3178 }
3179
Marek Vasutcf1f7352023-01-05 15:19:08 +01003180 if (enable && (ext_csd[EXT_CSD_BKOPS_EN] & bit)) {
Tomas Melincd3d4882016-11-25 11:01:03 +02003181 puts("Background operations already enabled\n");
3182 return 0;
3183 }
3184
Marek Vasutcf1f7352023-01-05 15:19:08 +01003185 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN,
3186 enable ? bit : 0);
Tomas Melincd3d4882016-11-25 11:01:03 +02003187 if (err) {
Marek Vasutcf1f7352023-01-05 15:19:08 +01003188 printf("Failed to %sable manual background operations\n",
3189 enable ? "en" : "dis");
Tomas Melincd3d4882016-11-25 11:01:03 +02003190 return err;
3191 }
3192
Marek Vasutcf1f7352023-01-05 15:19:08 +01003193 printf("%sabled %s background operations\n",
3194 enable ? "En" : "Dis", autobkops ? "auto" : "manual");
Tomas Melincd3d4882016-11-25 11:01:03 +02003195
3196 return 0;
3197}
3198#endif
David Woodhouse4dee3f72020-08-04 10:05:46 +01003199
3200__weak int mmc_get_env_dev(void)
3201{
3202#ifdef CONFIG_SYS_MMC_ENV_DEV
3203 return CONFIG_SYS_MMC_ENV_DEV;
3204#else
3205 return 0;
3206#endif
3207}