blob: ca62bb9891e35190604b10be25c76f9594a48767 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleming272cc702008-10-30 16:41:01 -05002/*
3 * Copyright 2008, Freescale Semiconductor, Inc
4 * Andy Fleming
5 *
6 * Based vaguely on the Linux code
Andy Fleming272cc702008-10-30 16:41:01 -05007 */
8
9#include <config.h>
10#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <blk.h>
Andy Fleming272cc702008-10-30 16:41:01 -050012#include <command.h>
Sjoerd Simons8e3332e2015-08-30 16:55:45 -060013#include <dm.h>
14#include <dm/device-internal.h>
Stephen Warrend4622df2014-05-23 12:47:06 -060015#include <errno.h>
Andy Fleming272cc702008-10-30 16:41:01 -050016#include <mmc.h>
17#include <part.h>
Peng Fan2051aef2016-10-11 15:08:43 +080018#include <power/regulator.h>
Andy Fleming272cc702008-10-30 16:41:01 -050019#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060020#include <memalign.h>
Andy Fleming272cc702008-10-30 16:41:01 -050021#include <linux/list.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053022#include <div64.h>
Paul Burtonda61fa52013-09-09 15:30:26 +010023#include "mmc_private.h"
Andy Fleming272cc702008-10-30 16:41:01 -050024
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +020025#define DEFAULT_CMD6_TIMEOUT_MS 500
26
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +020027static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
Marek Vasutb5b838f2016-12-01 02:06:33 +010028
Simon Glasse7881d82017-07-29 11:35:31 -060029#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020030
Sam Protsenko6cf8a902019-08-14 22:52:51 +030031static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020032{
33 return -ENOSYS;
34}
35
Jeroen Hofstee750121c2014-07-12 21:24:08 +020036__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000037{
38 return -1;
39}
40
41int mmc_getwp(struct mmc *mmc)
42{
43 int wp;
44
45 wp = board_mmc_getwp(mmc);
46
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000047 if (wp < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020048 if (mmc->cfg->ops->getwp)
49 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000050 else
51 wp = 0;
52 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000053
54 return wp;
55}
56
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +020057__weak int board_mmc_getcd(struct mmc *mmc)
58{
Stefano Babic11fdade2010-02-05 15:04:43 +010059 return -1;
60}
Simon Glass8ca51e52016-06-12 23:30:22 -060061#endif
Stefano Babic11fdade2010-02-05 15:04:43 +010062
Marek Vasut8635ff92012-03-15 18:41:35 +000063#ifdef CONFIG_MMC_TRACE
Simon Glassc0c76eb2016-06-12 23:30:20 -060064void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
65{
66 printf("CMD_SEND:%d\n", cmd->cmdidx);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010067 printf("\t\tARG\t\t\t 0x%08x\n", cmd->cmdarg);
Simon Glassc0c76eb2016-06-12 23:30:20 -060068}
69
70void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
71{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000072 int i;
73 u8 *ptr;
74
Bin Meng7863ce52016-03-17 21:53:14 -070075 if (ret) {
76 printf("\t\tRET\t\t\t %d\n", ret);
77 } else {
78 switch (cmd->resp_type) {
79 case MMC_RSP_NONE:
80 printf("\t\tMMC_RSP_NONE\n");
81 break;
82 case MMC_RSP_R1:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010083 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070084 cmd->response[0]);
85 break;
86 case MMC_RSP_R1b:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010087 printf("\t\tMMC_RSP_R1b\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070088 cmd->response[0]);
89 break;
90 case MMC_RSP_R2:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010091 printf("\t\tMMC_RSP_R2\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070092 cmd->response[0]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010093 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070094 cmd->response[1]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010095 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070096 cmd->response[2]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010097 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070098 cmd->response[3]);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000099 printf("\n");
Bin Meng7863ce52016-03-17 21:53:14 -0700100 printf("\t\t\t\t\tDUMPING DATA\n");
101 for (i = 0; i < 4; i++) {
102 int j;
103 printf("\t\t\t\t\t%03d - ", i*4);
104 ptr = (u8 *)&cmd->response[i];
105 ptr += 3;
106 for (j = 0; j < 4; j++)
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100107 printf("%02x ", *ptr--);
Bin Meng7863ce52016-03-17 21:53:14 -0700108 printf("\n");
109 }
110 break;
111 case MMC_RSP_R3:
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100112 printf("\t\tMMC_RSP_R3,4\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700113 cmd->response[0]);
114 break;
115 default:
116 printf("\t\tERROR MMC rsp not supported\n");
117 break;
Bin Meng53e8e402016-03-17 21:53:13 -0700118 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000119 }
Simon Glassc0c76eb2016-06-12 23:30:20 -0600120}
121
122void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
123{
124 int status;
125
126 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
127 printf("CURR STATE:%d\n", status);
128}
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000129#endif
Simon Glassc0c76eb2016-06-12 23:30:20 -0600130
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200131#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
132const char *mmc_mode_name(enum bus_mode mode)
133{
134 static const char *const names[] = {
135 [MMC_LEGACY] = "MMC legacy",
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200136 [MMC_HS] = "MMC High Speed (26MHz)",
137 [SD_HS] = "SD High Speed (50MHz)",
138 [UHS_SDR12] = "UHS SDR12 (25MHz)",
139 [UHS_SDR25] = "UHS SDR25 (50MHz)",
140 [UHS_SDR50] = "UHS SDR50 (100MHz)",
141 [UHS_SDR104] = "UHS SDR104 (208MHz)",
142 [UHS_DDR50] = "UHS DDR50 (50MHz)",
143 [MMC_HS_52] = "MMC High Speed (52MHz)",
144 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
145 [MMC_HS_200] = "HS200 (200MHz)",
Peng Fan3dd26262018-08-10 14:07:54 +0800146 [MMC_HS_400] = "HS400 (200MHz)",
Peng Fan44acd492019-07-10 14:43:07 +0800147 [MMC_HS_400_ES] = "HS400ES (200MHz)",
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200148 };
149
150 if (mode >= MMC_MODES_END)
151 return "Unknown mode";
152 else
153 return names[mode];
154}
155#endif
156
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200157static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
158{
159 static const int freqs[] = {
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900160 [MMC_LEGACY] = 25000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200161 [MMC_HS] = 26000000,
162 [SD_HS] = 50000000,
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900163 [MMC_HS_52] = 52000000,
164 [MMC_DDR_52] = 52000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200165 [UHS_SDR12] = 25000000,
166 [UHS_SDR25] = 50000000,
167 [UHS_SDR50] = 100000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200168 [UHS_DDR50] = 50000000,
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100169 [UHS_SDR104] = 208000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200170 [MMC_HS_200] = 200000000,
Peng Fan3dd26262018-08-10 14:07:54 +0800171 [MMC_HS_400] = 200000000,
Peng Fan44acd492019-07-10 14:43:07 +0800172 [MMC_HS_400_ES] = 200000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200173 };
174
175 if (mode == MMC_LEGACY)
176 return mmc->legacy_speed;
177 else if (mode >= MMC_MODES_END)
178 return 0;
179 else
180 return freqs[mode];
181}
182
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200183static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
184{
185 mmc->selected_mode = mode;
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200186 mmc->tran_speed = mmc_mode2freq(mmc, mode);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200187 mmc->ddr_mode = mmc_is_mode_ddr(mode);
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900188 pr_debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
189 mmc->tran_speed / 1000000);
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200190 return 0;
191}
192
Simon Glasse7881d82017-07-29 11:35:31 -0600193#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassc0c76eb2016-06-12 23:30:20 -0600194int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
195{
196 int ret;
197
198 mmmc_trace_before_send(mmc, cmd);
199 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
200 mmmc_trace_after_send(mmc, cmd, ret);
201
Marek Vasut8635ff92012-03-15 18:41:35 +0000202 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500203}
Simon Glass8ca51e52016-06-12 23:30:22 -0600204#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500205
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200206int mmc_send_status(struct mmc *mmc, unsigned int *status)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000207{
208 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000209 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000210
211 cmd.cmdidx = MMC_CMD_SEND_STATUS;
212 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200213 if (!mmc_host_is_spi(mmc))
214 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000215
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200216 while (retries--) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000217 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000218 if (!err) {
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200219 mmc_trace_state(mmc, &cmd);
220 *status = cmd.response[0];
221 return 0;
222 }
223 }
224 mmc_trace_state(mmc, &cmd);
225 return -ECOMM;
226}
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200227
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300228int mmc_poll_for_busy(struct mmc *mmc, int timeout_ms)
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200229{
230 unsigned int status;
231 int err;
232
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300233 err = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotcd0b80e2019-07-02 10:53:53 +0200234 if (err != -ENOSYS)
235 return err;
236
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200237 while (1) {
238 err = mmc_send_status(mmc, &status);
239 if (err)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000240 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000241
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200242 if ((status & MMC_STATUS_RDY_FOR_DATA) &&
243 (status & MMC_STATUS_CURR_STATE) !=
244 MMC_STATE_PRG)
245 break;
246
247 if (status & MMC_STATUS_MASK) {
248#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
249 pr_err("Status Error: 0x%08x\n", status);
250#endif
251 return -ECOMM;
252 }
253
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300254 if (timeout_ms-- <= 0)
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500255 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000256
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500257 udelay(1000);
258 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000259
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300260 if (timeout_ms <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100261#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100262 pr_err("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100263#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900264 return -ETIMEDOUT;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000265 }
266
267 return 0;
268}
269
Paul Burtonda61fa52013-09-09 15:30:26 +0100270int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500271{
272 struct mmc_cmd cmd;
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +0200273 int err;
Andy Fleming272cc702008-10-30 16:41:01 -0500274
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600275 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900276 return 0;
277
Andy Fleming272cc702008-10-30 16:41:01 -0500278 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
279 cmd.resp_type = MMC_RSP_R1;
280 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500281
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +0200282 err = mmc_send_cmd(mmc, &cmd, NULL);
283
284#ifdef CONFIG_MMC_QUIRKS
285 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SET_BLOCKLEN)) {
286 int retries = 4;
287 /*
288 * It has been seen that SET_BLOCKLEN may fail on the first
289 * attempt, let's try a few more time
290 */
291 do {
292 err = mmc_send_cmd(mmc, &cmd, NULL);
293 if (!err)
294 break;
295 } while (retries--);
296 }
297#endif
298
299 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500300}
301
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100302#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200303static const u8 tuning_blk_pattern_4bit[] = {
304 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
305 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
306 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
307 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
308 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
309 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
310 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
311 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
312};
313
314static const u8 tuning_blk_pattern_8bit[] = {
315 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
316 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
317 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
318 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
319 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
320 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
321 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
322 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
323 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
324 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
325 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
326 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
327 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
328 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
329 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
330 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
331};
332
333int mmc_send_tuning(struct mmc *mmc, u32 opcode, int *cmd_error)
334{
335 struct mmc_cmd cmd;
336 struct mmc_data data;
337 const u8 *tuning_block_pattern;
338 int size, err;
339
340 if (mmc->bus_width == 8) {
341 tuning_block_pattern = tuning_blk_pattern_8bit;
342 size = sizeof(tuning_blk_pattern_8bit);
343 } else if (mmc->bus_width == 4) {
344 tuning_block_pattern = tuning_blk_pattern_4bit;
345 size = sizeof(tuning_blk_pattern_4bit);
346 } else {
347 return -EINVAL;
348 }
349
350 ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size);
351
352 cmd.cmdidx = opcode;
353 cmd.cmdarg = 0;
354 cmd.resp_type = MMC_RSP_R1;
355
356 data.dest = (void *)data_buf;
357 data.blocks = 1;
358 data.blocksize = size;
359 data.flags = MMC_DATA_READ;
360
361 err = mmc_send_cmd(mmc, &cmd, &data);
362 if (err)
363 return err;
364
365 if (memcmp(data_buf, tuning_block_pattern, size))
366 return -EIO;
367
368 return 0;
369}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100370#endif
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200371
Sascha Silbeff8fef52013-06-14 13:07:25 +0200372static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000373 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500374{
375 struct mmc_cmd cmd;
376 struct mmc_data data;
377
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700378 if (blkcnt > 1)
379 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
380 else
381 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500382
383 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700384 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500385 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700386 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500387
388 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500389
390 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700391 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500392 data.blocksize = mmc->read_bl_len;
393 data.flags = MMC_DATA_READ;
394
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700395 if (mmc_send_cmd(mmc, &cmd, &data))
396 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500397
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700398 if (blkcnt > 1) {
399 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
400 cmd.cmdarg = 0;
401 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700402 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton56196822013-09-04 16:12:25 +0100403#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100404 pr_err("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100405#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700406 return 0;
407 }
Andy Fleming272cc702008-10-30 16:41:01 -0500408 }
409
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700410 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500411}
412
Marek Vasut145429a2020-04-04 12:45:05 +0200413#if !CONFIG_IS_ENABLED(DM_MMC)
414static int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
415{
416 if (mmc->cfg->ops->get_b_max)
417 return mmc->cfg->ops->get_b_max(mmc, dst, blkcnt);
418 else
419 return mmc->cfg->b_max;
420}
421#endif
422
Simon Glassc4d660d2017-07-04 13:31:19 -0600423#if CONFIG_IS_ENABLED(BLK)
Simon Glass7dba0b92016-06-12 23:30:15 -0600424ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600425#else
Simon Glass7dba0b92016-06-12 23:30:15 -0600426ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
427 void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600428#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500429{
Simon Glassc4d660d2017-07-04 13:31:19 -0600430#if CONFIG_IS_ENABLED(BLK)
Simon Glass33fb2112016-05-01 13:52:41 -0600431 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
432#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700433 int dev_num = block_dev->devnum;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700434 int err;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700435 lbaint_t cur, blocks_todo = blkcnt;
Marek Vasut145429a2020-04-04 12:45:05 +0200436 uint b_max;
Andy Fleming272cc702008-10-30 16:41:01 -0500437
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700438 if (blkcnt == 0)
439 return 0;
440
441 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500442 if (!mmc)
443 return 0;
444
Marek Vasutb5b838f2016-12-01 02:06:33 +0100445 if (CONFIG_IS_ENABLED(MMC_TINY))
446 err = mmc_switch_part(mmc, block_dev->hwpart);
447 else
448 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
449
Stephen Warren873cc1d2015-12-07 11:38:49 -0700450 if (err < 0)
451 return 0;
452
Simon Glassc40fdca2016-05-01 13:52:35 -0600453 if ((start + blkcnt) > block_dev->lba) {
Paul Burton56196822013-09-04 16:12:25 +0100454#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100455 pr_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
456 start + blkcnt, block_dev->lba);
Paul Burton56196822013-09-04 16:12:25 +0100457#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800458 return 0;
459 }
Andy Fleming272cc702008-10-30 16:41:01 -0500460
Simon Glass11692992015-06-23 15:38:50 -0600461 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900462 pr_debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500463 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600464 }
Andy Fleming272cc702008-10-30 16:41:01 -0500465
Marek Vasut145429a2020-04-04 12:45:05 +0200466 b_max = mmc_get_b_max(mmc, dst, blkcnt);
467
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700468 do {
Marek Vasut145429a2020-04-04 12:45:05 +0200469 cur = (blocks_todo > b_max) ? b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600470 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900471 pr_debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700472 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600473 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700474 blocks_todo -= cur;
475 start += cur;
476 dst += cur * mmc->read_bl_len;
477 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500478
479 return blkcnt;
480}
481
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000482static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500483{
484 struct mmc_cmd cmd;
485 int err;
486
487 udelay(1000);
488
489 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
490 cmd.cmdarg = 0;
491 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500492
493 err = mmc_send_cmd(mmc, &cmd, NULL);
494
495 if (err)
496 return err;
497
498 udelay(2000);
499
500 return 0;
501}
502
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100503#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200504static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
505{
506 struct mmc_cmd cmd;
507 int err = 0;
508
509 /*
510 * Send CMD11 only if the request is to switch the card to
511 * 1.8V signalling.
512 */
513 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
514 return mmc_set_signal_voltage(mmc, signal_voltage);
515
516 cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
517 cmd.cmdarg = 0;
518 cmd.resp_type = MMC_RSP_R1;
519
520 err = mmc_send_cmd(mmc, &cmd, NULL);
521 if (err)
522 return err;
523
524 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
525 return -EIO;
526
527 /*
528 * The card should drive cmd and dat[0:3] low immediately
529 * after the response of cmd11, but wait 100 us to be sure
530 */
531 err = mmc_wait_dat0(mmc, 0, 100);
532 if (err == -ENOSYS)
533 udelay(100);
534 else if (err)
535 return -ETIMEDOUT;
536
537 /*
538 * During a signal voltage level switch, the clock must be gated
539 * for 5 ms according to the SD spec
540 */
Jaehoon Chung65117182018-01-26 19:25:29 +0900541 mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200542
543 err = mmc_set_signal_voltage(mmc, signal_voltage);
544 if (err)
545 return err;
546
547 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
548 mdelay(10);
Jaehoon Chung65117182018-01-26 19:25:29 +0900549 mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200550
551 /*
552 * Failure to switch is indicated by the card holding
553 * dat[0:3] low. Wait for at least 1 ms according to spec
554 */
555 err = mmc_wait_dat0(mmc, 1, 1000);
556 if (err == -ENOSYS)
557 udelay(1000);
558 else if (err)
559 return -ETIMEDOUT;
560
561 return 0;
562}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100563#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200564
565static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
Andy Fleming272cc702008-10-30 16:41:01 -0500566{
567 int timeout = 1000;
568 int err;
569 struct mmc_cmd cmd;
570
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500571 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500572 cmd.cmdidx = MMC_CMD_APP_CMD;
573 cmd.resp_type = MMC_RSP_R1;
574 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500575
576 err = mmc_send_cmd(mmc, &cmd, NULL);
577
578 if (err)
579 return err;
580
581 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
582 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100583
584 /*
585 * Most cards do not answer if some reserved bits
586 * in the ocr are set. However, Some controller
587 * can set bit 7 (reserved for low voltages), but
588 * how to manage low voltages SD card is not yet
589 * specified.
590 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000591 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200592 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500593
594 if (mmc->version == SD_VERSION_2)
595 cmd.cmdarg |= OCR_HCS;
596
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200597 if (uhs_en)
598 cmd.cmdarg |= OCR_S18R;
599
Andy Fleming272cc702008-10-30 16:41:01 -0500600 err = mmc_send_cmd(mmc, &cmd, NULL);
601
602 if (err)
603 return err;
604
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500605 if (cmd.response[0] & OCR_BUSY)
606 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500607
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500608 if (timeout-- <= 0)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900609 return -EOPNOTSUPP;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500610
611 udelay(1000);
612 }
Andy Fleming272cc702008-10-30 16:41:01 -0500613
614 if (mmc->version != SD_VERSION_2)
615 mmc->version = SD_VERSION_1_0;
616
Thomas Choud52ebf12010-12-24 13:12:21 +0000617 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
618 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
619 cmd.resp_type = MMC_RSP_R3;
620 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000621
622 err = mmc_send_cmd(mmc, &cmd, NULL);
623
624 if (err)
625 return err;
626 }
627
Rabin Vincent998be3d2009-04-05 13:30:56 +0530628 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500629
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100630#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200631 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
632 == 0x41000000) {
633 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
634 if (err)
635 return err;
636 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100637#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200638
Andy Fleming272cc702008-10-30 16:41:01 -0500639 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
640 mmc->rca = 0;
641
642 return 0;
643}
644
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500645static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500646{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500647 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500648 int err;
649
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500650 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
651 cmd.resp_type = MMC_RSP_R3;
652 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500653 if (use_arg && !mmc_host_is_spi(mmc))
654 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200655 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500656 (mmc->ocr & OCR_VOLTAGE_MASK)) |
657 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000658
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500659 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000660 if (err)
661 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500662 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000663 return 0;
664}
665
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200666static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000667{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000668 int err, i;
669
Andy Fleming272cc702008-10-30 16:41:01 -0500670 /* Some cards seem to need this */
671 mmc_go_idle(mmc);
672
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000673 /* Asking to the card its capabilities */
Che-Liang Chioue9550442012-11-28 15:21:13 +0000674 for (i = 0; i < 2; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500675 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500676 if (err)
677 return err;
678
Che-Liang Chioue9550442012-11-28 15:21:13 +0000679 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500680 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500681 break;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000682 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500683 mmc->op_cond_pending = 1;
684 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000685}
Andy Fleming272cc702008-10-30 16:41:01 -0500686
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200687static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000688{
689 struct mmc_cmd cmd;
690 int timeout = 1000;
Vipul Kumar36332b62018-05-03 12:20:54 +0530691 ulong start;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000692 int err;
693
694 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500695 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lud188b112016-08-02 15:33:18 +0800696 /* Some cards seem to need this */
697 mmc_go_idle(mmc);
698
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500699 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500700 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500701 err = mmc_send_op_cond_iter(mmc, 1);
702 if (err)
703 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500704 if (mmc->ocr & OCR_BUSY)
705 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500706 if (get_timer(start) > timeout)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900707 return -EOPNOTSUPP;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500708 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500709 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500710 }
Andy Fleming272cc702008-10-30 16:41:01 -0500711
Thomas Choud52ebf12010-12-24 13:12:21 +0000712 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
713 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
714 cmd.resp_type = MMC_RSP_R3;
715 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000716
717 err = mmc_send_cmd(mmc, &cmd, NULL);
718
719 if (err)
720 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500721
722 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000723 }
724
Andy Fleming272cc702008-10-30 16:41:01 -0500725 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500726
727 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700728 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500729
730 return 0;
731}
732
733
Heinrich Schuchardt1601ea22020-03-30 07:24:17 +0200734int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500735{
736 struct mmc_cmd cmd;
737 struct mmc_data data;
738 int err;
739
740 /* Get the Card Status Register */
741 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
742 cmd.resp_type = MMC_RSP_R1;
743 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500744
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000745 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500746 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000747 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500748 data.flags = MMC_DATA_READ;
749
750 err = mmc_send_cmd(mmc, &cmd, &data);
751
752 return err;
753}
754
Marek Vasut68925502019-02-06 11:34:27 +0100755static int __mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value,
756 bool send_status)
Andy Fleming272cc702008-10-30 16:41:01 -0500757{
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200758 unsigned int status, start;
Andy Fleming272cc702008-10-30 16:41:01 -0500759 struct mmc_cmd cmd;
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300760 int timeout_ms = DEFAULT_CMD6_TIMEOUT_MS;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200761 bool is_part_switch = (set == EXT_CSD_CMD_SET_NORMAL) &&
762 (index == EXT_CSD_PART_CONF);
Maxime Riparda9003dc2016-11-04 16:18:08 +0100763 int retries = 3;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000764 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500765
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200766 if (mmc->gen_cmd6_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300767 timeout_ms = mmc->gen_cmd6_time * 10;
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200768
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200769 if (is_part_switch && mmc->part_switch_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300770 timeout_ms = mmc->part_switch_time * 10;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200771
Andy Fleming272cc702008-10-30 16:41:01 -0500772 cmd.cmdidx = MMC_CMD_SWITCH;
773 cmd.resp_type = MMC_RSP_R1b;
774 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000775 (index << 16) |
776 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500777
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200778 do {
Maxime Riparda9003dc2016-11-04 16:18:08 +0100779 ret = mmc_send_cmd(mmc, &cmd, NULL);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200780 } while (ret && retries-- > 0);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000781
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200782 if (ret)
783 return ret;
784
785 start = get_timer(0);
786
787 /* poll dat0 for rdy/buys status */
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300788 ret = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200789 if (ret && ret != -ENOSYS)
790 return ret;
791
792 /*
793 * In cases when not allowed to poll by using CMD13 or because we aren't
794 * capable of polling by using mmc_wait_dat0, then rely on waiting the
795 * stated timeout to be sufficient.
796 */
797 if (ret == -ENOSYS && !send_status)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300798 mdelay(timeout_ms);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200799
800 /* Finally wait until the card is ready or indicates a failure
801 * to switch. It doesn't hurt to use CMD13 here even if send_status
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300802 * is false, because by now (after 'timeout_ms' ms) the bus should be
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200803 * reliable.
804 */
805 do {
806 ret = mmc_send_status(mmc, &status);
807
808 if (!ret && (status & MMC_STATUS_SWITCH_ERROR)) {
809 pr_debug("switch failed %d/%d/0x%x !\n", set, index,
810 value);
811 return -EIO;
Maxime Riparda9003dc2016-11-04 16:18:08 +0100812 }
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200813 if (!ret && (status & MMC_STATUS_RDY_FOR_DATA))
Marek Vasut68925502019-02-06 11:34:27 +0100814 return 0;
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200815 udelay(100);
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300816 } while (get_timer(start) < timeout_ms);
Marek Vasut68925502019-02-06 11:34:27 +0100817
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200818 return -ETIMEDOUT;
Andy Fleming272cc702008-10-30 16:41:01 -0500819}
820
Marek Vasut68925502019-02-06 11:34:27 +0100821int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
822{
823 return __mmc_switch(mmc, set, index, value, true);
824}
825
Heinrich Schuchardt0469d842020-03-30 07:24:19 +0200826int mmc_boot_wp(struct mmc *mmc)
827{
828 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, 1);
829}
830
Marek Vasut62d77ce2018-04-15 00:37:11 +0200831#if !CONFIG_IS_ENABLED(MMC_TINY)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100832static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode,
833 bool hsdowngrade)
Andy Fleming272cc702008-10-30 16:41:01 -0500834{
Andy Fleming272cc702008-10-30 16:41:01 -0500835 int err;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200836 int speed_bits;
837
838 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
839
840 switch (mode) {
841 case MMC_HS:
842 case MMC_HS_52:
843 case MMC_DDR_52:
844 speed_bits = EXT_CSD_TIMING_HS;
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200845 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100846#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200847 case MMC_HS_200:
848 speed_bits = EXT_CSD_TIMING_HS200;
849 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100850#endif
Peng Fan3dd26262018-08-10 14:07:54 +0800851#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
852 case MMC_HS_400:
853 speed_bits = EXT_CSD_TIMING_HS400;
854 break;
855#endif
Peng Fan44acd492019-07-10 14:43:07 +0800856#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
857 case MMC_HS_400_ES:
858 speed_bits = EXT_CSD_TIMING_HS400;
859 break;
860#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200861 case MMC_LEGACY:
862 speed_bits = EXT_CSD_TIMING_LEGACY;
863 break;
864 default:
865 return -EINVAL;
866 }
Marek Vasut68925502019-02-06 11:34:27 +0100867
868 err = __mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
869 speed_bits, !hsdowngrade);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200870 if (err)
871 return err;
872
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100873#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
874 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
875 /*
876 * In case the eMMC is in HS200/HS400 mode and we are downgrading
877 * to HS mode, the card clock are still running much faster than
878 * the supported HS mode clock, so we can not reliably read out
879 * Extended CSD. Reconfigure the controller to run at HS mode.
880 */
881 if (hsdowngrade) {
882 mmc_select_mode(mmc, MMC_HS);
883 mmc_set_clock(mmc, mmc_mode2freq(mmc, MMC_HS), false);
884 }
885#endif
886
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200887 if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
888 /* Now check to see that it worked */
889 err = mmc_send_ext_csd(mmc, test_csd);
890 if (err)
891 return err;
892
893 /* No high-speed support */
894 if (!test_csd[EXT_CSD_HS_TIMING])
895 return -ENOTSUPP;
896 }
897
898 return 0;
899}
900
901static int mmc_get_capabilities(struct mmc *mmc)
902{
903 u8 *ext_csd = mmc->ext_csd;
904 char cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500905
Jean-Jacques Hiblot00e446f2017-11-30 17:43:56 +0100906 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -0500907
Thomas Choud52ebf12010-12-24 13:12:21 +0000908 if (mmc_host_is_spi(mmc))
909 return 0;
910
Andy Fleming272cc702008-10-30 16:41:01 -0500911 /* Only version 4 supports high-speed */
912 if (mmc->version < MMC_VERSION_4)
913 return 0;
914
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200915 if (!ext_csd) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100916 pr_err("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200917 return -ENOTSUPP;
918 }
919
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600920 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
921
Peng Fan3dd26262018-08-10 14:07:54 +0800922 cardtype = ext_csd[EXT_CSD_CARD_TYPE];
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +0200923 mmc->cardtype = cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500924
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100925#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200926 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
927 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
928 mmc->card_caps |= MMC_MODE_HS200;
929 }
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100930#endif
Peng Fan44acd492019-07-10 14:43:07 +0800931#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
932 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Peng Fan3dd26262018-08-10 14:07:54 +0800933 if (cardtype & (EXT_CSD_CARD_TYPE_HS400_1_2V |
934 EXT_CSD_CARD_TYPE_HS400_1_8V)) {
935 mmc->card_caps |= MMC_MODE_HS400;
936 }
937#endif
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900938 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200939 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900940 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200941 mmc->card_caps |= MMC_MODE_HS_52MHz;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900942 }
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200943 if (cardtype & EXT_CSD_CARD_TYPE_26)
944 mmc->card_caps |= MMC_MODE_HS;
Andy Fleming272cc702008-10-30 16:41:01 -0500945
Peng Fan44acd492019-07-10 14:43:07 +0800946#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
947 if (ext_csd[EXT_CSD_STROBE_SUPPORT] &&
948 (mmc->card_caps & MMC_MODE_HS400)) {
949 mmc->card_caps |= MMC_MODE_HS400_ES;
950 }
951#endif
952
Andy Fleming272cc702008-10-30 16:41:01 -0500953 return 0;
954}
Marek Vasut62d77ce2018-04-15 00:37:11 +0200955#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500956
Stephen Warrenf866a462013-06-11 15:14:01 -0600957static int mmc_set_capacity(struct mmc *mmc, int part_num)
958{
959 switch (part_num) {
960 case 0:
961 mmc->capacity = mmc->capacity_user;
962 break;
963 case 1:
964 case 2:
965 mmc->capacity = mmc->capacity_boot;
966 break;
967 case 3:
968 mmc->capacity = mmc->capacity_rpmb;
969 break;
970 case 4:
971 case 5:
972 case 6:
973 case 7:
974 mmc->capacity = mmc->capacity_gp[part_num - 4];
975 break;
976 default:
977 return -1;
978 }
979
Simon Glassc40fdca2016-05-01 13:52:35 -0600980 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -0600981
982 return 0;
983}
984
Simon Glass7dba0b92016-06-12 23:30:15 -0600985int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +0000986{
Stephen Warrenf866a462013-06-11 15:14:01 -0600987 int ret;
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +0200988 int retry = 3;
Lei Wenbc897b12011-05-02 16:26:26 +0000989
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +0200990 do {
991 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
992 EXT_CSD_PART_CONF,
993 (mmc->part_config & ~PART_ACCESS_MASK)
994 | (part_num & PART_ACCESS_MASK));
995 } while (ret && retry--);
Stephen Warrenf866a462013-06-11 15:14:01 -0600996
Peter Bigot6dc93e72014-09-02 18:31:23 -0500997 /*
998 * Set the capacity if the switch succeeded or was intended
999 * to return to representing the raw device.
1000 */
Stephen Warren873cc1d2015-12-07 11:38:49 -07001001 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -05001002 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -06001003 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -07001004 }
Peter Bigot6dc93e72014-09-02 18:31:23 -05001005
1006 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +00001007}
1008
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001009#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001010int mmc_hwpart_config(struct mmc *mmc,
1011 const struct mmc_hwpart_conf *conf,
1012 enum mmc_hwpart_conf_mode mode)
1013{
1014 u8 part_attrs = 0;
1015 u32 enh_size_mult;
1016 u32 enh_start_addr;
1017 u32 gp_size_mult[4];
1018 u32 max_enh_size_mult;
1019 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001020 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001021 int i, pidx, err;
1022 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1023
1024 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
1025 return -EINVAL;
1026
1027 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001028 pr_err("eMMC >= 4.4 required for enhanced user data area\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001029 return -EMEDIUMTYPE;
1030 }
1031
1032 if (!(mmc->part_support & PART_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001033 pr_err("Card does not support partitioning\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001034 return -EMEDIUMTYPE;
1035 }
1036
1037 if (!mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001038 pr_err("Card does not define HC WP group size\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001039 return -EMEDIUMTYPE;
1040 }
1041
1042 /* check partition alignment and total enhanced size */
1043 if (conf->user.enh_size) {
1044 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
1045 conf->user.enh_start % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001046 pr_err("User data enhanced area not HC WP group "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001047 "size aligned\n");
1048 return -EINVAL;
1049 }
1050 part_attrs |= EXT_CSD_ENH_USR;
1051 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
1052 if (mmc->high_capacity) {
1053 enh_start_addr = conf->user.enh_start;
1054 } else {
1055 enh_start_addr = (conf->user.enh_start << 9);
1056 }
1057 } else {
1058 enh_size_mult = 0;
1059 enh_start_addr = 0;
1060 }
1061 tot_enh_size_mult += enh_size_mult;
1062
1063 for (pidx = 0; pidx < 4; pidx++) {
1064 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001065 pr_err("GP%i partition not HC WP group size "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001066 "aligned\n", pidx+1);
1067 return -EINVAL;
1068 }
1069 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
1070 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
1071 part_attrs |= EXT_CSD_ENH_GP(pidx);
1072 tot_enh_size_mult += gp_size_mult[pidx];
1073 }
1074 }
1075
1076 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001077 pr_err("Card does not support enhanced attribute\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001078 return -EMEDIUMTYPE;
1079 }
1080
1081 err = mmc_send_ext_csd(mmc, ext_csd);
1082 if (err)
1083 return err;
1084
1085 max_enh_size_mult =
1086 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
1087 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
1088 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
1089 if (tot_enh_size_mult > max_enh_size_mult) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001090 pr_err("Total enhanced size exceeds maximum (%u > %u)\n",
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001091 tot_enh_size_mult, max_enh_size_mult);
1092 return -EMEDIUMTYPE;
1093 }
1094
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001095 /* The default value of EXT_CSD_WR_REL_SET is device
1096 * dependent, the values can only be changed if the
1097 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
1098 * changed only once and before partitioning is completed. */
1099 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1100 if (conf->user.wr_rel_change) {
1101 if (conf->user.wr_rel_set)
1102 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
1103 else
1104 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
1105 }
1106 for (pidx = 0; pidx < 4; pidx++) {
1107 if (conf->gp_part[pidx].wr_rel_change) {
1108 if (conf->gp_part[pidx].wr_rel_set)
1109 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
1110 else
1111 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
1112 }
1113 }
1114
1115 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
1116 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
1117 puts("Card does not support host controlled partition write "
1118 "reliability settings\n");
1119 return -EMEDIUMTYPE;
1120 }
1121
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001122 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
1123 EXT_CSD_PARTITION_SETTING_COMPLETED) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001124 pr_err("Card already partitioned\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001125 return -EPERM;
1126 }
1127
1128 if (mode == MMC_HWPART_CONF_CHECK)
1129 return 0;
1130
1131 /* Partitioning requires high-capacity size definitions */
1132 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
1133 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1134 EXT_CSD_ERASE_GROUP_DEF, 1);
1135
1136 if (err)
1137 return err;
1138
1139 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1140
Jaehoon Chung4af66592020-01-17 15:06:54 +09001141#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001142 /* update erase group size to be high-capacity */
1143 mmc->erase_grp_size =
1144 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jaehoon Chung4af66592020-01-17 15:06:54 +09001145#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001146
1147 }
1148
1149 /* all OK, write the configuration */
1150 for (i = 0; i < 4; i++) {
1151 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1152 EXT_CSD_ENH_START_ADDR+i,
1153 (enh_start_addr >> (i*8)) & 0xFF);
1154 if (err)
1155 return err;
1156 }
1157 for (i = 0; i < 3; i++) {
1158 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1159 EXT_CSD_ENH_SIZE_MULT+i,
1160 (enh_size_mult >> (i*8)) & 0xFF);
1161 if (err)
1162 return err;
1163 }
1164 for (pidx = 0; pidx < 4; pidx++) {
1165 for (i = 0; i < 3; i++) {
1166 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1167 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1168 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1169 if (err)
1170 return err;
1171 }
1172 }
1173 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1174 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1175 if (err)
1176 return err;
1177
1178 if (mode == MMC_HWPART_CONF_SET)
1179 return 0;
1180
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001181 /* The WR_REL_SET is a write-once register but shall be
1182 * written before setting PART_SETTING_COMPLETED. As it is
1183 * write-once we can only write it when completing the
1184 * partitioning. */
1185 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1186 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1187 EXT_CSD_WR_REL_SET, wr_rel_set);
1188 if (err)
1189 return err;
1190 }
1191
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001192 /* Setting PART_SETTING_COMPLETED confirms the partition
1193 * configuration but it only becomes effective after power
1194 * cycle, so we do not adjust the partition related settings
1195 * in the mmc struct. */
1196
1197 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1198 EXT_CSD_PARTITION_SETTING,
1199 EXT_CSD_PARTITION_SETTING_COMPLETED);
1200 if (err)
1201 return err;
1202
1203 return 0;
1204}
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001205#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001206
Simon Glasse7881d82017-07-29 11:35:31 -06001207#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +00001208int mmc_getcd(struct mmc *mmc)
1209{
1210 int cd;
1211
1212 cd = board_mmc_getcd(mmc);
1213
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001214 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001215 if (mmc->cfg->ops->getcd)
1216 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001217 else
1218 cd = 1;
1219 }
Thierry Reding48972d92012-01-02 01:15:37 +00001220
1221 return cd;
1222}
Simon Glass8ca51e52016-06-12 23:30:22 -06001223#endif
Thierry Reding48972d92012-01-02 01:15:37 +00001224
Marek Vasut62d77ce2018-04-15 00:37:11 +02001225#if !CONFIG_IS_ENABLED(MMC_TINY)
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001226static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -05001227{
1228 struct mmc_cmd cmd;
1229 struct mmc_data data;
1230
1231 /* Switch the frequency */
1232 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1233 cmd.resp_type = MMC_RSP_R1;
1234 cmd.cmdarg = (mode << 31) | 0xffffff;
1235 cmd.cmdarg &= ~(0xf << (group * 4));
1236 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -05001237
1238 data.dest = (char *)resp;
1239 data.blocksize = 64;
1240 data.blocks = 1;
1241 data.flags = MMC_DATA_READ;
1242
1243 return mmc_send_cmd(mmc, &cmd, &data);
1244}
1245
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001246static int sd_get_capabilities(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001247{
1248 int err;
1249 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +05301250 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1251 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -05001252 struct mmc_data data;
1253 int timeout;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001254#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001255 u32 sd3_bus_mode;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001256#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001257
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301258 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05001259
Thomas Choud52ebf12010-12-24 13:12:21 +00001260 if (mmc_host_is_spi(mmc))
1261 return 0;
1262
Andy Fleming272cc702008-10-30 16:41:01 -05001263 /* Read the SCR to find out if this card supports higher speeds */
1264 cmd.cmdidx = MMC_CMD_APP_CMD;
1265 cmd.resp_type = MMC_RSP_R1;
1266 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001267
1268 err = mmc_send_cmd(mmc, &cmd, NULL);
1269
1270 if (err)
1271 return err;
1272
1273 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1274 cmd.resp_type = MMC_RSP_R1;
1275 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001276
1277 timeout = 3;
1278
1279retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +00001280 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -05001281 data.blocksize = 8;
1282 data.blocks = 1;
1283 data.flags = MMC_DATA_READ;
1284
1285 err = mmc_send_cmd(mmc, &cmd, &data);
1286
1287 if (err) {
1288 if (timeout--)
1289 goto retry_scr;
1290
1291 return err;
1292 }
1293
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001294 mmc->scr[0] = __be32_to_cpu(scr[0]);
1295 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -05001296
1297 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -07001298 case 0:
1299 mmc->version = SD_VERSION_1_0;
1300 break;
1301 case 1:
1302 mmc->version = SD_VERSION_1_10;
1303 break;
1304 case 2:
1305 mmc->version = SD_VERSION_2;
1306 if ((mmc->scr[0] >> 15) & 0x1)
1307 mmc->version = SD_VERSION_3;
1308 break;
1309 default:
1310 mmc->version = SD_VERSION_1_0;
1311 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001312 }
1313
Alagu Sankarb44c7082010-05-12 15:08:24 +05301314 if (mmc->scr[0] & SD_DATA_4BIT)
1315 mmc->card_caps |= MMC_MODE_4BIT;
1316
Andy Fleming272cc702008-10-30 16:41:01 -05001317 /* Version 1.0 doesn't support switching */
1318 if (mmc->version == SD_VERSION_1_0)
1319 return 0;
1320
1321 timeout = 4;
1322 while (timeout--) {
1323 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +00001324 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001325
1326 if (err)
1327 return err;
1328
1329 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001330 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -05001331 break;
1332 }
1333
Andy Fleming272cc702008-10-30 16:41:01 -05001334 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001335 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1336 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Fleming272cc702008-10-30 16:41:01 -05001337
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001338#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001339 /* Version before 3.0 don't support UHS modes */
1340 if (mmc->version < SD_VERSION_3)
1341 return 0;
1342
1343 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1344 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1345 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1346 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1347 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1348 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1349 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1350 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1351 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1352 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1353 mmc->card_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001354#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001355
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001356 return 0;
1357}
1358
1359static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1360{
1361 int err;
1362
1363 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001364 int speed;
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001365
Marek Vasutcf345762018-11-18 03:25:08 +01001366 /* SD version 1.00 and 1.01 does not support CMD 6 */
1367 if (mmc->version == SD_VERSION_1_0)
1368 return 0;
1369
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001370 switch (mode) {
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301371 case MMC_LEGACY:
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001372 speed = UHS_SDR12_BUS_SPEED;
1373 break;
1374 case SD_HS:
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001375 speed = HIGH_SPEED_BUS_SPEED;
1376 break;
1377#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1378 case UHS_SDR12:
1379 speed = UHS_SDR12_BUS_SPEED;
1380 break;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001381 case UHS_SDR25:
1382 speed = UHS_SDR25_BUS_SPEED;
1383 break;
1384 case UHS_SDR50:
1385 speed = UHS_SDR50_BUS_SPEED;
1386 break;
1387 case UHS_DDR50:
1388 speed = UHS_DDR50_BUS_SPEED;
1389 break;
1390 case UHS_SDR104:
1391 speed = UHS_SDR104_BUS_SPEED;
1392 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001393#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001394 default:
1395 return -EINVAL;
1396 }
1397
1398 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001399 if (err)
1400 return err;
1401
Jean-Jacques Hiblota0276f32018-02-09 12:09:27 +01001402 if (((__be32_to_cpu(switch_status[4]) >> 24) & 0xF) != speed)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001403 return -ENOTSUPP;
1404
1405 return 0;
1406}
1407
Marek Vasutec360e62018-04-15 00:36:45 +02001408static int sd_select_bus_width(struct mmc *mmc, int w)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001409{
1410 int err;
1411 struct mmc_cmd cmd;
1412
1413 if ((w != 4) && (w != 1))
1414 return -EINVAL;
1415
1416 cmd.cmdidx = MMC_CMD_APP_CMD;
1417 cmd.resp_type = MMC_RSP_R1;
1418 cmd.cmdarg = mmc->rca << 16;
1419
1420 err = mmc_send_cmd(mmc, &cmd, NULL);
1421 if (err)
1422 return err;
1423
1424 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1425 cmd.resp_type = MMC_RSP_R1;
1426 if (w == 4)
1427 cmd.cmdarg = 2;
1428 else if (w == 1)
1429 cmd.cmdarg = 0;
1430 err = mmc_send_cmd(mmc, &cmd, NULL);
1431 if (err)
1432 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001433
1434 return 0;
1435}
Marek Vasut62d77ce2018-04-15 00:37:11 +02001436#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001437
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001438#if CONFIG_IS_ENABLED(MMC_WRITE)
Peng Fan3697e592016-09-01 11:13:38 +08001439static int sd_read_ssr(struct mmc *mmc)
1440{
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001441 static const unsigned int sd_au_size[] = {
1442 0, SZ_16K / 512, SZ_32K / 512,
1443 SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
1444 SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
1445 SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
1446 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512,
1447 SZ_64M / 512,
1448 };
Peng Fan3697e592016-09-01 11:13:38 +08001449 int err, i;
1450 struct mmc_cmd cmd;
1451 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1452 struct mmc_data data;
1453 int timeout = 3;
1454 unsigned int au, eo, et, es;
1455
1456 cmd.cmdidx = MMC_CMD_APP_CMD;
1457 cmd.resp_type = MMC_RSP_R1;
1458 cmd.cmdarg = mmc->rca << 16;
1459
1460 err = mmc_send_cmd(mmc, &cmd, NULL);
Joel Johnsond4a5fa32020-01-11 09:08:14 -07001461#ifdef CONFIG_MMC_QUIRKS
1462 if (err && (mmc->quirks & MMC_QUIRK_RETRY_APP_CMD)) {
1463 int retries = 4;
1464 /*
1465 * It has been seen that APP_CMD may fail on the first
1466 * attempt, let's try a few more times
1467 */
1468 do {
1469 err = mmc_send_cmd(mmc, &cmd, NULL);
1470 if (!err)
1471 break;
1472 } while (retries--);
1473 }
1474#endif
Peng Fan3697e592016-09-01 11:13:38 +08001475 if (err)
1476 return err;
1477
1478 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1479 cmd.resp_type = MMC_RSP_R1;
1480 cmd.cmdarg = 0;
1481
1482retry_ssr:
1483 data.dest = (char *)ssr;
1484 data.blocksize = 64;
1485 data.blocks = 1;
1486 data.flags = MMC_DATA_READ;
1487
1488 err = mmc_send_cmd(mmc, &cmd, &data);
1489 if (err) {
1490 if (timeout--)
1491 goto retry_ssr;
1492
1493 return err;
1494 }
1495
1496 for (i = 0; i < 16; i++)
1497 ssr[i] = be32_to_cpu(ssr[i]);
1498
1499 au = (ssr[2] >> 12) & 0xF;
1500 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1501 mmc->ssr.au = sd_au_size[au];
1502 es = (ssr[3] >> 24) & 0xFF;
1503 es |= (ssr[2] & 0xFF) << 8;
1504 et = (ssr[3] >> 18) & 0x3F;
1505 if (es && et) {
1506 eo = (ssr[3] >> 16) & 0x3;
1507 mmc->ssr.erase_timeout = (et * 1000) / es;
1508 mmc->ssr.erase_offset = eo * 1000;
1509 }
1510 } else {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001511 pr_debug("Invalid Allocation Unit Size.\n");
Peng Fan3697e592016-09-01 11:13:38 +08001512 }
1513
1514 return 0;
1515}
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001516#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001517/* frequency bases */
1518/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001519static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001520 10000,
1521 100000,
1522 1000000,
1523 10000000,
1524};
1525
1526/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1527 * to platforms without floating point.
1528 */
Simon Glass61fe0762016-05-14 14:02:57 -06001529static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001530 0, /* reserved */
1531 10,
1532 12,
1533 13,
1534 15,
1535 20,
1536 25,
1537 30,
1538 35,
1539 40,
1540 45,
1541 50,
1542 55,
1543 60,
1544 70,
1545 80,
1546};
1547
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001548static inline int bus_width(uint cap)
1549{
1550 if (cap == MMC_MODE_8BIT)
1551 return 8;
1552 if (cap == MMC_MODE_4BIT)
1553 return 4;
1554 if (cap == MMC_MODE_1BIT)
1555 return 1;
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001556 pr_warn("invalid bus witdh capability 0x%x\n", cap);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001557 return 0;
1558}
1559
Simon Glasse7881d82017-07-29 11:35:31 -06001560#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001561#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001562static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1563{
1564 return -ENOTSUPP;
1565}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001566#endif
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001567
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001568static int mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001569{
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001570 int ret = 0;
1571
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001572 if (mmc->cfg->ops->set_ios)
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001573 ret = mmc->cfg->ops->set_ios(mmc);
1574
1575 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001576}
Yann Gautier3602a562019-09-19 17:56:12 +02001577
1578static int mmc_host_power_cycle(struct mmc *mmc)
1579{
1580 int ret = 0;
1581
1582 if (mmc->cfg->ops->host_power_cycle)
1583 ret = mmc->cfg->ops->host_power_cycle(mmc);
1584
1585 return ret;
1586}
Simon Glass8ca51e52016-06-12 23:30:22 -06001587#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001588
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001589int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
Andy Fleming272cc702008-10-30 16:41:01 -05001590{
Jaehoon Chungc0fafe62018-01-23 14:04:30 +09001591 if (!disable) {
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001592 if (clock > mmc->cfg->f_max)
1593 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001594
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001595 if (clock < mmc->cfg->f_min)
1596 clock = mmc->cfg->f_min;
1597 }
Andy Fleming272cc702008-10-30 16:41:01 -05001598
1599 mmc->clock = clock;
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001600 mmc->clk_disable = disable;
Andy Fleming272cc702008-10-30 16:41:01 -05001601
Jaehoon Chungd2faadb2018-01-26 19:25:30 +09001602 debug("clock is %s (%dHz)\n", disable ? "disabled" : "enabled", clock);
1603
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001604 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001605}
1606
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001607static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001608{
1609 mmc->bus_width = width;
1610
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001611 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001612}
1613
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001614#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1615/*
1616 * helper function to display the capabilities in a human
1617 * friendly manner. The capabilities include bus width and
1618 * supported modes.
1619 */
1620void mmc_dump_capabilities(const char *text, uint caps)
1621{
1622 enum bus_mode mode;
1623
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001624 pr_debug("%s: widths [", text);
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001625 if (caps & MMC_MODE_8BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001626 pr_debug("8, ");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001627 if (caps & MMC_MODE_4BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001628 pr_debug("4, ");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001629 if (caps & MMC_MODE_1BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001630 pr_debug("1, ");
1631 pr_debug("\b\b] modes [");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001632 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1633 if (MMC_CAP(mode) & caps)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001634 pr_debug("%s, ", mmc_mode_name(mode));
1635 pr_debug("\b\b]\n");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001636}
1637#endif
1638
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001639struct mode_width_tuning {
1640 enum bus_mode mode;
1641 uint widths;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001642#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001643 uint tuning;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001644#endif
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001645};
1646
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001647#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001648int mmc_voltage_to_mv(enum mmc_voltage voltage)
1649{
1650 switch (voltage) {
1651 case MMC_SIGNAL_VOLTAGE_000: return 0;
1652 case MMC_SIGNAL_VOLTAGE_330: return 3300;
1653 case MMC_SIGNAL_VOLTAGE_180: return 1800;
1654 case MMC_SIGNAL_VOLTAGE_120: return 1200;
1655 }
1656 return -EINVAL;
1657}
1658
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001659static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1660{
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001661 int err;
1662
1663 if (mmc->signal_voltage == signal_voltage)
1664 return 0;
1665
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001666 mmc->signal_voltage = signal_voltage;
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001667 err = mmc_set_ios(mmc);
1668 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001669 pr_debug("unable to set voltage (err %d)\n", err);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001670
1671 return err;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001672}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001673#else
1674static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1675{
1676 return 0;
1677}
1678#endif
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001679
Marek Vasut62d77ce2018-04-15 00:37:11 +02001680#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001681static const struct mode_width_tuning sd_modes_by_pref[] = {
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001682#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1683#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001684 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001685 .mode = UHS_SDR104,
1686 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1687 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1688 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001689#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001690 {
1691 .mode = UHS_SDR50,
1692 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1693 },
1694 {
1695 .mode = UHS_DDR50,
1696 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1697 },
1698 {
1699 .mode = UHS_SDR25,
1700 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1701 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001702#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001703 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001704 .mode = SD_HS,
1705 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1706 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001707#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001708 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001709 .mode = UHS_SDR12,
1710 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1711 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001712#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001713 {
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301714 .mode = MMC_LEGACY,
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001715 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1716 }
1717};
1718
1719#define for_each_sd_mode_by_pref(caps, mwt) \
1720 for (mwt = sd_modes_by_pref;\
1721 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1722 mwt++) \
1723 if (caps & MMC_CAP(mwt->mode))
1724
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001725static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001726{
1727 int err;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001728 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1729 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001730#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001731 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001732#else
1733 bool uhs_en = false;
1734#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001735 uint caps;
1736
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001737#ifdef DEBUG
1738 mmc_dump_capabilities("sd card", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001739 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001740#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001741
Anup Patelf49ff792019-07-08 04:10:43 +00001742 if (mmc_host_is_spi(mmc)) {
1743 mmc_set_bus_width(mmc, 1);
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301744 mmc_select_mode(mmc, MMC_LEGACY);
Anup Patelf49ff792019-07-08 04:10:43 +00001745 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
1746 return 0;
1747 }
1748
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001749 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001750 caps = card_caps & mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001751
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001752 if (!uhs_en)
1753 caps &= ~UHS_CAPS;
1754
1755 for_each_sd_mode_by_pref(caps, mwt) {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001756 uint *w;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001757
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001758 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001759 if (*w & caps & mwt->widths) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001760 pr_debug("trying mode %s width %d (at %d MHz)\n",
1761 mmc_mode_name(mwt->mode),
1762 bus_width(*w),
1763 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001764
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001765 /* configure the bus width (card + host) */
1766 err = sd_select_bus_width(mmc, bus_width(*w));
1767 if (err)
1768 goto error;
1769 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001770
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001771 /* configure the bus mode (card) */
1772 err = sd_set_card_speed(mmc, mwt->mode);
1773 if (err)
1774 goto error;
1775
1776 /* configure the bus mode (host) */
1777 mmc_select_mode(mmc, mwt->mode);
Jaehoon Chung65117182018-01-26 19:25:29 +09001778 mmc_set_clock(mmc, mmc->tran_speed,
1779 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001780
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001781#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001782 /* execute tuning if needed */
1783 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1784 err = mmc_execute_tuning(mmc,
1785 mwt->tuning);
1786 if (err) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001787 pr_debug("tuning failed\n");
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001788 goto error;
1789 }
1790 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001791#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001792
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001793#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001794 err = sd_read_ssr(mmc);
Peng Fan0a4c2b02018-03-05 16:20:40 +08001795 if (err)
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001796 pr_warn("unable to read ssr\n");
1797#endif
1798 if (!err)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001799 return 0;
1800
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001801error:
1802 /* revert to a safer bus speed */
Faiz Abbase8d5dde2020-02-26 13:44:32 +05301803 mmc_select_mode(mmc, MMC_LEGACY);
Jaehoon Chung65117182018-01-26 19:25:29 +09001804 mmc_set_clock(mmc, mmc->tran_speed,
1805 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001806 }
1807 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001808 }
1809
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001810 pr_err("unable to select a mode\n");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001811 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001812}
1813
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001814/*
1815 * read the compare the part of ext csd that is constant.
1816 * This can be used to check that the transfer is working
1817 * as expected.
1818 */
1819static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1820{
1821 int err;
1822 const u8 *ext_csd = mmc->ext_csd;
1823 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1824
Jean-Jacques Hiblot1de06b92017-11-30 17:43:58 +01001825 if (mmc->version < MMC_VERSION_4)
1826 return 0;
1827
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001828 err = mmc_send_ext_csd(mmc, test_csd);
1829 if (err)
1830 return err;
1831
1832 /* Only compare read only fields */
1833 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1834 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1835 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1836 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1837 ext_csd[EXT_CSD_REV]
1838 == test_csd[EXT_CSD_REV] &&
1839 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1840 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1841 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1842 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1843 return 0;
1844
1845 return -EBADMSG;
1846}
1847
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001848#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001849static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1850 uint32_t allowed_mask)
1851{
1852 u32 card_mask = 0;
1853
1854 switch (mode) {
Peng Fan44acd492019-07-10 14:43:07 +08001855 case MMC_HS_400_ES:
Peng Fan3dd26262018-08-10 14:07:54 +08001856 case MMC_HS_400:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001857 case MMC_HS_200:
Peng Fan3dd26262018-08-10 14:07:54 +08001858 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_8V |
1859 EXT_CSD_CARD_TYPE_HS400_1_8V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001860 card_mask |= MMC_SIGNAL_VOLTAGE_180;
Peng Fan3dd26262018-08-10 14:07:54 +08001861 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1862 EXT_CSD_CARD_TYPE_HS400_1_2V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001863 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1864 break;
1865 case MMC_DDR_52:
1866 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
1867 card_mask |= MMC_SIGNAL_VOLTAGE_330 |
1868 MMC_SIGNAL_VOLTAGE_180;
1869 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
1870 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1871 break;
1872 default:
1873 card_mask |= MMC_SIGNAL_VOLTAGE_330;
1874 break;
1875 }
1876
1877 while (card_mask & allowed_mask) {
1878 enum mmc_voltage best_match;
1879
1880 best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
1881 if (!mmc_set_signal_voltage(mmc, best_match))
1882 return 0;
1883
1884 allowed_mask &= ~best_match;
1885 }
1886
1887 return -ENOTSUPP;
1888}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001889#else
1890static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1891 uint32_t allowed_mask)
1892{
1893 return 0;
1894}
1895#endif
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001896
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001897static const struct mode_width_tuning mmc_modes_by_pref[] = {
Peng Fan44acd492019-07-10 14:43:07 +08001898#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1899 {
1900 .mode = MMC_HS_400_ES,
1901 .widths = MMC_MODE_8BIT,
1902 },
1903#endif
Peng Fan3dd26262018-08-10 14:07:54 +08001904#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1905 {
1906 .mode = MMC_HS_400,
1907 .widths = MMC_MODE_8BIT,
1908 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
1909 },
1910#endif
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001911#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001912 {
1913 .mode = MMC_HS_200,
1914 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001915 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001916 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001917#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001918 {
1919 .mode = MMC_DDR_52,
1920 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1921 },
1922 {
1923 .mode = MMC_HS_52,
1924 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1925 },
1926 {
1927 .mode = MMC_HS,
1928 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1929 },
1930 {
1931 .mode = MMC_LEGACY,
1932 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1933 }
1934};
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001935
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001936#define for_each_mmc_mode_by_pref(caps, mwt) \
1937 for (mwt = mmc_modes_by_pref;\
1938 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
1939 mwt++) \
1940 if (caps & MMC_CAP(mwt->mode))
1941
1942static const struct ext_csd_bus_width {
1943 uint cap;
1944 bool is_ddr;
1945 uint ext_csd_bits;
1946} ext_csd_bus_width[] = {
1947 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
1948 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
1949 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
1950 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
1951 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
1952};
1953
Peng Fan3dd26262018-08-10 14:07:54 +08001954#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1955static int mmc_select_hs400(struct mmc *mmc)
1956{
1957 int err;
1958
1959 /* Set timing to HS200 for tuning */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01001960 err = mmc_set_card_speed(mmc, MMC_HS_200, false);
Peng Fan3dd26262018-08-10 14:07:54 +08001961 if (err)
1962 return err;
1963
1964 /* configure the bus mode (host) */
1965 mmc_select_mode(mmc, MMC_HS_200);
1966 mmc_set_clock(mmc, mmc->tran_speed, false);
1967
1968 /* execute tuning if needed */
1969 err = mmc_execute_tuning(mmc, MMC_CMD_SEND_TUNING_BLOCK_HS200);
1970 if (err) {
1971 debug("tuning failed\n");
1972 return err;
1973 }
1974
1975 /* Set back to HS */
BOUGH CHEN5cf12032019-03-26 06:24:17 +00001976 mmc_set_card_speed(mmc, MMC_HS, true);
Peng Fan3dd26262018-08-10 14:07:54 +08001977
1978 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1979 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG);
1980 if (err)
1981 return err;
1982
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01001983 err = mmc_set_card_speed(mmc, MMC_HS_400, false);
Peng Fan3dd26262018-08-10 14:07:54 +08001984 if (err)
1985 return err;
1986
1987 mmc_select_mode(mmc, MMC_HS_400);
1988 err = mmc_set_clock(mmc, mmc->tran_speed, false);
1989 if (err)
1990 return err;
1991
1992 return 0;
1993}
1994#else
1995static int mmc_select_hs400(struct mmc *mmc)
1996{
1997 return -ENOTSUPP;
1998}
1999#endif
2000
Peng Fan44acd492019-07-10 14:43:07 +08002001#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
2002#if !CONFIG_IS_ENABLED(DM_MMC)
2003static int mmc_set_enhanced_strobe(struct mmc *mmc)
2004{
2005 return -ENOTSUPP;
2006}
2007#endif
2008static int mmc_select_hs400es(struct mmc *mmc)
2009{
2010 int err;
2011
2012 err = mmc_set_card_speed(mmc, MMC_HS, true);
2013 if (err)
2014 return err;
2015
2016 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2017 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG |
2018 EXT_CSD_BUS_WIDTH_STROBE);
2019 if (err) {
2020 printf("switch to bus width for hs400 failed\n");
2021 return err;
2022 }
2023 /* TODO: driver strength */
2024 err = mmc_set_card_speed(mmc, MMC_HS_400_ES, false);
2025 if (err)
2026 return err;
2027
2028 mmc_select_mode(mmc, MMC_HS_400_ES);
2029 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2030 if (err)
2031 return err;
2032
2033 return mmc_set_enhanced_strobe(mmc);
2034}
2035#else
2036static int mmc_select_hs400es(struct mmc *mmc)
2037{
2038 return -ENOTSUPP;
2039}
2040#endif
2041
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002042#define for_each_supported_width(caps, ddr, ecbv) \
2043 for (ecbv = ext_csd_bus_width;\
2044 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
2045 ecbv++) \
2046 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
2047
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002048static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002049{
2050 int err;
2051 const struct mode_width_tuning *mwt;
2052 const struct ext_csd_bus_width *ecbw;
2053
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002054#ifdef DEBUG
2055 mmc_dump_capabilities("mmc", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002056 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002057#endif
2058
Anup Patelf49ff792019-07-08 04:10:43 +00002059 if (mmc_host_is_spi(mmc)) {
2060 mmc_set_bus_width(mmc, 1);
2061 mmc_select_mode(mmc, MMC_LEGACY);
2062 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
2063 return 0;
2064 }
2065
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002066 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002067 card_caps &= mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002068
2069 /* Only version 4 of MMC supports wider bus widths */
2070 if (mmc->version < MMC_VERSION_4)
2071 return 0;
2072
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002073 if (!mmc->ext_csd) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002074 pr_debug("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002075 return -ENOTSUPP;
2076 }
2077
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002078#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
2079 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2080 /*
2081 * In case the eMMC is in HS200/HS400 mode, downgrade to HS mode
2082 * before doing anything else, since a transition from either of
2083 * the HS200/HS400 mode directly to legacy mode is not supported.
2084 */
2085 if (mmc->selected_mode == MMC_HS_200 ||
2086 mmc->selected_mode == MMC_HS_400)
2087 mmc_set_card_speed(mmc, MMC_HS, true);
2088 else
2089#endif
2090 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002091
2092 for_each_mmc_mode_by_pref(card_caps, mwt) {
2093 for_each_supported_width(card_caps & mwt->widths,
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002094 mmc_is_mode_ddr(mwt->mode), ecbw) {
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002095 enum mmc_voltage old_voltage;
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002096 pr_debug("trying mode %s width %d (at %d MHz)\n",
2097 mmc_mode_name(mwt->mode),
2098 bus_width(ecbw->cap),
2099 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002100 old_voltage = mmc->signal_voltage;
2101 err = mmc_set_lowest_voltage(mmc, mwt->mode,
2102 MMC_ALL_SIGNAL_VOLTAGE);
2103 if (err)
2104 continue;
2105
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002106 /* configure the bus width (card + host) */
2107 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2108 EXT_CSD_BUS_WIDTH,
2109 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
2110 if (err)
2111 goto error;
2112 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
2113
Peng Fan3dd26262018-08-10 14:07:54 +08002114 if (mwt->mode == MMC_HS_400) {
2115 err = mmc_select_hs400(mmc);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002116 if (err) {
Peng Fan3dd26262018-08-10 14:07:54 +08002117 printf("Select HS400 failed %d\n", err);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002118 goto error;
2119 }
Peng Fan44acd492019-07-10 14:43:07 +08002120 } else if (mwt->mode == MMC_HS_400_ES) {
2121 err = mmc_select_hs400es(mmc);
2122 if (err) {
2123 printf("Select HS400ES failed %d\n",
2124 err);
2125 goto error;
2126 }
Peng Fan3dd26262018-08-10 14:07:54 +08002127 } else {
2128 /* configure the bus speed (card) */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002129 err = mmc_set_card_speed(mmc, mwt->mode, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002130 if (err)
2131 goto error;
2132
2133 /*
2134 * configure the bus width AND the ddr mode
2135 * (card). The host side will be taken care
2136 * of in the next step
2137 */
2138 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
2139 err = mmc_switch(mmc,
2140 EXT_CSD_CMD_SET_NORMAL,
2141 EXT_CSD_BUS_WIDTH,
2142 ecbw->ext_csd_bits);
2143 if (err)
2144 goto error;
2145 }
2146
2147 /* configure the bus mode (host) */
2148 mmc_select_mode(mmc, mwt->mode);
2149 mmc_set_clock(mmc, mmc->tran_speed,
2150 MMC_CLK_ENABLE);
2151#ifdef MMC_SUPPORTS_TUNING
2152
2153 /* execute tuning if needed */
2154 if (mwt->tuning) {
2155 err = mmc_execute_tuning(mmc,
2156 mwt->tuning);
2157 if (err) {
2158 pr_debug("tuning failed\n");
2159 goto error;
2160 }
2161 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01002162#endif
Peng Fan3dd26262018-08-10 14:07:54 +08002163 }
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002164
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002165 /* do a transfer to check the configuration */
2166 err = mmc_read_and_compare_ext_csd(mmc);
2167 if (!err)
2168 return 0;
2169error:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002170 mmc_set_signal_voltage(mmc, old_voltage);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002171 /* if an error occured, revert to a safer bus mode */
2172 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2173 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
2174 mmc_select_mode(mmc, MMC_LEGACY);
2175 mmc_set_bus_width(mmc, 1);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002176 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002177 }
2178
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002179 pr_err("unable to select a mode\n");
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002180
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002181 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002182}
Marek Vasut62d77ce2018-04-15 00:37:11 +02002183#endif
2184
2185#if CONFIG_IS_ENABLED(MMC_TINY)
2186DEFINE_CACHE_ALIGN_BUFFER(u8, ext_csd_bkup, MMC_MAX_BLOCK_LEN);
2187#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002188
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002189static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002190{
2191 int err, i;
2192 u64 capacity;
2193 bool has_parts = false;
2194 bool part_completed;
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002195 static const u32 mmc_versions[] = {
2196 MMC_VERSION_4,
2197 MMC_VERSION_4_1,
2198 MMC_VERSION_4_2,
2199 MMC_VERSION_4_3,
Jean-Jacques Hiblotace1bed2018-02-09 12:09:28 +01002200 MMC_VERSION_4_4,
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002201 MMC_VERSION_4_41,
2202 MMC_VERSION_4_5,
2203 MMC_VERSION_5_0,
2204 MMC_VERSION_5_1
2205 };
2206
Marek Vasut62d77ce2018-04-15 00:37:11 +02002207#if CONFIG_IS_ENABLED(MMC_TINY)
2208 u8 *ext_csd = ext_csd_bkup;
2209
2210 if (IS_SD(mmc) || mmc->version < MMC_VERSION_4)
2211 return 0;
2212
2213 if (!mmc->ext_csd)
2214 memset(ext_csd_bkup, 0, sizeof(ext_csd_bkup));
2215
2216 err = mmc_send_ext_csd(mmc, ext_csd);
2217 if (err)
2218 goto error;
2219
2220 /* store the ext csd for future reference */
2221 if (!mmc->ext_csd)
2222 mmc->ext_csd = ext_csd;
2223#else
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002224 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002225
2226 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
2227 return 0;
2228
2229 /* check ext_csd version and capacity */
2230 err = mmc_send_ext_csd(mmc, ext_csd);
2231 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002232 goto error;
2233
2234 /* store the ext csd for future reference */
2235 if (!mmc->ext_csd)
2236 mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN);
2237 if (!mmc->ext_csd)
2238 return -ENOMEM;
2239 memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002240#endif
Alexander Kochetkov76584e32018-02-20 14:35:55 +03002241 if (ext_csd[EXT_CSD_REV] >= ARRAY_SIZE(mmc_versions))
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002242 return -EINVAL;
2243
2244 mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]];
2245
2246 if (mmc->version >= MMC_VERSION_4_2) {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002247 /*
2248 * According to the JEDEC Standard, the value of
2249 * ext_csd's capacity is valid if the value is more
2250 * than 2GB
2251 */
2252 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
2253 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
2254 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
2255 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2256 capacity *= MMC_MAX_BLOCK_LEN;
2257 if ((capacity >> 20) > 2 * 1024)
2258 mmc->capacity_user = capacity;
2259 }
2260
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +02002261 if (mmc->version >= MMC_VERSION_4_5)
2262 mmc->gen_cmd6_time = ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
2263
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002264 /* The partition data may be non-zero but it is only
2265 * effective if PARTITION_SETTING_COMPLETED is set in
2266 * EXT_CSD, so ignore any data if this bit is not set,
2267 * except for enabling the high-capacity group size
2268 * definition (see below).
2269 */
2270 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
2271 EXT_CSD_PARTITION_SETTING_COMPLETED);
2272
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +02002273 mmc->part_switch_time = ext_csd[EXT_CSD_PART_SWITCH_TIME];
2274 /* Some eMMC set the value too low so set a minimum */
2275 if (mmc->part_switch_time < MMC_MIN_PART_SWITCH_TIME && mmc->part_switch_time)
2276 mmc->part_switch_time = MMC_MIN_PART_SWITCH_TIME;
2277
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002278 /* store the partition info of emmc */
2279 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
2280 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
2281 ext_csd[EXT_CSD_BOOT_MULT])
2282 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
2283 if (part_completed &&
2284 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
2285 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
2286
2287 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
2288
2289 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
2290
2291 for (i = 0; i < 4; i++) {
2292 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
2293 uint mult = (ext_csd[idx + 2] << 16) +
2294 (ext_csd[idx + 1] << 8) + ext_csd[idx];
2295 if (mult)
2296 has_parts = true;
2297 if (!part_completed)
2298 continue;
2299 mmc->capacity_gp[i] = mult;
2300 mmc->capacity_gp[i] *=
2301 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2302 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2303 mmc->capacity_gp[i] <<= 19;
2304 }
2305
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002306#ifndef CONFIG_SPL_BUILD
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002307 if (part_completed) {
2308 mmc->enh_user_size =
2309 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
2310 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
2311 ext_csd[EXT_CSD_ENH_SIZE_MULT];
2312 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2313 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2314 mmc->enh_user_size <<= 19;
2315 mmc->enh_user_start =
2316 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
2317 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
2318 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
2319 ext_csd[EXT_CSD_ENH_START_ADDR];
2320 if (mmc->high_capacity)
2321 mmc->enh_user_start <<= 9;
2322 }
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002323#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002324
2325 /*
2326 * Host needs to enable ERASE_GRP_DEF bit if device is
2327 * partitioned. This bit will be lost every time after a reset
2328 * or power off. This will affect erase size.
2329 */
2330 if (part_completed)
2331 has_parts = true;
2332 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
2333 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
2334 has_parts = true;
2335 if (has_parts) {
2336 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2337 EXT_CSD_ERASE_GROUP_DEF, 1);
2338
2339 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002340 goto error;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002341
2342 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
2343 }
2344
2345 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002346#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002347 /* Read out group size from ext_csd */
2348 mmc->erase_grp_size =
2349 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002350#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002351 /*
2352 * if high capacity and partition setting completed
2353 * SEC_COUNT is valid even if it is smaller than 2 GiB
2354 * JEDEC Standard JESD84-B45, 6.2.4
2355 */
2356 if (mmc->high_capacity && part_completed) {
2357 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
2358 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
2359 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
2360 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
2361 capacity *= MMC_MAX_BLOCK_LEN;
2362 mmc->capacity_user = capacity;
2363 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002364 }
2365#if CONFIG_IS_ENABLED(MMC_WRITE)
2366 else {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002367 /* Calculate the group size from the csd value. */
2368 int erase_gsz, erase_gmul;
2369
2370 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
2371 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
2372 mmc->erase_grp_size = (erase_gsz + 1)
2373 * (erase_gmul + 1);
2374 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002375#endif
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002376#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002377 mmc->hc_wp_grp_size = 1024
2378 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
2379 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002380#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002381
2382 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
2383
2384 return 0;
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002385error:
2386 if (mmc->ext_csd) {
Marek Vasut62d77ce2018-04-15 00:37:11 +02002387#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002388 free(mmc->ext_csd);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002389#endif
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002390 mmc->ext_csd = NULL;
2391 }
2392 return err;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002393}
2394
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002395static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002396{
Stephen Warrenf866a462013-06-11 15:14:01 -06002397 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05002398 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002399 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05002400 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06002401 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05002402
Thomas Choud52ebf12010-12-24 13:12:21 +00002403#ifdef CONFIG_MMC_SPI_CRC_ON
2404 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
2405 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
2406 cmd.resp_type = MMC_RSP_R1;
2407 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002408 err = mmc_send_cmd(mmc, &cmd, NULL);
Thomas Choud52ebf12010-12-24 13:12:21 +00002409 if (err)
2410 return err;
2411 }
2412#endif
2413
Andy Fleming272cc702008-10-30 16:41:01 -05002414 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002415 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
2416 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05002417 cmd.resp_type = MMC_RSP_R2;
2418 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002419
2420 err = mmc_send_cmd(mmc, &cmd, NULL);
2421
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002422#ifdef CONFIG_MMC_QUIRKS
2423 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SEND_CID)) {
2424 int retries = 4;
2425 /*
2426 * It has been seen that SEND_CID may fail on the first
2427 * attempt, let's try a few more time
2428 */
2429 do {
2430 err = mmc_send_cmd(mmc, &cmd, NULL);
2431 if (!err)
2432 break;
2433 } while (retries--);
2434 }
2435#endif
2436
Andy Fleming272cc702008-10-30 16:41:01 -05002437 if (err)
2438 return err;
2439
2440 memcpy(mmc->cid, cmd.response, 16);
2441
2442 /*
2443 * For MMC cards, set the Relative Address.
2444 * For SD cards, get the Relatvie Address.
2445 * This also puts the cards into Standby State
2446 */
Thomas Choud52ebf12010-12-24 13:12:21 +00002447 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2448 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
2449 cmd.cmdarg = mmc->rca << 16;
2450 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05002451
Thomas Choud52ebf12010-12-24 13:12:21 +00002452 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002453
Thomas Choud52ebf12010-12-24 13:12:21 +00002454 if (err)
2455 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002456
Thomas Choud52ebf12010-12-24 13:12:21 +00002457 if (IS_SD(mmc))
2458 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
2459 }
Andy Fleming272cc702008-10-30 16:41:01 -05002460
2461 /* Get the Card-Specific Data */
2462 cmd.cmdidx = MMC_CMD_SEND_CSD;
2463 cmd.resp_type = MMC_RSP_R2;
2464 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05002465
2466 err = mmc_send_cmd(mmc, &cmd, NULL);
2467
2468 if (err)
2469 return err;
2470
Rabin Vincent998be3d2009-04-05 13:30:56 +05302471 mmc->csd[0] = cmd.response[0];
2472 mmc->csd[1] = cmd.response[1];
2473 mmc->csd[2] = cmd.response[2];
2474 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05002475
2476 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302477 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05002478
2479 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07002480 case 0:
2481 mmc->version = MMC_VERSION_1_2;
2482 break;
2483 case 1:
2484 mmc->version = MMC_VERSION_1_4;
2485 break;
2486 case 2:
2487 mmc->version = MMC_VERSION_2_2;
2488 break;
2489 case 3:
2490 mmc->version = MMC_VERSION_3;
2491 break;
2492 case 4:
2493 mmc->version = MMC_VERSION_4;
2494 break;
2495 default:
2496 mmc->version = MMC_VERSION_1_2;
2497 break;
Andy Fleming272cc702008-10-30 16:41:01 -05002498 }
2499 }
2500
2501 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302502 freq = fbase[(cmd.response[0] & 0x7)];
2503 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05002504
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002505 mmc->legacy_speed = freq * mult;
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002506 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05002507
Markus Niebelab711882013-12-16 13:40:46 +01002508 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05302509 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002510#if CONFIG_IS_ENABLED(MMC_WRITE)
Andy Fleming272cc702008-10-30 16:41:01 -05002511
2512 if (IS_SD(mmc))
2513 mmc->write_bl_len = mmc->read_bl_len;
2514 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05302515 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002516#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002517
2518 if (mmc->high_capacity) {
2519 csize = (mmc->csd[1] & 0x3f) << 16
2520 | (mmc->csd[2] & 0xffff0000) >> 16;
2521 cmult = 8;
2522 } else {
2523 csize = (mmc->csd[1] & 0x3ff) << 2
2524 | (mmc->csd[2] & 0xc0000000) >> 30;
2525 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2526 }
2527
Stephen Warrenf866a462013-06-11 15:14:01 -06002528 mmc->capacity_user = (csize + 1) << (cmult + 2);
2529 mmc->capacity_user *= mmc->read_bl_len;
2530 mmc->capacity_boot = 0;
2531 mmc->capacity_rpmb = 0;
2532 for (i = 0; i < 4; i++)
2533 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002534
Simon Glass8bfa1952013-04-03 08:54:30 +00002535 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2536 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05002537
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002538#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass8bfa1952013-04-03 08:54:30 +00002539 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2540 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002541#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002542
Markus Niebelab711882013-12-16 13:40:46 +01002543 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2544 cmd.cmdidx = MMC_CMD_SET_DSR;
2545 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2546 cmd.resp_type = MMC_RSP_NONE;
2547 if (mmc_send_cmd(mmc, &cmd, NULL))
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002548 pr_warn("MMC: SET_DSR failed\n");
Markus Niebelab711882013-12-16 13:40:46 +01002549 }
2550
Andy Fleming272cc702008-10-30 16:41:01 -05002551 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002552 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2553 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00002554 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002555 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00002556 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002557
Thomas Choud52ebf12010-12-24 13:12:21 +00002558 if (err)
2559 return err;
2560 }
Andy Fleming272cc702008-10-30 16:41:01 -05002561
Lei Wene6f99a52011-06-22 17:03:31 +00002562 /*
2563 * For SD, its erase group is always one sector
2564 */
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002565#if CONFIG_IS_ENABLED(MMC_WRITE)
Lei Wene6f99a52011-06-22 17:03:31 +00002566 mmc->erase_grp_size = 1;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002567#endif
Lei Wenbc897b12011-05-02 16:26:26 +00002568 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00002569
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002570 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002571 if (err)
2572 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05302573
Simon Glassc40fdca2016-05-01 13:52:35 -06002574 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06002575 if (err)
2576 return err;
2577
Marek Vasut62d77ce2018-04-15 00:37:11 +02002578#if CONFIG_IS_ENABLED(MMC_TINY)
2579 mmc_set_clock(mmc, mmc->legacy_speed, false);
Faiz Abbase8d5dde2020-02-26 13:44:32 +05302580 mmc_select_mode(mmc, MMC_LEGACY);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002581 mmc_set_bus_width(mmc, 1);
2582#else
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002583 if (IS_SD(mmc)) {
2584 err = sd_get_capabilities(mmc);
2585 if (err)
2586 return err;
2587 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2588 } else {
2589 err = mmc_get_capabilities(mmc);
2590 if (err)
2591 return err;
Masahiro Yamada8adf50e2020-01-23 14:31:12 +09002592 err = mmc_select_mode_and_width(mmc, mmc->card_caps);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002593 }
Marek Vasut62d77ce2018-04-15 00:37:11 +02002594#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002595 if (err)
2596 return err;
2597
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002598 mmc->best_mode = mmc->selected_mode;
Jaehoon Chungad5fd922012-03-26 21:16:03 +00002599
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002600 /* Fix the block length for DDR mode */
2601 if (mmc->ddr_mode) {
2602 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002603#if CONFIG_IS_ENABLED(MMC_WRITE)
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002604 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002605#endif
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002606 }
2607
Andy Fleming272cc702008-10-30 16:41:01 -05002608 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06002609 bdesc = mmc_get_blk_desc(mmc);
2610 bdesc->lun = 0;
2611 bdesc->hwpart = 0;
2612 bdesc->type = 0;
2613 bdesc->blksz = mmc->read_bl_len;
2614 bdesc->log2blksz = LOG2(bdesc->blksz);
2615 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01002616#if !defined(CONFIG_SPL_BUILD) || \
2617 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
Simon Glass27084c02019-09-25 08:56:27 -06002618 !CONFIG_IS_ENABLED(USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06002619 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00002620 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2621 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002622 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002623 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2624 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2625 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002626 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002627 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01002628#else
Simon Glassc40fdca2016-05-01 13:52:35 -06002629 bdesc->vendor[0] = 0;
2630 bdesc->product[0] = 0;
2631 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01002632#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002633
Andre Przywaraeef05fd2018-12-17 10:05:45 +00002634#if !defined(CONFIG_DM_MMC) && (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT))
2635 part_init(bdesc);
2636#endif
2637
Andy Fleming272cc702008-10-30 16:41:01 -05002638 return 0;
2639}
2640
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002641static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002642{
2643 struct mmc_cmd cmd;
2644 int err;
2645
2646 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2647 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002648 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05002649 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05002650
2651 err = mmc_send_cmd(mmc, &cmd, NULL);
2652
2653 if (err)
2654 return err;
2655
Rabin Vincent998be3d2009-04-05 13:30:56 +05302656 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002657 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002658 else
2659 mmc->version = SD_VERSION_2;
2660
2661 return 0;
2662}
2663
Simon Glassc4d660d2017-07-04 13:31:19 -06002664#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002665/* board-specific MMC power initializations. */
2666__weak void board_mmc_power_init(void)
2667{
2668}
Simon Glass05cbeb72017-04-22 19:10:56 -06002669#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002670
Peng Fan2051aef2016-10-11 15:08:43 +08002671static int mmc_power_init(struct mmc *mmc)
2672{
Simon Glassc4d660d2017-07-04 13:31:19 -06002673#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002674#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08002675 int ret;
2676
2677 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002678 &mmc->vmmc_supply);
2679 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002680 pr_debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002681
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002682 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2683 &mmc->vqmmc_supply);
2684 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002685 pr_debug("%s: No vqmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002686#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06002687#else /* !CONFIG_DM_MMC */
2688 /*
2689 * Driver model should use a regulator, as above, rather than calling
2690 * out to board code.
2691 */
2692 board_mmc_power_init();
2693#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002694 return 0;
2695}
2696
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002697/*
2698 * put the host in the initial state:
2699 * - turn on Vdd (card power supply)
2700 * - configure the bus width and clock to minimal values
2701 */
2702static void mmc_set_initial_state(struct mmc *mmc)
2703{
2704 int err;
2705
2706 /* First try to set 3.3V. If it fails set to 1.8V */
2707 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2708 if (err != 0)
2709 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2710 if (err != 0)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002711 pr_warn("mmc: failed to set signal voltage\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002712
2713 mmc_select_mode(mmc, MMC_LEGACY);
2714 mmc_set_bus_width(mmc, 1);
Jaehoon Chung65117182018-01-26 19:25:29 +09002715 mmc_set_clock(mmc, 0, MMC_CLK_ENABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002716}
2717
2718static int mmc_power_on(struct mmc *mmc)
2719{
2720#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2721 if (mmc->vmmc_supply) {
2722 int ret = regulator_set_enable(mmc->vmmc_supply, true);
2723
2724 if (ret) {
2725 puts("Error enabling VMMC supply\n");
2726 return ret;
2727 }
2728 }
2729#endif
2730 return 0;
2731}
2732
2733static int mmc_power_off(struct mmc *mmc)
2734{
Jaehoon Chung65117182018-01-26 19:25:29 +09002735 mmc_set_clock(mmc, 0, MMC_CLK_DISABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002736#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2737 if (mmc->vmmc_supply) {
2738 int ret = regulator_set_enable(mmc->vmmc_supply, false);
2739
2740 if (ret) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002741 pr_debug("Error disabling VMMC supply\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002742 return ret;
2743 }
2744 }
2745#endif
2746 return 0;
2747}
2748
2749static int mmc_power_cycle(struct mmc *mmc)
2750{
2751 int ret;
2752
2753 ret = mmc_power_off(mmc);
2754 if (ret)
2755 return ret;
Yann Gautier3602a562019-09-19 17:56:12 +02002756
2757 ret = mmc_host_power_cycle(mmc);
2758 if (ret)
2759 return ret;
2760
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002761 /*
2762 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2763 * to be on the safer side.
2764 */
2765 udelay(2000);
2766 return mmc_power_on(mmc);
2767}
2768
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002769int mmc_get_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002770{
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002771 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
Macpaul Linafd59322011-11-14 23:35:39 +00002772 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05002773
Lei Wenbc897b12011-05-02 16:26:26 +00002774 if (mmc->has_init)
2775 return 0;
2776
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08002777#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
2778 mmc_adapter_card_type_ident();
2779#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002780 err = mmc_power_init(mmc);
2781 if (err)
2782 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002783
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002784#ifdef CONFIG_MMC_QUIRKS
2785 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
Joel Johnsond4a5fa32020-01-11 09:08:14 -07002786 MMC_QUIRK_RETRY_SEND_CID |
2787 MMC_QUIRK_RETRY_APP_CMD;
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002788#endif
2789
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002790 err = mmc_power_cycle(mmc);
2791 if (err) {
2792 /*
2793 * if power cycling is not supported, we should not try
2794 * to use the UHS modes, because we wouldn't be able to
2795 * recover from an error during the UHS initialization.
2796 */
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002797 pr_debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002798 uhs_en = false;
2799 mmc->host_caps &= ~UHS_CAPS;
2800 err = mmc_power_on(mmc);
2801 }
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002802 if (err)
2803 return err;
2804
Simon Glasse7881d82017-07-29 11:35:31 -06002805#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06002806 /* The device has already been probed ready for use */
2807#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02002808 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002809 err = mmc->cfg->ops->init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002810 if (err)
2811 return err;
Simon Glass8ca51e52016-06-12 23:30:22 -06002812#endif
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06002813 mmc->ddr_mode = 0;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02002814
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002815retry:
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002816 mmc_set_initial_state(mmc);
Jean-Jacques Hiblot318a7a52017-09-21 16:30:01 +02002817
Andy Fleming272cc702008-10-30 16:41:01 -05002818 /* Reset the Card */
2819 err = mmc_go_idle(mmc);
2820
2821 if (err)
2822 return err;
2823
Lei Wenbc897b12011-05-02 16:26:26 +00002824 /* The internal partition reset to user partition(0) at every CMD0*/
Simon Glassc40fdca2016-05-01 13:52:35 -06002825 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00002826
Andy Fleming272cc702008-10-30 16:41:01 -05002827 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00002828 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002829
Andy Fleming272cc702008-10-30 16:41:01 -05002830 /* Now try to get the SD card's operating condition */
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002831 err = sd_send_op_cond(mmc, uhs_en);
2832 if (err && uhs_en) {
2833 uhs_en = false;
2834 mmc_power_cycle(mmc);
2835 goto retry;
2836 }
Andy Fleming272cc702008-10-30 16:41:01 -05002837
2838 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002839 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05002840 err = mmc_send_op_cond(mmc);
2841
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002842 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01002843#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002844 pr_err("Card did not respond to voltage select!\n");
Paul Burton56196822013-09-04 16:12:25 +01002845#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002846 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002847 }
2848 }
2849
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002850 return err;
2851}
2852
2853int mmc_start_init(struct mmc *mmc)
2854{
2855 bool no_card;
2856 int err = 0;
2857
2858 /*
2859 * all hosts are capable of 1 bit bus-width and able to use the legacy
2860 * timings.
2861 */
Faiz Abbase8d5dde2020-02-26 13:44:32 +05302862 mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(MMC_LEGACY) |
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002863 MMC_CAP(MMC_LEGACY) | MMC_MODE_1BIT;
Faiz Abbas32860bd2020-02-26 13:44:30 +05302864#if CONFIG_IS_ENABLED(DM_MMC)
2865 mmc_deferred_probe(mmc);
2866#endif
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002867#if !defined(CONFIG_MMC_BROKEN_CD)
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002868 no_card = mmc_getcd(mmc) == 0;
2869#else
2870 no_card = 0;
2871#endif
2872#if !CONFIG_IS_ENABLED(DM_MMC)
Baruch Siachfea39392019-07-22 15:52:12 +03002873 /* we pretend there's no card when init is NULL */
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002874 no_card = no_card || (mmc->cfg->ops->init == NULL);
2875#endif
2876 if (no_card) {
2877 mmc->has_init = 0;
2878#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2879 pr_err("MMC: no card present\n");
2880#endif
2881 return -ENOMEDIUM;
2882 }
2883
2884 err = mmc_get_op_cond(mmc);
2885
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002886 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002887 mmc->init_in_progress = 1;
2888
2889 return err;
2890}
2891
2892static int mmc_complete_init(struct mmc *mmc)
2893{
2894 int err = 0;
2895
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002896 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002897 if (mmc->op_cond_pending)
2898 err = mmc_complete_op_cond(mmc);
2899
2900 if (!err)
2901 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00002902 if (err)
2903 mmc->has_init = 0;
2904 else
2905 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002906 return err;
2907}
2908
2909int mmc_init(struct mmc *mmc)
2910{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002911 int err = 0;
Vipul Kumar36332b62018-05-03 12:20:54 +05302912 __maybe_unused ulong start;
Simon Glassc4d660d2017-07-04 13:31:19 -06002913#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06002914 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00002915
Simon Glass33fb2112016-05-01 13:52:41 -06002916 upriv->mmc = mmc;
2917#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00002918 if (mmc->has_init)
2919 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02002920
2921 start = get_timer(0);
2922
Che-Liang Chioue9550442012-11-28 15:21:13 +00002923 if (!mmc->init_in_progress)
2924 err = mmc_start_init(mmc);
2925
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002926 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002927 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01002928 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002929 pr_info("%s: %d, time %lu\n", __func__, err, get_timer(start));
Jagan Teki919b4852017-01-10 11:18:43 +01002930
Lei Wenbc897b12011-05-02 16:26:26 +00002931 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002932}
2933
Marek Vasutfceea992019-01-29 04:45:51 +01002934#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
2935 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
2936 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2937int mmc_deinit(struct mmc *mmc)
2938{
2939 u32 caps_filtered;
2940
2941 if (!mmc->has_init)
2942 return 0;
2943
2944 if (IS_SD(mmc)) {
2945 caps_filtered = mmc->card_caps &
2946 ~(MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25) |
2947 MMC_CAP(UHS_SDR50) | MMC_CAP(UHS_DDR50) |
2948 MMC_CAP(UHS_SDR104));
2949
2950 return sd_select_mode_and_width(mmc, caps_filtered);
2951 } else {
2952 caps_filtered = mmc->card_caps &
2953 ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_HS_400));
2954
2955 return mmc_select_mode_and_width(mmc, caps_filtered);
2956 }
2957}
2958#endif
2959
Markus Niebelab711882013-12-16 13:40:46 +01002960int mmc_set_dsr(struct mmc *mmc, u16 val)
2961{
2962 mmc->dsr = val;
2963 return 0;
2964}
2965
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002966/* CPU-specific MMC initializations */
2967__weak int cpu_mmc_init(bd_t *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05002968{
2969 return -1;
2970}
2971
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002972/* board-specific MMC initializations. */
2973__weak int board_mmc_init(bd_t *bis)
2974{
2975 return -1;
2976}
Andy Fleming272cc702008-10-30 16:41:01 -05002977
Che-Liang Chioue9550442012-11-28 15:21:13 +00002978void mmc_set_preinit(struct mmc *mmc, int preinit)
2979{
2980 mmc->preinit = preinit;
2981}
2982
Faiz Abbas8a856db2018-02-12 19:35:24 +05302983#if CONFIG_IS_ENABLED(DM_MMC)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002984static int mmc_probe(bd_t *bis)
2985{
Simon Glass4a1db6d2015-12-29 05:22:49 -07002986 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002987 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07002988 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002989
2990 ret = uclass_get(UCLASS_MMC, &uc);
2991 if (ret)
2992 return ret;
2993
Simon Glass4a1db6d2015-12-29 05:22:49 -07002994 /*
2995 * Try to add them in sequence order. Really with driver model we
2996 * should allow holes, but the current MMC list does not allow that.
2997 * So if we request 0, 1, 3 we will get 0, 1, 2.
2998 */
2999 for (i = 0; ; i++) {
3000 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
3001 if (ret == -ENODEV)
3002 break;
3003 }
3004 uclass_foreach_dev(dev, uc) {
3005 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003006 if (ret)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01003007 pr_err("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003008 }
3009
3010 return 0;
3011}
3012#else
3013static int mmc_probe(bd_t *bis)
3014{
3015 if (board_mmc_init(bis) < 0)
3016 cpu_mmc_init(bis);
3017
3018 return 0;
3019}
3020#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00003021
Andy Fleming272cc702008-10-30 16:41:01 -05003022int mmc_initialize(bd_t *bis)
3023{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003024 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003025 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003026 if (initialized) /* Avoid initializing mmc multiple times */
3027 return 0;
3028 initialized = 1;
3029
Simon Glassc4d660d2017-07-04 13:31:19 -06003030#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01003031#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06003032 mmc_list_init();
3033#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01003034#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003035 ret = mmc_probe(bis);
3036 if (ret)
3037 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05003038
Ying Zhangbb0dc102013-08-16 15:16:11 +08003039#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05003040 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08003041#endif
Andy Fleming272cc702008-10-30 16:41:01 -05003042
Simon Glassc40fdca2016-05-01 13:52:35 -06003043 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05003044 return 0;
3045}
Tomas Melincd3d4882016-11-25 11:01:03 +02003046
Lokesh Vutla80f02012019-09-09 14:40:36 +05303047#if CONFIG_IS_ENABLED(DM_MMC)
3048int mmc_init_device(int num)
3049{
3050 struct udevice *dev;
3051 struct mmc *m;
3052 int ret;
3053
3054 ret = uclass_get_device(UCLASS_MMC, num, &dev);
3055 if (ret)
3056 return ret;
3057
3058 m = mmc_get_mmc_dev(dev);
3059 if (!m)
3060 return 0;
3061#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
3062 mmc_set_preinit(m, 1);
3063#endif
3064 if (m->preinit)
3065 mmc_start_init(m);
3066
3067 return 0;
3068}
3069#endif
3070
Tomas Melincd3d4882016-11-25 11:01:03 +02003071#ifdef CONFIG_CMD_BKOPS_ENABLE
3072int mmc_set_bkops_enable(struct mmc *mmc)
3073{
3074 int err;
3075 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
3076
3077 err = mmc_send_ext_csd(mmc, ext_csd);
3078 if (err) {
3079 puts("Could not get ext_csd register values\n");
3080 return err;
3081 }
3082
3083 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
3084 puts("Background operations not supported on device\n");
3085 return -EMEDIUMTYPE;
3086 }
3087
3088 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
3089 puts("Background operations already enabled\n");
3090 return 0;
3091 }
3092
3093 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
3094 if (err) {
3095 puts("Failed to enable manual background operations\n");
3096 return err;
3097 }
3098
3099 puts("Enabled manual background operations\n");
3100
3101 return 0;
3102}
3103#endif