blob: 43ea0bba763e4168fbef8f80a40cfa23e76f3e70 [file] [log] [blame]
Andy Flemingad347bb2008-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 Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Andy Flemingad347bb2008-10-30 16:41:01 -05008 */
9
10#include <config.h>
11#include <common.h>
12#include <command.h>
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -060013#include <dm.h>
14#include <dm/device-internal.h>
Stephen Warrenbf0c7852014-05-23 12:47:06 -060015#include <errno.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050016#include <mmc.h>
17#include <part.h>
18#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060019#include <memalign.h>
Andy Flemingad347bb2008-10-30 16:41:01 -050020#include <linux/list.h>
Rabin Vincent69d4e2c2009-04-05 13:30:54 +053021#include <div64.h>
Paul Burton8d30cc92013-09-09 15:30:26 +010022#include "mmc_private.h"
Andy Flemingad347bb2008-10-30 16:41:01 -050023
Simon Glass394dfc02016-06-12 23:30:22 -060024#ifndef CONFIG_DM_MMC_OPS
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +020025__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanov020f2612012-12-03 02:19:46 +000026{
27 return -1;
28}
29
30int mmc_getwp(struct mmc *mmc)
31{
32 int wp;
33
34 wp = board_mmc_getwp(mmc);
35
Peter Korsgaardf7b15102013-03-21 04:00:03 +000036 if (wp < 0) {
Pantelis Antoniou2c850462014-03-11 19:34:20 +020037 if (mmc->cfg->ops->getwp)
38 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardf7b15102013-03-21 04:00:03 +000039 else
40 wp = 0;
41 }
Nikita Kiryanov020f2612012-12-03 02:19:46 +000042
43 return wp;
44}
45
Jeroen Hofstee47726302014-07-10 22:46:28 +020046__weak int board_mmc_getcd(struct mmc *mmc)
47{
Stefano Babic6e00edf2010-02-05 15:04:43 +010048 return -1;
49}
Simon Glass394dfc02016-06-12 23:30:22 -060050#endif
Stefano Babic6e00edf2010-02-05 15:04:43 +010051
Simon Glassb23d96e2016-06-12 23:30:20 -060052#ifdef CONFIG_MMC_TRACE
53void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
Andy Flemingad347bb2008-10-30 16:41:01 -050054{
Simon Glassb23d96e2016-06-12 23:30:20 -060055 printf("CMD_SEND:%d\n", cmd->cmdidx);
56 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
57}
Marek Vasutdccb6082012-03-15 18:41:35 +000058
Simon Glassb23d96e2016-06-12 23:30:20 -060059void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
60{
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000061 int i;
62 u8 *ptr;
63
Bin Meng8d1ad1e2016-03-17 21:53:14 -070064 if (ret) {
65 printf("\t\tRET\t\t\t %d\n", ret);
66 } else {
67 switch (cmd->resp_type) {
68 case MMC_RSP_NONE:
69 printf("\t\tMMC_RSP_NONE\n");
70 break;
71 case MMC_RSP_R1:
72 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
73 cmd->response[0]);
74 break;
75 case MMC_RSP_R1b:
76 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
77 cmd->response[0]);
78 break;
79 case MMC_RSP_R2:
80 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
81 cmd->response[0]);
82 printf("\t\t \t\t 0x%08X \n",
83 cmd->response[1]);
84 printf("\t\t \t\t 0x%08X \n",
85 cmd->response[2]);
86 printf("\t\t \t\t 0x%08X \n",
87 cmd->response[3]);
Raffaele Recalcati894b1e22011-03-11 02:01:14 +000088 printf("\n");
Bin Meng8d1ad1e2016-03-17 21:53:14 -070089 printf("\t\t\t\t\tDUMPING DATA\n");
90 for (i = 0; i < 4; i++) {
91 int j;
92 printf("\t\t\t\t\t%03d - ", i*4);
93 ptr = (u8 *)&cmd->response[i];
94 ptr += 3;
95 for (j = 0; j < 4; j++)
96 printf("%02X ", *ptr--);
97 printf("\n");
98 }
99 break;
100 case MMC_RSP_R3:
101 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
102 cmd->response[0]);
103 break;
104 default:
105 printf("\t\tERROR MMC rsp not supported\n");
106 break;
Bin Meng4a4ef872016-03-17 21:53:13 -0700107 }
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000108 }
Simon Glassb23d96e2016-06-12 23:30:20 -0600109}
110
111void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
112{
113 int status;
114
115 status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
116 printf("CURR STATE:%d\n", status);
117}
Raffaele Recalcati894b1e22011-03-11 02:01:14 +0000118#endif
Simon Glassb23d96e2016-06-12 23:30:20 -0600119
Simon Glass394dfc02016-06-12 23:30:22 -0600120#ifndef CONFIG_DM_MMC_OPS
Simon Glassb23d96e2016-06-12 23:30:20 -0600121int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
122{
123 int ret;
124
125 mmmc_trace_before_send(mmc, cmd);
126 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
127 mmmc_trace_after_send(mmc, cmd, ret);
128
Marek Vasutdccb6082012-03-15 18:41:35 +0000129 return ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500130}
Simon Glass394dfc02016-06-12 23:30:22 -0600131#endif
Andy Flemingad347bb2008-10-30 16:41:01 -0500132
Paul Burton8d30cc92013-09-09 15:30:26 +0100133int mmc_send_status(struct mmc *mmc, int timeout)
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000134{
135 struct mmc_cmd cmd;
Jan Kloetzke31789322012-02-05 22:29:12 +0000136 int err, retries = 5;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000137
138 cmd.cmdidx = MMC_CMD_SEND_STATUS;
139 cmd.resp_type = MMC_RSP_R1;
Marek Vasutc4427392011-08-10 09:24:48 +0200140 if (!mmc_host_is_spi(mmc))
141 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000142
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500143 while (1) {
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000144 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzke31789322012-02-05 22:29:12 +0000145 if (!err) {
146 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
147 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
148 MMC_STATE_PRG)
149 break;
150 else if (cmd.response[0] & MMC_STATUS_MASK) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100151#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jan Kloetzke31789322012-02-05 22:29:12 +0000152 printf("Status Error: 0x%08X\n",
153 cmd.response[0]);
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100154#endif
Jaehoon Chung7825d202016-07-19 16:33:36 +0900155 return -ECOMM;
Jan Kloetzke31789322012-02-05 22:29:12 +0000156 }
157 } else if (--retries < 0)
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000158 return err;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000159
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500160 if (timeout-- <= 0)
161 break;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000162
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500163 udelay(1000);
164 }
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000165
Simon Glassb23d96e2016-06-12 23:30:20 -0600166 mmc_trace_state(mmc, &cmd);
Jongman Heo1be00d92012-06-03 21:32:13 +0000167 if (timeout <= 0) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100168#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000169 printf("Timeout waiting card ready\n");
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100170#endif
Jaehoon Chung7825d202016-07-19 16:33:36 +0900171 return -ETIMEDOUT;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000172 }
173
174 return 0;
175}
176
Paul Burton8d30cc92013-09-09 15:30:26 +0100177int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Flemingad347bb2008-10-30 16:41:01 -0500178{
179 struct mmc_cmd cmd;
180
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -0600181 if (mmc->ddr_mode)
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900182 return 0;
183
Andy Flemingad347bb2008-10-30 16:41:01 -0500184 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
185 cmd.resp_type = MMC_RSP_R1;
186 cmd.cmdarg = len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500187
188 return mmc_send_cmd(mmc, &cmd, NULL);
189}
190
Sascha Silbe4bdf6fd2013-06-14 13:07:25 +0200191static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillips87ea3892012-10-29 13:34:43 +0000192 lbaint_t blkcnt)
Andy Flemingad347bb2008-10-30 16:41:01 -0500193{
194 struct mmc_cmd cmd;
195 struct mmc_data data;
196
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700197 if (blkcnt > 1)
198 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
199 else
200 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Flemingad347bb2008-10-30 16:41:01 -0500201
202 if (mmc->high_capacity)
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700203 cmd.cmdarg = start;
Andy Flemingad347bb2008-10-30 16:41:01 -0500204 else
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700205 cmd.cmdarg = start * mmc->read_bl_len;
Andy Flemingad347bb2008-10-30 16:41:01 -0500206
207 cmd.resp_type = MMC_RSP_R1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500208
209 data.dest = dst;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700210 data.blocks = blkcnt;
Andy Flemingad347bb2008-10-30 16:41:01 -0500211 data.blocksize = mmc->read_bl_len;
212 data.flags = MMC_DATA_READ;
213
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700214 if (mmc_send_cmd(mmc, &cmd, &data))
215 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500216
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700217 if (blkcnt > 1) {
218 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
219 cmd.cmdarg = 0;
220 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700221 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100222#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700223 printf("mmc fail to send stop cmd\n");
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100224#endif
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700225 return 0;
226 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500227 }
228
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700229 return blkcnt;
Andy Flemingad347bb2008-10-30 16:41:01 -0500230}
231
Simon Glass59bc6f22016-05-01 13:52:41 -0600232#ifdef CONFIG_BLK
Simon Glass62e293a2016-06-12 23:30:15 -0600233ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
Simon Glass59bc6f22016-05-01 13:52:41 -0600234#else
Simon Glass62e293a2016-06-12 23:30:15 -0600235ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
236 void *dst)
Simon Glass59bc6f22016-05-01 13:52:41 -0600237#endif
Andy Flemingad347bb2008-10-30 16:41:01 -0500238{
Simon Glass59bc6f22016-05-01 13:52:41 -0600239#ifdef CONFIG_BLK
240 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
241#endif
Simon Glass2f26fff2016-02-29 15:25:51 -0700242 int dev_num = block_dev->devnum;
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700243 int err;
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700244 lbaint_t cur, blocks_todo = blkcnt;
245
246 if (blkcnt == 0)
247 return 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500248
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700249 struct mmc *mmc = find_mmc_device(dev_num);
Andy Flemingad347bb2008-10-30 16:41:01 -0500250 if (!mmc)
251 return 0;
252
Simon Glass11f2bb62016-05-01 13:52:29 -0600253 err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700254 if (err < 0)
255 return 0;
256
Simon Glasse5db1152016-05-01 13:52:35 -0600257 if ((start + blkcnt) > block_dev->lba) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100258#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Sascha Silbe4bdf6fd2013-06-14 13:07:25 +0200259 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Simon Glasse5db1152016-05-01 13:52:35 -0600260 start + blkcnt, block_dev->lba);
Paul Burton6a7c5ba2013-09-04 16:12:25 +0100261#endif
Lei Wene1cc9c82010-09-13 22:07:27 +0800262 return 0;
263 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500264
Simon Glassa4343c42015-06-23 15:38:50 -0600265 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
266 debug("%s: Failed to set blocklen\n", __func__);
Andy Flemingad347bb2008-10-30 16:41:01 -0500267 return 0;
Simon Glassa4343c42015-06-23 15:38:50 -0600268 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500269
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700270 do {
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200271 cur = (blocks_todo > mmc->cfg->b_max) ?
272 mmc->cfg->b_max : blocks_todo;
Simon Glassa4343c42015-06-23 15:38:50 -0600273 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
274 debug("%s: Failed to read blocks\n", __func__);
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700275 return 0;
Simon Glassa4343c42015-06-23 15:38:50 -0600276 }
Alagu Sankarc25d1b92010-10-25 07:23:56 -0700277 blocks_todo -= cur;
278 start += cur;
279 dst += cur * mmc->read_bl_len;
280 } while (blocks_todo > 0);
Andy Flemingad347bb2008-10-30 16:41:01 -0500281
282 return blkcnt;
283}
284
Kim Phillips87ea3892012-10-29 13:34:43 +0000285static int mmc_go_idle(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500286{
287 struct mmc_cmd cmd;
288 int err;
289
290 udelay(1000);
291
292 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
293 cmd.cmdarg = 0;
294 cmd.resp_type = MMC_RSP_NONE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500295
296 err = mmc_send_cmd(mmc, &cmd, NULL);
297
298 if (err)
299 return err;
300
301 udelay(2000);
302
303 return 0;
304}
305
Kim Phillips87ea3892012-10-29 13:34:43 +0000306static int sd_send_op_cond(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500307{
308 int timeout = 1000;
309 int err;
310 struct mmc_cmd cmd;
311
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500312 while (1) {
Andy Flemingad347bb2008-10-30 16:41:01 -0500313 cmd.cmdidx = MMC_CMD_APP_CMD;
314 cmd.resp_type = MMC_RSP_R1;
315 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500316
317 err = mmc_send_cmd(mmc, &cmd, NULL);
318
319 if (err)
320 return err;
321
322 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
323 cmd.resp_type = MMC_RSP_R3;
Stefano Babicf8e9a212010-01-20 18:20:39 +0100324
325 /*
326 * Most cards do not answer if some reserved bits
327 * in the ocr are set. However, Some controller
328 * can set bit 7 (reserved for low voltages), but
329 * how to manage low voltages SD card is not yet
330 * specified.
331 */
Thomas Chou1254c3d2010-12-24 13:12:21 +0000332 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200333 (mmc->cfg->voltages & 0xff8000);
Andy Flemingad347bb2008-10-30 16:41:01 -0500334
335 if (mmc->version == SD_VERSION_2)
336 cmd.cmdarg |= OCR_HCS;
337
338 err = mmc_send_cmd(mmc, &cmd, NULL);
339
340 if (err)
341 return err;
342
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500343 if (cmd.response[0] & OCR_BUSY)
344 break;
Andy Flemingad347bb2008-10-30 16:41:01 -0500345
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500346 if (timeout-- <= 0)
Jaehoon Chung7825d202016-07-19 16:33:36 +0900347 return -EOPNOTSUPP;
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500348
349 udelay(1000);
350 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500351
352 if (mmc->version != SD_VERSION_2)
353 mmc->version = SD_VERSION_1_0;
354
Thomas Chou1254c3d2010-12-24 13:12:21 +0000355 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
356 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
357 cmd.resp_type = MMC_RSP_R3;
358 cmd.cmdarg = 0;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000359
360 err = mmc_send_cmd(mmc, &cmd, NULL);
361
362 if (err)
363 return err;
364 }
365
Rabin Vincentb6eed942009-04-05 13:30:56 +0530366 mmc->ocr = cmd.response[0];
Andy Flemingad347bb2008-10-30 16:41:01 -0500367
368 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
369 mmc->rca = 0;
370
371 return 0;
372}
373
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500374static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Flemingad347bb2008-10-30 16:41:01 -0500375{
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500376 struct mmc_cmd cmd;
Andy Flemingad347bb2008-10-30 16:41:01 -0500377 int err;
378
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500379 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
380 cmd.resp_type = MMC_RSP_R3;
381 cmd.cmdarg = 0;
Rob Herring5fd3edd2015-03-23 17:56:59 -0500382 if (use_arg && !mmc_host_is_spi(mmc))
383 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200384 (mmc->cfg->voltages &
Andrew Gabbasovec600d12015-03-19 07:44:03 -0500385 (mmc->ocr & OCR_VOLTAGE_MASK)) |
386 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000387
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500388 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000389 if (err)
390 return err;
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500391 mmc->ocr = cmd.response[0];
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000392 return 0;
393}
394
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +0200395static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000396{
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000397 int err, i;
398
Andy Flemingad347bb2008-10-30 16:41:01 -0500399 /* Some cards seem to need this */
400 mmc_go_idle(mmc);
401
Raffaele Recalcati1df837e2011-03-11 02:01:13 +0000402 /* Asking to the card its capabilities */
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000403 for (i = 0; i < 2; i++) {
Andrew Gabbasovfafa6a02015-03-19 07:44:04 -0500404 err = mmc_send_op_cond_iter(mmc, i != 0);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000405 if (err)
406 return err;
Wolfgang Denk80f70212011-05-19 22:21:41 +0200407
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000408 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasovec600d12015-03-19 07:44:03 -0500409 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -0500410 break;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000411 }
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -0500412 mmc->op_cond_pending = 1;
413 return 0;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000414}
Wolfgang Denk80f70212011-05-19 22:21:41 +0200415
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +0200416static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000417{
418 struct mmc_cmd cmd;
419 int timeout = 1000;
420 uint start;
421 int err;
Wolfgang Denk80f70212011-05-19 22:21:41 +0200422
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +0000423 mmc->op_cond_pending = 0;
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500424 if (!(mmc->ocr & OCR_BUSY)) {
Yangbo Lu9c720612016-08-02 15:33:18 +0800425 /* Some cards seem to need this */
426 mmc_go_idle(mmc);
427
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500428 start = get_timer(0);
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500429 while (1) {
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500430 err = mmc_send_op_cond_iter(mmc, 1);
431 if (err)
432 return err;
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500433 if (mmc->ocr & OCR_BUSY)
434 break;
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500435 if (get_timer(start) > timeout)
Jaehoon Chung7825d202016-07-19 16:33:36 +0900436 return -EOPNOTSUPP;
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500437 udelay(100);
Andrew Gabbasov034857c2015-03-19 07:44:06 -0500438 }
Andrew Gabbasov5a513ca2015-03-19 07:44:05 -0500439 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500440
Thomas Chou1254c3d2010-12-24 13:12:21 +0000441 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
442 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
443 cmd.resp_type = MMC_RSP_R3;
444 cmd.cmdarg = 0;
Thomas Chou1254c3d2010-12-24 13:12:21 +0000445
446 err = mmc_send_cmd(mmc, &cmd, NULL);
447
448 if (err)
449 return err;
Andrew Gabbasovec600d12015-03-19 07:44:03 -0500450
451 mmc->ocr = cmd.response[0];
Thomas Chou1254c3d2010-12-24 13:12:21 +0000452 }
453
Andy Flemingad347bb2008-10-30 16:41:01 -0500454 mmc->version = MMC_VERSION_UNKNOWN;
Andy Flemingad347bb2008-10-30 16:41:01 -0500455
456 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrenf6545f12014-01-30 16:11:12 -0700457 mmc->rca = 1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500458
459 return 0;
460}
461
462
Kim Phillips87ea3892012-10-29 13:34:43 +0000463static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Flemingad347bb2008-10-30 16:41:01 -0500464{
465 struct mmc_cmd cmd;
466 struct mmc_data data;
467 int err;
468
469 /* Get the Card Status Register */
470 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
471 cmd.resp_type = MMC_RSP_R1;
472 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500473
Yoshihiro Shimodaf6bec732012-06-07 19:09:11 +0000474 data.dest = (char *)ext_csd;
Andy Flemingad347bb2008-10-30 16:41:01 -0500475 data.blocks = 1;
Simon Glassa09c2b72013-04-03 08:54:30 +0000476 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Flemingad347bb2008-10-30 16:41:01 -0500477 data.flags = MMC_DATA_READ;
478
479 err = mmc_send_cmd(mmc, &cmd, &data);
480
481 return err;
482}
483
Simon Glass84f9df92016-06-12 23:30:18 -0600484int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Flemingad347bb2008-10-30 16:41:01 -0500485{
486 struct mmc_cmd cmd;
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000487 int timeout = 1000;
488 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500489
490 cmd.cmdidx = MMC_CMD_SWITCH;
491 cmd.resp_type = MMC_RSP_R1b;
492 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000493 (index << 16) |
494 (value << 8);
Andy Flemingad347bb2008-10-30 16:41:01 -0500495
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000496 ret = mmc_send_cmd(mmc, &cmd, NULL);
497
498 /* Waiting for the ready status */
Jan Kloetzke4e929dd2012-02-05 22:29:11 +0000499 if (!ret)
500 ret = mmc_send_status(mmc, timeout);
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +0000501
502 return ret;
503
Andy Flemingad347bb2008-10-30 16:41:01 -0500504}
505
Kim Phillips87ea3892012-10-29 13:34:43 +0000506static int mmc_change_freq(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500507{
Simon Glassa09c2b72013-04-03 08:54:30 +0000508 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Andy Flemingad347bb2008-10-30 16:41:01 -0500509 char cardtype;
510 int err;
511
Andrew Gabbasovccb7b042014-12-25 10:22:25 -0600512 mmc->card_caps = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500513
Thomas Chou1254c3d2010-12-24 13:12:21 +0000514 if (mmc_host_is_spi(mmc))
515 return 0;
516
Andy Flemingad347bb2008-10-30 16:41:01 -0500517 /* Only version 4 supports high-speed */
518 if (mmc->version < MMC_VERSION_4)
519 return 0;
520
Andrew Gabbasovccb7b042014-12-25 10:22:25 -0600521 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
522
Andy Flemingad347bb2008-10-30 16:41:01 -0500523 err = mmc_send_ext_csd(mmc, ext_csd);
524
525 if (err)
526 return err;
527
Lei Wen217467f2011-10-03 20:35:10 +0000528 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Flemingad347bb2008-10-30 16:41:01 -0500529
530 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
531
532 if (err)
Heiko Schocher9016ab92016-06-07 08:31:21 +0200533 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -0500534
535 /* Now check to see that it worked */
536 err = mmc_send_ext_csd(mmc, ext_csd);
537
538 if (err)
539 return err;
540
541 /* No high-speed support */
Lei Wen217467f2011-10-03 20:35:10 +0000542 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Flemingad347bb2008-10-30 16:41:01 -0500543 return 0;
544
545 /* High Speed is set, there are two types: 52MHz and 26MHz */
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900546 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Andrew Gabbasov95a37132014-12-01 06:59:10 -0600547 if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900548 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Andy Flemingad347bb2008-10-30 16:41:01 -0500549 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900550 } else {
Andy Flemingad347bb2008-10-30 16:41:01 -0500551 mmc->card_caps |= MMC_MODE_HS;
Jaehoon Chung38ce30b2014-05-16 13:59:54 +0900552 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500553
554 return 0;
555}
556
Stephen Warrene315ae82013-06-11 15:14:01 -0600557static int mmc_set_capacity(struct mmc *mmc, int part_num)
558{
559 switch (part_num) {
560 case 0:
561 mmc->capacity = mmc->capacity_user;
562 break;
563 case 1:
564 case 2:
565 mmc->capacity = mmc->capacity_boot;
566 break;
567 case 3:
568 mmc->capacity = mmc->capacity_rpmb;
569 break;
570 case 4:
571 case 5:
572 case 6:
573 case 7:
574 mmc->capacity = mmc->capacity_gp[part_num - 4];
575 break;
576 default:
577 return -1;
578 }
579
Simon Glasse5db1152016-05-01 13:52:35 -0600580 mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Stephen Warrene315ae82013-06-11 15:14:01 -0600581
582 return 0;
583}
584
Simon Glass62e293a2016-06-12 23:30:15 -0600585int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
Lei Wen31b99802011-05-02 16:26:26 +0000586{
Stephen Warrene315ae82013-06-11 15:14:01 -0600587 int ret;
Lei Wen31b99802011-05-02 16:26:26 +0000588
Stephen Warrene315ae82013-06-11 15:14:01 -0600589 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
590 (mmc->part_config & ~PART_ACCESS_MASK)
591 | (part_num & PART_ACCESS_MASK));
Peter Bigot45fde892014-09-02 18:31:23 -0500592
593 /*
594 * Set the capacity if the switch succeeded or was intended
595 * to return to representing the raw device.
596 */
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700597 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
Peter Bigot45fde892014-09-02 18:31:23 -0500598 ret = mmc_set_capacity(mmc, part_num);
Simon Glass984db5d2016-05-01 13:52:37 -0600599 mmc_get_blk_desc(mmc)->hwpart = part_num;
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700600 }
Stephen Warrene315ae82013-06-11 15:14:01 -0600601
Peter Bigot45fde892014-09-02 18:31:23 -0500602 return ret;
Lei Wen31b99802011-05-02 16:26:26 +0000603}
604
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +0100605int mmc_hwpart_config(struct mmc *mmc,
606 const struct mmc_hwpart_conf *conf,
607 enum mmc_hwpart_conf_mode mode)
608{
609 u8 part_attrs = 0;
610 u32 enh_size_mult;
611 u32 enh_start_addr;
612 u32 gp_size_mult[4];
613 u32 max_enh_size_mult;
614 u32 tot_enh_size_mult = 0;
Diego Santa Cruz80200272014-12-23 10:50:31 +0100615 u8 wr_rel_set;
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +0100616 int i, pidx, err;
617 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
618
619 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
620 return -EINVAL;
621
622 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
623 printf("eMMC >= 4.4 required for enhanced user data area\n");
624 return -EMEDIUMTYPE;
625 }
626
627 if (!(mmc->part_support & PART_SUPPORT)) {
628 printf("Card does not support partitioning\n");
629 return -EMEDIUMTYPE;
630 }
631
632 if (!mmc->hc_wp_grp_size) {
633 printf("Card does not define HC WP group size\n");
634 return -EMEDIUMTYPE;
635 }
636
637 /* check partition alignment and total enhanced size */
638 if (conf->user.enh_size) {
639 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
640 conf->user.enh_start % mmc->hc_wp_grp_size) {
641 printf("User data enhanced area not HC WP group "
642 "size aligned\n");
643 return -EINVAL;
644 }
645 part_attrs |= EXT_CSD_ENH_USR;
646 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
647 if (mmc->high_capacity) {
648 enh_start_addr = conf->user.enh_start;
649 } else {
650 enh_start_addr = (conf->user.enh_start << 9);
651 }
652 } else {
653 enh_size_mult = 0;
654 enh_start_addr = 0;
655 }
656 tot_enh_size_mult += enh_size_mult;
657
658 for (pidx = 0; pidx < 4; pidx++) {
659 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
660 printf("GP%i partition not HC WP group size "
661 "aligned\n", pidx+1);
662 return -EINVAL;
663 }
664 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
665 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
666 part_attrs |= EXT_CSD_ENH_GP(pidx);
667 tot_enh_size_mult += gp_size_mult[pidx];
668 }
669 }
670
671 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
672 printf("Card does not support enhanced attribute\n");
673 return -EMEDIUMTYPE;
674 }
675
676 err = mmc_send_ext_csd(mmc, ext_csd);
677 if (err)
678 return err;
679
680 max_enh_size_mult =
681 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
682 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
683 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
684 if (tot_enh_size_mult > max_enh_size_mult) {
685 printf("Total enhanced size exceeds maximum (%u > %u)\n",
686 tot_enh_size_mult, max_enh_size_mult);
687 return -EMEDIUMTYPE;
688 }
689
Diego Santa Cruz80200272014-12-23 10:50:31 +0100690 /* The default value of EXT_CSD_WR_REL_SET is device
691 * dependent, the values can only be changed if the
692 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
693 * changed only once and before partitioning is completed. */
694 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
695 if (conf->user.wr_rel_change) {
696 if (conf->user.wr_rel_set)
697 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
698 else
699 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
700 }
701 for (pidx = 0; pidx < 4; pidx++) {
702 if (conf->gp_part[pidx].wr_rel_change) {
703 if (conf->gp_part[pidx].wr_rel_set)
704 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
705 else
706 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
707 }
708 }
709
710 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
711 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
712 puts("Card does not support host controlled partition write "
713 "reliability settings\n");
714 return -EMEDIUMTYPE;
715 }
716
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +0100717 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
718 EXT_CSD_PARTITION_SETTING_COMPLETED) {
719 printf("Card already partitioned\n");
720 return -EPERM;
721 }
722
723 if (mode == MMC_HWPART_CONF_CHECK)
724 return 0;
725
726 /* Partitioning requires high-capacity size definitions */
727 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
728 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
729 EXT_CSD_ERASE_GROUP_DEF, 1);
730
731 if (err)
732 return err;
733
734 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
735
736 /* update erase group size to be high-capacity */
737 mmc->erase_grp_size =
738 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
739
740 }
741
742 /* all OK, write the configuration */
743 for (i = 0; i < 4; i++) {
744 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
745 EXT_CSD_ENH_START_ADDR+i,
746 (enh_start_addr >> (i*8)) & 0xFF);
747 if (err)
748 return err;
749 }
750 for (i = 0; i < 3; i++) {
751 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
752 EXT_CSD_ENH_SIZE_MULT+i,
753 (enh_size_mult >> (i*8)) & 0xFF);
754 if (err)
755 return err;
756 }
757 for (pidx = 0; pidx < 4; pidx++) {
758 for (i = 0; i < 3; i++) {
759 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
760 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
761 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
762 if (err)
763 return err;
764 }
765 }
766 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
767 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
768 if (err)
769 return err;
770
771 if (mode == MMC_HWPART_CONF_SET)
772 return 0;
773
Diego Santa Cruz80200272014-12-23 10:50:31 +0100774 /* The WR_REL_SET is a write-once register but shall be
775 * written before setting PART_SETTING_COMPLETED. As it is
776 * write-once we can only write it when completing the
777 * partitioning. */
778 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
779 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
780 EXT_CSD_WR_REL_SET, wr_rel_set);
781 if (err)
782 return err;
783 }
784
Diego Santa Cruz69eb71a02014-12-23 10:50:29 +0100785 /* Setting PART_SETTING_COMPLETED confirms the partition
786 * configuration but it only becomes effective after power
787 * cycle, so we do not adjust the partition related settings
788 * in the mmc struct. */
789
790 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
791 EXT_CSD_PARTITION_SETTING,
792 EXT_CSD_PARTITION_SETTING_COMPLETED);
793 if (err)
794 return err;
795
796 return 0;
797}
798
Simon Glass394dfc02016-06-12 23:30:22 -0600799#ifndef CONFIG_DM_MMC_OPS
Thierry Redingb9c8b772012-01-02 01:15:37 +0000800int mmc_getcd(struct mmc *mmc)
801{
802 int cd;
803
804 cd = board_mmc_getcd(mmc);
805
Peter Korsgaardf7b15102013-03-21 04:00:03 +0000806 if (cd < 0) {
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200807 if (mmc->cfg->ops->getcd)
808 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardf7b15102013-03-21 04:00:03 +0000809 else
810 cd = 1;
811 }
Thierry Redingb9c8b772012-01-02 01:15:37 +0000812
813 return cd;
814}
Simon Glass394dfc02016-06-12 23:30:22 -0600815#endif
Thierry Redingb9c8b772012-01-02 01:15:37 +0000816
Kim Phillips87ea3892012-10-29 13:34:43 +0000817static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Flemingad347bb2008-10-30 16:41:01 -0500818{
819 struct mmc_cmd cmd;
820 struct mmc_data data;
821
822 /* Switch the frequency */
823 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
824 cmd.resp_type = MMC_RSP_R1;
825 cmd.cmdarg = (mode << 31) | 0xffffff;
826 cmd.cmdarg &= ~(0xf << (group * 4));
827 cmd.cmdarg |= value << (group * 4);
Andy Flemingad347bb2008-10-30 16:41:01 -0500828
829 data.dest = (char *)resp;
830 data.blocksize = 64;
831 data.blocks = 1;
832 data.flags = MMC_DATA_READ;
833
834 return mmc_send_cmd(mmc, &cmd, &data);
835}
836
837
Kim Phillips87ea3892012-10-29 13:34:43 +0000838static int sd_change_freq(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500839{
840 int err;
841 struct mmc_cmd cmd;
Anton staaf9b00f0d2011-10-03 13:54:59 +0000842 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
843 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Andy Flemingad347bb2008-10-30 16:41:01 -0500844 struct mmc_data data;
845 int timeout;
846
847 mmc->card_caps = 0;
848
Thomas Chou1254c3d2010-12-24 13:12:21 +0000849 if (mmc_host_is_spi(mmc))
850 return 0;
851
Andy Flemingad347bb2008-10-30 16:41:01 -0500852 /* Read the SCR to find out if this card supports higher speeds */
853 cmd.cmdidx = MMC_CMD_APP_CMD;
854 cmd.resp_type = MMC_RSP_R1;
855 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -0500856
857 err = mmc_send_cmd(mmc, &cmd, NULL);
858
859 if (err)
860 return err;
861
862 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
863 cmd.resp_type = MMC_RSP_R1;
864 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -0500865
866 timeout = 3;
867
868retry_scr:
Anton staaf9b00f0d2011-10-03 13:54:59 +0000869 data.dest = (char *)scr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500870 data.blocksize = 8;
871 data.blocks = 1;
872 data.flags = MMC_DATA_READ;
873
874 err = mmc_send_cmd(mmc, &cmd, &data);
875
876 if (err) {
877 if (timeout--)
878 goto retry_scr;
879
880 return err;
881 }
882
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300883 mmc->scr[0] = __be32_to_cpu(scr[0]);
884 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Flemingad347bb2008-10-30 16:41:01 -0500885
886 switch ((mmc->scr[0] >> 24) & 0xf) {
Bin Meng4a4ef872016-03-17 21:53:13 -0700887 case 0:
888 mmc->version = SD_VERSION_1_0;
889 break;
890 case 1:
891 mmc->version = SD_VERSION_1_10;
892 break;
893 case 2:
894 mmc->version = SD_VERSION_2;
895 if ((mmc->scr[0] >> 15) & 0x1)
896 mmc->version = SD_VERSION_3;
897 break;
898 default:
899 mmc->version = SD_VERSION_1_0;
900 break;
Andy Flemingad347bb2008-10-30 16:41:01 -0500901 }
902
Alagu Sankar24bb5ab2010-05-12 15:08:24 +0530903 if (mmc->scr[0] & SD_DATA_4BIT)
904 mmc->card_caps |= MMC_MODE_4BIT;
905
Andy Flemingad347bb2008-10-30 16:41:01 -0500906 /* Version 1.0 doesn't support switching */
907 if (mmc->version == SD_VERSION_1_0)
908 return 0;
909
910 timeout = 4;
911 while (timeout--) {
912 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaf9b00f0d2011-10-03 13:54:59 +0000913 (u8 *)switch_status);
Andy Flemingad347bb2008-10-30 16:41:01 -0500914
915 if (err)
916 return err;
917
918 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300919 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Flemingad347bb2008-10-30 16:41:01 -0500920 break;
921 }
922
Andy Flemingad347bb2008-10-30 16:41:01 -0500923 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300924 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Flemingad347bb2008-10-30 16:41:01 -0500925 return 0;
926
Macpaul Lin24e92ec2011-11-28 16:31:09 +0000927 /*
928 * If the host doesn't support SD_HIGHSPEED, do not switch card to
929 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
930 * This can avoid furthur problem when the card runs in different
931 * mode between the host.
932 */
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200933 if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
934 (mmc->cfg->host_caps & MMC_MODE_HS)))
Macpaul Lin24e92ec2011-11-28 16:31:09 +0000935 return 0;
936
Anton staaf9b00f0d2011-10-03 13:54:59 +0000937 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Flemingad347bb2008-10-30 16:41:01 -0500938
939 if (err)
940 return err;
941
Yauhen Kharuzhy6e8edf42009-05-07 00:43:30 +0300942 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Flemingad347bb2008-10-30 16:41:01 -0500943 mmc->card_caps |= MMC_MODE_HS;
944
945 return 0;
946}
947
948/* frequency bases */
949/* divided by 10 to be nice to platforms without floating point */
Mike Frysingerb588caf2010-10-20 01:15:53 +0000950static const int fbase[] = {
Andy Flemingad347bb2008-10-30 16:41:01 -0500951 10000,
952 100000,
953 1000000,
954 10000000,
955};
956
957/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
958 * to platforms without floating point.
959 */
Simon Glass03317cc2016-05-14 14:02:57 -0600960static const u8 multipliers[] = {
Andy Flemingad347bb2008-10-30 16:41:01 -0500961 0, /* reserved */
962 10,
963 12,
964 13,
965 15,
966 20,
967 25,
968 30,
969 35,
970 40,
971 45,
972 50,
973 55,
974 60,
975 70,
976 80,
977};
978
Simon Glass394dfc02016-06-12 23:30:22 -0600979#ifndef CONFIG_DM_MMC_OPS
Kim Phillips87ea3892012-10-29 13:34:43 +0000980static void mmc_set_ios(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -0500981{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200982 if (mmc->cfg->ops->set_ios)
983 mmc->cfg->ops->set_ios(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -0500984}
Simon Glass394dfc02016-06-12 23:30:22 -0600985#endif
Andy Flemingad347bb2008-10-30 16:41:01 -0500986
987void mmc_set_clock(struct mmc *mmc, uint clock)
988{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200989 if (clock > mmc->cfg->f_max)
990 clock = mmc->cfg->f_max;
Andy Flemingad347bb2008-10-30 16:41:01 -0500991
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200992 if (clock < mmc->cfg->f_min)
993 clock = mmc->cfg->f_min;
Andy Flemingad347bb2008-10-30 16:41:01 -0500994
995 mmc->clock = clock;
996
997 mmc_set_ios(mmc);
998}
999
Kim Phillips87ea3892012-10-29 13:34:43 +00001000static void mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Flemingad347bb2008-10-30 16:41:01 -05001001{
1002 mmc->bus_width = width;
1003
1004 mmc_set_ios(mmc);
1005}
1006
Kim Phillips87ea3892012-10-29 13:34:43 +00001007static int mmc_startup(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05001008{
Stephen Warrene315ae82013-06-11 15:14:01 -06001009 int err, i;
Andy Flemingad347bb2008-10-30 16:41:01 -05001010 uint mult, freq;
Yoshihiro Shimoda7d88de52011-07-04 22:13:26 +00001011 u64 cmult, csize, capacity;
Andy Flemingad347bb2008-10-30 16:41:01 -05001012 struct mmc_cmd cmd;
Simon Glassa09c2b72013-04-03 08:54:30 +00001013 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1014 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +00001015 int timeout = 1000;
Diego Santa Cruzcea8c5c2014-12-23 10:50:20 +01001016 bool has_parts = false;
Diego Santa Cruza7a75992014-12-23 10:50:27 +01001017 bool part_completed;
Simon Glasse5db1152016-05-01 13:52:35 -06001018 struct blk_desc *bdesc;
Andy Flemingad347bb2008-10-30 16:41:01 -05001019
Thomas Chou1254c3d2010-12-24 13:12:21 +00001020#ifdef CONFIG_MMC_SPI_CRC_ON
1021 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1022 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1023 cmd.resp_type = MMC_RSP_R1;
1024 cmd.cmdarg = 1;
Thomas Chou1254c3d2010-12-24 13:12:21 +00001025 err = mmc_send_cmd(mmc, &cmd, NULL);
1026
1027 if (err)
1028 return err;
1029 }
1030#endif
1031
Andy Flemingad347bb2008-10-30 16:41:01 -05001032 /* Put the Card in Identify Mode */
Thomas Chou1254c3d2010-12-24 13:12:21 +00001033 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1034 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Flemingad347bb2008-10-30 16:41:01 -05001035 cmd.resp_type = MMC_RSP_R2;
1036 cmd.cmdarg = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -05001037
1038 err = mmc_send_cmd(mmc, &cmd, NULL);
1039
1040 if (err)
1041 return err;
1042
1043 memcpy(mmc->cid, cmd.response, 16);
1044
1045 /*
1046 * For MMC cards, set the Relative Address.
1047 * For SD cards, get the Relatvie Address.
1048 * This also puts the cards into Standby State
1049 */
Thomas Chou1254c3d2010-12-24 13:12:21 +00001050 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1051 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1052 cmd.cmdarg = mmc->rca << 16;
1053 cmd.resp_type = MMC_RSP_R6;
Andy Flemingad347bb2008-10-30 16:41:01 -05001054
Thomas Chou1254c3d2010-12-24 13:12:21 +00001055 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -05001056
Thomas Chou1254c3d2010-12-24 13:12:21 +00001057 if (err)
1058 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05001059
Thomas Chou1254c3d2010-12-24 13:12:21 +00001060 if (IS_SD(mmc))
1061 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1062 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001063
1064 /* Get the Card-Specific Data */
1065 cmd.cmdidx = MMC_CMD_SEND_CSD;
1066 cmd.resp_type = MMC_RSP_R2;
1067 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -05001068
1069 err = mmc_send_cmd(mmc, &cmd, NULL);
1070
Raffaele Recalcati01a0dc62011-03-11 02:01:12 +00001071 /* Waiting for the ready status */
1072 mmc_send_status(mmc, timeout);
1073
Andy Flemingad347bb2008-10-30 16:41:01 -05001074 if (err)
1075 return err;
1076
Rabin Vincentb6eed942009-04-05 13:30:56 +05301077 mmc->csd[0] = cmd.response[0];
1078 mmc->csd[1] = cmd.response[1];
1079 mmc->csd[2] = cmd.response[2];
1080 mmc->csd[3] = cmd.response[3];
Andy Flemingad347bb2008-10-30 16:41:01 -05001081
1082 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincentbdf7a682009-04-05 13:30:55 +05301083 int version = (cmd.response[0] >> 26) & 0xf;
Andy Flemingad347bb2008-10-30 16:41:01 -05001084
1085 switch (version) {
Bin Meng4a4ef872016-03-17 21:53:13 -07001086 case 0:
1087 mmc->version = MMC_VERSION_1_2;
1088 break;
1089 case 1:
1090 mmc->version = MMC_VERSION_1_4;
1091 break;
1092 case 2:
1093 mmc->version = MMC_VERSION_2_2;
1094 break;
1095 case 3:
1096 mmc->version = MMC_VERSION_3;
1097 break;
1098 case 4:
1099 mmc->version = MMC_VERSION_4;
1100 break;
1101 default:
1102 mmc->version = MMC_VERSION_1_2;
1103 break;
Andy Flemingad347bb2008-10-30 16:41:01 -05001104 }
1105 }
1106
1107 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincentbdf7a682009-04-05 13:30:55 +05301108 freq = fbase[(cmd.response[0] & 0x7)];
1109 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Flemingad347bb2008-10-30 16:41:01 -05001110
1111 mmc->tran_speed = freq * mult;
1112
Markus Niebel03951412013-12-16 13:40:46 +01001113 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincentb6eed942009-04-05 13:30:56 +05301114 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Flemingad347bb2008-10-30 16:41:01 -05001115
1116 if (IS_SD(mmc))
1117 mmc->write_bl_len = mmc->read_bl_len;
1118 else
Rabin Vincentb6eed942009-04-05 13:30:56 +05301119 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Flemingad347bb2008-10-30 16:41:01 -05001120
1121 if (mmc->high_capacity) {
1122 csize = (mmc->csd[1] & 0x3f) << 16
1123 | (mmc->csd[2] & 0xffff0000) >> 16;
1124 cmult = 8;
1125 } else {
1126 csize = (mmc->csd[1] & 0x3ff) << 2
1127 | (mmc->csd[2] & 0xc0000000) >> 30;
1128 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1129 }
1130
Stephen Warrene315ae82013-06-11 15:14:01 -06001131 mmc->capacity_user = (csize + 1) << (cmult + 2);
1132 mmc->capacity_user *= mmc->read_bl_len;
1133 mmc->capacity_boot = 0;
1134 mmc->capacity_rpmb = 0;
1135 for (i = 0; i < 4; i++)
1136 mmc->capacity_gp[i] = 0;
Andy Flemingad347bb2008-10-30 16:41:01 -05001137
Simon Glassa09c2b72013-04-03 08:54:30 +00001138 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1139 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Flemingad347bb2008-10-30 16:41:01 -05001140
Simon Glassa09c2b72013-04-03 08:54:30 +00001141 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1142 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Andy Flemingad347bb2008-10-30 16:41:01 -05001143
Markus Niebel03951412013-12-16 13:40:46 +01001144 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
1145 cmd.cmdidx = MMC_CMD_SET_DSR;
1146 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
1147 cmd.resp_type = MMC_RSP_NONE;
1148 if (mmc_send_cmd(mmc, &cmd, NULL))
1149 printf("MMC: SET_DSR failed\n");
1150 }
1151
Andy Flemingad347bb2008-10-30 16:41:01 -05001152 /* Select the card, and put it into Transfer Mode */
Thomas Chou1254c3d2010-12-24 13:12:21 +00001153 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1154 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargav4a32fba2011-10-05 03:13:23 +00001155 cmd.resp_type = MMC_RSP_R1;
Thomas Chou1254c3d2010-12-24 13:12:21 +00001156 cmd.cmdarg = mmc->rca << 16;
Thomas Chou1254c3d2010-12-24 13:12:21 +00001157 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Flemingad347bb2008-10-30 16:41:01 -05001158
Thomas Chou1254c3d2010-12-24 13:12:21 +00001159 if (err)
1160 return err;
1161 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001162
Lei Wenea526762011-06-22 17:03:31 +00001163 /*
1164 * For SD, its erase group is always one sector
1165 */
1166 mmc->erase_grp_size = 1;
Lei Wen31b99802011-05-02 16:26:26 +00001167 mmc->part_config = MMCPART_NOAVAILABLE;
Sukumar Ghorai232293c2010-09-20 18:29:29 +05301168 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1169 /* check ext_csd version and capacity */
1170 err = mmc_send_ext_csd(mmc, ext_csd);
Diego Santa Cruzca25e062014-12-23 10:50:28 +01001171 if (err)
1172 return err;
1173 if (ext_csd[EXT_CSD_REV] >= 2) {
Yoshihiro Shimoda7d88de52011-07-04 22:13:26 +00001174 /*
1175 * According to the JEDEC Standard, the value of
1176 * ext_csd's capacity is valid if the value is more
1177 * than 2GB
1178 */
Lei Wen217467f2011-10-03 20:35:10 +00001179 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1180 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1181 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1182 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Simon Glassa09c2b72013-04-03 08:54:30 +00001183 capacity *= MMC_MAX_BLOCK_LEN;
Łukasz Majewski237823e2011-07-05 02:19:44 +00001184 if ((capacity >> 20) > 2 * 1024)
Stephen Warrene315ae82013-06-11 15:14:01 -06001185 mmc->capacity_user = capacity;
Sukumar Ghorai232293c2010-09-20 18:29:29 +05301186 }
Lei Wen31b99802011-05-02 16:26:26 +00001187
Jaehoon Chung6108ef62013-01-29 19:31:16 +00001188 switch (ext_csd[EXT_CSD_REV]) {
1189 case 1:
1190 mmc->version = MMC_VERSION_4_1;
1191 break;
1192 case 2:
1193 mmc->version = MMC_VERSION_4_2;
1194 break;
1195 case 3:
1196 mmc->version = MMC_VERSION_4_3;
1197 break;
1198 case 5:
1199 mmc->version = MMC_VERSION_4_41;
1200 break;
1201 case 6:
1202 mmc->version = MMC_VERSION_4_5;
1203 break;
Markus Niebel32f53b62014-11-18 15:13:53 +01001204 case 7:
1205 mmc->version = MMC_VERSION_5_0;
1206 break;
Stefan Wahren1243cd82016-06-16 17:54:06 +00001207 case 8:
1208 mmc->version = MMC_VERSION_5_1;
1209 break;
Jaehoon Chung6108ef62013-01-29 19:31:16 +00001210 }
1211
Diego Santa Cruza7a75992014-12-23 10:50:27 +01001212 /* The partition data may be non-zero but it is only
1213 * effective if PARTITION_SETTING_COMPLETED is set in
1214 * EXT_CSD, so ignore any data if this bit is not set,
1215 * except for enabling the high-capacity group size
1216 * definition (see below). */
1217 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
1218 EXT_CSD_PARTITION_SETTING_COMPLETED);
1219
Diego Santa Cruzcea8c5c2014-12-23 10:50:20 +01001220 /* store the partition info of emmc */
1221 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
1222 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1223 ext_csd[EXT_CSD_BOOT_MULT])
1224 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
Diego Santa Cruza7a75992014-12-23 10:50:27 +01001225 if (part_completed &&
1226 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
Diego Santa Cruzcea8c5c2014-12-23 10:50:20 +01001227 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
1228
1229 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1230
1231 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1232
1233 for (i = 0; i < 4; i++) {
1234 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
Diego Santa Cruza7a75992014-12-23 10:50:27 +01001235 uint mult = (ext_csd[idx + 2] << 16) +
Diego Santa Cruzcea8c5c2014-12-23 10:50:20 +01001236 (ext_csd[idx + 1] << 8) + ext_csd[idx];
Diego Santa Cruza7a75992014-12-23 10:50:27 +01001237 if (mult)
1238 has_parts = true;
1239 if (!part_completed)
1240 continue;
1241 mmc->capacity_gp[i] = mult;
Diego Santa Cruzcea8c5c2014-12-23 10:50:20 +01001242 mmc->capacity_gp[i] *=
1243 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1244 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Diego Santa Cruze5a2a3a2014-12-23 10:50:21 +01001245 mmc->capacity_gp[i] <<= 19;
Diego Santa Cruzcea8c5c2014-12-23 10:50:20 +01001246 }
1247
Diego Santa Cruza7a75992014-12-23 10:50:27 +01001248 if (part_completed) {
1249 mmc->enh_user_size =
1250 (ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) +
1251 (ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) +
1252 ext_csd[EXT_CSD_ENH_SIZE_MULT];
1253 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1254 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1255 mmc->enh_user_size <<= 19;
1256 mmc->enh_user_start =
1257 (ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) +
1258 (ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) +
1259 (ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) +
1260 ext_csd[EXT_CSD_ENH_START_ADDR];
1261 if (mmc->high_capacity)
1262 mmc->enh_user_start <<= 9;
1263 }
Diego Santa Cruz3b62d842014-12-23 10:50:22 +01001264
Lei Wenea526762011-06-22 17:03:31 +00001265 /*
Oliver Metzb3f14092013-10-01 20:32:07 +02001266 * Host needs to enable ERASE_GRP_DEF bit if device is
1267 * partitioned. This bit will be lost every time after a reset
1268 * or power off. This will affect erase size.
Lei Wenea526762011-06-22 17:03:31 +00001269 */
Diego Santa Cruza7a75992014-12-23 10:50:27 +01001270 if (part_completed)
Diego Santa Cruzcea8c5c2014-12-23 10:50:20 +01001271 has_parts = true;
Oliver Metzb3f14092013-10-01 20:32:07 +02001272 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
Diego Santa Cruzcea8c5c2014-12-23 10:50:20 +01001273 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
1274 has_parts = true;
1275 if (has_parts) {
Oliver Metzb3f14092013-10-01 20:32:07 +02001276 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1277 EXT_CSD_ERASE_GROUP_DEF, 1);
1278
1279 if (err)
1280 return err;
Hannes Petermaier15e874d2014-08-08 09:47:22 +02001281 else
1282 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
Diego Santa Cruz61b78fe2014-12-23 10:50:25 +01001283 }
Oliver Metzb3f14092013-10-01 20:32:07 +02001284
Diego Santa Cruz61b78fe2014-12-23 10:50:25 +01001285 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
Oliver Metzb3f14092013-10-01 20:32:07 +02001286 /* Read out group size from ext_csd */
Lei Wen217467f2011-10-03 20:35:10 +00001287 mmc->erase_grp_size =
Diego Santa Cruz747f6fa2014-12-23 10:50:24 +01001288 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Markus Niebel6d398922014-11-18 15:11:42 +01001289 /*
1290 * if high capacity and partition setting completed
1291 * SEC_COUNT is valid even if it is smaller than 2 GiB
1292 * JEDEC Standard JESD84-B45, 6.2.4
1293 */
Diego Santa Cruza7a75992014-12-23 10:50:27 +01001294 if (mmc->high_capacity && part_completed) {
Markus Niebel6d398922014-11-18 15:11:42 +01001295 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1296 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1297 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1298 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1299 capacity *= MMC_MAX_BLOCK_LEN;
1300 mmc->capacity_user = capacity;
1301 }
Simon Glassa09c2b72013-04-03 08:54:30 +00001302 } else {
Oliver Metzb3f14092013-10-01 20:32:07 +02001303 /* Calculate the group size from the csd value. */
Lei Wenea526762011-06-22 17:03:31 +00001304 int erase_gsz, erase_gmul;
1305 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1306 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1307 mmc->erase_grp_size = (erase_gsz + 1)
1308 * (erase_gmul + 1);
1309 }
Diego Santa Cruz61b78fe2014-12-23 10:50:25 +01001310
1311 mmc->hc_wp_grp_size = 1024
1312 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1313 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Diego Santa Cruz37a50b92014-12-23 10:50:33 +01001314
1315 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
Sukumar Ghorai232293c2010-09-20 18:29:29 +05301316 }
1317
Simon Glasse5db1152016-05-01 13:52:35 -06001318 err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
Stephen Warrene315ae82013-06-11 15:14:01 -06001319 if (err)
1320 return err;
1321
Andy Flemingad347bb2008-10-30 16:41:01 -05001322 if (IS_SD(mmc))
1323 err = sd_change_freq(mmc);
1324 else
1325 err = mmc_change_freq(mmc);
1326
1327 if (err)
1328 return err;
1329
1330 /* Restrict card's capabilities by what the host can do */
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001331 mmc->card_caps &= mmc->cfg->host_caps;
Andy Flemingad347bb2008-10-30 16:41:01 -05001332
1333 if (IS_SD(mmc)) {
1334 if (mmc->card_caps & MMC_MODE_4BIT) {
1335 cmd.cmdidx = MMC_CMD_APP_CMD;
1336 cmd.resp_type = MMC_RSP_R1;
1337 cmd.cmdarg = mmc->rca << 16;
Andy Flemingad347bb2008-10-30 16:41:01 -05001338
1339 err = mmc_send_cmd(mmc, &cmd, NULL);
1340 if (err)
1341 return err;
1342
1343 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1344 cmd.resp_type = MMC_RSP_R1;
1345 cmd.cmdarg = 2;
Andy Flemingad347bb2008-10-30 16:41:01 -05001346 err = mmc_send_cmd(mmc, &cmd, NULL);
1347 if (err)
1348 return err;
1349
1350 mmc_set_bus_width(mmc, 4);
1351 }
1352
1353 if (mmc->card_caps & MMC_MODE_HS)
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001354 mmc->tran_speed = 50000000;
Andy Flemingad347bb2008-10-30 16:41:01 -05001355 else
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001356 mmc->tran_speed = 25000000;
Andrew Gabbasovccb7b042014-12-25 10:22:25 -06001357 } else if (mmc->version >= MMC_VERSION_4) {
1358 /* Only version 4 of MMC supports wider bus widths */
Andy Flemingeb766ad2012-10-31 19:02:38 +00001359 int idx;
1360
1361 /* An array of possible bus widths in order of preference */
1362 static unsigned ext_csd_bits[] = {
Jaehoon Chung38ce30b2014-05-16 13:59:54 +09001363 EXT_CSD_DDR_BUS_WIDTH_8,
1364 EXT_CSD_DDR_BUS_WIDTH_4,
Andy Flemingeb766ad2012-10-31 19:02:38 +00001365 EXT_CSD_BUS_WIDTH_8,
1366 EXT_CSD_BUS_WIDTH_4,
1367 EXT_CSD_BUS_WIDTH_1,
1368 };
1369
1370 /* An array to map CSD bus widths to host cap bits */
1371 static unsigned ext_to_hostcaps[] = {
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06001372 [EXT_CSD_DDR_BUS_WIDTH_4] =
1373 MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1374 [EXT_CSD_DDR_BUS_WIDTH_8] =
1375 MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
Andy Flemingeb766ad2012-10-31 19:02:38 +00001376 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1377 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1378 };
1379
1380 /* An array to map chosen bus width to an integer */
1381 static unsigned widths[] = {
Jaehoon Chung38ce30b2014-05-16 13:59:54 +09001382 8, 4, 8, 4, 1,
Andy Flemingeb766ad2012-10-31 19:02:38 +00001383 };
1384
1385 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1386 unsigned int extw = ext_csd_bits[idx];
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06001387 unsigned int caps = ext_to_hostcaps[extw];
Andy Flemingeb766ad2012-10-31 19:02:38 +00001388
1389 /*
Andrew Gabbasovc1b2cf02014-12-25 10:22:24 -06001390 * If the bus width is still not changed,
1391 * don't try to set the default again.
1392 * Otherwise, recover from switch attempts
1393 * by switching to 1-bit bus width.
1394 */
1395 if (extw == EXT_CSD_BUS_WIDTH_1 &&
1396 mmc->bus_width == 1) {
1397 err = 0;
1398 break;
1399 }
1400
1401 /*
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06001402 * Check to make sure the card and controller support
1403 * these capabilities
Andy Flemingeb766ad2012-10-31 19:02:38 +00001404 */
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06001405 if ((mmc->card_caps & caps) != caps)
Andy Flemingeb766ad2012-10-31 19:02:38 +00001406 continue;
1407
Andy Flemingad347bb2008-10-30 16:41:01 -05001408 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
Andy Flemingeb766ad2012-10-31 19:02:38 +00001409 EXT_CSD_BUS_WIDTH, extw);
Andy Flemingad347bb2008-10-30 16:41:01 -05001410
1411 if (err)
Lei Wen4f5a6a52011-10-03 20:35:11 +00001412 continue;
Andy Flemingad347bb2008-10-30 16:41:01 -05001413
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06001414 mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
Andy Flemingeb766ad2012-10-31 19:02:38 +00001415 mmc_set_bus_width(mmc, widths[idx]);
Andy Flemingad347bb2008-10-30 16:41:01 -05001416
Lei Wen4f5a6a52011-10-03 20:35:11 +00001417 err = mmc_send_ext_csd(mmc, test_csd);
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06001418
1419 if (err)
1420 continue;
Andy Flemingad347bb2008-10-30 16:41:01 -05001421
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06001422 /* Only compare read only fields */
1423 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1424 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1425 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1426 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1427 ext_csd[EXT_CSD_REV]
1428 == test_csd[EXT_CSD_REV] &&
1429 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1430 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1431 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1432 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
Lei Wen4f5a6a52011-10-03 20:35:11 +00001433 break;
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06001434 else
Jaehoon Chung7825d202016-07-19 16:33:36 +09001435 err = -EBADMSG;
Andy Flemingad347bb2008-10-30 16:41:01 -05001436 }
1437
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06001438 if (err)
1439 return err;
1440
Andy Flemingad347bb2008-10-30 16:41:01 -05001441 if (mmc->card_caps & MMC_MODE_HS) {
1442 if (mmc->card_caps & MMC_MODE_HS_52MHz)
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001443 mmc->tran_speed = 52000000;
Andy Flemingad347bb2008-10-30 16:41:01 -05001444 else
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001445 mmc->tran_speed = 26000000;
1446 }
Andy Flemingad347bb2008-10-30 16:41:01 -05001447 }
1448
Jaehoon Chunge1d4c7b2012-03-26 21:16:03 +00001449 mmc_set_clock(mmc, mmc->tran_speed);
1450
Andrew Gabbasov532663b2014-12-01 06:59:11 -06001451 /* Fix the block length for DDR mode */
1452 if (mmc->ddr_mode) {
1453 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1454 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1455 }
1456
Andy Flemingad347bb2008-10-30 16:41:01 -05001457 /* fill in device description */
Simon Glasse5db1152016-05-01 13:52:35 -06001458 bdesc = mmc_get_blk_desc(mmc);
1459 bdesc->lun = 0;
1460 bdesc->hwpart = 0;
1461 bdesc->type = 0;
1462 bdesc->blksz = mmc->read_bl_len;
1463 bdesc->log2blksz = LOG2(bdesc->blksz);
1464 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsd67754f2015-12-04 23:27:40 +01001465#if !defined(CONFIG_SPL_BUILD) || \
1466 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
1467 !defined(CONFIG_USE_TINY_PRINTF))
Simon Glasse5db1152016-05-01 13:52:35 -06001468 sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
Taylor Hutt7367ec22012-10-20 17:15:59 +00001469 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1470 (mmc->cid[3] >> 16) & 0xffff);
Simon Glasse5db1152016-05-01 13:52:35 -06001471 sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
Taylor Hutt7367ec22012-10-20 17:15:59 +00001472 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1473 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1474 (mmc->cid[2] >> 24) & 0xff);
Simon Glasse5db1152016-05-01 13:52:35 -06001475 sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
Taylor Hutt7367ec22012-10-20 17:15:59 +00001476 (mmc->cid[2] >> 16) & 0xf);
Paul Burton6a7c5ba2013-09-04 16:12:25 +01001477#else
Simon Glasse5db1152016-05-01 13:52:35 -06001478 bdesc->vendor[0] = 0;
1479 bdesc->product[0] = 0;
1480 bdesc->revision[0] = 0;
Paul Burton6a7c5ba2013-09-04 16:12:25 +01001481#endif
Mikhail Kshevetskiy5cbfa8e7a2012-07-09 08:53:38 +00001482#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Simon Glasse5db1152016-05-01 13:52:35 -06001483 part_init(bdesc);
Mikhail Kshevetskiy5cbfa8e7a2012-07-09 08:53:38 +00001484#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001485
1486 return 0;
1487}
1488
Kim Phillips87ea3892012-10-29 13:34:43 +00001489static int mmc_send_if_cond(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05001490{
1491 struct mmc_cmd cmd;
1492 int err;
1493
1494 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1495 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001496 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Flemingad347bb2008-10-30 16:41:01 -05001497 cmd.resp_type = MMC_RSP_R7;
Andy Flemingad347bb2008-10-30 16:41:01 -05001498
1499 err = mmc_send_cmd(mmc, &cmd, NULL);
1500
1501 if (err)
1502 return err;
1503
Rabin Vincentb6eed942009-04-05 13:30:56 +05301504 if ((cmd.response[0] & 0xff) != 0xaa)
Jaehoon Chung7825d202016-07-19 16:33:36 +09001505 return -EOPNOTSUPP;
Andy Flemingad347bb2008-10-30 16:41:01 -05001506 else
1507 mmc->version = SD_VERSION_2;
1508
1509 return 0;
1510}
1511
Paul Kocialkowski2439fe92014-11-08 20:55:45 +01001512/* board-specific MMC power initializations. */
1513__weak void board_mmc_power_init(void)
1514{
1515}
1516
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00001517int mmc_start_init(struct mmc *mmc)
Andy Flemingad347bb2008-10-30 16:41:01 -05001518{
Simon Glass394dfc02016-06-12 23:30:22 -06001519 bool no_card;
Macpaul Lin028bde12011-11-14 23:35:39 +00001520 int err;
Andy Flemingad347bb2008-10-30 16:41:01 -05001521
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001522 /* we pretend there's no card when init is NULL */
Simon Glass394dfc02016-06-12 23:30:22 -06001523 no_card = mmc_getcd(mmc) == 0;
1524#ifndef CONFIG_DM_MMC_OPS
1525 no_card = no_card || (mmc->cfg->ops->init == NULL);
1526#endif
1527 if (no_card) {
Thierry Redingb9c8b772012-01-02 01:15:37 +00001528 mmc->has_init = 0;
Paul Burton6a7c5ba2013-09-04 16:12:25 +01001529#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Thierry Redingb9c8b772012-01-02 01:15:37 +00001530 printf("MMC: no card present\n");
Paul Burton6a7c5ba2013-09-04 16:12:25 +01001531#endif
Jaehoon Chung7825d202016-07-19 16:33:36 +09001532 return -ENOMEDIUM;
Thierry Redingb9c8b772012-01-02 01:15:37 +00001533 }
1534
Lei Wen31b99802011-05-02 16:26:26 +00001535 if (mmc->has_init)
1536 return 0;
1537
Yangbo Lub124f8a2015-04-22 13:57:00 +08001538#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1539 mmc_adapter_card_type_ident();
1540#endif
Paul Kocialkowski2439fe92014-11-08 20:55:45 +01001541 board_mmc_power_init();
1542
Simon Glass394dfc02016-06-12 23:30:22 -06001543#ifdef CONFIG_DM_MMC_OPS
1544 /* The device has already been probed ready for use */
1545#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001546 /* made sure it's not NULL earlier */
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001547 err = mmc->cfg->ops->init(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05001548 if (err)
1549 return err;
Simon Glass394dfc02016-06-12 23:30:22 -06001550#endif
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -06001551 mmc->ddr_mode = 0;
Ilya Yanok8459aab2009-06-29 17:53:16 +04001552 mmc_set_bus_width(mmc, 1);
1553 mmc_set_clock(mmc, 1);
1554
Andy Flemingad347bb2008-10-30 16:41:01 -05001555 /* Reset the Card */
1556 err = mmc_go_idle(mmc);
1557
1558 if (err)
1559 return err;
1560
Lei Wen31b99802011-05-02 16:26:26 +00001561 /* The internal partition reset to user partition(0) at every CMD0*/
Simon Glasse5db1152016-05-01 13:52:35 -06001562 mmc_get_blk_desc(mmc)->hwpart = 0;
Lei Wen31b99802011-05-02 16:26:26 +00001563
Andy Flemingad347bb2008-10-30 16:41:01 -05001564 /* Test for SD version 2 */
Macpaul Lin028bde12011-11-14 23:35:39 +00001565 err = mmc_send_if_cond(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -05001566
Andy Flemingad347bb2008-10-30 16:41:01 -05001567 /* Now try to get the SD card's operating condition */
1568 err = sd_send_op_cond(mmc);
1569
1570 /* If the command timed out, we check for an MMC card */
Jaehoon Chung7825d202016-07-19 16:33:36 +09001571 if (err == -ETIMEDOUT) {
Andy Flemingad347bb2008-10-30 16:41:01 -05001572 err = mmc_send_op_cond(mmc);
1573
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05001574 if (err) {
Paul Burton6a7c5ba2013-09-04 16:12:25 +01001575#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Andy Flemingad347bb2008-10-30 16:41:01 -05001576 printf("Card did not respond to voltage select!\n");
Paul Burton6a7c5ba2013-09-04 16:12:25 +01001577#endif
Jaehoon Chung7825d202016-07-19 16:33:36 +09001578 return -EOPNOTSUPP;
Andy Flemingad347bb2008-10-30 16:41:01 -05001579 }
1580 }
1581
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05001582 if (!err)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00001583 mmc->init_in_progress = 1;
1584
1585 return err;
1586}
1587
1588static int mmc_complete_init(struct mmc *mmc)
1589{
1590 int err = 0;
1591
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05001592 mmc->init_in_progress = 0;
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00001593 if (mmc->op_cond_pending)
1594 err = mmc_complete_op_cond(mmc);
1595
1596 if (!err)
1597 err = mmc_startup(mmc);
Lei Wen31b99802011-05-02 16:26:26 +00001598 if (err)
1599 mmc->has_init = 0;
1600 else
1601 mmc->has_init = 1;
1602 return err;
Andy Flemingad347bb2008-10-30 16:41:01 -05001603}
1604
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00001605int mmc_init(struct mmc *mmc)
1606{
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05001607 int err = 0;
Mateusz Zalegada351782014-04-29 20:15:30 +02001608 unsigned start;
Simon Glass59bc6f22016-05-01 13:52:41 -06001609#ifdef CONFIG_DM_MMC
1610 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00001611
Simon Glass59bc6f22016-05-01 13:52:41 -06001612 upriv->mmc = mmc;
1613#endif
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00001614 if (mmc->has_init)
1615 return 0;
Mateusz Zalegada351782014-04-29 20:15:30 +02001616
1617 start = get_timer(0);
1618
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00001619 if (!mmc->init_in_progress)
1620 err = mmc_start_init(mmc);
1621
Andrew Gabbasov3a669bc2015-03-19 07:44:07 -05001622 if (!err)
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00001623 err = mmc_complete_init(mmc);
1624 debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1625 return err;
1626}
1627
Markus Niebel03951412013-12-16 13:40:46 +01001628int mmc_set_dsr(struct mmc *mmc, u16 val)
1629{
1630 mmc->dsr = val;
1631 return 0;
1632}
1633
Jeroen Hofstee47726302014-07-10 22:46:28 +02001634/* CPU-specific MMC initializations */
1635__weak int cpu_mmc_init(bd_t *bis)
Andy Flemingad347bb2008-10-30 16:41:01 -05001636{
1637 return -1;
1638}
1639
Jeroen Hofstee47726302014-07-10 22:46:28 +02001640/* board-specific MMC initializations. */
1641__weak int board_mmc_init(bd_t *bis)
1642{
1643 return -1;
1644}
Andy Flemingad347bb2008-10-30 16:41:01 -05001645
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00001646void mmc_set_preinit(struct mmc *mmc, int preinit)
1647{
1648 mmc->preinit = preinit;
1649}
1650
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06001651#if defined(CONFIG_DM_MMC) && defined(CONFIG_SPL_BUILD)
1652static int mmc_probe(bd_t *bis)
1653{
1654 return 0;
1655}
1656#elif defined(CONFIG_DM_MMC)
1657static int mmc_probe(bd_t *bis)
1658{
Simon Glass547cb342015-12-29 05:22:49 -07001659 int ret, i;
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06001660 struct uclass *uc;
Simon Glass547cb342015-12-29 05:22:49 -07001661 struct udevice *dev;
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06001662
1663 ret = uclass_get(UCLASS_MMC, &uc);
1664 if (ret)
1665 return ret;
1666
Simon Glass547cb342015-12-29 05:22:49 -07001667 /*
1668 * Try to add them in sequence order. Really with driver model we
1669 * should allow holes, but the current MMC list does not allow that.
1670 * So if we request 0, 1, 3 we will get 0, 1, 2.
1671 */
1672 for (i = 0; ; i++) {
1673 ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
1674 if (ret == -ENODEV)
1675 break;
1676 }
1677 uclass_foreach_dev(dev, uc) {
1678 ret = device_probe(dev);
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06001679 if (ret)
Simon Glass547cb342015-12-29 05:22:49 -07001680 printf("%s - probe failed: %d\n", dev->name, ret);
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06001681 }
1682
1683 return 0;
1684}
1685#else
1686static int mmc_probe(bd_t *bis)
1687{
1688 if (board_mmc_init(bis) < 0)
1689 cpu_mmc_init(bis);
1690
1691 return 0;
1692}
1693#endif
Che-Liang Chiou4a2c7d72012-11-28 15:21:13 +00001694
Andy Flemingad347bb2008-10-30 16:41:01 -05001695int mmc_initialize(bd_t *bis)
1696{
Daniel Kochmański13df57b2015-05-29 16:55:43 +02001697 static int initialized = 0;
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06001698 int ret;
Daniel Kochmański13df57b2015-05-29 16:55:43 +02001699 if (initialized) /* Avoid initializing mmc multiple times */
1700 return 0;
1701 initialized = 1;
1702
Simon Glasse5db1152016-05-01 13:52:35 -06001703#ifndef CONFIG_BLK
1704 mmc_list_init();
1705#endif
Sjoerd Simonsdf8aa522015-08-30 16:55:45 -06001706 ret = mmc_probe(bis);
1707 if (ret)
1708 return ret;
Andy Flemingad347bb2008-10-30 16:41:01 -05001709
Ying Zhang9ff70262013-08-16 15:16:11 +08001710#ifndef CONFIG_SPL_BUILD
Andy Flemingad347bb2008-10-30 16:41:01 -05001711 print_mmc_devices(',');
Ying Zhang9ff70262013-08-16 15:16:11 +08001712#endif
Andy Flemingad347bb2008-10-30 16:41:01 -05001713
Simon Glasse5db1152016-05-01 13:52:35 -06001714 mmc_do_preinit();
Andy Flemingad347bb2008-10-30 16:41:01 -05001715 return 0;
1716}