blob: a347308976fe925630253a3d71c0a718bd94ce4f [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>
11#include <command.h>
Sjoerd Simons8e3332e2015-08-30 16:55:45 -060012#include <dm.h>
13#include <dm/device-internal.h>
Stephen Warrend4622df2014-05-23 12:47:06 -060014#include <errno.h>
Andy Fleming272cc702008-10-30 16:41:01 -050015#include <mmc.h>
16#include <part.h>
Peng Fan2051aef2016-10-11 15:08:43 +080017#include <power/regulator.h>
Andy Fleming272cc702008-10-30 16:41:01 -050018#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060019#include <memalign.h>
Andy Fleming272cc702008-10-30 16:41:01 -050020#include <linux/list.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053021#include <div64.h>
Paul Burtonda61fa52013-09-09 15:30:26 +010022#include "mmc_private.h"
Andy Fleming272cc702008-10-30 16:41:01 -050023
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +020024#define DEFAULT_CMD6_TIMEOUT_MS 500
25
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +020026static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
Marek Vasutb5b838f2016-12-01 02:06:33 +010027
Simon Glasse7881d82017-07-29 11:35:31 -060028#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020029
Sam Protsenko6cf8a902019-08-14 22:52:51 +030030static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020031{
32 return -ENOSYS;
33}
34
Jeroen Hofstee750121c2014-07-12 21:24:08 +020035__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000036{
37 return -1;
38}
39
40int mmc_getwp(struct mmc *mmc)
41{
42 int wp;
43
44 wp = board_mmc_getwp(mmc);
45
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000046 if (wp < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020047 if (mmc->cfg->ops->getwp)
48 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000049 else
50 wp = 0;
51 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000052
53 return wp;
54}
55
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +020056__weak int board_mmc_getcd(struct mmc *mmc)
57{
Stefano Babic11fdade2010-02-05 15:04:43 +010058 return -1;
59}
Simon Glass8ca51e52016-06-12 23:30:22 -060060#endif
Stefano Babic11fdade2010-02-05 15:04:43 +010061
Marek Vasut8635ff92012-03-15 18:41:35 +000062#ifdef CONFIG_MMC_TRACE
Simon Glassc0c76eb2016-06-12 23:30:20 -060063void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
64{
65 printf("CMD_SEND:%d\n", cmd->cmdidx);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010066 printf("\t\tARG\t\t\t 0x%08x\n", cmd->cmdarg);
Simon Glassc0c76eb2016-06-12 23:30:20 -060067}
68
69void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
70{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000071 int i;
72 u8 *ptr;
73
Bin Meng7863ce52016-03-17 21:53:14 -070074 if (ret) {
75 printf("\t\tRET\t\t\t %d\n", ret);
76 } else {
77 switch (cmd->resp_type) {
78 case MMC_RSP_NONE:
79 printf("\t\tMMC_RSP_NONE\n");
80 break;
81 case MMC_RSP_R1:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010082 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070083 cmd->response[0]);
84 break;
85 case MMC_RSP_R1b:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010086 printf("\t\tMMC_RSP_R1b\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070087 cmd->response[0]);
88 break;
89 case MMC_RSP_R2:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010090 printf("\t\tMMC_RSP_R2\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070091 cmd->response[0]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010092 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070093 cmd->response[1]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010094 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070095 cmd->response[2]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010096 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070097 cmd->response[3]);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000098 printf("\n");
Bin Meng7863ce52016-03-17 21:53:14 -070099 printf("\t\t\t\t\tDUMPING DATA\n");
100 for (i = 0; i < 4; i++) {
101 int j;
102 printf("\t\t\t\t\t%03d - ", i*4);
103 ptr = (u8 *)&cmd->response[i];
104 ptr += 3;
105 for (j = 0; j < 4; j++)
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100106 printf("%02x ", *ptr--);
Bin Meng7863ce52016-03-17 21:53:14 -0700107 printf("\n");
108 }
109 break;
110 case MMC_RSP_R3:
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100111 printf("\t\tMMC_RSP_R3,4\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700112 cmd->response[0]);
113 break;
114 default:
115 printf("\t\tERROR MMC rsp not supported\n");
116 break;
Bin Meng53e8e402016-03-17 21:53:13 -0700117 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000118 }
Simon Glassc0c76eb2016-06-12 23:30:20 -0600119}
120
121void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
122{
123 int status;
124
125 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
126 printf("CURR STATE:%d\n", status);
127}
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000128#endif
Simon Glassc0c76eb2016-06-12 23:30:20 -0600129
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200130#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
131const char *mmc_mode_name(enum bus_mode mode)
132{
133 static const char *const names[] = {
134 [MMC_LEGACY] = "MMC legacy",
135 [SD_LEGACY] = "SD Legacy",
136 [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 [SD_LEGACY] = 25000000,
162 [MMC_HS] = 26000000,
163 [SD_HS] = 50000000,
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900164 [MMC_HS_52] = 52000000,
165 [MMC_DDR_52] = 52000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200166 [UHS_SDR12] = 25000000,
167 [UHS_SDR25] = 50000000,
168 [UHS_SDR50] = 100000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200169 [UHS_DDR50] = 50000000,
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100170 [UHS_SDR104] = 208000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200171 [MMC_HS_200] = 200000000,
Peng Fan3dd26262018-08-10 14:07:54 +0800172 [MMC_HS_400] = 200000000,
Peng Fan44acd492019-07-10 14:43:07 +0800173 [MMC_HS_400_ES] = 200000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200174 };
175
176 if (mode == MMC_LEGACY)
177 return mmc->legacy_speed;
178 else if (mode >= MMC_MODES_END)
179 return 0;
180 else
181 return freqs[mode];
182}
183
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200184static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
185{
186 mmc->selected_mode = mode;
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200187 mmc->tran_speed = mmc_mode2freq(mmc, mode);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200188 mmc->ddr_mode = mmc_is_mode_ddr(mode);
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900189 pr_debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
190 mmc->tran_speed / 1000000);
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200191 return 0;
192}
193
Simon Glasse7881d82017-07-29 11:35:31 -0600194#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassc0c76eb2016-06-12 23:30:20 -0600195int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
196{
197 int ret;
198
199 mmmc_trace_before_send(mmc, cmd);
200 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
201 mmmc_trace_after_send(mmc, cmd, ret);
202
Marek Vasut8635ff92012-03-15 18:41:35 +0000203 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500204}
Simon Glass8ca51e52016-06-12 23:30:22 -0600205#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500206
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200207int mmc_send_status(struct mmc *mmc, unsigned int *status)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000208{
209 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000210 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000211
212 cmd.cmdidx = MMC_CMD_SEND_STATUS;
213 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200214 if (!mmc_host_is_spi(mmc))
215 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000216
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200217 while (retries--) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000218 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000219 if (!err) {
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200220 mmc_trace_state(mmc, &cmd);
221 *status = cmd.response[0];
222 return 0;
223 }
224 }
225 mmc_trace_state(mmc, &cmd);
226 return -ECOMM;
227}
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200228
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300229int mmc_poll_for_busy(struct mmc *mmc, int timeout_ms)
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200230{
231 unsigned int status;
232 int err;
233
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300234 err = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotcd0b80e2019-07-02 10:53:53 +0200235 if (err != -ENOSYS)
236 return err;
237
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200238 while (1) {
239 err = mmc_send_status(mmc, &status);
240 if (err)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000241 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000242
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200243 if ((status & MMC_STATUS_RDY_FOR_DATA) &&
244 (status & MMC_STATUS_CURR_STATE) !=
245 MMC_STATE_PRG)
246 break;
247
248 if (status & MMC_STATUS_MASK) {
249#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
250 pr_err("Status Error: 0x%08x\n", status);
251#endif
252 return -ECOMM;
253 }
254
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300255 if (timeout_ms-- <= 0)
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500256 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000257
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500258 udelay(1000);
259 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000260
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300261 if (timeout_ms <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100262#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100263 pr_err("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100264#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900265 return -ETIMEDOUT;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000266 }
267
268 return 0;
269}
270
Paul Burtonda61fa52013-09-09 15:30:26 +0100271int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500272{
273 struct mmc_cmd cmd;
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +0200274 int err;
Andy Fleming272cc702008-10-30 16:41:01 -0500275
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600276 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900277 return 0;
278
Andy Fleming272cc702008-10-30 16:41:01 -0500279 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
280 cmd.resp_type = MMC_RSP_R1;
281 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500282
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +0200283 err = mmc_send_cmd(mmc, &cmd, NULL);
284
285#ifdef CONFIG_MMC_QUIRKS
286 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SET_BLOCKLEN)) {
287 int retries = 4;
288 /*
289 * It has been seen that SET_BLOCKLEN may fail on the first
290 * attempt, let's try a few more time
291 */
292 do {
293 err = mmc_send_cmd(mmc, &cmd, NULL);
294 if (!err)
295 break;
296 } while (retries--);
297 }
298#endif
299
300 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500301}
302
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100303#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200304static const u8 tuning_blk_pattern_4bit[] = {
305 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
306 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
307 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
308 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
309 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
310 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
311 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
312 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
313};
314
315static const u8 tuning_blk_pattern_8bit[] = {
316 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
317 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
318 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
319 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
320 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
321 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
322 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
323 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
324 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
325 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
326 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
327 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
328 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
329 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
330 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
331 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
332};
333
334int mmc_send_tuning(struct mmc *mmc, u32 opcode, int *cmd_error)
335{
336 struct mmc_cmd cmd;
337 struct mmc_data data;
338 const u8 *tuning_block_pattern;
339 int size, err;
340
341 if (mmc->bus_width == 8) {
342 tuning_block_pattern = tuning_blk_pattern_8bit;
343 size = sizeof(tuning_blk_pattern_8bit);
344 } else if (mmc->bus_width == 4) {
345 tuning_block_pattern = tuning_blk_pattern_4bit;
346 size = sizeof(tuning_blk_pattern_4bit);
347 } else {
348 return -EINVAL;
349 }
350
351 ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size);
352
353 cmd.cmdidx = opcode;
354 cmd.cmdarg = 0;
355 cmd.resp_type = MMC_RSP_R1;
356
357 data.dest = (void *)data_buf;
358 data.blocks = 1;
359 data.blocksize = size;
360 data.flags = MMC_DATA_READ;
361
362 err = mmc_send_cmd(mmc, &cmd, &data);
363 if (err)
364 return err;
365
366 if (memcmp(data_buf, tuning_block_pattern, size))
367 return -EIO;
368
369 return 0;
370}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100371#endif
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200372
Sascha Silbeff8fef52013-06-14 13:07:25 +0200373static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000374 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500375{
376 struct mmc_cmd cmd;
377 struct mmc_data data;
378
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700379 if (blkcnt > 1)
380 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
381 else
382 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500383
384 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700385 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500386 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700387 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500388
389 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500390
391 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700392 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500393 data.blocksize = mmc->read_bl_len;
394 data.flags = MMC_DATA_READ;
395
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700396 if (mmc_send_cmd(mmc, &cmd, &data))
397 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500398
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700399 if (blkcnt > 1) {
400 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
401 cmd.cmdarg = 0;
402 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700403 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton56196822013-09-04 16:12:25 +0100404#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100405 pr_err("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100406#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700407 return 0;
408 }
Andy Fleming272cc702008-10-30 16:41:01 -0500409 }
410
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700411 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500412}
413
Simon Glassc4d660d2017-07-04 13:31:19 -0600414#if CONFIG_IS_ENABLED(BLK)
Simon Glass7dba0b92016-06-12 23:30:15 -0600415ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600416#else
Simon Glass7dba0b92016-06-12 23:30:15 -0600417ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
418 void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600419#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500420{
Simon Glassc4d660d2017-07-04 13:31:19 -0600421#if CONFIG_IS_ENABLED(BLK)
Simon Glass33fb2112016-05-01 13:52:41 -0600422 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
423#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700424 int dev_num = block_dev->devnum;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700425 int err;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700426 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500427
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700428 if (blkcnt == 0)
429 return 0;
430
431 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500432 if (!mmc)
433 return 0;
434
Marek Vasutb5b838f2016-12-01 02:06:33 +0100435 if (CONFIG_IS_ENABLED(MMC_TINY))
436 err = mmc_switch_part(mmc, block_dev->hwpart);
437 else
438 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
439
Stephen Warren873cc1d2015-12-07 11:38:49 -0700440 if (err < 0)
441 return 0;
442
Simon Glassc40fdca2016-05-01 13:52:35 -0600443 if ((start + blkcnt) > block_dev->lba) {
Paul Burton56196822013-09-04 16:12:25 +0100444#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100445 pr_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
446 start + blkcnt, block_dev->lba);
Paul Burton56196822013-09-04 16:12:25 +0100447#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800448 return 0;
449 }
Andy Fleming272cc702008-10-30 16:41:01 -0500450
Simon Glass11692992015-06-23 15:38:50 -0600451 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900452 pr_debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500453 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600454 }
Andy Fleming272cc702008-10-30 16:41:01 -0500455
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700456 do {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200457 cur = (blocks_todo > mmc->cfg->b_max) ?
458 mmc->cfg->b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600459 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900460 pr_debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700461 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600462 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700463 blocks_todo -= cur;
464 start += cur;
465 dst += cur * mmc->read_bl_len;
466 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500467
468 return blkcnt;
469}
470
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000471static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500472{
473 struct mmc_cmd cmd;
474 int err;
475
476 udelay(1000);
477
478 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
479 cmd.cmdarg = 0;
480 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500481
482 err = mmc_send_cmd(mmc, &cmd, NULL);
483
484 if (err)
485 return err;
486
487 udelay(2000);
488
489 return 0;
490}
491
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100492#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200493static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
494{
495 struct mmc_cmd cmd;
496 int err = 0;
497
498 /*
499 * Send CMD11 only if the request is to switch the card to
500 * 1.8V signalling.
501 */
502 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
503 return mmc_set_signal_voltage(mmc, signal_voltage);
504
505 cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
506 cmd.cmdarg = 0;
507 cmd.resp_type = MMC_RSP_R1;
508
509 err = mmc_send_cmd(mmc, &cmd, NULL);
510 if (err)
511 return err;
512
513 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
514 return -EIO;
515
516 /*
517 * The card should drive cmd and dat[0:3] low immediately
518 * after the response of cmd11, but wait 100 us to be sure
519 */
520 err = mmc_wait_dat0(mmc, 0, 100);
521 if (err == -ENOSYS)
522 udelay(100);
523 else if (err)
524 return -ETIMEDOUT;
525
526 /*
527 * During a signal voltage level switch, the clock must be gated
528 * for 5 ms according to the SD spec
529 */
Jaehoon Chung65117182018-01-26 19:25:29 +0900530 mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200531
532 err = mmc_set_signal_voltage(mmc, signal_voltage);
533 if (err)
534 return err;
535
536 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
537 mdelay(10);
Jaehoon Chung65117182018-01-26 19:25:29 +0900538 mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200539
540 /*
541 * Failure to switch is indicated by the card holding
542 * dat[0:3] low. Wait for at least 1 ms according to spec
543 */
544 err = mmc_wait_dat0(mmc, 1, 1000);
545 if (err == -ENOSYS)
546 udelay(1000);
547 else if (err)
548 return -ETIMEDOUT;
549
550 return 0;
551}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100552#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200553
554static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
Andy Fleming272cc702008-10-30 16:41:01 -0500555{
556 int timeout = 1000;
557 int err;
558 struct mmc_cmd cmd;
559
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500560 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500561 cmd.cmdidx = MMC_CMD_APP_CMD;
562 cmd.resp_type = MMC_RSP_R1;
563 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500564
565 err = mmc_send_cmd(mmc, &cmd, NULL);
566
567 if (err)
568 return err;
569
570 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
571 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100572
573 /*
574 * Most cards do not answer if some reserved bits
575 * in the ocr are set. However, Some controller
576 * can set bit 7 (reserved for low voltages), but
577 * how to manage low voltages SD card is not yet
578 * specified.
579 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000580 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200581 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500582
583 if (mmc->version == SD_VERSION_2)
584 cmd.cmdarg |= OCR_HCS;
585
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200586 if (uhs_en)
587 cmd.cmdarg |= OCR_S18R;
588
Andy Fleming272cc702008-10-30 16:41:01 -0500589 err = mmc_send_cmd(mmc, &cmd, NULL);
590
591 if (err)
592 return err;
593
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500594 if (cmd.response[0] & OCR_BUSY)
595 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500596
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500597 if (timeout-- <= 0)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900598 return -EOPNOTSUPP;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500599
600 udelay(1000);
601 }
Andy Fleming272cc702008-10-30 16:41:01 -0500602
603 if (mmc->version != SD_VERSION_2)
604 mmc->version = SD_VERSION_1_0;
605
Thomas Choud52ebf12010-12-24 13:12:21 +0000606 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
607 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
608 cmd.resp_type = MMC_RSP_R3;
609 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000610
611 err = mmc_send_cmd(mmc, &cmd, NULL);
612
613 if (err)
614 return err;
615 }
616
Rabin Vincent998be3d2009-04-05 13:30:56 +0530617 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500618
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100619#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200620 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
621 == 0x41000000) {
622 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
623 if (err)
624 return err;
625 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100626#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200627
Andy Fleming272cc702008-10-30 16:41:01 -0500628 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
629 mmc->rca = 0;
630
631 return 0;
632}
633
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500634static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500635{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500636 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500637 int err;
638
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500639 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
640 cmd.resp_type = MMC_RSP_R3;
641 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500642 if (use_arg && !mmc_host_is_spi(mmc))
643 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200644 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500645 (mmc->ocr & OCR_VOLTAGE_MASK)) |
646 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000647
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500648 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000649 if (err)
650 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500651 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000652 return 0;
653}
654
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200655static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000656{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000657 int err, i;
658
Andy Fleming272cc702008-10-30 16:41:01 -0500659 /* Some cards seem to need this */
660 mmc_go_idle(mmc);
661
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000662 /* Asking to the card its capabilities */
Che-Liang Chioue9550442012-11-28 15:21:13 +0000663 for (i = 0; i < 2; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500664 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500665 if (err)
666 return err;
667
Che-Liang Chioue9550442012-11-28 15:21:13 +0000668 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500669 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500670 break;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000671 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500672 mmc->op_cond_pending = 1;
673 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000674}
Andy Fleming272cc702008-10-30 16:41:01 -0500675
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200676static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000677{
678 struct mmc_cmd cmd;
679 int timeout = 1000;
Vipul Kumar36332b62018-05-03 12:20:54 +0530680 ulong start;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000681 int err;
682
683 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500684 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lud188b112016-08-02 15:33:18 +0800685 /* Some cards seem to need this */
686 mmc_go_idle(mmc);
687
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500688 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500689 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500690 err = mmc_send_op_cond_iter(mmc, 1);
691 if (err)
692 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500693 if (mmc->ocr & OCR_BUSY)
694 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500695 if (get_timer(start) > timeout)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900696 return -EOPNOTSUPP;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500697 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500698 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500699 }
Andy Fleming272cc702008-10-30 16:41:01 -0500700
Thomas Choud52ebf12010-12-24 13:12:21 +0000701 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
702 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
703 cmd.resp_type = MMC_RSP_R3;
704 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000705
706 err = mmc_send_cmd(mmc, &cmd, NULL);
707
708 if (err)
709 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500710
711 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000712 }
713
Andy Fleming272cc702008-10-30 16:41:01 -0500714 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500715
716 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700717 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500718
719 return 0;
720}
721
722
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000723static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500724{
725 struct mmc_cmd cmd;
726 struct mmc_data data;
727 int err;
728
729 /* Get the Card Status Register */
730 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
731 cmd.resp_type = MMC_RSP_R1;
732 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500733
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000734 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500735 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000736 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500737 data.flags = MMC_DATA_READ;
738
739 err = mmc_send_cmd(mmc, &cmd, &data);
740
741 return err;
742}
743
Marek Vasut68925502019-02-06 11:34:27 +0100744static int __mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value,
745 bool send_status)
Andy Fleming272cc702008-10-30 16:41:01 -0500746{
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200747 unsigned int status, start;
Andy Fleming272cc702008-10-30 16:41:01 -0500748 struct mmc_cmd cmd;
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300749 int timeout_ms = DEFAULT_CMD6_TIMEOUT_MS;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200750 bool is_part_switch = (set == EXT_CSD_CMD_SET_NORMAL) &&
751 (index == EXT_CSD_PART_CONF);
Maxime Riparda9003dc2016-11-04 16:18:08 +0100752 int retries = 3;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000753 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500754
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200755 if (mmc->gen_cmd6_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300756 timeout_ms = mmc->gen_cmd6_time * 10;
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200757
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200758 if (is_part_switch && mmc->part_switch_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300759 timeout_ms = mmc->part_switch_time * 10;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200760
Andy Fleming272cc702008-10-30 16:41:01 -0500761 cmd.cmdidx = MMC_CMD_SWITCH;
762 cmd.resp_type = MMC_RSP_R1b;
763 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000764 (index << 16) |
765 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500766
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200767 do {
Maxime Riparda9003dc2016-11-04 16:18:08 +0100768 ret = mmc_send_cmd(mmc, &cmd, NULL);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200769 } while (ret && retries-- > 0);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000770
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200771 if (ret)
772 return ret;
773
774 start = get_timer(0);
775
776 /* poll dat0 for rdy/buys status */
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300777 ret = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200778 if (ret && ret != -ENOSYS)
779 return ret;
780
781 /*
782 * In cases when not allowed to poll by using CMD13 or because we aren't
783 * capable of polling by using mmc_wait_dat0, then rely on waiting the
784 * stated timeout to be sufficient.
785 */
786 if (ret == -ENOSYS && !send_status)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300787 mdelay(timeout_ms);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200788
789 /* Finally wait until the card is ready or indicates a failure
790 * to switch. It doesn't hurt to use CMD13 here even if send_status
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300791 * is false, because by now (after 'timeout_ms' ms) the bus should be
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200792 * reliable.
793 */
794 do {
795 ret = mmc_send_status(mmc, &status);
796
797 if (!ret && (status & MMC_STATUS_SWITCH_ERROR)) {
798 pr_debug("switch failed %d/%d/0x%x !\n", set, index,
799 value);
800 return -EIO;
Maxime Riparda9003dc2016-11-04 16:18:08 +0100801 }
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200802 if (!ret && (status & MMC_STATUS_RDY_FOR_DATA))
Marek Vasut68925502019-02-06 11:34:27 +0100803 return 0;
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200804 udelay(100);
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300805 } while (get_timer(start) < timeout_ms);
Marek Vasut68925502019-02-06 11:34:27 +0100806
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200807 return -ETIMEDOUT;
Andy Fleming272cc702008-10-30 16:41:01 -0500808}
809
Marek Vasut68925502019-02-06 11:34:27 +0100810int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
811{
812 return __mmc_switch(mmc, set, index, value, true);
813}
814
Marek Vasut62d77ce2018-04-15 00:37:11 +0200815#if !CONFIG_IS_ENABLED(MMC_TINY)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100816static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode,
817 bool hsdowngrade)
Andy Fleming272cc702008-10-30 16:41:01 -0500818{
Andy Fleming272cc702008-10-30 16:41:01 -0500819 int err;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200820 int speed_bits;
821
822 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
823
824 switch (mode) {
825 case MMC_HS:
826 case MMC_HS_52:
827 case MMC_DDR_52:
828 speed_bits = EXT_CSD_TIMING_HS;
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200829 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100830#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200831 case MMC_HS_200:
832 speed_bits = EXT_CSD_TIMING_HS200;
833 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100834#endif
Peng Fan3dd26262018-08-10 14:07:54 +0800835#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
836 case MMC_HS_400:
837 speed_bits = EXT_CSD_TIMING_HS400;
838 break;
839#endif
Peng Fan44acd492019-07-10 14:43:07 +0800840#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
841 case MMC_HS_400_ES:
842 speed_bits = EXT_CSD_TIMING_HS400;
843 break;
844#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200845 case MMC_LEGACY:
846 speed_bits = EXT_CSD_TIMING_LEGACY;
847 break;
848 default:
849 return -EINVAL;
850 }
Marek Vasut68925502019-02-06 11:34:27 +0100851
852 err = __mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
853 speed_bits, !hsdowngrade);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200854 if (err)
855 return err;
856
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100857#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
858 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
859 /*
860 * In case the eMMC is in HS200/HS400 mode and we are downgrading
861 * to HS mode, the card clock are still running much faster than
862 * the supported HS mode clock, so we can not reliably read out
863 * Extended CSD. Reconfigure the controller to run at HS mode.
864 */
865 if (hsdowngrade) {
866 mmc_select_mode(mmc, MMC_HS);
867 mmc_set_clock(mmc, mmc_mode2freq(mmc, MMC_HS), false);
868 }
869#endif
870
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200871 if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
872 /* Now check to see that it worked */
873 err = mmc_send_ext_csd(mmc, test_csd);
874 if (err)
875 return err;
876
877 /* No high-speed support */
878 if (!test_csd[EXT_CSD_HS_TIMING])
879 return -ENOTSUPP;
880 }
881
882 return 0;
883}
884
885static int mmc_get_capabilities(struct mmc *mmc)
886{
887 u8 *ext_csd = mmc->ext_csd;
888 char cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500889
Jean-Jacques Hiblot00e446f2017-11-30 17:43:56 +0100890 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -0500891
Thomas Choud52ebf12010-12-24 13:12:21 +0000892 if (mmc_host_is_spi(mmc))
893 return 0;
894
Andy Fleming272cc702008-10-30 16:41:01 -0500895 /* Only version 4 supports high-speed */
896 if (mmc->version < MMC_VERSION_4)
897 return 0;
898
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200899 if (!ext_csd) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100900 pr_err("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200901 return -ENOTSUPP;
902 }
903
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600904 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
905
Peng Fan3dd26262018-08-10 14:07:54 +0800906 cardtype = ext_csd[EXT_CSD_CARD_TYPE];
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +0200907 mmc->cardtype = cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500908
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100909#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200910 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
911 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
912 mmc->card_caps |= MMC_MODE_HS200;
913 }
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100914#endif
Peng Fan44acd492019-07-10 14:43:07 +0800915#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
916 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Peng Fan3dd26262018-08-10 14:07:54 +0800917 if (cardtype & (EXT_CSD_CARD_TYPE_HS400_1_2V |
918 EXT_CSD_CARD_TYPE_HS400_1_8V)) {
919 mmc->card_caps |= MMC_MODE_HS400;
920 }
921#endif
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900922 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200923 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900924 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200925 mmc->card_caps |= MMC_MODE_HS_52MHz;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900926 }
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200927 if (cardtype & EXT_CSD_CARD_TYPE_26)
928 mmc->card_caps |= MMC_MODE_HS;
Andy Fleming272cc702008-10-30 16:41:01 -0500929
Peng Fan44acd492019-07-10 14:43:07 +0800930#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
931 if (ext_csd[EXT_CSD_STROBE_SUPPORT] &&
932 (mmc->card_caps & MMC_MODE_HS400)) {
933 mmc->card_caps |= MMC_MODE_HS400_ES;
934 }
935#endif
936
Andy Fleming272cc702008-10-30 16:41:01 -0500937 return 0;
938}
Marek Vasut62d77ce2018-04-15 00:37:11 +0200939#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500940
Stephen Warrenf866a462013-06-11 15:14:01 -0600941static int mmc_set_capacity(struct mmc *mmc, int part_num)
942{
943 switch (part_num) {
944 case 0:
945 mmc->capacity = mmc->capacity_user;
946 break;
947 case 1:
948 case 2:
949 mmc->capacity = mmc->capacity_boot;
950 break;
951 case 3:
952 mmc->capacity = mmc->capacity_rpmb;
953 break;
954 case 4:
955 case 5:
956 case 6:
957 case 7:
958 mmc->capacity = mmc->capacity_gp[part_num - 4];
959 break;
960 default:
961 return -1;
962 }
963
Simon Glassc40fdca2016-05-01 13:52:35 -0600964 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -0600965
966 return 0;
967}
968
Simon Glass7dba0b92016-06-12 23:30:15 -0600969int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +0000970{
Stephen Warrenf866a462013-06-11 15:14:01 -0600971 int ret;
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +0200972 int retry = 3;
Lei Wenbc897b12011-05-02 16:26:26 +0000973
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +0200974 do {
975 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
976 EXT_CSD_PART_CONF,
977 (mmc->part_config & ~PART_ACCESS_MASK)
978 | (part_num & PART_ACCESS_MASK));
979 } while (ret && retry--);
Stephen Warrenf866a462013-06-11 15:14:01 -0600980
Peter Bigot6dc93e72014-09-02 18:31:23 -0500981 /*
982 * Set the capacity if the switch succeeded or was intended
983 * to return to representing the raw device.
984 */
Stephen Warren873cc1d2015-12-07 11:38:49 -0700985 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -0500986 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -0600987 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700988 }
Peter Bigot6dc93e72014-09-02 18:31:23 -0500989
990 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000991}
992
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +0100993#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100994int mmc_hwpart_config(struct mmc *mmc,
995 const struct mmc_hwpart_conf *conf,
996 enum mmc_hwpart_conf_mode mode)
997{
998 u8 part_attrs = 0;
999 u32 enh_size_mult;
1000 u32 enh_start_addr;
1001 u32 gp_size_mult[4];
1002 u32 max_enh_size_mult;
1003 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001004 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001005 int i, pidx, err;
1006 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1007
1008 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
1009 return -EINVAL;
1010
1011 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001012 pr_err("eMMC >= 4.4 required for enhanced user data area\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001013 return -EMEDIUMTYPE;
1014 }
1015
1016 if (!(mmc->part_support & PART_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001017 pr_err("Card does not support partitioning\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001018 return -EMEDIUMTYPE;
1019 }
1020
1021 if (!mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001022 pr_err("Card does not define HC WP group size\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001023 return -EMEDIUMTYPE;
1024 }
1025
1026 /* check partition alignment and total enhanced size */
1027 if (conf->user.enh_size) {
1028 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
1029 conf->user.enh_start % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001030 pr_err("User data enhanced area not HC WP group "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001031 "size aligned\n");
1032 return -EINVAL;
1033 }
1034 part_attrs |= EXT_CSD_ENH_USR;
1035 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
1036 if (mmc->high_capacity) {
1037 enh_start_addr = conf->user.enh_start;
1038 } else {
1039 enh_start_addr = (conf->user.enh_start << 9);
1040 }
1041 } else {
1042 enh_size_mult = 0;
1043 enh_start_addr = 0;
1044 }
1045 tot_enh_size_mult += enh_size_mult;
1046
1047 for (pidx = 0; pidx < 4; pidx++) {
1048 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001049 pr_err("GP%i partition not HC WP group size "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001050 "aligned\n", pidx+1);
1051 return -EINVAL;
1052 }
1053 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
1054 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
1055 part_attrs |= EXT_CSD_ENH_GP(pidx);
1056 tot_enh_size_mult += gp_size_mult[pidx];
1057 }
1058 }
1059
1060 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001061 pr_err("Card does not support enhanced attribute\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001062 return -EMEDIUMTYPE;
1063 }
1064
1065 err = mmc_send_ext_csd(mmc, ext_csd);
1066 if (err)
1067 return err;
1068
1069 max_enh_size_mult =
1070 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
1071 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
1072 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
1073 if (tot_enh_size_mult > max_enh_size_mult) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001074 pr_err("Total enhanced size exceeds maximum (%u > %u)\n",
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001075 tot_enh_size_mult, max_enh_size_mult);
1076 return -EMEDIUMTYPE;
1077 }
1078
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001079 /* The default value of EXT_CSD_WR_REL_SET is device
1080 * dependent, the values can only be changed if the
1081 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
1082 * changed only once and before partitioning is completed. */
1083 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1084 if (conf->user.wr_rel_change) {
1085 if (conf->user.wr_rel_set)
1086 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
1087 else
1088 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
1089 }
1090 for (pidx = 0; pidx < 4; pidx++) {
1091 if (conf->gp_part[pidx].wr_rel_change) {
1092 if (conf->gp_part[pidx].wr_rel_set)
1093 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
1094 else
1095 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
1096 }
1097 }
1098
1099 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
1100 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
1101 puts("Card does not support host controlled partition write "
1102 "reliability settings\n");
1103 return -EMEDIUMTYPE;
1104 }
1105
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001106 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
1107 EXT_CSD_PARTITION_SETTING_COMPLETED) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001108 pr_err("Card already partitioned\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001109 return -EPERM;
1110 }
1111
1112 if (mode == MMC_HWPART_CONF_CHECK)
1113 return 0;
1114
1115 /* Partitioning requires high-capacity size definitions */
1116 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
1117 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1118 EXT_CSD_ERASE_GROUP_DEF, 1);
1119
1120 if (err)
1121 return err;
1122
1123 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1124
Jaehoon Chung4af66592020-01-17 15:06:54 +09001125#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001126 /* update erase group size to be high-capacity */
1127 mmc->erase_grp_size =
1128 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jaehoon Chung4af66592020-01-17 15:06:54 +09001129#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001130
1131 }
1132
1133 /* all OK, write the configuration */
1134 for (i = 0; i < 4; i++) {
1135 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1136 EXT_CSD_ENH_START_ADDR+i,
1137 (enh_start_addr >> (i*8)) & 0xFF);
1138 if (err)
1139 return err;
1140 }
1141 for (i = 0; i < 3; i++) {
1142 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1143 EXT_CSD_ENH_SIZE_MULT+i,
1144 (enh_size_mult >> (i*8)) & 0xFF);
1145 if (err)
1146 return err;
1147 }
1148 for (pidx = 0; pidx < 4; pidx++) {
1149 for (i = 0; i < 3; i++) {
1150 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1151 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1152 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1153 if (err)
1154 return err;
1155 }
1156 }
1157 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1158 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1159 if (err)
1160 return err;
1161
1162 if (mode == MMC_HWPART_CONF_SET)
1163 return 0;
1164
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001165 /* The WR_REL_SET is a write-once register but shall be
1166 * written before setting PART_SETTING_COMPLETED. As it is
1167 * write-once we can only write it when completing the
1168 * partitioning. */
1169 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1170 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1171 EXT_CSD_WR_REL_SET, wr_rel_set);
1172 if (err)
1173 return err;
1174 }
1175
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001176 /* Setting PART_SETTING_COMPLETED confirms the partition
1177 * configuration but it only becomes effective after power
1178 * cycle, so we do not adjust the partition related settings
1179 * in the mmc struct. */
1180
1181 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1182 EXT_CSD_PARTITION_SETTING,
1183 EXT_CSD_PARTITION_SETTING_COMPLETED);
1184 if (err)
1185 return err;
1186
1187 return 0;
1188}
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001189#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001190
Simon Glasse7881d82017-07-29 11:35:31 -06001191#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +00001192int mmc_getcd(struct mmc *mmc)
1193{
1194 int cd;
1195
1196 cd = board_mmc_getcd(mmc);
1197
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001198 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001199 if (mmc->cfg->ops->getcd)
1200 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001201 else
1202 cd = 1;
1203 }
Thierry Reding48972d92012-01-02 01:15:37 +00001204
1205 return cd;
1206}
Simon Glass8ca51e52016-06-12 23:30:22 -06001207#endif
Thierry Reding48972d92012-01-02 01:15:37 +00001208
Marek Vasut62d77ce2018-04-15 00:37:11 +02001209#if !CONFIG_IS_ENABLED(MMC_TINY)
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001210static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -05001211{
1212 struct mmc_cmd cmd;
1213 struct mmc_data data;
1214
1215 /* Switch the frequency */
1216 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1217 cmd.resp_type = MMC_RSP_R1;
1218 cmd.cmdarg = (mode << 31) | 0xffffff;
1219 cmd.cmdarg &= ~(0xf << (group * 4));
1220 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -05001221
1222 data.dest = (char *)resp;
1223 data.blocksize = 64;
1224 data.blocks = 1;
1225 data.flags = MMC_DATA_READ;
1226
1227 return mmc_send_cmd(mmc, &cmd, &data);
1228}
1229
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001230static int sd_get_capabilities(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001231{
1232 int err;
1233 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +05301234 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1235 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -05001236 struct mmc_data data;
1237 int timeout;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001238#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001239 u32 sd3_bus_mode;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001240#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001241
Jean-Jacques Hiblot00e446f2017-11-30 17:43:56 +01001242 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(SD_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05001243
Thomas Choud52ebf12010-12-24 13:12:21 +00001244 if (mmc_host_is_spi(mmc))
1245 return 0;
1246
Andy Fleming272cc702008-10-30 16:41:01 -05001247 /* Read the SCR to find out if this card supports higher speeds */
1248 cmd.cmdidx = MMC_CMD_APP_CMD;
1249 cmd.resp_type = MMC_RSP_R1;
1250 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001251
1252 err = mmc_send_cmd(mmc, &cmd, NULL);
1253
1254 if (err)
1255 return err;
1256
1257 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1258 cmd.resp_type = MMC_RSP_R1;
1259 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001260
1261 timeout = 3;
1262
1263retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +00001264 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -05001265 data.blocksize = 8;
1266 data.blocks = 1;
1267 data.flags = MMC_DATA_READ;
1268
1269 err = mmc_send_cmd(mmc, &cmd, &data);
1270
1271 if (err) {
1272 if (timeout--)
1273 goto retry_scr;
1274
1275 return err;
1276 }
1277
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001278 mmc->scr[0] = __be32_to_cpu(scr[0]);
1279 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -05001280
1281 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -07001282 case 0:
1283 mmc->version = SD_VERSION_1_0;
1284 break;
1285 case 1:
1286 mmc->version = SD_VERSION_1_10;
1287 break;
1288 case 2:
1289 mmc->version = SD_VERSION_2;
1290 if ((mmc->scr[0] >> 15) & 0x1)
1291 mmc->version = SD_VERSION_3;
1292 break;
1293 default:
1294 mmc->version = SD_VERSION_1_0;
1295 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001296 }
1297
Alagu Sankarb44c7082010-05-12 15:08:24 +05301298 if (mmc->scr[0] & SD_DATA_4BIT)
1299 mmc->card_caps |= MMC_MODE_4BIT;
1300
Andy Fleming272cc702008-10-30 16:41:01 -05001301 /* Version 1.0 doesn't support switching */
1302 if (mmc->version == SD_VERSION_1_0)
1303 return 0;
1304
1305 timeout = 4;
1306 while (timeout--) {
1307 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +00001308 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001309
1310 if (err)
1311 return err;
1312
1313 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001314 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -05001315 break;
1316 }
1317
Andy Fleming272cc702008-10-30 16:41:01 -05001318 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001319 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1320 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Fleming272cc702008-10-30 16:41:01 -05001321
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001322#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001323 /* Version before 3.0 don't support UHS modes */
1324 if (mmc->version < SD_VERSION_3)
1325 return 0;
1326
1327 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1328 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1329 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1330 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1331 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1332 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1333 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1334 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1335 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1336 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1337 mmc->card_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001338#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001339
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001340 return 0;
1341}
1342
1343static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1344{
1345 int err;
1346
1347 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001348 int speed;
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001349
Marek Vasutcf345762018-11-18 03:25:08 +01001350 /* SD version 1.00 and 1.01 does not support CMD 6 */
1351 if (mmc->version == SD_VERSION_1_0)
1352 return 0;
1353
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001354 switch (mode) {
1355 case SD_LEGACY:
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001356 speed = UHS_SDR12_BUS_SPEED;
1357 break;
1358 case SD_HS:
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001359 speed = HIGH_SPEED_BUS_SPEED;
1360 break;
1361#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1362 case UHS_SDR12:
1363 speed = UHS_SDR12_BUS_SPEED;
1364 break;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001365 case UHS_SDR25:
1366 speed = UHS_SDR25_BUS_SPEED;
1367 break;
1368 case UHS_SDR50:
1369 speed = UHS_SDR50_BUS_SPEED;
1370 break;
1371 case UHS_DDR50:
1372 speed = UHS_DDR50_BUS_SPEED;
1373 break;
1374 case UHS_SDR104:
1375 speed = UHS_SDR104_BUS_SPEED;
1376 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001377#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001378 default:
1379 return -EINVAL;
1380 }
1381
1382 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001383 if (err)
1384 return err;
1385
Jean-Jacques Hiblota0276f32018-02-09 12:09:27 +01001386 if (((__be32_to_cpu(switch_status[4]) >> 24) & 0xF) != speed)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001387 return -ENOTSUPP;
1388
1389 return 0;
1390}
1391
Marek Vasutec360e62018-04-15 00:36:45 +02001392static int sd_select_bus_width(struct mmc *mmc, int w)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001393{
1394 int err;
1395 struct mmc_cmd cmd;
1396
1397 if ((w != 4) && (w != 1))
1398 return -EINVAL;
1399
1400 cmd.cmdidx = MMC_CMD_APP_CMD;
1401 cmd.resp_type = MMC_RSP_R1;
1402 cmd.cmdarg = mmc->rca << 16;
1403
1404 err = mmc_send_cmd(mmc, &cmd, NULL);
1405 if (err)
1406 return err;
1407
1408 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1409 cmd.resp_type = MMC_RSP_R1;
1410 if (w == 4)
1411 cmd.cmdarg = 2;
1412 else if (w == 1)
1413 cmd.cmdarg = 0;
1414 err = mmc_send_cmd(mmc, &cmd, NULL);
1415 if (err)
1416 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001417
1418 return 0;
1419}
Marek Vasut62d77ce2018-04-15 00:37:11 +02001420#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001421
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001422#if CONFIG_IS_ENABLED(MMC_WRITE)
Peng Fan3697e592016-09-01 11:13:38 +08001423static int sd_read_ssr(struct mmc *mmc)
1424{
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001425 static const unsigned int sd_au_size[] = {
1426 0, SZ_16K / 512, SZ_32K / 512,
1427 SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
1428 SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
1429 SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
1430 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512,
1431 SZ_64M / 512,
1432 };
Peng Fan3697e592016-09-01 11:13:38 +08001433 int err, i;
1434 struct mmc_cmd cmd;
1435 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1436 struct mmc_data data;
1437 int timeout = 3;
1438 unsigned int au, eo, et, es;
1439
1440 cmd.cmdidx = MMC_CMD_APP_CMD;
1441 cmd.resp_type = MMC_RSP_R1;
1442 cmd.cmdarg = mmc->rca << 16;
1443
1444 err = mmc_send_cmd(mmc, &cmd, NULL);
Joel Johnsond4a5fa32020-01-11 09:08:14 -07001445#ifdef CONFIG_MMC_QUIRKS
1446 if (err && (mmc->quirks & MMC_QUIRK_RETRY_APP_CMD)) {
1447 int retries = 4;
1448 /*
1449 * It has been seen that APP_CMD may fail on the first
1450 * attempt, let's try a few more times
1451 */
1452 do {
1453 err = mmc_send_cmd(mmc, &cmd, NULL);
1454 if (!err)
1455 break;
1456 } while (retries--);
1457 }
1458#endif
Peng Fan3697e592016-09-01 11:13:38 +08001459 if (err)
1460 return err;
1461
1462 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1463 cmd.resp_type = MMC_RSP_R1;
1464 cmd.cmdarg = 0;
1465
1466retry_ssr:
1467 data.dest = (char *)ssr;
1468 data.blocksize = 64;
1469 data.blocks = 1;
1470 data.flags = MMC_DATA_READ;
1471
1472 err = mmc_send_cmd(mmc, &cmd, &data);
1473 if (err) {
1474 if (timeout--)
1475 goto retry_ssr;
1476
1477 return err;
1478 }
1479
1480 for (i = 0; i < 16; i++)
1481 ssr[i] = be32_to_cpu(ssr[i]);
1482
1483 au = (ssr[2] >> 12) & 0xF;
1484 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1485 mmc->ssr.au = sd_au_size[au];
1486 es = (ssr[3] >> 24) & 0xFF;
1487 es |= (ssr[2] & 0xFF) << 8;
1488 et = (ssr[3] >> 18) & 0x3F;
1489 if (es && et) {
1490 eo = (ssr[3] >> 16) & 0x3;
1491 mmc->ssr.erase_timeout = (et * 1000) / es;
1492 mmc->ssr.erase_offset = eo * 1000;
1493 }
1494 } else {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001495 pr_debug("Invalid Allocation Unit Size.\n");
Peng Fan3697e592016-09-01 11:13:38 +08001496 }
1497
1498 return 0;
1499}
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001500#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001501/* frequency bases */
1502/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001503static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001504 10000,
1505 100000,
1506 1000000,
1507 10000000,
1508};
1509
1510/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1511 * to platforms without floating point.
1512 */
Simon Glass61fe0762016-05-14 14:02:57 -06001513static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001514 0, /* reserved */
1515 10,
1516 12,
1517 13,
1518 15,
1519 20,
1520 25,
1521 30,
1522 35,
1523 40,
1524 45,
1525 50,
1526 55,
1527 60,
1528 70,
1529 80,
1530};
1531
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001532static inline int bus_width(uint cap)
1533{
1534 if (cap == MMC_MODE_8BIT)
1535 return 8;
1536 if (cap == MMC_MODE_4BIT)
1537 return 4;
1538 if (cap == MMC_MODE_1BIT)
1539 return 1;
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001540 pr_warn("invalid bus witdh capability 0x%x\n", cap);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001541 return 0;
1542}
1543
Simon Glasse7881d82017-07-29 11:35:31 -06001544#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001545#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001546static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1547{
1548 return -ENOTSUPP;
1549}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001550#endif
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001551
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001552static int mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001553{
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001554 int ret = 0;
1555
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001556 if (mmc->cfg->ops->set_ios)
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001557 ret = mmc->cfg->ops->set_ios(mmc);
1558
1559 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001560}
Yann Gautier3602a562019-09-19 17:56:12 +02001561
1562static int mmc_host_power_cycle(struct mmc *mmc)
1563{
1564 int ret = 0;
1565
1566 if (mmc->cfg->ops->host_power_cycle)
1567 ret = mmc->cfg->ops->host_power_cycle(mmc);
1568
1569 return ret;
1570}
Simon Glass8ca51e52016-06-12 23:30:22 -06001571#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001572
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001573int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
Andy Fleming272cc702008-10-30 16:41:01 -05001574{
Jaehoon Chungc0fafe62018-01-23 14:04:30 +09001575 if (!disable) {
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001576 if (clock > mmc->cfg->f_max)
1577 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001578
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001579 if (clock < mmc->cfg->f_min)
1580 clock = mmc->cfg->f_min;
1581 }
Andy Fleming272cc702008-10-30 16:41:01 -05001582
1583 mmc->clock = clock;
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001584 mmc->clk_disable = disable;
Andy Fleming272cc702008-10-30 16:41:01 -05001585
Jaehoon Chungd2faadb2018-01-26 19:25:30 +09001586 debug("clock is %s (%dHz)\n", disable ? "disabled" : "enabled", clock);
1587
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001588 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001589}
1590
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001591static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001592{
1593 mmc->bus_width = width;
1594
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001595 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001596}
1597
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001598#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1599/*
1600 * helper function to display the capabilities in a human
1601 * friendly manner. The capabilities include bus width and
1602 * supported modes.
1603 */
1604void mmc_dump_capabilities(const char *text, uint caps)
1605{
1606 enum bus_mode mode;
1607
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001608 pr_debug("%s: widths [", text);
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001609 if (caps & MMC_MODE_8BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001610 pr_debug("8, ");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001611 if (caps & MMC_MODE_4BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001612 pr_debug("4, ");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001613 if (caps & MMC_MODE_1BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001614 pr_debug("1, ");
1615 pr_debug("\b\b] modes [");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001616 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1617 if (MMC_CAP(mode) & caps)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001618 pr_debug("%s, ", mmc_mode_name(mode));
1619 pr_debug("\b\b]\n");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001620}
1621#endif
1622
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001623struct mode_width_tuning {
1624 enum bus_mode mode;
1625 uint widths;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001626#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001627 uint tuning;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001628#endif
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001629};
1630
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001631#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001632int mmc_voltage_to_mv(enum mmc_voltage voltage)
1633{
1634 switch (voltage) {
1635 case MMC_SIGNAL_VOLTAGE_000: return 0;
1636 case MMC_SIGNAL_VOLTAGE_330: return 3300;
1637 case MMC_SIGNAL_VOLTAGE_180: return 1800;
1638 case MMC_SIGNAL_VOLTAGE_120: return 1200;
1639 }
1640 return -EINVAL;
1641}
1642
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001643static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1644{
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001645 int err;
1646
1647 if (mmc->signal_voltage == signal_voltage)
1648 return 0;
1649
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001650 mmc->signal_voltage = signal_voltage;
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001651 err = mmc_set_ios(mmc);
1652 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001653 pr_debug("unable to set voltage (err %d)\n", err);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001654
1655 return err;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001656}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001657#else
1658static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1659{
1660 return 0;
1661}
1662#endif
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001663
Marek Vasut62d77ce2018-04-15 00:37:11 +02001664#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001665static const struct mode_width_tuning sd_modes_by_pref[] = {
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001666#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1667#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001668 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001669 .mode = UHS_SDR104,
1670 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1671 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1672 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001673#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001674 {
1675 .mode = UHS_SDR50,
1676 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1677 },
1678 {
1679 .mode = UHS_DDR50,
1680 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1681 },
1682 {
1683 .mode = UHS_SDR25,
1684 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1685 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001686#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001687 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001688 .mode = SD_HS,
1689 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1690 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001691#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001692 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001693 .mode = UHS_SDR12,
1694 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1695 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001696#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001697 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001698 .mode = SD_LEGACY,
1699 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1700 }
1701};
1702
1703#define for_each_sd_mode_by_pref(caps, mwt) \
1704 for (mwt = sd_modes_by_pref;\
1705 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1706 mwt++) \
1707 if (caps & MMC_CAP(mwt->mode))
1708
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001709static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001710{
1711 int err;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001712 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1713 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001714#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001715 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001716#else
1717 bool uhs_en = false;
1718#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001719 uint caps;
1720
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001721#ifdef DEBUG
1722 mmc_dump_capabilities("sd card", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001723 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001724#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001725
Anup Patelf49ff792019-07-08 04:10:43 +00001726 if (mmc_host_is_spi(mmc)) {
1727 mmc_set_bus_width(mmc, 1);
1728 mmc_select_mode(mmc, SD_LEGACY);
1729 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
1730 return 0;
1731 }
1732
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001733 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001734 caps = card_caps & mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001735
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001736 if (!uhs_en)
1737 caps &= ~UHS_CAPS;
1738
1739 for_each_sd_mode_by_pref(caps, mwt) {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001740 uint *w;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001741
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001742 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001743 if (*w & caps & mwt->widths) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001744 pr_debug("trying mode %s width %d (at %d MHz)\n",
1745 mmc_mode_name(mwt->mode),
1746 bus_width(*w),
1747 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001748
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001749 /* configure the bus width (card + host) */
1750 err = sd_select_bus_width(mmc, bus_width(*w));
1751 if (err)
1752 goto error;
1753 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001754
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001755 /* configure the bus mode (card) */
1756 err = sd_set_card_speed(mmc, mwt->mode);
1757 if (err)
1758 goto error;
1759
1760 /* configure the bus mode (host) */
1761 mmc_select_mode(mmc, mwt->mode);
Jaehoon Chung65117182018-01-26 19:25:29 +09001762 mmc_set_clock(mmc, mmc->tran_speed,
1763 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001764
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001765#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001766 /* execute tuning if needed */
1767 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1768 err = mmc_execute_tuning(mmc,
1769 mwt->tuning);
1770 if (err) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001771 pr_debug("tuning failed\n");
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001772 goto error;
1773 }
1774 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001775#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001776
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001777#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001778 err = sd_read_ssr(mmc);
Peng Fan0a4c2b02018-03-05 16:20:40 +08001779 if (err)
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001780 pr_warn("unable to read ssr\n");
1781#endif
1782 if (!err)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001783 return 0;
1784
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001785error:
1786 /* revert to a safer bus speed */
1787 mmc_select_mode(mmc, SD_LEGACY);
Jaehoon Chung65117182018-01-26 19:25:29 +09001788 mmc_set_clock(mmc, mmc->tran_speed,
1789 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001790 }
1791 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001792 }
1793
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001794 pr_err("unable to select a mode\n");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001795 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001796}
1797
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001798/*
1799 * read the compare the part of ext csd that is constant.
1800 * This can be used to check that the transfer is working
1801 * as expected.
1802 */
1803static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1804{
1805 int err;
1806 const u8 *ext_csd = mmc->ext_csd;
1807 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1808
Jean-Jacques Hiblot1de06b92017-11-30 17:43:58 +01001809 if (mmc->version < MMC_VERSION_4)
1810 return 0;
1811
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001812 err = mmc_send_ext_csd(mmc, test_csd);
1813 if (err)
1814 return err;
1815
1816 /* Only compare read only fields */
1817 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1818 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1819 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1820 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1821 ext_csd[EXT_CSD_REV]
1822 == test_csd[EXT_CSD_REV] &&
1823 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1824 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1825 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1826 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1827 return 0;
1828
1829 return -EBADMSG;
1830}
1831
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001832#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001833static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1834 uint32_t allowed_mask)
1835{
1836 u32 card_mask = 0;
1837
1838 switch (mode) {
Peng Fan44acd492019-07-10 14:43:07 +08001839 case MMC_HS_400_ES:
Peng Fan3dd26262018-08-10 14:07:54 +08001840 case MMC_HS_400:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001841 case MMC_HS_200:
Peng Fan3dd26262018-08-10 14:07:54 +08001842 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_8V |
1843 EXT_CSD_CARD_TYPE_HS400_1_8V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001844 card_mask |= MMC_SIGNAL_VOLTAGE_180;
Peng Fan3dd26262018-08-10 14:07:54 +08001845 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1846 EXT_CSD_CARD_TYPE_HS400_1_2V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001847 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1848 break;
1849 case MMC_DDR_52:
1850 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
1851 card_mask |= MMC_SIGNAL_VOLTAGE_330 |
1852 MMC_SIGNAL_VOLTAGE_180;
1853 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
1854 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1855 break;
1856 default:
1857 card_mask |= MMC_SIGNAL_VOLTAGE_330;
1858 break;
1859 }
1860
1861 while (card_mask & allowed_mask) {
1862 enum mmc_voltage best_match;
1863
1864 best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
1865 if (!mmc_set_signal_voltage(mmc, best_match))
1866 return 0;
1867
1868 allowed_mask &= ~best_match;
1869 }
1870
1871 return -ENOTSUPP;
1872}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001873#else
1874static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1875 uint32_t allowed_mask)
1876{
1877 return 0;
1878}
1879#endif
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001880
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001881static const struct mode_width_tuning mmc_modes_by_pref[] = {
Peng Fan44acd492019-07-10 14:43:07 +08001882#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1883 {
1884 .mode = MMC_HS_400_ES,
1885 .widths = MMC_MODE_8BIT,
1886 },
1887#endif
Peng Fan3dd26262018-08-10 14:07:54 +08001888#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1889 {
1890 .mode = MMC_HS_400,
1891 .widths = MMC_MODE_8BIT,
1892 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
1893 },
1894#endif
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001895#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001896 {
1897 .mode = MMC_HS_200,
1898 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001899 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001900 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001901#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001902 {
1903 .mode = MMC_DDR_52,
1904 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1905 },
1906 {
1907 .mode = MMC_HS_52,
1908 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1909 },
1910 {
1911 .mode = MMC_HS,
1912 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1913 },
1914 {
1915 .mode = MMC_LEGACY,
1916 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1917 }
1918};
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001919
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001920#define for_each_mmc_mode_by_pref(caps, mwt) \
1921 for (mwt = mmc_modes_by_pref;\
1922 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
1923 mwt++) \
1924 if (caps & MMC_CAP(mwt->mode))
1925
1926static const struct ext_csd_bus_width {
1927 uint cap;
1928 bool is_ddr;
1929 uint ext_csd_bits;
1930} ext_csd_bus_width[] = {
1931 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
1932 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
1933 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
1934 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
1935 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
1936};
1937
Peng Fan3dd26262018-08-10 14:07:54 +08001938#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1939static int mmc_select_hs400(struct mmc *mmc)
1940{
1941 int err;
1942
1943 /* Set timing to HS200 for tuning */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01001944 err = mmc_set_card_speed(mmc, MMC_HS_200, false);
Peng Fan3dd26262018-08-10 14:07:54 +08001945 if (err)
1946 return err;
1947
1948 /* configure the bus mode (host) */
1949 mmc_select_mode(mmc, MMC_HS_200);
1950 mmc_set_clock(mmc, mmc->tran_speed, false);
1951
1952 /* execute tuning if needed */
1953 err = mmc_execute_tuning(mmc, MMC_CMD_SEND_TUNING_BLOCK_HS200);
1954 if (err) {
1955 debug("tuning failed\n");
1956 return err;
1957 }
1958
1959 /* Set back to HS */
BOUGH CHEN5cf12032019-03-26 06:24:17 +00001960 mmc_set_card_speed(mmc, MMC_HS, true);
Peng Fan3dd26262018-08-10 14:07:54 +08001961
1962 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1963 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG);
1964 if (err)
1965 return err;
1966
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01001967 err = mmc_set_card_speed(mmc, MMC_HS_400, false);
Peng Fan3dd26262018-08-10 14:07:54 +08001968 if (err)
1969 return err;
1970
1971 mmc_select_mode(mmc, MMC_HS_400);
1972 err = mmc_set_clock(mmc, mmc->tran_speed, false);
1973 if (err)
1974 return err;
1975
1976 return 0;
1977}
1978#else
1979static int mmc_select_hs400(struct mmc *mmc)
1980{
1981 return -ENOTSUPP;
1982}
1983#endif
1984
Peng Fan44acd492019-07-10 14:43:07 +08001985#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1986#if !CONFIG_IS_ENABLED(DM_MMC)
1987static int mmc_set_enhanced_strobe(struct mmc *mmc)
1988{
1989 return -ENOTSUPP;
1990}
1991#endif
1992static int mmc_select_hs400es(struct mmc *mmc)
1993{
1994 int err;
1995
1996 err = mmc_set_card_speed(mmc, MMC_HS, true);
1997 if (err)
1998 return err;
1999
2000 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2001 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG |
2002 EXT_CSD_BUS_WIDTH_STROBE);
2003 if (err) {
2004 printf("switch to bus width for hs400 failed\n");
2005 return err;
2006 }
2007 /* TODO: driver strength */
2008 err = mmc_set_card_speed(mmc, MMC_HS_400_ES, false);
2009 if (err)
2010 return err;
2011
2012 mmc_select_mode(mmc, MMC_HS_400_ES);
2013 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2014 if (err)
2015 return err;
2016
2017 return mmc_set_enhanced_strobe(mmc);
2018}
2019#else
2020static int mmc_select_hs400es(struct mmc *mmc)
2021{
2022 return -ENOTSUPP;
2023}
2024#endif
2025
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002026#define for_each_supported_width(caps, ddr, ecbv) \
2027 for (ecbv = ext_csd_bus_width;\
2028 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
2029 ecbv++) \
2030 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
2031
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002032static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002033{
2034 int err;
2035 const struct mode_width_tuning *mwt;
2036 const struct ext_csd_bus_width *ecbw;
2037
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002038#ifdef DEBUG
2039 mmc_dump_capabilities("mmc", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002040 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002041#endif
2042
Anup Patelf49ff792019-07-08 04:10:43 +00002043 if (mmc_host_is_spi(mmc)) {
2044 mmc_set_bus_width(mmc, 1);
2045 mmc_select_mode(mmc, MMC_LEGACY);
2046 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
2047 return 0;
2048 }
2049
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002050 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002051 card_caps &= mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002052
2053 /* Only version 4 of MMC supports wider bus widths */
2054 if (mmc->version < MMC_VERSION_4)
2055 return 0;
2056
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002057 if (!mmc->ext_csd) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002058 pr_debug("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002059 return -ENOTSUPP;
2060 }
2061
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002062#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
2063 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2064 /*
2065 * In case the eMMC is in HS200/HS400 mode, downgrade to HS mode
2066 * before doing anything else, since a transition from either of
2067 * the HS200/HS400 mode directly to legacy mode is not supported.
2068 */
2069 if (mmc->selected_mode == MMC_HS_200 ||
2070 mmc->selected_mode == MMC_HS_400)
2071 mmc_set_card_speed(mmc, MMC_HS, true);
2072 else
2073#endif
2074 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002075
2076 for_each_mmc_mode_by_pref(card_caps, mwt) {
2077 for_each_supported_width(card_caps & mwt->widths,
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002078 mmc_is_mode_ddr(mwt->mode), ecbw) {
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002079 enum mmc_voltage old_voltage;
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002080 pr_debug("trying mode %s width %d (at %d MHz)\n",
2081 mmc_mode_name(mwt->mode),
2082 bus_width(ecbw->cap),
2083 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002084 old_voltage = mmc->signal_voltage;
2085 err = mmc_set_lowest_voltage(mmc, mwt->mode,
2086 MMC_ALL_SIGNAL_VOLTAGE);
2087 if (err)
2088 continue;
2089
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002090 /* configure the bus width (card + host) */
2091 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2092 EXT_CSD_BUS_WIDTH,
2093 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
2094 if (err)
2095 goto error;
2096 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
2097
Peng Fan3dd26262018-08-10 14:07:54 +08002098 if (mwt->mode == MMC_HS_400) {
2099 err = mmc_select_hs400(mmc);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002100 if (err) {
Peng Fan3dd26262018-08-10 14:07:54 +08002101 printf("Select HS400 failed %d\n", err);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002102 goto error;
2103 }
Peng Fan44acd492019-07-10 14:43:07 +08002104 } else if (mwt->mode == MMC_HS_400_ES) {
2105 err = mmc_select_hs400es(mmc);
2106 if (err) {
2107 printf("Select HS400ES failed %d\n",
2108 err);
2109 goto error;
2110 }
Peng Fan3dd26262018-08-10 14:07:54 +08002111 } else {
2112 /* configure the bus speed (card) */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002113 err = mmc_set_card_speed(mmc, mwt->mode, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002114 if (err)
2115 goto error;
2116
2117 /*
2118 * configure the bus width AND the ddr mode
2119 * (card). The host side will be taken care
2120 * of in the next step
2121 */
2122 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
2123 err = mmc_switch(mmc,
2124 EXT_CSD_CMD_SET_NORMAL,
2125 EXT_CSD_BUS_WIDTH,
2126 ecbw->ext_csd_bits);
2127 if (err)
2128 goto error;
2129 }
2130
2131 /* configure the bus mode (host) */
2132 mmc_select_mode(mmc, mwt->mode);
2133 mmc_set_clock(mmc, mmc->tran_speed,
2134 MMC_CLK_ENABLE);
2135#ifdef MMC_SUPPORTS_TUNING
2136
2137 /* execute tuning if needed */
2138 if (mwt->tuning) {
2139 err = mmc_execute_tuning(mmc,
2140 mwt->tuning);
2141 if (err) {
2142 pr_debug("tuning failed\n");
2143 goto error;
2144 }
2145 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01002146#endif
Peng Fan3dd26262018-08-10 14:07:54 +08002147 }
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002148
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002149 /* do a transfer to check the configuration */
2150 err = mmc_read_and_compare_ext_csd(mmc);
2151 if (!err)
2152 return 0;
2153error:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002154 mmc_set_signal_voltage(mmc, old_voltage);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002155 /* if an error occured, revert to a safer bus mode */
2156 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2157 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
2158 mmc_select_mode(mmc, MMC_LEGACY);
2159 mmc_set_bus_width(mmc, 1);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002160 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002161 }
2162
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002163 pr_err("unable to select a mode\n");
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002164
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002165 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002166}
Marek Vasut62d77ce2018-04-15 00:37:11 +02002167#endif
2168
2169#if CONFIG_IS_ENABLED(MMC_TINY)
2170DEFINE_CACHE_ALIGN_BUFFER(u8, ext_csd_bkup, MMC_MAX_BLOCK_LEN);
2171#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002172
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002173static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002174{
2175 int err, i;
2176 u64 capacity;
2177 bool has_parts = false;
2178 bool part_completed;
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002179 static const u32 mmc_versions[] = {
2180 MMC_VERSION_4,
2181 MMC_VERSION_4_1,
2182 MMC_VERSION_4_2,
2183 MMC_VERSION_4_3,
Jean-Jacques Hiblotace1bed2018-02-09 12:09:28 +01002184 MMC_VERSION_4_4,
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002185 MMC_VERSION_4_41,
2186 MMC_VERSION_4_5,
2187 MMC_VERSION_5_0,
2188 MMC_VERSION_5_1
2189 };
2190
Marek Vasut62d77ce2018-04-15 00:37:11 +02002191#if CONFIG_IS_ENABLED(MMC_TINY)
2192 u8 *ext_csd = ext_csd_bkup;
2193
2194 if (IS_SD(mmc) || mmc->version < MMC_VERSION_4)
2195 return 0;
2196
2197 if (!mmc->ext_csd)
2198 memset(ext_csd_bkup, 0, sizeof(ext_csd_bkup));
2199
2200 err = mmc_send_ext_csd(mmc, ext_csd);
2201 if (err)
2202 goto error;
2203
2204 /* store the ext csd for future reference */
2205 if (!mmc->ext_csd)
2206 mmc->ext_csd = ext_csd;
2207#else
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002208 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002209
2210 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
2211 return 0;
2212
2213 /* check ext_csd version and capacity */
2214 err = mmc_send_ext_csd(mmc, ext_csd);
2215 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002216 goto error;
2217
2218 /* store the ext csd for future reference */
2219 if (!mmc->ext_csd)
2220 mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN);
2221 if (!mmc->ext_csd)
2222 return -ENOMEM;
2223 memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002224#endif
Alexander Kochetkov76584e32018-02-20 14:35:55 +03002225 if (ext_csd[EXT_CSD_REV] >= ARRAY_SIZE(mmc_versions))
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002226 return -EINVAL;
2227
2228 mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]];
2229
2230 if (mmc->version >= MMC_VERSION_4_2) {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002231 /*
2232 * According to the JEDEC Standard, the value of
2233 * ext_csd's capacity is valid if the value is more
2234 * than 2GB
2235 */
2236 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
2237 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
2238 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
2239 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2240 capacity *= MMC_MAX_BLOCK_LEN;
2241 if ((capacity >> 20) > 2 * 1024)
2242 mmc->capacity_user = capacity;
2243 }
2244
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +02002245 if (mmc->version >= MMC_VERSION_4_5)
2246 mmc->gen_cmd6_time = ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
2247
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002248 /* The partition data may be non-zero but it is only
2249 * effective if PARTITION_SETTING_COMPLETED is set in
2250 * EXT_CSD, so ignore any data if this bit is not set,
2251 * except for enabling the high-capacity group size
2252 * definition (see below).
2253 */
2254 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
2255 EXT_CSD_PARTITION_SETTING_COMPLETED);
2256
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +02002257 mmc->part_switch_time = ext_csd[EXT_CSD_PART_SWITCH_TIME];
2258 /* Some eMMC set the value too low so set a minimum */
2259 if (mmc->part_switch_time < MMC_MIN_PART_SWITCH_TIME && mmc->part_switch_time)
2260 mmc->part_switch_time = MMC_MIN_PART_SWITCH_TIME;
2261
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002262 /* store the partition info of emmc */
2263 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
2264 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
2265 ext_csd[EXT_CSD_BOOT_MULT])
2266 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
2267 if (part_completed &&
2268 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
2269 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
2270
2271 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
2272
2273 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
2274
2275 for (i = 0; i < 4; i++) {
2276 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
2277 uint mult = (ext_csd[idx + 2] << 16) +
2278 (ext_csd[idx + 1] << 8) + ext_csd[idx];
2279 if (mult)
2280 has_parts = true;
2281 if (!part_completed)
2282 continue;
2283 mmc->capacity_gp[i] = mult;
2284 mmc->capacity_gp[i] *=
2285 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2286 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2287 mmc->capacity_gp[i] <<= 19;
2288 }
2289
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002290#ifndef CONFIG_SPL_BUILD
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002291 if (part_completed) {
2292 mmc->enh_user_size =
2293 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
2294 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
2295 ext_csd[EXT_CSD_ENH_SIZE_MULT];
2296 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2297 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2298 mmc->enh_user_size <<= 19;
2299 mmc->enh_user_start =
2300 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
2301 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
2302 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
2303 ext_csd[EXT_CSD_ENH_START_ADDR];
2304 if (mmc->high_capacity)
2305 mmc->enh_user_start <<= 9;
2306 }
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002307#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002308
2309 /*
2310 * Host needs to enable ERASE_GRP_DEF bit if device is
2311 * partitioned. This bit will be lost every time after a reset
2312 * or power off. This will affect erase size.
2313 */
2314 if (part_completed)
2315 has_parts = true;
2316 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
2317 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
2318 has_parts = true;
2319 if (has_parts) {
2320 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2321 EXT_CSD_ERASE_GROUP_DEF, 1);
2322
2323 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002324 goto error;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002325
2326 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
2327 }
2328
2329 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002330#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002331 /* Read out group size from ext_csd */
2332 mmc->erase_grp_size =
2333 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002334#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002335 /*
2336 * if high capacity and partition setting completed
2337 * SEC_COUNT is valid even if it is smaller than 2 GiB
2338 * JEDEC Standard JESD84-B45, 6.2.4
2339 */
2340 if (mmc->high_capacity && part_completed) {
2341 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
2342 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
2343 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
2344 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
2345 capacity *= MMC_MAX_BLOCK_LEN;
2346 mmc->capacity_user = capacity;
2347 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002348 }
2349#if CONFIG_IS_ENABLED(MMC_WRITE)
2350 else {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002351 /* Calculate the group size from the csd value. */
2352 int erase_gsz, erase_gmul;
2353
2354 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
2355 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
2356 mmc->erase_grp_size = (erase_gsz + 1)
2357 * (erase_gmul + 1);
2358 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002359#endif
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002360#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002361 mmc->hc_wp_grp_size = 1024
2362 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
2363 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002364#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002365
2366 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
2367
2368 return 0;
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002369error:
2370 if (mmc->ext_csd) {
Marek Vasut62d77ce2018-04-15 00:37:11 +02002371#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002372 free(mmc->ext_csd);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002373#endif
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002374 mmc->ext_csd = NULL;
2375 }
2376 return err;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002377}
2378
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002379static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002380{
Stephen Warrenf866a462013-06-11 15:14:01 -06002381 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05002382 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002383 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05002384 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06002385 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05002386
Thomas Choud52ebf12010-12-24 13:12:21 +00002387#ifdef CONFIG_MMC_SPI_CRC_ON
2388 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
2389 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
2390 cmd.resp_type = MMC_RSP_R1;
2391 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002392 err = mmc_send_cmd(mmc, &cmd, NULL);
Thomas Choud52ebf12010-12-24 13:12:21 +00002393 if (err)
2394 return err;
2395 }
2396#endif
2397
Andy Fleming272cc702008-10-30 16:41:01 -05002398 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002399 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
2400 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05002401 cmd.resp_type = MMC_RSP_R2;
2402 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002403
2404 err = mmc_send_cmd(mmc, &cmd, NULL);
2405
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002406#ifdef CONFIG_MMC_QUIRKS
2407 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SEND_CID)) {
2408 int retries = 4;
2409 /*
2410 * It has been seen that SEND_CID may fail on the first
2411 * attempt, let's try a few more time
2412 */
2413 do {
2414 err = mmc_send_cmd(mmc, &cmd, NULL);
2415 if (!err)
2416 break;
2417 } while (retries--);
2418 }
2419#endif
2420
Andy Fleming272cc702008-10-30 16:41:01 -05002421 if (err)
2422 return err;
2423
2424 memcpy(mmc->cid, cmd.response, 16);
2425
2426 /*
2427 * For MMC cards, set the Relative Address.
2428 * For SD cards, get the Relatvie Address.
2429 * This also puts the cards into Standby State
2430 */
Thomas Choud52ebf12010-12-24 13:12:21 +00002431 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2432 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
2433 cmd.cmdarg = mmc->rca << 16;
2434 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05002435
Thomas Choud52ebf12010-12-24 13:12:21 +00002436 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002437
Thomas Choud52ebf12010-12-24 13:12:21 +00002438 if (err)
2439 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002440
Thomas Choud52ebf12010-12-24 13:12:21 +00002441 if (IS_SD(mmc))
2442 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
2443 }
Andy Fleming272cc702008-10-30 16:41:01 -05002444
2445 /* Get the Card-Specific Data */
2446 cmd.cmdidx = MMC_CMD_SEND_CSD;
2447 cmd.resp_type = MMC_RSP_R2;
2448 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05002449
2450 err = mmc_send_cmd(mmc, &cmd, NULL);
2451
2452 if (err)
2453 return err;
2454
Rabin Vincent998be3d2009-04-05 13:30:56 +05302455 mmc->csd[0] = cmd.response[0];
2456 mmc->csd[1] = cmd.response[1];
2457 mmc->csd[2] = cmd.response[2];
2458 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05002459
2460 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302461 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05002462
2463 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07002464 case 0:
2465 mmc->version = MMC_VERSION_1_2;
2466 break;
2467 case 1:
2468 mmc->version = MMC_VERSION_1_4;
2469 break;
2470 case 2:
2471 mmc->version = MMC_VERSION_2_2;
2472 break;
2473 case 3:
2474 mmc->version = MMC_VERSION_3;
2475 break;
2476 case 4:
2477 mmc->version = MMC_VERSION_4;
2478 break;
2479 default:
2480 mmc->version = MMC_VERSION_1_2;
2481 break;
Andy Fleming272cc702008-10-30 16:41:01 -05002482 }
2483 }
2484
2485 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302486 freq = fbase[(cmd.response[0] & 0x7)];
2487 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05002488
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002489 mmc->legacy_speed = freq * mult;
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002490 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05002491
Markus Niebelab711882013-12-16 13:40:46 +01002492 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05302493 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002494#if CONFIG_IS_ENABLED(MMC_WRITE)
Andy Fleming272cc702008-10-30 16:41:01 -05002495
2496 if (IS_SD(mmc))
2497 mmc->write_bl_len = mmc->read_bl_len;
2498 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05302499 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002500#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002501
2502 if (mmc->high_capacity) {
2503 csize = (mmc->csd[1] & 0x3f) << 16
2504 | (mmc->csd[2] & 0xffff0000) >> 16;
2505 cmult = 8;
2506 } else {
2507 csize = (mmc->csd[1] & 0x3ff) << 2
2508 | (mmc->csd[2] & 0xc0000000) >> 30;
2509 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2510 }
2511
Stephen Warrenf866a462013-06-11 15:14:01 -06002512 mmc->capacity_user = (csize + 1) << (cmult + 2);
2513 mmc->capacity_user *= mmc->read_bl_len;
2514 mmc->capacity_boot = 0;
2515 mmc->capacity_rpmb = 0;
2516 for (i = 0; i < 4; i++)
2517 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002518
Simon Glass8bfa1952013-04-03 08:54:30 +00002519 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2520 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05002521
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002522#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass8bfa1952013-04-03 08:54:30 +00002523 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2524 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002525#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002526
Markus Niebelab711882013-12-16 13:40:46 +01002527 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2528 cmd.cmdidx = MMC_CMD_SET_DSR;
2529 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2530 cmd.resp_type = MMC_RSP_NONE;
2531 if (mmc_send_cmd(mmc, &cmd, NULL))
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002532 pr_warn("MMC: SET_DSR failed\n");
Markus Niebelab711882013-12-16 13:40:46 +01002533 }
2534
Andy Fleming272cc702008-10-30 16:41:01 -05002535 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002536 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2537 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00002538 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002539 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00002540 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002541
Thomas Choud52ebf12010-12-24 13:12:21 +00002542 if (err)
2543 return err;
2544 }
Andy Fleming272cc702008-10-30 16:41:01 -05002545
Lei Wene6f99a52011-06-22 17:03:31 +00002546 /*
2547 * For SD, its erase group is always one sector
2548 */
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002549#if CONFIG_IS_ENABLED(MMC_WRITE)
Lei Wene6f99a52011-06-22 17:03:31 +00002550 mmc->erase_grp_size = 1;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002551#endif
Lei Wenbc897b12011-05-02 16:26:26 +00002552 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00002553
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002554 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002555 if (err)
2556 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05302557
Simon Glassc40fdca2016-05-01 13:52:35 -06002558 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06002559 if (err)
2560 return err;
2561
Marek Vasut62d77ce2018-04-15 00:37:11 +02002562#if CONFIG_IS_ENABLED(MMC_TINY)
2563 mmc_set_clock(mmc, mmc->legacy_speed, false);
2564 mmc_select_mode(mmc, IS_SD(mmc) ? SD_LEGACY : MMC_LEGACY);
2565 mmc_set_bus_width(mmc, 1);
2566#else
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002567 if (IS_SD(mmc)) {
2568 err = sd_get_capabilities(mmc);
2569 if (err)
2570 return err;
2571 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2572 } else {
2573 err = mmc_get_capabilities(mmc);
2574 if (err)
2575 return err;
Masahiro Yamada8adf50e2020-01-23 14:31:12 +09002576 err = mmc_select_mode_and_width(mmc, mmc->card_caps);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002577 }
Marek Vasut62d77ce2018-04-15 00:37:11 +02002578#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002579 if (err)
2580 return err;
2581
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002582 mmc->best_mode = mmc->selected_mode;
Jaehoon Chungad5fd922012-03-26 21:16:03 +00002583
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002584 /* Fix the block length for DDR mode */
2585 if (mmc->ddr_mode) {
2586 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002587#if CONFIG_IS_ENABLED(MMC_WRITE)
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002588 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002589#endif
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002590 }
2591
Andy Fleming272cc702008-10-30 16:41:01 -05002592 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06002593 bdesc = mmc_get_blk_desc(mmc);
2594 bdesc->lun = 0;
2595 bdesc->hwpart = 0;
2596 bdesc->type = 0;
2597 bdesc->blksz = mmc->read_bl_len;
2598 bdesc->log2blksz = LOG2(bdesc->blksz);
2599 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01002600#if !defined(CONFIG_SPL_BUILD) || \
2601 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
Simon Glass27084c02019-09-25 08:56:27 -06002602 !CONFIG_IS_ENABLED(USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06002603 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00002604 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2605 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002606 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002607 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2608 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2609 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002610 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002611 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01002612#else
Simon Glassc40fdca2016-05-01 13:52:35 -06002613 bdesc->vendor[0] = 0;
2614 bdesc->product[0] = 0;
2615 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01002616#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002617
Andre Przywaraeef05fd2018-12-17 10:05:45 +00002618#if !defined(CONFIG_DM_MMC) && (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT))
2619 part_init(bdesc);
2620#endif
2621
Andy Fleming272cc702008-10-30 16:41:01 -05002622 return 0;
2623}
2624
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002625static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002626{
2627 struct mmc_cmd cmd;
2628 int err;
2629
2630 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2631 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002632 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05002633 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05002634
2635 err = mmc_send_cmd(mmc, &cmd, NULL);
2636
2637 if (err)
2638 return err;
2639
Rabin Vincent998be3d2009-04-05 13:30:56 +05302640 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002641 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002642 else
2643 mmc->version = SD_VERSION_2;
2644
2645 return 0;
2646}
2647
Simon Glassc4d660d2017-07-04 13:31:19 -06002648#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002649/* board-specific MMC power initializations. */
2650__weak void board_mmc_power_init(void)
2651{
2652}
Simon Glass05cbeb72017-04-22 19:10:56 -06002653#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002654
Peng Fan2051aef2016-10-11 15:08:43 +08002655static int mmc_power_init(struct mmc *mmc)
2656{
Simon Glassc4d660d2017-07-04 13:31:19 -06002657#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002658#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08002659 int ret;
2660
2661 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002662 &mmc->vmmc_supply);
2663 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002664 pr_debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002665
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002666 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2667 &mmc->vqmmc_supply);
2668 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002669 pr_debug("%s: No vqmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002670#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06002671#else /* !CONFIG_DM_MMC */
2672 /*
2673 * Driver model should use a regulator, as above, rather than calling
2674 * out to board code.
2675 */
2676 board_mmc_power_init();
2677#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002678 return 0;
2679}
2680
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002681/*
2682 * put the host in the initial state:
2683 * - turn on Vdd (card power supply)
2684 * - configure the bus width and clock to minimal values
2685 */
2686static void mmc_set_initial_state(struct mmc *mmc)
2687{
2688 int err;
2689
2690 /* First try to set 3.3V. If it fails set to 1.8V */
2691 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2692 if (err != 0)
2693 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2694 if (err != 0)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002695 pr_warn("mmc: failed to set signal voltage\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002696
2697 mmc_select_mode(mmc, MMC_LEGACY);
2698 mmc_set_bus_width(mmc, 1);
Jaehoon Chung65117182018-01-26 19:25:29 +09002699 mmc_set_clock(mmc, 0, MMC_CLK_ENABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002700}
2701
2702static int mmc_power_on(struct mmc *mmc)
2703{
2704#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2705 if (mmc->vmmc_supply) {
2706 int ret = regulator_set_enable(mmc->vmmc_supply, true);
2707
2708 if (ret) {
2709 puts("Error enabling VMMC supply\n");
2710 return ret;
2711 }
2712 }
2713#endif
2714 return 0;
2715}
2716
2717static int mmc_power_off(struct mmc *mmc)
2718{
Jaehoon Chung65117182018-01-26 19:25:29 +09002719 mmc_set_clock(mmc, 0, MMC_CLK_DISABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002720#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, false);
2723
2724 if (ret) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002725 pr_debug("Error disabling VMMC supply\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002726 return ret;
2727 }
2728 }
2729#endif
2730 return 0;
2731}
2732
2733static int mmc_power_cycle(struct mmc *mmc)
2734{
2735 int ret;
2736
2737 ret = mmc_power_off(mmc);
2738 if (ret)
2739 return ret;
Yann Gautier3602a562019-09-19 17:56:12 +02002740
2741 ret = mmc_host_power_cycle(mmc);
2742 if (ret)
2743 return ret;
2744
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002745 /*
2746 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2747 * to be on the safer side.
2748 */
2749 udelay(2000);
2750 return mmc_power_on(mmc);
2751}
2752
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002753int mmc_get_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002754{
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002755 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
Macpaul Linafd59322011-11-14 23:35:39 +00002756 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05002757
Lei Wenbc897b12011-05-02 16:26:26 +00002758 if (mmc->has_init)
2759 return 0;
2760
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08002761#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
2762 mmc_adapter_card_type_ident();
2763#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002764 err = mmc_power_init(mmc);
2765 if (err)
2766 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002767
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002768#ifdef CONFIG_MMC_QUIRKS
2769 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
Joel Johnsond4a5fa32020-01-11 09:08:14 -07002770 MMC_QUIRK_RETRY_SEND_CID |
2771 MMC_QUIRK_RETRY_APP_CMD;
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002772#endif
2773
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002774 err = mmc_power_cycle(mmc);
2775 if (err) {
2776 /*
2777 * if power cycling is not supported, we should not try
2778 * to use the UHS modes, because we wouldn't be able to
2779 * recover from an error during the UHS initialization.
2780 */
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002781 pr_debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002782 uhs_en = false;
2783 mmc->host_caps &= ~UHS_CAPS;
2784 err = mmc_power_on(mmc);
2785 }
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002786 if (err)
2787 return err;
2788
Simon Glasse7881d82017-07-29 11:35:31 -06002789#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06002790 /* The device has already been probed ready for use */
2791#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02002792 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002793 err = mmc->cfg->ops->init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002794 if (err)
2795 return err;
Simon Glass8ca51e52016-06-12 23:30:22 -06002796#endif
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06002797 mmc->ddr_mode = 0;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02002798
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002799retry:
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002800 mmc_set_initial_state(mmc);
Jean-Jacques Hiblot318a7a52017-09-21 16:30:01 +02002801
Andy Fleming272cc702008-10-30 16:41:01 -05002802 /* Reset the Card */
2803 err = mmc_go_idle(mmc);
2804
2805 if (err)
2806 return err;
2807
Lei Wenbc897b12011-05-02 16:26:26 +00002808 /* The internal partition reset to user partition(0) at every CMD0*/
Simon Glassc40fdca2016-05-01 13:52:35 -06002809 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00002810
Andy Fleming272cc702008-10-30 16:41:01 -05002811 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00002812 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002813
Andy Fleming272cc702008-10-30 16:41:01 -05002814 /* Now try to get the SD card's operating condition */
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002815 err = sd_send_op_cond(mmc, uhs_en);
2816 if (err && uhs_en) {
2817 uhs_en = false;
2818 mmc_power_cycle(mmc);
2819 goto retry;
2820 }
Andy Fleming272cc702008-10-30 16:41:01 -05002821
2822 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002823 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05002824 err = mmc_send_op_cond(mmc);
2825
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002826 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01002827#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002828 pr_err("Card did not respond to voltage select!\n");
Paul Burton56196822013-09-04 16:12:25 +01002829#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002830 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002831 }
2832 }
2833
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002834 return err;
2835}
2836
2837int mmc_start_init(struct mmc *mmc)
2838{
2839 bool no_card;
2840 int err = 0;
2841
2842 /*
2843 * all hosts are capable of 1 bit bus-width and able to use the legacy
2844 * timings.
2845 */
2846 mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(SD_LEGACY) |
2847 MMC_CAP(MMC_LEGACY) | MMC_MODE_1BIT;
Faiz Abbas32860bd2020-02-26 13:44:30 +05302848#if CONFIG_IS_ENABLED(DM_MMC)
2849 mmc_deferred_probe(mmc);
2850#endif
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002851#if !defined(CONFIG_MMC_BROKEN_CD)
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002852 no_card = mmc_getcd(mmc) == 0;
2853#else
2854 no_card = 0;
2855#endif
2856#if !CONFIG_IS_ENABLED(DM_MMC)
Baruch Siachfea39392019-07-22 15:52:12 +03002857 /* we pretend there's no card when init is NULL */
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002858 no_card = no_card || (mmc->cfg->ops->init == NULL);
2859#endif
2860 if (no_card) {
2861 mmc->has_init = 0;
2862#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2863 pr_err("MMC: no card present\n");
2864#endif
2865 return -ENOMEDIUM;
2866 }
2867
2868 err = mmc_get_op_cond(mmc);
2869
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002870 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002871 mmc->init_in_progress = 1;
2872
2873 return err;
2874}
2875
2876static int mmc_complete_init(struct mmc *mmc)
2877{
2878 int err = 0;
2879
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002880 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002881 if (mmc->op_cond_pending)
2882 err = mmc_complete_op_cond(mmc);
2883
2884 if (!err)
2885 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00002886 if (err)
2887 mmc->has_init = 0;
2888 else
2889 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002890 return err;
2891}
2892
2893int mmc_init(struct mmc *mmc)
2894{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002895 int err = 0;
Vipul Kumar36332b62018-05-03 12:20:54 +05302896 __maybe_unused ulong start;
Simon Glassc4d660d2017-07-04 13:31:19 -06002897#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06002898 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00002899
Simon Glass33fb2112016-05-01 13:52:41 -06002900 upriv->mmc = mmc;
2901#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00002902 if (mmc->has_init)
2903 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02002904
2905 start = get_timer(0);
2906
Che-Liang Chioue9550442012-11-28 15:21:13 +00002907 if (!mmc->init_in_progress)
2908 err = mmc_start_init(mmc);
2909
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002910 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002911 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01002912 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002913 pr_info("%s: %d, time %lu\n", __func__, err, get_timer(start));
Jagan Teki919b4852017-01-10 11:18:43 +01002914
Lei Wenbc897b12011-05-02 16:26:26 +00002915 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002916}
2917
Marek Vasutfceea992019-01-29 04:45:51 +01002918#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
2919 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
2920 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2921int mmc_deinit(struct mmc *mmc)
2922{
2923 u32 caps_filtered;
2924
2925 if (!mmc->has_init)
2926 return 0;
2927
2928 if (IS_SD(mmc)) {
2929 caps_filtered = mmc->card_caps &
2930 ~(MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25) |
2931 MMC_CAP(UHS_SDR50) | MMC_CAP(UHS_DDR50) |
2932 MMC_CAP(UHS_SDR104));
2933
2934 return sd_select_mode_and_width(mmc, caps_filtered);
2935 } else {
2936 caps_filtered = mmc->card_caps &
2937 ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_HS_400));
2938
2939 return mmc_select_mode_and_width(mmc, caps_filtered);
2940 }
2941}
2942#endif
2943
Markus Niebelab711882013-12-16 13:40:46 +01002944int mmc_set_dsr(struct mmc *mmc, u16 val)
2945{
2946 mmc->dsr = val;
2947 return 0;
2948}
2949
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002950/* CPU-specific MMC initializations */
2951__weak int cpu_mmc_init(bd_t *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05002952{
2953 return -1;
2954}
2955
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002956/* board-specific MMC initializations. */
2957__weak int board_mmc_init(bd_t *bis)
2958{
2959 return -1;
2960}
Andy Fleming272cc702008-10-30 16:41:01 -05002961
Che-Liang Chioue9550442012-11-28 15:21:13 +00002962void mmc_set_preinit(struct mmc *mmc, int preinit)
2963{
2964 mmc->preinit = preinit;
2965}
2966
Faiz Abbas8a856db2018-02-12 19:35:24 +05302967#if CONFIG_IS_ENABLED(DM_MMC)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002968static int mmc_probe(bd_t *bis)
2969{
Simon Glass4a1db6d2015-12-29 05:22:49 -07002970 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002971 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07002972 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002973
2974 ret = uclass_get(UCLASS_MMC, &uc);
2975 if (ret)
2976 return ret;
2977
Simon Glass4a1db6d2015-12-29 05:22:49 -07002978 /*
2979 * Try to add them in sequence order. Really with driver model we
2980 * should allow holes, but the current MMC list does not allow that.
2981 * So if we request 0, 1, 3 we will get 0, 1, 2.
2982 */
2983 for (i = 0; ; i++) {
2984 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
2985 if (ret == -ENODEV)
2986 break;
2987 }
2988 uclass_foreach_dev(dev, uc) {
2989 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002990 if (ret)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002991 pr_err("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002992 }
2993
2994 return 0;
2995}
2996#else
2997static int mmc_probe(bd_t *bis)
2998{
2999 if (board_mmc_init(bis) < 0)
3000 cpu_mmc_init(bis);
3001
3002 return 0;
3003}
3004#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00003005
Andy Fleming272cc702008-10-30 16:41:01 -05003006int mmc_initialize(bd_t *bis)
3007{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003008 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003009 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003010 if (initialized) /* Avoid initializing mmc multiple times */
3011 return 0;
3012 initialized = 1;
3013
Simon Glassc4d660d2017-07-04 13:31:19 -06003014#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01003015#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06003016 mmc_list_init();
3017#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01003018#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003019 ret = mmc_probe(bis);
3020 if (ret)
3021 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05003022
Ying Zhangbb0dc102013-08-16 15:16:11 +08003023#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05003024 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08003025#endif
Andy Fleming272cc702008-10-30 16:41:01 -05003026
Simon Glassc40fdca2016-05-01 13:52:35 -06003027 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05003028 return 0;
3029}
Tomas Melincd3d4882016-11-25 11:01:03 +02003030
Lokesh Vutla80f02012019-09-09 14:40:36 +05303031#if CONFIG_IS_ENABLED(DM_MMC)
3032int mmc_init_device(int num)
3033{
3034 struct udevice *dev;
3035 struct mmc *m;
3036 int ret;
3037
3038 ret = uclass_get_device(UCLASS_MMC, num, &dev);
3039 if (ret)
3040 return ret;
3041
3042 m = mmc_get_mmc_dev(dev);
3043 if (!m)
3044 return 0;
3045#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
3046 mmc_set_preinit(m, 1);
3047#endif
3048 if (m->preinit)
3049 mmc_start_init(m);
3050
3051 return 0;
3052}
3053#endif
3054
Tomas Melincd3d4882016-11-25 11:01:03 +02003055#ifdef CONFIG_CMD_BKOPS_ENABLE
3056int mmc_set_bkops_enable(struct mmc *mmc)
3057{
3058 int err;
3059 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
3060
3061 err = mmc_send_ext_csd(mmc, ext_csd);
3062 if (err) {
3063 puts("Could not get ext_csd register values\n");
3064 return err;
3065 }
3066
3067 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
3068 puts("Background operations not supported on device\n");
3069 return -EMEDIUMTYPE;
3070 }
3071
3072 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
3073 puts("Background operations already enabled\n");
3074 return 0;
3075 }
3076
3077 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
3078 if (err) {
3079 puts("Failed to enable manual background operations\n");
3080 return err;
3081 }
3082
3083 puts("Enabled manual background operations\n");
3084
3085 return 0;
3086}
3087#endif