blob: 2eb5e9d7aacc81240d22fde91107732b098af49e [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);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +020027static int mmc_power_cycle(struct mmc *mmc);
Marek Vasut62d77ce2018-04-15 00:37:11 +020028#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +020029static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps);
Marek Vasutb5b838f2016-12-01 02:06:33 +010030#endif
31
Simon Glasse7881d82017-07-29 11:35:31 -060032#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020033
Sam Protsenko6cf8a902019-08-14 22:52:51 +030034static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020035{
36 return -ENOSYS;
37}
38
Jeroen Hofstee750121c2014-07-12 21:24:08 +020039__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000040{
41 return -1;
42}
43
44int mmc_getwp(struct mmc *mmc)
45{
46 int wp;
47
48 wp = board_mmc_getwp(mmc);
49
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000050 if (wp < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020051 if (mmc->cfg->ops->getwp)
52 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000053 else
54 wp = 0;
55 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000056
57 return wp;
58}
59
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +020060__weak int board_mmc_getcd(struct mmc *mmc)
61{
Stefano Babic11fdade2010-02-05 15:04:43 +010062 return -1;
63}
Simon Glass8ca51e52016-06-12 23:30:22 -060064#endif
Stefano Babic11fdade2010-02-05 15:04:43 +010065
Marek Vasut8635ff92012-03-15 18:41:35 +000066#ifdef CONFIG_MMC_TRACE
Simon Glassc0c76eb2016-06-12 23:30:20 -060067void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
68{
69 printf("CMD_SEND:%d\n", cmd->cmdidx);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010070 printf("\t\tARG\t\t\t 0x%08x\n", cmd->cmdarg);
Simon Glassc0c76eb2016-06-12 23:30:20 -060071}
72
73void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
74{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000075 int i;
76 u8 *ptr;
77
Bin Meng7863ce52016-03-17 21:53:14 -070078 if (ret) {
79 printf("\t\tRET\t\t\t %d\n", ret);
80 } else {
81 switch (cmd->resp_type) {
82 case MMC_RSP_NONE:
83 printf("\t\tMMC_RSP_NONE\n");
84 break;
85 case MMC_RSP_R1:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010086 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070087 cmd->response[0]);
88 break;
89 case MMC_RSP_R1b:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010090 printf("\t\tMMC_RSP_R1b\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070091 cmd->response[0]);
92 break;
93 case MMC_RSP_R2:
Marek Vasut7d5ccb12019-03-23 18:54:45 +010094 printf("\t\tMMC_RSP_R2\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070095 cmd->response[0]);
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[1]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +010098 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -070099 cmd->response[2]);
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100100 printf("\t\t \t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700101 cmd->response[3]);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000102 printf("\n");
Bin Meng7863ce52016-03-17 21:53:14 -0700103 printf("\t\t\t\t\tDUMPING DATA\n");
104 for (i = 0; i < 4; i++) {
105 int j;
106 printf("\t\t\t\t\t%03d - ", i*4);
107 ptr = (u8 *)&cmd->response[i];
108 ptr += 3;
109 for (j = 0; j < 4; j++)
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100110 printf("%02x ", *ptr--);
Bin Meng7863ce52016-03-17 21:53:14 -0700111 printf("\n");
112 }
113 break;
114 case MMC_RSP_R3:
Marek Vasut7d5ccb12019-03-23 18:54:45 +0100115 printf("\t\tMMC_RSP_R3,4\t\t 0x%08x \n",
Bin Meng7863ce52016-03-17 21:53:14 -0700116 cmd->response[0]);
117 break;
118 default:
119 printf("\t\tERROR MMC rsp not supported\n");
120 break;
Bin Meng53e8e402016-03-17 21:53:13 -0700121 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000122 }
Simon Glassc0c76eb2016-06-12 23:30:20 -0600123}
124
125void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
126{
127 int status;
128
129 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
130 printf("CURR STATE:%d\n", status);
131}
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000132#endif
Simon Glassc0c76eb2016-06-12 23:30:20 -0600133
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200134#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
135const char *mmc_mode_name(enum bus_mode mode)
136{
137 static const char *const names[] = {
138 [MMC_LEGACY] = "MMC legacy",
139 [SD_LEGACY] = "SD Legacy",
140 [MMC_HS] = "MMC High Speed (26MHz)",
141 [SD_HS] = "SD High Speed (50MHz)",
142 [UHS_SDR12] = "UHS SDR12 (25MHz)",
143 [UHS_SDR25] = "UHS SDR25 (50MHz)",
144 [UHS_SDR50] = "UHS SDR50 (100MHz)",
145 [UHS_SDR104] = "UHS SDR104 (208MHz)",
146 [UHS_DDR50] = "UHS DDR50 (50MHz)",
147 [MMC_HS_52] = "MMC High Speed (52MHz)",
148 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
149 [MMC_HS_200] = "HS200 (200MHz)",
Peng Fan3dd26262018-08-10 14:07:54 +0800150 [MMC_HS_400] = "HS400 (200MHz)",
Peng Fan44acd492019-07-10 14:43:07 +0800151 [MMC_HS_400_ES] = "HS400ES (200MHz)",
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200152 };
153
154 if (mode >= MMC_MODES_END)
155 return "Unknown mode";
156 else
157 return names[mode];
158}
159#endif
160
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200161static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
162{
163 static const int freqs[] = {
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900164 [MMC_LEGACY] = 25000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200165 [SD_LEGACY] = 25000000,
166 [MMC_HS] = 26000000,
167 [SD_HS] = 50000000,
Jaehoon Chung1b313aa2018-01-30 14:10:16 +0900168 [MMC_HS_52] = 52000000,
169 [MMC_DDR_52] = 52000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200170 [UHS_SDR12] = 25000000,
171 [UHS_SDR25] = 50000000,
172 [UHS_SDR50] = 100000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200173 [UHS_DDR50] = 50000000,
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100174 [UHS_SDR104] = 208000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200175 [MMC_HS_200] = 200000000,
Peng Fan3dd26262018-08-10 14:07:54 +0800176 [MMC_HS_400] = 200000000,
Peng Fan44acd492019-07-10 14:43:07 +0800177 [MMC_HS_400_ES] = 200000000,
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200178 };
179
180 if (mode == MMC_LEGACY)
181 return mmc->legacy_speed;
182 else if (mode >= MMC_MODES_END)
183 return 0;
184 else
185 return freqs[mode];
186}
187
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200188static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
189{
190 mmc->selected_mode = mode;
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200191 mmc->tran_speed = mmc_mode2freq(mmc, mode);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200192 mmc->ddr_mode = mmc_is_mode_ddr(mode);
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900193 pr_debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
194 mmc->tran_speed / 1000000);
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200195 return 0;
196}
197
Simon Glasse7881d82017-07-29 11:35:31 -0600198#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassc0c76eb2016-06-12 23:30:20 -0600199int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
200{
201 int ret;
202
203 mmmc_trace_before_send(mmc, cmd);
204 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
205 mmmc_trace_after_send(mmc, cmd, ret);
206
Marek Vasut8635ff92012-03-15 18:41:35 +0000207 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500208}
Simon Glass8ca51e52016-06-12 23:30:22 -0600209#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500210
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200211int mmc_send_status(struct mmc *mmc, unsigned int *status)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000212{
213 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000214 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000215
216 cmd.cmdidx = MMC_CMD_SEND_STATUS;
217 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200218 if (!mmc_host_is_spi(mmc))
219 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000220
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200221 while (retries--) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000222 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000223 if (!err) {
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200224 mmc_trace_state(mmc, &cmd);
225 *status = cmd.response[0];
226 return 0;
227 }
228 }
229 mmc_trace_state(mmc, &cmd);
230 return -ECOMM;
231}
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200232
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300233int mmc_poll_for_busy(struct mmc *mmc, int timeout_ms)
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200234{
235 unsigned int status;
236 int err;
237
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300238 err = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotcd0b80e2019-07-02 10:53:53 +0200239 if (err != -ENOSYS)
240 return err;
241
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200242 while (1) {
243 err = mmc_send_status(mmc, &status);
244 if (err)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000245 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000246
Jean-Jacques Hiblot863d1002019-07-02 10:53:52 +0200247 if ((status & MMC_STATUS_RDY_FOR_DATA) &&
248 (status & MMC_STATUS_CURR_STATE) !=
249 MMC_STATE_PRG)
250 break;
251
252 if (status & MMC_STATUS_MASK) {
253#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
254 pr_err("Status Error: 0x%08x\n", status);
255#endif
256 return -ECOMM;
257 }
258
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300259 if (timeout_ms-- <= 0)
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500260 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000261
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500262 udelay(1000);
263 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000264
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300265 if (timeout_ms <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100266#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100267 pr_err("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100268#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900269 return -ETIMEDOUT;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000270 }
271
272 return 0;
273}
274
Paul Burtonda61fa52013-09-09 15:30:26 +0100275int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500276{
277 struct mmc_cmd cmd;
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +0200278 int err;
Andy Fleming272cc702008-10-30 16:41:01 -0500279
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600280 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900281 return 0;
282
Andy Fleming272cc702008-10-30 16:41:01 -0500283 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
284 cmd.resp_type = MMC_RSP_R1;
285 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500286
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +0200287 err = mmc_send_cmd(mmc, &cmd, NULL);
288
289#ifdef CONFIG_MMC_QUIRKS
290 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SET_BLOCKLEN)) {
291 int retries = 4;
292 /*
293 * It has been seen that SET_BLOCKLEN may fail on the first
294 * attempt, let's try a few more time
295 */
296 do {
297 err = mmc_send_cmd(mmc, &cmd, NULL);
298 if (!err)
299 break;
300 } while (retries--);
301 }
302#endif
303
304 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500305}
306
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100307#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200308static const u8 tuning_blk_pattern_4bit[] = {
309 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
310 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
311 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
312 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
313 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
314 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
315 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
316 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
317};
318
319static const u8 tuning_blk_pattern_8bit[] = {
320 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
321 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
322 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
323 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
324 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
325 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
326 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
327 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
328 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
329 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
330 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
331 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
332 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
333 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
334 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
335 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
336};
337
338int mmc_send_tuning(struct mmc *mmc, u32 opcode, int *cmd_error)
339{
340 struct mmc_cmd cmd;
341 struct mmc_data data;
342 const u8 *tuning_block_pattern;
343 int size, err;
344
345 if (mmc->bus_width == 8) {
346 tuning_block_pattern = tuning_blk_pattern_8bit;
347 size = sizeof(tuning_blk_pattern_8bit);
348 } else if (mmc->bus_width == 4) {
349 tuning_block_pattern = tuning_blk_pattern_4bit;
350 size = sizeof(tuning_blk_pattern_4bit);
351 } else {
352 return -EINVAL;
353 }
354
355 ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size);
356
357 cmd.cmdidx = opcode;
358 cmd.cmdarg = 0;
359 cmd.resp_type = MMC_RSP_R1;
360
361 data.dest = (void *)data_buf;
362 data.blocks = 1;
363 data.blocksize = size;
364 data.flags = MMC_DATA_READ;
365
366 err = mmc_send_cmd(mmc, &cmd, &data);
367 if (err)
368 return err;
369
370 if (memcmp(data_buf, tuning_block_pattern, size))
371 return -EIO;
372
373 return 0;
374}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100375#endif
Jean-Jacques Hiblot9815e3b2017-09-21 16:30:12 +0200376
Sascha Silbeff8fef52013-06-14 13:07:25 +0200377static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000378 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500379{
380 struct mmc_cmd cmd;
381 struct mmc_data data;
382
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700383 if (blkcnt > 1)
384 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
385 else
386 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500387
388 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700389 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500390 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700391 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500392
393 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500394
395 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700396 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500397 data.blocksize = mmc->read_bl_len;
398 data.flags = MMC_DATA_READ;
399
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700400 if (mmc_send_cmd(mmc, &cmd, &data))
401 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500402
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700403 if (blkcnt > 1) {
404 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
405 cmd.cmdarg = 0;
406 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700407 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton56196822013-09-04 16:12:25 +0100408#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100409 pr_err("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100410#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700411 return 0;
412 }
Andy Fleming272cc702008-10-30 16:41:01 -0500413 }
414
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700415 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500416}
417
Simon Glassc4d660d2017-07-04 13:31:19 -0600418#if CONFIG_IS_ENABLED(BLK)
Simon Glass7dba0b92016-06-12 23:30:15 -0600419ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600420#else
Simon Glass7dba0b92016-06-12 23:30:15 -0600421ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
422 void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600423#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500424{
Simon Glassc4d660d2017-07-04 13:31:19 -0600425#if CONFIG_IS_ENABLED(BLK)
Simon Glass33fb2112016-05-01 13:52:41 -0600426 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
427#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700428 int dev_num = block_dev->devnum;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700429 int err;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700430 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500431
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700432 if (blkcnt == 0)
433 return 0;
434
435 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500436 if (!mmc)
437 return 0;
438
Marek Vasutb5b838f2016-12-01 02:06:33 +0100439 if (CONFIG_IS_ENABLED(MMC_TINY))
440 err = mmc_switch_part(mmc, block_dev->hwpart);
441 else
442 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
443
Stephen Warren873cc1d2015-12-07 11:38:49 -0700444 if (err < 0)
445 return 0;
446
Simon Glassc40fdca2016-05-01 13:52:35 -0600447 if ((start + blkcnt) > block_dev->lba) {
Paul Burton56196822013-09-04 16:12:25 +0100448#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100449 pr_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
450 start + blkcnt, block_dev->lba);
Paul Burton56196822013-09-04 16:12:25 +0100451#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800452 return 0;
453 }
Andy Fleming272cc702008-10-30 16:41:01 -0500454
Simon Glass11692992015-06-23 15:38:50 -0600455 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900456 pr_debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500457 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600458 }
Andy Fleming272cc702008-10-30 16:41:01 -0500459
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700460 do {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200461 cur = (blocks_todo > mmc->cfg->b_max) ?
462 mmc->cfg->b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600463 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +0900464 pr_debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700465 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600466 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700467 blocks_todo -= cur;
468 start += cur;
469 dst += cur * mmc->read_bl_len;
470 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500471
472 return blkcnt;
473}
474
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000475static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500476{
477 struct mmc_cmd cmd;
478 int err;
479
480 udelay(1000);
481
482 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
483 cmd.cmdarg = 0;
484 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500485
486 err = mmc_send_cmd(mmc, &cmd, NULL);
487
488 if (err)
489 return err;
490
491 udelay(2000);
492
493 return 0;
494}
495
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100496#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200497static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
498{
499 struct mmc_cmd cmd;
500 int err = 0;
501
502 /*
503 * Send CMD11 only if the request is to switch the card to
504 * 1.8V signalling.
505 */
506 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
507 return mmc_set_signal_voltage(mmc, signal_voltage);
508
509 cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
510 cmd.cmdarg = 0;
511 cmd.resp_type = MMC_RSP_R1;
512
513 err = mmc_send_cmd(mmc, &cmd, NULL);
514 if (err)
515 return err;
516
517 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
518 return -EIO;
519
520 /*
521 * The card should drive cmd and dat[0:3] low immediately
522 * after the response of cmd11, but wait 100 us to be sure
523 */
524 err = mmc_wait_dat0(mmc, 0, 100);
525 if (err == -ENOSYS)
526 udelay(100);
527 else if (err)
528 return -ETIMEDOUT;
529
530 /*
531 * During a signal voltage level switch, the clock must be gated
532 * for 5 ms according to the SD spec
533 */
Jaehoon Chung65117182018-01-26 19:25:29 +0900534 mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200535
536 err = mmc_set_signal_voltage(mmc, signal_voltage);
537 if (err)
538 return err;
539
540 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
541 mdelay(10);
Jaehoon Chung65117182018-01-26 19:25:29 +0900542 mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200543
544 /*
545 * Failure to switch is indicated by the card holding
546 * dat[0:3] low. Wait for at least 1 ms according to spec
547 */
548 err = mmc_wait_dat0(mmc, 1, 1000);
549 if (err == -ENOSYS)
550 udelay(1000);
551 else if (err)
552 return -ETIMEDOUT;
553
554 return 0;
555}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100556#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200557
558static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
Andy Fleming272cc702008-10-30 16:41:01 -0500559{
560 int timeout = 1000;
561 int err;
562 struct mmc_cmd cmd;
563
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500564 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500565 cmd.cmdidx = MMC_CMD_APP_CMD;
566 cmd.resp_type = MMC_RSP_R1;
567 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500568
569 err = mmc_send_cmd(mmc, &cmd, NULL);
570
571 if (err)
572 return err;
573
574 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
575 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100576
577 /*
578 * Most cards do not answer if some reserved bits
579 * in the ocr are set. However, Some controller
580 * can set bit 7 (reserved for low voltages), but
581 * how to manage low voltages SD card is not yet
582 * specified.
583 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000584 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200585 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500586
587 if (mmc->version == SD_VERSION_2)
588 cmd.cmdarg |= OCR_HCS;
589
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200590 if (uhs_en)
591 cmd.cmdarg |= OCR_S18R;
592
Andy Fleming272cc702008-10-30 16:41:01 -0500593 err = mmc_send_cmd(mmc, &cmd, NULL);
594
595 if (err)
596 return err;
597
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500598 if (cmd.response[0] & OCR_BUSY)
599 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500600
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500601 if (timeout-- <= 0)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900602 return -EOPNOTSUPP;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500603
604 udelay(1000);
605 }
Andy Fleming272cc702008-10-30 16:41:01 -0500606
607 if (mmc->version != SD_VERSION_2)
608 mmc->version = SD_VERSION_1_0;
609
Thomas Choud52ebf12010-12-24 13:12:21 +0000610 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
611 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
612 cmd.resp_type = MMC_RSP_R3;
613 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000614
615 err = mmc_send_cmd(mmc, &cmd, NULL);
616
617 if (err)
618 return err;
619 }
620
Rabin Vincent998be3d2009-04-05 13:30:56 +0530621 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500622
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100623#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200624 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
625 == 0x41000000) {
626 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
627 if (err)
628 return err;
629 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +0100630#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200631
Andy Fleming272cc702008-10-30 16:41:01 -0500632 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
633 mmc->rca = 0;
634
635 return 0;
636}
637
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500638static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500639{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500640 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500641 int err;
642
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500643 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
644 cmd.resp_type = MMC_RSP_R3;
645 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500646 if (use_arg && !mmc_host_is_spi(mmc))
647 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200648 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500649 (mmc->ocr & OCR_VOLTAGE_MASK)) |
650 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000651
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500652 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000653 if (err)
654 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500655 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000656 return 0;
657}
658
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200659static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000660{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000661 int err, i;
662
Andy Fleming272cc702008-10-30 16:41:01 -0500663 /* Some cards seem to need this */
664 mmc_go_idle(mmc);
665
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000666 /* Asking to the card its capabilities */
Che-Liang Chioue9550442012-11-28 15:21:13 +0000667 for (i = 0; i < 2; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500668 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500669 if (err)
670 return err;
671
Che-Liang Chioue9550442012-11-28 15:21:13 +0000672 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500673 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500674 break;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000675 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500676 mmc->op_cond_pending = 1;
677 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000678}
Andy Fleming272cc702008-10-30 16:41:01 -0500679
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200680static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000681{
682 struct mmc_cmd cmd;
683 int timeout = 1000;
Vipul Kumar36332b62018-05-03 12:20:54 +0530684 ulong start;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000685 int err;
686
687 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500688 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lud188b112016-08-02 15:33:18 +0800689 /* Some cards seem to need this */
690 mmc_go_idle(mmc);
691
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500692 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500693 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500694 err = mmc_send_op_cond_iter(mmc, 1);
695 if (err)
696 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500697 if (mmc->ocr & OCR_BUSY)
698 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500699 if (get_timer(start) > timeout)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900700 return -EOPNOTSUPP;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500701 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500702 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500703 }
Andy Fleming272cc702008-10-30 16:41:01 -0500704
Thomas Choud52ebf12010-12-24 13:12:21 +0000705 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
706 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
707 cmd.resp_type = MMC_RSP_R3;
708 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000709
710 err = mmc_send_cmd(mmc, &cmd, NULL);
711
712 if (err)
713 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500714
715 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000716 }
717
Andy Fleming272cc702008-10-30 16:41:01 -0500718 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500719
720 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700721 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500722
723 return 0;
724}
725
726
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000727static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500728{
729 struct mmc_cmd cmd;
730 struct mmc_data data;
731 int err;
732
733 /* Get the Card Status Register */
734 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
735 cmd.resp_type = MMC_RSP_R1;
736 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500737
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000738 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500739 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000740 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500741 data.flags = MMC_DATA_READ;
742
743 err = mmc_send_cmd(mmc, &cmd, &data);
744
745 return err;
746}
747
Marek Vasut68925502019-02-06 11:34:27 +0100748static int __mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value,
749 bool send_status)
Andy Fleming272cc702008-10-30 16:41:01 -0500750{
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200751 unsigned int status, start;
Andy Fleming272cc702008-10-30 16:41:01 -0500752 struct mmc_cmd cmd;
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300753 int timeout_ms = DEFAULT_CMD6_TIMEOUT_MS;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200754 bool is_part_switch = (set == EXT_CSD_CMD_SET_NORMAL) &&
755 (index == EXT_CSD_PART_CONF);
Maxime Riparda9003dc2016-11-04 16:18:08 +0100756 int retries = 3;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000757 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500758
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200759 if (mmc->gen_cmd6_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300760 timeout_ms = mmc->gen_cmd6_time * 10;
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +0200761
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200762 if (is_part_switch && mmc->part_switch_time)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300763 timeout_ms = mmc->part_switch_time * 10;
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +0200764
Andy Fleming272cc702008-10-30 16:41:01 -0500765 cmd.cmdidx = MMC_CMD_SWITCH;
766 cmd.resp_type = MMC_RSP_R1b;
767 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000768 (index << 16) |
769 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500770
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200771 do {
Maxime Riparda9003dc2016-11-04 16:18:08 +0100772 ret = mmc_send_cmd(mmc, &cmd, NULL);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200773 } while (ret && retries-- > 0);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000774
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200775 if (ret)
776 return ret;
777
778 start = get_timer(0);
779
780 /* poll dat0 for rdy/buys status */
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300781 ret = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200782 if (ret && ret != -ENOSYS)
783 return ret;
784
785 /*
786 * In cases when not allowed to poll by using CMD13 or because we aren't
787 * capable of polling by using mmc_wait_dat0, then rely on waiting the
788 * stated timeout to be sufficient.
789 */
790 if (ret == -ENOSYS && !send_status)
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300791 mdelay(timeout_ms);
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200792
793 /* Finally wait until the card is ready or indicates a failure
794 * to switch. It doesn't hurt to use CMD13 here even if send_status
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300795 * is false, because by now (after 'timeout_ms' ms) the bus should be
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200796 * reliable.
797 */
798 do {
799 ret = mmc_send_status(mmc, &status);
800
801 if (!ret && (status & MMC_STATUS_SWITCH_ERROR)) {
802 pr_debug("switch failed %d/%d/0x%x !\n", set, index,
803 value);
804 return -EIO;
Maxime Riparda9003dc2016-11-04 16:18:08 +0100805 }
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200806 if (!ret && (status & MMC_STATUS_RDY_FOR_DATA))
Marek Vasut68925502019-02-06 11:34:27 +0100807 return 0;
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200808 udelay(100);
Sam Protsenko6cf8a902019-08-14 22:52:51 +0300809 } while (get_timer(start) < timeout_ms);
Marek Vasut68925502019-02-06 11:34:27 +0100810
Jean-Jacques Hiblotbb98b8c2019-07-02 10:53:56 +0200811 return -ETIMEDOUT;
Andy Fleming272cc702008-10-30 16:41:01 -0500812}
813
Marek Vasut68925502019-02-06 11:34:27 +0100814int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
815{
816 return __mmc_switch(mmc, set, index, value, true);
817}
818
Marek Vasut62d77ce2018-04-15 00:37:11 +0200819#if !CONFIG_IS_ENABLED(MMC_TINY)
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100820static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode,
821 bool hsdowngrade)
Andy Fleming272cc702008-10-30 16:41:01 -0500822{
Andy Fleming272cc702008-10-30 16:41:01 -0500823 int err;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200824 int speed_bits;
825
826 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
827
828 switch (mode) {
829 case MMC_HS:
830 case MMC_HS_52:
831 case MMC_DDR_52:
832 speed_bits = EXT_CSD_TIMING_HS;
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200833 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100834#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200835 case MMC_HS_200:
836 speed_bits = EXT_CSD_TIMING_HS200;
837 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100838#endif
Peng Fan3dd26262018-08-10 14:07:54 +0800839#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
840 case MMC_HS_400:
841 speed_bits = EXT_CSD_TIMING_HS400;
842 break;
843#endif
Peng Fan44acd492019-07-10 14:43:07 +0800844#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
845 case MMC_HS_400_ES:
846 speed_bits = EXT_CSD_TIMING_HS400;
847 break;
848#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200849 case MMC_LEGACY:
850 speed_bits = EXT_CSD_TIMING_LEGACY;
851 break;
852 default:
853 return -EINVAL;
854 }
Marek Vasut68925502019-02-06 11:34:27 +0100855
856 err = __mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
857 speed_bits, !hsdowngrade);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200858 if (err)
859 return err;
860
Marek Vasutb9a2a0e2019-01-03 21:19:24 +0100861#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
862 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
863 /*
864 * In case the eMMC is in HS200/HS400 mode and we are downgrading
865 * to HS mode, the card clock are still running much faster than
866 * the supported HS mode clock, so we can not reliably read out
867 * Extended CSD. Reconfigure the controller to run at HS mode.
868 */
869 if (hsdowngrade) {
870 mmc_select_mode(mmc, MMC_HS);
871 mmc_set_clock(mmc, mmc_mode2freq(mmc, MMC_HS), false);
872 }
873#endif
874
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200875 if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
876 /* Now check to see that it worked */
877 err = mmc_send_ext_csd(mmc, test_csd);
878 if (err)
879 return err;
880
881 /* No high-speed support */
882 if (!test_csd[EXT_CSD_HS_TIMING])
883 return -ENOTSUPP;
884 }
885
886 return 0;
887}
888
889static int mmc_get_capabilities(struct mmc *mmc)
890{
891 u8 *ext_csd = mmc->ext_csd;
892 char cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500893
Jean-Jacques Hiblot00e446f2017-11-30 17:43:56 +0100894 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -0500895
Thomas Choud52ebf12010-12-24 13:12:21 +0000896 if (mmc_host_is_spi(mmc))
897 return 0;
898
Andy Fleming272cc702008-10-30 16:41:01 -0500899 /* Only version 4 supports high-speed */
900 if (mmc->version < MMC_VERSION_4)
901 return 0;
902
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200903 if (!ext_csd) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +0100904 pr_err("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200905 return -ENOTSUPP;
906 }
907
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600908 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
909
Peng Fan3dd26262018-08-10 14:07:54 +0800910 cardtype = ext_csd[EXT_CSD_CARD_TYPE];
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +0200911 mmc->cardtype = cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500912
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100913#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200914 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
915 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
916 mmc->card_caps |= MMC_MODE_HS200;
917 }
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +0100918#endif
Peng Fan44acd492019-07-10 14:43:07 +0800919#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
920 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Peng Fan3dd26262018-08-10 14:07:54 +0800921 if (cardtype & (EXT_CSD_CARD_TYPE_HS400_1_2V |
922 EXT_CSD_CARD_TYPE_HS400_1_8V)) {
923 mmc->card_caps |= MMC_MODE_HS400;
924 }
925#endif
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900926 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200927 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900928 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200929 mmc->card_caps |= MMC_MODE_HS_52MHz;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900930 }
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200931 if (cardtype & EXT_CSD_CARD_TYPE_26)
932 mmc->card_caps |= MMC_MODE_HS;
Andy Fleming272cc702008-10-30 16:41:01 -0500933
Peng Fan44acd492019-07-10 14:43:07 +0800934#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
935 if (ext_csd[EXT_CSD_STROBE_SUPPORT] &&
936 (mmc->card_caps & MMC_MODE_HS400)) {
937 mmc->card_caps |= MMC_MODE_HS400_ES;
938 }
939#endif
940
Andy Fleming272cc702008-10-30 16:41:01 -0500941 return 0;
942}
Marek Vasut62d77ce2018-04-15 00:37:11 +0200943#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500944
Stephen Warrenf866a462013-06-11 15:14:01 -0600945static int mmc_set_capacity(struct mmc *mmc, int part_num)
946{
947 switch (part_num) {
948 case 0:
949 mmc->capacity = mmc->capacity_user;
950 break;
951 case 1:
952 case 2:
953 mmc->capacity = mmc->capacity_boot;
954 break;
955 case 3:
956 mmc->capacity = mmc->capacity_rpmb;
957 break;
958 case 4:
959 case 5:
960 case 6:
961 case 7:
962 mmc->capacity = mmc->capacity_gp[part_num - 4];
963 break;
964 default:
965 return -1;
966 }
967
Simon Glassc40fdca2016-05-01 13:52:35 -0600968 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -0600969
970 return 0;
971}
972
Simon Glass7dba0b92016-06-12 23:30:15 -0600973int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +0000974{
Stephen Warrenf866a462013-06-11 15:14:01 -0600975 int ret;
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +0200976 int retry = 3;
Lei Wenbc897b12011-05-02 16:26:26 +0000977
Jean-Jacques Hiblot05384772019-07-02 10:53:58 +0200978 do {
979 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
980 EXT_CSD_PART_CONF,
981 (mmc->part_config & ~PART_ACCESS_MASK)
982 | (part_num & PART_ACCESS_MASK));
983 } while (ret && retry--);
Stephen Warrenf866a462013-06-11 15:14:01 -0600984
Peter Bigot6dc93e72014-09-02 18:31:23 -0500985 /*
986 * Set the capacity if the switch succeeded or was intended
987 * to return to representing the raw device.
988 */
Stephen Warren873cc1d2015-12-07 11:38:49 -0700989 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -0500990 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -0600991 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700992 }
Peter Bigot6dc93e72014-09-02 18:31:23 -0500993
994 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000995}
996
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +0100997#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100998int mmc_hwpart_config(struct mmc *mmc,
999 const struct mmc_hwpart_conf *conf,
1000 enum mmc_hwpart_conf_mode mode)
1001{
1002 u8 part_attrs = 0;
1003 u32 enh_size_mult;
1004 u32 enh_start_addr;
1005 u32 gp_size_mult[4];
1006 u32 max_enh_size_mult;
1007 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001008 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001009 int i, pidx, err;
1010 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1011
1012 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
1013 return -EINVAL;
1014
1015 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001016 pr_err("eMMC >= 4.4 required for enhanced user data area\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001017 return -EMEDIUMTYPE;
1018 }
1019
1020 if (!(mmc->part_support & PART_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001021 pr_err("Card does not support partitioning\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001022 return -EMEDIUMTYPE;
1023 }
1024
1025 if (!mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001026 pr_err("Card does not define HC WP group size\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001027 return -EMEDIUMTYPE;
1028 }
1029
1030 /* check partition alignment and total enhanced size */
1031 if (conf->user.enh_size) {
1032 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
1033 conf->user.enh_start % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001034 pr_err("User data enhanced area not HC WP group "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001035 "size aligned\n");
1036 return -EINVAL;
1037 }
1038 part_attrs |= EXT_CSD_ENH_USR;
1039 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
1040 if (mmc->high_capacity) {
1041 enh_start_addr = conf->user.enh_start;
1042 } else {
1043 enh_start_addr = (conf->user.enh_start << 9);
1044 }
1045 } else {
1046 enh_size_mult = 0;
1047 enh_start_addr = 0;
1048 }
1049 tot_enh_size_mult += enh_size_mult;
1050
1051 for (pidx = 0; pidx < 4; pidx++) {
1052 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001053 pr_err("GP%i partition not HC WP group size "
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001054 "aligned\n", pidx+1);
1055 return -EINVAL;
1056 }
1057 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
1058 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
1059 part_attrs |= EXT_CSD_ENH_GP(pidx);
1060 tot_enh_size_mult += gp_size_mult[pidx];
1061 }
1062 }
1063
1064 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001065 pr_err("Card does not support enhanced attribute\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001066 return -EMEDIUMTYPE;
1067 }
1068
1069 err = mmc_send_ext_csd(mmc, ext_csd);
1070 if (err)
1071 return err;
1072
1073 max_enh_size_mult =
1074 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
1075 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
1076 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
1077 if (tot_enh_size_mult > max_enh_size_mult) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001078 pr_err("Total enhanced size exceeds maximum (%u > %u)\n",
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001079 tot_enh_size_mult, max_enh_size_mult);
1080 return -EMEDIUMTYPE;
1081 }
1082
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001083 /* The default value of EXT_CSD_WR_REL_SET is device
1084 * dependent, the values can only be changed if the
1085 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
1086 * changed only once and before partitioning is completed. */
1087 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1088 if (conf->user.wr_rel_change) {
1089 if (conf->user.wr_rel_set)
1090 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
1091 else
1092 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
1093 }
1094 for (pidx = 0; pidx < 4; pidx++) {
1095 if (conf->gp_part[pidx].wr_rel_change) {
1096 if (conf->gp_part[pidx].wr_rel_set)
1097 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
1098 else
1099 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
1100 }
1101 }
1102
1103 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
1104 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
1105 puts("Card does not support host controlled partition write "
1106 "reliability settings\n");
1107 return -EMEDIUMTYPE;
1108 }
1109
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001110 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
1111 EXT_CSD_PARTITION_SETTING_COMPLETED) {
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001112 pr_err("Card already partitioned\n");
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001113 return -EPERM;
1114 }
1115
1116 if (mode == MMC_HWPART_CONF_CHECK)
1117 return 0;
1118
1119 /* Partitioning requires high-capacity size definitions */
1120 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
1121 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1122 EXT_CSD_ERASE_GROUP_DEF, 1);
1123
1124 if (err)
1125 return err;
1126
1127 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1128
Jaehoon Chung4af66592020-01-17 15:06:54 +09001129#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001130 /* update erase group size to be high-capacity */
1131 mmc->erase_grp_size =
1132 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jaehoon Chung4af66592020-01-17 15:06:54 +09001133#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001134
1135 }
1136
1137 /* all OK, write the configuration */
1138 for (i = 0; i < 4; i++) {
1139 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1140 EXT_CSD_ENH_START_ADDR+i,
1141 (enh_start_addr >> (i*8)) & 0xFF);
1142 if (err)
1143 return err;
1144 }
1145 for (i = 0; i < 3; i++) {
1146 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1147 EXT_CSD_ENH_SIZE_MULT+i,
1148 (enh_size_mult >> (i*8)) & 0xFF);
1149 if (err)
1150 return err;
1151 }
1152 for (pidx = 0; pidx < 4; pidx++) {
1153 for (i = 0; i < 3; i++) {
1154 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1155 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1156 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1157 if (err)
1158 return err;
1159 }
1160 }
1161 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1162 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1163 if (err)
1164 return err;
1165
1166 if (mode == MMC_HWPART_CONF_SET)
1167 return 0;
1168
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001169 /* The WR_REL_SET is a write-once register but shall be
1170 * written before setting PART_SETTING_COMPLETED. As it is
1171 * write-once we can only write it when completing the
1172 * partitioning. */
1173 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1174 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1175 EXT_CSD_WR_REL_SET, wr_rel_set);
1176 if (err)
1177 return err;
1178 }
1179
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001180 /* Setting PART_SETTING_COMPLETED confirms the partition
1181 * configuration but it only becomes effective after power
1182 * cycle, so we do not adjust the partition related settings
1183 * in the mmc struct. */
1184
1185 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1186 EXT_CSD_PARTITION_SETTING,
1187 EXT_CSD_PARTITION_SETTING_COMPLETED);
1188 if (err)
1189 return err;
1190
1191 return 0;
1192}
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001193#endif
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001194
Simon Glasse7881d82017-07-29 11:35:31 -06001195#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +00001196int mmc_getcd(struct mmc *mmc)
1197{
1198 int cd;
1199
1200 cd = board_mmc_getcd(mmc);
1201
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001202 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001203 if (mmc->cfg->ops->getcd)
1204 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001205 else
1206 cd = 1;
1207 }
Thierry Reding48972d92012-01-02 01:15:37 +00001208
1209 return cd;
1210}
Simon Glass8ca51e52016-06-12 23:30:22 -06001211#endif
Thierry Reding48972d92012-01-02 01:15:37 +00001212
Marek Vasut62d77ce2018-04-15 00:37:11 +02001213#if !CONFIG_IS_ENABLED(MMC_TINY)
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001214static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -05001215{
1216 struct mmc_cmd cmd;
1217 struct mmc_data data;
1218
1219 /* Switch the frequency */
1220 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1221 cmd.resp_type = MMC_RSP_R1;
1222 cmd.cmdarg = (mode << 31) | 0xffffff;
1223 cmd.cmdarg &= ~(0xf << (group * 4));
1224 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -05001225
1226 data.dest = (char *)resp;
1227 data.blocksize = 64;
1228 data.blocks = 1;
1229 data.flags = MMC_DATA_READ;
1230
1231 return mmc_send_cmd(mmc, &cmd, &data);
1232}
1233
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001234static int sd_get_capabilities(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001235{
1236 int err;
1237 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +05301238 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1239 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -05001240 struct mmc_data data;
1241 int timeout;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001242#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001243 u32 sd3_bus_mode;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001244#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001245
Jean-Jacques Hiblot00e446f2017-11-30 17:43:56 +01001246 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(SD_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05001247
Thomas Choud52ebf12010-12-24 13:12:21 +00001248 if (mmc_host_is_spi(mmc))
1249 return 0;
1250
Andy Fleming272cc702008-10-30 16:41:01 -05001251 /* Read the SCR to find out if this card supports higher speeds */
1252 cmd.cmdidx = MMC_CMD_APP_CMD;
1253 cmd.resp_type = MMC_RSP_R1;
1254 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001255
1256 err = mmc_send_cmd(mmc, &cmd, NULL);
1257
1258 if (err)
1259 return err;
1260
1261 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1262 cmd.resp_type = MMC_RSP_R1;
1263 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001264
1265 timeout = 3;
1266
1267retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +00001268 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -05001269 data.blocksize = 8;
1270 data.blocks = 1;
1271 data.flags = MMC_DATA_READ;
1272
1273 err = mmc_send_cmd(mmc, &cmd, &data);
1274
1275 if (err) {
1276 if (timeout--)
1277 goto retry_scr;
1278
1279 return err;
1280 }
1281
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001282 mmc->scr[0] = __be32_to_cpu(scr[0]);
1283 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -05001284
1285 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -07001286 case 0:
1287 mmc->version = SD_VERSION_1_0;
1288 break;
1289 case 1:
1290 mmc->version = SD_VERSION_1_10;
1291 break;
1292 case 2:
1293 mmc->version = SD_VERSION_2;
1294 if ((mmc->scr[0] >> 15) & 0x1)
1295 mmc->version = SD_VERSION_3;
1296 break;
1297 default:
1298 mmc->version = SD_VERSION_1_0;
1299 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001300 }
1301
Alagu Sankarb44c7082010-05-12 15:08:24 +05301302 if (mmc->scr[0] & SD_DATA_4BIT)
1303 mmc->card_caps |= MMC_MODE_4BIT;
1304
Andy Fleming272cc702008-10-30 16:41:01 -05001305 /* Version 1.0 doesn't support switching */
1306 if (mmc->version == SD_VERSION_1_0)
1307 return 0;
1308
1309 timeout = 4;
1310 while (timeout--) {
1311 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +00001312 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001313
1314 if (err)
1315 return err;
1316
1317 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001318 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -05001319 break;
1320 }
1321
Andy Fleming272cc702008-10-30 16:41:01 -05001322 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001323 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1324 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Fleming272cc702008-10-30 16:41:01 -05001325
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001326#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001327 /* Version before 3.0 don't support UHS modes */
1328 if (mmc->version < SD_VERSION_3)
1329 return 0;
1330
1331 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1332 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1333 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1334 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1335 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1336 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1337 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1338 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1339 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1340 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1341 mmc->card_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001342#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001343
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001344 return 0;
1345}
1346
1347static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1348{
1349 int err;
1350
1351 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001352 int speed;
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001353
Marek Vasutcf345762018-11-18 03:25:08 +01001354 /* SD version 1.00 and 1.01 does not support CMD 6 */
1355 if (mmc->version == SD_VERSION_1_0)
1356 return 0;
1357
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001358 switch (mode) {
1359 case SD_LEGACY:
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001360 speed = UHS_SDR12_BUS_SPEED;
1361 break;
1362 case SD_HS:
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001363 speed = HIGH_SPEED_BUS_SPEED;
1364 break;
1365#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1366 case UHS_SDR12:
1367 speed = UHS_SDR12_BUS_SPEED;
1368 break;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001369 case UHS_SDR25:
1370 speed = UHS_SDR25_BUS_SPEED;
1371 break;
1372 case UHS_SDR50:
1373 speed = UHS_SDR50_BUS_SPEED;
1374 break;
1375 case UHS_DDR50:
1376 speed = UHS_DDR50_BUS_SPEED;
1377 break;
1378 case UHS_SDR104:
1379 speed = UHS_SDR104_BUS_SPEED;
1380 break;
Jean-Jacques Hiblotbaef2072018-01-04 15:23:30 +01001381#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001382 default:
1383 return -EINVAL;
1384 }
1385
1386 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001387 if (err)
1388 return err;
1389
Jean-Jacques Hiblota0276f32018-02-09 12:09:27 +01001390 if (((__be32_to_cpu(switch_status[4]) >> 24) & 0xF) != speed)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001391 return -ENOTSUPP;
1392
1393 return 0;
1394}
1395
Marek Vasutec360e62018-04-15 00:36:45 +02001396static int sd_select_bus_width(struct mmc *mmc, int w)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001397{
1398 int err;
1399 struct mmc_cmd cmd;
1400
1401 if ((w != 4) && (w != 1))
1402 return -EINVAL;
1403
1404 cmd.cmdidx = MMC_CMD_APP_CMD;
1405 cmd.resp_type = MMC_RSP_R1;
1406 cmd.cmdarg = mmc->rca << 16;
1407
1408 err = mmc_send_cmd(mmc, &cmd, NULL);
1409 if (err)
1410 return err;
1411
1412 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1413 cmd.resp_type = MMC_RSP_R1;
1414 if (w == 4)
1415 cmd.cmdarg = 2;
1416 else if (w == 1)
1417 cmd.cmdarg = 0;
1418 err = mmc_send_cmd(mmc, &cmd, NULL);
1419 if (err)
1420 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001421
1422 return 0;
1423}
Marek Vasut62d77ce2018-04-15 00:37:11 +02001424#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001425
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001426#if CONFIG_IS_ENABLED(MMC_WRITE)
Peng Fan3697e592016-09-01 11:13:38 +08001427static int sd_read_ssr(struct mmc *mmc)
1428{
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001429 static const unsigned int sd_au_size[] = {
1430 0, SZ_16K / 512, SZ_32K / 512,
1431 SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
1432 SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
1433 SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
1434 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512,
1435 SZ_64M / 512,
1436 };
Peng Fan3697e592016-09-01 11:13:38 +08001437 int err, i;
1438 struct mmc_cmd cmd;
1439 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1440 struct mmc_data data;
1441 int timeout = 3;
1442 unsigned int au, eo, et, es;
1443
1444 cmd.cmdidx = MMC_CMD_APP_CMD;
1445 cmd.resp_type = MMC_RSP_R1;
1446 cmd.cmdarg = mmc->rca << 16;
1447
1448 err = mmc_send_cmd(mmc, &cmd, NULL);
Joel Johnsond4a5fa32020-01-11 09:08:14 -07001449#ifdef CONFIG_MMC_QUIRKS
1450 if (err && (mmc->quirks & MMC_QUIRK_RETRY_APP_CMD)) {
1451 int retries = 4;
1452 /*
1453 * It has been seen that APP_CMD may fail on the first
1454 * attempt, let's try a few more times
1455 */
1456 do {
1457 err = mmc_send_cmd(mmc, &cmd, NULL);
1458 if (!err)
1459 break;
1460 } while (retries--);
1461 }
1462#endif
Peng Fan3697e592016-09-01 11:13:38 +08001463 if (err)
1464 return err;
1465
1466 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1467 cmd.resp_type = MMC_RSP_R1;
1468 cmd.cmdarg = 0;
1469
1470retry_ssr:
1471 data.dest = (char *)ssr;
1472 data.blocksize = 64;
1473 data.blocks = 1;
1474 data.flags = MMC_DATA_READ;
1475
1476 err = mmc_send_cmd(mmc, &cmd, &data);
1477 if (err) {
1478 if (timeout--)
1479 goto retry_ssr;
1480
1481 return err;
1482 }
1483
1484 for (i = 0; i < 16; i++)
1485 ssr[i] = be32_to_cpu(ssr[i]);
1486
1487 au = (ssr[2] >> 12) & 0xF;
1488 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1489 mmc->ssr.au = sd_au_size[au];
1490 es = (ssr[3] >> 24) & 0xFF;
1491 es |= (ssr[2] & 0xFF) << 8;
1492 et = (ssr[3] >> 18) & 0x3F;
1493 if (es && et) {
1494 eo = (ssr[3] >> 16) & 0x3;
1495 mmc->ssr.erase_timeout = (et * 1000) / es;
1496 mmc->ssr.erase_offset = eo * 1000;
1497 }
1498 } else {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001499 pr_debug("Invalid Allocation Unit Size.\n");
Peng Fan3697e592016-09-01 11:13:38 +08001500 }
1501
1502 return 0;
1503}
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001504#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001505/* frequency bases */
1506/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001507static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001508 10000,
1509 100000,
1510 1000000,
1511 10000000,
1512};
1513
1514/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1515 * to platforms without floating point.
1516 */
Simon Glass61fe0762016-05-14 14:02:57 -06001517static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001518 0, /* reserved */
1519 10,
1520 12,
1521 13,
1522 15,
1523 20,
1524 25,
1525 30,
1526 35,
1527 40,
1528 45,
1529 50,
1530 55,
1531 60,
1532 70,
1533 80,
1534};
1535
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001536static inline int bus_width(uint cap)
1537{
1538 if (cap == MMC_MODE_8BIT)
1539 return 8;
1540 if (cap == MMC_MODE_4BIT)
1541 return 4;
1542 if (cap == MMC_MODE_1BIT)
1543 return 1;
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01001544 pr_warn("invalid bus witdh capability 0x%x\n", cap);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001545 return 0;
1546}
1547
Simon Glasse7881d82017-07-29 11:35:31 -06001548#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001549#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001550static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1551{
1552 return -ENOTSUPP;
1553}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001554#endif
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001555
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001556static int mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001557{
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001558 int ret = 0;
1559
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001560 if (mmc->cfg->ops->set_ios)
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001561 ret = mmc->cfg->ops->set_ios(mmc);
1562
1563 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001564}
Yann Gautier3602a562019-09-19 17:56:12 +02001565
1566static int mmc_host_power_cycle(struct mmc *mmc)
1567{
1568 int ret = 0;
1569
1570 if (mmc->cfg->ops->host_power_cycle)
1571 ret = mmc->cfg->ops->host_power_cycle(mmc);
1572
1573 return ret;
1574}
Simon Glass8ca51e52016-06-12 23:30:22 -06001575#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001576
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001577int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
Andy Fleming272cc702008-10-30 16:41:01 -05001578{
Jaehoon Chungc0fafe62018-01-23 14:04:30 +09001579 if (!disable) {
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001580 if (clock > mmc->cfg->f_max)
1581 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001582
Jaehoon Chung9546eb92018-01-17 19:36:58 +09001583 if (clock < mmc->cfg->f_min)
1584 clock = mmc->cfg->f_min;
1585 }
Andy Fleming272cc702008-10-30 16:41:01 -05001586
1587 mmc->clock = clock;
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001588 mmc->clk_disable = disable;
Andy Fleming272cc702008-10-30 16:41:01 -05001589
Jaehoon Chungd2faadb2018-01-26 19:25:30 +09001590 debug("clock is %s (%dHz)\n", disable ? "disabled" : "enabled", clock);
1591
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001592 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001593}
1594
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001595static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001596{
1597 mmc->bus_width = width;
1598
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001599 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001600}
1601
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001602#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1603/*
1604 * helper function to display the capabilities in a human
1605 * friendly manner. The capabilities include bus width and
1606 * supported modes.
1607 */
1608void mmc_dump_capabilities(const char *text, uint caps)
1609{
1610 enum bus_mode mode;
1611
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001612 pr_debug("%s: widths [", text);
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001613 if (caps & MMC_MODE_8BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001614 pr_debug("8, ");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001615 if (caps & MMC_MODE_4BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001616 pr_debug("4, ");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001617 if (caps & MMC_MODE_1BIT)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001618 pr_debug("1, ");
1619 pr_debug("\b\b] modes [");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001620 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1621 if (MMC_CAP(mode) & caps)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001622 pr_debug("%s, ", mmc_mode_name(mode));
1623 pr_debug("\b\b]\n");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001624}
1625#endif
1626
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001627struct mode_width_tuning {
1628 enum bus_mode mode;
1629 uint widths;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001630#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001631 uint tuning;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001632#endif
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001633};
1634
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001635#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001636int mmc_voltage_to_mv(enum mmc_voltage voltage)
1637{
1638 switch (voltage) {
1639 case MMC_SIGNAL_VOLTAGE_000: return 0;
1640 case MMC_SIGNAL_VOLTAGE_330: return 3300;
1641 case MMC_SIGNAL_VOLTAGE_180: return 1800;
1642 case MMC_SIGNAL_VOLTAGE_120: return 1200;
1643 }
1644 return -EINVAL;
1645}
1646
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001647static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1648{
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001649 int err;
1650
1651 if (mmc->signal_voltage == signal_voltage)
1652 return 0;
1653
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001654 mmc->signal_voltage = signal_voltage;
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001655 err = mmc_set_ios(mmc);
1656 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001657 pr_debug("unable to set voltage (err %d)\n", err);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001658
1659 return err;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001660}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001661#else
1662static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1663{
1664 return 0;
1665}
1666#endif
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001667
Marek Vasut62d77ce2018-04-15 00:37:11 +02001668#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001669static const struct mode_width_tuning sd_modes_by_pref[] = {
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001670#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1671#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001672 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001673 .mode = UHS_SDR104,
1674 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1675 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1676 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001677#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001678 {
1679 .mode = UHS_SDR50,
1680 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1681 },
1682 {
1683 .mode = UHS_DDR50,
1684 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1685 },
1686 {
1687 .mode = UHS_SDR25,
1688 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1689 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001690#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001691 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001692 .mode = SD_HS,
1693 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1694 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001695#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001696 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001697 .mode = UHS_SDR12,
1698 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1699 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001700#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001701 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001702 .mode = SD_LEGACY,
1703 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1704 }
1705};
1706
1707#define for_each_sd_mode_by_pref(caps, mwt) \
1708 for (mwt = sd_modes_by_pref;\
1709 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1710 mwt++) \
1711 if (caps & MMC_CAP(mwt->mode))
1712
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001713static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001714{
1715 int err;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001716 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1717 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001718#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001719 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001720#else
1721 bool uhs_en = false;
1722#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001723 uint caps;
1724
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001725#ifdef DEBUG
1726 mmc_dump_capabilities("sd card", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001727 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01001728#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001729
Anup Patelf49ff792019-07-08 04:10:43 +00001730 if (mmc_host_is_spi(mmc)) {
1731 mmc_set_bus_width(mmc, 1);
1732 mmc_select_mode(mmc, SD_LEGACY);
1733 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
1734 return 0;
1735 }
1736
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001737 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01001738 caps = card_caps & mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001739
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001740 if (!uhs_en)
1741 caps &= ~UHS_CAPS;
1742
1743 for_each_sd_mode_by_pref(caps, mwt) {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001744 uint *w;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001745
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001746 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001747 if (*w & caps & mwt->widths) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001748 pr_debug("trying mode %s width %d (at %d MHz)\n",
1749 mmc_mode_name(mwt->mode),
1750 bus_width(*w),
1751 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001752
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001753 /* configure the bus width (card + host) */
1754 err = sd_select_bus_width(mmc, bus_width(*w));
1755 if (err)
1756 goto error;
1757 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001758
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001759 /* configure the bus mode (card) */
1760 err = sd_set_card_speed(mmc, mwt->mode);
1761 if (err)
1762 goto error;
1763
1764 /* configure the bus mode (host) */
1765 mmc_select_mode(mmc, mwt->mode);
Jaehoon Chung65117182018-01-26 19:25:29 +09001766 mmc_set_clock(mmc, mmc->tran_speed,
1767 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001768
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001769#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001770 /* execute tuning if needed */
1771 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1772 err = mmc_execute_tuning(mmc,
1773 mwt->tuning);
1774 if (err) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001775 pr_debug("tuning failed\n");
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001776 goto error;
1777 }
1778 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001779#endif
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001780
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001781#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001782 err = sd_read_ssr(mmc);
Peng Fan0a4c2b02018-03-05 16:20:40 +08001783 if (err)
Jean-Jacques Hiblot5b2e72f2018-01-04 15:23:33 +01001784 pr_warn("unable to read ssr\n");
1785#endif
1786 if (!err)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001787 return 0;
1788
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001789error:
1790 /* revert to a safer bus speed */
1791 mmc_select_mode(mmc, SD_LEGACY);
Jaehoon Chung65117182018-01-26 19:25:29 +09001792 mmc_set_clock(mmc, mmc->tran_speed,
1793 MMC_CLK_ENABLE);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001794 }
1795 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001796 }
1797
Masahiro Yamadad4d64882018-01-28 19:11:42 +09001798 pr_err("unable to select a mode\n");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001799 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001800}
1801
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001802/*
1803 * read the compare the part of ext csd that is constant.
1804 * This can be used to check that the transfer is working
1805 * as expected.
1806 */
1807static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1808{
1809 int err;
1810 const u8 *ext_csd = mmc->ext_csd;
1811 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1812
Jean-Jacques Hiblot1de06b92017-11-30 17:43:58 +01001813 if (mmc->version < MMC_VERSION_4)
1814 return 0;
1815
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001816 err = mmc_send_ext_csd(mmc, test_csd);
1817 if (err)
1818 return err;
1819
1820 /* Only compare read only fields */
1821 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1822 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1823 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1824 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1825 ext_csd[EXT_CSD_REV]
1826 == test_csd[EXT_CSD_REV] &&
1827 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1828 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1829 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1830 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1831 return 0;
1832
1833 return -EBADMSG;
1834}
1835
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001836#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001837static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1838 uint32_t allowed_mask)
1839{
1840 u32 card_mask = 0;
1841
1842 switch (mode) {
Peng Fan44acd492019-07-10 14:43:07 +08001843 case MMC_HS_400_ES:
Peng Fan3dd26262018-08-10 14:07:54 +08001844 case MMC_HS_400:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001845 case MMC_HS_200:
Peng Fan3dd26262018-08-10 14:07:54 +08001846 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_8V |
1847 EXT_CSD_CARD_TYPE_HS400_1_8V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001848 card_mask |= MMC_SIGNAL_VOLTAGE_180;
Peng Fan3dd26262018-08-10 14:07:54 +08001849 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1850 EXT_CSD_CARD_TYPE_HS400_1_2V))
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001851 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1852 break;
1853 case MMC_DDR_52:
1854 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
1855 card_mask |= MMC_SIGNAL_VOLTAGE_330 |
1856 MMC_SIGNAL_VOLTAGE_180;
1857 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
1858 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1859 break;
1860 default:
1861 card_mask |= MMC_SIGNAL_VOLTAGE_330;
1862 break;
1863 }
1864
1865 while (card_mask & allowed_mask) {
1866 enum mmc_voltage best_match;
1867
1868 best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
1869 if (!mmc_set_signal_voltage(mmc, best_match))
1870 return 0;
1871
1872 allowed_mask &= ~best_match;
1873 }
1874
1875 return -ENOTSUPP;
1876}
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001877#else
1878static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1879 uint32_t allowed_mask)
1880{
1881 return 0;
1882}
1883#endif
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001884
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001885static const struct mode_width_tuning mmc_modes_by_pref[] = {
Peng Fan44acd492019-07-10 14:43:07 +08001886#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1887 {
1888 .mode = MMC_HS_400_ES,
1889 .widths = MMC_MODE_8BIT,
1890 },
1891#endif
Peng Fan3dd26262018-08-10 14:07:54 +08001892#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1893 {
1894 .mode = MMC_HS_400,
1895 .widths = MMC_MODE_8BIT,
1896 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
1897 },
1898#endif
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001899#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001900 {
1901 .mode = MMC_HS_200,
1902 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001903 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001904 },
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01001905#endif
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001906 {
1907 .mode = MMC_DDR_52,
1908 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1909 },
1910 {
1911 .mode = MMC_HS_52,
1912 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1913 },
1914 {
1915 .mode = MMC_HS,
1916 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1917 },
1918 {
1919 .mode = MMC_LEGACY,
1920 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1921 }
1922};
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001923
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001924#define for_each_mmc_mode_by_pref(caps, mwt) \
1925 for (mwt = mmc_modes_by_pref;\
1926 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
1927 mwt++) \
1928 if (caps & MMC_CAP(mwt->mode))
1929
1930static const struct ext_csd_bus_width {
1931 uint cap;
1932 bool is_ddr;
1933 uint ext_csd_bits;
1934} ext_csd_bus_width[] = {
1935 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
1936 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
1937 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
1938 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
1939 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
1940};
1941
Peng Fan3dd26262018-08-10 14:07:54 +08001942#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1943static int mmc_select_hs400(struct mmc *mmc)
1944{
1945 int err;
1946
1947 /* Set timing to HS200 for tuning */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01001948 err = mmc_set_card_speed(mmc, MMC_HS_200, false);
Peng Fan3dd26262018-08-10 14:07:54 +08001949 if (err)
1950 return err;
1951
1952 /* configure the bus mode (host) */
1953 mmc_select_mode(mmc, MMC_HS_200);
1954 mmc_set_clock(mmc, mmc->tran_speed, false);
1955
1956 /* execute tuning if needed */
1957 err = mmc_execute_tuning(mmc, MMC_CMD_SEND_TUNING_BLOCK_HS200);
1958 if (err) {
1959 debug("tuning failed\n");
1960 return err;
1961 }
1962
1963 /* Set back to HS */
BOUGH CHEN5cf12032019-03-26 06:24:17 +00001964 mmc_set_card_speed(mmc, MMC_HS, true);
Peng Fan3dd26262018-08-10 14:07:54 +08001965
1966 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1967 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG);
1968 if (err)
1969 return err;
1970
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01001971 err = mmc_set_card_speed(mmc, MMC_HS_400, false);
Peng Fan3dd26262018-08-10 14:07:54 +08001972 if (err)
1973 return err;
1974
1975 mmc_select_mode(mmc, MMC_HS_400);
1976 err = mmc_set_clock(mmc, mmc->tran_speed, false);
1977 if (err)
1978 return err;
1979
1980 return 0;
1981}
1982#else
1983static int mmc_select_hs400(struct mmc *mmc)
1984{
1985 return -ENOTSUPP;
1986}
1987#endif
1988
Peng Fan44acd492019-07-10 14:43:07 +08001989#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1990#if !CONFIG_IS_ENABLED(DM_MMC)
1991static int mmc_set_enhanced_strobe(struct mmc *mmc)
1992{
1993 return -ENOTSUPP;
1994}
1995#endif
1996static int mmc_select_hs400es(struct mmc *mmc)
1997{
1998 int err;
1999
2000 err = mmc_set_card_speed(mmc, MMC_HS, true);
2001 if (err)
2002 return err;
2003
2004 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2005 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG |
2006 EXT_CSD_BUS_WIDTH_STROBE);
2007 if (err) {
2008 printf("switch to bus width for hs400 failed\n");
2009 return err;
2010 }
2011 /* TODO: driver strength */
2012 err = mmc_set_card_speed(mmc, MMC_HS_400_ES, false);
2013 if (err)
2014 return err;
2015
2016 mmc_select_mode(mmc, MMC_HS_400_ES);
2017 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2018 if (err)
2019 return err;
2020
2021 return mmc_set_enhanced_strobe(mmc);
2022}
2023#else
2024static int mmc_select_hs400es(struct mmc *mmc)
2025{
2026 return -ENOTSUPP;
2027}
2028#endif
2029
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002030#define for_each_supported_width(caps, ddr, ecbv) \
2031 for (ecbv = ext_csd_bus_width;\
2032 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
2033 ecbv++) \
2034 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
2035
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002036static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002037{
2038 int err;
2039 const struct mode_width_tuning *mwt;
2040 const struct ext_csd_bus_width *ecbw;
2041
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002042#ifdef DEBUG
2043 mmc_dump_capabilities("mmc", card_caps);
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002044 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +01002045#endif
2046
Anup Patelf49ff792019-07-08 04:10:43 +00002047 if (mmc_host_is_spi(mmc)) {
2048 mmc_set_bus_width(mmc, 1);
2049 mmc_select_mode(mmc, MMC_LEGACY);
2050 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
2051 return 0;
2052 }
2053
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002054 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot1da8eb52017-11-30 17:43:57 +01002055 card_caps &= mmc->host_caps;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002056
2057 /* Only version 4 of MMC supports wider bus widths */
2058 if (mmc->version < MMC_VERSION_4)
2059 return 0;
2060
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002061 if (!mmc->ext_csd) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002062 pr_debug("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002063 return -ENOTSUPP;
2064 }
2065
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002066#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
2067 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2068 /*
2069 * In case the eMMC is in HS200/HS400 mode, downgrade to HS mode
2070 * before doing anything else, since a transition from either of
2071 * the HS200/HS400 mode directly to legacy mode is not supported.
2072 */
2073 if (mmc->selected_mode == MMC_HS_200 ||
2074 mmc->selected_mode == MMC_HS_400)
2075 mmc_set_card_speed(mmc, MMC_HS, true);
2076 else
2077#endif
2078 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002079
2080 for_each_mmc_mode_by_pref(card_caps, mwt) {
2081 for_each_supported_width(card_caps & mwt->widths,
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002082 mmc_is_mode_ddr(mwt->mode), ecbw) {
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002083 enum mmc_voltage old_voltage;
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002084 pr_debug("trying mode %s width %d (at %d MHz)\n",
2085 mmc_mode_name(mwt->mode),
2086 bus_width(ecbw->cap),
2087 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002088 old_voltage = mmc->signal_voltage;
2089 err = mmc_set_lowest_voltage(mmc, mwt->mode,
2090 MMC_ALL_SIGNAL_VOLTAGE);
2091 if (err)
2092 continue;
2093
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002094 /* configure the bus width (card + host) */
2095 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2096 EXT_CSD_BUS_WIDTH,
2097 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
2098 if (err)
2099 goto error;
2100 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
2101
Peng Fan3dd26262018-08-10 14:07:54 +08002102 if (mwt->mode == MMC_HS_400) {
2103 err = mmc_select_hs400(mmc);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002104 if (err) {
Peng Fan3dd26262018-08-10 14:07:54 +08002105 printf("Select HS400 failed %d\n", err);
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002106 goto error;
2107 }
Peng Fan44acd492019-07-10 14:43:07 +08002108 } else if (mwt->mode == MMC_HS_400_ES) {
2109 err = mmc_select_hs400es(mmc);
2110 if (err) {
2111 printf("Select HS400ES failed %d\n",
2112 err);
2113 goto error;
2114 }
Peng Fan3dd26262018-08-10 14:07:54 +08002115 } else {
2116 /* configure the bus speed (card) */
Marek Vasutb9a2a0e2019-01-03 21:19:24 +01002117 err = mmc_set_card_speed(mmc, mwt->mode, false);
Peng Fan3dd26262018-08-10 14:07:54 +08002118 if (err)
2119 goto error;
2120
2121 /*
2122 * configure the bus width AND the ddr mode
2123 * (card). The host side will be taken care
2124 * of in the next step
2125 */
2126 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
2127 err = mmc_switch(mmc,
2128 EXT_CSD_CMD_SET_NORMAL,
2129 EXT_CSD_BUS_WIDTH,
2130 ecbw->ext_csd_bits);
2131 if (err)
2132 goto error;
2133 }
2134
2135 /* configure the bus mode (host) */
2136 mmc_select_mode(mmc, mwt->mode);
2137 mmc_set_clock(mmc, mmc->tran_speed,
2138 MMC_CLK_ENABLE);
2139#ifdef MMC_SUPPORTS_TUNING
2140
2141 /* execute tuning if needed */
2142 if (mwt->tuning) {
2143 err = mmc_execute_tuning(mmc,
2144 mwt->tuning);
2145 if (err) {
2146 pr_debug("tuning failed\n");
2147 goto error;
2148 }
2149 }
Jean-Jacques Hiblotf99c2ef2017-11-30 17:44:01 +01002150#endif
Peng Fan3dd26262018-08-10 14:07:54 +08002151 }
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02002152
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002153 /* do a transfer to check the configuration */
2154 err = mmc_read_and_compare_ext_csd(mmc);
2155 if (!err)
2156 return 0;
2157error:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02002158 mmc_set_signal_voltage(mmc, old_voltage);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002159 /* if an error occured, revert to a safer bus mode */
2160 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2161 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
2162 mmc_select_mode(mmc, MMC_LEGACY);
2163 mmc_set_bus_width(mmc, 1);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002164 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002165 }
2166
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002167 pr_err("unable to select a mode\n");
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002168
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02002169 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002170}
Marek Vasut62d77ce2018-04-15 00:37:11 +02002171#endif
2172
2173#if CONFIG_IS_ENABLED(MMC_TINY)
2174DEFINE_CACHE_ALIGN_BUFFER(u8, ext_csd_bkup, MMC_MAX_BLOCK_LEN);
2175#endif
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02002176
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002177static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002178{
2179 int err, i;
2180 u64 capacity;
2181 bool has_parts = false;
2182 bool part_completed;
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002183 static const u32 mmc_versions[] = {
2184 MMC_VERSION_4,
2185 MMC_VERSION_4_1,
2186 MMC_VERSION_4_2,
2187 MMC_VERSION_4_3,
Jean-Jacques Hiblotace1bed2018-02-09 12:09:28 +01002188 MMC_VERSION_4_4,
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002189 MMC_VERSION_4_41,
2190 MMC_VERSION_4_5,
2191 MMC_VERSION_5_0,
2192 MMC_VERSION_5_1
2193 };
2194
Marek Vasut62d77ce2018-04-15 00:37:11 +02002195#if CONFIG_IS_ENABLED(MMC_TINY)
2196 u8 *ext_csd = ext_csd_bkup;
2197
2198 if (IS_SD(mmc) || mmc->version < MMC_VERSION_4)
2199 return 0;
2200
2201 if (!mmc->ext_csd)
2202 memset(ext_csd_bkup, 0, sizeof(ext_csd_bkup));
2203
2204 err = mmc_send_ext_csd(mmc, ext_csd);
2205 if (err)
2206 goto error;
2207
2208 /* store the ext csd for future reference */
2209 if (!mmc->ext_csd)
2210 mmc->ext_csd = ext_csd;
2211#else
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002212 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002213
2214 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
2215 return 0;
2216
2217 /* check ext_csd version and capacity */
2218 err = mmc_send_ext_csd(mmc, ext_csd);
2219 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002220 goto error;
2221
2222 /* store the ext csd for future reference */
2223 if (!mmc->ext_csd)
2224 mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN);
2225 if (!mmc->ext_csd)
2226 return -ENOMEM;
2227 memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002228#endif
Alexander Kochetkov76584e32018-02-20 14:35:55 +03002229 if (ext_csd[EXT_CSD_REV] >= ARRAY_SIZE(mmc_versions))
Jean-Jacques Hiblot58a6fb72018-01-04 15:23:31 +01002230 return -EINVAL;
2231
2232 mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]];
2233
2234 if (mmc->version >= MMC_VERSION_4_2) {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002235 /*
2236 * According to the JEDEC Standard, the value of
2237 * ext_csd's capacity is valid if the value is more
2238 * than 2GB
2239 */
2240 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
2241 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
2242 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
2243 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2244 capacity *= MMC_MAX_BLOCK_LEN;
2245 if ((capacity >> 20) > 2 * 1024)
2246 mmc->capacity_user = capacity;
2247 }
2248
Jean-Jacques Hiblot39320c52019-07-02 10:53:54 +02002249 if (mmc->version >= MMC_VERSION_4_5)
2250 mmc->gen_cmd6_time = ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
2251
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002252 /* The partition data may be non-zero but it is only
2253 * effective if PARTITION_SETTING_COMPLETED is set in
2254 * EXT_CSD, so ignore any data if this bit is not set,
2255 * except for enabling the high-capacity group size
2256 * definition (see below).
2257 */
2258 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
2259 EXT_CSD_PARTITION_SETTING_COMPLETED);
2260
Jean-Jacques Hiblot513e00b2019-07-02 10:53:55 +02002261 mmc->part_switch_time = ext_csd[EXT_CSD_PART_SWITCH_TIME];
2262 /* Some eMMC set the value too low so set a minimum */
2263 if (mmc->part_switch_time < MMC_MIN_PART_SWITCH_TIME && mmc->part_switch_time)
2264 mmc->part_switch_time = MMC_MIN_PART_SWITCH_TIME;
2265
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002266 /* store the partition info of emmc */
2267 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
2268 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
2269 ext_csd[EXT_CSD_BOOT_MULT])
2270 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
2271 if (part_completed &&
2272 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
2273 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
2274
2275 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
2276
2277 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
2278
2279 for (i = 0; i < 4; i++) {
2280 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
2281 uint mult = (ext_csd[idx + 2] << 16) +
2282 (ext_csd[idx + 1] << 8) + ext_csd[idx];
2283 if (mult)
2284 has_parts = true;
2285 if (!part_completed)
2286 continue;
2287 mmc->capacity_gp[i] = mult;
2288 mmc->capacity_gp[i] *=
2289 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2290 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2291 mmc->capacity_gp[i] <<= 19;
2292 }
2293
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002294#ifndef CONFIG_SPL_BUILD
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002295 if (part_completed) {
2296 mmc->enh_user_size =
2297 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
2298 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
2299 ext_csd[EXT_CSD_ENH_SIZE_MULT];
2300 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2301 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2302 mmc->enh_user_size <<= 19;
2303 mmc->enh_user_start =
2304 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
2305 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
2306 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
2307 ext_csd[EXT_CSD_ENH_START_ADDR];
2308 if (mmc->high_capacity)
2309 mmc->enh_user_start <<= 9;
2310 }
Jean-Jacques Hiblot173c06d2018-01-04 15:23:35 +01002311#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002312
2313 /*
2314 * Host needs to enable ERASE_GRP_DEF bit if device is
2315 * partitioned. This bit will be lost every time after a reset
2316 * or power off. This will affect erase size.
2317 */
2318 if (part_completed)
2319 has_parts = true;
2320 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
2321 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
2322 has_parts = true;
2323 if (has_parts) {
2324 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2325 EXT_CSD_ERASE_GROUP_DEF, 1);
2326
2327 if (err)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002328 goto error;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002329
2330 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
2331 }
2332
2333 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002334#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002335 /* Read out group size from ext_csd */
2336 mmc->erase_grp_size =
2337 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002338#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002339 /*
2340 * if high capacity and partition setting completed
2341 * SEC_COUNT is valid even if it is smaller than 2 GiB
2342 * JEDEC Standard JESD84-B45, 6.2.4
2343 */
2344 if (mmc->high_capacity && part_completed) {
2345 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
2346 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
2347 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
2348 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
2349 capacity *= MMC_MAX_BLOCK_LEN;
2350 mmc->capacity_user = capacity;
2351 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002352 }
2353#if CONFIG_IS_ENABLED(MMC_WRITE)
2354 else {
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002355 /* Calculate the group size from the csd value. */
2356 int erase_gsz, erase_gmul;
2357
2358 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
2359 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
2360 mmc->erase_grp_size = (erase_gsz + 1)
2361 * (erase_gmul + 1);
2362 }
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002363#endif
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002364#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002365 mmc->hc_wp_grp_size = 1024
2366 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
2367 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +01002368#endif
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002369
2370 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
2371
2372 return 0;
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002373error:
2374 if (mmc->ext_csd) {
Marek Vasut62d77ce2018-04-15 00:37:11 +02002375#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002376 free(mmc->ext_csd);
Marek Vasut62d77ce2018-04-15 00:37:11 +02002377#endif
Jean-Jacques Hiblotf7d5dff2017-11-30 17:43:59 +01002378 mmc->ext_csd = NULL;
2379 }
2380 return err;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002381}
2382
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002383static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002384{
Stephen Warrenf866a462013-06-11 15:14:01 -06002385 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05002386 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002387 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05002388 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06002389 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05002390
Thomas Choud52ebf12010-12-24 13:12:21 +00002391#ifdef CONFIG_MMC_SPI_CRC_ON
2392 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
2393 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
2394 cmd.resp_type = MMC_RSP_R1;
2395 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002396 err = mmc_send_cmd(mmc, &cmd, NULL);
Thomas Choud52ebf12010-12-24 13:12:21 +00002397 if (err)
2398 return err;
2399 }
2400#endif
2401
Andy Fleming272cc702008-10-30 16:41:01 -05002402 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002403 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
2404 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05002405 cmd.resp_type = MMC_RSP_R2;
2406 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002407
2408 err = mmc_send_cmd(mmc, &cmd, NULL);
2409
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002410#ifdef CONFIG_MMC_QUIRKS
2411 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SEND_CID)) {
2412 int retries = 4;
2413 /*
2414 * It has been seen that SEND_CID may fail on the first
2415 * attempt, let's try a few more time
2416 */
2417 do {
2418 err = mmc_send_cmd(mmc, &cmd, NULL);
2419 if (!err)
2420 break;
2421 } while (retries--);
2422 }
2423#endif
2424
Andy Fleming272cc702008-10-30 16:41:01 -05002425 if (err)
2426 return err;
2427
2428 memcpy(mmc->cid, cmd.response, 16);
2429
2430 /*
2431 * For MMC cards, set the Relative Address.
2432 * For SD cards, get the Relatvie Address.
2433 * This also puts the cards into Standby State
2434 */
Thomas Choud52ebf12010-12-24 13:12:21 +00002435 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2436 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
2437 cmd.cmdarg = mmc->rca << 16;
2438 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05002439
Thomas Choud52ebf12010-12-24 13:12:21 +00002440 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002441
Thomas Choud52ebf12010-12-24 13:12:21 +00002442 if (err)
2443 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002444
Thomas Choud52ebf12010-12-24 13:12:21 +00002445 if (IS_SD(mmc))
2446 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
2447 }
Andy Fleming272cc702008-10-30 16:41:01 -05002448
2449 /* Get the Card-Specific Data */
2450 cmd.cmdidx = MMC_CMD_SEND_CSD;
2451 cmd.resp_type = MMC_RSP_R2;
2452 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05002453
2454 err = mmc_send_cmd(mmc, &cmd, NULL);
2455
2456 if (err)
2457 return err;
2458
Rabin Vincent998be3d2009-04-05 13:30:56 +05302459 mmc->csd[0] = cmd.response[0];
2460 mmc->csd[1] = cmd.response[1];
2461 mmc->csd[2] = cmd.response[2];
2462 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05002463
2464 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302465 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05002466
2467 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07002468 case 0:
2469 mmc->version = MMC_VERSION_1_2;
2470 break;
2471 case 1:
2472 mmc->version = MMC_VERSION_1_4;
2473 break;
2474 case 2:
2475 mmc->version = MMC_VERSION_2_2;
2476 break;
2477 case 3:
2478 mmc->version = MMC_VERSION_3;
2479 break;
2480 case 4:
2481 mmc->version = MMC_VERSION_4;
2482 break;
2483 default:
2484 mmc->version = MMC_VERSION_1_2;
2485 break;
Andy Fleming272cc702008-10-30 16:41:01 -05002486 }
2487 }
2488
2489 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302490 freq = fbase[(cmd.response[0] & 0x7)];
2491 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05002492
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002493 mmc->legacy_speed = freq * mult;
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002494 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05002495
Markus Niebelab711882013-12-16 13:40:46 +01002496 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05302497 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002498#if CONFIG_IS_ENABLED(MMC_WRITE)
Andy Fleming272cc702008-10-30 16:41:01 -05002499
2500 if (IS_SD(mmc))
2501 mmc->write_bl_len = mmc->read_bl_len;
2502 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05302503 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002504#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002505
2506 if (mmc->high_capacity) {
2507 csize = (mmc->csd[1] & 0x3f) << 16
2508 | (mmc->csd[2] & 0xffff0000) >> 16;
2509 cmult = 8;
2510 } else {
2511 csize = (mmc->csd[1] & 0x3ff) << 2
2512 | (mmc->csd[2] & 0xc0000000) >> 30;
2513 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2514 }
2515
Stephen Warrenf866a462013-06-11 15:14:01 -06002516 mmc->capacity_user = (csize + 1) << (cmult + 2);
2517 mmc->capacity_user *= mmc->read_bl_len;
2518 mmc->capacity_boot = 0;
2519 mmc->capacity_rpmb = 0;
2520 for (i = 0; i < 4; i++)
2521 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002522
Simon Glass8bfa1952013-04-03 08:54:30 +00002523 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2524 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05002525
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002526#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass8bfa1952013-04-03 08:54:30 +00002527 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2528 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002529#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002530
Markus Niebelab711882013-12-16 13:40:46 +01002531 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2532 cmd.cmdidx = MMC_CMD_SET_DSR;
2533 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2534 cmd.resp_type = MMC_RSP_NONE;
2535 if (mmc_send_cmd(mmc, &cmd, NULL))
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002536 pr_warn("MMC: SET_DSR failed\n");
Markus Niebelab711882013-12-16 13:40:46 +01002537 }
2538
Andy Fleming272cc702008-10-30 16:41:01 -05002539 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002540 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2541 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00002542 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002543 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00002544 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002545
Thomas Choud52ebf12010-12-24 13:12:21 +00002546 if (err)
2547 return err;
2548 }
Andy Fleming272cc702008-10-30 16:41:01 -05002549
Lei Wene6f99a52011-06-22 17:03:31 +00002550 /*
2551 * For SD, its erase group is always one sector
2552 */
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002553#if CONFIG_IS_ENABLED(MMC_WRITE)
Lei Wene6f99a52011-06-22 17:03:31 +00002554 mmc->erase_grp_size = 1;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002555#endif
Lei Wenbc897b12011-05-02 16:26:26 +00002556 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00002557
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002558 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002559 if (err)
2560 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05302561
Simon Glassc40fdca2016-05-01 13:52:35 -06002562 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06002563 if (err)
2564 return err;
2565
Marek Vasut62d77ce2018-04-15 00:37:11 +02002566#if CONFIG_IS_ENABLED(MMC_TINY)
2567 mmc_set_clock(mmc, mmc->legacy_speed, false);
2568 mmc_select_mode(mmc, IS_SD(mmc) ? SD_LEGACY : MMC_LEGACY);
2569 mmc_set_bus_width(mmc, 1);
2570#else
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002571 if (IS_SD(mmc)) {
2572 err = sd_get_capabilities(mmc);
2573 if (err)
2574 return err;
2575 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2576 } else {
2577 err = mmc_get_capabilities(mmc);
2578 if (err)
2579 return err;
Masahiro Yamada8adf50e2020-01-23 14:31:12 +09002580 err = mmc_select_mode_and_width(mmc, mmc->card_caps);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002581 }
Marek Vasut62d77ce2018-04-15 00:37:11 +02002582#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002583 if (err)
2584 return err;
2585
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002586 mmc->best_mode = mmc->selected_mode;
Jaehoon Chungad5fd922012-03-26 21:16:03 +00002587
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002588 /* Fix the block length for DDR mode */
2589 if (mmc->ddr_mode) {
2590 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002591#if CONFIG_IS_ENABLED(MMC_WRITE)
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002592 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01002593#endif
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002594 }
2595
Andy Fleming272cc702008-10-30 16:41:01 -05002596 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06002597 bdesc = mmc_get_blk_desc(mmc);
2598 bdesc->lun = 0;
2599 bdesc->hwpart = 0;
2600 bdesc->type = 0;
2601 bdesc->blksz = mmc->read_bl_len;
2602 bdesc->log2blksz = LOG2(bdesc->blksz);
2603 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01002604#if !defined(CONFIG_SPL_BUILD) || \
2605 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
Simon Glass27084c02019-09-25 08:56:27 -06002606 !CONFIG_IS_ENABLED(USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06002607 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00002608 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2609 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002610 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002611 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2612 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2613 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002614 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002615 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01002616#else
Simon Glassc40fdca2016-05-01 13:52:35 -06002617 bdesc->vendor[0] = 0;
2618 bdesc->product[0] = 0;
2619 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01002620#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002621
Andre Przywaraeef05fd2018-12-17 10:05:45 +00002622#if !defined(CONFIG_DM_MMC) && (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT))
2623 part_init(bdesc);
2624#endif
2625
Andy Fleming272cc702008-10-30 16:41:01 -05002626 return 0;
2627}
2628
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002629static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002630{
2631 struct mmc_cmd cmd;
2632 int err;
2633
2634 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2635 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002636 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05002637 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05002638
2639 err = mmc_send_cmd(mmc, &cmd, NULL);
2640
2641 if (err)
2642 return err;
2643
Rabin Vincent998be3d2009-04-05 13:30:56 +05302644 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002645 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002646 else
2647 mmc->version = SD_VERSION_2;
2648
2649 return 0;
2650}
2651
Simon Glassc4d660d2017-07-04 13:31:19 -06002652#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002653/* board-specific MMC power initializations. */
2654__weak void board_mmc_power_init(void)
2655{
2656}
Simon Glass05cbeb72017-04-22 19:10:56 -06002657#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002658
Peng Fan2051aef2016-10-11 15:08:43 +08002659static int mmc_power_init(struct mmc *mmc)
2660{
Simon Glassc4d660d2017-07-04 13:31:19 -06002661#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002662#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08002663 int ret;
2664
2665 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002666 &mmc->vmmc_supply);
2667 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002668 pr_debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002669
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002670 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2671 &mmc->vqmmc_supply);
2672 if (ret)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002673 pr_debug("%s: No vqmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002674#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06002675#else /* !CONFIG_DM_MMC */
2676 /*
2677 * Driver model should use a regulator, as above, rather than calling
2678 * out to board code.
2679 */
2680 board_mmc_power_init();
2681#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002682 return 0;
2683}
2684
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002685/*
2686 * put the host in the initial state:
2687 * - turn on Vdd (card power supply)
2688 * - configure the bus width and clock to minimal values
2689 */
2690static void mmc_set_initial_state(struct mmc *mmc)
2691{
2692 int err;
2693
2694 /* First try to set 3.3V. If it fails set to 1.8V */
2695 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2696 if (err != 0)
2697 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2698 if (err != 0)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002699 pr_warn("mmc: failed to set signal voltage\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002700
2701 mmc_select_mode(mmc, MMC_LEGACY);
2702 mmc_set_bus_width(mmc, 1);
Jaehoon Chung65117182018-01-26 19:25:29 +09002703 mmc_set_clock(mmc, 0, MMC_CLK_ENABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002704}
2705
2706static int mmc_power_on(struct mmc *mmc)
2707{
2708#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2709 if (mmc->vmmc_supply) {
2710 int ret = regulator_set_enable(mmc->vmmc_supply, true);
2711
2712 if (ret) {
2713 puts("Error enabling VMMC supply\n");
2714 return ret;
2715 }
2716 }
2717#endif
2718 return 0;
2719}
2720
2721static int mmc_power_off(struct mmc *mmc)
2722{
Jaehoon Chung65117182018-01-26 19:25:29 +09002723 mmc_set_clock(mmc, 0, MMC_CLK_DISABLE);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002724#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2725 if (mmc->vmmc_supply) {
2726 int ret = regulator_set_enable(mmc->vmmc_supply, false);
2727
2728 if (ret) {
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002729 pr_debug("Error disabling VMMC supply\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002730 return ret;
2731 }
2732 }
2733#endif
2734 return 0;
2735}
2736
2737static int mmc_power_cycle(struct mmc *mmc)
2738{
2739 int ret;
2740
2741 ret = mmc_power_off(mmc);
2742 if (ret)
2743 return ret;
Yann Gautier3602a562019-09-19 17:56:12 +02002744
2745 ret = mmc_host_power_cycle(mmc);
2746 if (ret)
2747 return ret;
2748
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002749 /*
2750 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2751 * to be on the safer side.
2752 */
2753 udelay(2000);
2754 return mmc_power_on(mmc);
2755}
2756
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002757int mmc_get_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002758{
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002759 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
Macpaul Linafd59322011-11-14 23:35:39 +00002760 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05002761
Lei Wenbc897b12011-05-02 16:26:26 +00002762 if (mmc->has_init)
2763 return 0;
2764
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08002765#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
2766 mmc_adapter_card_type_ident();
2767#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002768 err = mmc_power_init(mmc);
2769 if (err)
2770 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002771
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002772#ifdef CONFIG_MMC_QUIRKS
2773 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
Joel Johnsond4a5fa32020-01-11 09:08:14 -07002774 MMC_QUIRK_RETRY_SEND_CID |
2775 MMC_QUIRK_RETRY_APP_CMD;
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002776#endif
2777
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002778 err = mmc_power_cycle(mmc);
2779 if (err) {
2780 /*
2781 * if power cycling is not supported, we should not try
2782 * to use the UHS modes, because we wouldn't be able to
2783 * recover from an error during the UHS initialization.
2784 */
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002785 pr_debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002786 uhs_en = false;
2787 mmc->host_caps &= ~UHS_CAPS;
2788 err = mmc_power_on(mmc);
2789 }
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002790 if (err)
2791 return err;
2792
Simon Glasse7881d82017-07-29 11:35:31 -06002793#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06002794 /* The device has already been probed ready for use */
2795#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02002796 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002797 err = mmc->cfg->ops->init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002798 if (err)
2799 return err;
Simon Glass8ca51e52016-06-12 23:30:22 -06002800#endif
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06002801 mmc->ddr_mode = 0;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02002802
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002803retry:
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002804 mmc_set_initial_state(mmc);
Jean-Jacques Hiblot318a7a52017-09-21 16:30:01 +02002805
Andy Fleming272cc702008-10-30 16:41:01 -05002806 /* Reset the Card */
2807 err = mmc_go_idle(mmc);
2808
2809 if (err)
2810 return err;
2811
Lei Wenbc897b12011-05-02 16:26:26 +00002812 /* The internal partition reset to user partition(0) at every CMD0*/
Simon Glassc40fdca2016-05-01 13:52:35 -06002813 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00002814
Andy Fleming272cc702008-10-30 16:41:01 -05002815 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00002816 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002817
Andy Fleming272cc702008-10-30 16:41:01 -05002818 /* Now try to get the SD card's operating condition */
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002819 err = sd_send_op_cond(mmc, uhs_en);
2820 if (err && uhs_en) {
2821 uhs_en = false;
2822 mmc_power_cycle(mmc);
2823 goto retry;
2824 }
Andy Fleming272cc702008-10-30 16:41:01 -05002825
2826 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002827 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05002828 err = mmc_send_op_cond(mmc);
2829
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002830 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01002831#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002832 pr_err("Card did not respond to voltage select!\n");
Paul Burton56196822013-09-04 16:12:25 +01002833#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002834 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002835 }
2836 }
2837
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002838 return err;
2839}
2840
2841int mmc_start_init(struct mmc *mmc)
2842{
2843 bool no_card;
2844 int err = 0;
2845
2846 /*
2847 * all hosts are capable of 1 bit bus-width and able to use the legacy
2848 * timings.
2849 */
2850 mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(SD_LEGACY) |
2851 MMC_CAP(MMC_LEGACY) | MMC_MODE_1BIT;
2852
2853#if !defined(CONFIG_MMC_BROKEN_CD)
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002854 no_card = mmc_getcd(mmc) == 0;
2855#else
2856 no_card = 0;
2857#endif
2858#if !CONFIG_IS_ENABLED(DM_MMC)
Baruch Siachfea39392019-07-22 15:52:12 +03002859 /* we pretend there's no card when init is NULL */
Jon Nettleton6c09eba2018-06-11 15:26:19 +03002860 no_card = no_card || (mmc->cfg->ops->init == NULL);
2861#endif
2862 if (no_card) {
2863 mmc->has_init = 0;
2864#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2865 pr_err("MMC: no card present\n");
2866#endif
2867 return -ENOMEDIUM;
2868 }
2869
2870 err = mmc_get_op_cond(mmc);
2871
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002872 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002873 mmc->init_in_progress = 1;
2874
2875 return err;
2876}
2877
2878static int mmc_complete_init(struct mmc *mmc)
2879{
2880 int err = 0;
2881
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002882 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002883 if (mmc->op_cond_pending)
2884 err = mmc_complete_op_cond(mmc);
2885
2886 if (!err)
2887 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00002888 if (err)
2889 mmc->has_init = 0;
2890 else
2891 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002892 return err;
2893}
2894
2895int mmc_init(struct mmc *mmc)
2896{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002897 int err = 0;
Vipul Kumar36332b62018-05-03 12:20:54 +05302898 __maybe_unused ulong start;
Simon Glassc4d660d2017-07-04 13:31:19 -06002899#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06002900 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00002901
Simon Glass33fb2112016-05-01 13:52:41 -06002902 upriv->mmc = mmc;
2903#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00002904 if (mmc->has_init)
2905 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02002906
2907 start = get_timer(0);
2908
Che-Liang Chioue9550442012-11-28 15:21:13 +00002909 if (!mmc->init_in_progress)
2910 err = mmc_start_init(mmc);
2911
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002912 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002913 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01002914 if (err)
Masahiro Yamadad4d64882018-01-28 19:11:42 +09002915 pr_info("%s: %d, time %lu\n", __func__, err, get_timer(start));
Jagan Teki919b4852017-01-10 11:18:43 +01002916
Lei Wenbc897b12011-05-02 16:26:26 +00002917 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002918}
2919
Marek Vasutfceea992019-01-29 04:45:51 +01002920#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
2921 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
2922 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2923int mmc_deinit(struct mmc *mmc)
2924{
2925 u32 caps_filtered;
2926
2927 if (!mmc->has_init)
2928 return 0;
2929
2930 if (IS_SD(mmc)) {
2931 caps_filtered = mmc->card_caps &
2932 ~(MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25) |
2933 MMC_CAP(UHS_SDR50) | MMC_CAP(UHS_DDR50) |
2934 MMC_CAP(UHS_SDR104));
2935
2936 return sd_select_mode_and_width(mmc, caps_filtered);
2937 } else {
2938 caps_filtered = mmc->card_caps &
2939 ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_HS_400));
2940
2941 return mmc_select_mode_and_width(mmc, caps_filtered);
2942 }
2943}
2944#endif
2945
Markus Niebelab711882013-12-16 13:40:46 +01002946int mmc_set_dsr(struct mmc *mmc, u16 val)
2947{
2948 mmc->dsr = val;
2949 return 0;
2950}
2951
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002952/* CPU-specific MMC initializations */
2953__weak int cpu_mmc_init(bd_t *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05002954{
2955 return -1;
2956}
2957
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002958/* board-specific MMC initializations. */
2959__weak int board_mmc_init(bd_t *bis)
2960{
2961 return -1;
2962}
Andy Fleming272cc702008-10-30 16:41:01 -05002963
Che-Liang Chioue9550442012-11-28 15:21:13 +00002964void mmc_set_preinit(struct mmc *mmc, int preinit)
2965{
2966 mmc->preinit = preinit;
2967}
2968
Faiz Abbas8a856db2018-02-12 19:35:24 +05302969#if CONFIG_IS_ENABLED(DM_MMC)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002970static int mmc_probe(bd_t *bis)
2971{
Simon Glass4a1db6d2015-12-29 05:22:49 -07002972 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002973 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07002974 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002975
2976 ret = uclass_get(UCLASS_MMC, &uc);
2977 if (ret)
2978 return ret;
2979
Simon Glass4a1db6d2015-12-29 05:22:49 -07002980 /*
2981 * Try to add them in sequence order. Really with driver model we
2982 * should allow holes, but the current MMC list does not allow that.
2983 * So if we request 0, 1, 3 we will get 0, 1, 2.
2984 */
2985 for (i = 0; ; i++) {
2986 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
2987 if (ret == -ENODEV)
2988 break;
2989 }
2990 uclass_foreach_dev(dev, uc) {
2991 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002992 if (ret)
Jean-Jacques Hiblotd8e3d422017-11-30 17:44:00 +01002993 pr_err("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002994 }
2995
2996 return 0;
2997}
2998#else
2999static int mmc_probe(bd_t *bis)
3000{
3001 if (board_mmc_init(bis) < 0)
3002 cpu_mmc_init(bis);
3003
3004 return 0;
3005}
3006#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00003007
Andy Fleming272cc702008-10-30 16:41:01 -05003008int mmc_initialize(bd_t *bis)
3009{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003010 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003011 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02003012 if (initialized) /* Avoid initializing mmc multiple times */
3013 return 0;
3014 initialized = 1;
3015
Simon Glassc4d660d2017-07-04 13:31:19 -06003016#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01003017#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06003018 mmc_list_init();
3019#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01003020#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06003021 ret = mmc_probe(bis);
3022 if (ret)
3023 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05003024
Ying Zhangbb0dc102013-08-16 15:16:11 +08003025#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05003026 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08003027#endif
Andy Fleming272cc702008-10-30 16:41:01 -05003028
Simon Glassc40fdca2016-05-01 13:52:35 -06003029 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05003030 return 0;
3031}
Tomas Melincd3d4882016-11-25 11:01:03 +02003032
Lokesh Vutla80f02012019-09-09 14:40:36 +05303033#if CONFIG_IS_ENABLED(DM_MMC)
3034int mmc_init_device(int num)
3035{
3036 struct udevice *dev;
3037 struct mmc *m;
3038 int ret;
3039
3040 ret = uclass_get_device(UCLASS_MMC, num, &dev);
3041 if (ret)
3042 return ret;
3043
3044 m = mmc_get_mmc_dev(dev);
3045 if (!m)
3046 return 0;
3047#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
3048 mmc_set_preinit(m, 1);
3049#endif
3050 if (m->preinit)
3051 mmc_start_init(m);
3052
3053 return 0;
3054}
3055#endif
3056
Tomas Melincd3d4882016-11-25 11:01:03 +02003057#ifdef CONFIG_CMD_BKOPS_ENABLE
3058int mmc_set_bkops_enable(struct mmc *mmc)
3059{
3060 int err;
3061 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
3062
3063 err = mmc_send_ext_csd(mmc, ext_csd);
3064 if (err) {
3065 puts("Could not get ext_csd register values\n");
3066 return err;
3067 }
3068
3069 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
3070 puts("Background operations not supported on device\n");
3071 return -EMEDIUMTYPE;
3072 }
3073
3074 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
3075 puts("Background operations already enabled\n");
3076 return 0;
3077 }
3078
3079 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
3080 if (err) {
3081 puts("Failed to enable manual background operations\n");
3082 return err;
3083 }
3084
3085 puts("Enabled manual background operations\n");
3086
3087 return 0;
3088}
3089#endif