blob: df550e25edc1d56d588307f79b2e484fadda621a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Flemingad347bb2008-10-30 16:41:01 -05002/*
3 * Copyright 2008, Freescale Semiconductor, Inc
4 * Andy Fleming
5 *
6 * Based vaguely on the Linux code
Andy Flemingad347bb2008-10-30 16:41:01 -05007 */
8
9#include <config.h>
10#include <common.h>
Simon Glass655306c2020-05-10 11:39:58 -060011#include <blk.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050012#include <command.h>
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -060013#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -060015#include <dm/device-internal.h>
Stephen Warrenbf0c7852014-05-23 12:47:06 -060016#include <errno.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050017#include <mmc.h>
18#include <part.h>
Peng Fan15305962016-10-11 15:08:43 +080019#include <power/regulator.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050020#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060021#include <memalign.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050022#include <linux/list.h>
Rabin Vincent69d4e2c2009-04-05 13:30:54 +053023#include <div64.h>
Paul Burton8d30cc92013-09-09 15:30:26 +010024#include "mmc_private.h"
Andy Flemingad347bb2008-10-30 16:41:01 -050025
Jean-Jacques Hiblot201559c2019-07-02 10:53:54 +020026#define DEFAULT_CMD6_TIMEOUT_MS 500
27
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +020028static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
Marek Vasutf537e392016-12-01 02:06:33 +010029
Simon Glasseba48f92017-07-29 11:35:31 -060030#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +020031
Sam Protsenkodb174c62019-08-14 22:52:51 +030032static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +020033{
34 return -ENOSYS;
35}
36
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +020037__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanov020f2612012-12-03 02:19:46 +000038{
39 return -1;
40}
41
42int mmc_getwp(struct mmc *mmc)
43{
44 int wp;
45
46 wp = board_mmc_getwp(mmc);
47
Peter Korsgaardf7b15102013-03-21 04:00:03 +000048 if (wp < 0) {
Pantelis Antoniou2c850462014-03-11 19:34:20 +020049 if (mmc->cfg->ops->getwp)
50 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardf7b15102013-03-21 04:00:03 +000051 else
52 wp = 0;
53 }
Nikita Kiryanov020f2612012-12-03 02:19:46 +000054
55 return wp;
56}
57
Jeroen Hofstee47726302014-07-10 22:46:28 +020058__weak int board_mmc_getcd(struct mmc *mmc)
59{
Stefano Babic6e00edf2010-02-05 15:04:43 +010060 return -1;
61}
Simon Glass394dfc02016-06-12 23:30:22 -060062#endif
Stefano Babic6e00edf2010-02-05 15:04:43 +010063
Simon Glassb23d96e2016-06-12 23:30:20 -060064#ifdef CONFIG_MMC_TRACE
65void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
Andy Flemingad347bb2008-10-30 16:41:01 -050066{
Simon Glassb23d96e2016-06-12 23:30:20 -060067 printf("CMD_SEND:%d\n", cmd->cmdidx);
Marek Vasut6eeee302019-03-23 18:54:45 +010068 printf("\t\tARG\t\t\t 0x%08x\n", cmd->cmdarg);
Simon Glassb23d96e2016-06-12 23:30:20 -060069}
Marek Vasutdccb6082012-03-15 18:41:35 +000070
Simon Glassb23d96e2016-06-12 23:30:20 -060071void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
72{
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000073 int i;
74 u8 *ptr;
75
Bin Meng8d1ad1e2016-03-17 21:53:14 -070076 if (ret) {
77 printf("\t\tRET\t\t\t %d\n", ret);
78 } else {
79 switch (cmd->resp_type) {
80 case MMC_RSP_NONE:
81 printf("\t\tMMC_RSP_NONE\n");
82 break;
83 case MMC_RSP_R1:
Marek Vasut6eeee302019-03-23 18:54:45 +010084 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -070085 cmd->response[0]);
86 break;
87 case MMC_RSP_R1b:
Marek Vasut6eeee302019-03-23 18:54:45 +010088 printf("\t\tMMC_RSP_R1b\t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -070089 cmd->response[0]);
90 break;
91 case MMC_RSP_R2:
Marek Vasut6eeee302019-03-23 18:54:45 +010092 printf("\t\tMMC_RSP_R2\t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -070093 cmd->response[0]);
Marek Vasut6eeee302019-03-23 18:54:45 +010094 printf("\t\t \t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -070095 cmd->response[1]);
Marek Vasut6eeee302019-03-23 18:54:45 +010096 printf("\t\t \t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -070097 cmd->response[2]);
Marek Vasut6eeee302019-03-23 18:54:45 +010098 printf("\t\t \t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -070099 cmd->response[3]);
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000100 printf("\n");
Bin Meng8d1ad1e2016-03-17 21:53:14 -0700101 printf("\t\t\t\t\tDUMPING DATA\n");
102 for (i = 0; i < 4; i++) {
103 int j;
104 printf("\t\t\t\t\t%03d - ", i*4);
105 ptr = (u8 *)&cmd->response[i];
106 ptr += 3;
107 for (j = 0; j < 4; j++)
Marek Vasut6eeee302019-03-23 18:54:45 +0100108 printf("%02x ", *ptr--);
Bin Meng8d1ad1e2016-03-17 21:53:14 -0700109 printf("\n");
110 }
111 break;
112 case MMC_RSP_R3:
Marek Vasut6eeee302019-03-23 18:54:45 +0100113 printf("\t\tMMC_RSP_R3,4\t\t 0x%08x \n",
Bin Meng8d1ad1e2016-03-17 21:53:14 -0700114 cmd->response[0]);
115 break;
116 default:
117 printf("\t\tERROR MMC rsp not supported\n");
118 break;
Bin Meng4a4ef872016-03-17 21:53:13 -0700119 }
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000120 }
Simon Glassb23d96e2016-06-12 23:30:20 -0600121}
122
123void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
124{
125 int status;
126
127 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
128 printf("CURR STATE:%d\n", status);
129}
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000130#endif
Simon Glassb23d96e2016-06-12 23:30:20 -0600131
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +0200132#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
133const char *mmc_mode_name(enum bus_mode mode)
134{
135 static const char *const names[] = {
136 [MMC_LEGACY] = "MMC legacy",
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +0200137 [MMC_HS] = "MMC High Speed (26MHz)",
138 [SD_HS] = "SD High Speed (50MHz)",
139 [UHS_SDR12] = "UHS SDR12 (25MHz)",
140 [UHS_SDR25] = "UHS SDR25 (50MHz)",
141 [UHS_SDR50] = "UHS SDR50 (100MHz)",
142 [UHS_SDR104] = "UHS SDR104 (208MHz)",
143 [UHS_DDR50] = "UHS DDR50 (50MHz)",
144 [MMC_HS_52] = "MMC High Speed (52MHz)",
145 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
146 [MMC_HS_200] = "HS200 (200MHz)",
Peng Fan46801252018-08-10 14:07:54 +0800147 [MMC_HS_400] = "HS400 (200MHz)",
Peng Faneede83b2019-07-10 14:43:07 +0800148 [MMC_HS_400_ES] = "HS400ES (200MHz)",
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +0200149 };
150
151 if (mode >= MMC_MODES_END)
152 return "Unknown mode";
153 else
154 return names[mode];
155}
156#endif
157
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200158static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
159{
160 static const int freqs[] = {
Jaehoon Chung7c5c7302018-01-30 14:10:16 +0900161 [MMC_LEGACY] = 25000000,
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200162 [MMC_HS] = 26000000,
163 [SD_HS] = 50000000,
Jaehoon Chung7c5c7302018-01-30 14:10:16 +0900164 [MMC_HS_52] = 52000000,
165 [MMC_DDR_52] = 52000000,
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200166 [UHS_SDR12] = 25000000,
167 [UHS_SDR25] = 50000000,
168 [UHS_SDR50] = 100000000,
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200169 [UHS_DDR50] = 50000000,
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100170 [UHS_SDR104] = 208000000,
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200171 [MMC_HS_200] = 200000000,
Peng Fan46801252018-08-10 14:07:54 +0800172 [MMC_HS_400] = 200000000,
Peng Faneede83b2019-07-10 14:43:07 +0800173 [MMC_HS_400_ES] = 200000000,
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200174 };
175
176 if (mode == MMC_LEGACY)
177 return mmc->legacy_speed;
178 else if (mode >= MMC_MODES_END)
179 return 0;
180 else
181 return freqs[mode];
182}
183
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +0200184static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
185{
186 mmc->selected_mode = mode;
Jean-Jacques Hiblot78422312017-09-21 16:29:55 +0200187 mmc->tran_speed = mmc_mode2freq(mmc, mode);
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200188 mmc->ddr_mode = mmc_is_mode_ddr(mode);
Masahiro Yamadaf97b1482018-01-28 19:11:42 +0900189 pr_debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
190 mmc->tran_speed / 1000000);
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +0200191 return 0;
192}
193
Simon Glasseba48f92017-07-29 11:35:31 -0600194#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassb23d96e2016-06-12 23:30:20 -0600195int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
196{
197 int ret;
198
199 mmmc_trace_before_send(mmc, cmd);
200 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
201 mmmc_trace_after_send(mmc, cmd, ret);
202
Marek Vasutdccb6082012-03-15 18:41:35 +0000203 return ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500204}
Simon Glass394dfc02016-06-12 23:30:22 -0600205#endif
Andy Flemingad347bb2008-10-30 16:41:01 -0500206
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200207int mmc_send_status(struct mmc *mmc, unsigned int *status)
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000208{
209 struct mmc_cmd cmd;
Jan Kloetzke31789322012-02-05 22:29:12 +0000210 int err, retries = 5;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000211
212 cmd.cmdidx = MMC_CMD_SEND_STATUS;
213 cmd.resp_type = MMC_RSP_R1;
Marek Vasutc4427392011-08-10 09:24:48 +0200214 if (!mmc_host_is_spi(mmc))
215 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000216
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200217 while (retries--) {
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000218 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzke31789322012-02-05 22:29:12 +0000219 if (!err) {
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200220 mmc_trace_state(mmc, &cmd);
221 *status = cmd.response[0];
222 return 0;
223 }
224 }
225 mmc_trace_state(mmc, &cmd);
226 return -ECOMM;
227}
228
Sam Protsenkodb174c62019-08-14 22:52:51 +0300229int mmc_poll_for_busy(struct mmc *mmc, int timeout_ms)
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200230{
231 unsigned int status;
232 int err;
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +0200233
Sam Protsenkodb174c62019-08-14 22:52:51 +0300234 err = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblot4f04a322019-07-02 10:53:53 +0200235 if (err != -ENOSYS)
236 return err;
237
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200238 while (1) {
239 err = mmc_send_status(mmc, &status);
240 if (err)
241 return err;
242
243 if ((status & MMC_STATUS_RDY_FOR_DATA) &&
244 (status & MMC_STATUS_CURR_STATE) !=
245 MMC_STATE_PRG)
246 break;
247
248 if (status & MMC_STATUS_MASK) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100249#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200250 pr_err("Status Error: 0x%08x\n", status);
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100251#endif
Jean-Jacques Hiblot443edbe2019-07-02 10:53:52 +0200252 return -ECOMM;
253 }
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000254
Sam Protsenkodb174c62019-08-14 22:52:51 +0300255 if (timeout_ms-- <= 0)
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500256 break;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000257
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500258 udelay(1000);
259 }
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000260
Sam Protsenkodb174c62019-08-14 22:52:51 +0300261 if (timeout_ms <= 0) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100262#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +0100263 pr_err("Timeout waiting card ready\n");
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100264#endif
Jaehoon Chung7825d202016-07-19 16:33:36 +0900265 return -ETIMEDOUT;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000266 }
267
268 return 0;
269}
270
Paul Burton8d30cc92013-09-09 15:30:26 +0100271int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Flemingad347bb2008-10-30 16:41:01 -0500272{
273 struct mmc_cmd cmd;
Kishon Vijay Abraham I07baaa62017-09-21 16:30:10 +0200274 int err;
Andy Flemingad347bb2008-10-30 16:41:01 -0500275
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -0600276 if (mmc->ddr_mode)
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900277 return 0;
278
Andy Flemingad347bb2008-10-30 16:41:01 -0500279 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
280 cmd.resp_type = MMC_RSP_R1;
281 cmd.cmdarg = len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500282
Kishon Vijay Abraham I07baaa62017-09-21 16:30:10 +0200283 err = mmc_send_cmd(mmc, &cmd, NULL);
284
285#ifdef CONFIG_MMC_QUIRKS
286 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SET_BLOCKLEN)) {
287 int retries = 4;
288 /*
289 * It has been seen that SET_BLOCKLEN may fail on the first
290 * attempt, let's try a few more time
291 */
292 do {
293 err = mmc_send_cmd(mmc, &cmd, NULL);
294 if (!err)
295 break;
296 } while (retries--);
297 }
298#endif
299
300 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -0500301}
302
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100303#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot71264bb2017-09-21 16:30:12 +0200304static const u8 tuning_blk_pattern_4bit[] = {
305 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
306 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
307 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
308 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
309 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
310 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
311 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
312 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
313};
314
315static const u8 tuning_blk_pattern_8bit[] = {
316 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
317 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
318 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
319 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
320 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
321 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
322 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
323 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
324 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
325 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
326 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
327 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
328 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
329 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
330 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
331 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
332};
333
334int mmc_send_tuning(struct mmc *mmc, u32 opcode, int *cmd_error)
335{
336 struct mmc_cmd cmd;
337 struct mmc_data data;
338 const u8 *tuning_block_pattern;
339 int size, err;
340
341 if (mmc->bus_width == 8) {
342 tuning_block_pattern = tuning_blk_pattern_8bit;
343 size = sizeof(tuning_blk_pattern_8bit);
344 } else if (mmc->bus_width == 4) {
345 tuning_block_pattern = tuning_blk_pattern_4bit;
346 size = sizeof(tuning_blk_pattern_4bit);
347 } else {
348 return -EINVAL;
349 }
350
351 ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size);
352
353 cmd.cmdidx = opcode;
354 cmd.cmdarg = 0;
355 cmd.resp_type = MMC_RSP_R1;
356
357 data.dest = (void *)data_buf;
358 data.blocks = 1;
359 data.blocksize = size;
360 data.flags = MMC_DATA_READ;
361
362 err = mmc_send_cmd(mmc, &cmd, &data);
363 if (err)
364 return err;
365
366 if (memcmp(data_buf, tuning_block_pattern, size))
367 return -EIO;
368
369 return 0;
370}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100371#endif
Jean-Jacques Hiblot71264bb2017-09-21 16:30:12 +0200372
Sascha Silbe4bdf6fd2013-06-14 13:07:25 +0200373static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillips87ea3892012-10-29 13:34:43 +0000374 lbaint_t blkcnt)
Andy Flemingad347bb2008-10-30 16:41:01 -0500375{
376 struct mmc_cmd cmd;
377 struct mmc_data data;
378
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700379 if (blkcnt > 1)
380 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
381 else
382 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Flemingad347bb2008-10-30 16:41:01 -0500383
384 if (mmc->high_capacity)
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700385 cmd.cmdarg = start;
Andy Flemingad347bb2008-10-30 16:41:01 -0500386 else
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700387 cmd.cmdarg = start * mmc->read_bl_len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500388
389 cmd.resp_type = MMC_RSP_R1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500390
391 data.dest = dst;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700392 data.blocks = blkcnt;
Andy Flemingad347bb2008-10-30 16:41:01 -0500393 data.blocksize = mmc->read_bl_len;
394 data.flags = MMC_DATA_READ;
395
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700396 if (mmc_send_cmd(mmc, &cmd, &data))
397 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500398
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700399 if (blkcnt > 1) {
400 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
401 cmd.cmdarg = 0;
402 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700403 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100404#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +0100405 pr_err("mmc fail to send stop cmd\n");
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100406#endif
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700407 return 0;
408 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500409 }
410
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700411 return blkcnt;
Andy Flemingad347bb2008-10-30 16:41:01 -0500412}
413
Marek Vasut31976d92020-04-04 12:45:05 +0200414#if !CONFIG_IS_ENABLED(DM_MMC)
415static int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
416{
417 if (mmc->cfg->ops->get_b_max)
418 return mmc->cfg->ops->get_b_max(mmc, dst, blkcnt);
419 else
420 return mmc->cfg->b_max;
421}
422#endif
423
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600424#if CONFIG_IS_ENABLED(BLK)
Simon Glass62e293a2016-06-12 23:30:15 -0600425ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass59bc6f22016-05-01 13:52:41 -0600426#else
Simon Glass62e293a2016-06-12 23:30:15 -0600427ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
428 void *dst)
Simon Glass59bc6f22016-05-01 13:52:41 -0600429#endif
Andy Flemingad347bb2008-10-30 16:41:01 -0500430{
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600431#if CONFIG_IS_ENABLED(BLK)
Simon Glass59bc6f22016-05-01 13:52:41 -0600432 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
433#endif
Simon Glass2f26fff2016-02-29 15:25:51 -0700434 int dev_num = block_dev->devnum;
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700435 int err;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700436 lbaint_t cur, blocks_todo = blkcnt;
Marek Vasut31976d92020-04-04 12:45:05 +0200437 uint b_max;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700438
439 if (blkcnt == 0)
440 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500441
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700442 struct mmc *mmc = find_mmc_device(dev_num);
Andy Flemingad347bb2008-10-30 16:41:01 -0500443 if (!mmc)
444 return 0;
445
Marek Vasutf537e392016-12-01 02:06:33 +0100446 if (CONFIG_IS_ENABLED(MMC_TINY))
447 err = mmc_switch_part(mmc, block_dev->hwpart);
448 else
449 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
450
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700451 if (err < 0)
452 return 0;
453
Simon Glasse5db1152016-05-01 13:52:35 -0600454 if ((start + blkcnt) > block_dev->lba) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100455#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +0100456 pr_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
457 start + blkcnt, block_dev->lba);
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100458#endif
Lei Wene1cc9c82010-09-13 22:07:27 +0800459 return 0;
460 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500461
Simon Glassa4343c42015-06-23 15:38:50 -0600462 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +0900463 pr_debug("%s: Failed to set blocklen\n", __func__);
Andy Flemingad347bb2008-10-30 16:41:01 -0500464 return 0;
Simon Glassa4343c42015-06-23 15:38:50 -0600465 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500466
Marek Vasut31976d92020-04-04 12:45:05 +0200467 b_max = mmc_get_b_max(mmc, dst, blkcnt);
468
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700469 do {
Marek Vasut31976d92020-04-04 12:45:05 +0200470 cur = (blocks_todo > b_max) ? b_max : blocks_todo;
Simon Glassa4343c42015-06-23 15:38:50 -0600471 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +0900472 pr_debug("%s: Failed to read blocks\n", __func__);
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700473 return 0;
Simon Glassa4343c42015-06-23 15:38:50 -0600474 }
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700475 blocks_todo -= cur;
476 start += cur;
477 dst += cur * mmc->read_bl_len;
478 } while (blocks_todo > 0);
Andy Flemingad347bb2008-10-30 16:41:01 -0500479
480 return blkcnt;
481}
482
Kim Phillips87ea3892012-10-29 13:34:43 +0000483static int mmc_go_idle(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500484{
485 struct mmc_cmd cmd;
486 int err;
487
488 udelay(1000);
489
490 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
491 cmd.cmdarg = 0;
492 cmd.resp_type = MMC_RSP_NONE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500493
494 err = mmc_send_cmd(mmc, &cmd, NULL);
495
496 if (err)
497 return err;
498
499 udelay(2000);
500
501 return 0;
502}
503
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100504#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200505static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
506{
507 struct mmc_cmd cmd;
508 int err = 0;
509
510 /*
511 * Send CMD11 only if the request is to switch the card to
512 * 1.8V signalling.
513 */
514 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
515 return mmc_set_signal_voltage(mmc, signal_voltage);
516
517 cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
518 cmd.cmdarg = 0;
519 cmd.resp_type = MMC_RSP_R1;
520
521 err = mmc_send_cmd(mmc, &cmd, NULL);
522 if (err)
523 return err;
524
525 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
526 return -EIO;
527
528 /*
529 * The card should drive cmd and dat[0:3] low immediately
530 * after the response of cmd11, but wait 100 us to be sure
531 */
532 err = mmc_wait_dat0(mmc, 0, 100);
533 if (err == -ENOSYS)
534 udelay(100);
535 else if (err)
536 return -ETIMEDOUT;
537
538 /*
539 * During a signal voltage level switch, the clock must be gated
540 * for 5 ms according to the SD spec
541 */
Jaehoon Chung239cb2f2018-01-26 19:25:29 +0900542 mmc_set_clock(mmc, mmc->clock, MMC_CLK_DISABLE);
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200543
544 err = mmc_set_signal_voltage(mmc, signal_voltage);
545 if (err)
546 return err;
547
548 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
549 mdelay(10);
Jaehoon Chung239cb2f2018-01-26 19:25:29 +0900550 mmc_set_clock(mmc, mmc->clock, MMC_CLK_ENABLE);
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200551
552 /*
553 * Failure to switch is indicated by the card holding
554 * dat[0:3] low. Wait for at least 1 ms according to spec
555 */
556 err = mmc_wait_dat0(mmc, 1, 1000);
557 if (err == -ENOSYS)
558 udelay(1000);
559 else if (err)
560 return -ETIMEDOUT;
561
562 return 0;
563}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100564#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200565
566static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
Andy Flemingad347bb2008-10-30 16:41:01 -0500567{
568 int timeout = 1000;
569 int err;
570 struct mmc_cmd cmd;
571
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500572 while (1) {
Andy Flemingad347bb2008-10-30 16:41:01 -0500573 cmd.cmdidx = MMC_CMD_APP_CMD;
574 cmd.resp_type = MMC_RSP_R1;
575 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500576
577 err = mmc_send_cmd(mmc, &cmd, NULL);
578
579 if (err)
580 return err;
581
582 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
583 cmd.resp_type = MMC_RSP_R3;
Stefano Babicf8e9a212010-01-20 18:20:39 +0100584
585 /*
586 * Most cards do not answer if some reserved bits
587 * in the ocr are set. However, Some controller
588 * can set bit 7 (reserved for low voltages), but
589 * how to manage low voltages SD card is not yet
590 * specified.
591 */
Thomas Chou1254c3d2010-12-24 13:12:21 +0000592 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200593 (mmc->cfg->voltages & 0xff8000);
Andy Flemingad347bb2008-10-30 16:41:01 -0500594
595 if (mmc->version == SD_VERSION_2)
596 cmd.cmdarg |= OCR_HCS;
597
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200598 if (uhs_en)
599 cmd.cmdarg |= OCR_S18R;
600
Andy Flemingad347bb2008-10-30 16:41:01 -0500601 err = mmc_send_cmd(mmc, &cmd, NULL);
602
603 if (err)
604 return err;
605
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500606 if (cmd.response[0] & OCR_BUSY)
607 break;
Andy Flemingad347bb2008-10-30 16:41:01 -0500608
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500609 if (timeout-- <= 0)
Jaehoon Chung7825d202016-07-19 16:33:36 +0900610 return -EOPNOTSUPP;
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500611
612 udelay(1000);
613 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500614
615 if (mmc->version != SD_VERSION_2)
616 mmc->version = SD_VERSION_1_0;
617
Thomas Chou1254c3d2010-12-24 13:12:21 +0000618 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
619 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
620 cmd.resp_type = MMC_RSP_R3;
621 cmd.cmdarg = 0;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000622
623 err = mmc_send_cmd(mmc, &cmd, NULL);
624
625 if (err)
626 return err;
627 }
628
Rabin Vincentb6eed942009-04-05 13:30:56 +0530629 mmc->ocr = cmd.response[0];
Andy Flemingad347bb2008-10-30 16:41:01 -0500630
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100631#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200632 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
633 == 0x41000000) {
634 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
635 if (err)
636 return err;
637 }
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +0100638#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +0200639
Andy Flemingad347bb2008-10-30 16:41:01 -0500640 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
641 mmc->rca = 0;
642
643 return 0;
644}
645
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500646static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Flemingad347bb2008-10-30 16:41:01 -0500647{
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500648 struct mmc_cmd cmd;
Andy Flemingad347bb2008-10-30 16:41:01 -0500649 int err;
650
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500651 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
652 cmd.resp_type = MMC_RSP_R3;
653 cmd.cmdarg = 0;
Rob Herring5fd3edd2015-03-23 17:56:59 -0500654 if (use_arg && !mmc_host_is_spi(mmc))
655 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200656 (mmc->cfg->voltages &
Andrew Gabbasovec600d12015-03-19 07:44:03 -0500657 (mmc->ocr & OCR_VOLTAGE_MASK)) |
658 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000659
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500660 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000661 if (err)
662 return err;
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500663 mmc->ocr = cmd.response[0];
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000664 return 0;
665}
666
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +0200667static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000668{
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000669 int err, i;
670
Andy Flemingad347bb2008-10-30 16:41:01 -0500671 /* Some cards seem to need this */
672 mmc_go_idle(mmc);
673
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000674 /* Asking to the card its capabilities */
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000675 for (i = 0; i < 2; i++) {
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500676 err = mmc_send_op_cond_iter(mmc, i != 0);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000677 if (err)
678 return err;
Wolfgang Denk80f70212011-05-19 22:21:41 +0200679
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000680 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasovec600d12015-03-19 07:44:03 -0500681 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -0500682 break;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000683 }
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -0500684 mmc->op_cond_pending = 1;
685 return 0;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000686}
Wolfgang Denk80f70212011-05-19 22:21:41 +0200687
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +0200688static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000689{
690 struct mmc_cmd cmd;
691 int timeout = 1000;
Vipul Kumardbad7b42018-05-03 12:20:54 +0530692 ulong start;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000693 int err;
Wolfgang Denk80f70212011-05-19 22:21:41 +0200694
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000695 mmc->op_cond_pending = 0;
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500696 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lu9c720612016-08-02 15:33:18 +0800697 /* Some cards seem to need this */
698 mmc_go_idle(mmc);
699
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500700 start = get_timer(0);
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500701 while (1) {
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500702 err = mmc_send_op_cond_iter(mmc, 1);
703 if (err)
704 return err;
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500705 if (mmc->ocr & OCR_BUSY)
706 break;
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500707 if (get_timer(start) > timeout)
Jaehoon Chung7825d202016-07-19 16:33:36 +0900708 return -EOPNOTSUPP;
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500709 udelay(100);
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500710 }
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500711 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500712
Thomas Chou1254c3d2010-12-24 13:12:21 +0000713 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
714 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
715 cmd.resp_type = MMC_RSP_R3;
716 cmd.cmdarg = 0;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000717
718 err = mmc_send_cmd(mmc, &cmd, NULL);
719
720 if (err)
721 return err;
Andrew Gabbasovec600d12015-03-19 07:44:03 -0500722
723 mmc->ocr = cmd.response[0];
Thomas Chou1254c3d2010-12-24 13:12:21 +0000724 }
725
Andy Flemingad347bb2008-10-30 16:41:01 -0500726 mmc->version = MMC_VERSION_UNKNOWN;
Andy Flemingad347bb2008-10-30 16:41:01 -0500727
728 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrenf6545f12014-01-30 16:11:12 -0700729 mmc->rca = 1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500730
731 return 0;
732}
733
734
Heinrich Schuchardtbf230e12020-03-30 07:24:17 +0200735int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Flemingad347bb2008-10-30 16:41:01 -0500736{
737 struct mmc_cmd cmd;
738 struct mmc_data data;
739 int err;
740
741 /* Get the Card Status Register */
742 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
743 cmd.resp_type = MMC_RSP_R1;
744 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500745
Yoshihiro Shimodaf6bec732012-06-07 19:09:11 +0000746 data.dest = (char *)ext_csd;
Andy Flemingad347bb2008-10-30 16:41:01 -0500747 data.blocks = 1;
Simon Glassa09c2b72013-04-03 08:54:30 +0000748 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Flemingad347bb2008-10-30 16:41:01 -0500749 data.flags = MMC_DATA_READ;
750
751 err = mmc_send_cmd(mmc, &cmd, &data);
752
753 return err;
754}
755
Marek Vasut8a966472019-02-06 11:34:27 +0100756static int __mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value,
757 bool send_status)
Andy Flemingad347bb2008-10-30 16:41:01 -0500758{
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200759 unsigned int status, start;
Andy Flemingad347bb2008-10-30 16:41:01 -0500760 struct mmc_cmd cmd;
Sam Protsenkodb174c62019-08-14 22:52:51 +0300761 int timeout_ms = DEFAULT_CMD6_TIMEOUT_MS;
Jean-Jacques Hiblot7f5b1692019-07-02 10:53:55 +0200762 bool is_part_switch = (set == EXT_CSD_CMD_SET_NORMAL) &&
763 (index == EXT_CSD_PART_CONF);
Maxime Riparde7462aa2016-11-04 16:18:08 +0100764 int retries = 3;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000765 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500766
Jean-Jacques Hiblot201559c2019-07-02 10:53:54 +0200767 if (mmc->gen_cmd6_time)
Sam Protsenkodb174c62019-08-14 22:52:51 +0300768 timeout_ms = mmc->gen_cmd6_time * 10;
Jean-Jacques Hiblot201559c2019-07-02 10:53:54 +0200769
Jean-Jacques Hiblot7f5b1692019-07-02 10:53:55 +0200770 if (is_part_switch && mmc->part_switch_time)
Sam Protsenkodb174c62019-08-14 22:52:51 +0300771 timeout_ms = mmc->part_switch_time * 10;
Jean-Jacques Hiblot7f5b1692019-07-02 10:53:55 +0200772
Andy Flemingad347bb2008-10-30 16:41:01 -0500773 cmd.cmdidx = MMC_CMD_SWITCH;
774 cmd.resp_type = MMC_RSP_R1b;
775 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000776 (index << 16) |
777 (value << 8);
Andy Flemingad347bb2008-10-30 16:41:01 -0500778
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200779 do {
Maxime Riparde7462aa2016-11-04 16:18:08 +0100780 ret = mmc_send_cmd(mmc, &cmd, NULL);
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200781 } while (ret && retries-- > 0);
Maxime Riparde7462aa2016-11-04 16:18:08 +0100782
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200783 if (ret)
784 return ret;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000785
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200786 start = get_timer(0);
Marek Vasut8a966472019-02-06 11:34:27 +0100787
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200788 /* poll dat0 for rdy/buys status */
Sam Protsenkodb174c62019-08-14 22:52:51 +0300789 ret = mmc_wait_dat0(mmc, 1, timeout_ms * 1000);
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200790 if (ret && ret != -ENOSYS)
791 return ret;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000792
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200793 /*
794 * In cases when not allowed to poll by using CMD13 or because we aren't
795 * capable of polling by using mmc_wait_dat0, then rely on waiting the
796 * stated timeout to be sufficient.
797 */
798 if (ret == -ENOSYS && !send_status)
Sam Protsenkodb174c62019-08-14 22:52:51 +0300799 mdelay(timeout_ms);
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200800
801 /* Finally wait until the card is ready or indicates a failure
802 * to switch. It doesn't hurt to use CMD13 here even if send_status
Sam Protsenkodb174c62019-08-14 22:52:51 +0300803 * is false, because by now (after 'timeout_ms' ms) the bus should be
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200804 * reliable.
805 */
806 do {
807 ret = mmc_send_status(mmc, &status);
808
809 if (!ret && (status & MMC_STATUS_SWITCH_ERROR)) {
810 pr_debug("switch failed %d/%d/0x%x !\n", set, index,
811 value);
812 return -EIO;
813 }
814 if (!ret && (status & MMC_STATUS_RDY_FOR_DATA))
815 return 0;
816 udelay(100);
Sam Protsenkodb174c62019-08-14 22:52:51 +0300817 } while (get_timer(start) < timeout_ms);
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000818
Jean-Jacques Hiblot5a7cf402019-07-02 10:53:56 +0200819 return -ETIMEDOUT;
Andy Flemingad347bb2008-10-30 16:41:01 -0500820}
821
Marek Vasut8a966472019-02-06 11:34:27 +0100822int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
823{
824 return __mmc_switch(mmc, set, index, value, true);
825}
826
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +0200827int mmc_boot_wp(struct mmc *mmc)
828{
829 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, 1);
830}
831
Marek Vasuta318a7a2018-04-15 00:37:11 +0200832#if !CONFIG_IS_ENABLED(MMC_TINY)
Marek Vasut111572f2019-01-03 21:19:24 +0100833static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode,
834 bool hsdowngrade)
Andy Flemingad347bb2008-10-30 16:41:01 -0500835{
Andy Flemingad347bb2008-10-30 16:41:01 -0500836 int err;
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200837 int speed_bits;
838
839 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
840
841 switch (mode) {
842 case MMC_HS:
843 case MMC_HS_52:
844 case MMC_DDR_52:
845 speed_bits = EXT_CSD_TIMING_HS;
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +0200846 break;
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +0100847#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +0200848 case MMC_HS_200:
849 speed_bits = EXT_CSD_TIMING_HS200;
850 break;
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +0100851#endif
Peng Fan46801252018-08-10 14:07:54 +0800852#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
853 case MMC_HS_400:
854 speed_bits = EXT_CSD_TIMING_HS400;
855 break;
856#endif
Peng Faneede83b2019-07-10 14:43:07 +0800857#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
858 case MMC_HS_400_ES:
859 speed_bits = EXT_CSD_TIMING_HS400;
860 break;
861#endif
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200862 case MMC_LEGACY:
863 speed_bits = EXT_CSD_TIMING_LEGACY;
864 break;
865 default:
866 return -EINVAL;
867 }
Marek Vasut8a966472019-02-06 11:34:27 +0100868
869 err = __mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
870 speed_bits, !hsdowngrade);
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200871 if (err)
872 return err;
873
Marek Vasut111572f2019-01-03 21:19:24 +0100874#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
875 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
876 /*
877 * In case the eMMC is in HS200/HS400 mode and we are downgrading
878 * to HS mode, the card clock are still running much faster than
879 * the supported HS mode clock, so we can not reliably read out
880 * Extended CSD. Reconfigure the controller to run at HS mode.
881 */
882 if (hsdowngrade) {
883 mmc_select_mode(mmc, MMC_HS);
884 mmc_set_clock(mmc, mmc_mode2freq(mmc, MMC_HS), false);
885 }
886#endif
887
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200888 if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
889 /* Now check to see that it worked */
890 err = mmc_send_ext_csd(mmc, test_csd);
891 if (err)
892 return err;
893
894 /* No high-speed support */
895 if (!test_csd[EXT_CSD_HS_TIMING])
896 return -ENOTSUPP;
897 }
898
899 return 0;
900}
901
902static int mmc_get_capabilities(struct mmc *mmc)
903{
904 u8 *ext_csd = mmc->ext_csd;
905 char cardtype;
Andy Flemingad347bb2008-10-30 16:41:01 -0500906
Jean-Jacques Hiblot3f2ffc22017-11-30 17:43:56 +0100907 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Flemingad347bb2008-10-30 16:41:01 -0500908
Thomas Chou1254c3d2010-12-24 13:12:21 +0000909 if (mmc_host_is_spi(mmc))
910 return 0;
911
Andy Flemingad347bb2008-10-30 16:41:01 -0500912 /* Only version 4 supports high-speed */
913 if (mmc->version < MMC_VERSION_4)
914 return 0;
915
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200916 if (!ext_csd) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +0100917 pr_err("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200918 return -ENOTSUPP;
919 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500920
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200921 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
Andy Flemingad347bb2008-10-30 16:41:01 -0500922
Peng Fan46801252018-08-10 14:07:54 +0800923 cardtype = ext_csd[EXT_CSD_CARD_TYPE];
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +0200924 mmc->cardtype = cardtype;
Andy Flemingad347bb2008-10-30 16:41:01 -0500925
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +0100926#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +0200927 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
928 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
929 mmc->card_caps |= MMC_MODE_HS200;
930 }
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +0100931#endif
Peng Faneede83b2019-07-10 14:43:07 +0800932#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT) || \
933 CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
Peng Fan46801252018-08-10 14:07:54 +0800934 if (cardtype & (EXT_CSD_CARD_TYPE_HS400_1_2V |
935 EXT_CSD_CARD_TYPE_HS400_1_8V)) {
936 mmc->card_caps |= MMC_MODE_HS400;
937 }
938#endif
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900939 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200940 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900941 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200942 mmc->card_caps |= MMC_MODE_HS_52MHz;
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900943 }
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +0200944 if (cardtype & EXT_CSD_CARD_TYPE_26)
945 mmc->card_caps |= MMC_MODE_HS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500946
Peng Faneede83b2019-07-10 14:43:07 +0800947#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
948 if (ext_csd[EXT_CSD_STROBE_SUPPORT] &&
949 (mmc->card_caps & MMC_MODE_HS400)) {
950 mmc->card_caps |= MMC_MODE_HS400_ES;
951 }
952#endif
953
Andy Flemingad347bb2008-10-30 16:41:01 -0500954 return 0;
955}
Marek Vasuta318a7a2018-04-15 00:37:11 +0200956#endif
Andy Flemingad347bb2008-10-30 16:41:01 -0500957
Stephen Warrene315ae82013-06-11 15:14:01 -0600958static int mmc_set_capacity(struct mmc *mmc, int part_num)
959{
960 switch (part_num) {
961 case 0:
962 mmc->capacity = mmc->capacity_user;
963 break;
964 case 1:
965 case 2:
966 mmc->capacity = mmc->capacity_boot;
967 break;
968 case 3:
969 mmc->capacity = mmc->capacity_rpmb;
970 break;
971 case 4:
972 case 5:
973 case 6:
974 case 7:
975 mmc->capacity = mmc->capacity_gp[part_num - 4];
976 break;
977 default:
978 return -1;
979 }
980
Simon Glasse5db1152016-05-01 13:52:35 -0600981 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrene315ae82013-06-11 15:14:01 -0600982
983 return 0;
984}
985
Simon Glass62e293a2016-06-12 23:30:15 -0600986int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wen31b99802011-05-02 16:26:26 +0000987{
Stephen Warrene315ae82013-06-11 15:14:01 -0600988 int ret;
Jean-Jacques Hiblotfaf5c952019-07-02 10:53:58 +0200989 int retry = 3;
Lei Wen31b99802011-05-02 16:26:26 +0000990
Jean-Jacques Hiblotfaf5c952019-07-02 10:53:58 +0200991 do {
992 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
993 EXT_CSD_PART_CONF,
994 (mmc->part_config & ~PART_ACCESS_MASK)
995 | (part_num & PART_ACCESS_MASK));
996 } while (ret && retry--);
Peter Bigot45fde892014-09-02 18:31:23 -0500997
998 /*
999 * Set the capacity if the switch succeeded or was intended
1000 * to return to representing the raw device.
1001 */
Stephen Warren1e0f92a2015-12-07 11:38:49 -07001002 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot45fde892014-09-02 18:31:23 -05001003 ret = mmc_set_capacity(mmc, part_num);
Simon Glass984db5d2016-05-01 13:52:37 -06001004 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren1e0f92a2015-12-07 11:38:49 -07001005 }
Stephen Warrene315ae82013-06-11 15:14:01 -06001006
Peter Bigot45fde892014-09-02 18:31:23 -05001007 return ret;
Lei Wen31b99802011-05-02 16:26:26 +00001008}
1009
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001010#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001011int mmc_hwpart_config(struct mmc *mmc,
1012 const struct mmc_hwpart_conf *conf,
1013 enum mmc_hwpart_conf_mode mode)
1014{
1015 u8 part_attrs = 0;
1016 u32 enh_size_mult;
1017 u32 enh_start_addr;
1018 u32 gp_size_mult[4];
1019 u32 max_enh_size_mult;
1020 u32 tot_enh_size_mult = 0;
Diego Santa Cruz80200272014-12-23 10:50:31 +01001021 u8 wr_rel_set;
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001022 int i, pidx, err;
1023 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1024
1025 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
1026 return -EINVAL;
1027
1028 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001029 pr_err("eMMC >= 4.4 required for enhanced user data area\n");
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001030 return -EMEDIUMTYPE;
1031 }
1032
1033 if (!(mmc->part_support & PART_SUPPORT)) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001034 pr_err("Card does not support partitioning\n");
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001035 return -EMEDIUMTYPE;
1036 }
1037
1038 if (!mmc->hc_wp_grp_size) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001039 pr_err("Card does not define HC WP group size\n");
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001040 return -EMEDIUMTYPE;
1041 }
1042
1043 /* check partition alignment and total enhanced size */
1044 if (conf->user.enh_size) {
1045 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
1046 conf->user.enh_start % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001047 pr_err("User data enhanced area not HC WP group "
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001048 "size aligned\n");
1049 return -EINVAL;
1050 }
1051 part_attrs |= EXT_CSD_ENH_USR;
1052 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
1053 if (mmc->high_capacity) {
1054 enh_start_addr = conf->user.enh_start;
1055 } else {
1056 enh_start_addr = (conf->user.enh_start << 9);
1057 }
1058 } else {
1059 enh_size_mult = 0;
1060 enh_start_addr = 0;
1061 }
1062 tot_enh_size_mult += enh_size_mult;
1063
1064 for (pidx = 0; pidx < 4; pidx++) {
1065 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001066 pr_err("GP%i partition not HC WP group size "
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001067 "aligned\n", pidx+1);
1068 return -EINVAL;
1069 }
1070 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
1071 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
1072 part_attrs |= EXT_CSD_ENH_GP(pidx);
1073 tot_enh_size_mult += gp_size_mult[pidx];
1074 }
1075 }
1076
1077 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001078 pr_err("Card does not support enhanced attribute\n");
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001079 return -EMEDIUMTYPE;
1080 }
1081
1082 err = mmc_send_ext_csd(mmc, ext_csd);
1083 if (err)
1084 return err;
1085
1086 max_enh_size_mult =
1087 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
1088 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
1089 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
1090 if (tot_enh_size_mult > max_enh_size_mult) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001091 pr_err("Total enhanced size exceeds maximum (%u > %u)\n",
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001092 tot_enh_size_mult, max_enh_size_mult);
1093 return -EMEDIUMTYPE;
1094 }
1095
Diego Santa Cruz80200272014-12-23 10:50:31 +01001096 /* The default value of EXT_CSD_WR_REL_SET is device
1097 * dependent, the values can only be changed if the
1098 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
1099 * changed only once and before partitioning is completed. */
1100 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1101 if (conf->user.wr_rel_change) {
1102 if (conf->user.wr_rel_set)
1103 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
1104 else
1105 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
1106 }
1107 for (pidx = 0; pidx < 4; pidx++) {
1108 if (conf->gp_part[pidx].wr_rel_change) {
1109 if (conf->gp_part[pidx].wr_rel_set)
1110 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
1111 else
1112 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
1113 }
1114 }
1115
1116 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
1117 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
1118 puts("Card does not support host controlled partition write "
1119 "reliability settings\n");
1120 return -EMEDIUMTYPE;
1121 }
1122
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001123 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
1124 EXT_CSD_PARTITION_SETTING_COMPLETED) {
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001125 pr_err("Card already partitioned\n");
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001126 return -EPERM;
1127 }
1128
1129 if (mode == MMC_HWPART_CONF_CHECK)
1130 return 0;
1131
1132 /* Partitioning requires high-capacity size definitions */
1133 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
1134 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1135 EXT_CSD_ERASE_GROUP_DEF, 1);
1136
1137 if (err)
1138 return err;
1139
1140 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1141
Jaehoon Chung58b9eb82020-01-17 15:06:54 +09001142#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001143 /* update erase group size to be high-capacity */
1144 mmc->erase_grp_size =
1145 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jaehoon Chung58b9eb82020-01-17 15:06:54 +09001146#endif
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001147
1148 }
1149
1150 /* all OK, write the configuration */
1151 for (i = 0; i < 4; i++) {
1152 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1153 EXT_CSD_ENH_START_ADDR+i,
1154 (enh_start_addr >> (i*8)) & 0xFF);
1155 if (err)
1156 return err;
1157 }
1158 for (i = 0; i < 3; i++) {
1159 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1160 EXT_CSD_ENH_SIZE_MULT+i,
1161 (enh_size_mult >> (i*8)) & 0xFF);
1162 if (err)
1163 return err;
1164 }
1165 for (pidx = 0; pidx < 4; pidx++) {
1166 for (i = 0; i < 3; i++) {
1167 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1168 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1169 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1170 if (err)
1171 return err;
1172 }
1173 }
1174 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1175 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1176 if (err)
1177 return err;
1178
1179 if (mode == MMC_HWPART_CONF_SET)
1180 return 0;
1181
Diego Santa Cruz80200272014-12-23 10:50:31 +01001182 /* The WR_REL_SET is a write-once register but shall be
1183 * written before setting PART_SETTING_COMPLETED. As it is
1184 * write-once we can only write it when completing the
1185 * partitioning. */
1186 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1187 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1188 EXT_CSD_WR_REL_SET, wr_rel_set);
1189 if (err)
1190 return err;
1191 }
1192
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001193 /* Setting PART_SETTING_COMPLETED confirms the partition
1194 * configuration but it only becomes effective after power
1195 * cycle, so we do not adjust the partition related settings
1196 * in the mmc struct. */
1197
1198 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1199 EXT_CSD_PARTITION_SETTING,
1200 EXT_CSD_PARTITION_SETTING_COMPLETED);
1201 if (err)
1202 return err;
1203
1204 return 0;
1205}
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001206#endif
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +01001207
Simon Glasseba48f92017-07-29 11:35:31 -06001208#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Redingb9c8b772012-01-02 01:15:37 +00001209int mmc_getcd(struct mmc *mmc)
1210{
1211 int cd;
1212
1213 cd = board_mmc_getcd(mmc);
1214
Peter Korsgaardf7b15102013-03-21 04:00:03 +00001215 if (cd < 0) {
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001216 if (mmc->cfg->ops->getcd)
1217 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardf7b15102013-03-21 04:00:03 +00001218 else
1219 cd = 1;
1220 }
Thierry Redingb9c8b772012-01-02 01:15:37 +00001221
1222 return cd;
1223}
Simon Glass394dfc02016-06-12 23:30:22 -06001224#endif
Thierry Redingb9c8b772012-01-02 01:15:37 +00001225
Marek Vasuta318a7a2018-04-15 00:37:11 +02001226#if !CONFIG_IS_ENABLED(MMC_TINY)
Kim Phillips87ea3892012-10-29 13:34:43 +00001227static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Flemingad347bb2008-10-30 16:41:01 -05001228{
1229 struct mmc_cmd cmd;
1230 struct mmc_data data;
1231
1232 /* Switch the frequency */
1233 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1234 cmd.resp_type = MMC_RSP_R1;
1235 cmd.cmdarg = (mode << 31) | 0xffffff;
1236 cmd.cmdarg &= ~(0xf << (group * 4));
1237 cmd.cmdarg |= value << (group * 4);
Andy Flemingad347bb2008-10-30 16:41:01 -05001238
1239 data.dest = (char *)resp;
1240 data.blocksize = 64;
1241 data.blocks = 1;
1242 data.flags = MMC_DATA_READ;
1243
1244 return mmc_send_cmd(mmc, &cmd, &data);
1245}
1246
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001247static int sd_get_capabilities(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05001248{
1249 int err;
1250 struct mmc_cmd cmd;
Suniel Mahesh2f423da2017-10-05 11:32:00 +05301251 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1252 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Flemingad347bb2008-10-30 16:41:01 -05001253 struct mmc_data data;
1254 int timeout;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001255#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001256 u32 sd3_bus_mode;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001257#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001258
Faiz Abbas01db77e2020-02-26 13:44:32 +05301259 mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
Andy Flemingad347bb2008-10-30 16:41:01 -05001260
Thomas Chou1254c3d2010-12-24 13:12:21 +00001261 if (mmc_host_is_spi(mmc))
1262 return 0;
1263
Andy Flemingad347bb2008-10-30 16:41:01 -05001264 /* Read the SCR to find out if this card supports higher speeds */
1265 cmd.cmdidx = MMC_CMD_APP_CMD;
1266 cmd.resp_type = MMC_RSP_R1;
1267 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -05001268
1269 err = mmc_send_cmd(mmc, &cmd, NULL);
1270
1271 if (err)
1272 return err;
1273
1274 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1275 cmd.resp_type = MMC_RSP_R1;
1276 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -05001277
1278 timeout = 3;
1279
1280retry_scr:
Anton staaf9b00f0d2011-10-03 13:54:59 +00001281 data.dest = (char *)scr;
Andy Flemingad347bb2008-10-30 16:41:01 -05001282 data.blocksize = 8;
1283 data.blocks = 1;
1284 data.flags = MMC_DATA_READ;
1285
1286 err = mmc_send_cmd(mmc, &cmd, &data);
1287
1288 if (err) {
1289 if (timeout--)
1290 goto retry_scr;
1291
1292 return err;
1293 }
1294
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +03001295 mmc->scr[0] = __be32_to_cpu(scr[0]);
1296 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Flemingad347bb2008-10-30 16:41:01 -05001297
1298 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng4a4ef872016-03-17 21:53:13 -07001299 case 0:
1300 mmc->version = SD_VERSION_1_0;
1301 break;
1302 case 1:
1303 mmc->version = SD_VERSION_1_10;
1304 break;
1305 case 2:
1306 mmc->version = SD_VERSION_2;
1307 if ((mmc->scr[0] >> 15) & 0x1)
1308 mmc->version = SD_VERSION_3;
1309 break;
1310 default:
1311 mmc->version = SD_VERSION_1_0;
1312 break;
Andy Flemingad347bb2008-10-30 16:41:01 -05001313 }
1314
Alagu Sankar24bb5ab2010-05-12 15:08:24 +05301315 if (mmc->scr[0] & SD_DATA_4BIT)
1316 mmc->card_caps |= MMC_MODE_4BIT;
1317
Andy Flemingad347bb2008-10-30 16:41:01 -05001318 /* Version 1.0 doesn't support switching */
1319 if (mmc->version == SD_VERSION_1_0)
1320 return 0;
1321
1322 timeout = 4;
1323 while (timeout--) {
1324 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaf9b00f0d2011-10-03 13:54:59 +00001325 (u8 *)switch_status);
Andy Flemingad347bb2008-10-30 16:41:01 -05001326
1327 if (err)
1328 return err;
1329
1330 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +03001331 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Flemingad347bb2008-10-30 16:41:01 -05001332 break;
1333 }
1334
Andy Flemingad347bb2008-10-30 16:41:01 -05001335 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001336 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1337 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Flemingad347bb2008-10-30 16:41:01 -05001338
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001339#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001340 /* Version before 3.0 don't support UHS modes */
1341 if (mmc->version < SD_VERSION_3)
1342 return 0;
1343
1344 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1345 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1346 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1347 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1348 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1349 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1350 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1351 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1352 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1353 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1354 mmc->card_caps |= MMC_CAP(UHS_DDR50);
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001355#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001356
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001357 return 0;
1358}
1359
1360static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1361{
1362 int err;
1363
1364 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001365 int speed;
Macpaul Lin24e92ec2011-11-28 16:31:09 +00001366
Marek Vasut4105e972018-11-18 03:25:08 +01001367 /* SD version 1.00 and 1.01 does not support CMD 6 */
1368 if (mmc->version == SD_VERSION_1_0)
1369 return 0;
1370
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001371 switch (mode) {
Faiz Abbas01db77e2020-02-26 13:44:32 +05301372 case MMC_LEGACY:
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001373 speed = UHS_SDR12_BUS_SPEED;
1374 break;
1375 case SD_HS:
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +01001376 speed = HIGH_SPEED_BUS_SPEED;
1377 break;
1378#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1379 case UHS_SDR12:
1380 speed = UHS_SDR12_BUS_SPEED;
1381 break;
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001382 case UHS_SDR25:
1383 speed = UHS_SDR25_BUS_SPEED;
1384 break;
1385 case UHS_SDR50:
1386 speed = UHS_SDR50_BUS_SPEED;
1387 break;
1388 case UHS_DDR50:
1389 speed = UHS_DDR50_BUS_SPEED;
1390 break;
1391 case UHS_SDR104:
1392 speed = UHS_SDR104_BUS_SPEED;
1393 break;
Jean-Jacques Hiblot74c98b22018-01-04 15:23:30 +01001394#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001395 default:
1396 return -EINVAL;
1397 }
1398
1399 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001400 if (err)
1401 return err;
1402
Jean-Jacques Hiblote7f664e2018-02-09 12:09:27 +01001403 if (((__be32_to_cpu(switch_status[4]) >> 24) & 0xF) != speed)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001404 return -ENOTSUPP;
1405
1406 return 0;
1407}
Andy Flemingad347bb2008-10-30 16:41:01 -05001408
Marek Vasut8ff55fb2018-04-15 00:36:45 +02001409static int sd_select_bus_width(struct mmc *mmc, int w)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001410{
1411 int err;
1412 struct mmc_cmd cmd;
1413
1414 if ((w != 4) && (w != 1))
1415 return -EINVAL;
1416
1417 cmd.cmdidx = MMC_CMD_APP_CMD;
1418 cmd.resp_type = MMC_RSP_R1;
1419 cmd.cmdarg = mmc->rca << 16;
1420
1421 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -05001422 if (err)
1423 return err;
1424
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001425 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1426 cmd.resp_type = MMC_RSP_R1;
1427 if (w == 4)
1428 cmd.cmdarg = 2;
1429 else if (w == 1)
1430 cmd.cmdarg = 0;
1431 err = mmc_send_cmd(mmc, &cmd, NULL);
1432 if (err)
1433 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05001434
1435 return 0;
1436}
Marek Vasuta318a7a2018-04-15 00:37:11 +02001437#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001438
Jean-Jacques Hiblotcb534f02018-01-04 15:23:33 +01001439#if CONFIG_IS_ENABLED(MMC_WRITE)
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001440static int sd_read_ssr(struct mmc *mmc)
1441{
Jean-Jacques Hiblotcb534f02018-01-04 15:23:33 +01001442 static const unsigned int sd_au_size[] = {
1443 0, SZ_16K / 512, SZ_32K / 512,
1444 SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
1445 SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
1446 SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
1447 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512,
1448 SZ_64M / 512,
1449 };
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001450 int err, i;
1451 struct mmc_cmd cmd;
1452 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1453 struct mmc_data data;
1454 int timeout = 3;
1455 unsigned int au, eo, et, es;
1456
1457 cmd.cmdidx = MMC_CMD_APP_CMD;
1458 cmd.resp_type = MMC_RSP_R1;
1459 cmd.cmdarg = mmc->rca << 16;
1460
1461 err = mmc_send_cmd(mmc, &cmd, NULL);
Joel Johnson5ea041b2020-01-11 09:08:14 -07001462#ifdef CONFIG_MMC_QUIRKS
1463 if (err && (mmc->quirks & MMC_QUIRK_RETRY_APP_CMD)) {
1464 int retries = 4;
1465 /*
1466 * It has been seen that APP_CMD may fail on the first
1467 * attempt, let's try a few more times
1468 */
1469 do {
1470 err = mmc_send_cmd(mmc, &cmd, NULL);
1471 if (!err)
1472 break;
1473 } while (retries--);
1474 }
1475#endif
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001476 if (err)
1477 return err;
1478
1479 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1480 cmd.resp_type = MMC_RSP_R1;
1481 cmd.cmdarg = 0;
1482
1483retry_ssr:
1484 data.dest = (char *)ssr;
1485 data.blocksize = 64;
1486 data.blocks = 1;
1487 data.flags = MMC_DATA_READ;
1488
1489 err = mmc_send_cmd(mmc, &cmd, &data);
1490 if (err) {
1491 if (timeout--)
1492 goto retry_ssr;
1493
1494 return err;
1495 }
1496
1497 for (i = 0; i < 16; i++)
1498 ssr[i] = be32_to_cpu(ssr[i]);
1499
1500 au = (ssr[2] >> 12) & 0xF;
1501 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1502 mmc->ssr.au = sd_au_size[au];
1503 es = (ssr[3] >> 24) & 0xFF;
1504 es |= (ssr[2] & 0xFF) << 8;
1505 et = (ssr[3] >> 18) & 0x3F;
1506 if (es && et) {
1507 eo = (ssr[3] >> 16) & 0x3;
1508 mmc->ssr.erase_timeout = (et * 1000) / es;
1509 mmc->ssr.erase_offset = eo * 1000;
1510 }
1511 } else {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001512 pr_debug("Invalid Allocation Unit Size.\n");
Peng Fanb3fcf1e2016-09-01 11:13:38 +08001513 }
1514
1515 return 0;
1516}
Jean-Jacques Hiblotcb534f02018-01-04 15:23:33 +01001517#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001518/* frequency bases */
1519/* divided by 10 to be nice to platforms without floating point */
Mike Frysingerb588caf2010-10-20 01:15:53 +00001520static const int fbase[] = {
Andy Flemingad347bb2008-10-30 16:41:01 -05001521 10000,
1522 100000,
1523 1000000,
1524 10000000,
1525};
1526
1527/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1528 * to platforms without floating point.
1529 */
Simon Glass03317cc2016-05-14 14:02:57 -06001530static const u8 multipliers[] = {
Andy Flemingad347bb2008-10-30 16:41:01 -05001531 0, /* reserved */
1532 10,
1533 12,
1534 13,
1535 15,
1536 20,
1537 25,
1538 30,
1539 35,
1540 40,
1541 45,
1542 50,
1543 55,
1544 60,
1545 70,
1546 80,
1547};
1548
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001549static inline int bus_width(uint cap)
1550{
1551 if (cap == MMC_MODE_8BIT)
1552 return 8;
1553 if (cap == MMC_MODE_4BIT)
1554 return 4;
1555 if (cap == MMC_MODE_1BIT)
1556 return 1;
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01001557 pr_warn("invalid bus witdh capability 0x%x\n", cap);
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001558 return 0;
1559}
1560
Simon Glasseba48f92017-07-29 11:35:31 -06001561#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001562#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham Iae7174f2017-09-21 16:30:05 +02001563static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1564{
1565 return -ENOTSUPP;
1566}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001567#endif
Kishon Vijay Abraham Iae7174f2017-09-21 16:30:05 +02001568
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001569static int mmc_set_ios(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05001570{
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001571 int ret = 0;
1572
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001573 if (mmc->cfg->ops->set_ios)
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001574 ret = mmc->cfg->ops->set_ios(mmc);
1575
1576 return ret;
Andy Flemingad347bb2008-10-30 16:41:01 -05001577}
Yann Gautier6f558332019-09-19 17:56:12 +02001578
1579static int mmc_host_power_cycle(struct mmc *mmc)
1580{
1581 int ret = 0;
1582
1583 if (mmc->cfg->ops->host_power_cycle)
1584 ret = mmc->cfg->ops->host_power_cycle(mmc);
1585
1586 return ret;
1587}
Simon Glass394dfc02016-06-12 23:30:22 -06001588#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001589
Kishon Vijay Abraham Id6246bf2017-09-21 16:30:03 +02001590int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
Andy Flemingad347bb2008-10-30 16:41:01 -05001591{
Jaehoon Chungab4d4052018-01-23 14:04:30 +09001592 if (!disable) {
Jaehoon Chung8a933292018-01-17 19:36:58 +09001593 if (clock > mmc->cfg->f_max)
1594 clock = mmc->cfg->f_max;
Andy Flemingad347bb2008-10-30 16:41:01 -05001595
Jaehoon Chung8a933292018-01-17 19:36:58 +09001596 if (clock < mmc->cfg->f_min)
1597 clock = mmc->cfg->f_min;
1598 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001599
1600 mmc->clock = clock;
Kishon Vijay Abraham Id6246bf2017-09-21 16:30:03 +02001601 mmc->clk_disable = disable;
Andy Flemingad347bb2008-10-30 16:41:01 -05001602
Jaehoon Chungc8477d62018-01-26 19:25:30 +09001603 debug("clock is %s (%dHz)\n", disable ? "disabled" : "enabled", clock);
1604
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001605 return mmc_set_ios(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05001606}
1607
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001608static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Flemingad347bb2008-10-30 16:41:01 -05001609{
1610 mmc->bus_width = width;
1611
Kishon Vijay Abraham Ie178c112017-09-21 16:29:59 +02001612 return mmc_set_ios(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05001613}
1614
Jean-Jacques Hiblot00de5042017-09-21 16:29:54 +02001615#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1616/*
1617 * helper function to display the capabilities in a human
1618 * friendly manner. The capabilities include bus width and
1619 * supported modes.
1620 */
1621void mmc_dump_capabilities(const char *text, uint caps)
1622{
1623 enum bus_mode mode;
1624
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001625 pr_debug("%s: widths [", text);
Jean-Jacques Hiblot00de5042017-09-21 16:29:54 +02001626 if (caps & MMC_MODE_8BIT)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001627 pr_debug("8, ");
Jean-Jacques Hiblot00de5042017-09-21 16:29:54 +02001628 if (caps & MMC_MODE_4BIT)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001629 pr_debug("4, ");
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001630 if (caps & MMC_MODE_1BIT)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001631 pr_debug("1, ");
1632 pr_debug("\b\b] modes [");
Jean-Jacques Hiblot00de5042017-09-21 16:29:54 +02001633 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1634 if (MMC_CAP(mode) & caps)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001635 pr_debug("%s, ", mmc_mode_name(mode));
1636 pr_debug("\b\b]\n");
Jean-Jacques Hiblot00de5042017-09-21 16:29:54 +02001637}
1638#endif
1639
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001640struct mode_width_tuning {
1641 enum bus_mode mode;
1642 uint widths;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001643#ifdef MMC_SUPPORTS_TUNING
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +02001644 uint tuning;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001645#endif
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001646};
1647
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001648#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001649int mmc_voltage_to_mv(enum mmc_voltage voltage)
1650{
1651 switch (voltage) {
1652 case MMC_SIGNAL_VOLTAGE_000: return 0;
1653 case MMC_SIGNAL_VOLTAGE_330: return 3300;
1654 case MMC_SIGNAL_VOLTAGE_180: return 1800;
1655 case MMC_SIGNAL_VOLTAGE_120: return 1200;
1656 }
1657 return -EINVAL;
1658}
1659
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +02001660static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1661{
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001662 int err;
1663
1664 if (mmc->signal_voltage == signal_voltage)
1665 return 0;
1666
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +02001667 mmc->signal_voltage = signal_voltage;
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001668 err = mmc_set_ios(mmc);
1669 if (err)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001670 pr_debug("unable to set voltage (err %d)\n", err);
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001671
1672 return err;
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +02001673}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001674#else
1675static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1676{
1677 return 0;
1678}
1679#endif
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +02001680
Marek Vasuta318a7a2018-04-15 00:37:11 +02001681#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001682static const struct mode_width_tuning sd_modes_by_pref[] = {
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001683#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1684#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001685 {
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001686 .mode = UHS_SDR104,
1687 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1688 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1689 },
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001690#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001691 {
1692 .mode = UHS_SDR50,
1693 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1694 },
1695 {
1696 .mode = UHS_DDR50,
1697 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1698 },
1699 {
1700 .mode = UHS_SDR25,
1701 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1702 },
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001703#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001704 {
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001705 .mode = SD_HS,
1706 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1707 },
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001708#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001709 {
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001710 .mode = UHS_SDR12,
1711 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1712 },
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001713#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001714 {
Faiz Abbas01db77e2020-02-26 13:44:32 +05301715 .mode = MMC_LEGACY,
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001716 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1717 }
1718};
1719
1720#define for_each_sd_mode_by_pref(caps, mwt) \
1721 for (mwt = sd_modes_by_pref;\
1722 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1723 mwt++) \
1724 if (caps & MMC_CAP(mwt->mode))
1725
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02001726static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001727{
1728 int err;
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001729 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1730 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001731#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001732 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001733#else
1734 bool uhs_en = false;
1735#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001736 uint caps;
1737
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +01001738#ifdef DEBUG
1739 mmc_dump_capabilities("sd card", card_caps);
Jean-Jacques Hiblotd7e5e032017-11-30 17:43:57 +01001740 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +01001741#endif
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001742
Anup Pateld9c92c72019-07-08 04:10:43 +00001743 if (mmc_host_is_spi(mmc)) {
1744 mmc_set_bus_width(mmc, 1);
Faiz Abbas01db77e2020-02-26 13:44:32 +05301745 mmc_select_mode(mmc, MMC_LEGACY);
Anup Pateld9c92c72019-07-08 04:10:43 +00001746 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
1747 return 0;
1748 }
1749
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001750 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblotd7e5e032017-11-30 17:43:57 +01001751 caps = card_caps & mmc->host_caps;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001752
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001753 if (!uhs_en)
1754 caps &= ~UHS_CAPS;
1755
1756 for_each_sd_mode_by_pref(caps, mwt) {
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001757 uint *w;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001758
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001759 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001760 if (*w & caps & mwt->widths) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001761 pr_debug("trying mode %s width %d (at %d MHz)\n",
1762 mmc_mode_name(mwt->mode),
1763 bus_width(*w),
1764 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001765
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001766 /* configure the bus width (card + host) */
1767 err = sd_select_bus_width(mmc, bus_width(*w));
1768 if (err)
1769 goto error;
1770 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001771
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001772 /* configure the bus mode (card) */
1773 err = sd_set_card_speed(mmc, mwt->mode);
1774 if (err)
1775 goto error;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001776
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001777 /* configure the bus mode (host) */
1778 mmc_select_mode(mmc, mwt->mode);
Jaehoon Chung239cb2f2018-01-26 19:25:29 +09001779 mmc_set_clock(mmc, mmc->tran_speed,
1780 MMC_CLK_ENABLE);
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001781
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001782#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001783 /* execute tuning if needed */
1784 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1785 err = mmc_execute_tuning(mmc,
1786 mwt->tuning);
1787 if (err) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001788 pr_debug("tuning failed\n");
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001789 goto error;
1790 }
1791 }
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001792#endif
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02001793
Jean-Jacques Hiblotcb534f02018-01-04 15:23:33 +01001794#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001795 err = sd_read_ssr(mmc);
Peng Fan2d2fe8e2018-03-05 16:20:40 +08001796 if (err)
Jean-Jacques Hiblotcb534f02018-01-04 15:23:33 +01001797 pr_warn("unable to read ssr\n");
1798#endif
1799 if (!err)
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001800 return 0;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001801
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001802error:
1803 /* revert to a safer bus speed */
Faiz Abbas01db77e2020-02-26 13:44:32 +05301804 mmc_select_mode(mmc, MMC_LEGACY);
Jaehoon Chung239cb2f2018-01-26 19:25:29 +09001805 mmc_set_clock(mmc, mmc->tran_speed,
1806 MMC_CLK_ENABLE);
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001807 }
1808 }
1809 }
1810
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09001811 pr_err("unable to select a mode\n");
Jean-Jacques Hiblot5b1a4d92017-09-21 16:29:57 +02001812 return -ENOTSUPP;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001813}
1814
Jean-Jacques Hiblot933d1262017-09-21 16:29:52 +02001815/*
1816 * read the compare the part of ext csd that is constant.
1817 * This can be used to check that the transfer is working
1818 * as expected.
1819 */
1820static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02001821{
Jean-Jacques Hiblot933d1262017-09-21 16:29:52 +02001822 int err;
Jean-Jacques Hibloted9506b2017-09-21 16:29:51 +02001823 const u8 *ext_csd = mmc->ext_csd;
Jean-Jacques Hiblot933d1262017-09-21 16:29:52 +02001824 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1825
Jean-Jacques Hiblot7ab1b622017-11-30 17:43:58 +01001826 if (mmc->version < MMC_VERSION_4)
1827 return 0;
1828
Jean-Jacques Hiblot933d1262017-09-21 16:29:52 +02001829 err = mmc_send_ext_csd(mmc, test_csd);
1830 if (err)
1831 return err;
1832
1833 /* Only compare read only fields */
1834 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1835 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1836 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1837 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1838 ext_csd[EXT_CSD_REV]
1839 == test_csd[EXT_CSD_REV] &&
1840 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1841 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1842 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1843 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1844 return 0;
1845
1846 return -EBADMSG;
1847}
1848
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001849#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001850static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1851 uint32_t allowed_mask)
1852{
1853 u32 card_mask = 0;
1854
1855 switch (mode) {
Peng Faneede83b2019-07-10 14:43:07 +08001856 case MMC_HS_400_ES:
Peng Fan46801252018-08-10 14:07:54 +08001857 case MMC_HS_400:
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001858 case MMC_HS_200:
Peng Fan46801252018-08-10 14:07:54 +08001859 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_8V |
1860 EXT_CSD_CARD_TYPE_HS400_1_8V))
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001861 card_mask |= MMC_SIGNAL_VOLTAGE_180;
Peng Fan46801252018-08-10 14:07:54 +08001862 if (mmc->cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
1863 EXT_CSD_CARD_TYPE_HS400_1_2V))
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001864 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1865 break;
1866 case MMC_DDR_52:
1867 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
1868 card_mask |= MMC_SIGNAL_VOLTAGE_330 |
1869 MMC_SIGNAL_VOLTAGE_180;
1870 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
1871 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1872 break;
1873 default:
1874 card_mask |= MMC_SIGNAL_VOLTAGE_330;
1875 break;
1876 }
1877
1878 while (card_mask & allowed_mask) {
1879 enum mmc_voltage best_match;
1880
1881 best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
1882 if (!mmc_set_signal_voltage(mmc, best_match))
1883 return 0;
1884
1885 allowed_mask &= ~best_match;
1886 }
1887
1888 return -ENOTSUPP;
1889}
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001890#else
1891static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1892 uint32_t allowed_mask)
1893{
1894 return 0;
1895}
1896#endif
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02001897
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02001898static const struct mode_width_tuning mmc_modes_by_pref[] = {
Peng Faneede83b2019-07-10 14:43:07 +08001899#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1900 {
1901 .mode = MMC_HS_400_ES,
1902 .widths = MMC_MODE_8BIT,
1903 },
1904#endif
Peng Fan46801252018-08-10 14:07:54 +08001905#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1906 {
1907 .mode = MMC_HS_400,
1908 .widths = MMC_MODE_8BIT,
1909 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
1910 },
1911#endif
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001912#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02001913 {
1914 .mode = MMC_HS_200,
1915 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +02001916 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02001917 },
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01001918#endif
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02001919 {
1920 .mode = MMC_DDR_52,
1921 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1922 },
1923 {
1924 .mode = MMC_HS_52,
1925 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1926 },
1927 {
1928 .mode = MMC_HS,
1929 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1930 },
1931 {
1932 .mode = MMC_LEGACY,
1933 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1934 }
1935};
1936
1937#define for_each_mmc_mode_by_pref(caps, mwt) \
1938 for (mwt = mmc_modes_by_pref;\
1939 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
1940 mwt++) \
1941 if (caps & MMC_CAP(mwt->mode))
1942
1943static const struct ext_csd_bus_width {
1944 uint cap;
1945 bool is_ddr;
1946 uint ext_csd_bits;
1947} ext_csd_bus_width[] = {
1948 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
1949 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
1950 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
1951 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
1952 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
1953};
1954
Peng Fan46801252018-08-10 14:07:54 +08001955#if CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
1956static int mmc_select_hs400(struct mmc *mmc)
1957{
1958 int err;
1959
1960 /* Set timing to HS200 for tuning */
Marek Vasut111572f2019-01-03 21:19:24 +01001961 err = mmc_set_card_speed(mmc, MMC_HS_200, false);
Peng Fan46801252018-08-10 14:07:54 +08001962 if (err)
1963 return err;
1964
1965 /* configure the bus mode (host) */
1966 mmc_select_mode(mmc, MMC_HS_200);
1967 mmc_set_clock(mmc, mmc->tran_speed, false);
1968
1969 /* execute tuning if needed */
1970 err = mmc_execute_tuning(mmc, MMC_CMD_SEND_TUNING_BLOCK_HS200);
1971 if (err) {
1972 debug("tuning failed\n");
1973 return err;
1974 }
1975
1976 /* Set back to HS */
BOUGH CHEN8702bbc2019-03-26 06:24:17 +00001977 mmc_set_card_speed(mmc, MMC_HS, true);
Peng Fan46801252018-08-10 14:07:54 +08001978
1979 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1980 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG);
1981 if (err)
1982 return err;
1983
Marek Vasut111572f2019-01-03 21:19:24 +01001984 err = mmc_set_card_speed(mmc, MMC_HS_400, false);
Peng Fan46801252018-08-10 14:07:54 +08001985 if (err)
1986 return err;
1987
1988 mmc_select_mode(mmc, MMC_HS_400);
1989 err = mmc_set_clock(mmc, mmc->tran_speed, false);
1990 if (err)
1991 return err;
1992
1993 return 0;
1994}
1995#else
1996static int mmc_select_hs400(struct mmc *mmc)
1997{
1998 return -ENOTSUPP;
1999}
2000#endif
2001
Peng Faneede83b2019-07-10 14:43:07 +08002002#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
2003#if !CONFIG_IS_ENABLED(DM_MMC)
2004static int mmc_set_enhanced_strobe(struct mmc *mmc)
2005{
2006 return -ENOTSUPP;
2007}
2008#endif
2009static int mmc_select_hs400es(struct mmc *mmc)
2010{
2011 int err;
2012
2013 err = mmc_set_card_speed(mmc, MMC_HS, true);
2014 if (err)
2015 return err;
2016
2017 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
2018 EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_FLAG |
2019 EXT_CSD_BUS_WIDTH_STROBE);
2020 if (err) {
2021 printf("switch to bus width for hs400 failed\n");
2022 return err;
2023 }
2024 /* TODO: driver strength */
2025 err = mmc_set_card_speed(mmc, MMC_HS_400_ES, false);
2026 if (err)
2027 return err;
2028
2029 mmc_select_mode(mmc, MMC_HS_400_ES);
2030 err = mmc_set_clock(mmc, mmc->tran_speed, false);
2031 if (err)
2032 return err;
2033
2034 return mmc_set_enhanced_strobe(mmc);
2035}
2036#else
2037static int mmc_select_hs400es(struct mmc *mmc)
2038{
2039 return -ENOTSUPP;
2040}
2041#endif
2042
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002043#define for_each_supported_width(caps, ddr, ecbv) \
2044 for (ecbv = ext_csd_bus_width;\
2045 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
2046 ecbv++) \
2047 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
2048
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02002049static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot933d1262017-09-21 16:29:52 +02002050{
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002051 int err;
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002052 const struct mode_width_tuning *mwt;
2053 const struct ext_csd_bus_width *ecbw;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002054
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +01002055#ifdef DEBUG
2056 mmc_dump_capabilities("mmc", card_caps);
Jean-Jacques Hiblotd7e5e032017-11-30 17:43:57 +01002057 mmc_dump_capabilities("host", mmc->host_caps);
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +01002058#endif
2059
Anup Pateld9c92c72019-07-08 04:10:43 +00002060 if (mmc_host_is_spi(mmc)) {
2061 mmc_set_bus_width(mmc, 1);
2062 mmc_select_mode(mmc, MMC_LEGACY);
2063 mmc_set_clock(mmc, mmc->tran_speed, MMC_CLK_ENABLE);
2064 return 0;
2065 }
2066
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002067 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblotd7e5e032017-11-30 17:43:57 +01002068 card_caps &= mmc->host_caps;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002069
2070 /* Only version 4 of MMC supports wider bus widths */
2071 if (mmc->version < MMC_VERSION_4)
2072 return 0;
2073
Jean-Jacques Hibloted9506b2017-09-21 16:29:51 +02002074 if (!mmc->ext_csd) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002075 pr_debug("No ext_csd found!\n"); /* this should enver happen */
Jean-Jacques Hibloted9506b2017-09-21 16:29:51 +02002076 return -ENOTSUPP;
2077 }
2078
Marek Vasut111572f2019-01-03 21:19:24 +01002079#if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
2080 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2081 /*
2082 * In case the eMMC is in HS200/HS400 mode, downgrade to HS mode
2083 * before doing anything else, since a transition from either of
2084 * the HS200/HS400 mode directly to legacy mode is not supported.
2085 */
2086 if (mmc->selected_mode == MMC_HS_200 ||
2087 mmc->selected_mode == MMC_HS_400)
2088 mmc_set_card_speed(mmc, MMC_HS, true);
2089 else
2090#endif
2091 mmc_set_clock(mmc, mmc->legacy_speed, MMC_CLK_ENABLE);
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02002092
2093 for_each_mmc_mode_by_pref(card_caps, mwt) {
2094 for_each_supported_width(card_caps & mwt->widths,
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002095 mmc_is_mode_ddr(mwt->mode), ecbw) {
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02002096 enum mmc_voltage old_voltage;
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002097 pr_debug("trying mode %s width %d (at %d MHz)\n",
2098 mmc_mode_name(mwt->mode),
2099 bus_width(ecbw->cap),
2100 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02002101 old_voltage = mmc->signal_voltage;
2102 err = mmc_set_lowest_voltage(mmc, mwt->mode,
2103 MMC_ALL_SIGNAL_VOLTAGE);
2104 if (err)
2105 continue;
2106
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002107 /* configure the bus width (card + host) */
2108 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2109 EXT_CSD_BUS_WIDTH,
2110 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
2111 if (err)
2112 goto error;
2113 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002114
Peng Fan46801252018-08-10 14:07:54 +08002115 if (mwt->mode == MMC_HS_400) {
2116 err = mmc_select_hs400(mmc);
2117 if (err) {
2118 printf("Select HS400 failed %d\n", err);
2119 goto error;
2120 }
Peng Faneede83b2019-07-10 14:43:07 +08002121 } else if (mwt->mode == MMC_HS_400_ES) {
2122 err = mmc_select_hs400es(mmc);
2123 if (err) {
2124 printf("Select HS400ES failed %d\n",
2125 err);
2126 goto error;
2127 }
Peng Fan46801252018-08-10 14:07:54 +08002128 } else {
2129 /* configure the bus speed (card) */
Marek Vasut111572f2019-01-03 21:19:24 +01002130 err = mmc_set_card_speed(mmc, mwt->mode, false);
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002131 if (err)
2132 goto error;
Peng Fan46801252018-08-10 14:07:54 +08002133
2134 /*
2135 * configure the bus width AND the ddr mode
2136 * (card). The host side will be taken care
2137 * of in the next step
2138 */
2139 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
2140 err = mmc_switch(mmc,
2141 EXT_CSD_CMD_SET_NORMAL,
2142 EXT_CSD_BUS_WIDTH,
2143 ecbw->ext_csd_bits);
2144 if (err)
2145 goto error;
2146 }
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002147
Peng Fan46801252018-08-10 14:07:54 +08002148 /* configure the bus mode (host) */
2149 mmc_select_mode(mmc, mwt->mode);
2150 mmc_set_clock(mmc, mmc->tran_speed,
2151 MMC_CLK_ENABLE);
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01002152#ifdef MMC_SUPPORTS_TUNING
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002153
Peng Fan46801252018-08-10 14:07:54 +08002154 /* execute tuning if needed */
2155 if (mwt->tuning) {
2156 err = mmc_execute_tuning(mmc,
2157 mwt->tuning);
2158 if (err) {
2159 pr_debug("tuning failed\n");
2160 goto error;
2161 }
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +02002162 }
Jean-Jacques Hiblot6051e782017-11-30 17:44:01 +01002163#endif
Peng Fan46801252018-08-10 14:07:54 +08002164 }
Kishon Vijay Abraham I210369f2017-09-21 16:30:06 +02002165
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002166 /* do a transfer to check the configuration */
2167 err = mmc_read_and_compare_ext_csd(mmc);
2168 if (!err)
2169 return 0;
2170error:
Jean-Jacques Hiblotb6937d62017-09-21 16:30:11 +02002171 mmc_set_signal_voltage(mmc, old_voltage);
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002172 /* if an error occured, revert to a safer bus mode */
2173 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2174 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
2175 mmc_select_mode(mmc, MMC_LEGACY);
2176 mmc_set_bus_width(mmc, 1);
2177 }
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002178 }
2179
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01002180 pr_err("unable to select a mode\n");
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002181
Jean-Jacques Hiblotec346832017-09-21 16:29:58 +02002182 return -ENOTSUPP;
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002183}
Marek Vasuta318a7a2018-04-15 00:37:11 +02002184#endif
2185
2186#if CONFIG_IS_ENABLED(MMC_TINY)
2187DEFINE_CACHE_ALIGN_BUFFER(u8, ext_csd_bkup, MMC_MAX_BLOCK_LEN);
2188#endif
Jean-Jacques Hiblot31e7cf32017-09-21 16:29:49 +02002189
Jean-Jacques Hibloted9506b2017-09-21 16:29:51 +02002190static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002191{
2192 int err, i;
2193 u64 capacity;
2194 bool has_parts = false;
2195 bool part_completed;
Jean-Jacques Hiblotfa6c5772018-01-04 15:23:31 +01002196 static const u32 mmc_versions[] = {
2197 MMC_VERSION_4,
2198 MMC_VERSION_4_1,
2199 MMC_VERSION_4_2,
2200 MMC_VERSION_4_3,
Jean-Jacques Hiblotc64862b2018-02-09 12:09:28 +01002201 MMC_VERSION_4_4,
Jean-Jacques Hiblotfa6c5772018-01-04 15:23:31 +01002202 MMC_VERSION_4_41,
2203 MMC_VERSION_4_5,
2204 MMC_VERSION_5_0,
2205 MMC_VERSION_5_1
2206 };
2207
Marek Vasuta318a7a2018-04-15 00:37:11 +02002208#if CONFIG_IS_ENABLED(MMC_TINY)
2209 u8 *ext_csd = ext_csd_bkup;
2210
2211 if (IS_SD(mmc) || mmc->version < MMC_VERSION_4)
2212 return 0;
2213
2214 if (!mmc->ext_csd)
2215 memset(ext_csd_bkup, 0, sizeof(ext_csd_bkup));
2216
2217 err = mmc_send_ext_csd(mmc, ext_csd);
2218 if (err)
2219 goto error;
2220
2221 /* store the ext csd for future reference */
2222 if (!mmc->ext_csd)
2223 mmc->ext_csd = ext_csd;
2224#else
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002225 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002226
2227 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
2228 return 0;
2229
2230 /* check ext_csd version and capacity */
2231 err = mmc_send_ext_csd(mmc, ext_csd);
2232 if (err)
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002233 goto error;
2234
2235 /* store the ext csd for future reference */
2236 if (!mmc->ext_csd)
2237 mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN);
2238 if (!mmc->ext_csd)
2239 return -ENOMEM;
2240 memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN);
Marek Vasuta318a7a2018-04-15 00:37:11 +02002241#endif
Alexander Kochetkovf1133c92018-02-20 14:35:55 +03002242 if (ext_csd[EXT_CSD_REV] >= ARRAY_SIZE(mmc_versions))
Jean-Jacques Hiblotfa6c5772018-01-04 15:23:31 +01002243 return -EINVAL;
2244
2245 mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]];
2246
2247 if (mmc->version >= MMC_VERSION_4_2) {
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002248 /*
2249 * According to the JEDEC Standard, the value of
2250 * ext_csd's capacity is valid if the value is more
2251 * than 2GB
2252 */
2253 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
2254 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
2255 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
2256 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2257 capacity *= MMC_MAX_BLOCK_LEN;
2258 if ((capacity >> 20) > 2 * 1024)
2259 mmc->capacity_user = capacity;
2260 }
2261
Jean-Jacques Hiblot201559c2019-07-02 10:53:54 +02002262 if (mmc->version >= MMC_VERSION_4_5)
2263 mmc->gen_cmd6_time = ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
2264
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002265 /* The partition data may be non-zero but it is only
2266 * effective if PARTITION_SETTING_COMPLETED is set in
2267 * EXT_CSD, so ignore any data if this bit is not set,
2268 * except for enabling the high-capacity group size
2269 * definition (see below).
2270 */
2271 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
2272 EXT_CSD_PARTITION_SETTING_COMPLETED);
2273
Jean-Jacques Hiblot7f5b1692019-07-02 10:53:55 +02002274 mmc->part_switch_time = ext_csd[EXT_CSD_PART_SWITCH_TIME];
2275 /* Some eMMC set the value too low so set a minimum */
2276 if (mmc->part_switch_time < MMC_MIN_PART_SWITCH_TIME && mmc->part_switch_time)
2277 mmc->part_switch_time = MMC_MIN_PART_SWITCH_TIME;
2278
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002279 /* store the partition info of emmc */
2280 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
2281 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
2282 ext_csd[EXT_CSD_BOOT_MULT])
2283 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
2284 if (part_completed &&
2285 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
2286 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
2287
2288 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
2289
2290 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
2291
2292 for (i = 0; i < 4; i++) {
2293 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
2294 uint mult = (ext_csd[idx + 2] << 16) +
2295 (ext_csd[idx + 1] << 8) + ext_csd[idx];
2296 if (mult)
2297 has_parts = true;
2298 if (!part_completed)
2299 continue;
2300 mmc->capacity_gp[i] = mult;
2301 mmc->capacity_gp[i] *=
2302 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2303 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2304 mmc->capacity_gp[i] <<= 19;
2305 }
2306
Jean-Jacques Hiblotc94c5472018-01-04 15:23:35 +01002307#ifndef CONFIG_SPL_BUILD
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002308 if (part_completed) {
2309 mmc->enh_user_size =
2310 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
2311 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
2312 ext_csd[EXT_CSD_ENH_SIZE_MULT];
2313 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
2314 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
2315 mmc->enh_user_size <<= 19;
2316 mmc->enh_user_start =
2317 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
2318 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
2319 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
2320 ext_csd[EXT_CSD_ENH_START_ADDR];
2321 if (mmc->high_capacity)
2322 mmc->enh_user_start <<= 9;
2323 }
Jean-Jacques Hiblotc94c5472018-01-04 15:23:35 +01002324#endif
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002325
2326 /*
2327 * Host needs to enable ERASE_GRP_DEF bit if device is
2328 * partitioned. This bit will be lost every time after a reset
2329 * or power off. This will affect erase size.
2330 */
2331 if (part_completed)
2332 has_parts = true;
2333 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
2334 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
2335 has_parts = true;
2336 if (has_parts) {
2337 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
2338 EXT_CSD_ERASE_GROUP_DEF, 1);
2339
2340 if (err)
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002341 goto error;
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002342
2343 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
2344 }
2345
2346 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002347#if CONFIG_IS_ENABLED(MMC_WRITE)
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002348 /* Read out group size from ext_csd */
2349 mmc->erase_grp_size =
2350 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002351#endif
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002352 /*
2353 * if high capacity and partition setting completed
2354 * SEC_COUNT is valid even if it is smaller than 2 GiB
2355 * JEDEC Standard JESD84-B45, 6.2.4
2356 */
2357 if (mmc->high_capacity && part_completed) {
2358 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
2359 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
2360 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
2361 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
2362 capacity *= MMC_MAX_BLOCK_LEN;
2363 mmc->capacity_user = capacity;
2364 }
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002365 }
2366#if CONFIG_IS_ENABLED(MMC_WRITE)
2367 else {
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002368 /* Calculate the group size from the csd value. */
2369 int erase_gsz, erase_gmul;
2370
2371 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
2372 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
2373 mmc->erase_grp_size = (erase_gsz + 1)
2374 * (erase_gmul + 1);
2375 }
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002376#endif
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +01002377#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002378 mmc->hc_wp_grp_size = 1024
2379 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
2380 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +01002381#endif
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002382
2383 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
2384
2385 return 0;
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002386error:
2387 if (mmc->ext_csd) {
Marek Vasuta318a7a2018-04-15 00:37:11 +02002388#if !CONFIG_IS_ENABLED(MMC_TINY)
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002389 free(mmc->ext_csd);
Marek Vasuta318a7a2018-04-15 00:37:11 +02002390#endif
Jean-Jacques Hiblot06976eb2017-11-30 17:43:59 +01002391 mmc->ext_csd = NULL;
2392 }
2393 return err;
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002394}
2395
Kim Phillips87ea3892012-10-29 13:34:43 +00002396static int mmc_startup(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05002397{
Stephen Warrene315ae82013-06-11 15:14:01 -06002398 int err, i;
Andy Flemingad347bb2008-10-30 16:41:01 -05002399 uint mult, freq;
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002400 u64 cmult, csize;
Andy Flemingad347bb2008-10-30 16:41:01 -05002401 struct mmc_cmd cmd;
Simon Glasse5db1152016-05-01 13:52:35 -06002402 struct blk_desc *bdesc;
Andy Flemingad347bb2008-10-30 16:41:01 -05002403
Thomas Chou1254c3d2010-12-24 13:12:21 +00002404#ifdef CONFIG_MMC_SPI_CRC_ON
2405 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
2406 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
2407 cmd.resp_type = MMC_RSP_R1;
2408 cmd.cmdarg = 1;
Thomas Chou1254c3d2010-12-24 13:12:21 +00002409 err = mmc_send_cmd(mmc, &cmd, NULL);
Thomas Chou1254c3d2010-12-24 13:12:21 +00002410 if (err)
2411 return err;
2412 }
2413#endif
2414
Andy Flemingad347bb2008-10-30 16:41:01 -05002415 /* Put the Card in Identify Mode */
Thomas Chou1254c3d2010-12-24 13:12:21 +00002416 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
2417 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Flemingad347bb2008-10-30 16:41:01 -05002418 cmd.resp_type = MMC_RSP_R2;
2419 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -05002420
2421 err = mmc_send_cmd(mmc, &cmd, NULL);
2422
Kishon Vijay Abraham I07baaa62017-09-21 16:30:10 +02002423#ifdef CONFIG_MMC_QUIRKS
2424 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SEND_CID)) {
2425 int retries = 4;
2426 /*
2427 * It has been seen that SEND_CID may fail on the first
2428 * attempt, let's try a few more time
2429 */
2430 do {
2431 err = mmc_send_cmd(mmc, &cmd, NULL);
2432 if (!err)
2433 break;
2434 } while (retries--);
2435 }
2436#endif
2437
Andy Flemingad347bb2008-10-30 16:41:01 -05002438 if (err)
2439 return err;
2440
2441 memcpy(mmc->cid, cmd.response, 16);
2442
2443 /*
2444 * For MMC cards, set the Relative Address.
2445 * For SD cards, get the Relatvie Address.
2446 * This also puts the cards into Standby State
2447 */
Thomas Chou1254c3d2010-12-24 13:12:21 +00002448 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2449 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
2450 cmd.cmdarg = mmc->rca << 16;
2451 cmd.resp_type = MMC_RSP_R6;
Andy Flemingad347bb2008-10-30 16:41:01 -05002452
Thomas Chou1254c3d2010-12-24 13:12:21 +00002453 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -05002454
Thomas Chou1254c3d2010-12-24 13:12:21 +00002455 if (err)
2456 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05002457
Thomas Chou1254c3d2010-12-24 13:12:21 +00002458 if (IS_SD(mmc))
2459 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
2460 }
Andy Flemingad347bb2008-10-30 16:41:01 -05002461
2462 /* Get the Card-Specific Data */
2463 cmd.cmdidx = MMC_CMD_SEND_CSD;
2464 cmd.resp_type = MMC_RSP_R2;
2465 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -05002466
2467 err = mmc_send_cmd(mmc, &cmd, NULL);
2468
2469 if (err)
2470 return err;
2471
Rabin Vincentb6eed942009-04-05 13:30:56 +05302472 mmc->csd[0] = cmd.response[0];
2473 mmc->csd[1] = cmd.response[1];
2474 mmc->csd[2] = cmd.response[2];
2475 mmc->csd[3] = cmd.response[3];
Andy Flemingad347bb2008-10-30 16:41:01 -05002476
2477 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincentbdf7a682009-04-05 13:30:55 +05302478 int version = (cmd.response[0] >> 26) & 0xf;
Andy Flemingad347bb2008-10-30 16:41:01 -05002479
2480 switch (version) {
Bin Meng4a4ef872016-03-17 21:53:13 -07002481 case 0:
2482 mmc->version = MMC_VERSION_1_2;
2483 break;
2484 case 1:
2485 mmc->version = MMC_VERSION_1_4;
2486 break;
2487 case 2:
2488 mmc->version = MMC_VERSION_2_2;
2489 break;
2490 case 3:
2491 mmc->version = MMC_VERSION_3;
2492 break;
2493 case 4:
2494 mmc->version = MMC_VERSION_4;
2495 break;
2496 default:
2497 mmc->version = MMC_VERSION_1_2;
2498 break;
Andy Flemingad347bb2008-10-30 16:41:01 -05002499 }
2500 }
2501
2502 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincentbdf7a682009-04-05 13:30:55 +05302503 freq = fbase[(cmd.response[0] & 0x7)];
2504 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Flemingad347bb2008-10-30 16:41:01 -05002505
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +02002506 mmc->legacy_speed = freq * mult;
Jean-Jacques Hiblota94fb412017-09-21 16:29:53 +02002507 mmc_select_mode(mmc, MMC_LEGACY);
Andy Flemingad347bb2008-10-30 16:41:01 -05002508
Markus Niebel03951412013-12-16 13:40:46 +01002509 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincentb6eed942009-04-05 13:30:56 +05302510 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002511#if CONFIG_IS_ENABLED(MMC_WRITE)
Andy Flemingad347bb2008-10-30 16:41:01 -05002512
2513 if (IS_SD(mmc))
2514 mmc->write_bl_len = mmc->read_bl_len;
2515 else
Rabin Vincentb6eed942009-04-05 13:30:56 +05302516 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002517#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05002518
2519 if (mmc->high_capacity) {
2520 csize = (mmc->csd[1] & 0x3f) << 16
2521 | (mmc->csd[2] & 0xffff0000) >> 16;
2522 cmult = 8;
2523 } else {
2524 csize = (mmc->csd[1] & 0x3ff) << 2
2525 | (mmc->csd[2] & 0xc0000000) >> 30;
2526 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2527 }
2528
Stephen Warrene315ae82013-06-11 15:14:01 -06002529 mmc->capacity_user = (csize + 1) << (cmult + 2);
2530 mmc->capacity_user *= mmc->read_bl_len;
2531 mmc->capacity_boot = 0;
2532 mmc->capacity_rpmb = 0;
2533 for (i = 0; i < 4; i++)
2534 mmc->capacity_gp[i] = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -05002535
Simon Glassa09c2b72013-04-03 08:54:30 +00002536 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2537 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Flemingad347bb2008-10-30 16:41:01 -05002538
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002539#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glassa09c2b72013-04-03 08:54:30 +00002540 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2541 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002542#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05002543
Markus Niebel03951412013-12-16 13:40:46 +01002544 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2545 cmd.cmdidx = MMC_CMD_SET_DSR;
2546 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2547 cmd.resp_type = MMC_RSP_NONE;
2548 if (mmc_send_cmd(mmc, &cmd, NULL))
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01002549 pr_warn("MMC: SET_DSR failed\n");
Markus Niebel03951412013-12-16 13:40:46 +01002550 }
2551
Andy Flemingad347bb2008-10-30 16:41:01 -05002552 /* Select the card, and put it into Transfer Mode */
Thomas Chou1254c3d2010-12-24 13:12:21 +00002553 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2554 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargav4a32fba2011-10-05 03:13:23 +00002555 cmd.resp_type = MMC_RSP_R1;
Thomas Chou1254c3d2010-12-24 13:12:21 +00002556 cmd.cmdarg = mmc->rca << 16;
Thomas Chou1254c3d2010-12-24 13:12:21 +00002557 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -05002558
Thomas Chou1254c3d2010-12-24 13:12:21 +00002559 if (err)
2560 return err;
2561 }
Andy Flemingad347bb2008-10-30 16:41:01 -05002562
Lei Wenea526762011-06-22 17:03:31 +00002563 /*
2564 * For SD, its erase group is always one sector
2565 */
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002566#if CONFIG_IS_ENABLED(MMC_WRITE)
Lei Wenea526762011-06-22 17:03:31 +00002567 mmc->erase_grp_size = 1;
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002568#endif
Lei Wen31b99802011-05-02 16:26:26 +00002569 mmc->part_config = MMCPART_NOAVAILABLE;
Diego Santa Cruza7a75992014-12-23 10:50:27 +01002570
Jean-Jacques Hibloted9506b2017-09-21 16:29:51 +02002571 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblote84459c2017-09-21 16:29:50 +02002572 if (err)
2573 return err;
Sukumar Ghorai232293c2010-09-20 18:29:29 +05302574
Simon Glasse5db1152016-05-01 13:52:35 -06002575 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrene315ae82013-06-11 15:14:01 -06002576 if (err)
2577 return err;
2578
Marek Vasuta318a7a2018-04-15 00:37:11 +02002579#if CONFIG_IS_ENABLED(MMC_TINY)
2580 mmc_set_clock(mmc, mmc->legacy_speed, false);
Faiz Abbas01db77e2020-02-26 13:44:32 +05302581 mmc_select_mode(mmc, MMC_LEGACY);
Marek Vasuta318a7a2018-04-15 00:37:11 +02002582 mmc_set_bus_width(mmc, 1);
2583#else
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02002584 if (IS_SD(mmc)) {
2585 err = sd_get_capabilities(mmc);
2586 if (err)
2587 return err;
2588 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2589 } else {
2590 err = mmc_get_capabilities(mmc);
2591 if (err)
2592 return err;
Masahiro Yamadabf1f25c2020-01-23 14:31:12 +09002593 err = mmc_select_mode_and_width(mmc, mmc->card_caps);
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02002594 }
Marek Vasuta318a7a2018-04-15 00:37:11 +02002595#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05002596 if (err)
2597 return err;
2598
Jean-Jacques Hiblot3d30972b2017-09-21 16:30:09 +02002599 mmc->best_mode = mmc->selected_mode;
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00002600
Andrew Gabbasov532663b2014-12-01 06:59:11 -06002601 /* Fix the block length for DDR mode */
2602 if (mmc->ddr_mode) {
2603 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002604#if CONFIG_IS_ENABLED(MMC_WRITE)
Andrew Gabbasov532663b2014-12-01 06:59:11 -06002605 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01002606#endif
Andrew Gabbasov532663b2014-12-01 06:59:11 -06002607 }
2608
Andy Flemingad347bb2008-10-30 16:41:01 -05002609 /* fill in device description */
Simon Glasse5db1152016-05-01 13:52:35 -06002610 bdesc = mmc_get_blk_desc(mmc);
2611 bdesc->lun = 0;
2612 bdesc->hwpart = 0;
2613 bdesc->type = 0;
2614 bdesc->blksz = mmc->read_bl_len;
2615 bdesc->log2blksz = LOG2(bdesc->blksz);
2616 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsd67754f2015-12-04 23:27:40 +01002617#if !defined(CONFIG_SPL_BUILD) || \
2618 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
Simon Glass7611ac62019-09-25 08:56:27 -06002619 !CONFIG_IS_ENABLED(USE_TINY_PRINTF))
Simon Glasse5db1152016-05-01 13:52:35 -06002620 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Hutt7367ec22012-10-20 17:15:59 +00002621 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2622 (mmc->cid[3] >> 16) & 0xffff);
Simon Glasse5db1152016-05-01 13:52:35 -06002623 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Hutt7367ec22012-10-20 17:15:59 +00002624 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2625 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2626 (mmc->cid[2] >> 24) & 0xff);
Simon Glasse5db1152016-05-01 13:52:35 -06002627 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Hutt7367ec22012-10-20 17:15:59 +00002628 (mmc->cid[2] >> 16) & 0xf);
Paul Burton6a7c5ba2013-09-04 16:12:25 +01002629#else
Simon Glasse5db1152016-05-01 13:52:35 -06002630 bdesc->vendor[0] = 0;
2631 bdesc->product[0] = 0;
2632 bdesc->revision[0] = 0;
Paul Burton6a7c5ba2013-09-04 16:12:25 +01002633#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05002634
Andre Przywara17798042018-12-17 10:05:45 +00002635#if !defined(CONFIG_DM_MMC) && (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT))
2636 part_init(bdesc);
2637#endif
2638
Andy Flemingad347bb2008-10-30 16:41:01 -05002639 return 0;
2640}
2641
Kim Phillips87ea3892012-10-29 13:34:43 +00002642static int mmc_send_if_cond(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05002643{
2644 struct mmc_cmd cmd;
2645 int err;
2646
2647 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2648 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou2c850462014-03-11 19:34:20 +02002649 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Flemingad347bb2008-10-30 16:41:01 -05002650 cmd.resp_type = MMC_RSP_R7;
Andy Flemingad347bb2008-10-30 16:41:01 -05002651
2652 err = mmc_send_cmd(mmc, &cmd, NULL);
2653
2654 if (err)
2655 return err;
2656
Rabin Vincentb6eed942009-04-05 13:30:56 +05302657 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung7825d202016-07-19 16:33:36 +09002658 return -EOPNOTSUPP;
Andy Flemingad347bb2008-10-30 16:41:01 -05002659 else
2660 mmc->version = SD_VERSION_2;
2661
2662 return 0;
2663}
2664
Simon Glass5f4bd8c2017-07-04 13:31:19 -06002665#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski2439fe92014-11-08 20:55:45 +01002666/* board-specific MMC power initializations. */
2667__weak void board_mmc_power_init(void)
2668{
2669}
Simon Glass833b80d2017-04-22 19:10:56 -06002670#endif
Paul Kocialkowski2439fe92014-11-08 20:55:45 +01002671
Peng Fan15305962016-10-11 15:08:43 +08002672static int mmc_power_init(struct mmc *mmc)
2673{
Simon Glass5f4bd8c2017-07-04 13:31:19 -06002674#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblota49ffa12017-09-21 16:29:48 +02002675#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan15305962016-10-11 15:08:43 +08002676 int ret;
2677
2678 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblota49ffa12017-09-21 16:29:48 +02002679 &mmc->vmmc_supply);
2680 if (ret)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002681 pr_debug("%s: No vmmc supply\n", mmc->dev->name);
Jean-Jacques Hiblota49ffa12017-09-21 16:29:48 +02002682
2683 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2684 &mmc->vqmmc_supply);
2685 if (ret)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002686 pr_debug("%s: No vqmmc supply\n", mmc->dev->name);
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002687#endif
2688#else /* !CONFIG_DM_MMC */
2689 /*
2690 * Driver model should use a regulator, as above, rather than calling
2691 * out to board code.
2692 */
2693 board_mmc_power_init();
2694#endif
2695 return 0;
2696}
2697
2698/*
2699 * put the host in the initial state:
2700 * - turn on Vdd (card power supply)
2701 * - configure the bus width and clock to minimal values
2702 */
2703static void mmc_set_initial_state(struct mmc *mmc)
2704{
2705 int err;
2706
2707 /* First try to set 3.3V. If it fails set to 1.8V */
2708 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2709 if (err != 0)
2710 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2711 if (err != 0)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01002712 pr_warn("mmc: failed to set signal voltage\n");
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002713
2714 mmc_select_mode(mmc, MMC_LEGACY);
2715 mmc_set_bus_width(mmc, 1);
Jaehoon Chung239cb2f2018-01-26 19:25:29 +09002716 mmc_set_clock(mmc, 0, MMC_CLK_ENABLE);
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002717}
Peng Fan15305962016-10-11 15:08:43 +08002718
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002719static int mmc_power_on(struct mmc *mmc)
2720{
2721#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
Jean-Jacques Hiblota49ffa12017-09-21 16:29:48 +02002722 if (mmc->vmmc_supply) {
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002723 int ret = regulator_set_enable(mmc->vmmc_supply, true);
2724
Jean-Jacques Hiblota49ffa12017-09-21 16:29:48 +02002725 if (ret) {
2726 puts("Error enabling VMMC supply\n");
2727 return ret;
2728 }
Peng Fan15305962016-10-11 15:08:43 +08002729 }
2730#endif
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002731 return 0;
2732}
2733
2734static int mmc_power_off(struct mmc *mmc)
2735{
Jaehoon Chung239cb2f2018-01-26 19:25:29 +09002736 mmc_set_clock(mmc, 0, MMC_CLK_DISABLE);
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002737#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2738 if (mmc->vmmc_supply) {
2739 int ret = regulator_set_enable(mmc->vmmc_supply, false);
2740
2741 if (ret) {
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002742 pr_debug("Error disabling VMMC supply\n");
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002743 return ret;
2744 }
2745 }
Simon Glass833b80d2017-04-22 19:10:56 -06002746#endif
Peng Fan15305962016-10-11 15:08:43 +08002747 return 0;
2748}
2749
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002750static int mmc_power_cycle(struct mmc *mmc)
2751{
2752 int ret;
2753
2754 ret = mmc_power_off(mmc);
2755 if (ret)
2756 return ret;
Yann Gautier6f558332019-09-19 17:56:12 +02002757
2758 ret = mmc_host_power_cycle(mmc);
2759 if (ret)
2760 return ret;
2761
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002762 /*
2763 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2764 * to be on the safer side.
2765 */
2766 udelay(2000);
2767 return mmc_power_on(mmc);
2768}
2769
Jon Nettleton2663fe42018-06-11 15:26:19 +03002770int mmc_get_op_cond(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05002771{
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02002772 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
Macpaul Lin028bde12011-11-14 23:35:39 +00002773 int err;
Andy Flemingad347bb2008-10-30 16:41:01 -05002774
Lei Wen31b99802011-05-02 16:26:26 +00002775 if (mmc->has_init)
2776 return 0;
2777
Yangbo Lub124f8a2015-04-22 13:57:00 +08002778#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
2779 mmc_adapter_card_type_ident();
2780#endif
Peng Fan15305962016-10-11 15:08:43 +08002781 err = mmc_power_init(mmc);
2782 if (err)
2783 return err;
Paul Kocialkowski2439fe92014-11-08 20:55:45 +01002784
Kishon Vijay Abraham I07baaa62017-09-21 16:30:10 +02002785#ifdef CONFIG_MMC_QUIRKS
2786 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
Joel Johnson5ea041b2020-01-11 09:08:14 -07002787 MMC_QUIRK_RETRY_SEND_CID |
2788 MMC_QUIRK_RETRY_APP_CMD;
Kishon Vijay Abraham I07baaa62017-09-21 16:30:10 +02002789#endif
2790
Jean-Jacques Hiblotdc030fb2017-09-21 16:30:08 +02002791 err = mmc_power_cycle(mmc);
2792 if (err) {
2793 /*
2794 * if power cycling is not supported, we should not try
2795 * to use the UHS modes, because we wouldn't be able to
2796 * recover from an error during the UHS initialization.
2797 */
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002798 pr_debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
Jean-Jacques Hiblotdc030fb2017-09-21 16:30:08 +02002799 uhs_en = false;
2800 mmc->host_caps &= ~UHS_CAPS;
2801 err = mmc_power_on(mmc);
2802 }
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002803 if (err)
2804 return err;
2805
Simon Glasseba48f92017-07-29 11:35:31 -06002806#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass394dfc02016-06-12 23:30:22 -06002807 /* The device has already been probed ready for use */
2808#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02002809 /* made sure it's not NULL earlier */
Pantelis Antoniou2c850462014-03-11 19:34:20 +02002810 err = mmc->cfg->ops->init(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05002811 if (err)
2812 return err;
Simon Glass394dfc02016-06-12 23:30:22 -06002813#endif
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06002814 mmc->ddr_mode = 0;
Kishon Vijay Abraham I4afb12b2017-09-21 16:30:00 +02002815
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02002816retry:
Kishon Vijay Abraham I80b87e12017-09-21 16:30:02 +02002817 mmc_set_initial_state(mmc);
Jean-Jacques Hiblot5f23d872017-09-21 16:30:01 +02002818
Andy Flemingad347bb2008-10-30 16:41:01 -05002819 /* Reset the Card */
2820 err = mmc_go_idle(mmc);
2821
2822 if (err)
2823 return err;
2824
Lei Wen31b99802011-05-02 16:26:26 +00002825 /* The internal partition reset to user partition(0) at every CMD0*/
Simon Glasse5db1152016-05-01 13:52:35 -06002826 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wen31b99802011-05-02 16:26:26 +00002827
Andy Flemingad347bb2008-10-30 16:41:01 -05002828 /* Test for SD version 2 */
Macpaul Lin028bde12011-11-14 23:35:39 +00002829 err = mmc_send_if_cond(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05002830
Andy Flemingad347bb2008-10-30 16:41:01 -05002831 /* Now try to get the SD card's operating condition */
Jean-Jacques Hiblotf4d5b3e2017-09-21 16:30:07 +02002832 err = sd_send_op_cond(mmc, uhs_en);
2833 if (err && uhs_en) {
2834 uhs_en = false;
2835 mmc_power_cycle(mmc);
2836 goto retry;
2837 }
Andy Flemingad347bb2008-10-30 16:41:01 -05002838
2839 /* If the command timed out, we check for an MMC card */
Jaehoon Chung7825d202016-07-19 16:33:36 +09002840 if (err == -ETIMEDOUT) {
Andy Flemingad347bb2008-10-30 16:41:01 -05002841 err = mmc_send_op_cond(mmc);
2842
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05002843 if (err) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +01002844#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01002845 pr_err("Card did not respond to voltage select!\n");
Paul Burton6a7c5ba2013-09-04 16:12:25 +01002846#endif
Jaehoon Chung7825d202016-07-19 16:33:36 +09002847 return -EOPNOTSUPP;
Andy Flemingad347bb2008-10-30 16:41:01 -05002848 }
2849 }
2850
Jon Nettleton2663fe42018-06-11 15:26:19 +03002851 return err;
2852}
2853
2854int mmc_start_init(struct mmc *mmc)
2855{
2856 bool no_card;
2857 int err = 0;
2858
2859 /*
2860 * all hosts are capable of 1 bit bus-width and able to use the legacy
2861 * timings.
2862 */
Faiz Abbas01db77e2020-02-26 13:44:32 +05302863 mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(MMC_LEGACY) |
Jon Nettleton2663fe42018-06-11 15:26:19 +03002864 MMC_CAP(MMC_LEGACY) | MMC_MODE_1BIT;
Faiz Abbasf6fd4ec2020-02-26 13:44:30 +05302865#if CONFIG_IS_ENABLED(DM_MMC)
2866 mmc_deferred_probe(mmc);
2867#endif
Jon Nettleton2663fe42018-06-11 15:26:19 +03002868#if !defined(CONFIG_MMC_BROKEN_CD)
Jon Nettleton2663fe42018-06-11 15:26:19 +03002869 no_card = mmc_getcd(mmc) == 0;
2870#else
2871 no_card = 0;
2872#endif
2873#if !CONFIG_IS_ENABLED(DM_MMC)
Baruch Siach0448ce62019-07-22 15:52:12 +03002874 /* we pretend there's no card when init is NULL */
Jon Nettleton2663fe42018-06-11 15:26:19 +03002875 no_card = no_card || (mmc->cfg->ops->init == NULL);
2876#endif
2877 if (no_card) {
2878 mmc->has_init = 0;
2879#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2880 pr_err("MMC: no card present\n");
2881#endif
2882 return -ENOMEDIUM;
2883 }
2884
2885 err = mmc_get_op_cond(mmc);
2886
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05002887 if (!err)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002888 mmc->init_in_progress = 1;
2889
2890 return err;
2891}
2892
2893static int mmc_complete_init(struct mmc *mmc)
2894{
2895 int err = 0;
2896
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05002897 mmc->init_in_progress = 0;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002898 if (mmc->op_cond_pending)
2899 err = mmc_complete_op_cond(mmc);
2900
2901 if (!err)
2902 err = mmc_startup(mmc);
Lei Wen31b99802011-05-02 16:26:26 +00002903 if (err)
2904 mmc->has_init = 0;
2905 else
2906 mmc->has_init = 1;
2907 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05002908}
2909
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002910int mmc_init(struct mmc *mmc)
2911{
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05002912 int err = 0;
Vipul Kumardbad7b42018-05-03 12:20:54 +05302913 __maybe_unused ulong start;
Simon Glass5f4bd8c2017-07-04 13:31:19 -06002914#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass59bc6f22016-05-01 13:52:41 -06002915 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002916
Simon Glass59bc6f22016-05-01 13:52:41 -06002917 upriv->mmc = mmc;
2918#endif
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002919 if (mmc->has_init)
2920 return 0;
Mateusz Zalegada351782014-04-29 20:15:30 +02002921
2922 start = get_timer(0);
2923
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002924 if (!mmc->init_in_progress)
2925 err = mmc_start_init(mmc);
2926
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05002927 if (!err)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002928 err = mmc_complete_init(mmc);
Jagan Teki9bee2b52017-01-10 11:18:43 +01002929 if (err)
Masahiro Yamadaf97b1482018-01-28 19:11:42 +09002930 pr_info("%s: %d, time %lu\n", __func__, err, get_timer(start));
Jagan Teki9bee2b52017-01-10 11:18:43 +01002931
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002932 return err;
2933}
2934
Marek Vasuta4773fc2019-01-29 04:45:51 +01002935#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
2936 CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
2937 CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
2938int mmc_deinit(struct mmc *mmc)
2939{
2940 u32 caps_filtered;
2941
2942 if (!mmc->has_init)
2943 return 0;
2944
2945 if (IS_SD(mmc)) {
2946 caps_filtered = mmc->card_caps &
2947 ~(MMC_CAP(UHS_SDR12) | MMC_CAP(UHS_SDR25) |
2948 MMC_CAP(UHS_SDR50) | MMC_CAP(UHS_DDR50) |
2949 MMC_CAP(UHS_SDR104));
2950
2951 return sd_select_mode_and_width(mmc, caps_filtered);
2952 } else {
2953 caps_filtered = mmc->card_caps &
2954 ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_HS_400));
2955
2956 return mmc_select_mode_and_width(mmc, caps_filtered);
2957 }
2958}
2959#endif
2960
Markus Niebel03951412013-12-16 13:40:46 +01002961int mmc_set_dsr(struct mmc *mmc, u16 val)
2962{
2963 mmc->dsr = val;
2964 return 0;
2965}
2966
Jeroen Hofstee47726302014-07-10 22:46:28 +02002967/* CPU-specific MMC initializations */
2968__weak int cpu_mmc_init(bd_t *bis)
Andy Flemingad347bb2008-10-30 16:41:01 -05002969{
2970 return -1;
2971}
2972
Jeroen Hofstee47726302014-07-10 22:46:28 +02002973/* board-specific MMC initializations. */
2974__weak int board_mmc_init(bd_t *bis)
2975{
2976 return -1;
2977}
Andy Flemingad347bb2008-10-30 16:41:01 -05002978
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00002979void mmc_set_preinit(struct mmc *mmc, int preinit)
2980{
2981 mmc->preinit = preinit;
2982}
2983
Faiz Abbasb3857fd2018-02-12 19:35:24 +05302984#if CONFIG_IS_ENABLED(DM_MMC)
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06002985static int mmc_probe(bd_t *bis)
2986{
Simon Glass547cb342015-12-29 05:22:49 -07002987 int ret, i;
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06002988 struct uclass *uc;
Simon Glass547cb342015-12-29 05:22:49 -07002989 struct udevice *dev;
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06002990
2991 ret = uclass_get(UCLASS_MMC, &uc);
2992 if (ret)
2993 return ret;
2994
Simon Glass547cb342015-12-29 05:22:49 -07002995 /*
2996 * Try to add them in sequence order. Really with driver model we
2997 * should allow holes, but the current MMC list does not allow that.
2998 * So if we request 0, 1, 3 we will get 0, 1, 2.
2999 */
3000 for (i = 0; ; i++) {
3001 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
3002 if (ret == -ENODEV)
3003 break;
3004 }
3005 uclass_foreach_dev(dev, uc) {
3006 ret = device_probe(dev);
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003007 if (ret)
Jean-Jacques Hiblot678b6082017-11-30 17:44:00 +01003008 pr_err("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003009 }
3010
3011 return 0;
3012}
3013#else
3014static int mmc_probe(bd_t *bis)
3015{
3016 if (board_mmc_init(bis) < 0)
3017 cpu_mmc_init(bis);
3018
3019 return 0;
3020}
3021#endif
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00003022
Andy Flemingad347bb2008-10-30 16:41:01 -05003023int mmc_initialize(bd_t *bis)
3024{
Daniel Kochmański13df57b2015-05-29 16:55:43 +02003025 static int initialized = 0;
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003026 int ret;
Daniel Kochmański13df57b2015-05-29 16:55:43 +02003027 if (initialized) /* Avoid initializing mmc multiple times */
3028 return 0;
3029 initialized = 1;
3030
Simon Glass5f4bd8c2017-07-04 13:31:19 -06003031#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutf537e392016-12-01 02:06:33 +01003032#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glasse5db1152016-05-01 13:52:35 -06003033 mmc_list_init();
3034#endif
Marek Vasutf537e392016-12-01 02:06:33 +01003035#endif
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06003036 ret = mmc_probe(bis);
3037 if (ret)
3038 return ret;
Andy Flemingad347bb2008-10-30 16:41:01 -05003039
Ying Zhang9ff70262013-08-16 15:16:11 +08003040#ifndef CONFIG_SPL_BUILD
Andy Flemingad347bb2008-10-30 16:41:01 -05003041 print_mmc_devices(',');
Ying Zhang9ff70262013-08-16 15:16:11 +08003042#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05003043
Simon Glasse5db1152016-05-01 13:52:35 -06003044 mmc_do_preinit();
Andy Flemingad347bb2008-10-30 16:41:01 -05003045 return 0;
3046}
Tomas Melinc17dae52016-11-25 11:01:03 +02003047
Lokesh Vutlac59b41c2019-09-09 14:40:36 +05303048#if CONFIG_IS_ENABLED(DM_MMC)
3049int mmc_init_device(int num)
3050{
3051 struct udevice *dev;
3052 struct mmc *m;
3053 int ret;
3054
3055 ret = uclass_get_device(UCLASS_MMC, num, &dev);
3056 if (ret)
3057 return ret;
3058
3059 m = mmc_get_mmc_dev(dev);
3060 if (!m)
3061 return 0;
3062#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
3063 mmc_set_preinit(m, 1);
3064#endif
3065 if (m->preinit)
3066 mmc_start_init(m);
3067
3068 return 0;
3069}
3070#endif
3071
Tomas Melinc17dae52016-11-25 11:01:03 +02003072#ifdef CONFIG_CMD_BKOPS_ENABLE
3073int mmc_set_bkops_enable(struct mmc *mmc)
3074{
3075 int err;
3076 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
3077
3078 err = mmc_send_ext_csd(mmc, ext_csd);
3079 if (err) {
3080 puts("Could not get ext_csd register values\n");
3081 return err;
3082 }
3083
3084 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
3085 puts("Background operations not supported on device\n");
3086 return -EMEDIUMTYPE;
3087 }
3088
3089 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
3090 puts("Background operations already enabled\n");
3091 return 0;
3092 }
3093
3094 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
3095 if (err) {
3096 puts("Failed to enable manual background operations\n");
3097 return err;
3098 }
3099
3100 puts("Enabled manual background operations\n");
3101
3102 return 0;
3103}
3104#endif