blob: 12d29da528a8f39743fb0ec866eeab54a55d090a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Flemingad347bb2008-10-30 16:41:01 -05002/*
3 * Copyright 2008, Freescale Semiconductor, Inc
Yangbo Luf9049b22020-06-17 18:08:58 +08004 * Copyright 2020 NXP
Andy Flemingad347bb2008-10-30 16:41:01 -05005 * Andy Fleming
6 *
7 * Based vaguely on the Linux code
Andy Flemingad347bb2008-10-30 16:41:01 -05008 */
9
10#include <config.h>
11#include <common.h>
Simon Glass655306c2020-05-10 11:39:58 -060012#include <blk.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050013#include <command.h>
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -060014#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -060016#include <dm/device-internal.h>
Stephen Warrenbf0c7852014-05-23 12:47:06 -060017#include <errno.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050018#include <mmc.h>
19#include <part.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Peng Fan15305962016-10-11 15:08:43 +080022#include <power/regulator.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050023#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060024#include <memalign.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050025#include <linux/list.h>
Rabin Vincent69d4e2c2009-04-05 13:30:54 +053026#include <div64.h>
Paul Burton8d30cc92013-09-09 15:30:26 +010027#include "mmc_private.h"
Andy Flemingad347bb2008-10-30 16:41:01 -050028
Jean-Jacques Hiblot201559c2019-07-02 10:53:54 +020029#define DEFAULT_CMD6_TIMEOUT_MS 500
30
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +020031static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
Marek Vasutf537e392016-12-01 02:06:33 +010032
Simon Glasseba48f92017-07-29 11:35:31 -060033#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +020034
Sam Protsenkodb174c62019-08-14 22:52:51 +030035static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +020036{
Loic Poulain9c32f4f2022-05-26 16:37:21 +020037 if (mmc->cfg->ops->wait_dat0)
38 return mmc->cfg->ops->wait_dat0(mmc, state, timeout_us);
39
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +020040 return -ENOSYS;
41}
42
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +020043__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanov020f2612012-12-03 02:19:46 +000044{
45 return -1;
46}
47
48int mmc_getwp(struct mmc *mmc)
49{
50 int wp;
51
52 wp = board_mmc_getwp(mmc);
53
Peter Korsgaardf7b15102013-03-21 04:00:03 +000054 if (wp < 0) {
Pantelis Antoniou2c850462014-03-11 19:34:20 +020055 if (mmc->cfg->ops->getwp)
56 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardf7b15102013-03-21 04:00:03 +000057 else
58 wp = 0;
59 }
Nikita Kiryanov020f2612012-12-03 02:19:46 +000060
61 return wp;
62}
63
Jeroen Hofstee47726302014-07-10 22:46:28 +020064__weak int board_mmc_getcd(struct mmc *mmc)
65{
Stefano Babic6e00edf2010-02-05 15:04:43 +010066 return -1;
67}
Simon Glass394dfc02016-06-12 23:30:22 -060068#endif
Stefano Babic6e00edf2010-02-05 15:04:43 +010069
Simon Glassb23d96e2016-06-12 23:30:20 -060070#ifdef CONFIG_MMC_TRACE
71void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
Andy Flemingad347bb2008-10-30 16:41:01 -050072{
Simon Glassb23d96e2016-06-12 23:30:20 -060073 printf("CMD_SEND:%d\n", cmd->cmdidx);
Marek Vasut6eeee302019-03-23 18:54:45 +010074 printf("\t\tARG\t\t\t 0x%08x\n", cmd->cmdarg);
Simon Glassb23d96e2016-06-12 23:30:20 -060075}
Marek Vasutdccb6082012-03-15 18:41:35 +000076
Simon Glassb23d96e2016-06-12 23:30:20 -060077void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
78{
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000079 int i;
80 u8 *ptr;
81
Bin Meng8d1ad1e2016-03-17 21:53:14 -070082 if (ret) {
83 printf("\t\tRET\t\t\t %d\n", ret);
84 } else {
85 switch (cmd->resp_type) {
86 case MMC_RSP_NONE:
87 printf("\t\tMMC_RSP_NONE\n");
88 break;
89 case MMC_RSP_R1:
Marek Vasut6eeee302019-03-23 18:54:45 +010090 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -070091 cmd->response[0]);
92 break;
93 case MMC_RSP_R1b:
Marek Vasut6eeee302019-03-23 18:54:45 +010094 printf("\t\tMMC_RSP_R1b\t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -070095 cmd->response[0]);
96 break;
97 case MMC_RSP_R2:
Marek Vasut6eeee302019-03-23 18:54:45 +010098 printf("\t\tMMC_RSP_R2\t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -070099 cmd->response[0]);
Marek Vasut6eeee302019-03-23 18:54:45 +0100100 printf("\t\t \t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -0700101 cmd->response[1]);
Marek Vasut6eeee302019-03-23 18:54:45 +0100102 printf("\t\t \t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -0700103 cmd->response[2]);
Marek Vasut6eeee302019-03-23 18:54:45 +0100104 printf("\t\t \t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -0700105 cmd->response[3]);
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000106 printf("\n");
Bin Meng8d1ad1e2016-03-17 21:53:14 -0700107 printf("\t\t\t\t\tDUMPING DATA\n");
108 for (i = 0; i < 4; i++) {
109 int j;
110 printf("\t\t\t\t\t%03d - ", i*4);
111 ptr = (u8 *)&cmd->response[i];
112 ptr += 3;
113 for (j = 0; j < 4; j++)
Marek Vasut6eeee302019-03-23 18:54:45 +0100114 printf("%02x ", *ptr--);
Bin Meng8d1ad1e2016-03-17 21:53:14 -0700115 printf("\n");
116 }
117 break;
118 case MMC_RSP_R3:
Marek Vasut6eeee302019-03-23 18:54:45 +0100119 printf("\t\tMMC_RSP_R3,4\t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -0700120 cmd->response[0]);
121 break;
122 default:
123 printf("\t\tERROR MMC rsp not supported\n");
124 break;
Bin Meng4a4ef872016-03-17 21:53:13 -0700125 }
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000126 }
Simon Glassb23d96e2016-06-12 23:30:20 -0600127}
128
129void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
130{
131 int status;
132
133 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
134 printf("CURR STATE:%d\n", status);
135}
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000136#endif
Simon Glassb23d96e2016-06-12 23:30:20 -0600137
Pali Rohár377ecee2022-04-03 00:20:10 +0200138#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG) || CONFIG_VAL(LOGLEVEL) >= LOGL_DEBUG
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +0200139const char *mmc_mode_name(enum bus_mode mode)
140{
141 static const char *const names[] = {
142 [MMC_LEGACY] = "MMC legacy",
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +0200143 [MMC_HS] = "MMC High Speed (26MHz)",
144 [SD_HS] = "SD High Speed (50MHz)",
145 [UHS_SDR12] = "UHS SDR12 (25MHz)",
146 [UHS_SDR25] = "UHS SDR25 (50MHz)",
147 [UHS_SDR50] = "UHS SDR50 (100MHz)",
148 [UHS_SDR104] = "UHS SDR104 (208MHz)",
149 [UHS_DDR50] = "UHS DDR50 (50MHz)",
150 [MMC_HS_52] = "MMC High Speed (52MHz)",
151 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
152 [MMC_HS_200] = "HS200 (200MHz)",
Peng Fan46801252018-08-10 14:07:54 +0800153 [MMC_HS_400] = "HS400 (200MHz)",
Peng Faneede83b2019-07-10 14:43:07 +0800154 [MMC_HS_400_ES] = "HS400ES (200MHz)",
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +0200155 };
156
157 if (mode >= MMC_MODES_END)
158 return "Unknown mode";
159 else
160 return names[mode];
161}
162#endif
163
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200164static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
165{
166 static const int freqs[] = {
Jaehoon Chung7c5c7302018-01-30 14:10:16 +0900167 [MMC_LEGACY] = 25000000,
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200168 [MMC_HS] = 26000000,
169 [SD_HS] = 50000000,
Jaehoon Chung7c5c7302018-01-30 14:10:16 +0900170 [MMC_HS_52] = 52000000,
171 [MMC_DDR_52] = 52000000,
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200172 [UHS_SDR12] = 25000000,
173 [UHS_SDR25] = 50000000,
174 [UHS_SDR50] = 100000000,
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200175 [UHS_DDR50] = 50000000,
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100176 [UHS_SDR104] = 208000000,
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200177 [MMC_HS_200] = 200000000,
Peng Fan46801252018-08-10 14:07:54 +0800178 [MMC_HS_400] = 200000000,
Peng Faneede83b2019-07-10 14:43:07 +0800179 [MMC_HS_400_ES] = 200000000,
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200180 };
181
182 if (mode == MMC_LEGACY)
183 return mmc->legacy_speed;
184 else if (mode >= MMC_MODES_END)
185 return 0;
186 else
187 return freqs[mode];
188}
189
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +0200190static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
191{
192 mmc->selected_mode = mode;
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200193 mmc->tran_speed = mmc_mode2freq(mmc, mode);
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200194 mmc->ddr_mode = mmc_is_mode_ddr(mode);
Masahiro Yamadaf97b1482018-01-28 19:11:42 +0900195 pr_debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
196 mmc->tran_speed / 1000000);
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +0200197 return 0;
198}
199
Simon Glasseba48f92017-07-29 11:35:31 -0600200#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassb23d96e2016-06-12 23:30:20 -0600201int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
202{
203 int ret;
204
205 mmmc_trace_before_send(mmc, cmd);
206 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
207 mmmc_trace_after_send(mmc, cmd, ret);
208
Marek Vasutdccb6082012-03-15 18:41:35 +0000209 return ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500210}
Simon Glass394dfc02016-06-12 23:30:22 -0600211#endif
Andy Flemingad347bb2008-10-30 16:41:01 -0500212
Sean Anderson86325092020-10-17 08:36:27 -0400213/**
214 * mmc_send_cmd_retry() - send a command to the mmc device, retrying on error
215 *
216 * @dev: device to receive the command
217 * @cmd: command to send
218 * @data: additional data to send/receive
219 * @retries: how many times to retry; mmc_send_cmd is always called at least
220 * once
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100221 * Return: 0 if ok, -ve on error
Sean Anderson86325092020-10-17 08:36:27 -0400222 */
223static int mmc_send_cmd_retry(struct mmc *mmc, struct mmc_cmd *cmd,
224 struct mmc_data *data, uint retries)
225{
226 int ret;
227
228 do {
229 ret = mmc_send_cmd(mmc, cmd, data);
230 } while (ret && retries--);
231
232 return ret;
233}
234
235/**
236 * mmc_send_cmd_quirks() - send a command to the mmc device, retrying if a
237 * specific quirk is enabled
238 *
239 * @dev: device to receive the command
240 * @cmd: command to send
241 * @data: additional data to send/receive
242 * @quirk: retry only if this quirk is enabled
243 * @retries: how many times to retry; mmc_send_cmd is always called at least
244 * once
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100245 * Return: 0 if ok, -ve on error
Sean Anderson86325092020-10-17 08:36:27 -0400246 */
247static int mmc_send_cmd_quirks(struct mmc *mmc, struct mmc_cmd *cmd,
248 struct mmc_data *data, u32 quirk, uint retries)
249{
250 if (CONFIG_IS_ENABLED(MMC_QUIRKS) && mmc->quirks & quirk)
251 return mmc_send_cmd_retry(mmc, cmd, data, retries);
252 else
253 return mmc_send_cmd(mmc, cmd, data);
254}
255
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200256int mmc_send_status(struct mmc *mmc, unsigned int *status)
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000257{
258 struct mmc_cmd cmd;
Sean Anderson86325092020-10-17 08:36:27 -0400259 int ret;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000260
261 cmd.cmdidx = MMC_CMD_SEND_STATUS;
262 cmd.resp_type = MMC_RSP_R1;
Marek Vasutc4427392011-08-10 09:24:48 +0200263 if (!mmc_host_is_spi(mmc))
264 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000265
Sean Anderson86325092020-10-17 08:36:27 -0400266 ret = mmc_send_cmd_retry(mmc, &cmd, NULL, 4);
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200267 mmc_trace_state(mmc, &cmd);
Sean Anderson86325092020-10-17 08:36:27 -0400268 if (!ret)
269 *status = cmd.response[0];
270
271 return ret;
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200272}
273
Sam Protsenkodb174c62019-08-14 22:52:51 +0300274int mmc_poll_for_busy(struct mmc *mmc, int timeout_ms)
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200275{
276 unsigned int status;
277 int err;
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +0200278
Sam Protsenkodb174c62019-08-14 22:52:51 +0300279 err = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblot4f04a322019-07-02 10:53:53 +0200280 if (err != -ENOSYS)
281 return err;
282
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200283 while (1) {
284 err = mmc_send_status(mmc, &status);
285 if (err)
286 return err;
287
288 if ((status & MMC_STATUS_RDY_FOR_DATA) &&
289 (status & MMC_STATUS_CURR_STATE) !=
290 MMC_STATE_PRG)
291 break;
292
293 if (status & MMC_STATUS_MASK) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100294#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200295 pr_err("Status Error: 0x%08x\n", status);
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100296#endif
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200297 return -ECOMM;
298 }
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000299
Sam Protsenkodb174c62019-08-14 22:52:51 +0300300 if (timeout_ms-- <= 0)
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500301 break;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000302
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500303 udelay(1000);
304 }
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000305
Sam Protsenkodb174c62019-08-14 22:52:51 +0300306 if (timeout_ms <= 0) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100307#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +0100308 pr_err("Timeout waiting card ready\n");
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100309#endif
Jaehoon Chung7825d202016-07-19 16:33:36 +0900310 return -ETIMEDOUT;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000311 }
312
313 return 0;
314}
315
Paul Burton8d30cc92013-09-09 15:30:26 +0100316int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Flemingad347bb2008-10-30 16:41:01 -0500317{
318 struct mmc_cmd cmd;
319
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -0600320 if (mmc->ddr_mode)
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900321 return 0;
322
Andy Flemingad347bb2008-10-30 16:41:01 -0500323 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
324 cmd.resp_type = MMC_RSP_R1;
325 cmd.cmdarg = len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500326
Sean Anderson86325092020-10-17 08:36:27 -0400327 return mmc_send_cmd_quirks(mmc, &cmd, NULL,
328 MMC_QUIRK_RETRY_SET_BLOCKLEN, 4);
Andy Flemingad347bb2008-10-30 16:41:01 -0500329}
330
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100331#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot71264bb2017-09-21 16:30:12 +0200332static const u8 tuning_blk_pattern_4bit[] = {
333 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
334 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
335 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
336 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
337 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
338 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
339 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
340 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
341};
342
343static const u8 tuning_blk_pattern_8bit[] = {
344 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
345 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
346 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
347 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
348 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
349 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
350 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
351 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
352 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
353 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
354 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
355 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
356 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
357 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
358 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
359 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
360};
361
362int mmc_send_tuning(struct mmc *mmc, u32 opcode, int *cmd_error)
363{
364 struct mmc_cmd cmd;
365 struct mmc_data data;
366 const u8 *tuning_block_pattern;
367 int size, err;
368
369 if (mmc->bus_width == 8) {
370 tuning_block_pattern = tuning_blk_pattern_8bit;
371 size = sizeof(tuning_blk_pattern_8bit);
372 } else if (mmc->bus_width == 4) {
373 tuning_block_pattern = tuning_blk_pattern_4bit;
374 size = sizeof(tuning_blk_pattern_4bit);
375 } else {
376 return -EINVAL;
377 }
378
379 ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size);
380
381 cmd.cmdidx = opcode;
382 cmd.cmdarg = 0;
383 cmd.resp_type = MMC_RSP_R1;
384
385 data.dest = (void *)data_buf;
386 data.blocks = 1;
387 data.blocksize = size;
388 data.flags = MMC_DATA_READ;
389
390 err = mmc_send_cmd(mmc, &cmd, &data);
391 if (err)
392 return err;
393
394 if (memcmp(data_buf, tuning_block_pattern, size))
395 return -EIO;
396
397 return 0;
398}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100399#endif
Jean-Jacques Hiblot71264bb2017-09-21 16:30:12 +0200400
Sascha Silbe4bdf6fd2013-06-14 13:07:25 +0200401static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillips87ea3892012-10-29 13:34:43 +0000402 lbaint_t blkcnt)
Andy Flemingad347bb2008-10-30 16:41:01 -0500403{
404 struct mmc_cmd cmd;
405 struct mmc_data data;
406
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700407 if (blkcnt > 1)
408 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
409 else
410 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Flemingad347bb2008-10-30 16:41:01 -0500411
412 if (mmc->high_capacity)
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700413 cmd.cmdarg = start;
Andy Flemingad347bb2008-10-30 16:41:01 -0500414 else
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700415 cmd.cmdarg = start * mmc->read_bl_len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500416
417 cmd.resp_type = MMC_RSP_R1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500418
419 data.dest = dst;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700420 data.blocks = blkcnt;
Andy Flemingad347bb2008-10-30 16:41:01 -0500421 data.blocksize = mmc->read_bl_len;
422 data.flags = MMC_DATA_READ;
423
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700424 if (mmc_send_cmd(mmc, &cmd, &data))
425 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500426
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700427 if (blkcnt > 1) {
428 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
429 cmd.cmdarg = 0;
430 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700431 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100432#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +0100433 pr_err("mmc fail to send stop cmd\n");
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100434#endif
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700435 return 0;
436 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500437 }
438
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700439 return blkcnt;
Andy Flemingad347bb2008-10-30 16:41:01 -0500440}
441
Marek Vasut31976d92020-04-04 12:45:05 +0200442#if !CONFIG_IS_ENABLED(DM_MMC)
443static int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
444{
445 if (mmc->cfg->ops->get_b_max)
446 return mmc->cfg->ops->get_b_max(mmc, dst, blkcnt);
447 else
448 return mmc->cfg->b_max;
449}
450#endif
451
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600452#if CONFIG_IS_ENABLED(BLK)
Simon Glass62e293a2016-06-12 23:30:15 -0600453ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass59bc6f22016-05-01 13:52:41 -0600454#else
Simon Glass62e293a2016-06-12 23:30:15 -0600455ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
456 void *dst)
Simon Glass59bc6f22016-05-01 13:52:41 -0600457#endif
Andy Flemingad347bb2008-10-30 16:41:01 -0500458{
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600459#if CONFIG_IS_ENABLED(BLK)
Simon Glass71fa5b42020-12-03 16:55:18 -0700460 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass59bc6f22016-05-01 13:52:41 -0600461#endif
Simon Glass2f26fff2016-02-29 15:25:51 -0700462 int dev_num = block_dev->devnum;
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700463 int err;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700464 lbaint_t cur, blocks_todo = blkcnt;
Marek Vasut31976d92020-04-04 12:45:05 +0200465 uint b_max;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700466
467 if (blkcnt == 0)
468 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500469
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700470 struct mmc *mmc = find_mmc_device(dev_num);
Andy Flemingad347bb2008-10-30 16:41:01 -0500471 if (!mmc)
472 return 0;
473
Marek Vasutf537e392016-12-01 02:06:33 +0100474 if (CONFIG_IS_ENABLED(MMC_TINY))
475 err = mmc_switch_part(mmc, block_dev->hwpart);
476 else
477 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
478
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700479 if (err < 0)
480 return 0;
481
Simon Glasse5db1152016-05-01 13:52:35 -0600482 if ((start + blkcnt) > block_dev->lba) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100483#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +0100484 pr_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
485 start + blkcnt, block_dev->lba);
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100486#endif
Lei Wene1cc9c82010-09-13 22:07:27 +0800487 return 0;
488 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500489
Simon Glassa4343c42015-06-23 15:38:50 -0600490 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +0900491 pr_debug("%s: Failed to set blocklen\n", __func__);
Andy Flemingad347bb2008-10-30 16:41:01 -0500492 return 0;
Simon Glassa4343c42015-06-23 15:38:50 -0600493 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500494
Marek Vasut31976d92020-04-04 12:45:05 +0200495 b_max = mmc_get_b_max(mmc, dst, blkcnt);
496
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700497 do {
Marek Vasut31976d92020-04-04 12:45:05 +0200498 cur = (blocks_todo > b_max) ? b_max : blocks_todo;
Simon Glassa4343c42015-06-23 15:38:50 -0600499 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +0900500 pr_debug("%s: Failed to read blocks\n", __func__);
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700501 return 0;
Simon Glassa4343c42015-06-23 15:38:50 -0600502 }
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700503 blocks_todo -= cur;
504 start += cur;
505 dst += cur * mmc->read_bl_len;
506 } while (blocks_todo > 0);
Andy Flemingad347bb2008-10-30 16:41:01 -0500507
508 return blkcnt;
509}
510
Kim Phillips87ea3892012-10-29 13:34:43 +0000511static int mmc_go_idle(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500512{
513 struct mmc_cmd cmd;
514 int err;
515
516 udelay(1000);
517
518 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
519 cmd.cmdarg = 0;
520 cmd.resp_type = MMC_RSP_NONE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500521
522 err = mmc_send_cmd(mmc, &cmd, NULL);
523
524 if (err)
525 return err;
526
527 udelay(2000);
528
529 return 0;
530}
531
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100532#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200533static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
534{
535 struct mmc_cmd cmd;
536 int err = 0;
537
538 /*
539 * Send CMD11 only if the request is to switch the card to
540 * 1.8V signalling.
541 */
542 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
543 return mmc_set_signal_voltage(mmc, signal_voltage);
544
545 cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
546 cmd.cmdarg = 0;
547 cmd.resp_type = MMC_RSP_R1;
548
549 err = mmc_send_cmd(mmc, &cmd, NULL);
550 if (err)
551 return err;
552
553 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
554 return -EIO;
555
556 /*
557 * The card should drive cmd and dat[0:3] low immediately
558 * after the response of cmd11, but wait 100 us to be sure
559 */
560 err = mmc_wait_dat0(mmc, 0, 100);
561 if (err == -ENOSYS)
562 udelay(100);
563 else if (err)
564 return -ETIMEDOUT;
565
566 /*
567 * During a signal voltage level switch, the clock must be gated
568 * for 5 ms according to the SD spec
569 */
Jaehoon Chung239cb2f2018-01-26 19:25:29 +0900570 mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE);
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200571
572 err = mmc_set_signal_voltage(mmc, signal_voltage);
573 if (err)
574 return err;
575
576 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
577 mdelay(10);
Jaehoon Chung239cb2f2018-01-26 19:25:29 +0900578 mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE);
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200579
580 /*
581 * Failure to switch is indicated by the card holding
582 * dat[0:3] low. Wait for at least 1 ms according to spec
583 */
584 err = mmc_wait_dat0(mmc, 1, 1000);
585 if (err == -ENOSYS)
586 udelay(1000);
587 else if (err)
588 return -ETIMEDOUT;
589
590 return 0;
591}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100592#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200593
594static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
Andy Flemingad347bb2008-10-30 16:41:01 -0500595{
596 int timeout = 1000;
597 int err;
598 struct mmc_cmd cmd;
599
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500600 while (1) {
Andy Flemingad347bb2008-10-30 16:41:01 -0500601 cmd.cmdidx = MMC_CMD_APP_CMD;
602 cmd.resp_type = MMC_RSP_R1;
603 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500604
605 err = mmc_send_cmd(mmc, &cmd, NULL);
606
607 if (err)
608 return err;
609
610 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
611 cmd.resp_type = MMC_RSP_R3;
Stefano Babicf8e9a212010-01-20 18:20:39 +0100612
613 /*
614 * Most cards do not answer if some reserved bits
615 * in the ocr are set. However, Some controller
616 * can set bit 7 (reserved for low voltages), but
617 * how to manage low voltages SD card is not yet
618 * specified.
619 */
Thomas Chou1254c3d2010-12-24 13:12:21 +0000620 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200621 (mmc->cfg->voltages & 0xff8000);
Andy Flemingad347bb2008-10-30 16:41:01 -0500622
623 if (mmc->version == SD_VERSION_2)
624 cmd.cmdarg |= OCR_HCS;
625
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200626 if (uhs_en)
627 cmd.cmdarg |= OCR_S18R;
628
Andy Flemingad347bb2008-10-30 16:41:01 -0500629 err = mmc_send_cmd(mmc, &cmd, NULL);
630
631 if (err)
632 return err;
633
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500634 if (cmd.response[0] & OCR_BUSY)
635 break;
Andy Flemingad347bb2008-10-30 16:41:01 -0500636
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500637 if (timeout-- <= 0)
Jaehoon Chung7825d202016-07-19 16:33:36 +0900638 return -EOPNOTSUPP;
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500639
640 udelay(1000);
641 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500642
643 if (mmc->version != SD_VERSION_2)
644 mmc->version = SD_VERSION_1_0;
645
Thomas Chou1254c3d2010-12-24 13:12:21 +0000646 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
647 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
648 cmd.resp_type = MMC_RSP_R3;
649 cmd.cmdarg = 0;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000650
651 err = mmc_send_cmd(mmc, &cmd, NULL);
652
653 if (err)
654 return err;
655 }
656
Rabin Vincentb6eed942009-04-05 13:30:56 +0530657 mmc->ocr = cmd.response[0];
Andy Flemingad347bb2008-10-30 16:41:01 -0500658
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100659#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200660 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
661 == 0x41000000) {
662 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
663 if (err)
664 return err;
665 }
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100666#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200667
Andy Flemingad347bb2008-10-30 16:41:01 -0500668 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
669 mmc->rca = 0;
670
671 return 0;
672}
673
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500674static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Flemingad347bb2008-10-30 16:41:01 -0500675{
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500676 struct mmc_cmd cmd;
Andy Flemingad347bb2008-10-30 16:41:01 -0500677 int err;
678
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500679 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
680 cmd.resp_type = MMC_RSP_R3;
681 cmd.cmdarg = 0;
Rob Herring5fd3edd2015-03-23 17:56:59 -0500682 if (use_arg && !mmc_host_is_spi(mmc))
683 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200684 (mmc->cfg->voltages &
Andrew Gabbasovec600d12015-03-19 07:44:03 -0500685 (mmc->ocr & OCR_VOLTAGE_MASK)) |
686 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000687
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500688 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000689 if (err)
690 return err;
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500691 mmc->ocr = cmd.response[0];
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000692 return 0;
693}
694
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +0200695static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000696{
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000697 int err, i;
Haibo Chen71949512020-06-15 17:18:12 +0800698 int timeout = 1000;
699 uint start;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000700
Andy Flemingad347bb2008-10-30 16:41:01 -0500701 /* Some cards seem to need this */
702 mmc_go_idle(mmc);
703
Haibo Chen71949512020-06-15 17:18:12 +0800704 start = get_timer(0);
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200705 /* Asking to the card its capabilities */
Haibo Chen71949512020-06-15 17:18:12 +0800706 for (i = 0; ; i++) {
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500707 err = mmc_send_op_cond_iter(mmc, i != 0);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000708 if (err)
709 return err;
Wolfgang Denk80f70212011-05-19 22:21:41 +0200710
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000711 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasovec600d12015-03-19 07:44:03 -0500712 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -0500713 break;
Haibo Chen71949512020-06-15 17:18:12 +0800714
715 if (get_timer(start) > timeout)
716 return -ETIMEDOUT;
717 udelay(100);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000718 }
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -0500719 mmc->op_cond_pending = 1;
720 return 0;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000721}
Wolfgang Denk80f70212011-05-19 22:21:41 +0200722
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +0200723static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000724{
725 struct mmc_cmd cmd;
726 int timeout = 1000;
Vipul Kumardbad7b42018-05-03 12:20:54 +0530727 ulong start;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000728 int err;
Wolfgang Denk80f70212011-05-19 22:21:41 +0200729
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000730 mmc->op_cond_pending = 0;
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500731 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lu9c720612016-08-02 15:33:18 +0800732 /* Some cards seem to need this */
733 mmc_go_idle(mmc);
734
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500735 start = get_timer(0);
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500736 while (1) {
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500737 err = mmc_send_op_cond_iter(mmc, 1);
738 if (err)
739 return err;
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500740 if (mmc->ocr & OCR_BUSY)
741 break;
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500742 if (get_timer(start) > timeout)
Jaehoon Chung7825d202016-07-19 16:33:36 +0900743 return -EOPNOTSUPP;
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500744 udelay(100);
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500745 }
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500746 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500747
Thomas Chou1254c3d2010-12-24 13:12:21 +0000748 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
749 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
750 cmd.resp_type = MMC_RSP_R3;
751 cmd.cmdarg = 0;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000752
753 err = mmc_send_cmd(mmc, &cmd, NULL);
754
755 if (err)
756 return err;
Andrew Gabbasovec600d12015-03-19 07:44:03 -0500757
758 mmc->ocr = cmd.response[0];
Thomas Chou1254c3d2010-12-24 13:12:21 +0000759 }
760
Andy Flemingad347bb2008-10-30 16:41:01 -0500761 mmc->version = MMC_VERSION_UNKNOWN;
Andy Flemingad347bb2008-10-30 16:41:01 -0500762
763 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrenf6545f12014-01-30 16:11:12 -0700764 mmc->rca = 1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500765
766 return 0;
767}
768
769
Heinrich Schuchardtbf230e12020-03-30 07:24:17 +0200770int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Flemingad347bb2008-10-30 16:41:01 -0500771{
772 struct mmc_cmd cmd;
773 struct mmc_data data;
774 int err;
775
776 /* Get the Card Status Register */
777 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
778 cmd.resp_type = MMC_RSP_R1;
779 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500780
Yoshihiro Shimodaf6bec732012-06-07 19:09:11 +0000781 data.dest = (char *)ext_csd;
Andy Flemingad347bb2008-10-30 16:41:01 -0500782 data.blocks = 1;
Simon Glassa09c2b72013-04-03 08:54:30 +0000783 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Flemingad347bb2008-10-30 16:41:01 -0500784 data.flags = MMC_DATA_READ;
785
786 err = mmc_send_cmd(mmc, &cmd, &data);
787
788 return err;
789}
790
Marek Vasut8a966472019-02-06 11:34:27 +0100791static int __mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value,
792 bool send_status)
Andy Flemingad347bb2008-10-30 16:41:01 -0500793{
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200794 unsigned int status, start;
Andy Flemingad347bb2008-10-30 16:41:01 -0500795 struct mmc_cmd cmd;
Sam Protsenkodb174c62019-08-14 22:52:51 +0300796 int timeout_ms = DEFAULT_CMD6_TIMEOUT_MS;
Jean-Jacques Hiblot7f5b1692019-07-02 10:53:55 +0200797 bool is_part_switch = (set == EXT_CSD_CMD_SET_NORMAL) &&
798 (index == EXT_CSD_PART_CONF);
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000799 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500800
Jean-Jacques Hiblot201559c2019-07-02 10:53:54 +0200801 if (mmc->gen_cmd6_time)
Sam Protsenkodb174c62019-08-14 22:52:51 +0300802 timeout_ms = mmc->gen_cmd6_time * 10;
Jean-Jacques Hiblot201559c2019-07-02 10:53:54 +0200803
Jean-Jacques Hiblot7f5b1692019-07-02 10:53:55 +0200804 if (is_part_switch && mmc->part_switch_time)
Sam Protsenkodb174c62019-08-14 22:52:51 +0300805 timeout_ms = mmc->part_switch_time * 10;
Jean-Jacques Hiblot7f5b1692019-07-02 10:53:55 +0200806
Andy Flemingad347bb2008-10-30 16:41:01 -0500807 cmd.cmdidx = MMC_CMD_SWITCH;
808 cmd.resp_type = MMC_RSP_R1b;
809 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000810 (index << 16) |
811 (value << 8);
Andy Flemingad347bb2008-10-30 16:41:01 -0500812
Sean Anderson86325092020-10-17 08:36:27 -0400813 ret = mmc_send_cmd_retry(mmc, &cmd, NULL, 3);
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200814 if (ret)
815 return ret;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000816
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200817 start = get_timer(0);
Marek Vasut8a966472019-02-06 11:34:27 +0100818
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200819 /* poll dat0 for rdy/buys status */
Sam Protsenkodb174c62019-08-14 22:52:51 +0300820 ret = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200821 if (ret && ret != -ENOSYS)
822 return ret;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000823
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200824 /*
Kirill Kapranovcd9ea642021-10-09 23:49:59 +0300825 * In cases when neiter allowed to poll by using CMD13 nor we are
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200826 * capable of polling by using mmc_wait_dat0, then rely on waiting the
827 * stated timeout to be sufficient.
828 */
Kirill Kapranovcd9ea642021-10-09 23:49:59 +0300829 if (ret == -ENOSYS && !send_status) {
Sam Protsenkodb174c62019-08-14 22:52:51 +0300830 mdelay(timeout_ms);
Haibo Chend8de5e42020-09-22 18:11:42 +0800831 return 0;
832 }
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200833
834 /* Finally wait until the card is ready or indicates a failure
835 * to switch. It doesn't hurt to use CMD13 here even if send_status
Sam Protsenkodb174c62019-08-14 22:52:51 +0300836 * is false, because by now (after 'timeout_ms' ms) the bus should be
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200837 * reliable.
838 */
839 do {
840 ret = mmc_send_status(mmc, &status);
841
842 if (!ret && (status & MMC_STATUS_SWITCH_ERROR)) {
843 pr_debug("switch failed %d/%d/0x%x !\n", set, index,
844 value);
845 return -EIO;
846 }
Stefan Boscha463bbe2021-01-23 13:37:41 +0100847 if (!ret && (status & MMC_STATUS_RDY_FOR_DATA) &&
848 (status & MMC_STATUS_CURR_STATE) == MMC_STATE_TRANS)
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200849 return 0;
850 udelay(100);
Sam Protsenkodb174c62019-08-14 22:52:51 +0300851 } while (get_timer(start) < timeout_ms);
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000852
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200853 return -ETIMEDOUT;
Andy Flemingad347bb2008-10-30 16:41:01 -0500854}
855
Marek Vasut8a966472019-02-06 11:34:27 +0100856int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
857{
858 return __mmc_switch(mmc, set, index, value, true);
859}
860
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +0200861int mmc_boot_wp(struct mmc *mmc)
862{
863 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, 1);
864}
865
Marek Vasuta318a7a2018-04-15 00:37:11 +0200866#if !CONFIG_IS_ENABLED(MMC_TINY)
Marek Vasut111572f2019-01-03 21:19:24 +0100867static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode,
868 bool hsdowngrade)
Andy Flemingad347bb2008-10-30 16:41:01 -0500869{
Andy Flemingad347bb2008-10-30 16:41:01 -0500870 int err;
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200871 int speed_bits;
872
873 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
874
875 switch (mode) {
876 case MMC_HS:
877 case MMC_HS_52:
878 case MMC_DDR_52:
879 speed_bits = EXT_CSD_TIMING_HS;
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +0200880 break;
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +0100881#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +0200882 case MMC_HS_200:
883 speed_bits = EXT_CSD_TIMING_HS200;
884 break;
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +0100885#endif
Peng Fan46801252018-08-10 14:07:54 +0800886#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
887 case MMC_HS_400:
888 speed_bits = EXT_CSD_TIMING_HS400;
889 break;
890#endif
Peng Faneede83b2019-07-10 14:43:07 +0800891#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
892 case MMC_HS_400_ES:
893 speed_bits = EXT_CSD_TIMING_HS400;
894 break;
895#endif
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200896 case MMC_LEGACY:
897 speed_bits = EXT_CSD_TIMING_LEGACY;
898 break;
899 default:
900 return -EINVAL;
901 }
Marek Vasut8a966472019-02-06 11:34:27 +0100902
903 err = __mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
904 speed_bits, !hsdowngrade);
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200905 if (err)
906 return err;
907
Marek Vasut111572f2019-01-03 21:19:24 +0100908#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
909 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
910 /*
911 * In case the eMMC is in HS200/HS400 mode and we are downgrading
912 * to HS mode, the card clock are still running much faster than
913 * the supported HS mode clock, so we can not reliably read out
914 * Extended CSD. Reconfigure the controller to run at HS mode.
915 */
916 if (hsdowngrade) {
917 mmc_select_mode(mmc, MMC_HS);
918 mmc_set_clock(mmc, mmc_mode2freq(mmc, MMC_HS), false);
919 }
920#endif
921
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200922 if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
923 /* Now check to see that it worked */
924 err = mmc_send_ext_csd(mmc, test_csd);
925 if (err)
926 return err;
927
928 /* No high-speed support */
929 if (!test_csd[EXT_CSD_HS_TIMING])
930 return -ENOTSUPP;
931 }
932
933 return 0;
934}
935
936static int mmc_get_capabilities(struct mmc *mmc)
937{
938 u8 *ext_csd = mmc->ext_csd;
939 char cardtype;
Andy Flemingad347bb2008-10-30 16:41:01 -0500940
Jean-Jacques Hiblot3f2ffc22017-11-30 17:43:56 +0100941 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Flemingad347bb2008-10-30 16:41:01 -0500942
Thomas Chou1254c3d2010-12-24 13:12:21 +0000943 if (mmc_host_is_spi(mmc))
944 return 0;
945
Andy Flemingad347bb2008-10-30 16:41:01 -0500946 /* Only version 4 supports high-speed */
947 if (mmc->version < MMC_VERSION_4)
948 return 0;
949
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200950 if (!ext_csd) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +0100951 pr_err("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200952 return -ENOTSUPP;
953 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500954
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200955 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
Andy Flemingad347bb2008-10-30 16:41:01 -0500956
Peng Fan46801252018-08-10 14:07:54 +0800957 cardtype = ext_csd[EXT_CSD_CARD_TYPE];
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +0200958 mmc->cardtype = cardtype;
Andy Flemingad347bb2008-10-30 16:41:01 -0500959
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +0100960#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +0200961 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
962 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
963 mmc->card_caps |= MMC_MODE_HS200;
964 }
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +0100965#endif
Peng Faneede83b2019-07-10 14:43:07 +0800966#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
967 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Peng Fan46801252018-08-10 14:07:54 +0800968 if (cardtype & (EXT_CSD_CARD_TYPE_HS400_1_2V |
969 EXT_CSD_CARD_TYPE_HS400_1_8V)) {
970 mmc->card_caps |= MMC_MODE_HS400;
971 }
972#endif
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900973 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200974 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900975 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200976 mmc->card_caps |= MMC_MODE_HS_52MHz;
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900977 }
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200978 if (cardtype & EXT_CSD_CARD_TYPE_26)
979 mmc->card_caps |= MMC_MODE_HS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500980
Peng Faneede83b2019-07-10 14:43:07 +0800981#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
982 if (ext_csd[EXT_CSD_STROBE_SUPPORT] &&
983 (mmc->card_caps & MMC_MODE_HS400)) {
984 mmc->card_caps |= MMC_MODE_HS400_ES;
985 }
986#endif
987
Andy Flemingad347bb2008-10-30 16:41:01 -0500988 return 0;
989}
Marek Vasuta318a7a2018-04-15 00:37:11 +0200990#endif
Andy Flemingad347bb2008-10-30 16:41:01 -0500991
Stephen Warrene315ae82013-06-11 15:14:01 -0600992static int mmc_set_capacity(struct mmc *mmc, int part_num)
993{
994 switch (part_num) {
995 case 0:
996 mmc->capacity = mmc->capacity_user;
997 break;
998 case 1:
999 case 2:
1000 mmc->capacity = mmc->capacity_boot;
1001 break;
1002 case 3:
1003 mmc->capacity = mmc->capacity_rpmb;
1004 break;
1005 case 4:
1006 case 5:
1007 case 6:
1008 case 7:
1009 mmc->capacity = mmc->capacity_gp[part_num - 4];
1010 break;
1011 default:
1012 return -1;
1013 }
1014
Simon Glasse5db1152016-05-01 13:52:35 -06001015 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrene315ae82013-06-11 15:14:01 -06001016
1017 return 0;
1018}
1019
Simon Glass62e293a2016-06-12 23:30:15 -06001020int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wen31b99802011-05-02 16:26:26 +00001021{
Stephen Warrene315ae82013-06-11 15:14:01 -06001022 int ret;
Jean-Jacques Hiblotfaf5c952019-07-02 10:53:58 +02001023 int retry = 3;
Lei Wen31b99802011-05-02 16:26:26 +00001024
Jean-Jacques Hiblotfaf5c952019-07-02 10:53:58 +02001025 do {
1026 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1027 EXT_CSD_PART_CONF,
1028 (mmc->part_config & ~PART_ACCESS_MASK)
1029 | (part_num & PART_ACCESS_MASK));
1030 } while (ret && retry--);
Peter Bigot45fde892014-09-02 18:31:23 -05001031
1032 /*
1033 * Set the capacity if the switch succeeded or was intended
1034 * to return to representing the raw device.
1035 */
Stephen Warren1e0f92a2015-12-07 11:38:49 -07001036 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot45fde892014-09-02 18:31:23 -05001037 ret = mmc_set_capacity(mmc, part_num);
Simon Glass984db5d2016-05-01 13:52:37 -06001038 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren1e0f92a2015-12-07 11:38:49 -07001039 }
Stephen Warrene315ae82013-06-11 15:14:01 -06001040
Peter Bigot45fde892014-09-02 18:31:23 -05001041 return ret;
Lei Wen31b99802011-05-02 16:26:26 +00001042}
1043
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001044#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001045int mmc_hwpart_config(struct mmc *mmc,
1046 const struct mmc_hwpart_conf *conf,
1047 enum mmc_hwpart_conf_mode mode)
1048{
1049 u8 part_attrs = 0;
1050 u32 enh_size_mult;
1051 u32 enh_start_addr;
1052 u32 gp_size_mult[4];
1053 u32 max_enh_size_mult;
1054 u32 tot_enh_size_mult = 0;
Diego Santa Cruz80200272014-12-23 10:50:31 +01001055 u8 wr_rel_set;
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001056 int i, pidx, err;
1057 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1058
1059 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
1060 return -EINVAL;
1061
1062 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001063 pr_err("eMMC >= 4.4 required for enhanced user data area\n");
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001064 return -EMEDIUMTYPE;
1065 }
1066
1067 if (!(mmc->part_support & PART_SUPPORT)) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001068 pr_err("Card does not support partitioning\n");
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001069 return -EMEDIUMTYPE;
1070 }
1071
1072 if (!mmc->hc_wp_grp_size) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001073 pr_err("Card does not define HC WP group size\n");
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001074 return -EMEDIUMTYPE;
1075 }
1076
1077 /* check partition alignment and total enhanced size */
1078 if (conf->user.enh_size) {
1079 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
1080 conf->user.enh_start % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001081 pr_err("User data enhanced area not HC WP group "
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001082 "size aligned\n");
1083 return -EINVAL;
1084 }
1085 part_attrs |= EXT_CSD_ENH_USR;
1086 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
1087 if (mmc->high_capacity) {
1088 enh_start_addr = conf->user.enh_start;
1089 } else {
1090 enh_start_addr = (conf->user.enh_start << 9);
1091 }
1092 } else {
1093 enh_size_mult = 0;
1094 enh_start_addr = 0;
1095 }
1096 tot_enh_size_mult += enh_size_mult;
1097
1098 for (pidx = 0; pidx < 4; pidx++) {
1099 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001100 pr_err("GP%i partition not HC WP group size "
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001101 "aligned\n", pidx+1);
1102 return -EINVAL;
1103 }
1104 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
1105 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
1106 part_attrs |= EXT_CSD_ENH_GP(pidx);
1107 tot_enh_size_mult += gp_size_mult[pidx];
1108 }
1109 }
1110
1111 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001112 pr_err("Card does not support enhanced attribute\n");
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001113 return -EMEDIUMTYPE;
1114 }
1115
1116 err = mmc_send_ext_csd(mmc, ext_csd);
1117 if (err)
1118 return err;
1119
1120 max_enh_size_mult =
1121 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
1122 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
1123 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
1124 if (tot_enh_size_mult > max_enh_size_mult) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001125 pr_err("Total enhanced size exceeds maximum (%u > %u)\n",
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001126 tot_enh_size_mult, max_enh_size_mult);
1127 return -EMEDIUMTYPE;
1128 }
1129
Diego Santa Cruz80200272014-12-23 10:50:31 +01001130 /* The default value of EXT_CSD_WR_REL_SET is device
1131 * dependent, the values can only be changed if the
1132 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
1133 * changed only once and before partitioning is completed. */
1134 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1135 if (conf->user.wr_rel_change) {
1136 if (conf->user.wr_rel_set)
1137 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
1138 else
1139 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
1140 }
1141 for (pidx = 0; pidx < 4; pidx++) {
1142 if (conf->gp_part[pidx].wr_rel_change) {
1143 if (conf->gp_part[pidx].wr_rel_set)
1144 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
1145 else
1146 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
1147 }
1148 }
1149
1150 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
1151 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
1152 puts("Card does not support host controlled partition write "
1153 "reliability settings\n");
1154 return -EMEDIUMTYPE;
1155 }
1156
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001157 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
1158 EXT_CSD_PARTITION_SETTING_COMPLETED) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001159 pr_err("Card already partitioned\n");
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001160 return -EPERM;
1161 }
1162
1163 if (mode == MMC_HWPART_CONF_CHECK)
1164 return 0;
1165
1166 /* Partitioning requires high-capacity size definitions */
1167 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
1168 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1169 EXT_CSD_ERASE_GROUP_DEF, 1);
1170
1171 if (err)
1172 return err;
1173
1174 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1175
Jaehoon Chung58b9eb82020-01-17 15:06:54 +09001176#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001177 /* update erase group size to be high-capacity */
1178 mmc->erase_grp_size =
1179 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jaehoon Chung58b9eb82020-01-17 15:06:54 +09001180#endif
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001181
1182 }
1183
1184 /* all OK, write the configuration */
1185 for (i = 0; i < 4; i++) {
1186 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1187 EXT_CSD_ENH_START_ADDR+i,
1188 (enh_start_addr >> (i*8)) & 0xFF);
1189 if (err)
1190 return err;
1191 }
1192 for (i = 0; i < 3; i++) {
1193 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1194 EXT_CSD_ENH_SIZE_MULT+i,
1195 (enh_size_mult >> (i*8)) & 0xFF);
1196 if (err)
1197 return err;
1198 }
1199 for (pidx = 0; pidx < 4; pidx++) {
1200 for (i = 0; i < 3; i++) {
1201 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1202 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1203 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1204 if (err)
1205 return err;
1206 }
1207 }
1208 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1209 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1210 if (err)
1211 return err;
1212
1213 if (mode == MMC_HWPART_CONF_SET)
1214 return 0;
1215
Diego Santa Cruz80200272014-12-23 10:50:31 +01001216 /* The WR_REL_SET is a write-once register but shall be
1217 * written before setting PART_SETTING_COMPLETED. As it is
1218 * write-once we can only write it when completing the
1219 * partitioning. */
1220 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1221 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1222 EXT_CSD_WR_REL_SET, wr_rel_set);
1223 if (err)
1224 return err;
1225 }
1226
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001227 /* Setting PART_SETTING_COMPLETED confirms the partition
1228 * configuration but it only becomes effective after power
1229 * cycle, so we do not adjust the partition related settings
1230 * in the mmc struct. */
1231
1232 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1233 EXT_CSD_PARTITION_SETTING,
1234 EXT_CSD_PARTITION_SETTING_COMPLETED);
1235 if (err)
1236 return err;
1237
1238 return 0;
1239}
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001240#endif
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001241
Simon Glasseba48f92017-07-29 11:35:31 -06001242#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Redingb9c8b772012-01-02 01:15:37 +00001243int mmc_getcd(struct mmc *mmc)
1244{
1245 int cd;
1246
1247 cd = board_mmc_getcd(mmc);
1248
Peter Korsgaardf7b15102013-03-21 04:00:03 +00001249 if (cd < 0) {
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001250 if (mmc->cfg->ops->getcd)
1251 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardf7b15102013-03-21 04:00:03 +00001252 else
1253 cd = 1;
1254 }
Thierry Redingb9c8b772012-01-02 01:15:37 +00001255
1256 return cd;
1257}
Simon Glass394dfc02016-06-12 23:30:22 -06001258#endif
Thierry Redingb9c8b772012-01-02 01:15:37 +00001259
Marek Vasuta318a7a2018-04-15 00:37:11 +02001260#if !CONFIG_IS_ENABLED(MMC_TINY)
Kim Phillips87ea3892012-10-29 13:34:43 +00001261static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Flemingad347bb2008-10-30 16:41:01 -05001262{
1263 struct mmc_cmd cmd;
1264 struct mmc_data data;
1265
1266 /* Switch the frequency */
1267 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1268 cmd.resp_type = MMC_RSP_R1;
1269 cmd.cmdarg = (mode << 31) | 0xffffff;
1270 cmd.cmdarg &= ~(0xf << (group * 4));
1271 cmd.cmdarg |= value << (group * 4);
Andy Flemingad347bb2008-10-30 16:41:01 -05001272
1273 data.dest = (char *)resp;
1274 data.blocksize = 64;
1275 data.blocks = 1;
1276 data.flags = MMC_DATA_READ;
1277
1278 return mmc_send_cmd(mmc, &cmd, &data);
1279}
1280
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001281static int sd_get_capabilities(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05001282{
1283 int err;
1284 struct mmc_cmd cmd;
Suniel Mahesh2f423da2017-10-05 11:32:00 +05301285 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1286 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Flemingad347bb2008-10-30 16:41:01 -05001287 struct mmc_data data;
1288 int timeout;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001289#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001290 u32 sd3_bus_mode;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001291#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001292
Faiz Abbas01db77e2020-02-26 13:44:32 +05301293 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Flemingad347bb2008-10-30 16:41:01 -05001294
Thomas Chou1254c3d2010-12-24 13:12:21 +00001295 if (mmc_host_is_spi(mmc))
1296 return 0;
1297
Andy Flemingad347bb2008-10-30 16:41:01 -05001298 /* Read the SCR to find out if this card supports higher speeds */
1299 cmd.cmdidx = MMC_CMD_APP_CMD;
1300 cmd.resp_type = MMC_RSP_R1;
1301 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -05001302
1303 err = mmc_send_cmd(mmc, &cmd, NULL);
1304
1305 if (err)
1306 return err;
1307
1308 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1309 cmd.resp_type = MMC_RSP_R1;
1310 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -05001311
Anton staaf9b00f0d2011-10-03 13:54:59 +00001312 data.dest = (char *)scr;
Andy Flemingad347bb2008-10-30 16:41:01 -05001313 data.blocksize = 8;
1314 data.blocks = 1;
1315 data.flags = MMC_DATA_READ;
1316
Sean Anderson86325092020-10-17 08:36:27 -04001317 err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);
Andy Flemingad347bb2008-10-30 16:41:01 -05001318
Sean Anderson86325092020-10-17 08:36:27 -04001319 if (err)
Andy Flemingad347bb2008-10-30 16:41:01 -05001320 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05001321
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +03001322 mmc->scr[0] = __be32_to_cpu(scr[0]);
1323 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Flemingad347bb2008-10-30 16:41:01 -05001324
1325 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng4a4ef872016-03-17 21:53:13 -07001326 case 0:
1327 mmc->version = SD_VERSION_1_0;
1328 break;
1329 case 1:
1330 mmc->version = SD_VERSION_1_10;
1331 break;
1332 case 2:
1333 mmc->version = SD_VERSION_2;
1334 if ((mmc->scr[0] >> 15) & 0x1)
1335 mmc->version = SD_VERSION_3;
1336 break;
1337 default:
1338 mmc->version = SD_VERSION_1_0;
1339 break;
Andy Flemingad347bb2008-10-30 16:41:01 -05001340 }
1341
Alagu Sankar24bb5ab2010-05-12 15:08:24 +05301342 if (mmc->scr[0] & SD_DATA_4BIT)
1343 mmc->card_caps |= MMC_MODE_4BIT;
1344
Andy Flemingad347bb2008-10-30 16:41:01 -05001345 /* Version 1.0 doesn't support switching */
1346 if (mmc->version == SD_VERSION_1_0)
1347 return 0;
1348
1349 timeout = 4;
1350 while (timeout--) {
1351 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaf9b00f0d2011-10-03 13:54:59 +00001352 (u8 *)switch_status);
Andy Flemingad347bb2008-10-30 16:41:01 -05001353
1354 if (err)
1355 return err;
1356
1357 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +03001358 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Flemingad347bb2008-10-30 16:41:01 -05001359 break;
1360 }
1361
Andy Flemingad347bb2008-10-30 16:41:01 -05001362 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001363 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1364 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Flemingad347bb2008-10-30 16:41:01 -05001365
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001366#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001367 /* Version before 3.0 don't support UHS modes */
1368 if (mmc->version < SD_VERSION_3)
1369 return 0;
1370
1371 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1372 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1373 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1374 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1375 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1376 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1377 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1378 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1379 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1380 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1381 mmc->card_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001382#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001383
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001384 return 0;
1385}
1386
1387static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1388{
1389 int err;
1390
1391 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001392 int speed;
Macpaul Lin24e92ec2011-11-28 16:31:09 +00001393
Marek Vasut4105e972018-11-18 03:25:08 +01001394 /* SD version 1.00 and 1.01 does not support CMD 6 */
1395 if (mmc->version == SD_VERSION_1_0)
1396 return 0;
1397
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001398 switch (mode) {
Faiz Abbas01db77e2020-02-26 13:44:32 +05301399 case MMC_LEGACY:
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001400 speed = UHS_SDR12_BUS_SPEED;
1401 break;
1402 case SD_HS:
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +01001403 speed = HIGH_SPEED_BUS_SPEED;
1404 break;
1405#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1406 case UHS_SDR12:
1407 speed = UHS_SDR12_BUS_SPEED;
1408 break;
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001409 case UHS_SDR25:
1410 speed = UHS_SDR25_BUS_SPEED;
1411 break;
1412 case UHS_SDR50:
1413 speed = UHS_SDR50_BUS_SPEED;
1414 break;
1415 case UHS_DDR50:
1416 speed = UHS_DDR50_BUS_SPEED;
1417 break;
1418 case UHS_SDR104:
1419 speed = UHS_SDR104_BUS_SPEED;
1420 break;
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +01001421#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001422 default:
1423 return -EINVAL;
1424 }
1425
1426 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001427 if (err)
1428 return err;
1429
Jean-Jacques Hiblote7f664e2018-02-09 12:09:27 +01001430 if (((__be32_to_cpu(switch_status[4]) >> 24) & 0xF) != speed)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001431 return -ENOTSUPP;
1432
1433 return 0;
1434}
Andy Flemingad347bb2008-10-30 16:41:01 -05001435
Marek Vasut8ff55fb2018-04-15 00:36:45 +02001436static int sd_select_bus_width(struct mmc *mmc, int w)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001437{
1438 int err;
1439 struct mmc_cmd cmd;
1440
1441 if ((w != 4) && (w != 1))
1442 return -EINVAL;
1443
1444 cmd.cmdidx = MMC_CMD_APP_CMD;
1445 cmd.resp_type = MMC_RSP_R1;
1446 cmd.cmdarg = mmc->rca << 16;
1447
1448 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -05001449 if (err)
1450 return err;
1451
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001452 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1453 cmd.resp_type = MMC_RSP_R1;
1454 if (w == 4)
1455 cmd.cmdarg = 2;
1456 else if (w == 1)
1457 cmd.cmdarg = 0;
1458 err = mmc_send_cmd(mmc, &cmd, NULL);
1459 if (err)
1460 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05001461
1462 return 0;
1463}
Marek Vasuta318a7a2018-04-15 00:37:11 +02001464#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001465
Jean-Jacques Hiblotcb534f02018-01-04 15:23:33 +01001466#if CONFIG_IS_ENABLED(MMC_WRITE)
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001467static int sd_read_ssr(struct mmc *mmc)
1468{
Jean-Jacques Hiblotcb534f02018-01-04 15:23:33 +01001469 static const unsigned int sd_au_size[] = {
1470 0, SZ_16K / 512, SZ_32K / 512,
1471 SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
1472 SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
1473 SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
1474 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512,
1475 SZ_64M / 512,
1476 };
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001477 int err, i;
1478 struct mmc_cmd cmd;
1479 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1480 struct mmc_data data;
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001481 unsigned int au, eo, et, es;
1482
1483 cmd.cmdidx = MMC_CMD_APP_CMD;
1484 cmd.resp_type = MMC_RSP_R1;
1485 cmd.cmdarg = mmc->rca << 16;
1486
Sean Anderson86325092020-10-17 08:36:27 -04001487 err = mmc_send_cmd_quirks(mmc, &cmd, NULL, MMC_QUIRK_RETRY_APP_CMD, 4);
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001488 if (err)
1489 return err;
1490
1491 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1492 cmd.resp_type = MMC_RSP_R1;
1493 cmd.cmdarg = 0;
1494
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001495 data.dest = (char *)ssr;
1496 data.blocksize = 64;
1497 data.blocks = 1;
1498 data.flags = MMC_DATA_READ;
1499
Sean Anderson86325092020-10-17 08:36:27 -04001500 err = mmc_send_cmd_retry(mmc, &cmd, &data, 3);
1501 if (err)
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001502 return err;
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001503
1504 for (i = 0; i < 16; i++)
1505 ssr[i] = be32_to_cpu(ssr[i]);
1506
1507 au = (ssr[2] >> 12) & 0xF;
1508 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1509 mmc->ssr.au = sd_au_size[au];
1510 es = (ssr[3] >> 24) & 0xFF;
1511 es |= (ssr[2] & 0xFF) << 8;
1512 et = (ssr[3] >> 18) & 0x3F;
1513 if (es && et) {
1514 eo = (ssr[3] >> 16) & 0x3;
1515 mmc->ssr.erase_timeout = (et * 1000) / es;
1516 mmc->ssr.erase_offset = eo * 1000;
1517 }
1518 } else {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001519 pr_debug("Invalid Allocation Unit Size.\n");
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001520 }
1521
1522 return 0;
1523}
Jean-Jacques Hiblotcb534f02018-01-04 15:23:33 +01001524#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001525/* frequency bases */
1526/* divided by 10 to be nice to platforms without floating point */
Mike Frysingerb588caf2010-10-20 01:15:53 +00001527static const int fbase[] = {
Andy Flemingad347bb2008-10-30 16:41:01 -05001528 10000,
1529 100000,
1530 1000000,
1531 10000000,
1532};
1533
1534/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1535 * to platforms without floating point.
1536 */
Simon Glass03317cc2016-05-14 14:02:57 -06001537static const u8 multipliers[] = {
Andy Flemingad347bb2008-10-30 16:41:01 -05001538 0, /* reserved */
1539 10,
1540 12,
1541 13,
1542 15,
1543 20,
1544 25,
1545 30,
1546 35,
1547 40,
1548 45,
1549 50,
1550 55,
1551 60,
1552 70,
1553 80,
1554};
1555
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001556static inline int bus_width(uint cap)
1557{
1558 if (cap == MMC_MODE_8BIT)
1559 return 8;
1560 if (cap == MMC_MODE_4BIT)
1561 return 4;
1562 if (cap == MMC_MODE_1BIT)
1563 return 1;
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001564 pr_warn("invalid bus witdh capability 0x%x\n", cap);
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001565 return 0;
1566}
1567
Simon Glasseba48f92017-07-29 11:35:31 -06001568#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001569#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham Iae7174f2017-09-21 16:30:05 +02001570static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1571{
1572 return -ENOTSUPP;
1573}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001574#endif
Kishon Vijay Abraham Iae7174f2017-09-21 16:30:05 +02001575
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001576static int mmc_set_ios(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05001577{
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001578 int ret = 0;
1579
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001580 if (mmc->cfg->ops->set_ios)
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001581 ret = mmc->cfg->ops->set_ios(mmc);
1582
1583 return ret;
Andy Flemingad347bb2008-10-30 16:41:01 -05001584}
Yann Gautier6f558332019-09-19 17:56:12 +02001585
1586static int mmc_host_power_cycle(struct mmc *mmc)
1587{
1588 int ret = 0;
1589
1590 if (mmc->cfg->ops->host_power_cycle)
1591 ret = mmc->cfg->ops->host_power_cycle(mmc);
1592
1593 return ret;
1594}
Simon Glass394dfc02016-06-12 23:30:22 -06001595#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001596
Kishon Vijay Abraham Id6246bf2017-09-21 16:30:03 +02001597int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
Andy Flemingad347bb2008-10-30 16:41:01 -05001598{
Jaehoon Chungab4d4052018-01-23 14:04:30 +09001599 if (!disable) {
Jaehoon Chung8a933292018-01-17 19:36:58 +09001600 if (clock > mmc->cfg->f_max)
1601 clock = mmc->cfg->f_max;
Andy Flemingad347bb2008-10-30 16:41:01 -05001602
Jaehoon Chung8a933292018-01-17 19:36:58 +09001603 if (clock < mmc->cfg->f_min)
1604 clock = mmc->cfg->f_min;
1605 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001606
1607 mmc->clock = clock;
Kishon Vijay Abraham Id6246bf2017-09-21 16:30:03 +02001608 mmc->clk_disable = disable;
Andy Flemingad347bb2008-10-30 16:41:01 -05001609
Jaehoon Chungc8477d62018-01-26 19:25:30 +09001610 debug("clock is %s (%dHz)\n", disable ? "disabled" : "enabled", clock);
1611
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001612 return mmc_set_ios(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05001613}
1614
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001615static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Flemingad347bb2008-10-30 16:41:01 -05001616{
1617 mmc->bus_width = width;
1618
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001619 return mmc_set_ios(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05001620}
1621
Jean-Jacques Hiblot00de5042017-09-21 16:29:54 +02001622#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1623/*
1624 * helper function to display the capabilities in a human
1625 * friendly manner. The capabilities include bus width and
1626 * supported modes.
1627 */
1628void mmc_dump_capabilities(const char *text, uint caps)
1629{
1630 enum bus_mode mode;
1631
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001632 pr_debug("%s: widths [", text);
Jean-Jacques Hiblot00de5042017-09-21 16:29:54 +02001633 if (caps & MMC_MODE_8BIT)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001634 pr_debug("8, ");
Jean-Jacques Hiblot00de5042017-09-21 16:29:54 +02001635 if (caps & MMC_MODE_4BIT)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001636 pr_debug("4, ");
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001637 if (caps & MMC_MODE_1BIT)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001638 pr_debug("1, ");
1639 pr_debug("\b\b] modes [");
Jean-Jacques Hiblot00de5042017-09-21 16:29:54 +02001640 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1641 if (MMC_CAP(mode) & caps)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001642 pr_debug("%s, ", mmc_mode_name(mode));
1643 pr_debug("\b\b]\n");
Jean-Jacques Hiblot00de5042017-09-21 16:29:54 +02001644}
1645#endif
1646
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001647struct mode_width_tuning {
1648 enum bus_mode mode;
1649 uint widths;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001650#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +02001651 uint tuning;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001652#endif
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001653};
1654
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001655#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001656int mmc_voltage_to_mv(enum mmc_voltage voltage)
1657{
1658 switch (voltage) {
1659 case MMC_SIGNAL_VOLTAGE_000: return 0;
1660 case MMC_SIGNAL_VOLTAGE_330: return 3300;
1661 case MMC_SIGNAL_VOLTAGE_180: return 1800;
1662 case MMC_SIGNAL_VOLTAGE_120: return 1200;
1663 }
1664 return -EINVAL;
1665}
1666
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +02001667static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1668{
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001669 int err;
1670
1671 if (mmc->signal_voltage == signal_voltage)
1672 return 0;
1673
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +02001674 mmc->signal_voltage = signal_voltage;
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001675 err = mmc_set_ios(mmc);
1676 if (err)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001677 pr_debug("unable to set voltage (err %d)\n", err);
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001678
1679 return err;
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +02001680}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001681#else
1682static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1683{
1684 return 0;
1685}
1686#endif
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +02001687
Marek Vasuta318a7a2018-04-15 00:37:11 +02001688#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001689static const struct mode_width_tuning sd_modes_by_pref[] = {
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001690#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1691#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001692 {
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001693 .mode = UHS_SDR104,
1694 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1695 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1696 },
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001697#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001698 {
1699 .mode = UHS_SDR50,
1700 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1701 },
1702 {
1703 .mode = UHS_DDR50,
1704 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1705 },
1706 {
1707 .mode = UHS_SDR25,
1708 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1709 },
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001710#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001711 {
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001712 .mode = SD_HS,
1713 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1714 },
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001715#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001716 {
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001717 .mode = UHS_SDR12,
1718 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1719 },
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001720#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001721 {
Faiz Abbas01db77e2020-02-26 13:44:32 +05301722 .mode = MMC_LEGACY,
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001723 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1724 }
1725};
1726
1727#define for_each_sd_mode_by_pref(caps, mwt) \
1728 for (mwt = sd_modes_by_pref;\
1729 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1730 mwt++) \
1731 if (caps & MMC_CAP(mwt->mode))
1732
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02001733static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001734{
1735 int err;
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001736 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1737 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001738#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001739 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001740#else
1741 bool uhs_en = false;
1742#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001743 uint caps;
1744
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +01001745#ifdef DEBUG
1746 mmc_dump_capabilities("sd card", card_caps);
Jean-Jacques Hiblotd7e5e032017-11-30 17:43:57 +01001747 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +01001748#endif
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001749
Anup Pateld9c92c72019-07-08 04:10:43 +00001750 if (mmc_host_is_spi(mmc)) {
1751 mmc_set_bus_width(mmc, 1);
Faiz Abbas01db77e2020-02-26 13:44:32 +05301752 mmc_select_mode(mmc, MMC_LEGACY);
Anup Pateld9c92c72019-07-08 04:10:43 +00001753 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
Pragnesh Patela01f57e2020-06-29 15:17:26 +05301754#if CONFIG_IS_ENABLED(MMC_WRITE)
1755 err = sd_read_ssr(mmc);
1756 if (err)
1757 pr_warn("unable to read ssr\n");
1758#endif
Anup Pateld9c92c72019-07-08 04:10:43 +00001759 return 0;
1760 }
1761
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001762 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblotd7e5e032017-11-30 17:43:57 +01001763 caps = card_caps & mmc->host_caps;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001764
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001765 if (!uhs_en)
1766 caps &= ~UHS_CAPS;
1767
1768 for_each_sd_mode_by_pref(caps, mwt) {
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001769 uint *w;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001770
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001771 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001772 if (*w & caps & mwt->widths) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001773 pr_debug("trying mode %s width %d (at %d MHz)\n",
1774 mmc_mode_name(mwt->mode),
1775 bus_width(*w),
1776 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001777
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001778 /* configure the bus width (card + host) */
1779 err = sd_select_bus_width(mmc, bus_width(*w));
1780 if (err)
1781 goto error;
1782 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001783
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001784 /* configure the bus mode (card) */
1785 err = sd_set_card_speed(mmc, mwt->mode);
1786 if (err)
1787 goto error;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001788
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001789 /* configure the bus mode (host) */
1790 mmc_select_mode(mmc, mwt->mode);
Jaehoon Chung239cb2f2018-01-26 19:25:29 +09001791 mmc_set_clock(mmc, mmc->tran_speed,
1792 MMC_CLK_ENABLE);
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001793
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001794#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001795 /* execute tuning if needed */
1796 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1797 err = mmc_execute_tuning(mmc,
1798 mwt->tuning);
1799 if (err) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001800 pr_debug("tuning failed\n");
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001801 goto error;
1802 }
1803 }
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001804#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001805
Jean-Jacques Hiblotcb534f02018-01-04 15:23:33 +01001806#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001807 err = sd_read_ssr(mmc);
Peng Fan2d2fe8e2018-03-05 16:20:40 +08001808 if (err)
Jean-Jacques Hiblotcb534f02018-01-04 15:23:33 +01001809 pr_warn("unable to read ssr\n");
1810#endif
1811 if (!err)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001812 return 0;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001813
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001814error:
1815 /* revert to a safer bus speed */
Faiz Abbas01db77e2020-02-26 13:44:32 +05301816 mmc_select_mode(mmc, MMC_LEGACY);
Jaehoon Chung239cb2f2018-01-26 19:25:29 +09001817 mmc_set_clock(mmc, mmc->tran_speed,
1818 MMC_CLK_ENABLE);
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001819 }
1820 }
1821 }
1822
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001823 pr_err("unable to select a mode\n");
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001824 return -ENOTSUPP;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001825}
1826
Jean-Jacques Hiblot933d1262017-09-21 16:29:52 +02001827/*
1828 * read the compare the part of ext csd that is constant.
1829 * This can be used to check that the transfer is working
1830 * as expected.
1831 */
1832static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001833{
Jean-Jacques Hiblot933d1262017-09-21 16:29:52 +02001834 int err;
Jean-Jacques Hibloted9506b2017-09-21 16:29:51 +02001835 const u8 *ext_csd = mmc->ext_csd;
Jean-Jacques Hiblot933d1262017-09-21 16:29:52 +02001836 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1837
Jean-Jacques Hiblot7ab1b622017-11-30 17:43:58 +01001838 if (mmc->version < MMC_VERSION_4)
1839 return 0;
1840
Jean-Jacques Hiblot933d1262017-09-21 16:29:52 +02001841 err = mmc_send_ext_csd(mmc, test_csd);
1842 if (err)
1843 return err;
1844
1845 /* Only compare read only fields */
1846 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1847 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1848 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1849 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1850 ext_csd[EXT_CSD_REV]
1851 == test_csd[EXT_CSD_REV] &&
1852 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1853 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1854 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1855 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1856 return 0;
1857
1858 return -EBADMSG;
1859}
1860
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001861#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001862static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1863 uint32_t allowed_mask)
1864{
1865 u32 card_mask = 0;
1866
1867 switch (mode) {
Peng Faneede83b2019-07-10 14:43:07 +08001868 case MMC_HS_400_ES:
Peng Fan46801252018-08-10 14:07:54 +08001869 case MMC_HS_400:
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001870 case MMC_HS_200:
Peng Fan46801252018-08-10 14:07:54 +08001871 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_8V |
1872 EXT_CSD_CARD_TYPE_HS400_1_8V))
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001873 card_mask |= MMC_SIGNAL_VOLTAGE_180;
Peng Fan46801252018-08-10 14:07:54 +08001874 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1875 EXT_CSD_CARD_TYPE_HS400_1_2V))
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001876 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1877 break;
1878 case MMC_DDR_52:
1879 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
1880 card_mask |= MMC_SIGNAL_VOLTAGE_330 |
1881 MMC_SIGNAL_VOLTAGE_180;
1882 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
1883 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1884 break;
1885 default:
1886 card_mask |= MMC_SIGNAL_VOLTAGE_330;
1887 break;
1888 }
1889
1890 while (card_mask & allowed_mask) {
1891 enum mmc_voltage best_match;
1892
1893 best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
1894 if (!mmc_set_signal_voltage(mmc, best_match))
1895 return 0;
1896
1897 allowed_mask &= ~best_match;
1898 }
1899
1900 return -ENOTSUPP;
1901}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001902#else
1903static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1904 uint32_t allowed_mask)
1905{
1906 return 0;
1907}
1908#endif
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001909
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02001910static const struct mode_width_tuning mmc_modes_by_pref[] = {
Peng Faneede83b2019-07-10 14:43:07 +08001911#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1912 {
1913 .mode = MMC_HS_400_ES,
1914 .widths = MMC_MODE_8BIT,
1915 },
1916#endif
Peng Fan46801252018-08-10 14:07:54 +08001917#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1918 {
1919 .mode = MMC_HS_400,
1920 .widths = MMC_MODE_8BIT,
1921 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
1922 },
1923#endif
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001924#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02001925 {
1926 .mode = MMC_HS_200,
1927 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +02001928 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02001929 },
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001930#endif
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02001931 {
1932 .mode = MMC_DDR_52,
1933 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1934 },
1935 {
1936 .mode = MMC_HS_52,
1937 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1938 },
1939 {
1940 .mode = MMC_HS,
1941 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1942 },
1943 {
1944 .mode = MMC_LEGACY,
1945 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1946 }
1947};
1948
1949#define for_each_mmc_mode_by_pref(caps, mwt) \
1950 for (mwt = mmc_modes_by_pref;\
1951 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
1952 mwt++) \
1953 if (caps & MMC_CAP(mwt->mode))
1954
1955static const struct ext_csd_bus_width {
1956 uint cap;
1957 bool is_ddr;
1958 uint ext_csd_bits;
1959} ext_csd_bus_width[] = {
1960 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
1961 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
1962 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
1963 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
1964 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
1965};
1966
Peng Fan46801252018-08-10 14:07:54 +08001967#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1968static int mmc_select_hs400(struct mmc *mmc)
1969{
1970 int err;
1971
1972 /* Set timing to HS200 for tuning */
Marek Vasut111572f2019-01-03 21:19:24 +01001973 err = mmc_set_card_speed(mmc, MMC_HS_200, false);
Peng Fan46801252018-08-10 14:07:54 +08001974 if (err)
1975 return err;
1976
1977 /* configure the bus mode (host) */
1978 mmc_select_mode(mmc, MMC_HS_200);
1979 mmc_set_clock(mmc, mmc->tran_speed, false);
1980
1981 /* execute tuning if needed */
Yangbo Lu3ed53ac2020-09-01 16:58:03 +08001982 mmc->hs400_tuning = 1;
Peng Fan46801252018-08-10 14:07:54 +08001983 err = mmc_execute_tuning(mmc, MMC_CMD_SEND_TUNING_BLOCK_HS200);
Yangbo Lu3ed53ac2020-09-01 16:58:03 +08001984 mmc->hs400_tuning = 0;
Peng Fan46801252018-08-10 14:07:54 +08001985 if (err) {
1986 debug("tuning failed\n");
1987 return err;
1988 }
1989
1990 /* Set back to HS */
BOUGH CHEN8702bbc2019-03-26 06:24:17 +00001991 mmc_set_card_speed(mmc, MMC_HS, true);
Peng Fan46801252018-08-10 14:07:54 +08001992
Yangbo Lu5347aea2020-09-01 16:58:04 +08001993 err = mmc_hs400_prepare_ddr(mmc);
1994 if (err)
1995 return err;
1996
Peng Fan46801252018-08-10 14:07:54 +08001997 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1998 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG);
1999 if (err)
2000 return err;
2001
Marek Vasut111572f2019-01-03 21:19:24 +01002002 err = mmc_set_card_speed(mmc, MMC_HS_400, false);
Peng Fan46801252018-08-10 14:07:54 +08002003 if (err)
2004 return err;
2005
2006 mmc_select_mode(mmc, MMC_HS_400);
2007 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2008 if (err)
2009 return err;
2010
2011 return 0;
2012}
2013#else
2014static int mmc_select_hs400(struct mmc *mmc)
2015{
2016 return -ENOTSUPP;
2017}
2018#endif
2019
Peng Faneede83b2019-07-10 14:43:07 +08002020#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
2021#if !CONFIG_IS_ENABLED(DM_MMC)
2022static int mmc_set_enhanced_strobe(struct mmc *mmc)
2023{
2024 return -ENOTSUPP;
2025}
2026#endif
2027static int mmc_select_hs400es(struct mmc *mmc)
2028{
2029 int err;
2030
2031 err = mmc_set_card_speed(mmc, MMC_HS, true);
2032 if (err)
2033 return err;
2034
2035 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2036 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG |
2037 EXT_CSD_BUS_WIDTH_STROBE);
2038 if (err) {
2039 printf("switch to bus width for hs400 failed\n");
2040 return err;
2041 }
2042 /* TODO: driver strength */
2043 err = mmc_set_card_speed(mmc, MMC_HS_400_ES, false);
2044 if (err)
2045 return err;
2046
2047 mmc_select_mode(mmc, MMC_HS_400_ES);
2048 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2049 if (err)
2050 return err;
2051
2052 return mmc_set_enhanced_strobe(mmc);
2053}
2054#else
2055static int mmc_select_hs400es(struct mmc *mmc)
2056{
2057 return -ENOTSUPP;
2058}
2059#endif
2060
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002061#define for_each_supported_width(caps, ddr, ecbv) \
2062 for (ecbv = ext_csd_bus_width;\
2063 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
2064 ecbv++) \
2065 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
2066
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02002067static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot933d1262017-09-21 16:29:52 +02002068{
Jaehoon Chung6b3431c2020-12-04 06:36:00 +09002069 int err = 0;
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002070 const struct mode_width_tuning *mwt;
2071 const struct ext_csd_bus_width *ecbw;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002072
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +01002073#ifdef DEBUG
2074 mmc_dump_capabilities("mmc", card_caps);
Jean-Jacques Hiblotd7e5e032017-11-30 17:43:57 +01002075 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +01002076#endif
2077
Anup Pateld9c92c72019-07-08 04:10:43 +00002078 if (mmc_host_is_spi(mmc)) {
2079 mmc_set_bus_width(mmc, 1);
2080 mmc_select_mode(mmc, MMC_LEGACY);
2081 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
2082 return 0;
2083 }
2084
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002085 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblotd7e5e032017-11-30 17:43:57 +01002086 card_caps &= mmc->host_caps;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002087
2088 /* Only version 4 of MMC supports wider bus widths */
2089 if (mmc->version < MMC_VERSION_4)
2090 return 0;
2091
Jean-Jacques Hibloted9506b2017-09-21 16:29:51 +02002092 if (!mmc->ext_csd) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002093 pr_debug("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hibloted9506b2017-09-21 16:29:51 +02002094 return -ENOTSUPP;
2095 }
2096
Marek Vasut111572f2019-01-03 21:19:24 +01002097#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
Ye Li3679e802021-08-17 17:20:34 +08002098 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
2099 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Marek Vasut111572f2019-01-03 21:19:24 +01002100 /*
2101 * In case the eMMC is in HS200/HS400 mode, downgrade to HS mode
2102 * before doing anything else, since a transition from either of
2103 * the HS200/HS400 mode directly to legacy mode is not supported.
2104 */
2105 if (mmc->selected_mode == MMC_HS_200 ||
Ye Li3679e802021-08-17 17:20:34 +08002106 mmc->selected_mode == MMC_HS_400 ||
2107 mmc->selected_mode == MMC_HS_400_ES)
Marek Vasut111572f2019-01-03 21:19:24 +01002108 mmc_set_card_speed(mmc, MMC_HS, true);
2109 else
2110#endif
2111 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02002112
2113 for_each_mmc_mode_by_pref(card_caps, mwt) {
2114 for_each_supported_width(card_caps & mwt->widths,
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002115 mmc_is_mode_ddr(mwt->mode), ecbw) {
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02002116 enum mmc_voltage old_voltage;
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002117 pr_debug("trying mode %s width %d (at %d MHz)\n",
2118 mmc_mode_name(mwt->mode),
2119 bus_width(ecbw->cap),
2120 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02002121 old_voltage = mmc->signal_voltage;
2122 err = mmc_set_lowest_voltage(mmc, mwt->mode,
2123 MMC_ALL_SIGNAL_VOLTAGE);
2124 if (err)
2125 continue;
2126
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002127 /* configure the bus width (card + host) */
2128 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2129 EXT_CSD_BUS_WIDTH,
2130 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
2131 if (err)
2132 goto error;
2133 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002134
Peng Fan46801252018-08-10 14:07:54 +08002135 if (mwt->mode == MMC_HS_400) {
2136 err = mmc_select_hs400(mmc);
2137 if (err) {
2138 printf("Select HS400 failed %d\n", err);
2139 goto error;
2140 }
Peng Faneede83b2019-07-10 14:43:07 +08002141 } else if (mwt->mode == MMC_HS_400_ES) {
2142 err = mmc_select_hs400es(mmc);
2143 if (err) {
2144 printf("Select HS400ES failed %d\n",
2145 err);
2146 goto error;
2147 }
Peng Fan46801252018-08-10 14:07:54 +08002148 } else {
2149 /* configure the bus speed (card) */
Marek Vasut111572f2019-01-03 21:19:24 +01002150 err = mmc_set_card_speed(mmc, mwt->mode, false);
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002151 if (err)
2152 goto error;
Peng Fan46801252018-08-10 14:07:54 +08002153
2154 /*
2155 * configure the bus width AND the ddr mode
2156 * (card). The host side will be taken care
2157 * of in the next step
2158 */
2159 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
2160 err = mmc_switch(mmc,
2161 EXT_CSD_CMD_SET_NORMAL,
2162 EXT_CSD_BUS_WIDTH,
2163 ecbw->ext_csd_bits);
2164 if (err)
2165 goto error;
2166 }
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002167
Peng Fan46801252018-08-10 14:07:54 +08002168 /* configure the bus mode (host) */
2169 mmc_select_mode(mmc, mwt->mode);
2170 mmc_set_clock(mmc, mmc->tran_speed,
2171 MMC_CLK_ENABLE);
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01002172#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002173
Peng Fan46801252018-08-10 14:07:54 +08002174 /* execute tuning if needed */
2175 if (mwt->tuning) {
2176 err = mmc_execute_tuning(mmc,
2177 mwt->tuning);
2178 if (err) {
Jaehoon Chungad9f7ce2020-11-17 07:04:59 +09002179 pr_debug("tuning failed : %d\n", err);
Peng Fan46801252018-08-10 14:07:54 +08002180 goto error;
2181 }
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +02002182 }
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01002183#endif
Peng Fan46801252018-08-10 14:07:54 +08002184 }
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +02002185
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002186 /* do a transfer to check the configuration */
2187 err = mmc_read_and_compare_ext_csd(mmc);
2188 if (!err)
2189 return 0;
2190error:
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02002191 mmc_set_signal_voltage(mmc, old_voltage);
Naoki Hayama3110dcb2020-10-12 18:35:22 +09002192 /* if an error occurred, revert to a safer bus mode */
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002193 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2194 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
2195 mmc_select_mode(mmc, MMC_LEGACY);
2196 mmc_set_bus_width(mmc, 1);
2197 }
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002198 }
2199
Jaehoon Chungad9f7ce2020-11-17 07:04:59 +09002200 pr_err("unable to select a mode : %d\n", err);
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002201
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002202 return -ENOTSUPP;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002203}
Marek Vasuta318a7a2018-04-15 00:37:11 +02002204#endif
2205
2206#if CONFIG_IS_ENABLED(MMC_TINY)
2207DEFINE_CACHE_ALIGN_BUFFER(u8, ext_csd_bkup, MMC_MAX_BLOCK_LEN);
2208#endif
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002209
Jean-Jacques Hibloted9506b2017-09-21 16:29:51 +02002210static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002211{
2212 int err, i;
2213 u64 capacity;
2214 bool has_parts = false;
2215 bool part_completed;
Jean-Jacques Hiblotfa6c5772018-01-04 15:23:31 +01002216 static const u32 mmc_versions[] = {
2217 MMC_VERSION_4,
2218 MMC_VERSION_4_1,
2219 MMC_VERSION_4_2,
2220 MMC_VERSION_4_3,
Jean-Jacques Hiblotc64862b2018-02-09 12:09:28 +01002221 MMC_VERSION_4_4,
Jean-Jacques Hiblotfa6c5772018-01-04 15:23:31 +01002222 MMC_VERSION_4_41,
2223 MMC_VERSION_4_5,
2224 MMC_VERSION_5_0,
2225 MMC_VERSION_5_1
2226 };
2227
Marek Vasuta318a7a2018-04-15 00:37:11 +02002228#if CONFIG_IS_ENABLED(MMC_TINY)
2229 u8 *ext_csd = ext_csd_bkup;
2230
2231 if (IS_SD(mmc) || mmc->version < MMC_VERSION_4)
2232 return 0;
2233
2234 if (!mmc->ext_csd)
2235 memset(ext_csd_bkup, 0, sizeof(ext_csd_bkup));
2236
2237 err = mmc_send_ext_csd(mmc, ext_csd);
2238 if (err)
2239 goto error;
2240
2241 /* store the ext csd for future reference */
2242 if (!mmc->ext_csd)
2243 mmc->ext_csd = ext_csd;
2244#else
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002245 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002246
2247 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
2248 return 0;
2249
2250 /* check ext_csd version and capacity */
2251 err = mmc_send_ext_csd(mmc, ext_csd);
2252 if (err)
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002253 goto error;
2254
2255 /* store the ext csd for future reference */
2256 if (!mmc->ext_csd)
2257 mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN);
2258 if (!mmc->ext_csd)
2259 return -ENOMEM;
2260 memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN);
Marek Vasuta318a7a2018-04-15 00:37:11 +02002261#endif
Alexander Kochetkovf1133c92018-02-20 14:35:55 +03002262 if (ext_csd[EXT_CSD_REV] >= ARRAY_SIZE(mmc_versions))
Jean-Jacques Hiblotfa6c5772018-01-04 15:23:31 +01002263 return -EINVAL;
2264
2265 mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]];
2266
2267 if (mmc->version >= MMC_VERSION_4_2) {
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002268 /*
2269 * According to the JEDEC Standard, the value of
2270 * ext_csd's capacity is valid if the value is more
2271 * than 2GB
2272 */
2273 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
2274 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
2275 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
2276 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2277 capacity *= MMC_MAX_BLOCK_LEN;
2278 if ((capacity >> 20) > 2 * 1024)
2279 mmc->capacity_user = capacity;
2280 }
2281
Jean-Jacques Hiblot201559c2019-07-02 10:53:54 +02002282 if (mmc->version >= MMC_VERSION_4_5)
2283 mmc->gen_cmd6_time = ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
2284
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002285 /* The partition data may be non-zero but it is only
2286 * effective if PARTITION_SETTING_COMPLETED is set in
2287 * EXT_CSD, so ignore any data if this bit is not set,
2288 * except for enabling the high-capacity group size
2289 * definition (see below).
2290 */
2291 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
2292 EXT_CSD_PARTITION_SETTING_COMPLETED);
2293
Jean-Jacques Hiblot7f5b1692019-07-02 10:53:55 +02002294 mmc->part_switch_time = ext_csd[EXT_CSD_PART_SWITCH_TIME];
2295 /* Some eMMC set the value too low so set a minimum */
2296 if (mmc->part_switch_time < MMC_MIN_PART_SWITCH_TIME && mmc->part_switch_time)
2297 mmc->part_switch_time = MMC_MIN_PART_SWITCH_TIME;
2298
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002299 /* store the partition info of emmc */
2300 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
2301 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
2302 ext_csd[EXT_CSD_BOOT_MULT])
2303 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
2304 if (part_completed &&
2305 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
2306 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
2307
2308 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
2309
2310 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
2311
2312 for (i = 0; i < 4; i++) {
2313 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
2314 uint mult = (ext_csd[idx + 2] << 16) +
2315 (ext_csd[idx + 1] << 8) + ext_csd[idx];
2316 if (mult)
2317 has_parts = true;
2318 if (!part_completed)
2319 continue;
2320 mmc->capacity_gp[i] = mult;
2321 mmc->capacity_gp[i] *=
2322 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2323 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2324 mmc->capacity_gp[i] <<= 19;
2325 }
2326
Jean-Jacques Hiblotc94c5472018-01-04 15:23:35 +01002327#ifndef CONFIG_SPL_BUILD
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002328 if (part_completed) {
2329 mmc->enh_user_size =
2330 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
2331 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
2332 ext_csd[EXT_CSD_ENH_SIZE_MULT];
2333 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2334 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2335 mmc->enh_user_size <<= 19;
2336 mmc->enh_user_start =
2337 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
2338 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
2339 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
2340 ext_csd[EXT_CSD_ENH_START_ADDR];
2341 if (mmc->high_capacity)
2342 mmc->enh_user_start <<= 9;
2343 }
Jean-Jacques Hiblotc94c5472018-01-04 15:23:35 +01002344#endif
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002345
2346 /*
2347 * Host needs to enable ERASE_GRP_DEF bit if device is
2348 * partitioned. This bit will be lost every time after a reset
2349 * or power off. This will affect erase size.
2350 */
2351 if (part_completed)
2352 has_parts = true;
2353 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
2354 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
2355 has_parts = true;
2356 if (has_parts) {
2357 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2358 EXT_CSD_ERASE_GROUP_DEF, 1);
2359
2360 if (err)
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002361 goto error;
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002362
2363 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
2364 }
2365
2366 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002367#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002368 /* Read out group size from ext_csd */
2369 mmc->erase_grp_size =
2370 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002371#endif
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002372 /*
2373 * if high capacity and partition setting completed
2374 * SEC_COUNT is valid even if it is smaller than 2 GiB
2375 * JEDEC Standard JESD84-B45, 6.2.4
2376 */
2377 if (mmc->high_capacity && part_completed) {
2378 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
2379 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
2380 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
2381 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
2382 capacity *= MMC_MAX_BLOCK_LEN;
2383 mmc->capacity_user = capacity;
2384 }
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002385 }
2386#if CONFIG_IS_ENABLED(MMC_WRITE)
2387 else {
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002388 /* Calculate the group size from the csd value. */
2389 int erase_gsz, erase_gmul;
2390
2391 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
2392 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
2393 mmc->erase_grp_size = (erase_gsz + 1)
2394 * (erase_gmul + 1);
2395 }
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002396#endif
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +01002397#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002398 mmc->hc_wp_grp_size = 1024
2399 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
2400 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +01002401#endif
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002402
2403 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
2404
2405 return 0;
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002406error:
2407 if (mmc->ext_csd) {
Marek Vasuta318a7a2018-04-15 00:37:11 +02002408#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002409 free(mmc->ext_csd);
Marek Vasuta318a7a2018-04-15 00:37:11 +02002410#endif
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002411 mmc->ext_csd = NULL;
2412 }
2413 return err;
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002414}
2415
Kim Phillips87ea3892012-10-29 13:34:43 +00002416static int mmc_startup(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05002417{
Stephen Warrene315ae82013-06-11 15:14:01 -06002418 int err, i;
Andy Flemingad347bb2008-10-30 16:41:01 -05002419 uint mult, freq;
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002420 u64 cmult, csize;
Andy Flemingad347bb2008-10-30 16:41:01 -05002421 struct mmc_cmd cmd;
Simon Glasse5db1152016-05-01 13:52:35 -06002422 struct blk_desc *bdesc;
Andy Flemingad347bb2008-10-30 16:41:01 -05002423
Thomas Chou1254c3d2010-12-24 13:12:21 +00002424#ifdef CONFIG_MMC_SPI_CRC_ON
2425 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
2426 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
2427 cmd.resp_type = MMC_RSP_R1;
2428 cmd.cmdarg = 1;
Thomas Chou1254c3d2010-12-24 13:12:21 +00002429 err = mmc_send_cmd(mmc, &cmd, NULL);
Thomas Chou1254c3d2010-12-24 13:12:21 +00002430 if (err)
2431 return err;
2432 }
2433#endif
2434
Andy Flemingad347bb2008-10-30 16:41:01 -05002435 /* Put the Card in Identify Mode */
Thomas Chou1254c3d2010-12-24 13:12:21 +00002436 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
2437 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Flemingad347bb2008-10-30 16:41:01 -05002438 cmd.resp_type = MMC_RSP_R2;
2439 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -05002440
Sean Anderson86325092020-10-17 08:36:27 -04002441 err = mmc_send_cmd_quirks(mmc, &cmd, NULL, MMC_QUIRK_RETRY_SEND_CID, 4);
Andy Flemingad347bb2008-10-30 16:41:01 -05002442 if (err)
2443 return err;
2444
2445 memcpy(mmc->cid, cmd.response, 16);
2446
2447 /*
2448 * For MMC cards, set the Relative Address.
2449 * For SD cards, get the Relatvie Address.
2450 * This also puts the cards into Standby State
2451 */
Thomas Chou1254c3d2010-12-24 13:12:21 +00002452 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2453 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
2454 cmd.cmdarg = mmc->rca << 16;
2455 cmd.resp_type = MMC_RSP_R6;
Andy Flemingad347bb2008-10-30 16:41:01 -05002456
Thomas Chou1254c3d2010-12-24 13:12:21 +00002457 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -05002458
Thomas Chou1254c3d2010-12-24 13:12:21 +00002459 if (err)
2460 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05002461
Thomas Chou1254c3d2010-12-24 13:12:21 +00002462 if (IS_SD(mmc))
2463 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
2464 }
Andy Flemingad347bb2008-10-30 16:41:01 -05002465
2466 /* Get the Card-Specific Data */
2467 cmd.cmdidx = MMC_CMD_SEND_CSD;
2468 cmd.resp_type = MMC_RSP_R2;
2469 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -05002470
2471 err = mmc_send_cmd(mmc, &cmd, NULL);
2472
2473 if (err)
2474 return err;
2475
Rabin Vincentb6eed942009-04-05 13:30:56 +05302476 mmc->csd[0] = cmd.response[0];
2477 mmc->csd[1] = cmd.response[1];
2478 mmc->csd[2] = cmd.response[2];
2479 mmc->csd[3] = cmd.response[3];
Andy Flemingad347bb2008-10-30 16:41:01 -05002480
2481 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincentbdf7a682009-04-05 13:30:55 +05302482 int version = (cmd.response[0] >> 26) & 0xf;
Andy Flemingad347bb2008-10-30 16:41:01 -05002483
2484 switch (version) {
Bin Meng4a4ef872016-03-17 21:53:13 -07002485 case 0:
2486 mmc->version = MMC_VERSION_1_2;
2487 break;
2488 case 1:
2489 mmc->version = MMC_VERSION_1_4;
2490 break;
2491 case 2:
2492 mmc->version = MMC_VERSION_2_2;
2493 break;
2494 case 3:
2495 mmc->version = MMC_VERSION_3;
2496 break;
2497 case 4:
2498 mmc->version = MMC_VERSION_4;
2499 break;
2500 default:
2501 mmc->version = MMC_VERSION_1_2;
2502 break;
Andy Flemingad347bb2008-10-30 16:41:01 -05002503 }
2504 }
2505
2506 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincentbdf7a682009-04-05 13:30:55 +05302507 freq = fbase[(cmd.response[0] & 0x7)];
2508 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Flemingad347bb2008-10-30 16:41:01 -05002509
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +02002510 mmc->legacy_speed = freq * mult;
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +02002511 mmc_select_mode(mmc, MMC_LEGACY);
Andy Flemingad347bb2008-10-30 16:41:01 -05002512
Markus Niebel03951412013-12-16 13:40:46 +01002513 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincentb6eed942009-04-05 13:30:56 +05302514 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002515#if CONFIG_IS_ENABLED(MMC_WRITE)
Andy Flemingad347bb2008-10-30 16:41:01 -05002516
2517 if (IS_SD(mmc))
2518 mmc->write_bl_len = mmc->read_bl_len;
2519 else
Rabin Vincentb6eed942009-04-05 13:30:56 +05302520 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002521#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05002522
2523 if (mmc->high_capacity) {
2524 csize = (mmc->csd[1] & 0x3f) << 16
2525 | (mmc->csd[2] & 0xffff0000) >> 16;
2526 cmult = 8;
2527 } else {
2528 csize = (mmc->csd[1] & 0x3ff) << 2
2529 | (mmc->csd[2] & 0xc0000000) >> 30;
2530 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2531 }
2532
Stephen Warrene315ae82013-06-11 15:14:01 -06002533 mmc->capacity_user = (csize + 1) << (cmult + 2);
2534 mmc->capacity_user *= mmc->read_bl_len;
2535 mmc->capacity_boot = 0;
2536 mmc->capacity_rpmb = 0;
2537 for (i = 0; i < 4; i++)
2538 mmc->capacity_gp[i] = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -05002539
Simon Glassa09c2b72013-04-03 08:54:30 +00002540 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2541 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Flemingad347bb2008-10-30 16:41:01 -05002542
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002543#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glassa09c2b72013-04-03 08:54:30 +00002544 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2545 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002546#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05002547
Markus Niebel03951412013-12-16 13:40:46 +01002548 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2549 cmd.cmdidx = MMC_CMD_SET_DSR;
2550 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2551 cmd.resp_type = MMC_RSP_NONE;
2552 if (mmc_send_cmd(mmc, &cmd, NULL))
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01002553 pr_warn("MMC: SET_DSR failed\n");
Markus Niebel03951412013-12-16 13:40:46 +01002554 }
2555
Andy Flemingad347bb2008-10-30 16:41:01 -05002556 /* Select the card, and put it into Transfer Mode */
Thomas Chou1254c3d2010-12-24 13:12:21 +00002557 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2558 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargav4a32fba2011-10-05 03:13:23 +00002559 cmd.resp_type = MMC_RSP_R1;
Thomas Chou1254c3d2010-12-24 13:12:21 +00002560 cmd.cmdarg = mmc->rca << 16;
Thomas Chou1254c3d2010-12-24 13:12:21 +00002561 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -05002562
Thomas Chou1254c3d2010-12-24 13:12:21 +00002563 if (err)
2564 return err;
2565 }
Andy Flemingad347bb2008-10-30 16:41:01 -05002566
Lei Wenea526762011-06-22 17:03:31 +00002567 /*
2568 * For SD, its erase group is always one sector
2569 */
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002570#if CONFIG_IS_ENABLED(MMC_WRITE)
Lei Wenea526762011-06-22 17:03:31 +00002571 mmc->erase_grp_size = 1;
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002572#endif
Lei Wen31b99802011-05-02 16:26:26 +00002573 mmc->part_config = MMCPART_NOAVAILABLE;
Diego Santa Cruza7a75992014-12-23 10:50:27 +01002574
Jean-Jacques Hibloted9506b2017-09-21 16:29:51 +02002575 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002576 if (err)
2577 return err;
Sukumar Ghorai232293c2010-09-20 18:29:29 +05302578
Simon Glasse5db1152016-05-01 13:52:35 -06002579 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrene315ae82013-06-11 15:14:01 -06002580 if (err)
2581 return err;
2582
Marek Vasuta318a7a2018-04-15 00:37:11 +02002583#if CONFIG_IS_ENABLED(MMC_TINY)
2584 mmc_set_clock(mmc, mmc->legacy_speed, false);
Faiz Abbas01db77e2020-02-26 13:44:32 +05302585 mmc_select_mode(mmc, MMC_LEGACY);
Marek Vasuta318a7a2018-04-15 00:37:11 +02002586 mmc_set_bus_width(mmc, 1);
2587#else
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02002588 if (IS_SD(mmc)) {
2589 err = sd_get_capabilities(mmc);
2590 if (err)
2591 return err;
2592 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2593 } else {
2594 err = mmc_get_capabilities(mmc);
2595 if (err)
2596 return err;
Masahiro Yamadabf1f25c2020-01-23 14:31:12 +09002597 err = mmc_select_mode_and_width(mmc, mmc->card_caps);
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02002598 }
Marek Vasuta318a7a2018-04-15 00:37:11 +02002599#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05002600 if (err)
2601 return err;
2602
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02002603 mmc->best_mode = mmc->selected_mode;
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00002604
Andrew Gabbasov532663b2014-12-01 06:59:11 -06002605 /* Fix the block length for DDR mode */
2606 if (mmc->ddr_mode) {
2607 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002608#if CONFIG_IS_ENABLED(MMC_WRITE)
Andrew Gabbasov532663b2014-12-01 06:59:11 -06002609 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002610#endif
Andrew Gabbasov532663b2014-12-01 06:59:11 -06002611 }
2612
Andy Flemingad347bb2008-10-30 16:41:01 -05002613 /* fill in device description */
Simon Glasse5db1152016-05-01 13:52:35 -06002614 bdesc = mmc_get_blk_desc(mmc);
2615 bdesc->lun = 0;
2616 bdesc->hwpart = 0;
2617 bdesc->type = 0;
2618 bdesc->blksz = mmc->read_bl_len;
2619 bdesc->log2blksz = LOG2(bdesc->blksz);
2620 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsd67754f2015-12-04 23:27:40 +01002621#if !defined(CONFIG_SPL_BUILD) || \
2622 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
Simon Glass7611ac62019-09-25 08:56:27 -06002623 !CONFIG_IS_ENABLED(USE_TINY_PRINTF))
Simon Glasse5db1152016-05-01 13:52:35 -06002624 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Hutt7367ec22012-10-20 17:15:59 +00002625 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2626 (mmc->cid[3] >> 16) & 0xffff);
Simon Glasse5db1152016-05-01 13:52:35 -06002627 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Hutt7367ec22012-10-20 17:15:59 +00002628 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2629 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2630 (mmc->cid[2] >> 24) & 0xff);
Simon Glasse5db1152016-05-01 13:52:35 -06002631 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Hutt7367ec22012-10-20 17:15:59 +00002632 (mmc->cid[2] >> 16) & 0xf);
Paul Burton6a7c5ba2013-09-04 16:12:25 +01002633#else
Simon Glasse5db1152016-05-01 13:52:35 -06002634 bdesc->vendor[0] = 0;
2635 bdesc->product[0] = 0;
2636 bdesc->revision[0] = 0;
Paul Burton6a7c5ba2013-09-04 16:12:25 +01002637#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05002638
Andre Przywara17798042018-12-17 10:05:45 +00002639#if !defined(CONFIG_DM_MMC) && (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT))
2640 part_init(bdesc);
2641#endif
2642
Andy Flemingad347bb2008-10-30 16:41:01 -05002643 return 0;
2644}
2645
Kim Phillips87ea3892012-10-29 13:34:43 +00002646static int mmc_send_if_cond(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05002647{
2648 struct mmc_cmd cmd;
2649 int err;
2650
2651 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2652 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou2c850462014-03-11 19:34:20 +02002653 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Flemingad347bb2008-10-30 16:41:01 -05002654 cmd.resp_type = MMC_RSP_R7;
Andy Flemingad347bb2008-10-30 16:41:01 -05002655
2656 err = mmc_send_cmd(mmc, &cmd, NULL);
2657
2658 if (err)
2659 return err;
2660
Rabin Vincentb6eed942009-04-05 13:30:56 +05302661 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung7825d202016-07-19 16:33:36 +09002662 return -EOPNOTSUPP;
Andy Flemingad347bb2008-10-30 16:41:01 -05002663 else
2664 mmc->version = SD_VERSION_2;
2665
2666 return 0;
2667}
2668
Simon Glass5f4bd8c2017-07-04 13:31:19 -06002669#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski2439fe92014-11-08 20:55:45 +01002670/* board-specific MMC power initializations. */
2671__weak void board_mmc_power_init(void)
2672{
2673}
Simon Glass833b80d2017-04-22 19:10:56 -06002674#endif
Paul Kocialkowski2439fe92014-11-08 20:55:45 +01002675
Peng Fan15305962016-10-11 15:08:43 +08002676static int mmc_power_init(struct mmc *mmc)
2677{
Simon Glass5f4bd8c2017-07-04 13:31:19 -06002678#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblota49ffa12017-09-21 16:29:48 +02002679#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan15305962016-10-11 15:08:43 +08002680 int ret;
2681
2682 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblota49ffa12017-09-21 16:29:48 +02002683 &mmc->vmmc_supply);
2684 if (ret)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002685 pr_debug("%s: No vmmc supply\n", mmc->dev->name);
Jean-Jacques Hiblota49ffa12017-09-21 16:29:48 +02002686
2687 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2688 &mmc->vqmmc_supply);
2689 if (ret)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002690 pr_debug("%s: No vqmmc supply\n", mmc->dev->name);
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002691#endif
2692#else /* !CONFIG_DM_MMC */
2693 /*
2694 * Driver model should use a regulator, as above, rather than calling
2695 * out to board code.
2696 */
2697 board_mmc_power_init();
2698#endif
2699 return 0;
2700}
2701
2702/*
2703 * put the host in the initial state:
2704 * - turn on Vdd (card power supply)
2705 * - configure the bus width and clock to minimal values
2706 */
2707static void mmc_set_initial_state(struct mmc *mmc)
2708{
2709 int err;
2710
2711 /* First try to set 3.3V. If it fails set to 1.8V */
2712 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2713 if (err != 0)
2714 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2715 if (err != 0)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01002716 pr_warn("mmc: failed to set signal voltage\n");
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002717
2718 mmc_select_mode(mmc, MMC_LEGACY);
2719 mmc_set_bus_width(mmc, 1);
Jaehoon Chung239cb2f2018-01-26 19:25:29 +09002720 mmc_set_clock(mmc, 0, MMC_CLK_ENABLE);
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002721}
Peng Fan15305962016-10-11 15:08:43 +08002722
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002723static int mmc_power_on(struct mmc *mmc)
2724{
2725#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
Jean-Jacques Hiblota49ffa12017-09-21 16:29:48 +02002726 if (mmc->vmmc_supply) {
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002727 int ret = regulator_set_enable(mmc->vmmc_supply, true);
2728
Jaehoon Chungc71c95c2020-11-06 20:30:41 +09002729 if (ret && ret != -EACCES) {
Jaehoon Chungad9f7ce2020-11-17 07:04:59 +09002730 printf("Error enabling VMMC supply : %d\n", ret);
Jean-Jacques Hiblota49ffa12017-09-21 16:29:48 +02002731 return ret;
2732 }
Peng Fan15305962016-10-11 15:08:43 +08002733 }
2734#endif
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002735 return 0;
2736}
2737
2738static int mmc_power_off(struct mmc *mmc)
2739{
Jaehoon Chung239cb2f2018-01-26 19:25:29 +09002740 mmc_set_clock(mmc, 0, MMC_CLK_DISABLE);
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002741#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2742 if (mmc->vmmc_supply) {
2743 int ret = regulator_set_enable(mmc->vmmc_supply, false);
2744
Jaehoon Chungc71c95c2020-11-06 20:30:41 +09002745 if (ret && ret != -EACCES) {
Jaehoon Chungad9f7ce2020-11-17 07:04:59 +09002746 pr_debug("Error disabling VMMC supply : %d\n", ret);
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002747 return ret;
2748 }
2749 }
Simon Glass833b80d2017-04-22 19:10:56 -06002750#endif
Peng Fan15305962016-10-11 15:08:43 +08002751 return 0;
2752}
2753
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002754static int mmc_power_cycle(struct mmc *mmc)
2755{
2756 int ret;
2757
2758 ret = mmc_power_off(mmc);
2759 if (ret)
2760 return ret;
Yann Gautier6f558332019-09-19 17:56:12 +02002761
2762 ret = mmc_host_power_cycle(mmc);
2763 if (ret)
2764 return ret;
2765
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002766 /*
2767 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2768 * to be on the safer side.
2769 */
2770 udelay(2000);
2771 return mmc_power_on(mmc);
2772}
2773
Pali Rohár7c639622021-07-14 16:37:29 +02002774int mmc_get_op_cond(struct mmc *mmc, bool quiet)
Andy Flemingad347bb2008-10-30 16:41:01 -05002775{
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02002776 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
Macpaul Lin028bde12011-11-14 23:35:39 +00002777 int err;
Andy Flemingad347bb2008-10-30 16:41:01 -05002778
Lei Wen31b99802011-05-02 16:26:26 +00002779 if (mmc->has_init)
2780 return 0;
2781
Peng Fan15305962016-10-11 15:08:43 +08002782 err = mmc_power_init(mmc);
2783 if (err)
2784 return err;
Paul Kocialkowski2439fe92014-11-08 20:55:45 +01002785
Kishon Vijay Abraham I07baaa62017-09-21 16:30:10 +02002786#ifdef CONFIG_MMC_QUIRKS
2787 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
Joel Johnson5ea041b2020-01-11 09:08:14 -07002788 MMC_QUIRK_RETRY_SEND_CID |
2789 MMC_QUIRK_RETRY_APP_CMD;
Kishon Vijay Abraham I07baaa62017-09-21 16:30:10 +02002790#endif
2791
Jean-Jacques Hiblotdc030fb2017-09-21 16:30:08 +02002792 err = mmc_power_cycle(mmc);
2793 if (err) {
2794 /*
2795 * if power cycling is not supported, we should not try
2796 * to use the UHS modes, because we wouldn't be able to
2797 * recover from an error during the UHS initialization.
2798 */
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002799 pr_debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
Jean-Jacques Hiblotdc030fb2017-09-21 16:30:08 +02002800 uhs_en = false;
2801 mmc->host_caps &= ~UHS_CAPS;
2802 err = mmc_power_on(mmc);
2803 }
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002804 if (err)
2805 return err;
2806
Simon Glasseba48f92017-07-29 11:35:31 -06002807#if CONFIG_IS_ENABLED(DM_MMC)
Yangbo Luc46f5d72020-09-01 16:57:59 +08002808 /*
2809 * Re-initialization is needed to clear old configuration for
2810 * mmc rescan.
2811 */
2812 err = mmc_reinit(mmc);
Simon Glass394dfc02016-06-12 23:30:22 -06002813#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02002814 /* made sure it's not NULL earlier */
Pantelis Antoniou2c850462014-03-11 19:34:20 +02002815 err = mmc->cfg->ops->init(mmc);
Yangbo Luc46f5d72020-09-01 16:57:59 +08002816#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05002817 if (err)
2818 return err;
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06002819 mmc->ddr_mode = 0;
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +02002820
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02002821retry:
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002822 mmc_set_initial_state(mmc);
Jean-Jacques Hiblot5f23d872017-09-21 16:30:01 +02002823
Andy Flemingad347bb2008-10-30 16:41:01 -05002824 /* Reset the Card */
2825 err = mmc_go_idle(mmc);
2826
2827 if (err)
2828 return err;
2829
Marcel Ziswilerb2b7fc82019-05-20 02:44:53 +02002830 /* The internal partition reset to user partition(0) at every CMD0 */
Simon Glasse5db1152016-05-01 13:52:35 -06002831 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wen31b99802011-05-02 16:26:26 +00002832
Andy Flemingad347bb2008-10-30 16:41:01 -05002833 /* Test for SD version 2 */
Macpaul Lin028bde12011-11-14 23:35:39 +00002834 err = mmc_send_if_cond(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05002835
Andy Flemingad347bb2008-10-30 16:41:01 -05002836 /* Now try to get the SD card's operating condition */
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02002837 err = sd_send_op_cond(mmc, uhs_en);
2838 if (err && uhs_en) {
2839 uhs_en = false;
2840 mmc_power_cycle(mmc);
2841 goto retry;
2842 }
Andy Flemingad347bb2008-10-30 16:41:01 -05002843
2844 /* If the command timed out, we check for an MMC card */
Jaehoon Chung7825d202016-07-19 16:33:36 +09002845 if (err == -ETIMEDOUT) {
Andy Flemingad347bb2008-10-30 16:41:01 -05002846 err = mmc_send_op_cond(mmc);
2847
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05002848 if (err) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +01002849#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Pali Rohár7c639622021-07-14 16:37:29 +02002850 if (!quiet)
2851 pr_err("Card did not respond to voltage select! : %d\n", err);
Paul Burton6a7c5ba2013-09-04 16:12:25 +01002852#endif
Jaehoon Chung7825d202016-07-19 16:33:36 +09002853 return -EOPNOTSUPP;
Andy Flemingad347bb2008-10-30 16:41:01 -05002854 }
2855 }
2856
Jon Nettleton2663fe42018-06-11 15:26:19 +03002857 return err;
2858}
2859
2860int mmc_start_init(struct mmc *mmc)
2861{
2862 bool no_card;
2863 int err = 0;
2864
2865 /*
2866 * all hosts are capable of 1 bit bus-width and able to use the legacy
2867 * timings.
2868 */
Faiz Abbas01db77e2020-02-26 13:44:32 +05302869 mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(MMC_LEGACY) |
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05302870 MMC_MODE_1BIT;
2871
2872 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET)) {
2873 if (mmc->user_speed_mode != MMC_MODES_END) {
2874 int i;
2875 /* set host caps */
2876 if (mmc->host_caps & MMC_CAP(mmc->user_speed_mode)) {
2877 /* Remove all existing speed capabilities */
2878 for (i = MMC_LEGACY; i < MMC_MODES_END; i++)
2879 mmc->host_caps &= ~MMC_CAP(i);
2880 mmc->host_caps |= (MMC_CAP(mmc->user_speed_mode)
2881 | MMC_CAP(MMC_LEGACY) |
2882 MMC_MODE_1BIT);
2883 } else {
2884 pr_err("bus_mode requested is not supported\n");
2885 return -EINVAL;
2886 }
2887 }
2888 }
Faiz Abbasf6fd4ec2020-02-26 13:44:30 +05302889#if CONFIG_IS_ENABLED(DM_MMC)
2890 mmc_deferred_probe(mmc);
2891#endif
Jon Nettleton2663fe42018-06-11 15:26:19 +03002892#if !defined(CONFIG_MMC_BROKEN_CD)
Jon Nettleton2663fe42018-06-11 15:26:19 +03002893 no_card = mmc_getcd(mmc) == 0;
2894#else
2895 no_card = 0;
2896#endif
2897#if !CONFIG_IS_ENABLED(DM_MMC)
Baruch Siach0448ce62019-07-22 15:52:12 +03002898 /* we pretend there's no card when init is NULL */
Jon Nettleton2663fe42018-06-11 15:26:19 +03002899 no_card = no_card || (mmc->cfg->ops->init == NULL);
2900#endif
2901 if (no_card) {
2902 mmc->has_init = 0;
2903#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2904 pr_err("MMC: no card present\n");
2905#endif
2906 return -ENOMEDIUM;
2907 }
2908
Pali Rohár7c639622021-07-14 16:37:29 +02002909 err = mmc_get_op_cond(mmc, false);
Jon Nettleton2663fe42018-06-11 15:26:19 +03002910
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05002911 if (!err)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002912 mmc->init_in_progress = 1;
2913
2914 return err;
2915}
2916
2917static int mmc_complete_init(struct mmc *mmc)
2918{
2919 int err = 0;
2920
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05002921 mmc->init_in_progress = 0;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002922 if (mmc->op_cond_pending)
2923 err = mmc_complete_op_cond(mmc);
2924
2925 if (!err)
2926 err = mmc_startup(mmc);
Lei Wen31b99802011-05-02 16:26:26 +00002927 if (err)
2928 mmc->has_init = 0;
2929 else
2930 mmc->has_init = 1;
2931 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05002932}
2933
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002934int mmc_init(struct mmc *mmc)
2935{
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05002936 int err = 0;
Vipul Kumardbad7b42018-05-03 12:20:54 +05302937 __maybe_unused ulong start;
Simon Glass5f4bd8c2017-07-04 13:31:19 -06002938#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass59bc6f22016-05-01 13:52:41 -06002939 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002940
Simon Glass59bc6f22016-05-01 13:52:41 -06002941 upriv->mmc = mmc;
2942#endif
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002943 if (mmc->has_init)
2944 return 0;
Mateusz Zalegada351782014-04-29 20:15:30 +02002945
2946 start = get_timer(0);
2947
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002948 if (!mmc->init_in_progress)
2949 err = mmc_start_init(mmc);
2950
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05002951 if (!err)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002952 err = mmc_complete_init(mmc);
Jagan Teki9bee2b52017-01-10 11:18:43 +01002953 if (err)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002954 pr_info("%s: %d, time %lu\n", __func__, err, get_timer(start));
Jagan Teki9bee2b52017-01-10 11:18:43 +01002955
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002956 return err;
2957}
2958
Marek Vasuta4773fc2019-01-29 04:45:51 +01002959#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
2960 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
2961 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2962int mmc_deinit(struct mmc *mmc)
2963{
2964 u32 caps_filtered;
2965
2966 if (!mmc->has_init)
2967 return 0;
2968
2969 if (IS_SD(mmc)) {
2970 caps_filtered = mmc->card_caps &
2971 ~(MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25) |
2972 MMC_CAP(UHS_SDR50) | MMC_CAP(UHS_DDR50) |
2973 MMC_CAP(UHS_SDR104));
2974
2975 return sd_select_mode_and_width(mmc, caps_filtered);
2976 } else {
2977 caps_filtered = mmc->card_caps &
Ye Li3679e802021-08-17 17:20:34 +08002978 ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_400_ES));
Marek Vasuta4773fc2019-01-29 04:45:51 +01002979
2980 return mmc_select_mode_and_width(mmc, caps_filtered);
2981 }
2982}
2983#endif
2984
Markus Niebel03951412013-12-16 13:40:46 +01002985int mmc_set_dsr(struct mmc *mmc, u16 val)
2986{
2987 mmc->dsr = val;
2988 return 0;
2989}
2990
Jeroen Hofstee47726302014-07-10 22:46:28 +02002991/* CPU-specific MMC initializations */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09002992__weak int cpu_mmc_init(struct bd_info *bis)
Andy Flemingad347bb2008-10-30 16:41:01 -05002993{
2994 return -1;
2995}
2996
Jeroen Hofstee47726302014-07-10 22:46:28 +02002997/* board-specific MMC initializations. */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09002998__weak int board_mmc_init(struct bd_info *bis)
Jeroen Hofstee47726302014-07-10 22:46:28 +02002999{
3000 return -1;
3001}
Andy Flemingad347bb2008-10-30 16:41:01 -05003002
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00003003void mmc_set_preinit(struct mmc *mmc, int preinit)
3004{
3005 mmc->preinit = preinit;
3006}
3007
Faiz Abbasb3857fd2018-02-12 19:35:24 +05303008#if CONFIG_IS_ENABLED(DM_MMC)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09003009static int mmc_probe(struct bd_info *bis)
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003010{
Simon Glass547cb342015-12-29 05:22:49 -07003011 int ret, i;
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003012 struct uclass *uc;
Simon Glass547cb342015-12-29 05:22:49 -07003013 struct udevice *dev;
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003014
3015 ret = uclass_get(UCLASS_MMC, &uc);
3016 if (ret)
3017 return ret;
3018
Simon Glass547cb342015-12-29 05:22:49 -07003019 /*
3020 * Try to add them in sequence order. Really with driver model we
3021 * should allow holes, but the current MMC list does not allow that.
3022 * So if we request 0, 1, 3 we will get 0, 1, 2.
3023 */
3024 for (i = 0; ; i++) {
3025 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
3026 if (ret == -ENODEV)
3027 break;
3028 }
3029 uclass_foreach_dev(dev, uc) {
3030 ret = device_probe(dev);
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003031 if (ret)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01003032 pr_err("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003033 }
3034
3035 return 0;
3036}
3037#else
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09003038static int mmc_probe(struct bd_info *bis)
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003039{
3040 if (board_mmc_init(bis) < 0)
3041 cpu_mmc_init(bis);
3042
3043 return 0;
3044}
3045#endif
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00003046
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09003047int mmc_initialize(struct bd_info *bis)
Andy Flemingad347bb2008-10-30 16:41:01 -05003048{
Daniel Kochmański13df57b2015-05-29 16:55:43 +02003049 static int initialized = 0;
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003050 int ret;
Daniel Kochmański13df57b2015-05-29 16:55:43 +02003051 if (initialized) /* Avoid initializing mmc multiple times */
3052 return 0;
3053 initialized = 1;
3054
Simon Glass5f4bd8c2017-07-04 13:31:19 -06003055#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutf537e392016-12-01 02:06:33 +01003056#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glasse5db1152016-05-01 13:52:35 -06003057 mmc_list_init();
3058#endif
Marek Vasutf537e392016-12-01 02:06:33 +01003059#endif
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003060 ret = mmc_probe(bis);
3061 if (ret)
3062 return ret;
Andy Flemingad347bb2008-10-30 16:41:01 -05003063
Ying Zhang9ff70262013-08-16 15:16:11 +08003064#ifndef CONFIG_SPL_BUILD
Andy Flemingad347bb2008-10-30 16:41:01 -05003065 print_mmc_devices(',');
Ying Zhang9ff70262013-08-16 15:16:11 +08003066#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05003067
Simon Glasse5db1152016-05-01 13:52:35 -06003068 mmc_do_preinit();
Andy Flemingad347bb2008-10-30 16:41:01 -05003069 return 0;
3070}
Tomas Melinc17dae52016-11-25 11:01:03 +02003071
Lokesh Vutlac59b41c2019-09-09 14:40:36 +05303072#if CONFIG_IS_ENABLED(DM_MMC)
3073int mmc_init_device(int num)
3074{
3075 struct udevice *dev;
3076 struct mmc *m;
3077 int ret;
3078
Aswath Govindraju57e2ccb2021-03-25 12:48:47 +05303079 if (uclass_get_device_by_seq(UCLASS_MMC, num, &dev)) {
3080 ret = uclass_get_device(UCLASS_MMC, num, &dev);
3081 if (ret)
3082 return ret;
3083 }
Lokesh Vutlac59b41c2019-09-09 14:40:36 +05303084
3085 m = mmc_get_mmc_dev(dev);
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05303086 m->user_speed_mode = MMC_MODES_END; /* Initialising user set speed mode */
3087
Lokesh Vutlac59b41c2019-09-09 14:40:36 +05303088 if (!m)
3089 return 0;
Lokesh Vutlac59b41c2019-09-09 14:40:36 +05303090 if (m->preinit)
3091 mmc_start_init(m);
3092
3093 return 0;
3094}
3095#endif
3096
Tomas Melinc17dae52016-11-25 11:01:03 +02003097#ifdef CONFIG_CMD_BKOPS_ENABLE
3098int mmc_set_bkops_enable(struct mmc *mmc)
3099{
3100 int err;
3101 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
3102
3103 err = mmc_send_ext_csd(mmc, ext_csd);
3104 if (err) {
3105 puts("Could not get ext_csd register values\n");
3106 return err;
3107 }
3108
3109 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
3110 puts("Background operations not supported on device\n");
3111 return -EMEDIUMTYPE;
3112 }
3113
3114 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
3115 puts("Background operations already enabled\n");
3116 return 0;
3117 }
3118
3119 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
3120 if (err) {
3121 puts("Failed to enable manual background operations\n");
3122 return err;
3123 }
3124
3125 puts("Enabled manual background operations\n");
3126
3127 return 0;
3128}
3129#endif
David Woodhouse49fee032020-08-04 10:05:46 +01003130
3131__weak int mmc_get_env_dev(void)
3132{
3133#ifdef CONFIG_SYS_MMC_ENV_DEV
3134 return CONFIG_SYS_MMC_ENV_DEV;
3135#else
3136 return 0;
3137#endif
3138}