blob: ae829143966af92fc4bcccbd231fa6b4ef69190c [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
Marek Vasutb5b838f2016-12-01 02:06:33 +010033#if CONFIG_IS_ENABLED(MMC_TINY)
34static struct mmc mmc_static;
35struct mmc *find_mmc_device(int dev_num)
36{
37 return &mmc_static;
38}
39
40void mmc_do_preinit(void)
41{
42 struct mmc *m = &mmc_static;
43#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
44 mmc_set_preinit(m, 1);
45#endif
46 if (m->preinit)
47 mmc_start_init(m);
48}
49
50struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
51{
52 return &mmc->block_dev;
53}
54#endif
55
Simon Glasse7881d82017-07-29 11:35:31 -060056#if !CONFIG_IS_ENABLED(DM_MMC)
Jeroen Hofstee750121c2014-07-12 21:24:08 +020057__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000058{
59 return -1;
60}
61
62int mmc_getwp(struct mmc *mmc)
63{
64 int wp;
65
66 wp = board_mmc_getwp(mmc);
67
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000068 if (wp < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020069 if (mmc->cfg->ops->getwp)
70 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000071 else
72 wp = 0;
73 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000074
75 return wp;
76}
77
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +020078__weak int board_mmc_getcd(struct mmc *mmc)
79{
Stefano Babic11fdade2010-02-05 15:04:43 +010080 return -1;
81}
Simon Glass8ca51e52016-06-12 23:30:22 -060082#endif
Stefano Babic11fdade2010-02-05 15:04:43 +010083
Marek Vasut8635ff92012-03-15 18:41:35 +000084#ifdef CONFIG_MMC_TRACE
Simon Glassc0c76eb2016-06-12 23:30:20 -060085void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
86{
87 printf("CMD_SEND:%d\n", cmd->cmdidx);
88 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
89}
90
91void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
92{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000093 int i;
94 u8 *ptr;
95
Bin Meng7863ce52016-03-17 21:53:14 -070096 if (ret) {
97 printf("\t\tRET\t\t\t %d\n", ret);
98 } else {
99 switch (cmd->resp_type) {
100 case MMC_RSP_NONE:
101 printf("\t\tMMC_RSP_NONE\n");
102 break;
103 case MMC_RSP_R1:
104 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
105 cmd->response[0]);
106 break;
107 case MMC_RSP_R1b:
108 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
109 cmd->response[0]);
110 break;
111 case MMC_RSP_R2:
112 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
113 cmd->response[0]);
114 printf("\t\t \t\t 0x%08X \n",
115 cmd->response[1]);
116 printf("\t\t \t\t 0x%08X \n",
117 cmd->response[2]);
118 printf("\t\t \t\t 0x%08X \n",
119 cmd->response[3]);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000120 printf("\n");
Bin Meng7863ce52016-03-17 21:53:14 -0700121 printf("\t\t\t\t\tDUMPING DATA\n");
122 for (i = 0; i < 4; i++) {
123 int j;
124 printf("\t\t\t\t\t%03d - ", i*4);
125 ptr = (u8 *)&cmd->response[i];
126 ptr += 3;
127 for (j = 0; j < 4; j++)
128 printf("%02X ", *ptr--);
129 printf("\n");
130 }
131 break;
132 case MMC_RSP_R3:
133 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
134 cmd->response[0]);
135 break;
136 default:
137 printf("\t\tERROR MMC rsp not supported\n");
138 break;
Bin Meng53e8e402016-03-17 21:53:13 -0700139 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000140 }
Simon Glassc0c76eb2016-06-12 23:30:20 -0600141}
142
143void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
144{
145 int status;
146
147 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
148 printf("CURR STATE:%d\n", status);
149}
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000150#endif
Simon Glassc0c76eb2016-06-12 23:30:20 -0600151
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +0200152#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
153const char *mmc_mode_name(enum bus_mode mode)
154{
155 static const char *const names[] = {
156 [MMC_LEGACY] = "MMC legacy",
157 [SD_LEGACY] = "SD Legacy",
158 [MMC_HS] = "MMC High Speed (26MHz)",
159 [SD_HS] = "SD High Speed (50MHz)",
160 [UHS_SDR12] = "UHS SDR12 (25MHz)",
161 [UHS_SDR25] = "UHS SDR25 (50MHz)",
162 [UHS_SDR50] = "UHS SDR50 (100MHz)",
163 [UHS_SDR104] = "UHS SDR104 (208MHz)",
164 [UHS_DDR50] = "UHS DDR50 (50MHz)",
165 [MMC_HS_52] = "MMC High Speed (52MHz)",
166 [MMC_DDR_52] = "MMC DDR52 (52MHz)",
167 [MMC_HS_200] = "HS200 (200MHz)",
168 };
169
170 if (mode >= MMC_MODES_END)
171 return "Unknown mode";
172 else
173 return names[mode];
174}
175#endif
176
177static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
178{
179 mmc->selected_mode = mode;
180 debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
181 mmc->tran_speed / 1000000);
182 return 0;
183}
184
Simon Glasse7881d82017-07-29 11:35:31 -0600185#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassc0c76eb2016-06-12 23:30:20 -0600186int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
187{
188 int ret;
189
190 mmmc_trace_before_send(mmc, cmd);
191 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
192 mmmc_trace_after_send(mmc, cmd, ret);
193
Marek Vasut8635ff92012-03-15 18:41:35 +0000194 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500195}
Simon Glass8ca51e52016-06-12 23:30:22 -0600196#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500197
Paul Burtonda61fa52013-09-09 15:30:26 +0100198int mmc_send_status(struct mmc *mmc, int timeout)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000199{
200 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000201 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000202
203 cmd.cmdidx = MMC_CMD_SEND_STATUS;
204 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200205 if (!mmc_host_is_spi(mmc))
206 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000207
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500208 while (1) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000209 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000210 if (!err) {
211 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
212 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
213 MMC_STATE_PRG)
214 break;
215 else if (cmd.response[0] & MMC_STATUS_MASK) {
Paul Burton56196822013-09-04 16:12:25 +0100216#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jan Kloetzked617c422012-02-05 22:29:12 +0000217 printf("Status Error: 0x%08X\n",
218 cmd.response[0]);
Paul Burton56196822013-09-04 16:12:25 +0100219#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900220 return -ECOMM;
Jan Kloetzked617c422012-02-05 22:29:12 +0000221 }
222 } else if (--retries < 0)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000223 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000224
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500225 if (timeout-- <= 0)
226 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000227
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500228 udelay(1000);
229 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000230
Simon Glassc0c76eb2016-06-12 23:30:20 -0600231 mmc_trace_state(mmc, &cmd);
Jongman Heo5b0c9422012-06-03 21:32:13 +0000232 if (timeout <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100233#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000234 printf("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100235#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900236 return -ETIMEDOUT;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000237 }
238
239 return 0;
240}
241
Paul Burtonda61fa52013-09-09 15:30:26 +0100242int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500243{
244 struct mmc_cmd cmd;
245
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600246 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900247 return 0;
248
Andy Fleming272cc702008-10-30 16:41:01 -0500249 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
250 cmd.resp_type = MMC_RSP_R1;
251 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500252
253 return mmc_send_cmd(mmc, &cmd, NULL);
254}
255
Sascha Silbeff8fef52013-06-14 13:07:25 +0200256static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000257 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500258{
259 struct mmc_cmd cmd;
260 struct mmc_data data;
261
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700262 if (blkcnt > 1)
263 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
264 else
265 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500266
267 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700268 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500269 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700270 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500271
272 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500273
274 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700275 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500276 data.blocksize = mmc->read_bl_len;
277 data.flags = MMC_DATA_READ;
278
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700279 if (mmc_send_cmd(mmc, &cmd, &data))
280 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500281
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700282 if (blkcnt > 1) {
283 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
284 cmd.cmdarg = 0;
285 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700286 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton56196822013-09-04 16:12:25 +0100287#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700288 printf("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100289#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700290 return 0;
291 }
Andy Fleming272cc702008-10-30 16:41:01 -0500292 }
293
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700294 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500295}
296
Simon Glassc4d660d2017-07-04 13:31:19 -0600297#if CONFIG_IS_ENABLED(BLK)
Simon Glass7dba0b92016-06-12 23:30:15 -0600298ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600299#else
Simon Glass7dba0b92016-06-12 23:30:15 -0600300ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
301 void *dst)
Simon Glass33fb2112016-05-01 13:52:41 -0600302#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500303{
Simon Glassc4d660d2017-07-04 13:31:19 -0600304#if CONFIG_IS_ENABLED(BLK)
Simon Glass33fb2112016-05-01 13:52:41 -0600305 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
306#endif
Simon Glassbcce53d2016-02-29 15:25:51 -0700307 int dev_num = block_dev->devnum;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700308 int err;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700309 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500310
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700311 if (blkcnt == 0)
312 return 0;
313
314 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500315 if (!mmc)
316 return 0;
317
Marek Vasutb5b838f2016-12-01 02:06:33 +0100318 if (CONFIG_IS_ENABLED(MMC_TINY))
319 err = mmc_switch_part(mmc, block_dev->hwpart);
320 else
321 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
322
Stephen Warren873cc1d2015-12-07 11:38:49 -0700323 if (err < 0)
324 return 0;
325
Simon Glassc40fdca2016-05-01 13:52:35 -0600326 if ((start + blkcnt) > block_dev->lba) {
Paul Burton56196822013-09-04 16:12:25 +0100327#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Sascha Silbeff8fef52013-06-14 13:07:25 +0200328 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Simon Glassc40fdca2016-05-01 13:52:35 -0600329 start + blkcnt, block_dev->lba);
Paul Burton56196822013-09-04 16:12:25 +0100330#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800331 return 0;
332 }
Andy Fleming272cc702008-10-30 16:41:01 -0500333
Simon Glass11692992015-06-23 15:38:50 -0600334 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
335 debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500336 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600337 }
Andy Fleming272cc702008-10-30 16:41:01 -0500338
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700339 do {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200340 cur = (blocks_todo > mmc->cfg->b_max) ?
341 mmc->cfg->b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600342 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
343 debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700344 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600345 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700346 blocks_todo -= cur;
347 start += cur;
348 dst += cur * mmc->read_bl_len;
349 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500350
351 return blkcnt;
352}
353
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000354static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500355{
356 struct mmc_cmd cmd;
357 int err;
358
359 udelay(1000);
360
361 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
362 cmd.cmdarg = 0;
363 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500364
365 err = mmc_send_cmd(mmc, &cmd, NULL);
366
367 if (err)
368 return err;
369
370 udelay(2000);
371
372 return 0;
373}
374
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000375static int sd_send_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500376{
377 int timeout = 1000;
378 int err;
379 struct mmc_cmd cmd;
380
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500381 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500382 cmd.cmdidx = MMC_CMD_APP_CMD;
383 cmd.resp_type = MMC_RSP_R1;
384 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500385
386 err = mmc_send_cmd(mmc, &cmd, NULL);
387
388 if (err)
389 return err;
390
391 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
392 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100393
394 /*
395 * Most cards do not answer if some reserved bits
396 * in the ocr are set. However, Some controller
397 * can set bit 7 (reserved for low voltages), but
398 * how to manage low voltages SD card is not yet
399 * specified.
400 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000401 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200402 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500403
404 if (mmc->version == SD_VERSION_2)
405 cmd.cmdarg |= OCR_HCS;
406
407 err = mmc_send_cmd(mmc, &cmd, NULL);
408
409 if (err)
410 return err;
411
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500412 if (cmd.response[0] & OCR_BUSY)
413 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500414
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500415 if (timeout-- <= 0)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900416 return -EOPNOTSUPP;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500417
418 udelay(1000);
419 }
Andy Fleming272cc702008-10-30 16:41:01 -0500420
421 if (mmc->version != SD_VERSION_2)
422 mmc->version = SD_VERSION_1_0;
423
Thomas Choud52ebf12010-12-24 13:12:21 +0000424 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
425 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
426 cmd.resp_type = MMC_RSP_R3;
427 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000428
429 err = mmc_send_cmd(mmc, &cmd, NULL);
430
431 if (err)
432 return err;
433 }
434
Rabin Vincent998be3d2009-04-05 13:30:56 +0530435 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500436
437 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
438 mmc->rca = 0;
439
440 return 0;
441}
442
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500443static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500444{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500445 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500446 int err;
447
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500448 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
449 cmd.resp_type = MMC_RSP_R3;
450 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500451 if (use_arg && !mmc_host_is_spi(mmc))
452 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200453 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500454 (mmc->ocr & OCR_VOLTAGE_MASK)) |
455 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000456
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500457 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000458 if (err)
459 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500460 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000461 return 0;
462}
463
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200464static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000465{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000466 int err, i;
467
Andy Fleming272cc702008-10-30 16:41:01 -0500468 /* Some cards seem to need this */
469 mmc_go_idle(mmc);
470
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000471 /* Asking to the card its capabilities */
Che-Liang Chioue9550442012-11-28 15:21:13 +0000472 for (i = 0; i < 2; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500473 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500474 if (err)
475 return err;
476
Che-Liang Chioue9550442012-11-28 15:21:13 +0000477 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500478 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500479 break;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000480 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500481 mmc->op_cond_pending = 1;
482 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000483}
Andy Fleming272cc702008-10-30 16:41:01 -0500484
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200485static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000486{
487 struct mmc_cmd cmd;
488 int timeout = 1000;
489 uint start;
490 int err;
491
492 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500493 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lud188b112016-08-02 15:33:18 +0800494 /* Some cards seem to need this */
495 mmc_go_idle(mmc);
496
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500497 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500498 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500499 err = mmc_send_op_cond_iter(mmc, 1);
500 if (err)
501 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500502 if (mmc->ocr & OCR_BUSY)
503 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500504 if (get_timer(start) > timeout)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900505 return -EOPNOTSUPP;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500506 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500507 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500508 }
Andy Fleming272cc702008-10-30 16:41:01 -0500509
Thomas Choud52ebf12010-12-24 13:12:21 +0000510 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
511 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
512 cmd.resp_type = MMC_RSP_R3;
513 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000514
515 err = mmc_send_cmd(mmc, &cmd, NULL);
516
517 if (err)
518 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500519
520 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000521 }
522
Andy Fleming272cc702008-10-30 16:41:01 -0500523 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500524
525 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700526 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500527
528 return 0;
529}
530
531
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000532static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500533{
534 struct mmc_cmd cmd;
535 struct mmc_data data;
536 int err;
537
538 /* Get the Card Status Register */
539 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
540 cmd.resp_type = MMC_RSP_R1;
541 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500542
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000543 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500544 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000545 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500546 data.flags = MMC_DATA_READ;
547
548 err = mmc_send_cmd(mmc, &cmd, &data);
549
550 return err;
551}
552
Simon Glassc40704f2016-06-12 23:30:18 -0600553int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Fleming272cc702008-10-30 16:41:01 -0500554{
555 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000556 int timeout = 1000;
Maxime Riparda9003dc2016-11-04 16:18:08 +0100557 int retries = 3;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000558 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500559
560 cmd.cmdidx = MMC_CMD_SWITCH;
561 cmd.resp_type = MMC_RSP_R1b;
562 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000563 (index << 16) |
564 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500565
Maxime Riparda9003dc2016-11-04 16:18:08 +0100566 while (retries > 0) {
567 ret = mmc_send_cmd(mmc, &cmd, NULL);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000568
Maxime Riparda9003dc2016-11-04 16:18:08 +0100569 /* Waiting for the ready status */
570 if (!ret) {
571 ret = mmc_send_status(mmc, timeout);
572 return ret;
573 }
574
575 retries--;
576 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000577
578 return ret;
579
Andy Fleming272cc702008-10-30 16:41:01 -0500580}
581
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000582static int mmc_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500583{
Simon Glass8bfa1952013-04-03 08:54:30 +0000584 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Andy Fleming272cc702008-10-30 16:41:01 -0500585 char cardtype;
586 int err;
587
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600588 mmc->card_caps = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500589
Thomas Choud52ebf12010-12-24 13:12:21 +0000590 if (mmc_host_is_spi(mmc))
591 return 0;
592
Andy Fleming272cc702008-10-30 16:41:01 -0500593 /* Only version 4 supports high-speed */
594 if (mmc->version < MMC_VERSION_4)
595 return 0;
596
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600597 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
598
Andy Fleming272cc702008-10-30 16:41:01 -0500599 err = mmc_send_ext_csd(mmc, ext_csd);
600
601 if (err)
602 return err;
603
Lei Wen0560db12011-10-03 20:35:10 +0000604 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500605
606 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
607
608 if (err)
Heiko Schochera5e27b42016-06-07 08:31:21 +0200609 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500610
611 /* Now check to see that it worked */
612 err = mmc_send_ext_csd(mmc, ext_csd);
613
614 if (err)
615 return err;
616
617 /* No high-speed support */
Lei Wen0560db12011-10-03 20:35:10 +0000618 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Fleming272cc702008-10-30 16:41:01 -0500619 return 0;
620
621 /* High Speed is set, there are two types: 52MHz and 26MHz */
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900622 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Andrew Gabbasov201d5ac2014-12-01 06:59:10 -0600623 if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900624 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Andy Fleming272cc702008-10-30 16:41:01 -0500625 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900626 } else {
Andy Fleming272cc702008-10-30 16:41:01 -0500627 mmc->card_caps |= MMC_MODE_HS;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900628 }
Andy Fleming272cc702008-10-30 16:41:01 -0500629
630 return 0;
631}
632
Stephen Warrenf866a462013-06-11 15:14:01 -0600633static int mmc_set_capacity(struct mmc *mmc, int part_num)
634{
635 switch (part_num) {
636 case 0:
637 mmc->capacity = mmc->capacity_user;
638 break;
639 case 1:
640 case 2:
641 mmc->capacity = mmc->capacity_boot;
642 break;
643 case 3:
644 mmc->capacity = mmc->capacity_rpmb;
645 break;
646 case 4:
647 case 5:
648 case 6:
649 case 7:
650 mmc->capacity = mmc->capacity_gp[part_num - 4];
651 break;
652 default:
653 return -1;
654 }
655
Simon Glassc40fdca2016-05-01 13:52:35 -0600656 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrenf866a462013-06-11 15:14:01 -0600657
658 return 0;
659}
660
Simon Glass7dba0b92016-06-12 23:30:15 -0600661int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wenbc897b12011-05-02 16:26:26 +0000662{
Stephen Warrenf866a462013-06-11 15:14:01 -0600663 int ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000664
Stephen Warrenf866a462013-06-11 15:14:01 -0600665 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
666 (mmc->part_config & ~PART_ACCESS_MASK)
667 | (part_num & PART_ACCESS_MASK));
Stephen Warrenf866a462013-06-11 15:14:01 -0600668
Peter Bigot6dc93e72014-09-02 18:31:23 -0500669 /*
670 * Set the capacity if the switch succeeded or was intended
671 * to return to representing the raw device.
672 */
Stephen Warren873cc1d2015-12-07 11:38:49 -0700673 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot6dc93e72014-09-02 18:31:23 -0500674 ret = mmc_set_capacity(mmc, part_num);
Simon Glassfdbb1392016-05-01 13:52:37 -0600675 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren873cc1d2015-12-07 11:38:49 -0700676 }
Peter Bigot6dc93e72014-09-02 18:31:23 -0500677
678 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000679}
680
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100681int mmc_hwpart_config(struct mmc *mmc,
682 const struct mmc_hwpart_conf *conf,
683 enum mmc_hwpart_conf_mode mode)
684{
685 u8 part_attrs = 0;
686 u32 enh_size_mult;
687 u32 enh_start_addr;
688 u32 gp_size_mult[4];
689 u32 max_enh_size_mult;
690 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100691 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100692 int i, pidx, err;
693 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
694
695 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
696 return -EINVAL;
697
698 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
699 printf("eMMC >= 4.4 required for enhanced user data area\n");
700 return -EMEDIUMTYPE;
701 }
702
703 if (!(mmc->part_support & PART_SUPPORT)) {
704 printf("Card does not support partitioning\n");
705 return -EMEDIUMTYPE;
706 }
707
708 if (!mmc->hc_wp_grp_size) {
709 printf("Card does not define HC WP group size\n");
710 return -EMEDIUMTYPE;
711 }
712
713 /* check partition alignment and total enhanced size */
714 if (conf->user.enh_size) {
715 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
716 conf->user.enh_start % mmc->hc_wp_grp_size) {
717 printf("User data enhanced area not HC WP group "
718 "size aligned\n");
719 return -EINVAL;
720 }
721 part_attrs |= EXT_CSD_ENH_USR;
722 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
723 if (mmc->high_capacity) {
724 enh_start_addr = conf->user.enh_start;
725 } else {
726 enh_start_addr = (conf->user.enh_start << 9);
727 }
728 } else {
729 enh_size_mult = 0;
730 enh_start_addr = 0;
731 }
732 tot_enh_size_mult += enh_size_mult;
733
734 for (pidx = 0; pidx < 4; pidx++) {
735 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
736 printf("GP%i partition not HC WP group size "
737 "aligned\n", pidx+1);
738 return -EINVAL;
739 }
740 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
741 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
742 part_attrs |= EXT_CSD_ENH_GP(pidx);
743 tot_enh_size_mult += gp_size_mult[pidx];
744 }
745 }
746
747 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
748 printf("Card does not support enhanced attribute\n");
749 return -EMEDIUMTYPE;
750 }
751
752 err = mmc_send_ext_csd(mmc, ext_csd);
753 if (err)
754 return err;
755
756 max_enh_size_mult =
757 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
758 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
759 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
760 if (tot_enh_size_mult > max_enh_size_mult) {
761 printf("Total enhanced size exceeds maximum (%u > %u)\n",
762 tot_enh_size_mult, max_enh_size_mult);
763 return -EMEDIUMTYPE;
764 }
765
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100766 /* The default value of EXT_CSD_WR_REL_SET is device
767 * dependent, the values can only be changed if the
768 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
769 * changed only once and before partitioning is completed. */
770 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
771 if (conf->user.wr_rel_change) {
772 if (conf->user.wr_rel_set)
773 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
774 else
775 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
776 }
777 for (pidx = 0; pidx < 4; pidx++) {
778 if (conf->gp_part[pidx].wr_rel_change) {
779 if (conf->gp_part[pidx].wr_rel_set)
780 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
781 else
782 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
783 }
784 }
785
786 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
787 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
788 puts("Card does not support host controlled partition write "
789 "reliability settings\n");
790 return -EMEDIUMTYPE;
791 }
792
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100793 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
794 EXT_CSD_PARTITION_SETTING_COMPLETED) {
795 printf("Card already partitioned\n");
796 return -EPERM;
797 }
798
799 if (mode == MMC_HWPART_CONF_CHECK)
800 return 0;
801
802 /* Partitioning requires high-capacity size definitions */
803 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
804 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
805 EXT_CSD_ERASE_GROUP_DEF, 1);
806
807 if (err)
808 return err;
809
810 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
811
812 /* update erase group size to be high-capacity */
813 mmc->erase_grp_size =
814 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
815
816 }
817
818 /* all OK, write the configuration */
819 for (i = 0; i < 4; i++) {
820 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
821 EXT_CSD_ENH_START_ADDR+i,
822 (enh_start_addr >> (i*8)) & 0xFF);
823 if (err)
824 return err;
825 }
826 for (i = 0; i < 3; i++) {
827 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
828 EXT_CSD_ENH_SIZE_MULT+i,
829 (enh_size_mult >> (i*8)) & 0xFF);
830 if (err)
831 return err;
832 }
833 for (pidx = 0; pidx < 4; pidx++) {
834 for (i = 0; i < 3; i++) {
835 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
836 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
837 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
838 if (err)
839 return err;
840 }
841 }
842 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
843 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
844 if (err)
845 return err;
846
847 if (mode == MMC_HWPART_CONF_SET)
848 return 0;
849
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100850 /* The WR_REL_SET is a write-once register but shall be
851 * written before setting PART_SETTING_COMPLETED. As it is
852 * write-once we can only write it when completing the
853 * partitioning. */
854 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
855 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
856 EXT_CSD_WR_REL_SET, wr_rel_set);
857 if (err)
858 return err;
859 }
860
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100861 /* Setting PART_SETTING_COMPLETED confirms the partition
862 * configuration but it only becomes effective after power
863 * cycle, so we do not adjust the partition related settings
864 * in the mmc struct. */
865
866 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
867 EXT_CSD_PARTITION_SETTING,
868 EXT_CSD_PARTITION_SETTING_COMPLETED);
869 if (err)
870 return err;
871
872 return 0;
873}
874
Simon Glasse7881d82017-07-29 11:35:31 -0600875#if !CONFIG_IS_ENABLED(DM_MMC)
Thierry Reding48972d92012-01-02 01:15:37 +0000876int mmc_getcd(struct mmc *mmc)
877{
878 int cd;
879
880 cd = board_mmc_getcd(mmc);
881
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000882 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200883 if (mmc->cfg->ops->getcd)
884 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000885 else
886 cd = 1;
887 }
Thierry Reding48972d92012-01-02 01:15:37 +0000888
889 return cd;
890}
Simon Glass8ca51e52016-06-12 23:30:22 -0600891#endif
Thierry Reding48972d92012-01-02 01:15:37 +0000892
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000893static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -0500894{
895 struct mmc_cmd cmd;
896 struct mmc_data data;
897
898 /* Switch the frequency */
899 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
900 cmd.resp_type = MMC_RSP_R1;
901 cmd.cmdarg = (mode << 31) | 0xffffff;
902 cmd.cmdarg &= ~(0xf << (group * 4));
903 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500904
905 data.dest = (char *)resp;
906 data.blocksize = 64;
907 data.blocks = 1;
908 data.flags = MMC_DATA_READ;
909
910 return mmc_send_cmd(mmc, &cmd, &data);
911}
912
913
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000914static int sd_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500915{
916 int err;
917 struct mmc_cmd cmd;
Suniel Mahesh18e7c8f2017-10-05 11:32:00 +0530918 ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
919 ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500920 struct mmc_data data;
921 int timeout;
922
923 mmc->card_caps = 0;
924
Thomas Choud52ebf12010-12-24 13:12:21 +0000925 if (mmc_host_is_spi(mmc))
926 return 0;
927
Andy Fleming272cc702008-10-30 16:41:01 -0500928 /* Read the SCR to find out if this card supports higher speeds */
929 cmd.cmdidx = MMC_CMD_APP_CMD;
930 cmd.resp_type = MMC_RSP_R1;
931 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -0500932
933 err = mmc_send_cmd(mmc, &cmd, NULL);
934
935 if (err)
936 return err;
937
938 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
939 cmd.resp_type = MMC_RSP_R1;
940 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500941
942 timeout = 3;
943
944retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +0000945 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -0500946 data.blocksize = 8;
947 data.blocks = 1;
948 data.flags = MMC_DATA_READ;
949
950 err = mmc_send_cmd(mmc, &cmd, &data);
951
952 if (err) {
953 if (timeout--)
954 goto retry_scr;
955
956 return err;
957 }
958
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300959 mmc->scr[0] = __be32_to_cpu(scr[0]);
960 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500961
962 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng53e8e402016-03-17 21:53:13 -0700963 case 0:
964 mmc->version = SD_VERSION_1_0;
965 break;
966 case 1:
967 mmc->version = SD_VERSION_1_10;
968 break;
969 case 2:
970 mmc->version = SD_VERSION_2;
971 if ((mmc->scr[0] >> 15) & 0x1)
972 mmc->version = SD_VERSION_3;
973 break;
974 default:
975 mmc->version = SD_VERSION_1_0;
976 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500977 }
978
Alagu Sankarb44c7082010-05-12 15:08:24 +0530979 if (mmc->scr[0] & SD_DATA_4BIT)
980 mmc->card_caps |= MMC_MODE_4BIT;
981
Andy Fleming272cc702008-10-30 16:41:01 -0500982 /* Version 1.0 doesn't support switching */
983 if (mmc->version == SD_VERSION_1_0)
984 return 0;
985
986 timeout = 4;
987 while (timeout--) {
988 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +0000989 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500990
991 if (err)
992 return err;
993
994 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300995 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500996 break;
997 }
998
Andy Fleming272cc702008-10-30 16:41:01 -0500999 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001000 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -05001001 return 0;
1002
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001003 /*
1004 * If the host doesn't support SD_HIGHSPEED, do not switch card to
1005 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
1006 * This can avoid furthur problem when the card runs in different
1007 * mode between the host.
1008 */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001009 if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
1010 (mmc->cfg->host_caps & MMC_MODE_HS)))
Macpaul Lin2c3fbf42011-11-28 16:31:09 +00001011 return 0;
1012
Anton staaff781dd32011-10-03 13:54:59 +00001013 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -05001014
1015 if (err)
1016 return err;
1017
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +03001018 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -05001019 mmc->card_caps |= MMC_MODE_HS;
1020
1021 return 0;
1022}
1023
Peng Fan3697e592016-09-01 11:13:38 +08001024static int sd_read_ssr(struct mmc *mmc)
1025{
1026 int err, i;
1027 struct mmc_cmd cmd;
1028 ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
1029 struct mmc_data data;
1030 int timeout = 3;
1031 unsigned int au, eo, et, es;
1032
1033 cmd.cmdidx = MMC_CMD_APP_CMD;
1034 cmd.resp_type = MMC_RSP_R1;
1035 cmd.cmdarg = mmc->rca << 16;
1036
1037 err = mmc_send_cmd(mmc, &cmd, NULL);
1038 if (err)
1039 return err;
1040
1041 cmd.cmdidx = SD_CMD_APP_SD_STATUS;
1042 cmd.resp_type = MMC_RSP_R1;
1043 cmd.cmdarg = 0;
1044
1045retry_ssr:
1046 data.dest = (char *)ssr;
1047 data.blocksize = 64;
1048 data.blocks = 1;
1049 data.flags = MMC_DATA_READ;
1050
1051 err = mmc_send_cmd(mmc, &cmd, &data);
1052 if (err) {
1053 if (timeout--)
1054 goto retry_ssr;
1055
1056 return err;
1057 }
1058
1059 for (i = 0; i < 16; i++)
1060 ssr[i] = be32_to_cpu(ssr[i]);
1061
1062 au = (ssr[2] >> 12) & 0xF;
1063 if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
1064 mmc->ssr.au = sd_au_size[au];
1065 es = (ssr[3] >> 24) & 0xFF;
1066 es |= (ssr[2] & 0xFF) << 8;
1067 et = (ssr[3] >> 18) & 0x3F;
1068 if (es && et) {
1069 eo = (ssr[3] >> 16) & 0x3;
1070 mmc->ssr.erase_timeout = (et * 1000) / es;
1071 mmc->ssr.erase_offset = eo * 1000;
1072 }
1073 } else {
1074 debug("Invalid Allocation Unit Size.\n");
1075 }
1076
1077 return 0;
1078}
1079
Andy Fleming272cc702008-10-30 16:41:01 -05001080/* frequency bases */
1081/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +00001082static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001083 10000,
1084 100000,
1085 1000000,
1086 10000000,
1087};
1088
1089/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
1090 * to platforms without floating point.
1091 */
Simon Glass61fe0762016-05-14 14:02:57 -06001092static const u8 multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -05001093 0, /* reserved */
1094 10,
1095 12,
1096 13,
1097 15,
1098 20,
1099 25,
1100 30,
1101 35,
1102 40,
1103 45,
1104 50,
1105 55,
1106 60,
1107 70,
1108 80,
1109};
1110
Simon Glasse7881d82017-07-29 11:35:31 -06001111#if !CONFIG_IS_ENABLED(DM_MMC)
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001112static void mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001113{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001114 if (mmc->cfg->ops->set_ios)
1115 mmc->cfg->ops->set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001116}
Simon Glass8ca51e52016-06-12 23:30:22 -06001117#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001118
1119void mmc_set_clock(struct mmc *mmc, uint clock)
1120{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001121 if (clock > mmc->cfg->f_max)
1122 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001123
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001124 if (clock < mmc->cfg->f_min)
1125 clock = mmc->cfg->f_min;
Andy Fleming272cc702008-10-30 16:41:01 -05001126
1127 mmc->clock = clock;
1128
1129 mmc_set_ios(mmc);
1130}
1131
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001132static void mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001133{
1134 mmc->bus_width = width;
1135
1136 mmc_set_ios(mmc);
1137}
1138
Jean-Jacques Hiblot4c9d2aa2017-09-21 16:29:54 +02001139#if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
1140/*
1141 * helper function to display the capabilities in a human
1142 * friendly manner. The capabilities include bus width and
1143 * supported modes.
1144 */
1145void mmc_dump_capabilities(const char *text, uint caps)
1146{
1147 enum bus_mode mode;
1148
1149 printf("%s: widths [", text);
1150 if (caps & MMC_MODE_8BIT)
1151 printf("8, ");
1152 if (caps & MMC_MODE_4BIT)
1153 printf("4, ");
1154 printf("1] modes [");
1155
1156 for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
1157 if (MMC_CAP(mode) & caps)
1158 printf("%s, ", mmc_mode_name(mode));
1159 printf("\b\b]\n");
1160}
1161#endif
1162
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001163static int sd_select_bus_freq_width(struct mmc *mmc)
1164{
1165 int err;
1166 struct mmc_cmd cmd;
1167
1168 err = sd_change_freq(mmc);
1169 if (err)
1170 return err;
1171
1172 /* Restrict card's capabilities by what the host can do */
1173 mmc->card_caps &= mmc->cfg->host_caps;
1174
1175 if (mmc->card_caps & MMC_MODE_4BIT) {
1176 cmd.cmdidx = MMC_CMD_APP_CMD;
1177 cmd.resp_type = MMC_RSP_R1;
1178 cmd.cmdarg = mmc->rca << 16;
1179
1180 err = mmc_send_cmd(mmc, &cmd, NULL);
1181 if (err)
1182 return err;
1183
1184 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1185 cmd.resp_type = MMC_RSP_R1;
1186 cmd.cmdarg = 2;
1187 err = mmc_send_cmd(mmc, &cmd, NULL);
1188 if (err)
1189 return err;
1190
1191 mmc_set_bus_width(mmc, 4);
1192 }
1193
1194 err = sd_read_ssr(mmc);
1195 if (err)
1196 return err;
1197
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001198 if (mmc->card_caps & MMC_MODE_HS) {
1199 mmc_select_mode(mmc, SD_HS);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001200 mmc->tran_speed = 50000000;
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001201 } else {
1202 mmc_select_mode(mmc, SD_LEGACY);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001203 mmc->tran_speed = 25000000;
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001204 }
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001205
1206 return 0;
1207}
1208
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001209/*
1210 * read the compare the part of ext csd that is constant.
1211 * This can be used to check that the transfer is working
1212 * as expected.
1213 */
1214static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
1215{
1216 int err;
1217 const u8 *ext_csd = mmc->ext_csd;
1218 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1219
1220 err = mmc_send_ext_csd(mmc, test_csd);
1221 if (err)
1222 return err;
1223
1224 /* Only compare read only fields */
1225 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1226 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1227 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1228 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1229 ext_csd[EXT_CSD_REV]
1230 == test_csd[EXT_CSD_REV] &&
1231 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1232 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1233 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1234 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1235 return 0;
1236
1237 return -EBADMSG;
1238}
1239
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001240static int mmc_select_bus_freq_width(struct mmc *mmc)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001241{
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001242 /* An array of possible bus widths in order of preference */
1243 static const unsigned int ext_csd_bits[] = {
1244 EXT_CSD_DDR_BUS_WIDTH_8,
1245 EXT_CSD_DDR_BUS_WIDTH_4,
1246 EXT_CSD_BUS_WIDTH_8,
1247 EXT_CSD_BUS_WIDTH_4,
1248 EXT_CSD_BUS_WIDTH_1,
1249 };
1250 /* An array to map CSD bus widths to host cap bits */
1251 static const unsigned int ext_to_hostcaps[] = {
1252 [EXT_CSD_DDR_BUS_WIDTH_4] =
1253 MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1254 [EXT_CSD_DDR_BUS_WIDTH_8] =
1255 MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
1256 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1257 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1258 };
1259 /* An array to map chosen bus width to an integer */
1260 static const unsigned int widths[] = {
1261 8, 4, 8, 4, 1,
1262 };
1263 int err;
1264 int idx;
1265
1266 err = mmc_change_freq(mmc);
1267 if (err)
1268 return err;
1269
1270 /* Restrict card's capabilities by what the host can do */
1271 mmc->card_caps &= mmc->cfg->host_caps;
1272
1273 /* Only version 4 of MMC supports wider bus widths */
1274 if (mmc->version < MMC_VERSION_4)
1275 return 0;
1276
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001277 if (!mmc->ext_csd) {
1278 debug("No ext_csd found!\n"); /* this should enver happen */
1279 return -ENOTSUPP;
1280 }
1281
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001282 for (idx = 0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1283 unsigned int extw = ext_csd_bits[idx];
1284 unsigned int caps = ext_to_hostcaps[extw];
1285 /*
1286 * If the bus width is still not changed,
1287 * don't try to set the default again.
1288 * Otherwise, recover from switch attempts
1289 * by switching to 1-bit bus width.
1290 */
1291 if (extw == EXT_CSD_BUS_WIDTH_1 &&
1292 mmc->bus_width == 1) {
1293 err = 0;
1294 break;
1295 }
1296
1297 /*
1298 * Check to make sure the card and controller support
1299 * these capabilities
1300 */
1301 if ((mmc->card_caps & caps) != caps)
1302 continue;
1303
1304 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1305 EXT_CSD_BUS_WIDTH, extw);
1306
1307 if (err)
1308 continue;
1309
1310 mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
1311 mmc_set_bus_width(mmc, widths[idx]);
1312
Jean-Jacques Hiblot7382e692017-09-21 16:29:52 +02001313 err = mmc_read_and_compare_ext_csd(mmc);
1314 if (!err)
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001315 break;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001316 }
1317
1318 if (err)
1319 return err;
1320
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001321 if (mmc->card_caps & MMC_MODE_HS_52MHz) {
1322 if (mmc->ddr_mode)
1323 mmc_select_mode(mmc, MMC_DDR_52);
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001324 else
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001325 mmc_select_mode(mmc, MMC_HS_52);
1326 mmc->tran_speed = 52000000;
1327 } else if (mmc->card_caps & MMC_MODE_HS) {
1328 mmc_select_mode(mmc, MMC_HS);
1329 mmc->tran_speed = 26000000;
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001330 }
1331
1332 return err;
1333}
1334
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001335static int mmc_startup_v4(struct mmc *mmc)
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001336{
1337 int err, i;
1338 u64 capacity;
1339 bool has_parts = false;
1340 bool part_completed;
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001341 u8 *ext_csd;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001342
1343 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
1344 return 0;
1345
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001346 ext_csd = malloc_cache_aligned(MMC_MAX_BLOCK_LEN);
1347 if (!ext_csd)
1348 return -ENOMEM;
1349
1350 mmc->ext_csd = ext_csd;
1351
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001352 /* check ext_csd version and capacity */
1353 err = mmc_send_ext_csd(mmc, ext_csd);
1354 if (err)
1355 return err;
1356 if (ext_csd[EXT_CSD_REV] >= 2) {
1357 /*
1358 * According to the JEDEC Standard, the value of
1359 * ext_csd's capacity is valid if the value is more
1360 * than 2GB
1361 */
1362 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1363 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1364 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1365 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1366 capacity *= MMC_MAX_BLOCK_LEN;
1367 if ((capacity >> 20) > 2 * 1024)
1368 mmc->capacity_user = capacity;
1369 }
1370
1371 switch (ext_csd[EXT_CSD_REV]) {
1372 case 1:
1373 mmc->version = MMC_VERSION_4_1;
1374 break;
1375 case 2:
1376 mmc->version = MMC_VERSION_4_2;
1377 break;
1378 case 3:
1379 mmc->version = MMC_VERSION_4_3;
1380 break;
1381 case 5:
1382 mmc->version = MMC_VERSION_4_41;
1383 break;
1384 case 6:
1385 mmc->version = MMC_VERSION_4_5;
1386 break;
1387 case 7:
1388 mmc->version = MMC_VERSION_5_0;
1389 break;
1390 case 8:
1391 mmc->version = MMC_VERSION_5_1;
1392 break;
1393 }
1394
1395 /* The partition data may be non-zero but it is only
1396 * effective if PARTITION_SETTING_COMPLETED is set in
1397 * EXT_CSD, so ignore any data if this bit is not set,
1398 * except for enabling the high-capacity group size
1399 * definition (see below).
1400 */
1401 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
1402 EXT_CSD_PARTITION_SETTING_COMPLETED);
1403
1404 /* store the partition info of emmc */
1405 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
1406 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1407 ext_csd[EXT_CSD_BOOT_MULT])
1408 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1409 if (part_completed &&
1410 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
1411 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
1412
1413 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1414
1415 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1416
1417 for (i = 0; i < 4; i++) {
1418 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1419 uint mult = (ext_csd[idx + 2] << 16) +
1420 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1421 if (mult)
1422 has_parts = true;
1423 if (!part_completed)
1424 continue;
1425 mmc->capacity_gp[i] = mult;
1426 mmc->capacity_gp[i] *=
1427 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1428 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1429 mmc->capacity_gp[i] <<= 19;
1430 }
1431
1432 if (part_completed) {
1433 mmc->enh_user_size =
1434 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
1435 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
1436 ext_csd[EXT_CSD_ENH_SIZE_MULT];
1437 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1438 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1439 mmc->enh_user_size <<= 19;
1440 mmc->enh_user_start =
1441 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
1442 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
1443 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
1444 ext_csd[EXT_CSD_ENH_START_ADDR];
1445 if (mmc->high_capacity)
1446 mmc->enh_user_start <<= 9;
1447 }
1448
1449 /*
1450 * Host needs to enable ERASE_GRP_DEF bit if device is
1451 * partitioned. This bit will be lost every time after a reset
1452 * or power off. This will affect erase size.
1453 */
1454 if (part_completed)
1455 has_parts = true;
1456 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
1457 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
1458 has_parts = true;
1459 if (has_parts) {
1460 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1461 EXT_CSD_ERASE_GROUP_DEF, 1);
1462
1463 if (err)
1464 return err;
1465
1466 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1467 }
1468
1469 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
1470 /* Read out group size from ext_csd */
1471 mmc->erase_grp_size =
1472 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1473 /*
1474 * if high capacity and partition setting completed
1475 * SEC_COUNT is valid even if it is smaller than 2 GiB
1476 * JEDEC Standard JESD84-B45, 6.2.4
1477 */
1478 if (mmc->high_capacity && part_completed) {
1479 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1480 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1481 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1482 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1483 capacity *= MMC_MAX_BLOCK_LEN;
1484 mmc->capacity_user = capacity;
1485 }
1486 } else {
1487 /* Calculate the group size from the csd value. */
1488 int erase_gsz, erase_gmul;
1489
1490 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1491 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1492 mmc->erase_grp_size = (erase_gsz + 1)
1493 * (erase_gmul + 1);
1494 }
1495
1496 mmc->hc_wp_grp_size = 1024
1497 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1498 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1499
1500 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1501
1502 return 0;
1503}
1504
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001505static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001506{
Stephen Warrenf866a462013-06-11 15:14:01 -06001507 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05001508 uint mult, freq;
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001509 u64 cmult, csize;
Andy Fleming272cc702008-10-30 16:41:01 -05001510 struct mmc_cmd cmd;
Simon Glassc40fdca2016-05-01 13:52:35 -06001511 struct blk_desc *bdesc;
Andy Fleming272cc702008-10-30 16:41:01 -05001512
Thomas Choud52ebf12010-12-24 13:12:21 +00001513#ifdef CONFIG_MMC_SPI_CRC_ON
1514 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1515 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1516 cmd.resp_type = MMC_RSP_R1;
1517 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001518 err = mmc_send_cmd(mmc, &cmd, NULL);
1519
1520 if (err)
1521 return err;
1522 }
1523#endif
1524
Andy Fleming272cc702008-10-30 16:41:01 -05001525 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001526 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1527 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05001528 cmd.resp_type = MMC_RSP_R2;
1529 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001530
1531 err = mmc_send_cmd(mmc, &cmd, NULL);
1532
1533 if (err)
1534 return err;
1535
1536 memcpy(mmc->cid, cmd.response, 16);
1537
1538 /*
1539 * For MMC cards, set the Relative Address.
1540 * For SD cards, get the Relatvie Address.
1541 * This also puts the cards into Standby State
1542 */
Thomas Choud52ebf12010-12-24 13:12:21 +00001543 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1544 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1545 cmd.cmdarg = mmc->rca << 16;
1546 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05001547
Thomas Choud52ebf12010-12-24 13:12:21 +00001548 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001549
Thomas Choud52ebf12010-12-24 13:12:21 +00001550 if (err)
1551 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001552
Thomas Choud52ebf12010-12-24 13:12:21 +00001553 if (IS_SD(mmc))
1554 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1555 }
Andy Fleming272cc702008-10-30 16:41:01 -05001556
1557 /* Get the Card-Specific Data */
1558 cmd.cmdidx = MMC_CMD_SEND_CSD;
1559 cmd.resp_type = MMC_RSP_R2;
1560 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001561
1562 err = mmc_send_cmd(mmc, &cmd, NULL);
1563
1564 if (err)
1565 return err;
1566
Rabin Vincent998be3d2009-04-05 13:30:56 +05301567 mmc->csd[0] = cmd.response[0];
1568 mmc->csd[1] = cmd.response[1];
1569 mmc->csd[2] = cmd.response[2];
1570 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05001571
1572 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301573 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05001574
1575 switch (version) {
Bin Meng53e8e402016-03-17 21:53:13 -07001576 case 0:
1577 mmc->version = MMC_VERSION_1_2;
1578 break;
1579 case 1:
1580 mmc->version = MMC_VERSION_1_4;
1581 break;
1582 case 2:
1583 mmc->version = MMC_VERSION_2_2;
1584 break;
1585 case 3:
1586 mmc->version = MMC_VERSION_3;
1587 break;
1588 case 4:
1589 mmc->version = MMC_VERSION_4;
1590 break;
1591 default:
1592 mmc->version = MMC_VERSION_1_2;
1593 break;
Andy Fleming272cc702008-10-30 16:41:01 -05001594 }
1595 }
1596
1597 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301598 freq = fbase[(cmd.response[0] & 0x7)];
1599 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05001600
Jean-Jacques Hiblot35f9e192017-09-21 16:29:53 +02001601 mmc->legacy_speed = freq * mult;
1602 mmc->tran_speed = mmc->legacy_speed;
1603 mmc_select_mode(mmc, MMC_LEGACY);
Andy Fleming272cc702008-10-30 16:41:01 -05001604
Markus Niebelab711882013-12-16 13:40:46 +01001605 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05301606 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001607
1608 if (IS_SD(mmc))
1609 mmc->write_bl_len = mmc->read_bl_len;
1610 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05301611 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001612
1613 if (mmc->high_capacity) {
1614 csize = (mmc->csd[1] & 0x3f) << 16
1615 | (mmc->csd[2] & 0xffff0000) >> 16;
1616 cmult = 8;
1617 } else {
1618 csize = (mmc->csd[1] & 0x3ff) << 2
1619 | (mmc->csd[2] & 0xc0000000) >> 30;
1620 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1621 }
1622
Stephen Warrenf866a462013-06-11 15:14:01 -06001623 mmc->capacity_user = (csize + 1) << (cmult + 2);
1624 mmc->capacity_user *= mmc->read_bl_len;
1625 mmc->capacity_boot = 0;
1626 mmc->capacity_rpmb = 0;
1627 for (i = 0; i < 4; i++)
1628 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001629
Simon Glass8bfa1952013-04-03 08:54:30 +00001630 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1631 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001632
Simon Glass8bfa1952013-04-03 08:54:30 +00001633 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1634 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001635
Markus Niebelab711882013-12-16 13:40:46 +01001636 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
1637 cmd.cmdidx = MMC_CMD_SET_DSR;
1638 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
1639 cmd.resp_type = MMC_RSP_NONE;
1640 if (mmc_send_cmd(mmc, &cmd, NULL))
1641 printf("MMC: SET_DSR failed\n");
1642 }
1643
Andy Fleming272cc702008-10-30 16:41:01 -05001644 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001645 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1646 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00001647 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001648 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00001649 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001650
Thomas Choud52ebf12010-12-24 13:12:21 +00001651 if (err)
1652 return err;
1653 }
Andy Fleming272cc702008-10-30 16:41:01 -05001654
Lei Wene6f99a52011-06-22 17:03:31 +00001655 /*
1656 * For SD, its erase group is always one sector
1657 */
1658 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00001659 mmc->part_config = MMCPART_NOAVAILABLE;
Lei Wenbc897b12011-05-02 16:26:26 +00001660
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001661 err = mmc_startup_v4(mmc);
Jean-Jacques Hiblotc744b6f2017-09-21 16:29:50 +02001662 if (err)
1663 return err;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301664
Simon Glassc40fdca2016-05-01 13:52:35 -06001665 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrenf866a462013-06-11 15:14:01 -06001666 if (err)
1667 return err;
1668
Andy Fleming272cc702008-10-30 16:41:01 -05001669 if (IS_SD(mmc))
Jean-Jacques Hiblot8ac8a262017-09-21 16:29:49 +02001670 err = sd_select_bus_freq_width(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001671 else
Jean-Jacques Hiblotdfda9d82017-09-21 16:29:51 +02001672 err = mmc_select_bus_freq_width(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001673
1674 if (err)
1675 return err;
1676
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001677 mmc_set_clock(mmc, mmc->tran_speed);
1678
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06001679 /* Fix the block length for DDR mode */
1680 if (mmc->ddr_mode) {
1681 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1682 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1683 }
1684
Andy Fleming272cc702008-10-30 16:41:01 -05001685 /* fill in device description */
Simon Glassc40fdca2016-05-01 13:52:35 -06001686 bdesc = mmc_get_blk_desc(mmc);
1687 bdesc->lun = 0;
1688 bdesc->hwpart = 0;
1689 bdesc->type = 0;
1690 bdesc->blksz = mmc->read_bl_len;
1691 bdesc->log2blksz = LOG2(bdesc->blksz);
1692 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01001693#if !defined(CONFIG_SPL_BUILD) || \
1694 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
1695 !defined(CONFIG_USE_TINY_PRINTF))
Simon Glassc40fdca2016-05-01 13:52:35 -06001696 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Huttbabce5f2012-10-20 17:15:59 +00001697 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1698 (mmc->cid[3] >> 16) & 0xffff);
Simon Glassc40fdca2016-05-01 13:52:35 -06001699 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Huttbabce5f2012-10-20 17:15:59 +00001700 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1701 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1702 (mmc->cid[2] >> 24) & 0xff);
Simon Glassc40fdca2016-05-01 13:52:35 -06001703 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Huttbabce5f2012-10-20 17:15:59 +00001704 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01001705#else
Simon Glassc40fdca2016-05-01 13:52:35 -06001706 bdesc->vendor[0] = 0;
1707 bdesc->product[0] = 0;
1708 bdesc->revision[0] = 0;
Paul Burton56196822013-09-04 16:12:25 +01001709#endif
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001710#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Simon Glassc40fdca2016-05-01 13:52:35 -06001711 part_init(bdesc);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001712#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001713
1714 return 0;
1715}
1716
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001717static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001718{
1719 struct mmc_cmd cmd;
1720 int err;
1721
1722 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1723 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001724 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05001725 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05001726
1727 err = mmc_send_cmd(mmc, &cmd, NULL);
1728
1729 if (err)
1730 return err;
1731
Rabin Vincent998be3d2009-04-05 13:30:56 +05301732 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001733 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05001734 else
1735 mmc->version = SD_VERSION_2;
1736
1737 return 0;
1738}
1739
Simon Glassc4d660d2017-07-04 13:31:19 -06001740#if !CONFIG_IS_ENABLED(DM_MMC)
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001741/* board-specific MMC power initializations. */
1742__weak void board_mmc_power_init(void)
1743{
1744}
Simon Glass05cbeb72017-04-22 19:10:56 -06001745#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001746
Peng Fan2051aef2016-10-11 15:08:43 +08001747static int mmc_power_init(struct mmc *mmc)
1748{
Simon Glassc4d660d2017-07-04 13:31:19 -06001749#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02001750#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan2051aef2016-10-11 15:08:43 +08001751 int ret;
1752
1753 ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02001754 &mmc->vmmc_supply);
1755 if (ret)
Jaehoon Chung288db7c2016-10-24 15:22:22 +09001756 debug("%s: No vmmc supply\n", mmc->dev->name);
Peng Fan2051aef2016-10-11 15:08:43 +08001757
Jean-Jacques Hiblot06ec0452017-09-21 16:29:48 +02001758 ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
1759 &mmc->vqmmc_supply);
1760 if (ret)
1761 debug("%s: No vqmmc supply\n", mmc->dev->name);
1762
1763 if (mmc->vmmc_supply) {
1764 ret = regulator_set_enable(mmc->vmmc_supply, true);
1765 if (ret) {
1766 puts("Error enabling VMMC supply\n");
1767 return ret;
1768 }
Peng Fan2051aef2016-10-11 15:08:43 +08001769 }
1770#endif
Simon Glass05cbeb72017-04-22 19:10:56 -06001771#else /* !CONFIG_DM_MMC */
1772 /*
1773 * Driver model should use a regulator, as above, rather than calling
1774 * out to board code.
1775 */
1776 board_mmc_power_init();
1777#endif
Peng Fan2051aef2016-10-11 15:08:43 +08001778 return 0;
1779}
1780
Che-Liang Chioue9550442012-11-28 15:21:13 +00001781int mmc_start_init(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001782{
Simon Glass8ca51e52016-06-12 23:30:22 -06001783 bool no_card;
Macpaul Linafd59322011-11-14 23:35:39 +00001784 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05001785
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001786 /* we pretend there's no card when init is NULL */
Simon Glass8ca51e52016-06-12 23:30:22 -06001787 no_card = mmc_getcd(mmc) == 0;
Simon Glasse7881d82017-07-29 11:35:31 -06001788#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06001789 no_card = no_card || (mmc->cfg->ops->init == NULL);
1790#endif
1791 if (no_card) {
Thierry Reding48972d92012-01-02 01:15:37 +00001792 mmc->has_init = 0;
Paul Burton56196822013-09-04 16:12:25 +01001793#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Thierry Reding48972d92012-01-02 01:15:37 +00001794 printf("MMC: no card present\n");
Paul Burton56196822013-09-04 16:12:25 +01001795#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001796 return -ENOMEDIUM;
Thierry Reding48972d92012-01-02 01:15:37 +00001797 }
1798
Lei Wenbc897b12011-05-02 16:26:26 +00001799 if (mmc->has_init)
1800 return 0;
1801
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001802#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1803 mmc_adapter_card_type_ident();
1804#endif
Peng Fan2051aef2016-10-11 15:08:43 +08001805 err = mmc_power_init(mmc);
1806 if (err)
1807 return err;
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001808
Simon Glasse7881d82017-07-29 11:35:31 -06001809#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8ca51e52016-06-12 23:30:22 -06001810 /* The device has already been probed ready for use */
1811#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001812 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001813 err = mmc->cfg->ops->init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001814 if (err)
1815 return err;
Simon Glass8ca51e52016-06-12 23:30:22 -06001816#endif
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001817 mmc->ddr_mode = 0;
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001818 mmc_set_bus_width(mmc, 1);
1819 mmc_set_clock(mmc, 1);
1820
Andy Fleming272cc702008-10-30 16:41:01 -05001821 /* Reset the Card */
1822 err = mmc_go_idle(mmc);
1823
1824 if (err)
1825 return err;
1826
Lei Wenbc897b12011-05-02 16:26:26 +00001827 /* The internal partition reset to user partition(0) at every CMD0*/
Simon Glassc40fdca2016-05-01 13:52:35 -06001828 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wenbc897b12011-05-02 16:26:26 +00001829
Andy Fleming272cc702008-10-30 16:41:01 -05001830 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00001831 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001832
Andy Fleming272cc702008-10-30 16:41:01 -05001833 /* Now try to get the SD card's operating condition */
1834 err = sd_send_op_cond(mmc);
1835
1836 /* If the command timed out, we check for an MMC card */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001837 if (err == -ETIMEDOUT) {
Andy Fleming272cc702008-10-30 16:41:01 -05001838 err = mmc_send_op_cond(mmc);
1839
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001840 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01001841#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -05001842 printf("Card did not respond to voltage select!\n");
Paul Burton56196822013-09-04 16:12:25 +01001843#endif
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001844 return -EOPNOTSUPP;
Andy Fleming272cc702008-10-30 16:41:01 -05001845 }
1846 }
1847
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001848 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00001849 mmc->init_in_progress = 1;
1850
1851 return err;
1852}
1853
1854static int mmc_complete_init(struct mmc *mmc)
1855{
1856 int err = 0;
1857
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001858 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001859 if (mmc->op_cond_pending)
1860 err = mmc_complete_op_cond(mmc);
1861
1862 if (!err)
1863 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00001864 if (err)
1865 mmc->has_init = 0;
1866 else
1867 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001868 return err;
1869}
1870
1871int mmc_init(struct mmc *mmc)
1872{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001873 int err = 0;
Marek Vasutce9eca92016-12-01 02:06:32 +01001874 __maybe_unused unsigned start;
Simon Glassc4d660d2017-07-04 13:31:19 -06001875#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass33fb2112016-05-01 13:52:41 -06001876 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chioue9550442012-11-28 15:21:13 +00001877
Simon Glass33fb2112016-05-01 13:52:41 -06001878 upriv->mmc = mmc;
1879#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00001880 if (mmc->has_init)
1881 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02001882
1883 start = get_timer(0);
1884
Che-Liang Chioue9550442012-11-28 15:21:13 +00001885 if (!mmc->init_in_progress)
1886 err = mmc_start_init(mmc);
1887
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001888 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00001889 err = mmc_complete_init(mmc);
Jagan Teki919b4852017-01-10 11:18:43 +01001890 if (err)
1891 printf("%s: %d, time %lu\n", __func__, err, get_timer(start));
1892
Lei Wenbc897b12011-05-02 16:26:26 +00001893 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001894}
1895
Markus Niebelab711882013-12-16 13:40:46 +01001896int mmc_set_dsr(struct mmc *mmc, u16 val)
1897{
1898 mmc->dsr = val;
1899 return 0;
1900}
1901
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02001902/* CPU-specific MMC initializations */
1903__weak int cpu_mmc_init(bd_t *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05001904{
1905 return -1;
1906}
1907
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02001908/* board-specific MMC initializations. */
1909__weak int board_mmc_init(bd_t *bis)
1910{
1911 return -1;
1912}
Andy Fleming272cc702008-10-30 16:41:01 -05001913
Che-Liang Chioue9550442012-11-28 15:21:13 +00001914void mmc_set_preinit(struct mmc *mmc, int preinit)
1915{
1916 mmc->preinit = preinit;
1917}
1918
Simon Glassc4d660d2017-07-04 13:31:19 -06001919#if CONFIG_IS_ENABLED(DM_MMC) && defined(CONFIG_SPL_BUILD)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001920static int mmc_probe(bd_t *bis)
1921{
1922 return 0;
1923}
Simon Glassc4d660d2017-07-04 13:31:19 -06001924#elif CONFIG_IS_ENABLED(DM_MMC)
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001925static int mmc_probe(bd_t *bis)
1926{
Simon Glass4a1db6d2015-12-29 05:22:49 -07001927 int ret, i;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001928 struct uclass *uc;
Simon Glass4a1db6d2015-12-29 05:22:49 -07001929 struct udevice *dev;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001930
1931 ret = uclass_get(UCLASS_MMC, &uc);
1932 if (ret)
1933 return ret;
1934
Simon Glass4a1db6d2015-12-29 05:22:49 -07001935 /*
1936 * Try to add them in sequence order. Really with driver model we
1937 * should allow holes, but the current MMC list does not allow that.
1938 * So if we request 0, 1, 3 we will get 0, 1, 2.
1939 */
1940 for (i = 0; ; i++) {
1941 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
1942 if (ret == -ENODEV)
1943 break;
1944 }
1945 uclass_foreach_dev(dev, uc) {
1946 ret = device_probe(dev);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001947 if (ret)
Simon Glass4a1db6d2015-12-29 05:22:49 -07001948 printf("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001949 }
1950
1951 return 0;
1952}
1953#else
1954static int mmc_probe(bd_t *bis)
1955{
1956 if (board_mmc_init(bis) < 0)
1957 cpu_mmc_init(bis);
1958
1959 return 0;
1960}
1961#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00001962
Andy Fleming272cc702008-10-30 16:41:01 -05001963int mmc_initialize(bd_t *bis)
1964{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02001965 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001966 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02001967 if (initialized) /* Avoid initializing mmc multiple times */
1968 return 0;
1969 initialized = 1;
1970
Simon Glassc4d660d2017-07-04 13:31:19 -06001971#if !CONFIG_IS_ENABLED(BLK)
Marek Vasutb5b838f2016-12-01 02:06:33 +01001972#if !CONFIG_IS_ENABLED(MMC_TINY)
Simon Glassc40fdca2016-05-01 13:52:35 -06001973 mmc_list_init();
1974#endif
Marek Vasutb5b838f2016-12-01 02:06:33 +01001975#endif
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001976 ret = mmc_probe(bis);
1977 if (ret)
1978 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001979
Ying Zhangbb0dc102013-08-16 15:16:11 +08001980#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05001981 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08001982#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001983
Simon Glassc40fdca2016-05-01 13:52:35 -06001984 mmc_do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05001985 return 0;
1986}
Tomas Melincd3d4882016-11-25 11:01:03 +02001987
1988#ifdef CONFIG_CMD_BKOPS_ENABLE
1989int mmc_set_bkops_enable(struct mmc *mmc)
1990{
1991 int err;
1992 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1993
1994 err = mmc_send_ext_csd(mmc, ext_csd);
1995 if (err) {
1996 puts("Could not get ext_csd register values\n");
1997 return err;
1998 }
1999
2000 if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
2001 puts("Background operations not supported on device\n");
2002 return -EMEDIUMTYPE;
2003 }
2004
2005 if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
2006 puts("Background operations already enabled\n");
2007 return 0;
2008 }
2009
2010 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
2011 if (err) {
2012 puts("Failed to enable manual background operations\n");
2013 return err;
2014 }
2015
2016 puts("Enabled manual background operations\n");
2017
2018 return 0;
2019}
2020#endif