blob: 26589b8771a892e8ecf524c14a47d95f7dde5529 [file] [log] [blame]
Andy Fleming272cc702008-10-30 16:41:01 -05001/*
2 * Copyright 2008, Freescale Semiconductor, Inc
3 * Andy Fleming
4 *
5 * Based vaguely on the Linux code
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Andy Fleming272cc702008-10-30 16:41:01 -05008 */
9
10#include <config.h>
11#include <common.h>
12#include <command.h>
Sjoerd Simons8e3332e2015-08-30 16:55:45 -060013#include <dm.h>
14#include <dm/device-internal.h>
Stephen Warrend4622df2014-05-23 12:47:06 -060015#include <errno.h>
Andy Fleming272cc702008-10-30 16:41:01 -050016#include <mmc.h>
17#include <part.h>
Peng Fan2051aef2016-10-11 15:08:43 +080018#include <power/regulator.h>
Andy Fleming272cc702008-10-30 16:41:01 -050019#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060020#include <memalign.h>
Andy Fleming272cc702008-10-30 16:41:01 -050021#include <linux/list.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053022#include <div64.h>
Paul Burtonda61fa52013-09-09 15:30:26 +010023#include "mmc_private.h"
Andy Fleming272cc702008-10-30 16:41:01 -050024
Peng Fan3697e592016-09-01 11:13:38 +080025static const unsigned int sd_au_size[] = {
26 0, SZ_16K / 512, SZ_32K / 512,
27 SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
28 SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
29 SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
30 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512, SZ_64M / 512,
31};
32
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +020033static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +020034static int mmc_power_cycle(struct mmc *mmc);
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +020035static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps);
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +020036
Marek Vasutb5b838f2016-12-01 02:06:33 +010037#if CONFIG_IS_ENABLED(MMC_TINY)
38static struct mmc mmc_static;
39struct mmc *find_mmc_device(int dev_num)
40{
41 return &mmc_static;
42}
43
44void mmc_do_preinit(void)
45{
46 struct mmc *m = &mmc_static;
47#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
48 mmc_set_preinit(m, 1);
49#endif
50 if (m->preinit)
51 mmc_start_init(m);
52}
53
54struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
55{
56 return &mmc->block_dev;
57}
58#endif
59
Simon Glasse7881d82017-07-29 11:35:31 -060060#if !CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +020061
62static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout)
63{
64 return -ENOSYS;
65}
66
Jeroen Hofstee750121c2014-07-12 21:24:08 +020067__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000068{
69 return -1;
70}
71
72int mmc_getwp(struct mmc *mmc)
73{
74 int wp;
75
76 wp = board_mmc_getwp(mmc);
77
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000078 if (wp < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020079 if (mmc->cfg->ops->getwp)
80 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000081 else
82 wp = 0;
83 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000084
85 return wp;
86}
87
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +020088__weak int board_mmc_getcd(struct mmc *mmc)
89{
Stefano Babic11fdade2010-02-05 15:04:43 +010090 return -1;
91}
Simon Glass8ca51e52016-06-12 23:30:22 -060092#endif
Stefano Babic11fdade2010-02-05 15:04:43 +010093
Marek Vasut8635ff92012-03-15 18:41:35 +000094#ifdef CONFIG_MMC_TRACE
Simon Glassc0c76eb2016-06-12 23:30:20 -060095void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
96{
97 printf("CMD_SEND:%d\n", cmd->cmdidx);
98 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
99}
100
101void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
102{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000103 int i;
104 u8 *ptr;
105
Bin Meng7863ce52016-03-17 21:53:14 -0700106 if (ret) {
107 printf("\t\tRET\t\t\t %d\n", ret);
108 } else {
109 switch (cmd->resp_type) {
110 case MMC_RSP_NONE:
111 printf("\t\tMMC_RSP_NONE\n");
112 break;
113 case MMC_RSP_R1:
114 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
115 cmd->response[0]);
116 break;
117 case MMC_RSP_R1b:
118 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
119 cmd->response[0]);
120 break;
121 case MMC_RSP_R2:
122 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
123 cmd->response[0]);
124 printf("\t\t \t\t 0x%08X \n",
125 cmd->response[1]);
126 printf("\t\t \t\t 0x%08X \n",
127 cmd->response[2]);
128 printf("\t\t \t\t 0x%08X \n",
129 cmd->response[3]);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000130 printf("\n");
Bin Meng7863ce52016-03-17 21:53:14 -0700131 printf("\t\t\t\t\tDUMPING DATA\n");
132 for (i = 0; i < 4; i++) {
133 int j;
134 printf("\t\t\t\t\t%03d - ", i*4);
135 ptr = (u8 *)&cmd->response[i];
136 ptr += 3;
137 for (j = 0; j < 4; j++)
138 printf("%02X ", *ptr--);
139 printf("\n");
140 }
141 break;
142 case MMC_RSP_R3:
143 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
144 cmd->response[0]);
145 break;
146 default:
147 printf("\t\tERROR MMC rsp not supported\n");
148 break;
Bin Meng53e8e402016-03-17 21:53:13 -0700149 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000150 }
Simon Glassc0c76eb2016-06-12 23:30:20 -0600151}
152
153void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
154{
155 int status;
156
157 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
158 printf("CURR STATE:%d\n", status);
159}
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000160#endif
Simon Glassc0c76eb2016-06-12 23:30:20 -0600161
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200162#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
163const char *mmc_mode_name(enum bus_mode mode)
164{
165 static const char *const names[] = {
166 [MMC_LEGACY] = "MMC legacy",
167 [SD_LEGACY] = "SD Legacy",
168 [MMC_HS] = "MMC High Speed (26MHz)",
169 [SD_HS] = "SD High Speed (50MHz)",
170 [UHS_SDR12] = "UHS SDR12 (25MHz)",
171 [UHS_SDR25] = "UHS SDR25 (50MHz)",
172 [UHS_SDR50] = "UHS SDR50 (100MHz)",
173 [UHS_SDR104] = "UHS SDR104 (208MHz)",
174 [UHS_DDR50] = "UHS DDR50 (50MHz)",
175 [MMC_HS_52] = "MMC High Speed (52MHz)",
176 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
177 [MMC_HS_200] = "HS200 (200MHz)",
178 };
179
180 if (mode >= MMC_MODES_END)
181 return "Unknown mode";
182 else
183 return names[mode];
184}
185#endif
186
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200187static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
188{
189 static const int freqs[] = {
190 [SD_LEGACY] = 25000000,
191 [MMC_HS] = 26000000,
192 [SD_HS] = 50000000,
193 [UHS_SDR12] = 25000000,
194 [UHS_SDR25] = 50000000,
195 [UHS_SDR50] = 100000000,
196 [UHS_SDR104] = 208000000,
197 [UHS_DDR50] = 50000000,
198 [MMC_HS_52] = 52000000,
199 [MMC_DDR_52] = 52000000,
200 [MMC_HS_200] = 200000000,
201 };
202
203 if (mode == MMC_LEGACY)
204 return mmc->legacy_speed;
205 else if (mode >= MMC_MODES_END)
206 return 0;
207 else
208 return freqs[mode];
209}
210
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200211static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
212{
213 mmc->selected_mode = mode;
Jean-Jacques Hiblot05038572017-09-21 16:29:55 +0200214 mmc->tran_speed = mmc_mode2freq(mmc, mode);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200215 mmc->ddr_mode = mmc_is_mode_ddr(mode);
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200216 debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
217 mmc->tran_speed / 1000000);
218 return 0;
219}
220
Simon Glasse7881d82017-07-29 11:35:31 -0600221#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassc0c76eb2016-06-12 23:30:20 -0600222int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
223{
224 int ret;
225
226 mmmc_trace_before_send(mmc, cmd);
227 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
228 mmmc_trace_after_send(mmc, cmd, ret);
229
Marek Vasut8635ff92012-03-15 18:41:35 +0000230 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500231}
Simon Glass8ca51e52016-06-12 23:30:22 -0600232#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500233
Paul Burtonda61fa52013-09-09 15:30:26 +0100234int mmc_send_status(struct mmc *mmc, int timeout)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000235{
236 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000237 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000238
239 cmd.cmdidx = MMC_CMD_SEND_STATUS;
240 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200241 if (!mmc_host_is_spi(mmc))
242 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000243
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500244 while (1) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000245 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000246 if (!err) {
247 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
248 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
249 MMC_STATE_PRG)
250 break;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200251
252 if (cmd.response[0] & MMC_STATUS_MASK) {
Paul Burton56196822013-09-04 16:12:25 +0100253#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jan Kloetzked617c422012-02-05 22:29:12 +0000254 printf("Status Error: 0x%08X\n",
255 cmd.response[0]);
Paul Burton56196822013-09-04 16:12:25 +0100256#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900257 return -ECOMM;
Jan Kloetzked617c422012-02-05 22:29:12 +0000258 }
259 } else if (--retries < 0)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000260 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000261
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500262 if (timeout-- <= 0)
263 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000264
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500265 udelay(1000);
266 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000267
Simon Glassc0c76eb2016-06-12 23:30:20 -0600268 mmc_trace_state(mmc, &cmd);
Jongman Heo5b0c9422012-06-03 21:32:13 +0000269 if (timeout <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100270#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000271 printf("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100272#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900273 return -ETIMEDOUT;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000274 }
275
276 return 0;
277}
278
Paul Burtonda61fa52013-09-09 15:30:26 +0100279int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500280{
281 struct mmc_cmd cmd;
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +0200282 int err;
Andy Fleming272cc702008-10-30 16:41:01 -0500283
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600284 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900285 return 0;
286
Andy Fleming272cc702008-10-30 16:41:01 -0500287 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
288 cmd.resp_type = MMC_RSP_R1;
289 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500290
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +0200291 err = mmc_send_cmd(mmc, &cmd, NULL);
292
293#ifdef CONFIG_MMC_QUIRKS
294 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SET_BLOCKLEN)) {
295 int retries = 4;
296 /*
297 * It has been seen that SET_BLOCKLEN may fail on the first
298 * attempt, let's try a few more time
299 */
300 do {
301 err = mmc_send_cmd(mmc, &cmd, NULL);
302 if (!err)
303 break;
304 } while (retries--);
305 }
306#endif
307
308 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500309}
310
Sascha Silbeff8fef52013-06-14 13:07:25 +0200311static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000312 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500313{
314 struct mmc_cmd cmd;
315 struct mmc_data data;
316
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700317 if (blkcnt > 1)
318 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
319 else
320 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500321
322 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700323 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500324 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700325 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500326
327 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500328
329 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700330 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500331 data.blocksize = mmc->read_bl_len;
332 data.flags = MMC_DATA_READ;
333
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700334 if (mmc_send_cmd(mmc, &cmd, &data))
335 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500336
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700337 if (blkcnt > 1) {
338 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
339 cmd.cmdarg = 0;
340 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700341 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton56196822013-09-04 16:12:25 +0100342#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700343 printf("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100344#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700345 return 0;
346 }
Andy Fleming272cc702008-10-30 16:41:01 -0500347 }
348
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700349 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500350}
351
Simon Glassc4d660d2017-07-04 13:31:19 -0600352#if CONFIG_IS_ENABLED(BLK)
Simon Glass7dba0b92016-06-12 23:30:15 -0600353ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600354#else
Simon Glass7dba0b92016-06-12 23:30:15 -0600355ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
356 void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600357#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500358{
Simon Glassc4d660d2017-07-04 13:31:19 -0600359#if CONFIG_IS_ENABLED(BLK)
Simon Glass33fb2112016-05-01 13:52:41 -0600360 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
361#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700362 int dev_num = block_dev->devnum;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700363 int err;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700364 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500365
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700366 if (blkcnt == 0)
367 return 0;
368
369 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500370 if (!mmc)
371 return 0;
372
Marek Vasutb5b838f2016-12-01 02:06:33 +0100373 if (CONFIG_IS_ENABLED(MMC_TINY))
374 err = mmc_switch_part(mmc, block_dev->hwpart);
375 else
376 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
377
Stephen Warren873cc1d2015-12-07 11:38:49 -0700378 if (err < 0)
379 return 0;
380
Simon Glassc40fdca2016-05-01 13:52:35 -0600381 if ((start + blkcnt) > block_dev->lba) {
Paul Burton56196822013-09-04 16:12:25 +0100382#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Sascha Silbeff8fef52013-06-14 13:07:25 +0200383 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Simon Glassc40fdca2016-05-01 13:52:35 -0600384 start + blkcnt, block_dev->lba);
Paul Burton56196822013-09-04 16:12:25 +0100385#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800386 return 0;
387 }
Andy Fleming272cc702008-10-30 16:41:01 -0500388
Simon Glass11692992015-06-23 15:38:50 -0600389 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
390 debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500391 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600392 }
Andy Fleming272cc702008-10-30 16:41:01 -0500393
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700394 do {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200395 cur = (blocks_todo > mmc->cfg->b_max) ?
396 mmc->cfg->b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600397 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
398 debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700399 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600400 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700401 blocks_todo -= cur;
402 start += cur;
403 dst += cur * mmc->read_bl_len;
404 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500405
406 return blkcnt;
407}
408
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000409static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500410{
411 struct mmc_cmd cmd;
412 int err;
413
414 udelay(1000);
415
416 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
417 cmd.cmdarg = 0;
418 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500419
420 err = mmc_send_cmd(mmc, &cmd, NULL);
421
422 if (err)
423 return err;
424
425 udelay(2000);
426
427 return 0;
428}
429
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200430static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
431{
432 struct mmc_cmd cmd;
433 int err = 0;
434
435 /*
436 * Send CMD11 only if the request is to switch the card to
437 * 1.8V signalling.
438 */
439 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
440 return mmc_set_signal_voltage(mmc, signal_voltage);
441
442 cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
443 cmd.cmdarg = 0;
444 cmd.resp_type = MMC_RSP_R1;
445
446 err = mmc_send_cmd(mmc, &cmd, NULL);
447 if (err)
448 return err;
449
450 if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
451 return -EIO;
452
453 /*
454 * The card should drive cmd and dat[0:3] low immediately
455 * after the response of cmd11, but wait 100 us to be sure
456 */
457 err = mmc_wait_dat0(mmc, 0, 100);
458 if (err == -ENOSYS)
459 udelay(100);
460 else if (err)
461 return -ETIMEDOUT;
462
463 /*
464 * During a signal voltage level switch, the clock must be gated
465 * for 5 ms according to the SD spec
466 */
467 mmc_set_clock(mmc, mmc->clock, true);
468
469 err = mmc_set_signal_voltage(mmc, signal_voltage);
470 if (err)
471 return err;
472
473 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
474 mdelay(10);
475 mmc_set_clock(mmc, mmc->clock, false);
476
477 /*
478 * Failure to switch is indicated by the card holding
479 * dat[0:3] low. Wait for at least 1 ms according to spec
480 */
481 err = mmc_wait_dat0(mmc, 1, 1000);
482 if (err == -ENOSYS)
483 udelay(1000);
484 else if (err)
485 return -ETIMEDOUT;
486
487 return 0;
488}
489
490static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
Andy Fleming272cc702008-10-30 16:41:01 -0500491{
492 int timeout = 1000;
493 int err;
494 struct mmc_cmd cmd;
495
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500496 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500497 cmd.cmdidx = MMC_CMD_APP_CMD;
498 cmd.resp_type = MMC_RSP_R1;
499 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500500
501 err = mmc_send_cmd(mmc, &cmd, NULL);
502
503 if (err)
504 return err;
505
506 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
507 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100508
509 /*
510 * Most cards do not answer if some reserved bits
511 * in the ocr are set. However, Some controller
512 * can set bit 7 (reserved for low voltages), but
513 * how to manage low voltages SD card is not yet
514 * specified.
515 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000516 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200517 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500518
519 if (mmc->version == SD_VERSION_2)
520 cmd.cmdarg |= OCR_HCS;
521
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200522 if (uhs_en)
523 cmd.cmdarg |= OCR_S18R;
524
Andy Fleming272cc702008-10-30 16:41:01 -0500525 err = mmc_send_cmd(mmc, &cmd, NULL);
526
527 if (err)
528 return err;
529
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500530 if (cmd.response[0] & OCR_BUSY)
531 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500532
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500533 if (timeout-- <= 0)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900534 return -EOPNOTSUPP;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500535
536 udelay(1000);
537 }
Andy Fleming272cc702008-10-30 16:41:01 -0500538
539 if (mmc->version != SD_VERSION_2)
540 mmc->version = SD_VERSION_1_0;
541
Thomas Choud52ebf12010-12-24 13:12:21 +0000542 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
543 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
544 cmd.resp_type = MMC_RSP_R3;
545 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000546
547 err = mmc_send_cmd(mmc, &cmd, NULL);
548
549 if (err)
550 return err;
551 }
552
Rabin Vincent998be3d2009-04-05 13:30:56 +0530553 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500554
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +0200555 if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
556 == 0x41000000) {
557 err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
558 if (err)
559 return err;
560 }
561
Andy Fleming272cc702008-10-30 16:41:01 -0500562 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
563 mmc->rca = 0;
564
565 return 0;
566}
567
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500568static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500569{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500570 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500571 int err;
572
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500573 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
574 cmd.resp_type = MMC_RSP_R3;
575 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500576 if (use_arg && !mmc_host_is_spi(mmc))
577 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200578 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500579 (mmc->ocr & OCR_VOLTAGE_MASK)) |
580 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000581
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500582 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000583 if (err)
584 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500585 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000586 return 0;
587}
588
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200589static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000590{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000591 int err, i;
592
Andy Fleming272cc702008-10-30 16:41:01 -0500593 /* Some cards seem to need this */
594 mmc_go_idle(mmc);
595
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000596 /* Asking to the card its capabilities */
Che-Liang Chioue9550442012-11-28 15:21:13 +0000597 for (i = 0; i < 2; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500598 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500599 if (err)
600 return err;
601
Che-Liang Chioue9550442012-11-28 15:21:13 +0000602 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500603 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500604 break;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000605 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500606 mmc->op_cond_pending = 1;
607 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000608}
Andy Fleming272cc702008-10-30 16:41:01 -0500609
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200610static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000611{
612 struct mmc_cmd cmd;
613 int timeout = 1000;
614 uint start;
615 int err;
616
617 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500618 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lud188b112016-08-02 15:33:18 +0800619 /* Some cards seem to need this */
620 mmc_go_idle(mmc);
621
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500622 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500623 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500624 err = mmc_send_op_cond_iter(mmc, 1);
625 if (err)
626 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500627 if (mmc->ocr & OCR_BUSY)
628 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500629 if (get_timer(start) > timeout)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900630 return -EOPNOTSUPP;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500631 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500632 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500633 }
Andy Fleming272cc702008-10-30 16:41:01 -0500634
Thomas Choud52ebf12010-12-24 13:12:21 +0000635 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
636 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
637 cmd.resp_type = MMC_RSP_R3;
638 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000639
640 err = mmc_send_cmd(mmc, &cmd, NULL);
641
642 if (err)
643 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500644
645 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000646 }
647
Andy Fleming272cc702008-10-30 16:41:01 -0500648 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500649
650 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700651 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500652
653 return 0;
654}
655
656
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000657static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500658{
659 struct mmc_cmd cmd;
660 struct mmc_data data;
661 int err;
662
663 /* Get the Card Status Register */
664 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
665 cmd.resp_type = MMC_RSP_R1;
666 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500667
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000668 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500669 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000670 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500671 data.flags = MMC_DATA_READ;
672
673 err = mmc_send_cmd(mmc, &cmd, &data);
674
675 return err;
676}
677
Simon Glassc40704f2016-06-12 23:30:18 -0600678int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Fleming272cc702008-10-30 16:41:01 -0500679{
680 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000681 int timeout = 1000;
Maxime Riparda9003dc2016-11-04 16:18:08 +0100682 int retries = 3;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000683 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500684
685 cmd.cmdidx = MMC_CMD_SWITCH;
686 cmd.resp_type = MMC_RSP_R1b;
687 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000688 (index << 16) |
689 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500690
Maxime Riparda9003dc2016-11-04 16:18:08 +0100691 while (retries > 0) {
692 ret = mmc_send_cmd(mmc, &cmd, NULL);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000693
Maxime Riparda9003dc2016-11-04 16:18:08 +0100694 /* Waiting for the ready status */
695 if (!ret) {
696 ret = mmc_send_status(mmc, timeout);
697 return ret;
698 }
699
700 retries--;
701 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000702
703 return ret;
704
Andy Fleming272cc702008-10-30 16:41:01 -0500705}
706
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200707static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode)
Andy Fleming272cc702008-10-30 16:41:01 -0500708{
Andy Fleming272cc702008-10-30 16:41:01 -0500709 int err;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200710 int speed_bits;
711
712 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
713
714 switch (mode) {
715 case MMC_HS:
716 case MMC_HS_52:
717 case MMC_DDR_52:
718 speed_bits = EXT_CSD_TIMING_HS;
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200719 break;
720 case MMC_HS_200:
721 speed_bits = EXT_CSD_TIMING_HS200;
722 break;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200723 case MMC_LEGACY:
724 speed_bits = EXT_CSD_TIMING_LEGACY;
725 break;
726 default:
727 return -EINVAL;
728 }
729 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
730 speed_bits);
731 if (err)
732 return err;
733
734 if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
735 /* Now check to see that it worked */
736 err = mmc_send_ext_csd(mmc, test_csd);
737 if (err)
738 return err;
739
740 /* No high-speed support */
741 if (!test_csd[EXT_CSD_HS_TIMING])
742 return -ENOTSUPP;
743 }
744
745 return 0;
746}
747
748static int mmc_get_capabilities(struct mmc *mmc)
749{
750 u8 *ext_csd = mmc->ext_csd;
751 char cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500752
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +0200753 mmc->card_caps = MMC_MODE_1BIT;
Andy Fleming272cc702008-10-30 16:41:01 -0500754
Thomas Choud52ebf12010-12-24 13:12:21 +0000755 if (mmc_host_is_spi(mmc))
756 return 0;
757
Andy Fleming272cc702008-10-30 16:41:01 -0500758 /* Only version 4 supports high-speed */
759 if (mmc->version < MMC_VERSION_4)
760 return 0;
761
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200762 if (!ext_csd) {
763 printf("No ext_csd found!\n"); /* this should enver happen */
764 return -ENOTSUPP;
765 }
766
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600767 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
768
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200769 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0x3f;
Andy Fleming272cc702008-10-30 16:41:01 -0500770
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200771 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
772 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
773 mmc->card_caps |= MMC_MODE_HS200;
774 }
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900775 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200776 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900777 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200778 mmc->card_caps |= MMC_MODE_HS_52MHz;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900779 }
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200780 if (cardtype & EXT_CSD_CARD_TYPE_26)
781 mmc->card_caps |= MMC_MODE_HS;
Andy Fleming272cc702008-10-30 16:41:01 -0500782
783 return 0;
784}
785
Stephen Warrenf866a462013-06-11 15:14:01 -0600786static int mmc_set_capacity(struct mmc *mmc, int part_num)
787{
788 switch (part_num) {
789 case 0:
790 mmc->capacity = mmc->capacity_user;
791 break;
792 case 1:
793 case 2:
794 mmc->capacity = mmc->capacity_boot;
795 break;
796 case 3:
797 mmc->capacity = mmc->capacity_rpmb;
798 break;
799 case 4:
800 case 5:
801 case 6:
802 case 7:
803 mmc->capacity = mmc->capacity_gp[part_num - 4];
804 break;
805 default:
806 return -1;
807 }
808
Simon Glassc40fdca2016-05-01 13:52:35 -0600809 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -0600810
811 return 0;
812}
813
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +0200814static int mmc_boot_part_access_chk(struct mmc *mmc, unsigned int part_num)
815{
816 int forbidden = 0;
817 bool change = false;
818
819 if (part_num & PART_ACCESS_MASK)
820 forbidden = MMC_CAP(MMC_HS_200);
821
822 if (MMC_CAP(mmc->selected_mode) & forbidden) {
823 debug("selected mode (%s) is forbidden for part %d\n",
824 mmc_mode_name(mmc->selected_mode), part_num);
825 change = true;
826 } else if (mmc->selected_mode != mmc->best_mode) {
827 debug("selected mode is not optimal\n");
828 change = true;
829 }
830
831 if (change)
832 return mmc_select_mode_and_width(mmc,
833 mmc->card_caps & ~forbidden);
834
835 return 0;
836}
837
Simon Glass7dba0b92016-06-12 23:30:15 -0600838int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +0000839{
Stephen Warrenf866a462013-06-11 15:14:01 -0600840 int ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000841
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +0200842 ret = mmc_boot_part_access_chk(mmc, part_num);
843 if (ret)
844 return ret;
845
Stephen Warrenf866a462013-06-11 15:14:01 -0600846 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
847 (mmc->part_config & ~PART_ACCESS_MASK)
848 | (part_num & PART_ACCESS_MASK));
Stephen Warrenf866a462013-06-11 15:14:01 -0600849
Peter Bigot6dc93e72014-09-02 18:31:23 -0500850 /*
851 * Set the capacity if the switch succeeded or was intended
852 * to return to representing the raw device.
853 */
Stephen Warren873cc1d2015-12-07 11:38:49 -0700854 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -0500855 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -0600856 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700857 }
Peter Bigot6dc93e72014-09-02 18:31:23 -0500858
859 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000860}
861
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100862int mmc_hwpart_config(struct mmc *mmc,
863 const struct mmc_hwpart_conf *conf,
864 enum mmc_hwpart_conf_mode mode)
865{
866 u8 part_attrs = 0;
867 u32 enh_size_mult;
868 u32 enh_start_addr;
869 u32 gp_size_mult[4];
870 u32 max_enh_size_mult;
871 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100872 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100873 int i, pidx, err;
874 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
875
876 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
877 return -EINVAL;
878
879 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
880 printf("eMMC >= 4.4 required for enhanced user data area\n");
881 return -EMEDIUMTYPE;
882 }
883
884 if (!(mmc->part_support & PART_SUPPORT)) {
885 printf("Card does not support partitioning\n");
886 return -EMEDIUMTYPE;
887 }
888
889 if (!mmc->hc_wp_grp_size) {
890 printf("Card does not define HC WP group size\n");
891 return -EMEDIUMTYPE;
892 }
893
894 /* check partition alignment and total enhanced size */
895 if (conf->user.enh_size) {
896 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
897 conf->user.enh_start % mmc->hc_wp_grp_size) {
898 printf("User data enhanced area not HC WP group "
899 "size aligned\n");
900 return -EINVAL;
901 }
902 part_attrs |= EXT_CSD_ENH_USR;
903 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
904 if (mmc->high_capacity) {
905 enh_start_addr = conf->user.enh_start;
906 } else {
907 enh_start_addr = (conf->user.enh_start << 9);
908 }
909 } else {
910 enh_size_mult = 0;
911 enh_start_addr = 0;
912 }
913 tot_enh_size_mult += enh_size_mult;
914
915 for (pidx = 0; pidx < 4; pidx++) {
916 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
917 printf("GP%i partition not HC WP group size "
918 "aligned\n", pidx+1);
919 return -EINVAL;
920 }
921 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
922 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
923 part_attrs |= EXT_CSD_ENH_GP(pidx);
924 tot_enh_size_mult += gp_size_mult[pidx];
925 }
926 }
927
928 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
929 printf("Card does not support enhanced attribute\n");
930 return -EMEDIUMTYPE;
931 }
932
933 err = mmc_send_ext_csd(mmc, ext_csd);
934 if (err)
935 return err;
936
937 max_enh_size_mult =
938 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
939 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
940 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
941 if (tot_enh_size_mult > max_enh_size_mult) {
942 printf("Total enhanced size exceeds maximum (%u > %u)\n",
943 tot_enh_size_mult, max_enh_size_mult);
944 return -EMEDIUMTYPE;
945 }
946
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100947 /* The default value of EXT_CSD_WR_REL_SET is device
948 * dependent, the values can only be changed if the
949 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
950 * changed only once and before partitioning is completed. */
951 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
952 if (conf->user.wr_rel_change) {
953 if (conf->user.wr_rel_set)
954 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
955 else
956 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
957 }
958 for (pidx = 0; pidx < 4; pidx++) {
959 if (conf->gp_part[pidx].wr_rel_change) {
960 if (conf->gp_part[pidx].wr_rel_set)
961 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
962 else
963 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
964 }
965 }
966
967 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
968 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
969 puts("Card does not support host controlled partition write "
970 "reliability settings\n");
971 return -EMEDIUMTYPE;
972 }
973
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100974 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
975 EXT_CSD_PARTITION_SETTING_COMPLETED) {
976 printf("Card already partitioned\n");
977 return -EPERM;
978 }
979
980 if (mode == MMC_HWPART_CONF_CHECK)
981 return 0;
982
983 /* Partitioning requires high-capacity size definitions */
984 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
985 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
986 EXT_CSD_ERASE_GROUP_DEF, 1);
987
988 if (err)
989 return err;
990
991 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
992
993 /* update erase group size to be high-capacity */
994 mmc->erase_grp_size =
995 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
996
997 }
998
999 /* all OK, write the configuration */
1000 for (i = 0; i < 4; i++) {
1001 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1002 EXT_CSD_ENH_START_ADDR+i,
1003 (enh_start_addr >> (i*8)) & 0xFF);
1004 if (err)
1005 return err;
1006 }
1007 for (i = 0; i < 3; i++) {
1008 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1009 EXT_CSD_ENH_SIZE_MULT+i,
1010 (enh_size_mult >> (i*8)) & 0xFF);
1011 if (err)
1012 return err;
1013 }
1014 for (pidx = 0; pidx < 4; pidx++) {
1015 for (i = 0; i < 3; i++) {
1016 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1017 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1018 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1019 if (err)
1020 return err;
1021 }
1022 }
1023 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1024 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1025 if (err)
1026 return err;
1027
1028 if (mode == MMC_HWPART_CONF_SET)
1029 return 0;
1030
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001031 /* The WR_REL_SET is a write-once register but shall be
1032 * written before setting PART_SETTING_COMPLETED. As it is
1033 * write-once we can only write it when completing the
1034 * partitioning. */
1035 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1036 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1037 EXT_CSD_WR_REL_SET, wr_rel_set);
1038 if (err)
1039 return err;
1040 }
1041
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001042 /* Setting PART_SETTING_COMPLETED confirms the partition
1043 * configuration but it only becomes effective after power
1044 * cycle, so we do not adjust the partition related settings
1045 * in the mmc struct. */
1046
1047 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1048 EXT_CSD_PARTITION_SETTING,
1049 EXT_CSD_PARTITION_SETTING_COMPLETED);
1050 if (err)
1051 return err;
1052
1053 return 0;
1054}
1055
Simon Glasse7881d82017-07-29 11:35:31 -06001056#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +00001057int mmc_getcd(struct mmc *mmc)
1058{
1059 int cd;
1060
1061 cd = board_mmc_getcd(mmc);
1062
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001063 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001064 if (mmc->cfg->ops->getcd)
1065 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001066 else
1067 cd = 1;
1068 }
Thierry Reding48972d92012-01-02 01:15:37 +00001069
1070 return cd;
1071}
Simon Glass8ca51e52016-06-12 23:30:22 -06001072#endif
Thierry Reding48972d92012-01-02 01:15:37 +00001073
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001074static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -05001075{
1076 struct mmc_cmd cmd;
1077 struct mmc_data data;
1078
1079 /* Switch the frequency */
1080 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1081 cmd.resp_type = MMC_RSP_R1;
1082 cmd.cmdarg = (mode << 31) | 0xffffff;
1083 cmd.cmdarg &= ~(0xf << (group * 4));
1084 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -05001085
1086 data.dest = (char *)resp;
1087 data.blocksize = 64;
1088 data.blocks = 1;
1089 data.flags = MMC_DATA_READ;
1090
1091 return mmc_send_cmd(mmc, &cmd, &data);
1092}
1093
1094
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001095static int sd_get_capabilities(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001096{
1097 int err;
1098 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +05301099 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1100 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -05001101 struct mmc_data data;
1102 int timeout;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001103 u32 sd3_bus_mode;
Andy Fleming272cc702008-10-30 16:41:01 -05001104
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001105 mmc->card_caps = MMC_MODE_1BIT;
Andy Fleming272cc702008-10-30 16:41:01 -05001106
Thomas Choud52ebf12010-12-24 13:12:21 +00001107 if (mmc_host_is_spi(mmc))
1108 return 0;
1109
Andy Fleming272cc702008-10-30 16:41:01 -05001110 /* Read the SCR to find out if this card supports higher speeds */
1111 cmd.cmdidx = MMC_CMD_APP_CMD;
1112 cmd.resp_type = MMC_RSP_R1;
1113 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001114
1115 err = mmc_send_cmd(mmc, &cmd, NULL);
1116
1117 if (err)
1118 return err;
1119
1120 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1121 cmd.resp_type = MMC_RSP_R1;
1122 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001123
1124 timeout = 3;
1125
1126retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +00001127 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -05001128 data.blocksize = 8;
1129 data.blocks = 1;
1130 data.flags = MMC_DATA_READ;
1131
1132 err = mmc_send_cmd(mmc, &cmd, &data);
1133
1134 if (err) {
1135 if (timeout--)
1136 goto retry_scr;
1137
1138 return err;
1139 }
1140
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001141 mmc->scr[0] = __be32_to_cpu(scr[0]);
1142 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -05001143
1144 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -07001145 case 0:
1146 mmc->version = SD_VERSION_1_0;
1147 break;
1148 case 1:
1149 mmc->version = SD_VERSION_1_10;
1150 break;
1151 case 2:
1152 mmc->version = SD_VERSION_2;
1153 if ((mmc->scr[0] >> 15) & 0x1)
1154 mmc->version = SD_VERSION_3;
1155 break;
1156 default:
1157 mmc->version = SD_VERSION_1_0;
1158 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001159 }
1160
Alagu Sankarb44c7082010-05-12 15:08:24 +05301161 if (mmc->scr[0] & SD_DATA_4BIT)
1162 mmc->card_caps |= MMC_MODE_4BIT;
1163
Andy Fleming272cc702008-10-30 16:41:01 -05001164 /* Version 1.0 doesn't support switching */
1165 if (mmc->version == SD_VERSION_1_0)
1166 return 0;
1167
1168 timeout = 4;
1169 while (timeout--) {
1170 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +00001171 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001172
1173 if (err)
1174 return err;
1175
1176 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001177 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -05001178 break;
1179 }
1180
Andy Fleming272cc702008-10-30 16:41:01 -05001181 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001182 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1183 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Fleming272cc702008-10-30 16:41:01 -05001184
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001185 /* Version before 3.0 don't support UHS modes */
1186 if (mmc->version < SD_VERSION_3)
1187 return 0;
1188
1189 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1190 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1191 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1192 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1193 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1194 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1195 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1196 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1197 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1198 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1199 mmc->card_caps |= MMC_CAP(UHS_DDR50);
1200
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001201 return 0;
1202}
1203
1204static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1205{
1206 int err;
1207
1208 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001209 int speed;
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001210
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001211 switch (mode) {
1212 case SD_LEGACY:
1213 case UHS_SDR12:
1214 speed = UHS_SDR12_BUS_SPEED;
1215 break;
1216 case SD_HS:
1217 case UHS_SDR25:
1218 speed = UHS_SDR25_BUS_SPEED;
1219 break;
1220 case UHS_SDR50:
1221 speed = UHS_SDR50_BUS_SPEED;
1222 break;
1223 case UHS_DDR50:
1224 speed = UHS_DDR50_BUS_SPEED;
1225 break;
1226 case UHS_SDR104:
1227 speed = UHS_SDR104_BUS_SPEED;
1228 break;
1229 default:
1230 return -EINVAL;
1231 }
1232
1233 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001234 if (err)
1235 return err;
1236
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001237 if ((__be32_to_cpu(switch_status[4]) >> 24) != speed)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001238 return -ENOTSUPP;
1239
1240 return 0;
1241}
1242
1243int sd_select_bus_width(struct mmc *mmc, int w)
1244{
1245 int err;
1246 struct mmc_cmd cmd;
1247
1248 if ((w != 4) && (w != 1))
1249 return -EINVAL;
1250
1251 cmd.cmdidx = MMC_CMD_APP_CMD;
1252 cmd.resp_type = MMC_RSP_R1;
1253 cmd.cmdarg = mmc->rca << 16;
1254
1255 err = mmc_send_cmd(mmc, &cmd, NULL);
1256 if (err)
1257 return err;
1258
1259 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1260 cmd.resp_type = MMC_RSP_R1;
1261 if (w == 4)
1262 cmd.cmdarg = 2;
1263 else if (w == 1)
1264 cmd.cmdarg = 0;
1265 err = mmc_send_cmd(mmc, &cmd, NULL);
1266 if (err)
1267 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001268
1269 return 0;
1270}
1271
Peng Fan3697e592016-09-01 11:13:38 +08001272static int sd_read_ssr(struct mmc *mmc)
1273{
1274 int err, i;
1275 struct mmc_cmd cmd;
1276 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1277 struct mmc_data data;
1278 int timeout = 3;
1279 unsigned int au, eo, et, es;
1280
1281 cmd.cmdidx = MMC_CMD_APP_CMD;
1282 cmd.resp_type = MMC_RSP_R1;
1283 cmd.cmdarg = mmc->rca << 16;
1284
1285 err = mmc_send_cmd(mmc, &cmd, NULL);
1286 if (err)
1287 return err;
1288
1289 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1290 cmd.resp_type = MMC_RSP_R1;
1291 cmd.cmdarg = 0;
1292
1293retry_ssr:
1294 data.dest = (char *)ssr;
1295 data.blocksize = 64;
1296 data.blocks = 1;
1297 data.flags = MMC_DATA_READ;
1298
1299 err = mmc_send_cmd(mmc, &cmd, &data);
1300 if (err) {
1301 if (timeout--)
1302 goto retry_ssr;
1303
1304 return err;
1305 }
1306
1307 for (i = 0; i < 16; i++)
1308 ssr[i] = be32_to_cpu(ssr[i]);
1309
1310 au = (ssr[2] >> 12) & 0xF;
1311 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1312 mmc->ssr.au = sd_au_size[au];
1313 es = (ssr[3] >> 24) & 0xFF;
1314 es |= (ssr[2] & 0xFF) << 8;
1315 et = (ssr[3] >> 18) & 0x3F;
1316 if (es && et) {
1317 eo = (ssr[3] >> 16) & 0x3;
1318 mmc->ssr.erase_timeout = (et * 1000) / es;
1319 mmc->ssr.erase_offset = eo * 1000;
1320 }
1321 } else {
1322 debug("Invalid Allocation Unit Size.\n");
1323 }
1324
1325 return 0;
1326}
1327
Andy Fleming272cc702008-10-30 16:41:01 -05001328/* frequency bases */
1329/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001330static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001331 10000,
1332 100000,
1333 1000000,
1334 10000000,
1335};
1336
1337/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1338 * to platforms without floating point.
1339 */
Simon Glass61fe0762016-05-14 14:02:57 -06001340static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001341 0, /* reserved */
1342 10,
1343 12,
1344 13,
1345 15,
1346 20,
1347 25,
1348 30,
1349 35,
1350 40,
1351 45,
1352 50,
1353 55,
1354 60,
1355 70,
1356 80,
1357};
1358
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001359static inline int bus_width(uint cap)
1360{
1361 if (cap == MMC_MODE_8BIT)
1362 return 8;
1363 if (cap == MMC_MODE_4BIT)
1364 return 4;
1365 if (cap == MMC_MODE_1BIT)
1366 return 1;
1367 printf("invalid bus witdh capability 0x%x\n", cap);
1368 return 0;
1369}
1370
Simon Glasse7881d82017-07-29 11:35:31 -06001371#if !CONFIG_IS_ENABLED(DM_MMC)
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001372static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1373{
1374 return -ENOTSUPP;
1375}
1376
Jean-Jacques Hiblot318a7a52017-09-21 16:30:01 +02001377static void mmc_send_init_stream(struct mmc *mmc)
1378{
1379}
1380
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001381static int mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001382{
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001383 int ret = 0;
1384
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001385 if (mmc->cfg->ops->set_ios)
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001386 ret = mmc->cfg->ops->set_ios(mmc);
1387
1388 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001389}
Simon Glass8ca51e52016-06-12 23:30:22 -06001390#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001391
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001392int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
Andy Fleming272cc702008-10-30 16:41:01 -05001393{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001394 if (clock > mmc->cfg->f_max)
1395 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001396
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001397 if (clock < mmc->cfg->f_min)
1398 clock = mmc->cfg->f_min;
Andy Fleming272cc702008-10-30 16:41:01 -05001399
1400 mmc->clock = clock;
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001401 mmc->clk_disable = disable;
Andy Fleming272cc702008-10-30 16:41:01 -05001402
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001403 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001404}
1405
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001406static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001407{
1408 mmc->bus_width = width;
1409
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001410 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001411}
1412
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001413#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1414/*
1415 * helper function to display the capabilities in a human
1416 * friendly manner. The capabilities include bus width and
1417 * supported modes.
1418 */
1419void mmc_dump_capabilities(const char *text, uint caps)
1420{
1421 enum bus_mode mode;
1422
1423 printf("%s: widths [", text);
1424 if (caps & MMC_MODE_8BIT)
1425 printf("8, ");
1426 if (caps & MMC_MODE_4BIT)
1427 printf("4, ");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001428 if (caps & MMC_MODE_1BIT)
1429 printf("1, ");
1430 printf("\b\b] modes [");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001431 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1432 if (MMC_CAP(mode) & caps)
1433 printf("%s, ", mmc_mode_name(mode));
1434 printf("\b\b]\n");
1435}
1436#endif
1437
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001438struct mode_width_tuning {
1439 enum bus_mode mode;
1440 uint widths;
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001441 uint tuning;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001442};
1443
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001444static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1445{
1446 mmc->signal_voltage = signal_voltage;
1447 return mmc_set_ios(mmc);
1448}
1449
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001450static const struct mode_width_tuning sd_modes_by_pref[] = {
1451 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001452 .mode = UHS_SDR104,
1453 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1454 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1455 },
1456 {
1457 .mode = UHS_SDR50,
1458 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1459 },
1460 {
1461 .mode = UHS_DDR50,
1462 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1463 },
1464 {
1465 .mode = UHS_SDR25,
1466 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1467 },
1468 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001469 .mode = SD_HS,
1470 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1471 },
1472 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001473 .mode = UHS_SDR12,
1474 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1475 },
1476 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001477 .mode = SD_LEGACY,
1478 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1479 }
1480};
1481
1482#define for_each_sd_mode_by_pref(caps, mwt) \
1483 for (mwt = sd_modes_by_pref;\
1484 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1485 mwt++) \
1486 if (caps & MMC_CAP(mwt->mode))
1487
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001488static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001489{
1490 int err;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001491 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1492 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001493 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
1494 uint caps;
1495
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001496
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001497 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001498 caps = card_caps & (mmc->host_caps | MMC_MODE_1BIT);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001499
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001500 if (!uhs_en)
1501 caps &= ~UHS_CAPS;
1502
1503 for_each_sd_mode_by_pref(caps, mwt) {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001504 uint *w;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001505
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001506 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001507 if (*w & caps & mwt->widths) {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001508 debug("trying mode %s width %d (at %d MHz)\n",
1509 mmc_mode_name(mwt->mode),
1510 bus_width(*w),
1511 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001512
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001513 /* configure the bus width (card + host) */
1514 err = sd_select_bus_width(mmc, bus_width(*w));
1515 if (err)
1516 goto error;
1517 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001518
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001519 /* configure the bus mode (card) */
1520 err = sd_set_card_speed(mmc, mwt->mode);
1521 if (err)
1522 goto error;
1523
1524 /* configure the bus mode (host) */
1525 mmc_select_mode(mmc, mwt->mode);
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001526 mmc_set_clock(mmc, mmc->tran_speed, false);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001527
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001528 /* execute tuning if needed */
1529 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1530 err = mmc_execute_tuning(mmc,
1531 mwt->tuning);
1532 if (err) {
1533 debug("tuning failed\n");
1534 goto error;
1535 }
1536 }
1537
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001538 err = sd_read_ssr(mmc);
1539 if (!err)
1540 return 0;
1541
1542 printf("bad ssr\n");
1543
1544error:
1545 /* revert to a safer bus speed */
1546 mmc_select_mode(mmc, SD_LEGACY);
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001547 mmc_set_clock(mmc, mmc->tran_speed, false);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001548 }
1549 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001550 }
1551
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001552 printf("unable to select a mode\n");
1553 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001554}
1555
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001556/*
1557 * read the compare the part of ext csd that is constant.
1558 * This can be used to check that the transfer is working
1559 * as expected.
1560 */
1561static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1562{
1563 int err;
1564 const u8 *ext_csd = mmc->ext_csd;
1565 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1566
1567 err = mmc_send_ext_csd(mmc, test_csd);
1568 if (err)
1569 return err;
1570
1571 /* Only compare read only fields */
1572 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1573 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1574 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1575 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1576 ext_csd[EXT_CSD_REV]
1577 == test_csd[EXT_CSD_REV] &&
1578 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1579 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1580 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1581 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1582 return 0;
1583
1584 return -EBADMSG;
1585}
1586
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001587static const struct mode_width_tuning mmc_modes_by_pref[] = {
1588 {
1589 .mode = MMC_HS_200,
1590 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001591 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001592 },
1593 {
1594 .mode = MMC_DDR_52,
1595 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1596 },
1597 {
1598 .mode = MMC_HS_52,
1599 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1600 },
1601 {
1602 .mode = MMC_HS,
1603 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1604 },
1605 {
1606 .mode = MMC_LEGACY,
1607 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1608 }
1609};
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001610
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001611#define for_each_mmc_mode_by_pref(caps, mwt) \
1612 for (mwt = mmc_modes_by_pref;\
1613 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
1614 mwt++) \
1615 if (caps & MMC_CAP(mwt->mode))
1616
1617static const struct ext_csd_bus_width {
1618 uint cap;
1619 bool is_ddr;
1620 uint ext_csd_bits;
1621} ext_csd_bus_width[] = {
1622 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
1623 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
1624 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
1625 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
1626 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
1627};
1628
1629#define for_each_supported_width(caps, ddr, ecbv) \
1630 for (ecbv = ext_csd_bus_width;\
1631 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
1632 ecbv++) \
1633 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
1634
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001635static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001636{
1637 int err;
1638 const struct mode_width_tuning *mwt;
1639 const struct ext_csd_bus_width *ecbw;
1640
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001641 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001642 card_caps &= (mmc->host_caps | MMC_MODE_1BIT);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001643
1644 /* Only version 4 of MMC supports wider bus widths */
1645 if (mmc->version < MMC_VERSION_4)
1646 return 0;
1647
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001648 if (!mmc->ext_csd) {
1649 debug("No ext_csd found!\n"); /* this should enver happen */
1650 return -ENOTSUPP;
1651 }
1652
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001653 mmc_set_clock(mmc, mmc->legacy_speed, false);
1654
1655 for_each_mmc_mode_by_pref(card_caps, mwt) {
1656 for_each_supported_width(card_caps & mwt->widths,
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001657 mmc_is_mode_ddr(mwt->mode), ecbw) {
1658 debug("trying mode %s width %d (at %d MHz)\n",
1659 mmc_mode_name(mwt->mode),
1660 bus_width(ecbw->cap),
1661 mmc_mode2freq(mmc, mwt->mode) / 1000000);
1662 /* configure the bus width (card + host) */
1663 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1664 EXT_CSD_BUS_WIDTH,
1665 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
1666 if (err)
1667 goto error;
1668 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
1669
1670 /* configure the bus speed (card) */
1671 err = mmc_set_card_speed(mmc, mwt->mode);
1672 if (err)
1673 goto error;
1674
1675 /*
1676 * configure the bus width AND the ddr mode (card)
1677 * The host side will be taken care of in the next step
1678 */
1679 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
1680 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1681 EXT_CSD_BUS_WIDTH,
1682 ecbw->ext_csd_bits);
1683 if (err)
1684 goto error;
1685 }
1686
1687 /* configure the bus mode (host) */
1688 mmc_select_mode(mmc, mwt->mode);
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001689 mmc_set_clock(mmc, mmc->tran_speed, false);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001690
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001691 /* execute tuning if needed */
1692 if (mwt->tuning) {
1693 err = mmc_execute_tuning(mmc, mwt->tuning);
1694 if (err) {
1695 debug("tuning failed\n");
1696 goto error;
1697 }
1698 }
1699
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001700 /* do a transfer to check the configuration */
1701 err = mmc_read_and_compare_ext_csd(mmc);
1702 if (!err)
1703 return 0;
1704error:
1705 /* if an error occured, revert to a safer bus mode */
1706 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1707 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
1708 mmc_select_mode(mmc, MMC_LEGACY);
1709 mmc_set_bus_width(mmc, 1);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001710 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001711 }
1712
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001713 printf("unable to select a mode\n");
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001714
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001715 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001716}
1717
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001718static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001719{
1720 int err, i;
1721 u64 capacity;
1722 bool has_parts = false;
1723 bool part_completed;
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001724 u8 *ext_csd;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001725
1726 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
1727 return 0;
1728
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001729 ext_csd = malloc_cache_aligned(MMC_MAX_BLOCK_LEN);
1730 if (!ext_csd)
1731 return -ENOMEM;
1732
1733 mmc->ext_csd = ext_csd;
1734
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001735 /* check ext_csd version and capacity */
1736 err = mmc_send_ext_csd(mmc, ext_csd);
1737 if (err)
1738 return err;
1739 if (ext_csd[EXT_CSD_REV] >= 2) {
1740 /*
1741 * According to the JEDEC Standard, the value of
1742 * ext_csd's capacity is valid if the value is more
1743 * than 2GB
1744 */
1745 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1746 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1747 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1748 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1749 capacity *= MMC_MAX_BLOCK_LEN;
1750 if ((capacity >> 20) > 2 * 1024)
1751 mmc->capacity_user = capacity;
1752 }
1753
1754 switch (ext_csd[EXT_CSD_REV]) {
1755 case 1:
1756 mmc->version = MMC_VERSION_4_1;
1757 break;
1758 case 2:
1759 mmc->version = MMC_VERSION_4_2;
1760 break;
1761 case 3:
1762 mmc->version = MMC_VERSION_4_3;
1763 break;
1764 case 5:
1765 mmc->version = MMC_VERSION_4_41;
1766 break;
1767 case 6:
1768 mmc->version = MMC_VERSION_4_5;
1769 break;
1770 case 7:
1771 mmc->version = MMC_VERSION_5_0;
1772 break;
1773 case 8:
1774 mmc->version = MMC_VERSION_5_1;
1775 break;
1776 }
1777
1778 /* The partition data may be non-zero but it is only
1779 * effective if PARTITION_SETTING_COMPLETED is set in
1780 * EXT_CSD, so ignore any data if this bit is not set,
1781 * except for enabling the high-capacity group size
1782 * definition (see below).
1783 */
1784 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
1785 EXT_CSD_PARTITION_SETTING_COMPLETED);
1786
1787 /* store the partition info of emmc */
1788 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
1789 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1790 ext_csd[EXT_CSD_BOOT_MULT])
1791 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1792 if (part_completed &&
1793 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
1794 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
1795
1796 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1797
1798 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1799
1800 for (i = 0; i < 4; i++) {
1801 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1802 uint mult = (ext_csd[idx + 2] << 16) +
1803 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1804 if (mult)
1805 has_parts = true;
1806 if (!part_completed)
1807 continue;
1808 mmc->capacity_gp[i] = mult;
1809 mmc->capacity_gp[i] *=
1810 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1811 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1812 mmc->capacity_gp[i] <<= 19;
1813 }
1814
1815 if (part_completed) {
1816 mmc->enh_user_size =
1817 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
1818 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
1819 ext_csd[EXT_CSD_ENH_SIZE_MULT];
1820 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1821 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1822 mmc->enh_user_size <<= 19;
1823 mmc->enh_user_start =
1824 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
1825 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
1826 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
1827 ext_csd[EXT_CSD_ENH_START_ADDR];
1828 if (mmc->high_capacity)
1829 mmc->enh_user_start <<= 9;
1830 }
1831
1832 /*
1833 * Host needs to enable ERASE_GRP_DEF bit if device is
1834 * partitioned. This bit will be lost every time after a reset
1835 * or power off. This will affect erase size.
1836 */
1837 if (part_completed)
1838 has_parts = true;
1839 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
1840 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
1841 has_parts = true;
1842 if (has_parts) {
1843 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1844 EXT_CSD_ERASE_GROUP_DEF, 1);
1845
1846 if (err)
1847 return err;
1848
1849 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1850 }
1851
1852 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
1853 /* Read out group size from ext_csd */
1854 mmc->erase_grp_size =
1855 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1856 /*
1857 * if high capacity and partition setting completed
1858 * SEC_COUNT is valid even if it is smaller than 2 GiB
1859 * JEDEC Standard JESD84-B45, 6.2.4
1860 */
1861 if (mmc->high_capacity && part_completed) {
1862 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1863 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1864 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1865 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1866 capacity *= MMC_MAX_BLOCK_LEN;
1867 mmc->capacity_user = capacity;
1868 }
1869 } else {
1870 /* Calculate the group size from the csd value. */
1871 int erase_gsz, erase_gmul;
1872
1873 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1874 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1875 mmc->erase_grp_size = (erase_gsz + 1)
1876 * (erase_gmul + 1);
1877 }
1878
1879 mmc->hc_wp_grp_size = 1024
1880 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1881 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1882
1883 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1884
1885 return 0;
1886}
1887
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001888static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001889{
Stephen Warrenf866a462013-06-11 15:14:01 -06001890 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05001891 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001892 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05001893 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06001894 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05001895
Thomas Choud52ebf12010-12-24 13:12:21 +00001896#ifdef CONFIG_MMC_SPI_CRC_ON
1897 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1898 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1899 cmd.resp_type = MMC_RSP_R1;
1900 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001901 err = mmc_send_cmd(mmc, &cmd, NULL);
Thomas Choud52ebf12010-12-24 13:12:21 +00001902 if (err)
1903 return err;
1904 }
1905#endif
1906
Andy Fleming272cc702008-10-30 16:41:01 -05001907 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001908 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1909 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05001910 cmd.resp_type = MMC_RSP_R2;
1911 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001912
1913 err = mmc_send_cmd(mmc, &cmd, NULL);
1914
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02001915#ifdef CONFIG_MMC_QUIRKS
1916 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SEND_CID)) {
1917 int retries = 4;
1918 /*
1919 * It has been seen that SEND_CID may fail on the first
1920 * attempt, let's try a few more time
1921 */
1922 do {
1923 err = mmc_send_cmd(mmc, &cmd, NULL);
1924 if (!err)
1925 break;
1926 } while (retries--);
1927 }
1928#endif
1929
Andy Fleming272cc702008-10-30 16:41:01 -05001930 if (err)
1931 return err;
1932
1933 memcpy(mmc->cid, cmd.response, 16);
1934
1935 /*
1936 * For MMC cards, set the Relative Address.
1937 * For SD cards, get the Relatvie Address.
1938 * This also puts the cards into Standby State
1939 */
Thomas Choud52ebf12010-12-24 13:12:21 +00001940 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1941 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1942 cmd.cmdarg = mmc->rca << 16;
1943 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05001944
Thomas Choud52ebf12010-12-24 13:12:21 +00001945 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001946
Thomas Choud52ebf12010-12-24 13:12:21 +00001947 if (err)
1948 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001949
Thomas Choud52ebf12010-12-24 13:12:21 +00001950 if (IS_SD(mmc))
1951 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1952 }
Andy Fleming272cc702008-10-30 16:41:01 -05001953
1954 /* Get the Card-Specific Data */
1955 cmd.cmdidx = MMC_CMD_SEND_CSD;
1956 cmd.resp_type = MMC_RSP_R2;
1957 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001958
1959 err = mmc_send_cmd(mmc, &cmd, NULL);
1960
1961 if (err)
1962 return err;
1963
Rabin Vincent998be3d2009-04-05 13:30:56 +05301964 mmc->csd[0] = cmd.response[0];
1965 mmc->csd[1] = cmd.response[1];
1966 mmc->csd[2] = cmd.response[2];
1967 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05001968
1969 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301970 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05001971
1972 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07001973 case 0:
1974 mmc->version = MMC_VERSION_1_2;
1975 break;
1976 case 1:
1977 mmc->version = MMC_VERSION_1_4;
1978 break;
1979 case 2:
1980 mmc->version = MMC_VERSION_2_2;
1981 break;
1982 case 3:
1983 mmc->version = MMC_VERSION_3;
1984 break;
1985 case 4:
1986 mmc->version = MMC_VERSION_4;
1987 break;
1988 default:
1989 mmc->version = MMC_VERSION_1_2;
1990 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001991 }
1992 }
1993
1994 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301995 freq = fbase[(cmd.response[0] & 0x7)];
1996 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05001997
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001998 mmc->legacy_speed = freq * mult;
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001999 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05002000
Markus Niebelab711882013-12-16 13:40:46 +01002001 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05302002 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05002003
2004 if (IS_SD(mmc))
2005 mmc->write_bl_len = mmc->read_bl_len;
2006 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05302007 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05002008
2009 if (mmc->high_capacity) {
2010 csize = (mmc->csd[1] & 0x3f) << 16
2011 | (mmc->csd[2] & 0xffff0000) >> 16;
2012 cmult = 8;
2013 } else {
2014 csize = (mmc->csd[1] & 0x3ff) << 2
2015 | (mmc->csd[2] & 0xc0000000) >> 30;
2016 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2017 }
2018
Stephen Warrenf866a462013-06-11 15:14:01 -06002019 mmc->capacity_user = (csize + 1) << (cmult + 2);
2020 mmc->capacity_user *= mmc->read_bl_len;
2021 mmc->capacity_boot = 0;
2022 mmc->capacity_rpmb = 0;
2023 for (i = 0; i < 4; i++)
2024 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002025
Simon Glass8bfa1952013-04-03 08:54:30 +00002026 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2027 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05002028
Simon Glass8bfa1952013-04-03 08:54:30 +00002029 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2030 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05002031
Markus Niebelab711882013-12-16 13:40:46 +01002032 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2033 cmd.cmdidx = MMC_CMD_SET_DSR;
2034 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2035 cmd.resp_type = MMC_RSP_NONE;
2036 if (mmc_send_cmd(mmc, &cmd, NULL))
2037 printf("MMC: SET_DSR failed\n");
2038 }
2039
Andy Fleming272cc702008-10-30 16:41:01 -05002040 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002041 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2042 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00002043 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002044 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00002045 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002046
Thomas Choud52ebf12010-12-24 13:12:21 +00002047 if (err)
2048 return err;
2049 }
Andy Fleming272cc702008-10-30 16:41:01 -05002050
Lei Wene6f99a52011-06-22 17:03:31 +00002051 /*
2052 * For SD, its erase group is always one sector
2053 */
2054 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00002055 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00002056
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002057 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002058 if (err)
2059 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05302060
Simon Glassc40fdca2016-05-01 13:52:35 -06002061 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06002062 if (err)
2063 return err;
2064
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002065 if (IS_SD(mmc)) {
2066 err = sd_get_capabilities(mmc);
2067 if (err)
2068 return err;
2069 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2070 } else {
2071 err = mmc_get_capabilities(mmc);
2072 if (err)
2073 return err;
2074 mmc_select_mode_and_width(mmc, mmc->card_caps);
2075 }
Andy Fleming272cc702008-10-30 16:41:01 -05002076
2077 if (err)
2078 return err;
2079
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002080 mmc->best_mode = mmc->selected_mode;
Jaehoon Chungad5fd922012-03-26 21:16:03 +00002081
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002082 /* Fix the block length for DDR mode */
2083 if (mmc->ddr_mode) {
2084 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
2085 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
2086 }
2087
Andy Fleming272cc702008-10-30 16:41:01 -05002088 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06002089 bdesc = mmc_get_blk_desc(mmc);
2090 bdesc->lun = 0;
2091 bdesc->hwpart = 0;
2092 bdesc->type = 0;
2093 bdesc->blksz = mmc->read_bl_len;
2094 bdesc->log2blksz = LOG2(bdesc->blksz);
2095 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01002096#if !defined(CONFIG_SPL_BUILD) || \
2097 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
2098 !defined(CONFIG_USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06002099 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00002100 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2101 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002102 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002103 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2104 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2105 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002106 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002107 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01002108#else
Simon Glassc40fdca2016-05-01 13:52:35 -06002109 bdesc->vendor[0] = 0;
2110 bdesc->product[0] = 0;
2111 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01002112#endif
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00002113#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Simon Glassc40fdca2016-05-01 13:52:35 -06002114 part_init(bdesc);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00002115#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002116
2117 return 0;
2118}
2119
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002120static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002121{
2122 struct mmc_cmd cmd;
2123 int err;
2124
2125 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2126 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002127 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05002128 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05002129
2130 err = mmc_send_cmd(mmc, &cmd, NULL);
2131
2132 if (err)
2133 return err;
2134
Rabin Vincent998be3d2009-04-05 13:30:56 +05302135 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002136 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002137 else
2138 mmc->version = SD_VERSION_2;
2139
2140 return 0;
2141}
2142
Simon Glassc4d660d2017-07-04 13:31:19 -06002143#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002144/* board-specific MMC power initializations. */
2145__weak void board_mmc_power_init(void)
2146{
2147}
Simon Glass05cbeb72017-04-22 19:10:56 -06002148#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002149
Peng Fan2051aef2016-10-11 15:08:43 +08002150static int mmc_power_init(struct mmc *mmc)
2151{
Simon Glassc4d660d2017-07-04 13:31:19 -06002152#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002153#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08002154 int ret;
2155
2156 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002157 &mmc->vmmc_supply);
2158 if (ret)
Jaehoon Chung288db7c2016-10-24 15:22:22 +09002159 debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002160
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002161 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2162 &mmc->vqmmc_supply);
2163 if (ret)
2164 debug("%s: No vqmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002165#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06002166#else /* !CONFIG_DM_MMC */
2167 /*
2168 * Driver model should use a regulator, as above, rather than calling
2169 * out to board code.
2170 */
2171 board_mmc_power_init();
2172#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002173 return 0;
2174}
2175
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002176/*
2177 * put the host in the initial state:
2178 * - turn on Vdd (card power supply)
2179 * - configure the bus width and clock to minimal values
2180 */
2181static void mmc_set_initial_state(struct mmc *mmc)
2182{
2183 int err;
2184
2185 /* First try to set 3.3V. If it fails set to 1.8V */
2186 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2187 if (err != 0)
2188 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2189 if (err != 0)
2190 printf("mmc: failed to set signal voltage\n");
2191
2192 mmc_select_mode(mmc, MMC_LEGACY);
2193 mmc_set_bus_width(mmc, 1);
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02002194 mmc_set_clock(mmc, 0, false);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002195}
2196
2197static int mmc_power_on(struct mmc *mmc)
2198{
2199#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2200 if (mmc->vmmc_supply) {
2201 int ret = regulator_set_enable(mmc->vmmc_supply, true);
2202
2203 if (ret) {
2204 puts("Error enabling VMMC supply\n");
2205 return ret;
2206 }
2207 }
2208#endif
2209 return 0;
2210}
2211
2212static int mmc_power_off(struct mmc *mmc)
2213{
Kishon Vijay Abraham I2e7410d2017-09-21 16:30:04 +02002214 mmc_set_clock(mmc, 1, true);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002215#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2216 if (mmc->vmmc_supply) {
2217 int ret = regulator_set_enable(mmc->vmmc_supply, false);
2218
2219 if (ret) {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002220 debug("Error disabling VMMC supply\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002221 return ret;
2222 }
2223 }
2224#endif
2225 return 0;
2226}
2227
2228static int mmc_power_cycle(struct mmc *mmc)
2229{
2230 int ret;
2231
2232 ret = mmc_power_off(mmc);
2233 if (ret)
2234 return ret;
2235 /*
2236 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2237 * to be on the safer side.
2238 */
2239 udelay(2000);
2240 return mmc_power_on(mmc);
2241}
2242
Che-Liang Chioue9550442012-11-28 15:21:13 +00002243int mmc_start_init(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002244{
Simon Glass8ca51e52016-06-12 23:30:22 -06002245 bool no_card;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002246 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
Macpaul Linafd59322011-11-14 23:35:39 +00002247 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05002248
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002249 mmc->host_caps = mmc->cfg->host_caps;
2250
Pantelis Antoniouab769f22014-02-26 19:28:45 +02002251 /* we pretend there's no card when init is NULL */
Simon Glass8ca51e52016-06-12 23:30:22 -06002252 no_card = mmc_getcd(mmc) == 0;
Simon Glasse7881d82017-07-29 11:35:31 -06002253#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06002254 no_card = no_card || (mmc->cfg->ops->init == NULL);
2255#endif
2256 if (no_card) {
Thierry Reding48972d92012-01-02 01:15:37 +00002257 mmc->has_init = 0;
Paul Burton56196822013-09-04 16:12:25 +01002258#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Thierry Reding48972d92012-01-02 01:15:37 +00002259 printf("MMC: no card present\n");
Paul Burton56196822013-09-04 16:12:25 +01002260#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002261 return -ENOMEDIUM;
Thierry Reding48972d92012-01-02 01:15:37 +00002262 }
2263
Lei Wenbc897b12011-05-02 16:26:26 +00002264 if (mmc->has_init)
2265 return 0;
2266
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08002267#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
2268 mmc_adapter_card_type_ident();
2269#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002270 err = mmc_power_init(mmc);
2271 if (err)
2272 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002273
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002274#ifdef CONFIG_MMC_QUIRKS
2275 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
2276 MMC_QUIRK_RETRY_SEND_CID;
2277#endif
2278
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002279 err = mmc_power_cycle(mmc);
2280 if (err) {
2281 /*
2282 * if power cycling is not supported, we should not try
2283 * to use the UHS modes, because we wouldn't be able to
2284 * recover from an error during the UHS initialization.
2285 */
2286 debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
2287 uhs_en = false;
2288 mmc->host_caps &= ~UHS_CAPS;
2289 err = mmc_power_on(mmc);
2290 }
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002291 if (err)
2292 return err;
2293
Simon Glasse7881d82017-07-29 11:35:31 -06002294#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06002295 /* The device has already been probed ready for use */
2296#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02002297 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002298 err = mmc->cfg->ops->init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002299 if (err)
2300 return err;
Simon Glass8ca51e52016-06-12 23:30:22 -06002301#endif
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06002302 mmc->ddr_mode = 0;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02002303
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002304retry:
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002305 mmc_set_initial_state(mmc);
Jean-Jacques Hiblot318a7a52017-09-21 16:30:01 +02002306 mmc_send_init_stream(mmc);
2307
Andy Fleming272cc702008-10-30 16:41:01 -05002308 /* Reset the Card */
2309 err = mmc_go_idle(mmc);
2310
2311 if (err)
2312 return err;
2313
Lei Wenbc897b12011-05-02 16:26:26 +00002314 /* The internal partition reset to user partition(0) at every CMD0*/
Simon Glassc40fdca2016-05-01 13:52:35 -06002315 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00002316
Andy Fleming272cc702008-10-30 16:41:01 -05002317 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00002318 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002319
Andy Fleming272cc702008-10-30 16:41:01 -05002320 /* Now try to get the SD card's operating condition */
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002321 err = sd_send_op_cond(mmc, uhs_en);
2322 if (err && uhs_en) {
2323 uhs_en = false;
2324 mmc_power_cycle(mmc);
2325 goto retry;
2326 }
Andy Fleming272cc702008-10-30 16:41:01 -05002327
2328 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002329 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05002330 err = mmc_send_op_cond(mmc);
2331
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002332 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01002333#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -05002334 printf("Card did not respond to voltage select!\n");
Paul Burton56196822013-09-04 16:12:25 +01002335#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002336 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002337 }
2338 }
2339
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002340 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002341 mmc->init_in_progress = 1;
2342
2343 return err;
2344}
2345
2346static int mmc_complete_init(struct mmc *mmc)
2347{
2348 int err = 0;
2349
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002350 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002351 if (mmc->op_cond_pending)
2352 err = mmc_complete_op_cond(mmc);
2353
2354 if (!err)
2355 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00002356 if (err)
2357 mmc->has_init = 0;
2358 else
2359 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002360 return err;
2361}
2362
2363int mmc_init(struct mmc *mmc)
2364{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002365 int err = 0;
Marek Vasutce9eca92016-12-01 02:06:32 +01002366 __maybe_unused unsigned start;
Simon Glassc4d660d2017-07-04 13:31:19 -06002367#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06002368 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00002369
Simon Glass33fb2112016-05-01 13:52:41 -06002370 upriv->mmc = mmc;
2371#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00002372 if (mmc->has_init)
2373 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02002374
2375 start = get_timer(0);
2376
Che-Liang Chioue9550442012-11-28 15:21:13 +00002377 if (!mmc->init_in_progress)
2378 err = mmc_start_init(mmc);
2379
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002380 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002381 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01002382 if (err)
2383 printf("%s: %d, time %lu\n", __func__, err, get_timer(start));
2384
Lei Wenbc897b12011-05-02 16:26:26 +00002385 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002386}
2387
Markus Niebelab711882013-12-16 13:40:46 +01002388int mmc_set_dsr(struct mmc *mmc, u16 val)
2389{
2390 mmc->dsr = val;
2391 return 0;
2392}
2393
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002394/* CPU-specific MMC initializations */
2395__weak int cpu_mmc_init(bd_t *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05002396{
2397 return -1;
2398}
2399
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002400/* board-specific MMC initializations. */
2401__weak int board_mmc_init(bd_t *bis)
2402{
2403 return -1;
2404}
Andy Fleming272cc702008-10-30 16:41:01 -05002405
Che-Liang Chioue9550442012-11-28 15:21:13 +00002406void mmc_set_preinit(struct mmc *mmc, int preinit)
2407{
2408 mmc->preinit = preinit;
2409}
2410
Simon Glassc4d660d2017-07-04 13:31:19 -06002411#if CONFIG_IS_ENABLED(DM_MMC) && defined(CONFIG_SPL_BUILD)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002412static int mmc_probe(bd_t *bis)
2413{
2414 return 0;
2415}
Simon Glassc4d660d2017-07-04 13:31:19 -06002416#elif CONFIG_IS_ENABLED(DM_MMC)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002417static int mmc_probe(bd_t *bis)
2418{
Simon Glass4a1db6d2015-12-29 05:22:49 -07002419 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002420 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07002421 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002422
2423 ret = uclass_get(UCLASS_MMC, &uc);
2424 if (ret)
2425 return ret;
2426
Simon Glass4a1db6d2015-12-29 05:22:49 -07002427 /*
2428 * Try to add them in sequence order. Really with driver model we
2429 * should allow holes, but the current MMC list does not allow that.
2430 * So if we request 0, 1, 3 we will get 0, 1, 2.
2431 */
2432 for (i = 0; ; i++) {
2433 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
2434 if (ret == -ENODEV)
2435 break;
2436 }
2437 uclass_foreach_dev(dev, uc) {
2438 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002439 if (ret)
Simon Glass4a1db6d2015-12-29 05:22:49 -07002440 printf("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002441 }
2442
2443 return 0;
2444}
2445#else
2446static int mmc_probe(bd_t *bis)
2447{
2448 if (board_mmc_init(bis) < 0)
2449 cpu_mmc_init(bis);
2450
2451 return 0;
2452}
2453#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00002454
Andy Fleming272cc702008-10-30 16:41:01 -05002455int mmc_initialize(bd_t *bis)
2456{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02002457 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002458 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02002459 if (initialized) /* Avoid initializing mmc multiple times */
2460 return 0;
2461 initialized = 1;
2462
Simon Glassc4d660d2017-07-04 13:31:19 -06002463#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01002464#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06002465 mmc_list_init();
2466#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01002467#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002468 ret = mmc_probe(bis);
2469 if (ret)
2470 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05002471
Ying Zhangbb0dc102013-08-16 15:16:11 +08002472#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05002473 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08002474#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002475
Simon Glassc40fdca2016-05-01 13:52:35 -06002476 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05002477 return 0;
2478}
Tomas Melincd3d4882016-11-25 11:01:03 +02002479
2480#ifdef CONFIG_CMD_BKOPS_ENABLE
2481int mmc_set_bkops_enable(struct mmc *mmc)
2482{
2483 int err;
2484 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
2485
2486 err = mmc_send_ext_csd(mmc, ext_csd);
2487 if (err) {
2488 puts("Could not get ext_csd register values\n");
2489 return err;
2490 }
2491
2492 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
2493 puts("Background operations not supported on device\n");
2494 return -EMEDIUMTYPE;
2495 }
2496
2497 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
2498 puts("Background operations already enabled\n");
2499 return 0;
2500 }
2501
2502 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
2503 if (err) {
2504 puts("Failed to enable manual background operations\n");
2505 return err;
2506 }
2507
2508 puts("Enabled manual background operations\n");
2509
2510 return 0;
2511}
2512#endif