blob: 6d88db4bb6dd2fd3a29e7d7d78a5fc458d7ab31d [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>
18#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060019#include <memalign.h>
Andy Fleming272cc702008-10-30 16:41:01 -050020#include <linux/list.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053021#include <div64.h>
Paul Burtonda61fa52013-09-09 15:30:26 +010022#include "mmc_private.h"
Andy Fleming272cc702008-10-30 16:41:01 -050023
24static struct list_head mmc_devices;
25static int cur_dev_num = -1;
26
Jeroen Hofstee750121c2014-07-12 21:24:08 +020027__weak int board_mmc_getwp(struct mmc *mmc)
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000028{
29 return -1;
30}
31
32int mmc_getwp(struct mmc *mmc)
33{
34 int wp;
35
36 wp = board_mmc_getwp(mmc);
37
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000038 if (wp < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020039 if (mmc->cfg->ops->getwp)
40 wp = mmc->cfg->ops->getwp(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000041 else
42 wp = 0;
43 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000044
45 return wp;
46}
47
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +020048__weak int board_mmc_getcd(struct mmc *mmc)
49{
Stefano Babic11fdade2010-02-05 15:04:43 +010050 return -1;
51}
52
Paul Burtonda61fa52013-09-09 15:30:26 +010053int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming272cc702008-10-30 16:41:01 -050054{
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000055 int ret;
Marek Vasut8635ff92012-03-15 18:41:35 +000056
Marek Vasut8635ff92012-03-15 18:41:35 +000057#ifdef CONFIG_MMC_TRACE
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000058 int i;
59 u8 *ptr;
60
61 printf("CMD_SEND:%d\n", cmd->cmdidx);
62 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020063 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000064 switch (cmd->resp_type) {
65 case MMC_RSP_NONE:
66 printf("\t\tMMC_RSP_NONE\n");
67 break;
68 case MMC_RSP_R1:
69 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
70 cmd->response[0]);
71 break;
72 case MMC_RSP_R1b:
73 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
74 cmd->response[0]);
75 break;
76 case MMC_RSP_R2:
77 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
78 cmd->response[0]);
79 printf("\t\t \t\t 0x%08X \n",
80 cmd->response[1]);
81 printf("\t\t \t\t 0x%08X \n",
82 cmd->response[2]);
83 printf("\t\t \t\t 0x%08X \n",
84 cmd->response[3]);
85 printf("\n");
86 printf("\t\t\t\t\tDUMPING DATA\n");
87 for (i = 0; i < 4; i++) {
88 int j;
89 printf("\t\t\t\t\t%03d - ", i*4);
Dirk Behme146bec72012-03-08 02:35:34 +000090 ptr = (u8 *)&cmd->response[i];
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000091 ptr += 3;
92 for (j = 0; j < 4; j++)
93 printf("%02X ", *ptr--);
94 printf("\n");
95 }
96 break;
97 case MMC_RSP_R3:
98 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
99 cmd->response[0]);
100 break;
101 default:
102 printf("\t\tERROR MMC rsp not supported\n");
103 break;
104 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000105#else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200106 ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000107#endif
Marek Vasut8635ff92012-03-15 18:41:35 +0000108 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500109}
110
Paul Burtonda61fa52013-09-09 15:30:26 +0100111int mmc_send_status(struct mmc *mmc, int timeout)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000112{
113 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000114 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000115#ifdef CONFIG_MMC_TRACE
116 int status;
117#endif
118
119 cmd.cmdidx = MMC_CMD_SEND_STATUS;
120 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200121 if (!mmc_host_is_spi(mmc))
122 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000123
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500124 while (1) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000125 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000126 if (!err) {
127 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
128 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
129 MMC_STATE_PRG)
130 break;
131 else if (cmd.response[0] & MMC_STATUS_MASK) {
Paul Burton56196822013-09-04 16:12:25 +0100132#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Jan Kloetzked617c422012-02-05 22:29:12 +0000133 printf("Status Error: 0x%08X\n",
134 cmd.response[0]);
Paul Burton56196822013-09-04 16:12:25 +0100135#endif
Jan Kloetzked617c422012-02-05 22:29:12 +0000136 return COMM_ERR;
137 }
138 } else if (--retries < 0)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000139 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000140
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500141 if (timeout-- <= 0)
142 break;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000143
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500144 udelay(1000);
145 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000146
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000147#ifdef CONFIG_MMC_TRACE
148 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
149 printf("CURR STATE:%d\n", status);
150#endif
Jongman Heo5b0c9422012-06-03 21:32:13 +0000151 if (timeout <= 0) {
Paul Burton56196822013-09-04 16:12:25 +0100152#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000153 printf("Timeout waiting card ready\n");
Paul Burton56196822013-09-04 16:12:25 +0100154#endif
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000155 return TIMEOUT;
156 }
Andrew Gabbasov6b2221b2014-04-03 04:34:32 -0500157 if (cmd.response[0] & MMC_STATUS_SWITCH_ERROR)
158 return SWITCH_ERR;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000159
160 return 0;
161}
162
Paul Burtonda61fa52013-09-09 15:30:26 +0100163int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500164{
165 struct mmc_cmd cmd;
166
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600167 if (mmc->ddr_mode)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900168 return 0;
169
Andy Fleming272cc702008-10-30 16:41:01 -0500170 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
171 cmd.resp_type = MMC_RSP_R1;
172 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500173
174 return mmc_send_cmd(mmc, &cmd, NULL);
175}
176
177struct mmc *find_mmc_device(int dev_num)
178{
179 struct mmc *m;
180 struct list_head *entry;
181
182 list_for_each(entry, &mmc_devices) {
183 m = list_entry(entry, struct mmc, link);
184
185 if (m->block_dev.dev == dev_num)
186 return m;
187 }
188
Paul Burton56196822013-09-04 16:12:25 +0100189#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -0500190 printf("MMC Device %d not found\n", dev_num);
Paul Burton56196822013-09-04 16:12:25 +0100191#endif
Andy Fleming272cc702008-10-30 16:41:01 -0500192
193 return NULL;
194}
195
Sascha Silbeff8fef52013-06-14 13:07:25 +0200196static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000197 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500198{
199 struct mmc_cmd cmd;
200 struct mmc_data data;
201
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700202 if (blkcnt > 1)
203 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
204 else
205 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500206
207 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700208 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500209 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700210 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500211
212 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500213
214 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700215 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500216 data.blocksize = mmc->read_bl_len;
217 data.flags = MMC_DATA_READ;
218
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700219 if (mmc_send_cmd(mmc, &cmd, &data))
220 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500221
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700222 if (blkcnt > 1) {
223 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
224 cmd.cmdarg = 0;
225 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700226 if (mmc_send_cmd(mmc, &cmd, NULL)) {
Paul Burton56196822013-09-04 16:12:25 +0100227#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700228 printf("mmc fail to send stop cmd\n");
Paul Burton56196822013-09-04 16:12:25 +0100229#endif
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700230 return 0;
231 }
Andy Fleming272cc702008-10-30 16:41:01 -0500232 }
233
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700234 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500235}
236
Stephen Warren7c4213f2015-12-07 11:38:48 -0700237static ulong mmc_bread(block_dev_desc_t *block_dev, lbaint_t start,
238 lbaint_t blkcnt, void *dst)
Andy Fleming272cc702008-10-30 16:41:01 -0500239{
Stephen Warren7c4213f2015-12-07 11:38:48 -0700240 int dev_num = block_dev->dev;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700241 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500242
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700243 if (blkcnt == 0)
244 return 0;
245
246 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500247 if (!mmc)
248 return 0;
249
Lei Wend2bf29e2010-09-13 22:07:27 +0800250 if ((start + blkcnt) > mmc->block_dev.lba) {
Paul Burton56196822013-09-04 16:12:25 +0100251#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Sascha Silbeff8fef52013-06-14 13:07:25 +0200252 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800253 start + blkcnt, mmc->block_dev.lba);
Paul Burton56196822013-09-04 16:12:25 +0100254#endif
Lei Wend2bf29e2010-09-13 22:07:27 +0800255 return 0;
256 }
Andy Fleming272cc702008-10-30 16:41:01 -0500257
Simon Glass11692992015-06-23 15:38:50 -0600258 if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
259 debug("%s: Failed to set blocklen\n", __func__);
Andy Fleming272cc702008-10-30 16:41:01 -0500260 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600261 }
Andy Fleming272cc702008-10-30 16:41:01 -0500262
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700263 do {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200264 cur = (blocks_todo > mmc->cfg->b_max) ?
265 mmc->cfg->b_max : blocks_todo;
Simon Glass11692992015-06-23 15:38:50 -0600266 if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
267 debug("%s: Failed to read blocks\n", __func__);
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700268 return 0;
Simon Glass11692992015-06-23 15:38:50 -0600269 }
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700270 blocks_todo -= cur;
271 start += cur;
272 dst += cur * mmc->read_bl_len;
273 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500274
275 return blkcnt;
276}
277
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000278static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500279{
280 struct mmc_cmd cmd;
281 int err;
282
283 udelay(1000);
284
285 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
286 cmd.cmdarg = 0;
287 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500288
289 err = mmc_send_cmd(mmc, &cmd, NULL);
290
291 if (err)
292 return err;
293
294 udelay(2000);
295
296 return 0;
297}
298
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000299static int sd_send_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500300{
301 int timeout = 1000;
302 int err;
303 struct mmc_cmd cmd;
304
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500305 while (1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500306 cmd.cmdidx = MMC_CMD_APP_CMD;
307 cmd.resp_type = MMC_RSP_R1;
308 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500309
310 err = mmc_send_cmd(mmc, &cmd, NULL);
311
312 if (err)
313 return err;
314
315 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
316 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100317
318 /*
319 * Most cards do not answer if some reserved bits
320 * in the ocr are set. However, Some controller
321 * can set bit 7 (reserved for low voltages), but
322 * how to manage low voltages SD card is not yet
323 * specified.
324 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000325 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200326 (mmc->cfg->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500327
328 if (mmc->version == SD_VERSION_2)
329 cmd.cmdarg |= OCR_HCS;
330
331 err = mmc_send_cmd(mmc, &cmd, NULL);
332
333 if (err)
334 return err;
335
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500336 if (cmd.response[0] & OCR_BUSY)
337 break;
Andy Fleming272cc702008-10-30 16:41:01 -0500338
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500339 if (timeout-- <= 0)
340 return UNUSABLE_ERR;
341
342 udelay(1000);
343 }
Andy Fleming272cc702008-10-30 16:41:01 -0500344
345 if (mmc->version != SD_VERSION_2)
346 mmc->version = SD_VERSION_1_0;
347
Thomas Choud52ebf12010-12-24 13:12:21 +0000348 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
349 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
350 cmd.resp_type = MMC_RSP_R3;
351 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000352
353 err = mmc_send_cmd(mmc, &cmd, NULL);
354
355 if (err)
356 return err;
357 }
358
Rabin Vincent998be3d2009-04-05 13:30:56 +0530359 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500360
361 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
362 mmc->rca = 0;
363
364 return 0;
365}
366
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500367static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500368{
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500369 struct mmc_cmd cmd;
Andy Fleming272cc702008-10-30 16:41:01 -0500370 int err;
371
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500372 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
373 cmd.resp_type = MMC_RSP_R3;
374 cmd.cmdarg = 0;
Rob Herring5a203972015-03-23 17:56:59 -0500375 if (use_arg && !mmc_host_is_spi(mmc))
376 cmd.cmdarg = OCR_HCS |
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200377 (mmc->cfg->voltages &
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500378 (mmc->ocr & OCR_VOLTAGE_MASK)) |
379 (mmc->ocr & OCR_ACCESS_MODE);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000380
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500381 err = mmc_send_cmd(mmc, &cmd, NULL);
Che-Liang Chioue9550442012-11-28 15:21:13 +0000382 if (err)
383 return err;
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500384 mmc->ocr = cmd.response[0];
Che-Liang Chioue9550442012-11-28 15:21:13 +0000385 return 0;
386}
387
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200388static int mmc_send_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000389{
Che-Liang Chioue9550442012-11-28 15:21:13 +0000390 int err, i;
391
Andy Fleming272cc702008-10-30 16:41:01 -0500392 /* Some cards seem to need this */
393 mmc_go_idle(mmc);
394
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000395 /* Asking to the card its capabilities */
Che-Liang Chioue9550442012-11-28 15:21:13 +0000396 for (i = 0; i < 2; i++) {
Andrew Gabbasov5289b532015-03-19 07:44:04 -0500397 err = mmc_send_op_cond_iter(mmc, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500398 if (err)
399 return err;
400
Che-Liang Chioue9550442012-11-28 15:21:13 +0000401 /* exit if not busy (flag seems to be inverted) */
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500402 if (mmc->ocr & OCR_BUSY)
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500403 break;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000404 }
Andrew Gabbasovbd47c132015-03-19 07:44:07 -0500405 mmc->op_cond_pending = 1;
406 return 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +0000407}
Andy Fleming272cc702008-10-30 16:41:01 -0500408
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200409static int mmc_complete_op_cond(struct mmc *mmc)
Che-Liang Chioue9550442012-11-28 15:21:13 +0000410{
411 struct mmc_cmd cmd;
412 int timeout = 1000;
413 uint start;
414 int err;
415
416 mmc->op_cond_pending = 0;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500417 if (!(mmc->ocr & OCR_BUSY)) {
418 start = get_timer(0);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500419 while (1) {
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500420 err = mmc_send_op_cond_iter(mmc, 1);
421 if (err)
422 return err;
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500423 if (mmc->ocr & OCR_BUSY)
424 break;
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500425 if (get_timer(start) > timeout)
426 return UNUSABLE_ERR;
427 udelay(100);
Andrew Gabbasov1677eef2015-03-19 07:44:06 -0500428 }
Andrew Gabbasovcc17c012015-03-19 07:44:05 -0500429 }
Andy Fleming272cc702008-10-30 16:41:01 -0500430
Thomas Choud52ebf12010-12-24 13:12:21 +0000431 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
432 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
433 cmd.resp_type = MMC_RSP_R3;
434 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000435
436 err = mmc_send_cmd(mmc, &cmd, NULL);
437
438 if (err)
439 return err;
Andrew Gabbasova626c8d2015-03-19 07:44:03 -0500440
441 mmc->ocr = cmd.response[0];
Thomas Choud52ebf12010-12-24 13:12:21 +0000442 }
443
Andy Fleming272cc702008-10-30 16:41:01 -0500444 mmc->version = MMC_VERSION_UNKNOWN;
Andy Fleming272cc702008-10-30 16:41:01 -0500445
446 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
Stephen Warrendef816a2014-01-30 16:11:12 -0700447 mmc->rca = 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500448
449 return 0;
450}
451
452
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000453static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500454{
455 struct mmc_cmd cmd;
456 struct mmc_data data;
457 int err;
458
459 /* Get the Card Status Register */
460 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
461 cmd.resp_type = MMC_RSP_R1;
462 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500463
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000464 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500465 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000466 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500467 data.flags = MMC_DATA_READ;
468
469 err = mmc_send_cmd(mmc, &cmd, &data);
470
471 return err;
472}
473
474
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000475static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Fleming272cc702008-10-30 16:41:01 -0500476{
477 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000478 int timeout = 1000;
479 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500480
481 cmd.cmdidx = MMC_CMD_SWITCH;
482 cmd.resp_type = MMC_RSP_R1b;
483 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000484 (index << 16) |
485 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500486
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000487 ret = mmc_send_cmd(mmc, &cmd, NULL);
488
489 /* Waiting for the ready status */
Jan Kloetzke93ad0d12012-02-05 22:29:11 +0000490 if (!ret)
491 ret = mmc_send_status(mmc, timeout);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000492
493 return ret;
494
Andy Fleming272cc702008-10-30 16:41:01 -0500495}
496
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000497static int mmc_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500498{
Simon Glass8bfa1952013-04-03 08:54:30 +0000499 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Andy Fleming272cc702008-10-30 16:41:01 -0500500 char cardtype;
501 int err;
502
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600503 mmc->card_caps = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500504
Thomas Choud52ebf12010-12-24 13:12:21 +0000505 if (mmc_host_is_spi(mmc))
506 return 0;
507
Andy Fleming272cc702008-10-30 16:41:01 -0500508 /* Only version 4 supports high-speed */
509 if (mmc->version < MMC_VERSION_4)
510 return 0;
511
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -0600512 mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
513
Andy Fleming272cc702008-10-30 16:41:01 -0500514 err = mmc_send_ext_csd(mmc, ext_csd);
515
516 if (err)
517 return err;
518
Lei Wen0560db12011-10-03 20:35:10 +0000519 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500520
521 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
522
523 if (err)
Andrew Gabbasov6b2221b2014-04-03 04:34:32 -0500524 return err == SWITCH_ERR ? 0 : err;
Andy Fleming272cc702008-10-30 16:41:01 -0500525
526 /* Now check to see that it worked */
527 err = mmc_send_ext_csd(mmc, ext_csd);
528
529 if (err)
530 return err;
531
532 /* No high-speed support */
Lei Wen0560db12011-10-03 20:35:10 +0000533 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Fleming272cc702008-10-30 16:41:01 -0500534 return 0;
535
536 /* High Speed is set, there are two types: 52MHz and 26MHz */
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900537 if (cardtype & EXT_CSD_CARD_TYPE_52) {
Andrew Gabbasov201d5ac2014-12-01 06:59:10 -0600538 if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900539 mmc->card_caps |= MMC_MODE_DDR_52MHz;
Andy Fleming272cc702008-10-30 16:41:01 -0500540 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900541 } else {
Andy Fleming272cc702008-10-30 16:41:01 -0500542 mmc->card_caps |= MMC_MODE_HS;
Jaehoon Chungd22e3d42014-05-16 13:59:54 +0900543 }
Andy Fleming272cc702008-10-30 16:41:01 -0500544
545 return 0;
546}
547
Stephen Warrenf866a462013-06-11 15:14:01 -0600548static int mmc_set_capacity(struct mmc *mmc, int part_num)
549{
550 switch (part_num) {
551 case 0:
552 mmc->capacity = mmc->capacity_user;
553 break;
554 case 1:
555 case 2:
556 mmc->capacity = mmc->capacity_boot;
557 break;
558 case 3:
559 mmc->capacity = mmc->capacity_rpmb;
560 break;
561 case 4:
562 case 5:
563 case 6:
564 case 7:
565 mmc->capacity = mmc->capacity_gp[part_num - 4];
566 break;
567 default:
568 return -1;
569 }
570
571 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
572
573 return 0;
574}
575
Stephen Warrend2356282014-05-07 12:19:02 -0600576int mmc_select_hwpart(int dev_num, int hwpart)
577{
578 struct mmc *mmc = find_mmc_device(dev_num);
579 int ret;
580
581 if (!mmc)
Stephen Warrend4622df2014-05-23 12:47:06 -0600582 return -ENODEV;
Stephen Warrend2356282014-05-07 12:19:02 -0600583
584 if (mmc->part_num == hwpart)
585 return 0;
586
587 if (mmc->part_config == MMCPART_NOAVAILABLE) {
588 printf("Card doesn't support part_switch\n");
Stephen Warrend4622df2014-05-23 12:47:06 -0600589 return -EMEDIUMTYPE;
Stephen Warrend2356282014-05-07 12:19:02 -0600590 }
591
592 ret = mmc_switch_part(dev_num, hwpart);
593 if (ret)
Stephen Warrend4622df2014-05-23 12:47:06 -0600594 return ret;
Stephen Warrend2356282014-05-07 12:19:02 -0600595
596 mmc->part_num = hwpart;
597
598 return 0;
599}
600
601
Lei Wenbc897b12011-05-02 16:26:26 +0000602int mmc_switch_part(int dev_num, unsigned int part_num)
603{
604 struct mmc *mmc = find_mmc_device(dev_num);
Stephen Warrenf866a462013-06-11 15:14:01 -0600605 int ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000606
607 if (!mmc)
608 return -1;
609
Stephen Warrenf866a462013-06-11 15:14:01 -0600610 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
611 (mmc->part_config & ~PART_ACCESS_MASK)
612 | (part_num & PART_ACCESS_MASK));
Stephen Warrenf866a462013-06-11 15:14:01 -0600613
Peter Bigot6dc93e72014-09-02 18:31:23 -0500614 /*
615 * Set the capacity if the switch succeeded or was intended
616 * to return to representing the raw device.
617 */
618 if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0)))
619 ret = mmc_set_capacity(mmc, part_num);
620
621 return ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000622}
623
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100624int mmc_hwpart_config(struct mmc *mmc,
625 const struct mmc_hwpart_conf *conf,
626 enum mmc_hwpart_conf_mode mode)
627{
628 u8 part_attrs = 0;
629 u32 enh_size_mult;
630 u32 enh_start_addr;
631 u32 gp_size_mult[4];
632 u32 max_enh_size_mult;
633 u32 tot_enh_size_mult = 0;
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100634 u8 wr_rel_set;
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100635 int i, pidx, err;
636 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
637
638 if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
639 return -EINVAL;
640
641 if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
642 printf("eMMC >= 4.4 required for enhanced user data area\n");
643 return -EMEDIUMTYPE;
644 }
645
646 if (!(mmc->part_support & PART_SUPPORT)) {
647 printf("Card does not support partitioning\n");
648 return -EMEDIUMTYPE;
649 }
650
651 if (!mmc->hc_wp_grp_size) {
652 printf("Card does not define HC WP group size\n");
653 return -EMEDIUMTYPE;
654 }
655
656 /* check partition alignment and total enhanced size */
657 if (conf->user.enh_size) {
658 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
659 conf->user.enh_start % mmc->hc_wp_grp_size) {
660 printf("User data enhanced area not HC WP group "
661 "size aligned\n");
662 return -EINVAL;
663 }
664 part_attrs |= EXT_CSD_ENH_USR;
665 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
666 if (mmc->high_capacity) {
667 enh_start_addr = conf->user.enh_start;
668 } else {
669 enh_start_addr = (conf->user.enh_start << 9);
670 }
671 } else {
672 enh_size_mult = 0;
673 enh_start_addr = 0;
674 }
675 tot_enh_size_mult += enh_size_mult;
676
677 for (pidx = 0; pidx < 4; pidx++) {
678 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
679 printf("GP%i partition not HC WP group size "
680 "aligned\n", pidx+1);
681 return -EINVAL;
682 }
683 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
684 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
685 part_attrs |= EXT_CSD_ENH_GP(pidx);
686 tot_enh_size_mult += gp_size_mult[pidx];
687 }
688 }
689
690 if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
691 printf("Card does not support enhanced attribute\n");
692 return -EMEDIUMTYPE;
693 }
694
695 err = mmc_send_ext_csd(mmc, ext_csd);
696 if (err)
697 return err;
698
699 max_enh_size_mult =
700 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
701 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
702 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
703 if (tot_enh_size_mult > max_enh_size_mult) {
704 printf("Total enhanced size exceeds maximum (%u > %u)\n",
705 tot_enh_size_mult, max_enh_size_mult);
706 return -EMEDIUMTYPE;
707 }
708
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100709 /* The default value of EXT_CSD_WR_REL_SET is device
710 * dependent, the values can only be changed if the
711 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
712 * changed only once and before partitioning is completed. */
713 wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
714 if (conf->user.wr_rel_change) {
715 if (conf->user.wr_rel_set)
716 wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
717 else
718 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
719 }
720 for (pidx = 0; pidx < 4; pidx++) {
721 if (conf->gp_part[pidx].wr_rel_change) {
722 if (conf->gp_part[pidx].wr_rel_set)
723 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
724 else
725 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
726 }
727 }
728
729 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
730 !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
731 puts("Card does not support host controlled partition write "
732 "reliability settings\n");
733 return -EMEDIUMTYPE;
734 }
735
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100736 if (ext_csd[EXT_CSD_PARTITION_SETTING] &
737 EXT_CSD_PARTITION_SETTING_COMPLETED) {
738 printf("Card already partitioned\n");
739 return -EPERM;
740 }
741
742 if (mode == MMC_HWPART_CONF_CHECK)
743 return 0;
744
745 /* Partitioning requires high-capacity size definitions */
746 if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
747 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
748 EXT_CSD_ERASE_GROUP_DEF, 1);
749
750 if (err)
751 return err;
752
753 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
754
755 /* update erase group size to be high-capacity */
756 mmc->erase_grp_size =
757 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
758
759 }
760
761 /* all OK, write the configuration */
762 for (i = 0; i < 4; i++) {
763 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
764 EXT_CSD_ENH_START_ADDR+i,
765 (enh_start_addr >> (i*8)) & 0xFF);
766 if (err)
767 return err;
768 }
769 for (i = 0; i < 3; i++) {
770 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
771 EXT_CSD_ENH_SIZE_MULT+i,
772 (enh_size_mult >> (i*8)) & 0xFF);
773 if (err)
774 return err;
775 }
776 for (pidx = 0; pidx < 4; pidx++) {
777 for (i = 0; i < 3; i++) {
778 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
779 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
780 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
781 if (err)
782 return err;
783 }
784 }
785 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
786 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
787 if (err)
788 return err;
789
790 if (mode == MMC_HWPART_CONF_SET)
791 return 0;
792
Diego Santa Cruz8dda5b0e2014-12-23 10:50:31 +0100793 /* The WR_REL_SET is a write-once register but shall be
794 * written before setting PART_SETTING_COMPLETED. As it is
795 * write-once we can only write it when completing the
796 * partitioning. */
797 if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
798 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
799 EXT_CSD_WR_REL_SET, wr_rel_set);
800 if (err)
801 return err;
802 }
803
Diego Santa Cruzac9da0e2014-12-23 10:50:29 +0100804 /* Setting PART_SETTING_COMPLETED confirms the partition
805 * configuration but it only becomes effective after power
806 * cycle, so we do not adjust the partition related settings
807 * in the mmc struct. */
808
809 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
810 EXT_CSD_PARTITION_SETTING,
811 EXT_CSD_PARTITION_SETTING_COMPLETED);
812 if (err)
813 return err;
814
815 return 0;
816}
817
Thierry Reding48972d92012-01-02 01:15:37 +0000818int mmc_getcd(struct mmc *mmc)
819{
820 int cd;
821
822 cd = board_mmc_getcd(mmc);
823
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000824 if (cd < 0) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200825 if (mmc->cfg->ops->getcd)
826 cd = mmc->cfg->ops->getcd(mmc);
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000827 else
828 cd = 1;
829 }
Thierry Reding48972d92012-01-02 01:15:37 +0000830
831 return cd;
832}
833
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000834static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -0500835{
836 struct mmc_cmd cmd;
837 struct mmc_data data;
838
839 /* Switch the frequency */
840 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
841 cmd.resp_type = MMC_RSP_R1;
842 cmd.cmdarg = (mode << 31) | 0xffffff;
843 cmd.cmdarg &= ~(0xf << (group * 4));
844 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500845
846 data.dest = (char *)resp;
847 data.blocksize = 64;
848 data.blocks = 1;
849 data.flags = MMC_DATA_READ;
850
851 return mmc_send_cmd(mmc, &cmd, &data);
852}
853
854
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000855static int sd_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500856{
857 int err;
858 struct mmc_cmd cmd;
Anton staaff781dd32011-10-03 13:54:59 +0000859 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
860 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500861 struct mmc_data data;
862 int timeout;
863
864 mmc->card_caps = 0;
865
Thomas Choud52ebf12010-12-24 13:12:21 +0000866 if (mmc_host_is_spi(mmc))
867 return 0;
868
Andy Fleming272cc702008-10-30 16:41:01 -0500869 /* Read the SCR to find out if this card supports higher speeds */
870 cmd.cmdidx = MMC_CMD_APP_CMD;
871 cmd.resp_type = MMC_RSP_R1;
872 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -0500873
874 err = mmc_send_cmd(mmc, &cmd, NULL);
875
876 if (err)
877 return err;
878
879 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
880 cmd.resp_type = MMC_RSP_R1;
881 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500882
883 timeout = 3;
884
885retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +0000886 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -0500887 data.blocksize = 8;
888 data.blocks = 1;
889 data.flags = MMC_DATA_READ;
890
891 err = mmc_send_cmd(mmc, &cmd, &data);
892
893 if (err) {
894 if (timeout--)
895 goto retry_scr;
896
897 return err;
898 }
899
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300900 mmc->scr[0] = __be32_to_cpu(scr[0]);
901 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500902
903 switch ((mmc->scr[0] >> 24) & 0xf) {
904 case 0:
905 mmc->version = SD_VERSION_1_0;
906 break;
907 case 1:
908 mmc->version = SD_VERSION_1_10;
909 break;
910 case 2:
911 mmc->version = SD_VERSION_2;
Jaehoon Chung1741c642013-01-29 22:58:16 +0000912 if ((mmc->scr[0] >> 15) & 0x1)
913 mmc->version = SD_VERSION_3;
Andy Fleming272cc702008-10-30 16:41:01 -0500914 break;
915 default:
916 mmc->version = SD_VERSION_1_0;
917 break;
918 }
919
Alagu Sankarb44c7082010-05-12 15:08:24 +0530920 if (mmc->scr[0] & SD_DATA_4BIT)
921 mmc->card_caps |= MMC_MODE_4BIT;
922
Andy Fleming272cc702008-10-30 16:41:01 -0500923 /* Version 1.0 doesn't support switching */
924 if (mmc->version == SD_VERSION_1_0)
925 return 0;
926
927 timeout = 4;
928 while (timeout--) {
929 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +0000930 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500931
932 if (err)
933 return err;
934
935 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300936 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500937 break;
938 }
939
Andy Fleming272cc702008-10-30 16:41:01 -0500940 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300941 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500942 return 0;
943
Macpaul Lin2c3fbf42011-11-28 16:31:09 +0000944 /*
945 * If the host doesn't support SD_HIGHSPEED, do not switch card to
946 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
947 * This can avoid furthur problem when the card runs in different
948 * mode between the host.
949 */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200950 if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
951 (mmc->cfg->host_caps & MMC_MODE_HS)))
Macpaul Lin2c3fbf42011-11-28 16:31:09 +0000952 return 0;
953
Anton staaff781dd32011-10-03 13:54:59 +0000954 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500955
956 if (err)
957 return err;
958
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300959 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500960 mmc->card_caps |= MMC_MODE_HS;
961
962 return 0;
963}
964
965/* frequency bases */
966/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000967static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500968 10000,
969 100000,
970 1000000,
971 10000000,
972};
973
974/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
975 * to platforms without floating point.
976 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000977static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500978 0, /* reserved */
979 10,
980 12,
981 13,
982 15,
983 20,
984 25,
985 30,
986 35,
987 40,
988 45,
989 50,
990 55,
991 60,
992 70,
993 80,
994};
995
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000996static void mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500997{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200998 if (mmc->cfg->ops->set_ios)
999 mmc->cfg->ops->set_ios(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001000}
1001
1002void mmc_set_clock(struct mmc *mmc, uint clock)
1003{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001004 if (clock > mmc->cfg->f_max)
1005 clock = mmc->cfg->f_max;
Andy Fleming272cc702008-10-30 16:41:01 -05001006
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001007 if (clock < mmc->cfg->f_min)
1008 clock = mmc->cfg->f_min;
Andy Fleming272cc702008-10-30 16:41:01 -05001009
1010 mmc->clock = clock;
1011
1012 mmc_set_ios(mmc);
1013}
1014
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001015static void mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -05001016{
1017 mmc->bus_width = width;
1018
1019 mmc_set_ios(mmc);
1020}
1021
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001022static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001023{
Stephen Warrenf866a462013-06-11 15:14:01 -06001024 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -05001025 uint mult, freq;
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001026 u64 cmult, csize, capacity;
Andy Fleming272cc702008-10-30 16:41:01 -05001027 struct mmc_cmd cmd;
Simon Glass8bfa1952013-04-03 08:54:30 +00001028 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1029 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +00001030 int timeout = 1000;
Diego Santa Cruz0c453bb2014-12-23 10:50:20 +01001031 bool has_parts = false;
Diego Santa Cruz8a0cf492014-12-23 10:50:27 +01001032 bool part_completed;
Andy Fleming272cc702008-10-30 16:41:01 -05001033
Thomas Choud52ebf12010-12-24 13:12:21 +00001034#ifdef CONFIG_MMC_SPI_CRC_ON
1035 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1036 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1037 cmd.resp_type = MMC_RSP_R1;
1038 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001039 err = mmc_send_cmd(mmc, &cmd, NULL);
1040
1041 if (err)
1042 return err;
1043 }
1044#endif
1045
Andy Fleming272cc702008-10-30 16:41:01 -05001046 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001047 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1048 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -05001049 cmd.resp_type = MMC_RSP_R2;
1050 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001051
1052 err = mmc_send_cmd(mmc, &cmd, NULL);
1053
1054 if (err)
1055 return err;
1056
1057 memcpy(mmc->cid, cmd.response, 16);
1058
1059 /*
1060 * For MMC cards, set the Relative Address.
1061 * For SD cards, get the Relatvie Address.
1062 * This also puts the cards into Standby State
1063 */
Thomas Choud52ebf12010-12-24 13:12:21 +00001064 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1065 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1066 cmd.cmdarg = mmc->rca << 16;
1067 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -05001068
Thomas Choud52ebf12010-12-24 13:12:21 +00001069 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001070
Thomas Choud52ebf12010-12-24 13:12:21 +00001071 if (err)
1072 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001073
Thomas Choud52ebf12010-12-24 13:12:21 +00001074 if (IS_SD(mmc))
1075 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1076 }
Andy Fleming272cc702008-10-30 16:41:01 -05001077
1078 /* Get the Card-Specific Data */
1079 cmd.cmdidx = MMC_CMD_SEND_CSD;
1080 cmd.resp_type = MMC_RSP_R2;
1081 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001082
1083 err = mmc_send_cmd(mmc, &cmd, NULL);
1084
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +00001085 /* Waiting for the ready status */
1086 mmc_send_status(mmc, timeout);
1087
Andy Fleming272cc702008-10-30 16:41:01 -05001088 if (err)
1089 return err;
1090
Rabin Vincent998be3d2009-04-05 13:30:56 +05301091 mmc->csd[0] = cmd.response[0];
1092 mmc->csd[1] = cmd.response[1];
1093 mmc->csd[2] = cmd.response[2];
1094 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05001095
1096 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301097 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05001098
1099 switch (version) {
1100 case 0:
1101 mmc->version = MMC_VERSION_1_2;
1102 break;
1103 case 1:
1104 mmc->version = MMC_VERSION_1_4;
1105 break;
1106 case 2:
1107 mmc->version = MMC_VERSION_2_2;
1108 break;
1109 case 3:
1110 mmc->version = MMC_VERSION_3;
1111 break;
1112 case 4:
1113 mmc->version = MMC_VERSION_4;
1114 break;
1115 default:
1116 mmc->version = MMC_VERSION_1_2;
1117 break;
1118 }
1119 }
1120
1121 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301122 freq = fbase[(cmd.response[0] & 0x7)];
1123 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05001124
1125 mmc->tran_speed = freq * mult;
1126
Markus Niebelab711882013-12-16 13:40:46 +01001127 mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
Rabin Vincent998be3d2009-04-05 13:30:56 +05301128 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001129
1130 if (IS_SD(mmc))
1131 mmc->write_bl_len = mmc->read_bl_len;
1132 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05301133 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001134
1135 if (mmc->high_capacity) {
1136 csize = (mmc->csd[1] & 0x3f) << 16
1137 | (mmc->csd[2] & 0xffff0000) >> 16;
1138 cmult = 8;
1139 } else {
1140 csize = (mmc->csd[1] & 0x3ff) << 2
1141 | (mmc->csd[2] & 0xc0000000) >> 30;
1142 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1143 }
1144
Stephen Warrenf866a462013-06-11 15:14:01 -06001145 mmc->capacity_user = (csize + 1) << (cmult + 2);
1146 mmc->capacity_user *= mmc->read_bl_len;
1147 mmc->capacity_boot = 0;
1148 mmc->capacity_rpmb = 0;
1149 for (i = 0; i < 4; i++)
1150 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001151
Simon Glass8bfa1952013-04-03 08:54:30 +00001152 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1153 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001154
Simon Glass8bfa1952013-04-03 08:54:30 +00001155 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1156 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001157
Markus Niebelab711882013-12-16 13:40:46 +01001158 if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
1159 cmd.cmdidx = MMC_CMD_SET_DSR;
1160 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
1161 cmd.resp_type = MMC_RSP_NONE;
1162 if (mmc_send_cmd(mmc, &cmd, NULL))
1163 printf("MMC: SET_DSR failed\n");
1164 }
1165
Andy Fleming272cc702008-10-30 16:41:01 -05001166 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001167 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1168 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00001169 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001170 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00001171 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001172
Thomas Choud52ebf12010-12-24 13:12:21 +00001173 if (err)
1174 return err;
1175 }
Andy Fleming272cc702008-10-30 16:41:01 -05001176
Lei Wene6f99a52011-06-22 17:03:31 +00001177 /*
1178 * For SD, its erase group is always one sector
1179 */
1180 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00001181 mmc->part_config = MMCPART_NOAVAILABLE;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301182 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1183 /* check ext_csd version and capacity */
1184 err = mmc_send_ext_csd(mmc, ext_csd);
Diego Santa Cruz9cf199e2014-12-23 10:50:28 +01001185 if (err)
1186 return err;
1187 if (ext_csd[EXT_CSD_REV] >= 2) {
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001188 /*
1189 * According to the JEDEC Standard, the value of
1190 * ext_csd's capacity is valid if the value is more
1191 * than 2GB
1192 */
Lei Wen0560db12011-10-03 20:35:10 +00001193 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1194 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1195 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1196 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Simon Glass8bfa1952013-04-03 08:54:30 +00001197 capacity *= MMC_MAX_BLOCK_LEN;
Łukasz Majewskib1f1e8212011-07-05 02:19:44 +00001198 if ((capacity >> 20) > 2 * 1024)
Stephen Warrenf866a462013-06-11 15:14:01 -06001199 mmc->capacity_user = capacity;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301200 }
Lei Wenbc897b12011-05-02 16:26:26 +00001201
Jaehoon Chung64f4a612013-01-29 19:31:16 +00001202 switch (ext_csd[EXT_CSD_REV]) {
1203 case 1:
1204 mmc->version = MMC_VERSION_4_1;
1205 break;
1206 case 2:
1207 mmc->version = MMC_VERSION_4_2;
1208 break;
1209 case 3:
1210 mmc->version = MMC_VERSION_4_3;
1211 break;
1212 case 5:
1213 mmc->version = MMC_VERSION_4_41;
1214 break;
1215 case 6:
1216 mmc->version = MMC_VERSION_4_5;
1217 break;
Markus Niebeledab7232014-11-18 15:13:53 +01001218 case 7:
1219 mmc->version = MMC_VERSION_5_0;
1220 break;
Jaehoon Chung64f4a612013-01-29 19:31:16 +00001221 }
1222
Diego Santa Cruz8a0cf492014-12-23 10:50:27 +01001223 /* The partition data may be non-zero but it is only
1224 * effective if PARTITION_SETTING_COMPLETED is set in
1225 * EXT_CSD, so ignore any data if this bit is not set,
1226 * except for enabling the high-capacity group size
1227 * definition (see below). */
1228 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
1229 EXT_CSD_PARTITION_SETTING_COMPLETED);
1230
Diego Santa Cruz0c453bb2014-12-23 10:50:20 +01001231 /* store the partition info of emmc */
1232 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
1233 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1234 ext_csd[EXT_CSD_BOOT_MULT])
1235 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
Diego Santa Cruz8a0cf492014-12-23 10:50:27 +01001236 if (part_completed &&
1237 (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
Diego Santa Cruz0c453bb2014-12-23 10:50:20 +01001238 mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
1239
1240 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1241
1242 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1243
1244 for (i = 0; i < 4; i++) {
1245 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
Diego Santa Cruz8a0cf492014-12-23 10:50:27 +01001246 uint mult = (ext_csd[idx + 2] << 16) +
Diego Santa Cruz0c453bb2014-12-23 10:50:20 +01001247 (ext_csd[idx + 1] << 8) + ext_csd[idx];
Diego Santa Cruz8a0cf492014-12-23 10:50:27 +01001248 if (mult)
1249 has_parts = true;
1250 if (!part_completed)
1251 continue;
1252 mmc->capacity_gp[i] = mult;
Diego Santa Cruz0c453bb2014-12-23 10:50:20 +01001253 mmc->capacity_gp[i] *=
1254 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1255 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Diego Santa Cruzf8e89d62014-12-23 10:50:21 +01001256 mmc->capacity_gp[i] <<= 19;
Diego Santa Cruz0c453bb2014-12-23 10:50:20 +01001257 }
1258
Diego Santa Cruz8a0cf492014-12-23 10:50:27 +01001259 if (part_completed) {
1260 mmc->enh_user_size =
1261 (ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) +
1262 (ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) +
1263 ext_csd[EXT_CSD_ENH_SIZE_MULT];
1264 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1265 mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1266 mmc->enh_user_size <<= 19;
1267 mmc->enh_user_start =
1268 (ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) +
1269 (ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) +
1270 (ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) +
1271 ext_csd[EXT_CSD_ENH_START_ADDR];
1272 if (mmc->high_capacity)
1273 mmc->enh_user_start <<= 9;
1274 }
Diego Santa Cruza7f852b2014-12-23 10:50:22 +01001275
Lei Wene6f99a52011-06-22 17:03:31 +00001276 /*
Oliver Metz1937e5a2013-10-01 20:32:07 +02001277 * Host needs to enable ERASE_GRP_DEF bit if device is
1278 * partitioned. This bit will be lost every time after a reset
1279 * or power off. This will affect erase size.
Lei Wene6f99a52011-06-22 17:03:31 +00001280 */
Diego Santa Cruz8a0cf492014-12-23 10:50:27 +01001281 if (part_completed)
Diego Santa Cruz0c453bb2014-12-23 10:50:20 +01001282 has_parts = true;
Oliver Metz1937e5a2013-10-01 20:32:07 +02001283 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
Diego Santa Cruz0c453bb2014-12-23 10:50:20 +01001284 (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
1285 has_parts = true;
1286 if (has_parts) {
Oliver Metz1937e5a2013-10-01 20:32:07 +02001287 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1288 EXT_CSD_ERASE_GROUP_DEF, 1);
1289
1290 if (err)
1291 return err;
Hannes Petermaier021a8052014-08-08 09:47:22 +02001292 else
1293 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
Diego Santa Cruz037dc0a2014-12-23 10:50:25 +01001294 }
Oliver Metz1937e5a2013-10-01 20:32:07 +02001295
Diego Santa Cruz037dc0a2014-12-23 10:50:25 +01001296 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
Oliver Metz1937e5a2013-10-01 20:32:07 +02001297 /* Read out group size from ext_csd */
Lei Wen0560db12011-10-03 20:35:10 +00001298 mmc->erase_grp_size =
Diego Santa Cruza4ff9f82014-12-23 10:50:24 +01001299 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
Markus Niebeld7b29122014-11-18 15:11:42 +01001300 /*
1301 * if high capacity and partition setting completed
1302 * SEC_COUNT is valid even if it is smaller than 2 GiB
1303 * JEDEC Standard JESD84-B45, 6.2.4
1304 */
Diego Santa Cruz8a0cf492014-12-23 10:50:27 +01001305 if (mmc->high_capacity && part_completed) {
Markus Niebeld7b29122014-11-18 15:11:42 +01001306 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1307 (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1308 (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1309 (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1310 capacity *= MMC_MAX_BLOCK_LEN;
1311 mmc->capacity_user = capacity;
1312 }
Simon Glass8bfa1952013-04-03 08:54:30 +00001313 } else {
Oliver Metz1937e5a2013-10-01 20:32:07 +02001314 /* Calculate the group size from the csd value. */
Lei Wene6f99a52011-06-22 17:03:31 +00001315 int erase_gsz, erase_gmul;
1316 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1317 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1318 mmc->erase_grp_size = (erase_gsz + 1)
1319 * (erase_gmul + 1);
1320 }
Diego Santa Cruz037dc0a2014-12-23 10:50:25 +01001321
1322 mmc->hc_wp_grp_size = 1024
1323 * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1324 * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Diego Santa Cruz9e41a002014-12-23 10:50:33 +01001325
1326 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301327 }
1328
Stephen Warrenf866a462013-06-11 15:14:01 -06001329 err = mmc_set_capacity(mmc, mmc->part_num);
1330 if (err)
1331 return err;
1332
Andy Fleming272cc702008-10-30 16:41:01 -05001333 if (IS_SD(mmc))
1334 err = sd_change_freq(mmc);
1335 else
1336 err = mmc_change_freq(mmc);
1337
1338 if (err)
1339 return err;
1340
1341 /* Restrict card's capabilities by what the host can do */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001342 mmc->card_caps &= mmc->cfg->host_caps;
Andy Fleming272cc702008-10-30 16:41:01 -05001343
1344 if (IS_SD(mmc)) {
1345 if (mmc->card_caps & MMC_MODE_4BIT) {
1346 cmd.cmdidx = MMC_CMD_APP_CMD;
1347 cmd.resp_type = MMC_RSP_R1;
1348 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001349
1350 err = mmc_send_cmd(mmc, &cmd, NULL);
1351 if (err)
1352 return err;
1353
1354 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1355 cmd.resp_type = MMC_RSP_R1;
1356 cmd.cmdarg = 2;
Andy Fleming272cc702008-10-30 16:41:01 -05001357 err = mmc_send_cmd(mmc, &cmd, NULL);
1358 if (err)
1359 return err;
1360
1361 mmc_set_bus_width(mmc, 4);
1362 }
1363
1364 if (mmc->card_caps & MMC_MODE_HS)
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001365 mmc->tran_speed = 50000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001366 else
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001367 mmc->tran_speed = 25000000;
Andrew Gabbasovfc5b32f2014-12-25 10:22:25 -06001368 } else if (mmc->version >= MMC_VERSION_4) {
1369 /* Only version 4 of MMC supports wider bus widths */
Andy Fleming7798f6d2012-10-31 19:02:38 +00001370 int idx;
1371
1372 /* An array of possible bus widths in order of preference */
1373 static unsigned ext_csd_bits[] = {
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001374 EXT_CSD_DDR_BUS_WIDTH_8,
1375 EXT_CSD_DDR_BUS_WIDTH_4,
Andy Fleming7798f6d2012-10-31 19:02:38 +00001376 EXT_CSD_BUS_WIDTH_8,
1377 EXT_CSD_BUS_WIDTH_4,
1378 EXT_CSD_BUS_WIDTH_1,
1379 };
1380
1381 /* An array to map CSD bus widths to host cap bits */
1382 static unsigned ext_to_hostcaps[] = {
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001383 [EXT_CSD_DDR_BUS_WIDTH_4] =
1384 MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1385 [EXT_CSD_DDR_BUS_WIDTH_8] =
1386 MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
Andy Fleming7798f6d2012-10-31 19:02:38 +00001387 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1388 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1389 };
1390
1391 /* An array to map chosen bus width to an integer */
1392 static unsigned widths[] = {
Jaehoon Chungd22e3d42014-05-16 13:59:54 +09001393 8, 4, 8, 4, 1,
Andy Fleming7798f6d2012-10-31 19:02:38 +00001394 };
1395
1396 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1397 unsigned int extw = ext_csd_bits[idx];
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001398 unsigned int caps = ext_to_hostcaps[extw];
Andy Fleming7798f6d2012-10-31 19:02:38 +00001399
1400 /*
Andrew Gabbasovbf477072014-12-25 10:22:24 -06001401 * If the bus width is still not changed,
1402 * don't try to set the default again.
1403 * Otherwise, recover from switch attempts
1404 * by switching to 1-bit bus width.
1405 */
1406 if (extw == EXT_CSD_BUS_WIDTH_1 &&
1407 mmc->bus_width == 1) {
1408 err = 0;
1409 break;
1410 }
1411
1412 /*
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001413 * Check to make sure the card and controller support
1414 * these capabilities
Andy Fleming7798f6d2012-10-31 19:02:38 +00001415 */
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001416 if ((mmc->card_caps & caps) != caps)
Andy Fleming7798f6d2012-10-31 19:02:38 +00001417 continue;
1418
Andy Fleming272cc702008-10-30 16:41:01 -05001419 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
Andy Fleming7798f6d2012-10-31 19:02:38 +00001420 EXT_CSD_BUS_WIDTH, extw);
Andy Fleming272cc702008-10-30 16:41:01 -05001421
1422 if (err)
Lei Wen41378942011-10-03 20:35:11 +00001423 continue;
Andy Fleming272cc702008-10-30 16:41:01 -05001424
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001425 mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
Andy Fleming7798f6d2012-10-31 19:02:38 +00001426 mmc_set_bus_width(mmc, widths[idx]);
Andy Fleming272cc702008-10-30 16:41:01 -05001427
Lei Wen41378942011-10-03 20:35:11 +00001428 err = mmc_send_ext_csd(mmc, test_csd);
Andy Fleming272cc702008-10-30 16:41:01 -05001429
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001430 if (err)
1431 continue;
1432
1433 /* Only compare read only fields */
1434 if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1435 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1436 ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1437 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1438 ext_csd[EXT_CSD_REV]
1439 == test_csd[EXT_CSD_REV] &&
1440 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1441 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1442 memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1443 &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
Lei Wen41378942011-10-03 20:35:11 +00001444 break;
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001445 else
1446 err = SWITCH_ERR;
Andy Fleming272cc702008-10-30 16:41:01 -05001447 }
1448
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001449 if (err)
1450 return err;
1451
Andy Fleming272cc702008-10-30 16:41:01 -05001452 if (mmc->card_caps & MMC_MODE_HS) {
1453 if (mmc->card_caps & MMC_MODE_HS_52MHz)
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001454 mmc->tran_speed = 52000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001455 else
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001456 mmc->tran_speed = 26000000;
1457 }
Andy Fleming272cc702008-10-30 16:41:01 -05001458 }
1459
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001460 mmc_set_clock(mmc, mmc->tran_speed);
1461
Andrew Gabbasov5af8f452014-12-01 06:59:11 -06001462 /* Fix the block length for DDR mode */
1463 if (mmc->ddr_mode) {
1464 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1465 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1466 }
1467
Andy Fleming272cc702008-10-30 16:41:01 -05001468 /* fill in device description */
1469 mmc->block_dev.lun = 0;
1470 mmc->block_dev.type = 0;
1471 mmc->block_dev.blksz = mmc->read_bl_len;
Egbert Eich0472fbf2013-04-09 21:11:56 +00001472 mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
Rabin Vincent9b1f9422009-04-05 13:30:54 +05301473 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Sjoerd Simonsfc011f62015-12-04 23:27:40 +01001474#if !defined(CONFIG_SPL_BUILD) || \
1475 (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
1476 !defined(CONFIG_USE_TINY_PRINTF))
Taylor Huttbabce5f2012-10-20 17:15:59 +00001477 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1478 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1479 (mmc->cid[3] >> 16) & 0xffff);
1480 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1481 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1482 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1483 (mmc->cid[2] >> 24) & 0xff);
1484 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1485 (mmc->cid[2] >> 16) & 0xf);
Paul Burton56196822013-09-04 16:12:25 +01001486#else
1487 mmc->block_dev.vendor[0] = 0;
1488 mmc->block_dev.product[0] = 0;
1489 mmc->block_dev.revision[0] = 0;
1490#endif
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001491#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -05001492 init_part(&mmc->block_dev);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001493#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001494
1495 return 0;
1496}
1497
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001498static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001499{
1500 struct mmc_cmd cmd;
1501 int err;
1502
1503 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1504 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001505 cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
Andy Fleming272cc702008-10-30 16:41:01 -05001506 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05001507
1508 err = mmc_send_cmd(mmc, &cmd, NULL);
1509
1510 if (err)
1511 return err;
1512
Rabin Vincent998be3d2009-04-05 13:30:56 +05301513 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -05001514 return UNUSABLE_ERR;
1515 else
1516 mmc->version = SD_VERSION_2;
1517
1518 return 0;
1519}
1520
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001521/* not used any more */
1522int __deprecated mmc_register(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001523{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001524#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1525 printf("%s is deprecated! use mmc_create() instead.\n", __func__);
1526#endif
1527 return -1;
1528}
1529
1530struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
1531{
1532 struct mmc *mmc;
1533
1534 /* quick validation */
1535 if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
1536 cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
1537 return NULL;
1538
1539 mmc = calloc(1, sizeof(*mmc));
1540 if (mmc == NULL)
1541 return NULL;
1542
1543 mmc->cfg = cfg;
1544 mmc->priv = priv;
1545
1546 /* the following chunk was mmc_register() */
1547
Markus Niebelab711882013-12-16 13:40:46 +01001548 /* Setup dsr related values */
1549 mmc->dsr_imp = 0;
1550 mmc->dsr = 0xffffffff;
Andy Fleming272cc702008-10-30 16:41:01 -05001551 /* Setup the universal parts of the block interface just once */
1552 mmc->block_dev.if_type = IF_TYPE_MMC;
1553 mmc->block_dev.dev = cur_dev_num++;
1554 mmc->block_dev.removable = 1;
1555 mmc->block_dev.block_read = mmc_bread;
1556 mmc->block_dev.block_write = mmc_bwrite;
Lei Wene6f99a52011-06-22 17:03:31 +00001557 mmc->block_dev.block_erase = mmc_berase;
Andy Fleming272cc702008-10-30 16:41:01 -05001558
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001559 /* setup initial part type */
1560 mmc->block_dev.part_type = mmc->cfg->part_type;
Andy Fleming272cc702008-10-30 16:41:01 -05001561
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001562 INIT_LIST_HEAD(&mmc->link);
Andy Fleming272cc702008-10-30 16:41:01 -05001563
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001564 list_add_tail(&mmc->link, &mmc_devices);
1565
1566 return mmc;
1567}
1568
1569void mmc_destroy(struct mmc *mmc)
1570{
1571 /* only freeing memory for now */
1572 free(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001573}
1574
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001575#ifdef CONFIG_PARTITIONS
Andy Fleming272cc702008-10-30 16:41:01 -05001576block_dev_desc_t *mmc_get_dev(int dev)
1577{
1578 struct mmc *mmc = find_mmc_device(dev);
Benoît Thébaudeau6bb4b4b2012-08-10 08:59:12 +00001579 if (!mmc || mmc_init(mmc))
Łukasz Majewski40242bc2012-04-19 02:39:18 +00001580 return NULL;
Andy Fleming272cc702008-10-30 16:41:01 -05001581
Łukasz Majewski40242bc2012-04-19 02:39:18 +00001582 return &mmc->block_dev;
Andy Fleming272cc702008-10-30 16:41:01 -05001583}
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001584#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001585
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001586/* board-specific MMC power initializations. */
1587__weak void board_mmc_power_init(void)
1588{
1589}
1590
Che-Liang Chioue9550442012-11-28 15:21:13 +00001591int mmc_start_init(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001592{
Macpaul Linafd59322011-11-14 23:35:39 +00001593 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05001594
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001595 /* we pretend there's no card when init is NULL */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001596 if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
Thierry Reding48972d92012-01-02 01:15:37 +00001597 mmc->has_init = 0;
Paul Burton56196822013-09-04 16:12:25 +01001598#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Thierry Reding48972d92012-01-02 01:15:37 +00001599 printf("MMC: no card present\n");
Paul Burton56196822013-09-04 16:12:25 +01001600#endif
Thierry Reding48972d92012-01-02 01:15:37 +00001601 return NO_CARD_ERR;
1602 }
1603
Lei Wenbc897b12011-05-02 16:26:26 +00001604 if (mmc->has_init)
1605 return 0;
1606
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001607#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1608 mmc_adapter_card_type_ident();
1609#endif
Paul Kocialkowski95de9ab2014-11-08 20:55:45 +01001610 board_mmc_power_init();
1611
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001612 /* made sure it's not NULL earlier */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001613 err = mmc->cfg->ops->init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001614
1615 if (err)
1616 return err;
1617
Andrew Gabbasov786e8f82014-12-01 06:59:09 -06001618 mmc->ddr_mode = 0;
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001619 mmc_set_bus_width(mmc, 1);
1620 mmc_set_clock(mmc, 1);
1621
Andy Fleming272cc702008-10-30 16:41:01 -05001622 /* Reset the Card */
1623 err = mmc_go_idle(mmc);
1624
1625 if (err)
1626 return err;
1627
Lei Wenbc897b12011-05-02 16:26:26 +00001628 /* The internal partition reset to user partition(0) at every CMD0*/
1629 mmc->part_num = 0;
1630
Andy Fleming272cc702008-10-30 16:41:01 -05001631 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00001632 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001633
Andy Fleming272cc702008-10-30 16:41:01 -05001634 /* Now try to get the SD card's operating condition */
1635 err = sd_send_op_cond(mmc);
1636
1637 /* If the command timed out, we check for an MMC card */
1638 if (err == TIMEOUT) {
1639 err = mmc_send_op_cond(mmc);
1640
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001641 if (err) {
Paul Burton56196822013-09-04 16:12:25 +01001642#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -05001643 printf("Card did not respond to voltage select!\n");
Paul Burton56196822013-09-04 16:12:25 +01001644#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001645 return UNUSABLE_ERR;
1646 }
1647 }
1648
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001649 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00001650 mmc->init_in_progress = 1;
1651
1652 return err;
1653}
1654
1655static int mmc_complete_init(struct mmc *mmc)
1656{
1657 int err = 0;
1658
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001659 mmc->init_in_progress = 0;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001660 if (mmc->op_cond_pending)
1661 err = mmc_complete_op_cond(mmc);
1662
1663 if (!err)
1664 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00001665 if (err)
1666 mmc->has_init = 0;
1667 else
1668 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001669 return err;
1670}
1671
1672int mmc_init(struct mmc *mmc)
1673{
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001674 int err = 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02001675 unsigned start;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001676
1677 if (mmc->has_init)
1678 return 0;
Mateusz Zalegad803fea2014-04-29 20:15:30 +02001679
1680 start = get_timer(0);
1681
Che-Liang Chioue9550442012-11-28 15:21:13 +00001682 if (!mmc->init_in_progress)
1683 err = mmc_start_init(mmc);
1684
Andrew Gabbasovbd47c132015-03-19 07:44:07 -05001685 if (!err)
Che-Liang Chioue9550442012-11-28 15:21:13 +00001686 err = mmc_complete_init(mmc);
1687 debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
Lei Wenbc897b12011-05-02 16:26:26 +00001688 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001689}
1690
Markus Niebelab711882013-12-16 13:40:46 +01001691int mmc_set_dsr(struct mmc *mmc, u16 val)
1692{
1693 mmc->dsr = val;
1694 return 0;
1695}
1696
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02001697/* CPU-specific MMC initializations */
1698__weak int cpu_mmc_init(bd_t *bis)
Andy Fleming272cc702008-10-30 16:41:01 -05001699{
1700 return -1;
1701}
1702
Jeroen Hofsteecee9ab72014-07-10 22:46:28 +02001703/* board-specific MMC initializations. */
1704__weak int board_mmc_init(bd_t *bis)
1705{
1706 return -1;
1707}
Andy Fleming272cc702008-10-30 16:41:01 -05001708
Paul Burton56196822013-09-04 16:12:25 +01001709#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1710
Andy Fleming272cc702008-10-30 16:41:01 -05001711void print_mmc_devices(char separator)
1712{
1713 struct mmc *m;
1714 struct list_head *entry;
Przemyslaw Marczak34dd9282015-02-20 12:29:27 +01001715 char *mmc_type;
Andy Fleming272cc702008-10-30 16:41:01 -05001716
1717 list_for_each(entry, &mmc_devices) {
1718 m = list_entry(entry, struct mmc, link);
1719
Przemyslaw Marczak34dd9282015-02-20 12:29:27 +01001720 if (m->has_init)
1721 mmc_type = IS_SD(m) ? "SD" : "eMMC";
1722 else
1723 mmc_type = NULL;
1724
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001725 printf("%s: %d", m->cfg->name, m->block_dev.dev);
Przemyslaw Marczak34dd9282015-02-20 12:29:27 +01001726 if (mmc_type)
1727 printf(" (%s)", mmc_type);
Andy Fleming272cc702008-10-30 16:41:01 -05001728
Lubomir Popove75eaf12014-11-11 12:25:42 +02001729 if (entry->next != &mmc_devices) {
1730 printf("%c", separator);
1731 if (separator != '\n')
1732 puts (" ");
1733 }
Andy Fleming272cc702008-10-30 16:41:01 -05001734 }
1735
1736 printf("\n");
1737}
1738
Paul Burton56196822013-09-04 16:12:25 +01001739#else
1740void print_mmc_devices(char separator) { }
1741#endif
1742
Lei Wenea6ebe22011-05-02 16:26:25 +00001743int get_mmc_num(void)
1744{
1745 return cur_dev_num;
1746}
1747
Che-Liang Chioue9550442012-11-28 15:21:13 +00001748void mmc_set_preinit(struct mmc *mmc, int preinit)
1749{
1750 mmc->preinit = preinit;
1751}
1752
1753static void do_preinit(void)
1754{
1755 struct mmc *m;
1756 struct list_head *entry;
1757
1758 list_for_each(entry, &mmc_devices) {
1759 m = list_entry(entry, struct mmc, link);
1760
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001761#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1762 mmc_set_preinit(m, 1);
1763#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00001764 if (m->preinit)
1765 mmc_start_init(m);
1766 }
1767}
1768
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001769#if defined(CONFIG_DM_MMC) && defined(CONFIG_SPL_BUILD)
1770static int mmc_probe(bd_t *bis)
1771{
1772 return 0;
1773}
1774#elif defined(CONFIG_DM_MMC)
1775static int mmc_probe(bd_t *bis)
1776{
1777 int ret;
1778 struct uclass *uc;
1779 struct udevice *m;
1780
1781 ret = uclass_get(UCLASS_MMC, &uc);
1782 if (ret)
1783 return ret;
1784
1785 uclass_foreach_dev(m, uc) {
1786 ret = device_probe(m);
1787 if (ret)
1788 printf("%s - probe failed: %d\n", m->name, ret);
1789 }
1790
1791 return 0;
1792}
1793#else
1794static int mmc_probe(bd_t *bis)
1795{
1796 if (board_mmc_init(bis) < 0)
1797 cpu_mmc_init(bis);
1798
1799 return 0;
1800}
1801#endif
Che-Liang Chioue9550442012-11-28 15:21:13 +00001802
Andy Fleming272cc702008-10-30 16:41:01 -05001803int mmc_initialize(bd_t *bis)
1804{
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02001805 static int initialized = 0;
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001806 int ret;
Daniel Kochmański1b26bab2015-05-29 16:55:43 +02001807 if (initialized) /* Avoid initializing mmc multiple times */
1808 return 0;
1809 initialized = 1;
1810
Andy Fleming272cc702008-10-30 16:41:01 -05001811 INIT_LIST_HEAD (&mmc_devices);
1812 cur_dev_num = 0;
1813
Sjoerd Simons8e3332e2015-08-30 16:55:45 -06001814 ret = mmc_probe(bis);
1815 if (ret)
1816 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -05001817
Ying Zhangbb0dc102013-08-16 15:16:11 +08001818#ifndef CONFIG_SPL_BUILD
Andy Fleming272cc702008-10-30 16:41:01 -05001819 print_mmc_devices(',');
Ying Zhangbb0dc102013-08-16 15:16:11 +08001820#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001821
Che-Liang Chioue9550442012-11-28 15:21:13 +00001822 do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05001823 return 0;
1824}
Amar3690d6d2013-04-27 11:42:58 +05301825
1826#ifdef CONFIG_SUPPORT_EMMC_BOOT
1827/*
1828 * This function changes the size of boot partition and the size of rpmb
1829 * partition present on EMMC devices.
1830 *
1831 * Input Parameters:
1832 * struct *mmc: pointer for the mmc device strcuture
1833 * bootsize: size of boot partition
1834 * rpmbsize: size of rpmb partition
1835 *
1836 * Returns 0 on success.
1837 */
1838
1839int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1840 unsigned long rpmbsize)
1841{
1842 int err;
1843 struct mmc_cmd cmd;
1844
1845 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1846 cmd.cmdidx = MMC_CMD_RES_MAN;
1847 cmd.resp_type = MMC_RSP_R1b;
1848 cmd.cmdarg = MMC_CMD62_ARG1;
1849
1850 err = mmc_send_cmd(mmc, &cmd, NULL);
1851 if (err) {
1852 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1853 return err;
1854 }
1855
1856 /* Boot partition changing mode */
1857 cmd.cmdidx = MMC_CMD_RES_MAN;
1858 cmd.resp_type = MMC_RSP_R1b;
1859 cmd.cmdarg = MMC_CMD62_ARG2;
1860
1861 err = mmc_send_cmd(mmc, &cmd, NULL);
1862 if (err) {
1863 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1864 return err;
1865 }
1866 /* boot partition size is multiple of 128KB */
1867 bootsize = (bootsize * 1024) / 128;
1868
1869 /* Arg: boot partition size */
1870 cmd.cmdidx = MMC_CMD_RES_MAN;
1871 cmd.resp_type = MMC_RSP_R1b;
1872 cmd.cmdarg = bootsize;
1873
1874 err = mmc_send_cmd(mmc, &cmd, NULL);
1875 if (err) {
1876 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1877 return err;
1878 }
1879 /* RPMB partition size is multiple of 128KB */
1880 rpmbsize = (rpmbsize * 1024) / 128;
1881 /* Arg: RPMB partition size */
1882 cmd.cmdidx = MMC_CMD_RES_MAN;
1883 cmd.resp_type = MMC_RSP_R1b;
1884 cmd.cmdarg = rpmbsize;
1885
1886 err = mmc_send_cmd(mmc, &cmd, NULL);
1887 if (err) {
1888 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1889 return err;
1890 }
1891 return 0;
1892}
1893
1894/*
Tom Rini5a99b9d2014-02-05 10:24:22 -05001895 * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
1896 * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
1897 * and BOOT_MODE.
1898 *
1899 * Returns 0 on success.
1900 */
1901int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
1902{
1903 int err;
1904
1905 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
1906 EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
1907 EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
1908 EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
1909
1910 if (err)
1911 return err;
1912 return 0;
1913}
1914
1915/*
Tom Rini792970b2014-02-05 10:24:21 -05001916 * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1917 * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1918 * PARTITION_ACCESS.
1919 *
1920 * Returns 0 on success.
1921 */
1922int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1923{
1924 int err;
1925
1926 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
1927 EXT_CSD_BOOT_ACK(ack) |
1928 EXT_CSD_BOOT_PART_NUM(part_num) |
1929 EXT_CSD_PARTITION_ACCESS(access));
1930
1931 if (err)
1932 return err;
1933 return 0;
1934}
Tom Rini33ace362014-02-07 14:15:20 -05001935
1936/*
1937 * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
1938 * for enable. Note that this is a write-once field for non-zero values.
1939 *
1940 * Returns 0 on success.
1941 */
1942int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
1943{
1944 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,
1945 enable);
1946}
Amar3690d6d2013-04-27 11:42:58 +05301947#endif