blob: 877ce1757da38bce892e0d6c319e29d39a5e7bf9 [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;
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +0200770 mmc->cardtype = cardtype;
Andy Fleming272cc702008-10-30 16:41:01 -0500771
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +0200772 if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
773 EXT_CSD_CARD_TYPE_HS200_1_8V)) {
774 mmc->card_caps |= MMC_MODE_HS200;
775 }
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900776 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200777 if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900778 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200779 mmc->card_caps |= MMC_MODE_HS_52MHz;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900780 }
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +0200781 if (cardtype & EXT_CSD_CARD_TYPE_26)
782 mmc->card_caps |= MMC_MODE_HS;
Andy Fleming272cc702008-10-30 16:41:01 -0500783
784 return 0;
785}
786
Stephen Warrenf866a462013-06-11 15:14:01 -0600787static int mmc_set_capacity(struct mmc *mmc, int part_num)
788{
789 switch (part_num) {
790 case 0:
791 mmc->capacity = mmc->capacity_user;
792 break;
793 case 1:
794 case 2:
795 mmc->capacity = mmc->capacity_boot;
796 break;
797 case 3:
798 mmc->capacity = mmc->capacity_rpmb;
799 break;
800 case 4:
801 case 5:
802 case 6:
803 case 7:
804 mmc->capacity = mmc->capacity_gp[part_num - 4];
805 break;
806 default:
807 return -1;
808 }
809
Simon Glassc40fdca2016-05-01 13:52:35 -0600810 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -0600811
812 return 0;
813}
814
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +0200815static int mmc_boot_part_access_chk(struct mmc *mmc, unsigned int part_num)
816{
817 int forbidden = 0;
818 bool change = false;
819
820 if (part_num & PART_ACCESS_MASK)
821 forbidden = MMC_CAP(MMC_HS_200);
822
823 if (MMC_CAP(mmc->selected_mode) & forbidden) {
824 debug("selected mode (%s) is forbidden for part %d\n",
825 mmc_mode_name(mmc->selected_mode), part_num);
826 change = true;
827 } else if (mmc->selected_mode != mmc->best_mode) {
828 debug("selected mode is not optimal\n");
829 change = true;
830 }
831
832 if (change)
833 return mmc_select_mode_and_width(mmc,
834 mmc->card_caps & ~forbidden);
835
836 return 0;
837}
838
Simon Glass7dba0b92016-06-12 23:30:15 -0600839int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +0000840{
Stephen Warrenf866a462013-06-11 15:14:01 -0600841 int ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000842
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +0200843 ret = mmc_boot_part_access_chk(mmc, part_num);
844 if (ret)
845 return ret;
846
Stephen Warrenf866a462013-06-11 15:14:01 -0600847 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
848 (mmc->part_config & ~PART_ACCESS_MASK)
849 | (part_num & PART_ACCESS_MASK));
Stephen Warrenf866a462013-06-11 15:14:01 -0600850
Peter Bigot6dc93e72014-09-02 18:31:23 -0500851 /*
852 * Set the capacity if the switch succeeded or was intended
853 * to return to representing the raw device.
854 */
Stephen Warren873cc1d2015-12-07 11:38:49 -0700855 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -0500856 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -0600857 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700858 }
Peter Bigot6dc93e72014-09-02 18:31:23 -0500859
860 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000861}
862
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100863int mmc_hwpart_config(struct mmc *mmc,
864 const struct mmc_hwpart_conf *conf,
865 enum mmc_hwpart_conf_mode mode)
866{
867 u8 part_attrs = 0;
868 u32 enh_size_mult;
869 u32 enh_start_addr;
870 u32 gp_size_mult[4];
871 u32 max_enh_size_mult;
872 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100873 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100874 int i, pidx, err;
875 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
876
877 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
878 return -EINVAL;
879
880 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
881 printf("eMMC >= 4.4 required for enhanced user data area\n");
882 return -EMEDIUMTYPE;
883 }
884
885 if (!(mmc->part_support & PART_SUPPORT)) {
886 printf("Card does not support partitioning\n");
887 return -EMEDIUMTYPE;
888 }
889
890 if (!mmc->hc_wp_grp_size) {
891 printf("Card does not define HC WP group size\n");
892 return -EMEDIUMTYPE;
893 }
894
895 /* check partition alignment and total enhanced size */
896 if (conf->user.enh_size) {
897 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
898 conf->user.enh_start % mmc->hc_wp_grp_size) {
899 printf("User data enhanced area not HC WP group "
900 "size aligned\n");
901 return -EINVAL;
902 }
903 part_attrs |= EXT_CSD_ENH_USR;
904 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
905 if (mmc->high_capacity) {
906 enh_start_addr = conf->user.enh_start;
907 } else {
908 enh_start_addr = (conf->user.enh_start << 9);
909 }
910 } else {
911 enh_size_mult = 0;
912 enh_start_addr = 0;
913 }
914 tot_enh_size_mult += enh_size_mult;
915
916 for (pidx = 0; pidx < 4; pidx++) {
917 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
918 printf("GP%i partition not HC WP group size "
919 "aligned\n", pidx+1);
920 return -EINVAL;
921 }
922 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
923 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
924 part_attrs |= EXT_CSD_ENH_GP(pidx);
925 tot_enh_size_mult += gp_size_mult[pidx];
926 }
927 }
928
929 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
930 printf("Card does not support enhanced attribute\n");
931 return -EMEDIUMTYPE;
932 }
933
934 err = mmc_send_ext_csd(mmc, ext_csd);
935 if (err)
936 return err;
937
938 max_enh_size_mult =
939 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
940 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
941 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
942 if (tot_enh_size_mult > max_enh_size_mult) {
943 printf("Total enhanced size exceeds maximum (%u > %u)\n",
944 tot_enh_size_mult, max_enh_size_mult);
945 return -EMEDIUMTYPE;
946 }
947
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100948 /* The default value of EXT_CSD_WR_REL_SET is device
949 * dependent, the values can only be changed if the
950 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
951 * changed only once and before partitioning is completed. */
952 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
953 if (conf->user.wr_rel_change) {
954 if (conf->user.wr_rel_set)
955 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
956 else
957 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
958 }
959 for (pidx = 0; pidx < 4; pidx++) {
960 if (conf->gp_part[pidx].wr_rel_change) {
961 if (conf->gp_part[pidx].wr_rel_set)
962 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
963 else
964 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
965 }
966 }
967
968 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
969 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
970 puts("Card does not support host controlled partition write "
971 "reliability settings\n");
972 return -EMEDIUMTYPE;
973 }
974
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100975 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
976 EXT_CSD_PARTITION_SETTING_COMPLETED) {
977 printf("Card already partitioned\n");
978 return -EPERM;
979 }
980
981 if (mode == MMC_HWPART_CONF_CHECK)
982 return 0;
983
984 /* Partitioning requires high-capacity size definitions */
985 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
986 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
987 EXT_CSD_ERASE_GROUP_DEF, 1);
988
989 if (err)
990 return err;
991
992 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
993
994 /* update erase group size to be high-capacity */
995 mmc->erase_grp_size =
996 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
997
998 }
999
1000 /* all OK, write the configuration */
1001 for (i = 0; i < 4; i++) {
1002 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1003 EXT_CSD_ENH_START_ADDR+i,
1004 (enh_start_addr >> (i*8)) & 0xFF);
1005 if (err)
1006 return err;
1007 }
1008 for (i = 0; i < 3; i++) {
1009 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1010 EXT_CSD_ENH_SIZE_MULT+i,
1011 (enh_size_mult >> (i*8)) & 0xFF);
1012 if (err)
1013 return err;
1014 }
1015 for (pidx = 0; pidx < 4; pidx++) {
1016 for (i = 0; i < 3; i++) {
1017 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1018 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
1019 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
1020 if (err)
1021 return err;
1022 }
1023 }
1024 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1025 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
1026 if (err)
1027 return err;
1028
1029 if (mode == MMC_HWPART_CONF_SET)
1030 return 0;
1031
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +01001032 /* The WR_REL_SET is a write-once register but shall be
1033 * written before setting PART_SETTING_COMPLETED. As it is
1034 * write-once we can only write it when completing the
1035 * partitioning. */
1036 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
1037 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1038 EXT_CSD_WR_REL_SET, wr_rel_set);
1039 if (err)
1040 return err;
1041 }
1042
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +01001043 /* Setting PART_SETTING_COMPLETED confirms the partition
1044 * configuration but it only becomes effective after power
1045 * cycle, so we do not adjust the partition related settings
1046 * in the mmc struct. */
1047
1048 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1049 EXT_CSD_PARTITION_SETTING,
1050 EXT_CSD_PARTITION_SETTING_COMPLETED);
1051 if (err)
1052 return err;
1053
1054 return 0;
1055}
1056
Simon Glasse7881d82017-07-29 11:35:31 -06001057#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +00001058int mmc_getcd(struct mmc *mmc)
1059{
1060 int cd;
1061
1062 cd = board_mmc_getcd(mmc);
1063
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001064 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001065 if (mmc->cfg->ops->getcd)
1066 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +00001067 else
1068 cd = 1;
1069 }
Thierry Reding48972d92012-01-02 01:15:37 +00001070
1071 return cd;
1072}
Simon Glass8ca51e52016-06-12 23:30:22 -06001073#endif
Thierry Reding48972d92012-01-02 01:15:37 +00001074
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001075static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -05001076{
1077 struct mmc_cmd cmd;
1078 struct mmc_data data;
1079
1080 /* Switch the frequency */
1081 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
1082 cmd.resp_type = MMC_RSP_R1;
1083 cmd.cmdarg = (mode << 31) | 0xffffff;
1084 cmd.cmdarg &= ~(0xf << (group * 4));
1085 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -05001086
1087 data.dest = (char *)resp;
1088 data.blocksize = 64;
1089 data.blocks = 1;
1090 data.flags = MMC_DATA_READ;
1091
1092 return mmc_send_cmd(mmc, &cmd, &data);
1093}
1094
1095
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001096static int sd_get_capabilities(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001097{
1098 int err;
1099 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +05301100 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
1101 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -05001102 struct mmc_data data;
1103 int timeout;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001104 u32 sd3_bus_mode;
Andy Fleming272cc702008-10-30 16:41:01 -05001105
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001106 mmc->card_caps = MMC_MODE_1BIT;
Andy Fleming272cc702008-10-30 16:41:01 -05001107
Thomas Choud52ebf12010-12-24 13:12:21 +00001108 if (mmc_host_is_spi(mmc))
1109 return 0;
1110
Andy Fleming272cc702008-10-30 16:41:01 -05001111 /* Read the SCR to find out if this card supports higher speeds */
1112 cmd.cmdidx = MMC_CMD_APP_CMD;
1113 cmd.resp_type = MMC_RSP_R1;
1114 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001115
1116 err = mmc_send_cmd(mmc, &cmd, NULL);
1117
1118 if (err)
1119 return err;
1120
1121 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
1122 cmd.resp_type = MMC_RSP_R1;
1123 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001124
1125 timeout = 3;
1126
1127retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +00001128 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -05001129 data.blocksize = 8;
1130 data.blocks = 1;
1131 data.flags = MMC_DATA_READ;
1132
1133 err = mmc_send_cmd(mmc, &cmd, &data);
1134
1135 if (err) {
1136 if (timeout--)
1137 goto retry_scr;
1138
1139 return err;
1140 }
1141
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001142 mmc->scr[0] = __be32_to_cpu(scr[0]);
1143 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -05001144
1145 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -07001146 case 0:
1147 mmc->version = SD_VERSION_1_0;
1148 break;
1149 case 1:
1150 mmc->version = SD_VERSION_1_10;
1151 break;
1152 case 2:
1153 mmc->version = SD_VERSION_2;
1154 if ((mmc->scr[0] >> 15) & 0x1)
1155 mmc->version = SD_VERSION_3;
1156 break;
1157 default:
1158 mmc->version = SD_VERSION_1_0;
1159 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001160 }
1161
Alagu Sankarb44c7082010-05-12 15:08:24 +05301162 if (mmc->scr[0] & SD_DATA_4BIT)
1163 mmc->card_caps |= MMC_MODE_4BIT;
1164
Andy Fleming272cc702008-10-30 16:41:01 -05001165 /* Version 1.0 doesn't support switching */
1166 if (mmc->version == SD_VERSION_1_0)
1167 return 0;
1168
1169 timeout = 4;
1170 while (timeout--) {
1171 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +00001172 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001173
1174 if (err)
1175 return err;
1176
1177 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001178 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -05001179 break;
1180 }
1181
Andy Fleming272cc702008-10-30 16:41:01 -05001182 /* If high-speed isn't supported, we return */
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001183 if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
1184 mmc->card_caps |= MMC_CAP(SD_HS);
Andy Fleming272cc702008-10-30 16:41:01 -05001185
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001186 /* Version before 3.0 don't support UHS modes */
1187 if (mmc->version < SD_VERSION_3)
1188 return 0;
1189
1190 sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
1191 if (sd3_bus_mode & SD_MODE_UHS_SDR104)
1192 mmc->card_caps |= MMC_CAP(UHS_SDR104);
1193 if (sd3_bus_mode & SD_MODE_UHS_SDR50)
1194 mmc->card_caps |= MMC_CAP(UHS_SDR50);
1195 if (sd3_bus_mode & SD_MODE_UHS_SDR25)
1196 mmc->card_caps |= MMC_CAP(UHS_SDR25);
1197 if (sd3_bus_mode & SD_MODE_UHS_SDR12)
1198 mmc->card_caps |= MMC_CAP(UHS_SDR12);
1199 if (sd3_bus_mode & SD_MODE_UHS_DDR50)
1200 mmc->card_caps |= MMC_CAP(UHS_DDR50);
1201
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001202 return 0;
1203}
1204
1205static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
1206{
1207 int err;
1208
1209 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001210 int speed;
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001211
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001212 switch (mode) {
1213 case SD_LEGACY:
1214 case UHS_SDR12:
1215 speed = UHS_SDR12_BUS_SPEED;
1216 break;
1217 case SD_HS:
1218 case UHS_SDR25:
1219 speed = UHS_SDR25_BUS_SPEED;
1220 break;
1221 case UHS_SDR50:
1222 speed = UHS_SDR50_BUS_SPEED;
1223 break;
1224 case UHS_DDR50:
1225 speed = UHS_DDR50_BUS_SPEED;
1226 break;
1227 case UHS_SDR104:
1228 speed = UHS_SDR104_BUS_SPEED;
1229 break;
1230 default:
1231 return -EINVAL;
1232 }
1233
1234 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001235 if (err)
1236 return err;
1237
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001238 if ((__be32_to_cpu(switch_status[4]) >> 24) != speed)
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001239 return -ENOTSUPP;
1240
1241 return 0;
1242}
1243
1244int sd_select_bus_width(struct mmc *mmc, int w)
1245{
1246 int err;
1247 struct mmc_cmd cmd;
1248
1249 if ((w != 4) && (w != 1))
1250 return -EINVAL;
1251
1252 cmd.cmdidx = MMC_CMD_APP_CMD;
1253 cmd.resp_type = MMC_RSP_R1;
1254 cmd.cmdarg = mmc->rca << 16;
1255
1256 err = mmc_send_cmd(mmc, &cmd, NULL);
1257 if (err)
1258 return err;
1259
1260 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1261 cmd.resp_type = MMC_RSP_R1;
1262 if (w == 4)
1263 cmd.cmdarg = 2;
1264 else if (w == 1)
1265 cmd.cmdarg = 0;
1266 err = mmc_send_cmd(mmc, &cmd, NULL);
1267 if (err)
1268 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001269
1270 return 0;
1271}
1272
Peng Fan3697e592016-09-01 11:13:38 +08001273static int sd_read_ssr(struct mmc *mmc)
1274{
1275 int err, i;
1276 struct mmc_cmd cmd;
1277 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1278 struct mmc_data data;
1279 int timeout = 3;
1280 unsigned int au, eo, et, es;
1281
1282 cmd.cmdidx = MMC_CMD_APP_CMD;
1283 cmd.resp_type = MMC_RSP_R1;
1284 cmd.cmdarg = mmc->rca << 16;
1285
1286 err = mmc_send_cmd(mmc, &cmd, NULL);
1287 if (err)
1288 return err;
1289
1290 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1291 cmd.resp_type = MMC_RSP_R1;
1292 cmd.cmdarg = 0;
1293
1294retry_ssr:
1295 data.dest = (char *)ssr;
1296 data.blocksize = 64;
1297 data.blocks = 1;
1298 data.flags = MMC_DATA_READ;
1299
1300 err = mmc_send_cmd(mmc, &cmd, &data);
1301 if (err) {
1302 if (timeout--)
1303 goto retry_ssr;
1304
1305 return err;
1306 }
1307
1308 for (i = 0; i < 16; i++)
1309 ssr[i] = be32_to_cpu(ssr[i]);
1310
1311 au = (ssr[2] >> 12) & 0xF;
1312 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1313 mmc->ssr.au = sd_au_size[au];
1314 es = (ssr[3] >> 24) & 0xFF;
1315 es |= (ssr[2] & 0xFF) << 8;
1316 et = (ssr[3] >> 18) & 0x3F;
1317 if (es && et) {
1318 eo = (ssr[3] >> 16) & 0x3;
1319 mmc->ssr.erase_timeout = (et * 1000) / es;
1320 mmc->ssr.erase_offset = eo * 1000;
1321 }
1322 } else {
1323 debug("Invalid Allocation Unit Size.\n");
1324 }
1325
1326 return 0;
1327}
1328
Andy Fleming272cc702008-10-30 16:41:01 -05001329/* frequency bases */
1330/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001331static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001332 10000,
1333 100000,
1334 1000000,
1335 10000000,
1336};
1337
1338/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1339 * to platforms without floating point.
1340 */
Simon Glass61fe0762016-05-14 14:02:57 -06001341static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001342 0, /* reserved */
1343 10,
1344 12,
1345 13,
1346 15,
1347 20,
1348 25,
1349 30,
1350 35,
1351 40,
1352 45,
1353 50,
1354 55,
1355 60,
1356 70,
1357 80,
1358};
1359
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001360static inline int bus_width(uint cap)
1361{
1362 if (cap == MMC_MODE_8BIT)
1363 return 8;
1364 if (cap == MMC_MODE_4BIT)
1365 return 4;
1366 if (cap == MMC_MODE_1BIT)
1367 return 1;
1368 printf("invalid bus witdh capability 0x%x\n", cap);
1369 return 0;
1370}
1371
Simon Glasse7881d82017-07-29 11:35:31 -06001372#if !CONFIG_IS_ENABLED(DM_MMC)
Kishon Vijay Abraham Iec841202017-09-21 16:30:05 +02001373static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
1374{
1375 return -ENOTSUPP;
1376}
1377
Jean-Jacques Hiblot318a7a52017-09-21 16:30:01 +02001378static void mmc_send_init_stream(struct mmc *mmc)
1379{
1380}
1381
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001382static int mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001383{
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001384 int ret = 0;
1385
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001386 if (mmc->cfg->ops->set_ios)
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001387 ret = mmc->cfg->ops->set_ios(mmc);
1388
1389 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001390}
Simon Glass8ca51e52016-06-12 23:30:22 -06001391#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001392
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001393int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
Andy Fleming272cc702008-10-30 16:41:01 -05001394{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001395 if (clock > mmc->cfg->f_max)
1396 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001397
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001398 if (clock < mmc->cfg->f_min)
1399 clock = mmc->cfg->f_min;
Andy Fleming272cc702008-10-30 16:41:01 -05001400
1401 mmc->clock = clock;
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001402 mmc->clk_disable = disable;
Andy Fleming272cc702008-10-30 16:41:01 -05001403
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001404 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001405}
1406
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001407static int mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001408{
1409 mmc->bus_width = width;
1410
Kishon Vijay Abraham I2a4d2122017-09-21 16:29:59 +02001411 return mmc_set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001412}
1413
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001414#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1415/*
1416 * helper function to display the capabilities in a human
1417 * friendly manner. The capabilities include bus width and
1418 * supported modes.
1419 */
1420void mmc_dump_capabilities(const char *text, uint caps)
1421{
1422 enum bus_mode mode;
1423
1424 printf("%s: widths [", text);
1425 if (caps & MMC_MODE_8BIT)
1426 printf("8, ");
1427 if (caps & MMC_MODE_4BIT)
1428 printf("4, ");
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001429 if (caps & MMC_MODE_1BIT)
1430 printf("1, ");
1431 printf("\b\b] modes [");
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001432 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1433 if (MMC_CAP(mode) & caps)
1434 printf("%s, ", mmc_mode_name(mode));
1435 printf("\b\b]\n");
1436}
1437#endif
1438
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001439struct mode_width_tuning {
1440 enum bus_mode mode;
1441 uint widths;
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001442 uint tuning;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001443};
1444
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001445int mmc_voltage_to_mv(enum mmc_voltage voltage)
1446{
1447 switch (voltage) {
1448 case MMC_SIGNAL_VOLTAGE_000: return 0;
1449 case MMC_SIGNAL_VOLTAGE_330: return 3300;
1450 case MMC_SIGNAL_VOLTAGE_180: return 1800;
1451 case MMC_SIGNAL_VOLTAGE_120: return 1200;
1452 }
1453 return -EINVAL;
1454}
1455
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001456static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
1457{
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001458 int err;
1459
1460 if (mmc->signal_voltage == signal_voltage)
1461 return 0;
1462
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001463 mmc->signal_voltage = signal_voltage;
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001464 err = mmc_set_ios(mmc);
1465 if (err)
1466 debug("unable to set voltage (err %d)\n", err);
1467
1468 return err;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02001469}
1470
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001471static const struct mode_width_tuning sd_modes_by_pref[] = {
1472 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001473 .mode = UHS_SDR104,
1474 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1475 .tuning = MMC_CMD_SEND_TUNING_BLOCK
1476 },
1477 {
1478 .mode = UHS_SDR50,
1479 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1480 },
1481 {
1482 .mode = UHS_DDR50,
1483 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1484 },
1485 {
1486 .mode = UHS_SDR25,
1487 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1488 },
1489 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001490 .mode = SD_HS,
1491 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1492 },
1493 {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001494 .mode = UHS_SDR12,
1495 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1496 },
1497 {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001498 .mode = SD_LEGACY,
1499 .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
1500 }
1501};
1502
1503#define for_each_sd_mode_by_pref(caps, mwt) \
1504 for (mwt = sd_modes_by_pref;\
1505 mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
1506 mwt++) \
1507 if (caps & MMC_CAP(mwt->mode))
1508
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001509static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001510{
1511 int err;
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001512 uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
1513 const struct mode_width_tuning *mwt;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001514 bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
1515 uint caps;
1516
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001517
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001518 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001519 caps = card_caps & (mmc->host_caps | MMC_MODE_1BIT);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001520
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001521 if (!uhs_en)
1522 caps &= ~UHS_CAPS;
1523
1524 for_each_sd_mode_by_pref(caps, mwt) {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001525 uint *w;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001526
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001527 for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001528 if (*w & caps & mwt->widths) {
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001529 debug("trying mode %s width %d (at %d MHz)\n",
1530 mmc_mode_name(mwt->mode),
1531 bus_width(*w),
1532 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001533
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001534 /* configure the bus width (card + host) */
1535 err = sd_select_bus_width(mmc, bus_width(*w));
1536 if (err)
1537 goto error;
1538 mmc_set_bus_width(mmc, bus_width(*w));
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001539
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001540 /* configure the bus mode (card) */
1541 err = sd_set_card_speed(mmc, mwt->mode);
1542 if (err)
1543 goto error;
1544
1545 /* configure the bus mode (host) */
1546 mmc_select_mode(mmc, mwt->mode);
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
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02001549 /* execute tuning if needed */
1550 if (mwt->tuning && !mmc_host_is_spi(mmc)) {
1551 err = mmc_execute_tuning(mmc,
1552 mwt->tuning);
1553 if (err) {
1554 debug("tuning failed\n");
1555 goto error;
1556 }
1557 }
1558
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001559 err = sd_read_ssr(mmc);
1560 if (!err)
1561 return 0;
1562
1563 printf("bad ssr\n");
1564
1565error:
1566 /* revert to a safer bus speed */
1567 mmc_select_mode(mmc, SD_LEGACY);
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001568 mmc_set_clock(mmc, mmc->tran_speed, false);
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001569 }
1570 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001571 }
1572
Jean-Jacques Hiblotd0c221f2017-09-21 16:29:57 +02001573 printf("unable to select a mode\n");
1574 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001575}
1576
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001577/*
1578 * read the compare the part of ext csd that is constant.
1579 * This can be used to check that the transfer is working
1580 * as expected.
1581 */
1582static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1583{
1584 int err;
1585 const u8 *ext_csd = mmc->ext_csd;
1586 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1587
1588 err = mmc_send_ext_csd(mmc, test_csd);
1589 if (err)
1590 return err;
1591
1592 /* Only compare read only fields */
1593 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1594 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1595 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1596 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1597 ext_csd[EXT_CSD_REV]
1598 == test_csd[EXT_CSD_REV] &&
1599 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1600 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1601 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1602 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1603 return 0;
1604
1605 return -EBADMSG;
1606}
1607
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001608static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
1609 uint32_t allowed_mask)
1610{
1611 u32 card_mask = 0;
1612
1613 switch (mode) {
1614 case MMC_HS_200:
1615 if (mmc->cardtype & EXT_CSD_CARD_TYPE_HS200_1_8V)
1616 card_mask |= MMC_SIGNAL_VOLTAGE_180;
1617 if (mmc->cardtype & EXT_CSD_CARD_TYPE_HS200_1_2V)
1618 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1619 break;
1620 case MMC_DDR_52:
1621 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
1622 card_mask |= MMC_SIGNAL_VOLTAGE_330 |
1623 MMC_SIGNAL_VOLTAGE_180;
1624 if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
1625 card_mask |= MMC_SIGNAL_VOLTAGE_120;
1626 break;
1627 default:
1628 card_mask |= MMC_SIGNAL_VOLTAGE_330;
1629 break;
1630 }
1631
1632 while (card_mask & allowed_mask) {
1633 enum mmc_voltage best_match;
1634
1635 best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
1636 if (!mmc_set_signal_voltage(mmc, best_match))
1637 return 0;
1638
1639 allowed_mask &= ~best_match;
1640 }
1641
1642 return -ENOTSUPP;
1643}
1644
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001645static const struct mode_width_tuning mmc_modes_by_pref[] = {
1646 {
1647 .mode = MMC_HS_200,
1648 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001649 .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001650 },
1651 {
1652 .mode = MMC_DDR_52,
1653 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
1654 },
1655 {
1656 .mode = MMC_HS_52,
1657 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1658 },
1659 {
1660 .mode = MMC_HS,
1661 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1662 },
1663 {
1664 .mode = MMC_LEGACY,
1665 .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
1666 }
1667};
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001668
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001669#define for_each_mmc_mode_by_pref(caps, mwt) \
1670 for (mwt = mmc_modes_by_pref;\
1671 mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
1672 mwt++) \
1673 if (caps & MMC_CAP(mwt->mode))
1674
1675static const struct ext_csd_bus_width {
1676 uint cap;
1677 bool is_ddr;
1678 uint ext_csd_bits;
1679} ext_csd_bus_width[] = {
1680 {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
1681 {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
1682 {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
1683 {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
1684 {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
1685};
1686
1687#define for_each_supported_width(caps, ddr, ecbv) \
1688 for (ecbv = ext_csd_bus_width;\
1689 ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
1690 ecbv++) \
1691 if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
1692
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001693static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001694{
1695 int err;
1696 const struct mode_width_tuning *mwt;
1697 const struct ext_csd_bus_width *ecbw;
1698
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001699 /* Restrict card's capabilities by what the host can do */
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001700 card_caps &= (mmc->host_caps | MMC_MODE_1BIT);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001701
1702 /* Only version 4 of MMC supports wider bus widths */
1703 if (mmc->version < MMC_VERSION_4)
1704 return 0;
1705
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001706 if (!mmc->ext_csd) {
1707 debug("No ext_csd found!\n"); /* this should enver happen */
1708 return -ENOTSUPP;
1709 }
1710
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02001711 mmc_set_clock(mmc, mmc->legacy_speed, false);
1712
1713 for_each_mmc_mode_by_pref(card_caps, mwt) {
1714 for_each_supported_width(card_caps & mwt->widths,
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001715 mmc_is_mode_ddr(mwt->mode), ecbw) {
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001716 enum mmc_voltage old_voltage;
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001717 debug("trying mode %s width %d (at %d MHz)\n",
1718 mmc_mode_name(mwt->mode),
1719 bus_width(ecbw->cap),
1720 mmc_mode2freq(mmc, mwt->mode) / 1000000);
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001721 old_voltage = mmc->signal_voltage;
1722 err = mmc_set_lowest_voltage(mmc, mwt->mode,
1723 MMC_ALL_SIGNAL_VOLTAGE);
1724 if (err)
1725 continue;
1726
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001727 /* configure the bus width (card + host) */
1728 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1729 EXT_CSD_BUS_WIDTH,
1730 ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
1731 if (err)
1732 goto error;
1733 mmc_set_bus_width(mmc, bus_width(ecbw->cap));
1734
1735 /* configure the bus speed (card) */
1736 err = mmc_set_card_speed(mmc, mwt->mode);
1737 if (err)
1738 goto error;
1739
1740 /*
1741 * configure the bus width AND the ddr mode (card)
1742 * The host side will be taken care of in the next step
1743 */
1744 if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
1745 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1746 EXT_CSD_BUS_WIDTH,
1747 ecbw->ext_csd_bits);
1748 if (err)
1749 goto error;
1750 }
1751
1752 /* configure the bus mode (host) */
1753 mmc_select_mode(mmc, mwt->mode);
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02001754 mmc_set_clock(mmc, mmc->tran_speed, false);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001755
Kishon Vijay Abraham I634d4842017-09-21 16:30:06 +02001756 /* execute tuning if needed */
1757 if (mwt->tuning) {
1758 err = mmc_execute_tuning(mmc, mwt->tuning);
1759 if (err) {
1760 debug("tuning failed\n");
1761 goto error;
1762 }
1763 }
1764
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001765 /* do a transfer to check the configuration */
1766 err = mmc_read_and_compare_ext_csd(mmc);
1767 if (!err)
1768 return 0;
1769error:
Jean-Jacques Hiblotbc1e3272017-09-21 16:30:11 +02001770 mmc_set_signal_voltage(mmc, old_voltage);
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001771 /* if an error occured, revert to a safer bus mode */
1772 mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1773 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
1774 mmc_select_mode(mmc, MMC_LEGACY);
1775 mmc_set_bus_width(mmc, 1);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001776 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001777 }
1778
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001779 printf("unable to select a mode\n");
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001780
Jean-Jacques Hiblot3862b852017-09-21 16:29:58 +02001781 return -ENOTSUPP;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001782}
1783
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001784static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001785{
1786 int err, i;
1787 u64 capacity;
1788 bool has_parts = false;
1789 bool part_completed;
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001790 u8 *ext_csd;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001791
1792 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
1793 return 0;
1794
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001795 ext_csd = malloc_cache_aligned(MMC_MAX_BLOCK_LEN);
1796 if (!ext_csd)
1797 return -ENOMEM;
1798
1799 mmc->ext_csd = ext_csd;
1800
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001801 /* check ext_csd version and capacity */
1802 err = mmc_send_ext_csd(mmc, ext_csd);
1803 if (err)
1804 return err;
1805 if (ext_csd[EXT_CSD_REV] >= 2) {
1806 /*
1807 * According to the JEDEC Standard, the value of
1808 * ext_csd's capacity is valid if the value is more
1809 * than 2GB
1810 */
1811 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1812 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1813 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1814 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1815 capacity *= MMC_MAX_BLOCK_LEN;
1816 if ((capacity >> 20) > 2 * 1024)
1817 mmc->capacity_user = capacity;
1818 }
1819
1820 switch (ext_csd[EXT_CSD_REV]) {
1821 case 1:
1822 mmc->version = MMC_VERSION_4_1;
1823 break;
1824 case 2:
1825 mmc->version = MMC_VERSION_4_2;
1826 break;
1827 case 3:
1828 mmc->version = MMC_VERSION_4_3;
1829 break;
1830 case 5:
1831 mmc->version = MMC_VERSION_4_41;
1832 break;
1833 case 6:
1834 mmc->version = MMC_VERSION_4_5;
1835 break;
1836 case 7:
1837 mmc->version = MMC_VERSION_5_0;
1838 break;
1839 case 8:
1840 mmc->version = MMC_VERSION_5_1;
1841 break;
1842 }
1843
1844 /* The partition data may be non-zero but it is only
1845 * effective if PARTITION_SETTING_COMPLETED is set in
1846 * EXT_CSD, so ignore any data if this bit is not set,
1847 * except for enabling the high-capacity group size
1848 * definition (see below).
1849 */
1850 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
1851 EXT_CSD_PARTITION_SETTING_COMPLETED);
1852
1853 /* store the partition info of emmc */
1854 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
1855 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1856 ext_csd[EXT_CSD_BOOT_MULT])
1857 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1858 if (part_completed &&
1859 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
1860 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
1861
1862 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1863
1864 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1865
1866 for (i = 0; i < 4; i++) {
1867 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1868 uint mult = (ext_csd[idx + 2] << 16) +
1869 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1870 if (mult)
1871 has_parts = true;
1872 if (!part_completed)
1873 continue;
1874 mmc->capacity_gp[i] = mult;
1875 mmc->capacity_gp[i] *=
1876 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1877 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1878 mmc->capacity_gp[i] <<= 19;
1879 }
1880
1881 if (part_completed) {
1882 mmc->enh_user_size =
1883 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
1884 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
1885 ext_csd[EXT_CSD_ENH_SIZE_MULT];
1886 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1887 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1888 mmc->enh_user_size <<= 19;
1889 mmc->enh_user_start =
1890 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
1891 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
1892 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
1893 ext_csd[EXT_CSD_ENH_START_ADDR];
1894 if (mmc->high_capacity)
1895 mmc->enh_user_start <<= 9;
1896 }
1897
1898 /*
1899 * Host needs to enable ERASE_GRP_DEF bit if device is
1900 * partitioned. This bit will be lost every time after a reset
1901 * or power off. This will affect erase size.
1902 */
1903 if (part_completed)
1904 has_parts = true;
1905 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
1906 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
1907 has_parts = true;
1908 if (has_parts) {
1909 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1910 EXT_CSD_ERASE_GROUP_DEF, 1);
1911
1912 if (err)
1913 return err;
1914
1915 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1916 }
1917
1918 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
1919 /* Read out group size from ext_csd */
1920 mmc->erase_grp_size =
1921 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1922 /*
1923 * if high capacity and partition setting completed
1924 * SEC_COUNT is valid even if it is smaller than 2 GiB
1925 * JEDEC Standard JESD84-B45, 6.2.4
1926 */
1927 if (mmc->high_capacity && part_completed) {
1928 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1929 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1930 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1931 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1932 capacity *= MMC_MAX_BLOCK_LEN;
1933 mmc->capacity_user = capacity;
1934 }
1935 } else {
1936 /* Calculate the group size from the csd value. */
1937 int erase_gsz, erase_gmul;
1938
1939 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1940 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1941 mmc->erase_grp_size = (erase_gsz + 1)
1942 * (erase_gmul + 1);
1943 }
1944
1945 mmc->hc_wp_grp_size = 1024
1946 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1947 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1948
1949 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1950
1951 return 0;
1952}
1953
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001954static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001955{
Stephen Warrenf866a462013-06-11 15:14:01 -06001956 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05001957 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001958 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05001959 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06001960 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05001961
Thomas Choud52ebf12010-12-24 13:12:21 +00001962#ifdef CONFIG_MMC_SPI_CRC_ON
1963 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1964 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1965 cmd.resp_type = MMC_RSP_R1;
1966 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001967 err = mmc_send_cmd(mmc, &cmd, NULL);
Thomas Choud52ebf12010-12-24 13:12:21 +00001968 if (err)
1969 return err;
1970 }
1971#endif
1972
Andy Fleming272cc702008-10-30 16:41:01 -05001973 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001974 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1975 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05001976 cmd.resp_type = MMC_RSP_R2;
1977 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001978
1979 err = mmc_send_cmd(mmc, &cmd, NULL);
1980
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02001981#ifdef CONFIG_MMC_QUIRKS
1982 if (err && (mmc->quirks & MMC_QUIRK_RETRY_SEND_CID)) {
1983 int retries = 4;
1984 /*
1985 * It has been seen that SEND_CID may fail on the first
1986 * attempt, let's try a few more time
1987 */
1988 do {
1989 err = mmc_send_cmd(mmc, &cmd, NULL);
1990 if (!err)
1991 break;
1992 } while (retries--);
1993 }
1994#endif
1995
Andy Fleming272cc702008-10-30 16:41:01 -05001996 if (err)
1997 return err;
1998
1999 memcpy(mmc->cid, cmd.response, 16);
2000
2001 /*
2002 * For MMC cards, set the Relative Address.
2003 * For SD cards, get the Relatvie Address.
2004 * This also puts the cards into Standby State
2005 */
Thomas Choud52ebf12010-12-24 13:12:21 +00002006 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2007 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
2008 cmd.cmdarg = mmc->rca << 16;
2009 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05002010
Thomas Choud52ebf12010-12-24 13:12:21 +00002011 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002012
Thomas Choud52ebf12010-12-24 13:12:21 +00002013 if (err)
2014 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002015
Thomas Choud52ebf12010-12-24 13:12:21 +00002016 if (IS_SD(mmc))
2017 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
2018 }
Andy Fleming272cc702008-10-30 16:41:01 -05002019
2020 /* Get the Card-Specific Data */
2021 cmd.cmdidx = MMC_CMD_SEND_CSD;
2022 cmd.resp_type = MMC_RSP_R2;
2023 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05002024
2025 err = mmc_send_cmd(mmc, &cmd, NULL);
2026
2027 if (err)
2028 return err;
2029
Rabin Vincent998be3d2009-04-05 13:30:56 +05302030 mmc->csd[0] = cmd.response[0];
2031 mmc->csd[1] = cmd.response[1];
2032 mmc->csd[2] = cmd.response[2];
2033 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05002034
2035 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302036 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05002037
2038 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07002039 case 0:
2040 mmc->version = MMC_VERSION_1_2;
2041 break;
2042 case 1:
2043 mmc->version = MMC_VERSION_1_4;
2044 break;
2045 case 2:
2046 mmc->version = MMC_VERSION_2_2;
2047 break;
2048 case 3:
2049 mmc->version = MMC_VERSION_3;
2050 break;
2051 case 4:
2052 mmc->version = MMC_VERSION_4;
2053 break;
2054 default:
2055 mmc->version = MMC_VERSION_1_2;
2056 break;
Andy Fleming272cc702008-10-30 16:41:01 -05002057 }
2058 }
2059
2060 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05302061 freq = fbase[(cmd.response[0] & 0x7)];
2062 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05002063
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002064 mmc->legacy_speed = freq * mult;
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02002065 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05002066
Markus Niebelab711882013-12-16 13:40:46 +01002067 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05302068 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05002069
2070 if (IS_SD(mmc))
2071 mmc->write_bl_len = mmc->read_bl_len;
2072 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05302073 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05002074
2075 if (mmc->high_capacity) {
2076 csize = (mmc->csd[1] & 0x3f) << 16
2077 | (mmc->csd[2] & 0xffff0000) >> 16;
2078 cmult = 8;
2079 } else {
2080 csize = (mmc->csd[1] & 0x3ff) << 2
2081 | (mmc->csd[2] & 0xc0000000) >> 30;
2082 cmult = (mmc->csd[2] & 0x00038000) >> 15;
2083 }
2084
Stephen Warrenf866a462013-06-11 15:14:01 -06002085 mmc->capacity_user = (csize + 1) << (cmult + 2);
2086 mmc->capacity_user *= mmc->read_bl_len;
2087 mmc->capacity_boot = 0;
2088 mmc->capacity_rpmb = 0;
2089 for (i = 0; i < 4; i++)
2090 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05002091
Simon Glass8bfa1952013-04-03 08:54:30 +00002092 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
2093 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05002094
Simon Glass8bfa1952013-04-03 08:54:30 +00002095 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
2096 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05002097
Markus Niebelab711882013-12-16 13:40:46 +01002098 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
2099 cmd.cmdidx = MMC_CMD_SET_DSR;
2100 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
2101 cmd.resp_type = MMC_RSP_NONE;
2102 if (mmc_send_cmd(mmc, &cmd, NULL))
2103 printf("MMC: SET_DSR failed\n");
2104 }
2105
Andy Fleming272cc702008-10-30 16:41:01 -05002106 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00002107 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
2108 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00002109 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00002110 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00002111 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05002112
Thomas Choud52ebf12010-12-24 13:12:21 +00002113 if (err)
2114 return err;
2115 }
Andy Fleming272cc702008-10-30 16:41:01 -05002116
Lei Wene6f99a52011-06-22 17:03:31 +00002117 /*
2118 * For SD, its erase group is always one sector
2119 */
2120 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00002121 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00002122
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02002123 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02002124 if (err)
2125 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05302126
Simon Glassc40fdca2016-05-01 13:52:35 -06002127 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06002128 if (err)
2129 return err;
2130
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002131 if (IS_SD(mmc)) {
2132 err = sd_get_capabilities(mmc);
2133 if (err)
2134 return err;
2135 err = sd_select_mode_and_width(mmc, mmc->card_caps);
2136 } else {
2137 err = mmc_get_capabilities(mmc);
2138 if (err)
2139 return err;
2140 mmc_select_mode_and_width(mmc, mmc->card_caps);
2141 }
Andy Fleming272cc702008-10-30 16:41:01 -05002142
2143 if (err)
2144 return err;
2145
Jean-Jacques Hiblot01298da2017-09-21 16:30:09 +02002146 mmc->best_mode = mmc->selected_mode;
Jaehoon Chungad5fd922012-03-26 21:16:03 +00002147
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06002148 /* Fix the block length for DDR mode */
2149 if (mmc->ddr_mode) {
2150 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
2151 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
2152 }
2153
Andy Fleming272cc702008-10-30 16:41:01 -05002154 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06002155 bdesc = mmc_get_blk_desc(mmc);
2156 bdesc->lun = 0;
2157 bdesc->hwpart = 0;
2158 bdesc->type = 0;
2159 bdesc->blksz = mmc->read_bl_len;
2160 bdesc->log2blksz = LOG2(bdesc->blksz);
2161 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01002162#if !defined(CONFIG_SPL_BUILD) || \
2163 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
2164 !defined(CONFIG_USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06002165 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00002166 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
2167 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002168 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002169 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
2170 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
2171 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06002172 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00002173 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01002174#else
Simon Glassc40fdca2016-05-01 13:52:35 -06002175 bdesc->vendor[0] = 0;
2176 bdesc->product[0] = 0;
2177 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01002178#endif
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00002179#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Simon Glassc40fdca2016-05-01 13:52:35 -06002180 part_init(bdesc);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00002181#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002182
2183 return 0;
2184}
2185
Kim Phillipsfdbb8732012-10-29 13:34:43 +00002186static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002187{
2188 struct mmc_cmd cmd;
2189 int err;
2190
2191 cmd.cmdidx = SD_CMD_SEND_IF_COND;
2192 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002193 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05002194 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05002195
2196 err = mmc_send_cmd(mmc, &cmd, NULL);
2197
2198 if (err)
2199 return err;
2200
Rabin Vincent998be3d2009-04-05 13:30:56 +05302201 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002202 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002203 else
2204 mmc->version = SD_VERSION_2;
2205
2206 return 0;
2207}
2208
Simon Glassc4d660d2017-07-04 13:31:19 -06002209#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002210/* board-specific MMC power initializations. */
2211__weak void board_mmc_power_init(void)
2212{
2213}
Simon Glass05cbeb72017-04-22 19:10:56 -06002214#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002215
Peng Fan2051aef2016-10-11 15:08:43 +08002216static int mmc_power_init(struct mmc *mmc)
2217{
Simon Glassc4d660d2017-07-04 13:31:19 -06002218#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002219#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08002220 int ret;
2221
2222 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002223 &mmc->vmmc_supply);
2224 if (ret)
Jaehoon Chung288db7c2016-10-24 15:22:22 +09002225 debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002226
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02002227 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
2228 &mmc->vqmmc_supply);
2229 if (ret)
2230 debug("%s: No vqmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08002231#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06002232#else /* !CONFIG_DM_MMC */
2233 /*
2234 * Driver model should use a regulator, as above, rather than calling
2235 * out to board code.
2236 */
2237 board_mmc_power_init();
2238#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002239 return 0;
2240}
2241
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002242/*
2243 * put the host in the initial state:
2244 * - turn on Vdd (card power supply)
2245 * - configure the bus width and clock to minimal values
2246 */
2247static void mmc_set_initial_state(struct mmc *mmc)
2248{
2249 int err;
2250
2251 /* First try to set 3.3V. If it fails set to 1.8V */
2252 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
2253 if (err != 0)
2254 err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
2255 if (err != 0)
2256 printf("mmc: failed to set signal voltage\n");
2257
2258 mmc_select_mode(mmc, MMC_LEGACY);
2259 mmc_set_bus_width(mmc, 1);
Kishon Vijay Abraham I35f67822017-09-21 16:30:03 +02002260 mmc_set_clock(mmc, 0, false);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002261}
2262
2263static int mmc_power_on(struct mmc *mmc)
2264{
2265#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2266 if (mmc->vmmc_supply) {
2267 int ret = regulator_set_enable(mmc->vmmc_supply, true);
2268
2269 if (ret) {
2270 puts("Error enabling VMMC supply\n");
2271 return ret;
2272 }
2273 }
2274#endif
2275 return 0;
2276}
2277
2278static int mmc_power_off(struct mmc *mmc)
2279{
Kishon Vijay Abraham I2e7410d2017-09-21 16:30:04 +02002280 mmc_set_clock(mmc, 1, true);
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002281#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
2282 if (mmc->vmmc_supply) {
2283 int ret = regulator_set_enable(mmc->vmmc_supply, false);
2284
2285 if (ret) {
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002286 debug("Error disabling VMMC supply\n");
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002287 return ret;
2288 }
2289 }
2290#endif
2291 return 0;
2292}
2293
2294static int mmc_power_cycle(struct mmc *mmc)
2295{
2296 int ret;
2297
2298 ret = mmc_power_off(mmc);
2299 if (ret)
2300 return ret;
2301 /*
2302 * SD spec recommends at least 1ms of delay. Let's wait for 2ms
2303 * to be on the safer side.
2304 */
2305 udelay(2000);
2306 return mmc_power_on(mmc);
2307}
2308
Che-Liang Chioue9550442012-11-28 15:21:13 +00002309int mmc_start_init(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05002310{
Simon Glass8ca51e52016-06-12 23:30:22 -06002311 bool no_card;
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002312 bool uhs_en = supports_uhs(mmc->cfg->host_caps);
Macpaul Linafd59322011-11-14 23:35:39 +00002313 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05002314
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002315 mmc->host_caps = mmc->cfg->host_caps;
2316
Pantelis Antoniouab769f22014-02-26 19:28:45 +02002317 /* we pretend there's no card when init is NULL */
Simon Glass8ca51e52016-06-12 23:30:22 -06002318 no_card = mmc_getcd(mmc) == 0;
Simon Glasse7881d82017-07-29 11:35:31 -06002319#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06002320 no_card = no_card || (mmc->cfg->ops->init == NULL);
2321#endif
2322 if (no_card) {
Thierry Reding48972d92012-01-02 01:15:37 +00002323 mmc->has_init = 0;
Paul Burton56196822013-09-04 16:12:25 +01002324#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Thierry Reding48972d92012-01-02 01:15:37 +00002325 printf("MMC: no card present\n");
Paul Burton56196822013-09-04 16:12:25 +01002326#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002327 return -ENOMEDIUM;
Thierry Reding48972d92012-01-02 01:15:37 +00002328 }
2329
Lei Wenbc897b12011-05-02 16:26:26 +00002330 if (mmc->has_init)
2331 return 0;
2332
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08002333#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
2334 mmc_adapter_card_type_ident();
2335#endif
Peng Fan2051aef2016-10-11 15:08:43 +08002336 err = mmc_power_init(mmc);
2337 if (err)
2338 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01002339
Kishon Vijay Abraham I83dc4222017-09-21 16:30:10 +02002340#ifdef CONFIG_MMC_QUIRKS
2341 mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
2342 MMC_QUIRK_RETRY_SEND_CID;
2343#endif
2344
Jean-Jacques Hiblot04a2ea22017-09-21 16:30:08 +02002345 err = mmc_power_cycle(mmc);
2346 if (err) {
2347 /*
2348 * if power cycling is not supported, we should not try
2349 * to use the UHS modes, because we wouldn't be able to
2350 * recover from an error during the UHS initialization.
2351 */
2352 debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
2353 uhs_en = false;
2354 mmc->host_caps &= ~UHS_CAPS;
2355 err = mmc_power_on(mmc);
2356 }
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002357 if (err)
2358 return err;
2359
Simon Glasse7881d82017-07-29 11:35:31 -06002360#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06002361 /* The device has already been probed ready for use */
2362#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02002363 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02002364 err = mmc->cfg->ops->init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002365 if (err)
2366 return err;
Simon Glass8ca51e52016-06-12 23:30:22 -06002367#endif
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06002368 mmc->ddr_mode = 0;
Kishon Vijay Abraham Iaff5d3c2017-09-21 16:30:00 +02002369
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002370retry:
Kishon Vijay Abraham Ifb7c3be2017-09-21 16:30:02 +02002371 mmc_set_initial_state(mmc);
Jean-Jacques Hiblot318a7a52017-09-21 16:30:01 +02002372 mmc_send_init_stream(mmc);
2373
Andy Fleming272cc702008-10-30 16:41:01 -05002374 /* Reset the Card */
2375 err = mmc_go_idle(mmc);
2376
2377 if (err)
2378 return err;
2379
Lei Wenbc897b12011-05-02 16:26:26 +00002380 /* The internal partition reset to user partition(0) at every CMD0*/
Simon Glassc40fdca2016-05-01 13:52:35 -06002381 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00002382
Andy Fleming272cc702008-10-30 16:41:01 -05002383 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00002384 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05002385
Andy Fleming272cc702008-10-30 16:41:01 -05002386 /* Now try to get the SD card's operating condition */
Jean-Jacques Hiblotc10b85d2017-09-21 16:30:07 +02002387 err = sd_send_op_cond(mmc, uhs_en);
2388 if (err && uhs_en) {
2389 uhs_en = false;
2390 mmc_power_cycle(mmc);
2391 goto retry;
2392 }
Andy Fleming272cc702008-10-30 16:41:01 -05002393
2394 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002395 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05002396 err = mmc_send_op_cond(mmc);
2397
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002398 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01002399#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -05002400 printf("Card did not respond to voltage select!\n");
Paul Burton56196822013-09-04 16:12:25 +01002401#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09002402 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05002403 }
2404 }
2405
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002406 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002407 mmc->init_in_progress = 1;
2408
2409 return err;
2410}
2411
2412static int mmc_complete_init(struct mmc *mmc)
2413{
2414 int err = 0;
2415
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002416 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002417 if (mmc->op_cond_pending)
2418 err = mmc_complete_op_cond(mmc);
2419
2420 if (!err)
2421 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00002422 if (err)
2423 mmc->has_init = 0;
2424 else
2425 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00002426 return err;
2427}
2428
2429int mmc_init(struct mmc *mmc)
2430{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002431 int err = 0;
Marek Vasutce9eca92016-12-01 02:06:32 +01002432 __maybe_unused unsigned start;
Simon Glassc4d660d2017-07-04 13:31:19 -06002433#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06002434 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00002435
Simon Glass33fb2112016-05-01 13:52:41 -06002436 upriv->mmc = mmc;
2437#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00002438 if (mmc->has_init)
2439 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02002440
2441 start = get_timer(0);
2442
Che-Liang Chioue9550442012-11-28 15:21:13 +00002443 if (!mmc->init_in_progress)
2444 err = mmc_start_init(mmc);
2445
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05002446 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00002447 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01002448 if (err)
2449 printf("%s: %d, time %lu\n", __func__, err, get_timer(start));
2450
Lei Wenbc897b12011-05-02 16:26:26 +00002451 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05002452}
2453
Markus Niebelab711882013-12-16 13:40:46 +01002454int mmc_set_dsr(struct mmc *mmc, u16 val)
2455{
2456 mmc->dsr = val;
2457 return 0;
2458}
2459
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002460/* CPU-specific MMC initializations */
2461__weak int cpu_mmc_init(bd_t *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05002462{
2463 return -1;
2464}
2465
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02002466/* board-specific MMC initializations. */
2467__weak int board_mmc_init(bd_t *bis)
2468{
2469 return -1;
2470}
Andy Fleming272cc702008-10-30 16:41:01 -05002471
Che-Liang Chioue9550442012-11-28 15:21:13 +00002472void mmc_set_preinit(struct mmc *mmc, int preinit)
2473{
2474 mmc->preinit = preinit;
2475}
2476
Simon Glassc4d660d2017-07-04 13:31:19 -06002477#if CONFIG_IS_ENABLED(DM_MMC) && defined(CONFIG_SPL_BUILD)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002478static int mmc_probe(bd_t *bis)
2479{
2480 return 0;
2481}
Simon Glassc4d660d2017-07-04 13:31:19 -06002482#elif CONFIG_IS_ENABLED(DM_MMC)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002483static int mmc_probe(bd_t *bis)
2484{
Simon Glass4a1db6d2015-12-29 05:22:49 -07002485 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002486 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07002487 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002488
2489 ret = uclass_get(UCLASS_MMC, &uc);
2490 if (ret)
2491 return ret;
2492
Simon Glass4a1db6d2015-12-29 05:22:49 -07002493 /*
2494 * Try to add them in sequence order. Really with driver model we
2495 * should allow holes, but the current MMC list does not allow that.
2496 * So if we request 0, 1, 3 we will get 0, 1, 2.
2497 */
2498 for (i = 0; ; i++) {
2499 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
2500 if (ret == -ENODEV)
2501 break;
2502 }
2503 uclass_foreach_dev(dev, uc) {
2504 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002505 if (ret)
Simon Glass4a1db6d2015-12-29 05:22:49 -07002506 printf("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002507 }
2508
2509 return 0;
2510}
2511#else
2512static int mmc_probe(bd_t *bis)
2513{
2514 if (board_mmc_init(bis) < 0)
2515 cpu_mmc_init(bis);
2516
2517 return 0;
2518}
2519#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00002520
Andy Fleming272cc702008-10-30 16:41:01 -05002521int mmc_initialize(bd_t *bis)
2522{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02002523 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002524 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02002525 if (initialized) /* Avoid initializing mmc multiple times */
2526 return 0;
2527 initialized = 1;
2528
Simon Glassc4d660d2017-07-04 13:31:19 -06002529#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01002530#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06002531 mmc_list_init();
2532#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01002533#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06002534 ret = mmc_probe(bis);
2535 if (ret)
2536 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05002537
Ying Zhangbb0dc102013-08-16 15:16:11 +08002538#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05002539 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08002540#endif
Andy Fleming272cc702008-10-30 16:41:01 -05002541
Simon Glassc40fdca2016-05-01 13:52:35 -06002542 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05002543 return 0;
2544}
Tomas Melincd3d4882016-11-25 11:01:03 +02002545
2546#ifdef CONFIG_CMD_BKOPS_ENABLE
2547int mmc_set_bkops_enable(struct mmc *mmc)
2548{
2549 int err;
2550 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
2551
2552 err = mmc_send_ext_csd(mmc, ext_csd);
2553 if (err) {
2554 puts("Could not get ext_csd register values\n");
2555 return err;
2556 }
2557
2558 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
2559 puts("Background operations not supported on device\n");
2560 return -EMEDIUMTYPE;
2561 }
2562
2563 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
2564 puts("Background operations already enabled\n");
2565 return 0;
2566 }
2567
2568 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
2569 if (err) {
2570 puts("Failed to enable manual background operations\n");
2571 return err;
2572 }
2573
2574 puts("Enabled manual background operations\n");
2575
2576 return 0;
2577}
2578#endif