blob: 73f71957926b8c73adcd3a37ccd2917ddbbc53e1 [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 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <config.h>
27#include <common.h>
28#include <command.h>
29#include <mmc.h>
30#include <part.h>
31#include <malloc.h>
32#include <linux/list.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053033#include <div64.h>
Andy Fleming272cc702008-10-30 16:41:01 -050034
Matt Waddelce0fbcd2011-02-24 16:35:23 +000035/* Set block count limit because of 16 bit register limit on some hardware*/
36#ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
37#define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
38#endif
39
Andy Fleming272cc702008-10-30 16:41:01 -050040static struct list_head mmc_devices;
41static int cur_dev_num = -1;
42
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000043int __weak board_mmc_getwp(struct mmc *mmc)
44{
45 return -1;
46}
47
48int mmc_getwp(struct mmc *mmc)
49{
50 int wp;
51
52 wp = board_mmc_getwp(mmc);
53
Peter Korsgaardd4e1da42013-03-21 04:00:03 +000054 if (wp < 0) {
55 if (mmc->getwp)
56 wp = mmc->getwp(mmc);
57 else
58 wp = 0;
59 }
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +000060
61 return wp;
62}
63
Thierry Reding314284b2012-01-02 01:15:36 +000064int __board_mmc_getcd(struct mmc *mmc) {
Stefano Babic11fdade2010-02-05 15:04:43 +010065 return -1;
66}
67
Thierry Reding314284b2012-01-02 01:15:36 +000068int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
Stefano Babic11fdade2010-02-05 15:04:43 +010069 alias("__board_mmc_getcd")));
70
Kim Phillipsfdbb8732012-10-29 13:34:43 +000071static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
72 struct mmc_data *data)
Andy Fleming272cc702008-10-30 16:41:01 -050073{
Marek Vasut8635ff92012-03-15 18:41:35 +000074 struct mmc_data backup;
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000075 int ret;
Marek Vasut8635ff92012-03-15 18:41:35 +000076
77 memset(&backup, 0, sizeof(backup));
78
Marek Vasut8635ff92012-03-15 18:41:35 +000079#ifdef CONFIG_MMC_TRACE
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000080 int i;
81 u8 *ptr;
82
83 printf("CMD_SEND:%d\n", cmd->cmdidx);
84 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +000085 ret = mmc->send_cmd(mmc, cmd, data);
86 switch (cmd->resp_type) {
87 case MMC_RSP_NONE:
88 printf("\t\tMMC_RSP_NONE\n");
89 break;
90 case MMC_RSP_R1:
91 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
92 cmd->response[0]);
93 break;
94 case MMC_RSP_R1b:
95 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
96 cmd->response[0]);
97 break;
98 case MMC_RSP_R2:
99 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
100 cmd->response[0]);
101 printf("\t\t \t\t 0x%08X \n",
102 cmd->response[1]);
103 printf("\t\t \t\t 0x%08X \n",
104 cmd->response[2]);
105 printf("\t\t \t\t 0x%08X \n",
106 cmd->response[3]);
107 printf("\n");
108 printf("\t\t\t\t\tDUMPING DATA\n");
109 for (i = 0; i < 4; i++) {
110 int j;
111 printf("\t\t\t\t\t%03d - ", i*4);
Dirk Behme146bec72012-03-08 02:35:34 +0000112 ptr = (u8 *)&cmd->response[i];
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000113 ptr += 3;
114 for (j = 0; j < 4; j++)
115 printf("%02X ", *ptr--);
116 printf("\n");
117 }
118 break;
119 case MMC_RSP_R3:
120 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
121 cmd->response[0]);
122 break;
123 default:
124 printf("\t\tERROR MMC rsp not supported\n");
125 break;
126 }
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000127#else
Marek Vasut8635ff92012-03-15 18:41:35 +0000128 ret = mmc->send_cmd(mmc, cmd, data);
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000129#endif
Marek Vasut8635ff92012-03-15 18:41:35 +0000130 return ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500131}
132
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000133static int mmc_send_status(struct mmc *mmc, int timeout)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000134{
135 struct mmc_cmd cmd;
Jan Kloetzked617c422012-02-05 22:29:12 +0000136 int err, retries = 5;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000137#ifdef CONFIG_MMC_TRACE
138 int status;
139#endif
140
141 cmd.cmdidx = MMC_CMD_SEND_STATUS;
142 cmd.resp_type = MMC_RSP_R1;
Marek Vasutaaf3d412011-08-10 09:24:48 +0200143 if (!mmc_host_is_spi(mmc))
144 cmd.cmdarg = mmc->rca << 16;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000145
146 do {
147 err = mmc_send_cmd(mmc, &cmd, NULL);
Jan Kloetzked617c422012-02-05 22:29:12 +0000148 if (!err) {
149 if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
150 (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
151 MMC_STATE_PRG)
152 break;
153 else if (cmd.response[0] & MMC_STATUS_MASK) {
154 printf("Status Error: 0x%08X\n",
155 cmd.response[0]);
156 return COMM_ERR;
157 }
158 } else if (--retries < 0)
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000159 return err;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000160
161 udelay(1000);
162
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000163 } while (timeout--);
164
Raffaele Recalcati5db2fe32011-03-11 02:01:14 +0000165#ifdef CONFIG_MMC_TRACE
166 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
167 printf("CURR STATE:%d\n", status);
168#endif
Jongman Heo5b0c9422012-06-03 21:32:13 +0000169 if (timeout <= 0) {
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000170 printf("Timeout waiting card ready\n");
171 return TIMEOUT;
172 }
173
174 return 0;
175}
176
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000177static int mmc_set_blocklen(struct mmc *mmc, int len)
Andy Fleming272cc702008-10-30 16:41:01 -0500178{
179 struct mmc_cmd cmd;
180
181 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
182 cmd.resp_type = MMC_RSP_R1;
183 cmd.cmdarg = len;
Andy Fleming272cc702008-10-30 16:41:01 -0500184
185 return mmc_send_cmd(mmc, &cmd, NULL);
186}
187
188struct mmc *find_mmc_device(int dev_num)
189{
190 struct mmc *m;
191 struct list_head *entry;
192
193 list_for_each(entry, &mmc_devices) {
194 m = list_entry(entry, struct mmc, link);
195
196 if (m->block_dev.dev == dev_num)
197 return m;
198 }
199
200 printf("MMC Device %d not found\n", dev_num);
201
202 return NULL;
203}
204
Lei Wene6f99a52011-06-22 17:03:31 +0000205static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
206{
207 struct mmc_cmd cmd;
208 ulong end;
209 int err, start_cmd, end_cmd;
210
211 if (mmc->high_capacity)
212 end = start + blkcnt - 1;
213 else {
214 end = (start + blkcnt - 1) * mmc->write_bl_len;
215 start *= mmc->write_bl_len;
216 }
217
218 if (IS_SD(mmc)) {
219 start_cmd = SD_CMD_ERASE_WR_BLK_START;
220 end_cmd = SD_CMD_ERASE_WR_BLK_END;
221 } else {
222 start_cmd = MMC_CMD_ERASE_GROUP_START;
223 end_cmd = MMC_CMD_ERASE_GROUP_END;
224 }
225
226 cmd.cmdidx = start_cmd;
227 cmd.cmdarg = start;
228 cmd.resp_type = MMC_RSP_R1;
Lei Wene6f99a52011-06-22 17:03:31 +0000229
230 err = mmc_send_cmd(mmc, &cmd, NULL);
231 if (err)
232 goto err_out;
233
234 cmd.cmdidx = end_cmd;
235 cmd.cmdarg = end;
236
237 err = mmc_send_cmd(mmc, &cmd, NULL);
238 if (err)
239 goto err_out;
240
241 cmd.cmdidx = MMC_CMD_ERASE;
242 cmd.cmdarg = SECURE_ERASE;
243 cmd.resp_type = MMC_RSP_R1b;
244
245 err = mmc_send_cmd(mmc, &cmd, NULL);
246 if (err)
247 goto err_out;
248
249 return 0;
250
251err_out:
252 puts("mmc erase failed\n");
253 return err;
254}
255
256static unsigned long
Sascha Silbeff8fef52013-06-14 13:07:25 +0200257mmc_berase(int dev_num, lbaint_t start, lbaint_t blkcnt)
Lei Wene6f99a52011-06-22 17:03:31 +0000258{
259 int err = 0;
260 struct mmc *mmc = find_mmc_device(dev_num);
261 lbaint_t blk = 0, blk_r = 0;
Jerry Huangd2d8afa2012-05-17 23:00:51 +0000262 int timeout = 1000;
Lei Wene6f99a52011-06-22 17:03:31 +0000263
264 if (!mmc)
265 return -1;
266
267 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
268 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
Sascha Silbeff8fef52013-06-14 13:07:25 +0200269 "The erase range would be change to "
270 "0x" LBAF "~0x" LBAF "\n\n",
Lei Wene6f99a52011-06-22 17:03:31 +0000271 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
272 ((start + blkcnt + mmc->erase_grp_size)
273 & ~(mmc->erase_grp_size - 1)) - 1);
274
275 while (blk < blkcnt) {
276 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
277 mmc->erase_grp_size : (blkcnt - blk);
278 err = mmc_erase_t(mmc, start + blk, blk_r);
279 if (err)
280 break;
281
282 blk += blk_r;
Jerry Huangd2d8afa2012-05-17 23:00:51 +0000283
284 /* Waiting for the ready status */
285 if (mmc_send_status(mmc, timeout))
286 return 0;
Lei Wene6f99a52011-06-22 17:03:31 +0000287 }
288
289 return blk;
290}
291
Andy Fleming272cc702008-10-30 16:41:01 -0500292static ulong
Sascha Silbeff8fef52013-06-14 13:07:25 +0200293mmc_write_blocks(struct mmc *mmc, lbaint_t start, lbaint_t blkcnt, const void*src)
Andy Fleming272cc702008-10-30 16:41:01 -0500294{
295 struct mmc_cmd cmd;
296 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000297 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500298
Lei Wend2bf29e2010-09-13 22:07:27 +0800299 if ((start + blkcnt) > mmc->block_dev.lba) {
Sascha Silbeff8fef52013-06-14 13:07:25 +0200300 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800301 start + blkcnt, mmc->block_dev.lba);
302 return 0;
303 }
Andy Fleming272cc702008-10-30 16:41:01 -0500304
Ruud Commandeura586c0a2013-05-22 13:19:43 +0200305 if (blkcnt == 0)
306 return 0;
307 else if (blkcnt == 1)
Andy Fleming272cc702008-10-30 16:41:01 -0500308 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
Ruud Commandeura586c0a2013-05-22 13:19:43 +0200309 else
310 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500311
312 if (mmc->high_capacity)
313 cmd.cmdarg = start;
314 else
Steve Sakomandef412b2010-10-28 09:00:26 -0700315 cmd.cmdarg = start * mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500316
317 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500318
319 data.src = src;
320 data.blocks = blkcnt;
Steve Sakomandef412b2010-10-28 09:00:26 -0700321 data.blocksize = mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500322 data.flags = MMC_DATA_WRITE;
323
Steve Sakomandef412b2010-10-28 09:00:26 -0700324 if (mmc_send_cmd(mmc, &cmd, &data)) {
325 printf("mmc write failed\n");
326 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500327 }
328
Thomas Choud52ebf12010-12-24 13:12:21 +0000329 /* SPI multiblock writes terminate using a special
330 * token, not a STOP_TRANSMISSION request.
331 */
332 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500333 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
334 cmd.cmdarg = 0;
335 cmd.resp_type = MMC_RSP_R1b;
Steve Sakomandef412b2010-10-28 09:00:26 -0700336 if (mmc_send_cmd(mmc, &cmd, NULL)) {
337 printf("mmc fail to send stop cmd\n");
338 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800339 }
Andy Fleming272cc702008-10-30 16:41:01 -0500340 }
341
Jan Kloetzke93ad0d12012-02-05 22:29:11 +0000342 /* Waiting for the ready status */
343 if (mmc_send_status(mmc, timeout))
344 return 0;
345
Andy Fleming272cc702008-10-30 16:41:01 -0500346 return blkcnt;
347}
348
Lei Wen01581262010-10-14 13:38:11 +0800349static ulong
Sascha Silbeff8fef52013-06-14 13:07:25 +0200350mmc_bwrite(int dev_num, lbaint_t start, lbaint_t blkcnt, const void*src)
Lei Wen01581262010-10-14 13:38:11 +0800351{
Lei Wen01581262010-10-14 13:38:11 +0800352 lbaint_t cur, blocks_todo = blkcnt;
353
Steve Sakomandef412b2010-10-28 09:00:26 -0700354 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen01581262010-10-14 13:38:11 +0800355 if (!mmc)
Steve Sakomandef412b2010-10-28 09:00:26 -0700356 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800357
Steve Sakomandef412b2010-10-28 09:00:26 -0700358 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
359 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800360
361 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000362 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Lei Wen01581262010-10-14 13:38:11 +0800363 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomandef412b2010-10-28 09:00:26 -0700364 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800365 blocks_todo -= cur;
366 start += cur;
367 src += cur * mmc->write_bl_len;
368 } while (blocks_todo > 0);
369
370 return blkcnt;
371}
372
Sascha Silbeff8fef52013-06-14 13:07:25 +0200373static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000374 lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500375{
376 struct mmc_cmd cmd;
377 struct mmc_data data;
378
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700379 if (blkcnt > 1)
380 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
381 else
382 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500383
384 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700385 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500386 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700387 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500388
389 cmd.resp_type = MMC_RSP_R1;
Andy Fleming272cc702008-10-30 16:41:01 -0500390
391 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700392 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500393 data.blocksize = mmc->read_bl_len;
394 data.flags = MMC_DATA_READ;
395
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700396 if (mmc_send_cmd(mmc, &cmd, &data))
397 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500398
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700399 if (blkcnt > 1) {
400 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
401 cmd.cmdarg = 0;
402 cmd.resp_type = MMC_RSP_R1b;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700403 if (mmc_send_cmd(mmc, &cmd, NULL)) {
404 printf("mmc fail to send stop cmd\n");
405 return 0;
406 }
Andy Fleming272cc702008-10-30 16:41:01 -0500407 }
408
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700409 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500410}
411
Sascha Silbeff8fef52013-06-14 13:07:25 +0200412static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
Andy Fleming272cc702008-10-30 16:41:01 -0500413{
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700414 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500415
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700416 if (blkcnt == 0)
417 return 0;
418
419 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500420 if (!mmc)
421 return 0;
422
Lei Wend2bf29e2010-09-13 22:07:27 +0800423 if ((start + blkcnt) > mmc->block_dev.lba) {
Sascha Silbeff8fef52013-06-14 13:07:25 +0200424 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800425 start + blkcnt, mmc->block_dev.lba);
426 return 0;
427 }
Andy Fleming272cc702008-10-30 16:41:01 -0500428
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700429 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Fleming272cc702008-10-30 16:41:01 -0500430 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500431
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700432 do {
John Rigby8feafcc2011-04-18 05:50:08 +0000433 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700434 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
435 return 0;
436 blocks_todo -= cur;
437 start += cur;
438 dst += cur * mmc->read_bl_len;
439 } while (blocks_todo > 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500440
441 return blkcnt;
442}
443
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000444static int mmc_go_idle(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500445{
446 struct mmc_cmd cmd;
447 int err;
448
449 udelay(1000);
450
451 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
452 cmd.cmdarg = 0;
453 cmd.resp_type = MMC_RSP_NONE;
Andy Fleming272cc702008-10-30 16:41:01 -0500454
455 err = mmc_send_cmd(mmc, &cmd, NULL);
456
457 if (err)
458 return err;
459
460 udelay(2000);
461
462 return 0;
463}
464
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000465static int sd_send_op_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500466{
467 int timeout = 1000;
468 int err;
469 struct mmc_cmd cmd;
470
471 do {
472 cmd.cmdidx = MMC_CMD_APP_CMD;
473 cmd.resp_type = MMC_RSP_R1;
474 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500475
476 err = mmc_send_cmd(mmc, &cmd, NULL);
477
478 if (err)
479 return err;
480
481 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
482 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100483
484 /*
485 * Most cards do not answer if some reserved bits
486 * in the ocr are set. However, Some controller
487 * can set bit 7 (reserved for low voltages), but
488 * how to manage low voltages SD card is not yet
489 * specified.
490 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000491 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
492 (mmc->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500493
494 if (mmc->version == SD_VERSION_2)
495 cmd.cmdarg |= OCR_HCS;
496
497 err = mmc_send_cmd(mmc, &cmd, NULL);
498
499 if (err)
500 return err;
501
502 udelay(1000);
503 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
504
505 if (timeout <= 0)
506 return UNUSABLE_ERR;
507
508 if (mmc->version != SD_VERSION_2)
509 mmc->version = SD_VERSION_1_0;
510
Thomas Choud52ebf12010-12-24 13:12:21 +0000511 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
512 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
513 cmd.resp_type = MMC_RSP_R3;
514 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000515
516 err = mmc_send_cmd(mmc, &cmd, NULL);
517
518 if (err)
519 return err;
520 }
521
Rabin Vincent998be3d2009-04-05 13:30:56 +0530522 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500523
524 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
525 mmc->rca = 0;
526
527 return 0;
528}
529
Che-Liang Chioue9550442012-11-28 15:21:13 +0000530/* We pass in the cmd since otherwise the init seems to fail */
531static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
532 int use_arg)
Andy Fleming272cc702008-10-30 16:41:01 -0500533{
Andy Fleming272cc702008-10-30 16:41:01 -0500534 int err;
535
Che-Liang Chioue9550442012-11-28 15:21:13 +0000536 cmd->cmdidx = MMC_CMD_SEND_OP_COND;
537 cmd->resp_type = MMC_RSP_R3;
538 cmd->cmdarg = 0;
539 if (use_arg && !mmc_host_is_spi(mmc)) {
540 cmd->cmdarg =
541 (mmc->voltages &
542 (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
543 (mmc->op_cond_response & OCR_ACCESS_MODE);
544
545 if (mmc->host_caps & MMC_MODE_HC)
546 cmd->cmdarg |= OCR_HCS;
547 }
548 err = mmc_send_cmd(mmc, cmd, NULL);
549 if (err)
550 return err;
551 mmc->op_cond_response = cmd->response[0];
552 return 0;
553}
554
555int mmc_send_op_cond(struct mmc *mmc)
556{
557 struct mmc_cmd cmd;
558 int err, i;
559
Andy Fleming272cc702008-10-30 16:41:01 -0500560 /* Some cards seem to need this */
561 mmc_go_idle(mmc);
562
Raffaele Recalcati31cacba2011-03-11 02:01:13 +0000563 /* Asking to the card its capabilities */
Che-Liang Chioue9550442012-11-28 15:21:13 +0000564 mmc->op_cond_pending = 1;
565 for (i = 0; i < 2; i++) {
566 err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
Andy Fleming272cc702008-10-30 16:41:01 -0500567 if (err)
568 return err;
569
Che-Liang Chioue9550442012-11-28 15:21:13 +0000570 /* exit if not busy (flag seems to be inverted) */
571 if (mmc->op_cond_response & OCR_BUSY)
572 return 0;
573 }
574 return IN_PROGRESS;
575}
Andy Fleming272cc702008-10-30 16:41:01 -0500576
Che-Liang Chioue9550442012-11-28 15:21:13 +0000577int mmc_complete_op_cond(struct mmc *mmc)
578{
579 struct mmc_cmd cmd;
580 int timeout = 1000;
581 uint start;
582 int err;
583
584 mmc->op_cond_pending = 0;
585 start = get_timer(0);
586 do {
587 err = mmc_send_op_cond_iter(mmc, &cmd, 1);
588 if (err)
589 return err;
590 if (get_timer(start) > timeout)
591 return UNUSABLE_ERR;
592 udelay(100);
593 } while (!(mmc->op_cond_response & OCR_BUSY));
Andy Fleming272cc702008-10-30 16:41:01 -0500594
Thomas Choud52ebf12010-12-24 13:12:21 +0000595 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
596 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
597 cmd.resp_type = MMC_RSP_R3;
598 cmd.cmdarg = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000599
600 err = mmc_send_cmd(mmc, &cmd, NULL);
601
602 if (err)
603 return err;
604 }
605
Andy Fleming272cc702008-10-30 16:41:01 -0500606 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530607 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500608
609 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
610 mmc->rca = 0;
611
612 return 0;
613}
614
615
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000616static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
Andy Fleming272cc702008-10-30 16:41:01 -0500617{
618 struct mmc_cmd cmd;
619 struct mmc_data data;
620 int err;
621
622 /* Get the Card Status Register */
623 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
624 cmd.resp_type = MMC_RSP_R1;
625 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500626
Yoshihiro Shimodacdfd1ac2012-06-07 19:09:11 +0000627 data.dest = (char *)ext_csd;
Andy Fleming272cc702008-10-30 16:41:01 -0500628 data.blocks = 1;
Simon Glass8bfa1952013-04-03 08:54:30 +0000629 data.blocksize = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -0500630 data.flags = MMC_DATA_READ;
631
632 err = mmc_send_cmd(mmc, &cmd, &data);
633
634 return err;
635}
636
637
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000638static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
Andy Fleming272cc702008-10-30 16:41:01 -0500639{
640 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000641 int timeout = 1000;
642 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500643
644 cmd.cmdidx = MMC_CMD_SWITCH;
645 cmd.resp_type = MMC_RSP_R1b;
646 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000647 (index << 16) |
648 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500649
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000650 ret = mmc_send_cmd(mmc, &cmd, NULL);
651
652 /* Waiting for the ready status */
Jan Kloetzke93ad0d12012-02-05 22:29:11 +0000653 if (!ret)
654 ret = mmc_send_status(mmc, timeout);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000655
656 return ret;
657
Andy Fleming272cc702008-10-30 16:41:01 -0500658}
659
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000660static int mmc_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500661{
Simon Glass8bfa1952013-04-03 08:54:30 +0000662 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
Andy Fleming272cc702008-10-30 16:41:01 -0500663 char cardtype;
664 int err;
665
666 mmc->card_caps = 0;
667
Thomas Choud52ebf12010-12-24 13:12:21 +0000668 if (mmc_host_is_spi(mmc))
669 return 0;
670
Andy Fleming272cc702008-10-30 16:41:01 -0500671 /* Only version 4 supports high-speed */
672 if (mmc->version < MMC_VERSION_4)
673 return 0;
674
Andy Fleming272cc702008-10-30 16:41:01 -0500675 err = mmc_send_ext_csd(mmc, ext_csd);
676
677 if (err)
678 return err;
679
Lei Wen0560db12011-10-03 20:35:10 +0000680 cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500681
682 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
683
684 if (err)
685 return err;
686
687 /* Now check to see that it worked */
688 err = mmc_send_ext_csd(mmc, ext_csd);
689
690 if (err)
691 return err;
692
693 /* No high-speed support */
Lei Wen0560db12011-10-03 20:35:10 +0000694 if (!ext_csd[EXT_CSD_HS_TIMING])
Andy Fleming272cc702008-10-30 16:41:01 -0500695 return 0;
696
697 /* High Speed is set, there are two types: 52MHz and 26MHz */
698 if (cardtype & MMC_HS_52MHZ)
699 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
700 else
701 mmc->card_caps |= MMC_MODE_HS;
702
703 return 0;
704}
705
Stephen Warrenf866a462013-06-11 15:14:01 -0600706static int mmc_set_capacity(struct mmc *mmc, int part_num)
707{
708 switch (part_num) {
709 case 0:
710 mmc->capacity = mmc->capacity_user;
711 break;
712 case 1:
713 case 2:
714 mmc->capacity = mmc->capacity_boot;
715 break;
716 case 3:
717 mmc->capacity = mmc->capacity_rpmb;
718 break;
719 case 4:
720 case 5:
721 case 6:
722 case 7:
723 mmc->capacity = mmc->capacity_gp[part_num - 4];
724 break;
725 default:
726 return -1;
727 }
728
729 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
730
731 return 0;
732}
733
Lei Wenbc897b12011-05-02 16:26:26 +0000734int mmc_switch_part(int dev_num, unsigned int part_num)
735{
736 struct mmc *mmc = find_mmc_device(dev_num);
Stephen Warrenf866a462013-06-11 15:14:01 -0600737 int ret;
Lei Wenbc897b12011-05-02 16:26:26 +0000738
739 if (!mmc)
740 return -1;
741
Stephen Warrenf866a462013-06-11 15:14:01 -0600742 ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
743 (mmc->part_config & ~PART_ACCESS_MASK)
744 | (part_num & PART_ACCESS_MASK));
745 if (ret)
746 return ret;
747
748 return mmc_set_capacity(mmc, part_num);
Lei Wenbc897b12011-05-02 16:26:26 +0000749}
750
Thierry Reding48972d92012-01-02 01:15:37 +0000751int mmc_getcd(struct mmc *mmc)
752{
753 int cd;
754
755 cd = board_mmc_getcd(mmc);
756
Peter Korsgaardd4e1da42013-03-21 04:00:03 +0000757 if (cd < 0) {
758 if (mmc->getcd)
759 cd = mmc->getcd(mmc);
760 else
761 cd = 1;
762 }
Thierry Reding48972d92012-01-02 01:15:37 +0000763
764 return cd;
765}
766
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000767static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
Andy Fleming272cc702008-10-30 16:41:01 -0500768{
769 struct mmc_cmd cmd;
770 struct mmc_data data;
771
772 /* Switch the frequency */
773 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
774 cmd.resp_type = MMC_RSP_R1;
775 cmd.cmdarg = (mode << 31) | 0xffffff;
776 cmd.cmdarg &= ~(0xf << (group * 4));
777 cmd.cmdarg |= value << (group * 4);
Andy Fleming272cc702008-10-30 16:41:01 -0500778
779 data.dest = (char *)resp;
780 data.blocksize = 64;
781 data.blocks = 1;
782 data.flags = MMC_DATA_READ;
783
784 return mmc_send_cmd(mmc, &cmd, &data);
785}
786
787
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000788static int sd_change_freq(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500789{
790 int err;
791 struct mmc_cmd cmd;
Anton staaff781dd32011-10-03 13:54:59 +0000792 ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
793 ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500794 struct mmc_data data;
795 int timeout;
796
797 mmc->card_caps = 0;
798
Thomas Choud52ebf12010-12-24 13:12:21 +0000799 if (mmc_host_is_spi(mmc))
800 return 0;
801
Andy Fleming272cc702008-10-30 16:41:01 -0500802 /* Read the SCR to find out if this card supports higher speeds */
803 cmd.cmdidx = MMC_CMD_APP_CMD;
804 cmd.resp_type = MMC_RSP_R1;
805 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -0500806
807 err = mmc_send_cmd(mmc, &cmd, NULL);
808
809 if (err)
810 return err;
811
812 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
813 cmd.resp_type = MMC_RSP_R1;
814 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500815
816 timeout = 3;
817
818retry_scr:
Anton staaff781dd32011-10-03 13:54:59 +0000819 data.dest = (char *)scr;
Andy Fleming272cc702008-10-30 16:41:01 -0500820 data.blocksize = 8;
821 data.blocks = 1;
822 data.flags = MMC_DATA_READ;
823
824 err = mmc_send_cmd(mmc, &cmd, &data);
825
826 if (err) {
827 if (timeout--)
828 goto retry_scr;
829
830 return err;
831 }
832
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300833 mmc->scr[0] = __be32_to_cpu(scr[0]);
834 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500835
836 switch ((mmc->scr[0] >> 24) & 0xf) {
837 case 0:
838 mmc->version = SD_VERSION_1_0;
839 break;
840 case 1:
841 mmc->version = SD_VERSION_1_10;
842 break;
843 case 2:
844 mmc->version = SD_VERSION_2;
Jaehoon Chung1741c642013-01-29 22:58:16 +0000845 if ((mmc->scr[0] >> 15) & 0x1)
846 mmc->version = SD_VERSION_3;
Andy Fleming272cc702008-10-30 16:41:01 -0500847 break;
848 default:
849 mmc->version = SD_VERSION_1_0;
850 break;
851 }
852
Alagu Sankarb44c7082010-05-12 15:08:24 +0530853 if (mmc->scr[0] & SD_DATA_4BIT)
854 mmc->card_caps |= MMC_MODE_4BIT;
855
Andy Fleming272cc702008-10-30 16:41:01 -0500856 /* Version 1.0 doesn't support switching */
857 if (mmc->version == SD_VERSION_1_0)
858 return 0;
859
860 timeout = 4;
861 while (timeout--) {
862 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
Anton staaff781dd32011-10-03 13:54:59 +0000863 (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500864
865 if (err)
866 return err;
867
868 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300869 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500870 break;
871 }
872
Andy Fleming272cc702008-10-30 16:41:01 -0500873 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300874 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500875 return 0;
876
Macpaul Lin2c3fbf42011-11-28 16:31:09 +0000877 /*
878 * If the host doesn't support SD_HIGHSPEED, do not switch card to
879 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
880 * This can avoid furthur problem when the card runs in different
881 * mode between the host.
882 */
883 if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
884 (mmc->host_caps & MMC_MODE_HS)))
885 return 0;
886
Anton staaff781dd32011-10-03 13:54:59 +0000887 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
Andy Fleming272cc702008-10-30 16:41:01 -0500888
889 if (err)
890 return err;
891
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300892 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500893 mmc->card_caps |= MMC_MODE_HS;
894
895 return 0;
896}
897
898/* frequency bases */
899/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000900static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500901 10000,
902 100000,
903 1000000,
904 10000000,
905};
906
907/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
908 * to platforms without floating point.
909 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000910static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500911 0, /* reserved */
912 10,
913 12,
914 13,
915 15,
916 20,
917 25,
918 30,
919 35,
920 40,
921 45,
922 50,
923 55,
924 60,
925 70,
926 80,
927};
928
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000929static void mmc_set_ios(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500930{
931 mmc->set_ios(mmc);
932}
933
934void mmc_set_clock(struct mmc *mmc, uint clock)
935{
936 if (clock > mmc->f_max)
937 clock = mmc->f_max;
938
939 if (clock < mmc->f_min)
940 clock = mmc->f_min;
941
942 mmc->clock = clock;
943
944 mmc_set_ios(mmc);
945}
946
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000947static void mmc_set_bus_width(struct mmc *mmc, uint width)
Andy Fleming272cc702008-10-30 16:41:01 -0500948{
949 mmc->bus_width = width;
950
951 mmc_set_ios(mmc);
952}
953
Kim Phillipsfdbb8732012-10-29 13:34:43 +0000954static int mmc_startup(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -0500955{
Stephen Warrenf866a462013-06-11 15:14:01 -0600956 int err, i;
Andy Fleming272cc702008-10-30 16:41:01 -0500957 uint mult, freq;
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +0000958 u64 cmult, csize, capacity;
Andy Fleming272cc702008-10-30 16:41:01 -0500959 struct mmc_cmd cmd;
Simon Glass8bfa1952013-04-03 08:54:30 +0000960 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
961 ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000962 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500963
Thomas Choud52ebf12010-12-24 13:12:21 +0000964#ifdef CONFIG_MMC_SPI_CRC_ON
965 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
966 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
967 cmd.resp_type = MMC_RSP_R1;
968 cmd.cmdarg = 1;
Thomas Choud52ebf12010-12-24 13:12:21 +0000969 err = mmc_send_cmd(mmc, &cmd, NULL);
970
971 if (err)
972 return err;
973 }
974#endif
975
Andy Fleming272cc702008-10-30 16:41:01 -0500976 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000977 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
978 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -0500979 cmd.resp_type = MMC_RSP_R2;
980 cmd.cmdarg = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500981
982 err = mmc_send_cmd(mmc, &cmd, NULL);
983
984 if (err)
985 return err;
986
987 memcpy(mmc->cid, cmd.response, 16);
988
989 /*
990 * For MMC cards, set the Relative Address.
991 * For SD cards, get the Relatvie Address.
992 * This also puts the cards into Standby State
993 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000994 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
995 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
996 cmd.cmdarg = mmc->rca << 16;
997 cmd.resp_type = MMC_RSP_R6;
Andy Fleming272cc702008-10-30 16:41:01 -0500998
Thomas Choud52ebf12010-12-24 13:12:21 +0000999 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001000
Thomas Choud52ebf12010-12-24 13:12:21 +00001001 if (err)
1002 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001003
Thomas Choud52ebf12010-12-24 13:12:21 +00001004 if (IS_SD(mmc))
1005 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1006 }
Andy Fleming272cc702008-10-30 16:41:01 -05001007
1008 /* Get the Card-Specific Data */
1009 cmd.cmdidx = MMC_CMD_SEND_CSD;
1010 cmd.resp_type = MMC_RSP_R2;
1011 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001012
1013 err = mmc_send_cmd(mmc, &cmd, NULL);
1014
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +00001015 /* Waiting for the ready status */
1016 mmc_send_status(mmc, timeout);
1017
Andy Fleming272cc702008-10-30 16:41:01 -05001018 if (err)
1019 return err;
1020
Rabin Vincent998be3d2009-04-05 13:30:56 +05301021 mmc->csd[0] = cmd.response[0];
1022 mmc->csd[1] = cmd.response[1];
1023 mmc->csd[2] = cmd.response[2];
1024 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -05001025
1026 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301027 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -05001028
1029 switch (version) {
1030 case 0:
1031 mmc->version = MMC_VERSION_1_2;
1032 break;
1033 case 1:
1034 mmc->version = MMC_VERSION_1_4;
1035 break;
1036 case 2:
1037 mmc->version = MMC_VERSION_2_2;
1038 break;
1039 case 3:
1040 mmc->version = MMC_VERSION_3;
1041 break;
1042 case 4:
1043 mmc->version = MMC_VERSION_4;
1044 break;
1045 default:
1046 mmc->version = MMC_VERSION_1_2;
1047 break;
1048 }
1049 }
1050
1051 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +05301052 freq = fbase[(cmd.response[0] & 0x7)];
1053 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -05001054
1055 mmc->tran_speed = freq * mult;
1056
Rabin Vincent998be3d2009-04-05 13:30:56 +05301057 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001058
1059 if (IS_SD(mmc))
1060 mmc->write_bl_len = mmc->read_bl_len;
1061 else
Rabin Vincent998be3d2009-04-05 13:30:56 +05301062 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -05001063
1064 if (mmc->high_capacity) {
1065 csize = (mmc->csd[1] & 0x3f) << 16
1066 | (mmc->csd[2] & 0xffff0000) >> 16;
1067 cmult = 8;
1068 } else {
1069 csize = (mmc->csd[1] & 0x3ff) << 2
1070 | (mmc->csd[2] & 0xc0000000) >> 30;
1071 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1072 }
1073
Stephen Warrenf866a462013-06-11 15:14:01 -06001074 mmc->capacity_user = (csize + 1) << (cmult + 2);
1075 mmc->capacity_user *= mmc->read_bl_len;
1076 mmc->capacity_boot = 0;
1077 mmc->capacity_rpmb = 0;
1078 for (i = 0; i < 4; i++)
1079 mmc->capacity_gp[i] = 0;
Andy Fleming272cc702008-10-30 16:41:01 -05001080
Simon Glass8bfa1952013-04-03 08:54:30 +00001081 if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1082 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001083
Simon Glass8bfa1952013-04-03 08:54:30 +00001084 if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1085 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
Andy Fleming272cc702008-10-30 16:41:01 -05001086
1087 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +00001088 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1089 cmd.cmdidx = MMC_CMD_SELECT_CARD;
Ajay Bhargavfe8f7062011-10-05 03:13:23 +00001090 cmd.resp_type = MMC_RSP_R1;
Thomas Choud52ebf12010-12-24 13:12:21 +00001091 cmd.cmdarg = mmc->rca << 16;
Thomas Choud52ebf12010-12-24 13:12:21 +00001092 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -05001093
Thomas Choud52ebf12010-12-24 13:12:21 +00001094 if (err)
1095 return err;
1096 }
Andy Fleming272cc702008-10-30 16:41:01 -05001097
Lei Wene6f99a52011-06-22 17:03:31 +00001098 /*
1099 * For SD, its erase group is always one sector
1100 */
1101 mmc->erase_grp_size = 1;
Lei Wenbc897b12011-05-02 16:26:26 +00001102 mmc->part_config = MMCPART_NOAVAILABLE;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301103 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1104 /* check ext_csd version and capacity */
1105 err = mmc_send_ext_csd(mmc, ext_csd);
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001106 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
Yoshihiro Shimoda639b7822011-07-04 22:13:26 +00001107 /*
1108 * According to the JEDEC Standard, the value of
1109 * ext_csd's capacity is valid if the value is more
1110 * than 2GB
1111 */
Lei Wen0560db12011-10-03 20:35:10 +00001112 capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1113 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1114 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1115 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Simon Glass8bfa1952013-04-03 08:54:30 +00001116 capacity *= MMC_MAX_BLOCK_LEN;
Łukasz Majewskib1f1e8212011-07-05 02:19:44 +00001117 if ((capacity >> 20) > 2 * 1024)
Stephen Warrenf866a462013-06-11 15:14:01 -06001118 mmc->capacity_user = capacity;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301119 }
Lei Wenbc897b12011-05-02 16:26:26 +00001120
Jaehoon Chung64f4a612013-01-29 19:31:16 +00001121 switch (ext_csd[EXT_CSD_REV]) {
1122 case 1:
1123 mmc->version = MMC_VERSION_4_1;
1124 break;
1125 case 2:
1126 mmc->version = MMC_VERSION_4_2;
1127 break;
1128 case 3:
1129 mmc->version = MMC_VERSION_4_3;
1130 break;
1131 case 5:
1132 mmc->version = MMC_VERSION_4_41;
1133 break;
1134 case 6:
1135 mmc->version = MMC_VERSION_4_5;
1136 break;
1137 }
1138
Lei Wene6f99a52011-06-22 17:03:31 +00001139 /*
1140 * Check whether GROUP_DEF is set, if yes, read out
1141 * group size from ext_csd directly, or calculate
1142 * the group size from the csd value.
1143 */
Simon Glass8bfa1952013-04-03 08:54:30 +00001144 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF]) {
Lei Wen0560db12011-10-03 20:35:10 +00001145 mmc->erase_grp_size =
Simon Glass8bfa1952013-04-03 08:54:30 +00001146 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
1147 MMC_MAX_BLOCK_LEN * 1024;
1148 } else {
Lei Wene6f99a52011-06-22 17:03:31 +00001149 int erase_gsz, erase_gmul;
1150 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1151 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1152 mmc->erase_grp_size = (erase_gsz + 1)
1153 * (erase_gmul + 1);
1154 }
1155
Lei Wenbc897b12011-05-02 16:26:26 +00001156 /* store the partition info of emmc */
Stephen Warren8948ea82012-07-30 10:55:43 +00001157 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1158 ext_csd[EXT_CSD_BOOT_MULT])
Lei Wen0560db12011-10-03 20:35:10 +00001159 mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
Stephen Warrenf866a462013-06-11 15:14:01 -06001160
1161 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1162
1163 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1164
1165 for (i = 0; i < 4; i++) {
1166 int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1167 mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1168 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1169 mmc->capacity_gp[i] *=
1170 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1171 mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1172 }
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +05301173 }
1174
Stephen Warrenf866a462013-06-11 15:14:01 -06001175 err = mmc_set_capacity(mmc, mmc->part_num);
1176 if (err)
1177 return err;
1178
Andy Fleming272cc702008-10-30 16:41:01 -05001179 if (IS_SD(mmc))
1180 err = sd_change_freq(mmc);
1181 else
1182 err = mmc_change_freq(mmc);
1183
1184 if (err)
1185 return err;
1186
1187 /* Restrict card's capabilities by what the host can do */
1188 mmc->card_caps &= mmc->host_caps;
1189
1190 if (IS_SD(mmc)) {
1191 if (mmc->card_caps & MMC_MODE_4BIT) {
1192 cmd.cmdidx = MMC_CMD_APP_CMD;
1193 cmd.resp_type = MMC_RSP_R1;
1194 cmd.cmdarg = mmc->rca << 16;
Andy Fleming272cc702008-10-30 16:41:01 -05001195
1196 err = mmc_send_cmd(mmc, &cmd, NULL);
1197 if (err)
1198 return err;
1199
1200 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1201 cmd.resp_type = MMC_RSP_R1;
1202 cmd.cmdarg = 2;
Andy Fleming272cc702008-10-30 16:41:01 -05001203 err = mmc_send_cmd(mmc, &cmd, NULL);
1204 if (err)
1205 return err;
1206
1207 mmc_set_bus_width(mmc, 4);
1208 }
1209
1210 if (mmc->card_caps & MMC_MODE_HS)
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001211 mmc->tran_speed = 50000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001212 else
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001213 mmc->tran_speed = 25000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001214 } else {
Andy Fleming7798f6d2012-10-31 19:02:38 +00001215 int idx;
1216
1217 /* An array of possible bus widths in order of preference */
1218 static unsigned ext_csd_bits[] = {
1219 EXT_CSD_BUS_WIDTH_8,
1220 EXT_CSD_BUS_WIDTH_4,
1221 EXT_CSD_BUS_WIDTH_1,
1222 };
1223
1224 /* An array to map CSD bus widths to host cap bits */
1225 static unsigned ext_to_hostcaps[] = {
1226 [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1227 [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1228 };
1229
1230 /* An array to map chosen bus width to an integer */
1231 static unsigned widths[] = {
1232 8, 4, 1,
1233 };
1234
1235 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1236 unsigned int extw = ext_csd_bits[idx];
1237
1238 /*
1239 * Check to make sure the controller supports
1240 * this bus width, if it's more than 1
1241 */
1242 if (extw != EXT_CSD_BUS_WIDTH_1 &&
1243 !(mmc->host_caps & ext_to_hostcaps[extw]))
1244 continue;
1245
Andy Fleming272cc702008-10-30 16:41:01 -05001246 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
Andy Fleming7798f6d2012-10-31 19:02:38 +00001247 EXT_CSD_BUS_WIDTH, extw);
Andy Fleming272cc702008-10-30 16:41:01 -05001248
1249 if (err)
Lei Wen41378942011-10-03 20:35:11 +00001250 continue;
Andy Fleming272cc702008-10-30 16:41:01 -05001251
Andy Fleming7798f6d2012-10-31 19:02:38 +00001252 mmc_set_bus_width(mmc, widths[idx]);
Andy Fleming272cc702008-10-30 16:41:01 -05001253
Lei Wen41378942011-10-03 20:35:11 +00001254 err = mmc_send_ext_csd(mmc, test_csd);
1255 if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1256 == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1257 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1258 == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1259 && ext_csd[EXT_CSD_REV] \
1260 == test_csd[EXT_CSD_REV]
1261 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1262 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1263 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1264 &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
Andy Fleming272cc702008-10-30 16:41:01 -05001265
Andy Fleming7798f6d2012-10-31 19:02:38 +00001266 mmc->card_caps |= ext_to_hostcaps[extw];
Lei Wen41378942011-10-03 20:35:11 +00001267 break;
1268 }
Andy Fleming272cc702008-10-30 16:41:01 -05001269 }
1270
1271 if (mmc->card_caps & MMC_MODE_HS) {
1272 if (mmc->card_caps & MMC_MODE_HS_52MHz)
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001273 mmc->tran_speed = 52000000;
Andy Fleming272cc702008-10-30 16:41:01 -05001274 else
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001275 mmc->tran_speed = 26000000;
1276 }
Andy Fleming272cc702008-10-30 16:41:01 -05001277 }
1278
Jaehoon Chungad5fd922012-03-26 21:16:03 +00001279 mmc_set_clock(mmc, mmc->tran_speed);
1280
Andy Fleming272cc702008-10-30 16:41:01 -05001281 /* fill in device description */
1282 mmc->block_dev.lun = 0;
1283 mmc->block_dev.type = 0;
1284 mmc->block_dev.blksz = mmc->read_bl_len;
Egbert Eich0472fbf2013-04-09 21:11:56 +00001285 mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
Rabin Vincent9b1f9422009-04-05 13:30:54 +05301286 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Taylor Huttbabce5f2012-10-20 17:15:59 +00001287 sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1288 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1289 (mmc->cid[3] >> 16) & 0xffff);
1290 sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1291 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1292 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1293 (mmc->cid[2] >> 24) & 0xff);
1294 sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1295 (mmc->cid[2] >> 16) & 0xf);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001296#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
Andy Fleming272cc702008-10-30 16:41:01 -05001297 init_part(&mmc->block_dev);
Mikhail Kshevetskiy122efd42012-07-09 08:53:38 +00001298#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001299
1300 return 0;
1301}
1302
Kim Phillipsfdbb8732012-10-29 13:34:43 +00001303static int mmc_send_if_cond(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001304{
1305 struct mmc_cmd cmd;
1306 int err;
1307
1308 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1309 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1310 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1311 cmd.resp_type = MMC_RSP_R7;
Andy Fleming272cc702008-10-30 16:41:01 -05001312
1313 err = mmc_send_cmd(mmc, &cmd, NULL);
1314
1315 if (err)
1316 return err;
1317
Rabin Vincent998be3d2009-04-05 13:30:56 +05301318 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -05001319 return UNUSABLE_ERR;
1320 else
1321 mmc->version = SD_VERSION_2;
1322
1323 return 0;
1324}
1325
1326int mmc_register(struct mmc *mmc)
1327{
1328 /* Setup the universal parts of the block interface just once */
1329 mmc->block_dev.if_type = IF_TYPE_MMC;
1330 mmc->block_dev.dev = cur_dev_num++;
1331 mmc->block_dev.removable = 1;
1332 mmc->block_dev.block_read = mmc_bread;
1333 mmc->block_dev.block_write = mmc_bwrite;
Lei Wene6f99a52011-06-22 17:03:31 +00001334 mmc->block_dev.block_erase = mmc_berase;
John Rigby8feafcc2011-04-18 05:50:08 +00001335 if (!mmc->b_max)
1336 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Andy Fleming272cc702008-10-30 16:41:01 -05001337
1338 INIT_LIST_HEAD (&mmc->link);
1339
1340 list_add_tail (&mmc->link, &mmc_devices);
1341
1342 return 0;
1343}
1344
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001345#ifdef CONFIG_PARTITIONS
Andy Fleming272cc702008-10-30 16:41:01 -05001346block_dev_desc_t *mmc_get_dev(int dev)
1347{
1348 struct mmc *mmc = find_mmc_device(dev);
Benoît Thébaudeau6bb4b4b2012-08-10 08:59:12 +00001349 if (!mmc || mmc_init(mmc))
Łukasz Majewski40242bc2012-04-19 02:39:18 +00001350 return NULL;
Andy Fleming272cc702008-10-30 16:41:01 -05001351
Łukasz Majewski40242bc2012-04-19 02:39:18 +00001352 return &mmc->block_dev;
Andy Fleming272cc702008-10-30 16:41:01 -05001353}
Matthew McClintockdf3fc522011-05-24 05:31:19 +00001354#endif
Andy Fleming272cc702008-10-30 16:41:01 -05001355
Che-Liang Chioue9550442012-11-28 15:21:13 +00001356int mmc_start_init(struct mmc *mmc)
Andy Fleming272cc702008-10-30 16:41:01 -05001357{
Macpaul Linafd59322011-11-14 23:35:39 +00001358 int err;
Andy Fleming272cc702008-10-30 16:41:01 -05001359
Thierry Reding48972d92012-01-02 01:15:37 +00001360 if (mmc_getcd(mmc) == 0) {
1361 mmc->has_init = 0;
1362 printf("MMC: no card present\n");
1363 return NO_CARD_ERR;
1364 }
1365
Lei Wenbc897b12011-05-02 16:26:26 +00001366 if (mmc->has_init)
1367 return 0;
1368
Andy Fleming272cc702008-10-30 16:41:01 -05001369 err = mmc->init(mmc);
1370
1371 if (err)
1372 return err;
1373
Ilya Yanokb86b85e2009-06-29 17:53:16 +04001374 mmc_set_bus_width(mmc, 1);
1375 mmc_set_clock(mmc, 1);
1376
Andy Fleming272cc702008-10-30 16:41:01 -05001377 /* Reset the Card */
1378 err = mmc_go_idle(mmc);
1379
1380 if (err)
1381 return err;
1382
Lei Wenbc897b12011-05-02 16:26:26 +00001383 /* The internal partition reset to user partition(0) at every CMD0*/
1384 mmc->part_num = 0;
1385
Andy Fleming272cc702008-10-30 16:41:01 -05001386 /* Test for SD version 2 */
Macpaul Linafd59322011-11-14 23:35:39 +00001387 err = mmc_send_if_cond(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -05001388
Andy Fleming272cc702008-10-30 16:41:01 -05001389 /* Now try to get the SD card's operating condition */
1390 err = sd_send_op_cond(mmc);
1391
1392 /* If the command timed out, we check for an MMC card */
1393 if (err == TIMEOUT) {
1394 err = mmc_send_op_cond(mmc);
1395
Che-Liang Chioue9550442012-11-28 15:21:13 +00001396 if (err && err != IN_PROGRESS) {
Andy Fleming272cc702008-10-30 16:41:01 -05001397 printf("Card did not respond to voltage select!\n");
1398 return UNUSABLE_ERR;
1399 }
1400 }
1401
Che-Liang Chioue9550442012-11-28 15:21:13 +00001402 if (err == IN_PROGRESS)
1403 mmc->init_in_progress = 1;
1404
1405 return err;
1406}
1407
1408static int mmc_complete_init(struct mmc *mmc)
1409{
1410 int err = 0;
1411
1412 if (mmc->op_cond_pending)
1413 err = mmc_complete_op_cond(mmc);
1414
1415 if (!err)
1416 err = mmc_startup(mmc);
Lei Wenbc897b12011-05-02 16:26:26 +00001417 if (err)
1418 mmc->has_init = 0;
1419 else
1420 mmc->has_init = 1;
Che-Liang Chioue9550442012-11-28 15:21:13 +00001421 mmc->init_in_progress = 0;
1422 return err;
1423}
1424
1425int mmc_init(struct mmc *mmc)
1426{
1427 int err = IN_PROGRESS;
1428 unsigned start = get_timer(0);
1429
1430 if (mmc->has_init)
1431 return 0;
1432 if (!mmc->init_in_progress)
1433 err = mmc_start_init(mmc);
1434
1435 if (!err || err == IN_PROGRESS)
1436 err = mmc_complete_init(mmc);
1437 debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
Lei Wenbc897b12011-05-02 16:26:26 +00001438 return err;
Andy Fleming272cc702008-10-30 16:41:01 -05001439}
1440
1441/*
1442 * CPU and board-specific MMC initializations. Aliased function
1443 * signals caller to move on
1444 */
1445static int __def_mmc_init(bd_t *bis)
1446{
1447 return -1;
1448}
1449
Peter Tyserf9a109b2009-04-20 11:08:46 -05001450int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1451int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -05001452
1453void print_mmc_devices(char separator)
1454{
1455 struct mmc *m;
1456 struct list_head *entry;
1457
1458 list_for_each(entry, &mmc_devices) {
1459 m = list_entry(entry, struct mmc, link);
1460
1461 printf("%s: %d", m->name, m->block_dev.dev);
1462
1463 if (entry->next != &mmc_devices)
1464 printf("%c ", separator);
1465 }
1466
1467 printf("\n");
1468}
1469
Lei Wenea6ebe22011-05-02 16:26:25 +00001470int get_mmc_num(void)
1471{
1472 return cur_dev_num;
1473}
1474
Che-Liang Chioue9550442012-11-28 15:21:13 +00001475void mmc_set_preinit(struct mmc *mmc, int preinit)
1476{
1477 mmc->preinit = preinit;
1478}
1479
1480static void do_preinit(void)
1481{
1482 struct mmc *m;
1483 struct list_head *entry;
1484
1485 list_for_each(entry, &mmc_devices) {
1486 m = list_entry(entry, struct mmc, link);
1487
1488 if (m->preinit)
1489 mmc_start_init(m);
1490 }
1491}
1492
1493
Andy Fleming272cc702008-10-30 16:41:01 -05001494int mmc_initialize(bd_t *bis)
1495{
1496 INIT_LIST_HEAD (&mmc_devices);
1497 cur_dev_num = 0;
1498
1499 if (board_mmc_init(bis) < 0)
1500 cpu_mmc_init(bis);
1501
1502 print_mmc_devices(',');
1503
Che-Liang Chioue9550442012-11-28 15:21:13 +00001504 do_preinit();
Andy Fleming272cc702008-10-30 16:41:01 -05001505 return 0;
1506}
Amar3690d6d2013-04-27 11:42:58 +05301507
1508#ifdef CONFIG_SUPPORT_EMMC_BOOT
1509/*
1510 * This function changes the size of boot partition and the size of rpmb
1511 * partition present on EMMC devices.
1512 *
1513 * Input Parameters:
1514 * struct *mmc: pointer for the mmc device strcuture
1515 * bootsize: size of boot partition
1516 * rpmbsize: size of rpmb partition
1517 *
1518 * Returns 0 on success.
1519 */
1520
1521int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1522 unsigned long rpmbsize)
1523{
1524 int err;
1525 struct mmc_cmd cmd;
1526
1527 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1528 cmd.cmdidx = MMC_CMD_RES_MAN;
1529 cmd.resp_type = MMC_RSP_R1b;
1530 cmd.cmdarg = MMC_CMD62_ARG1;
1531
1532 err = mmc_send_cmd(mmc, &cmd, NULL);
1533 if (err) {
1534 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1535 return err;
1536 }
1537
1538 /* Boot partition changing mode */
1539 cmd.cmdidx = MMC_CMD_RES_MAN;
1540 cmd.resp_type = MMC_RSP_R1b;
1541 cmd.cmdarg = MMC_CMD62_ARG2;
1542
1543 err = mmc_send_cmd(mmc, &cmd, NULL);
1544 if (err) {
1545 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1546 return err;
1547 }
1548 /* boot partition size is multiple of 128KB */
1549 bootsize = (bootsize * 1024) / 128;
1550
1551 /* Arg: boot partition size */
1552 cmd.cmdidx = MMC_CMD_RES_MAN;
1553 cmd.resp_type = MMC_RSP_R1b;
1554 cmd.cmdarg = bootsize;
1555
1556 err = mmc_send_cmd(mmc, &cmd, NULL);
1557 if (err) {
1558 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1559 return err;
1560 }
1561 /* RPMB partition size is multiple of 128KB */
1562 rpmbsize = (rpmbsize * 1024) / 128;
1563 /* Arg: RPMB partition size */
1564 cmd.cmdidx = MMC_CMD_RES_MAN;
1565 cmd.resp_type = MMC_RSP_R1b;
1566 cmd.cmdarg = rpmbsize;
1567
1568 err = mmc_send_cmd(mmc, &cmd, NULL);
1569 if (err) {
1570 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1571 return err;
1572 }
1573 return 0;
1574}
1575
1576/*
1577 * This function shall form and send the commands to open / close the
1578 * boot partition specified by user.
1579 *
1580 * Input Parameters:
1581 * ack: 0x0 - No boot acknowledge sent (default)
1582 * 0x1 - Boot acknowledge sent during boot operation
1583 * part_num: User selects boot data that will be sent to master
1584 * 0x0 - Device not boot enabled (default)
1585 * 0x1 - Boot partition 1 enabled for boot
1586 * 0x2 - Boot partition 2 enabled for boot
1587 * access: User selects partitions to access
1588 * 0x0 : No access to boot partition (default)
1589 * 0x1 : R/W boot partition 1
1590 * 0x2 : R/W boot partition 2
1591 * 0x3 : R/W Replay Protected Memory Block (RPMB)
1592 *
1593 * Returns 0 on success.
1594 */
1595int mmc_boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1596{
1597 int err;
1598 struct mmc_cmd cmd;
1599
1600 /* Boot ack enable, boot partition enable , boot partition access */
1601 cmd.cmdidx = MMC_CMD_SWITCH;
1602 cmd.resp_type = MMC_RSP_R1b;
1603
1604 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1605 (EXT_CSD_PART_CONF << 16) |
1606 ((EXT_CSD_BOOT_ACK(ack) |
1607 EXT_CSD_BOOT_PART_NUM(part_num) |
1608 EXT_CSD_PARTITION_ACCESS(access)) << 8);
1609
1610 err = mmc_send_cmd(mmc, &cmd, NULL);
1611 if (err) {
1612 if (access) {
1613 debug("mmc boot partition#%d open fail:Error1 = %d\n",
1614 part_num, err);
1615 } else {
1616 debug("mmc boot partition#%d close fail:Error = %d\n",
1617 part_num, err);
1618 }
1619 return err;
1620 }
1621
1622 if (access) {
1623 /* 4bit transfer mode at booting time. */
1624 cmd.cmdidx = MMC_CMD_SWITCH;
1625 cmd.resp_type = MMC_RSP_R1b;
1626
1627 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1628 (EXT_CSD_BOOT_BUS_WIDTH << 16) |
1629 ((1 << 0) << 8);
1630
1631 err = mmc_send_cmd(mmc, &cmd, NULL);
1632 if (err) {
1633 debug("mmc boot partition#%d open fail:Error2 = %d\n",
1634 part_num, err);
1635 return err;
1636 }
1637 }
1638 return 0;
1639}
1640#endif