blob: 3d445c0e2695598cccf0c6a9a8db8270a3e55bfc [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
Stefano Babic11fdade2010-02-05 15:04:43 +010043int __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
44 return -1;
45}
46
47int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
48 alias("__board_mmc_getcd")));
49
Andy Fleming272cc702008-10-30 16:41:01 -050050int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
51{
52 return mmc->send_cmd(mmc, cmd, data);
53}
54
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +000055int mmc_send_status(struct mmc *mmc, int timeout)
56{
57 struct mmc_cmd cmd;
58 int err;
59#ifdef CONFIG_MMC_TRACE
60 int status;
61#endif
62
63 cmd.cmdidx = MMC_CMD_SEND_STATUS;
64 cmd.resp_type = MMC_RSP_R1;
65 cmd.cmdarg = 0;
66 cmd.flags = 0;
67
68 do {
69 err = mmc_send_cmd(mmc, &cmd, NULL);
70 if (err)
71 return err;
72 else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
73 break;
74
75 udelay(1000);
76
77 if (cmd.response[0] & MMC_STATUS_MASK) {
78 printf("Status Error: 0x%08X\n", cmd.response[0]);
79 return COMM_ERR;
80 }
81 } while (timeout--);
82
83 if (!timeout) {
84 printf("Timeout waiting card ready\n");
85 return TIMEOUT;
86 }
87
88 return 0;
89}
90
Andy Fleming272cc702008-10-30 16:41:01 -050091int mmc_set_blocklen(struct mmc *mmc, int len)
92{
93 struct mmc_cmd cmd;
94
95 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
96 cmd.resp_type = MMC_RSP_R1;
97 cmd.cmdarg = len;
98 cmd.flags = 0;
99
100 return mmc_send_cmd(mmc, &cmd, NULL);
101}
102
103struct mmc *find_mmc_device(int dev_num)
104{
105 struct mmc *m;
106 struct list_head *entry;
107
108 list_for_each(entry, &mmc_devices) {
109 m = list_entry(entry, struct mmc, link);
110
111 if (m->block_dev.dev == dev_num)
112 return m;
113 }
114
115 printf("MMC Device %d not found\n", dev_num);
116
117 return NULL;
118}
119
120static ulong
Lei Wen01581262010-10-14 13:38:11 +0800121mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
Andy Fleming272cc702008-10-30 16:41:01 -0500122{
123 struct mmc_cmd cmd;
124 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000125 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500126
Lei Wend2bf29e2010-09-13 22:07:27 +0800127 if ((start + blkcnt) > mmc->block_dev.lba) {
Steve Sakomandef412b2010-10-28 09:00:26 -0700128 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800129 start + blkcnt, mmc->block_dev.lba);
130 return 0;
131 }
Andy Fleming272cc702008-10-30 16:41:01 -0500132
133 if (blkcnt > 1)
134 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
135 else
136 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
137
138 if (mmc->high_capacity)
139 cmd.cmdarg = start;
140 else
Steve Sakomandef412b2010-10-28 09:00:26 -0700141 cmd.cmdarg = start * mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500142
143 cmd.resp_type = MMC_RSP_R1;
144 cmd.flags = 0;
145
146 data.src = src;
147 data.blocks = blkcnt;
Steve Sakomandef412b2010-10-28 09:00:26 -0700148 data.blocksize = mmc->write_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500149 data.flags = MMC_DATA_WRITE;
150
Steve Sakomandef412b2010-10-28 09:00:26 -0700151 if (mmc_send_cmd(mmc, &cmd, &data)) {
152 printf("mmc write failed\n");
153 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500154 }
155
Thomas Choud52ebf12010-12-24 13:12:21 +0000156 /* SPI multiblock writes terminate using a special
157 * token, not a STOP_TRANSMISSION request.
158 */
159 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
Andy Fleming272cc702008-10-30 16:41:01 -0500160 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
161 cmd.cmdarg = 0;
162 cmd.resp_type = MMC_RSP_R1b;
163 cmd.flags = 0;
Steve Sakomandef412b2010-10-28 09:00:26 -0700164 if (mmc_send_cmd(mmc, &cmd, NULL)) {
165 printf("mmc fail to send stop cmd\n");
166 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800167 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000168
169 /* Waiting for the ready status */
170 mmc_send_status(mmc, timeout);
Andy Fleming272cc702008-10-30 16:41:01 -0500171 }
172
173 return blkcnt;
174}
175
Lei Wen01581262010-10-14 13:38:11 +0800176static ulong
177mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
178{
Lei Wen01581262010-10-14 13:38:11 +0800179 lbaint_t cur, blocks_todo = blkcnt;
180
Steve Sakomandef412b2010-10-28 09:00:26 -0700181 struct mmc *mmc = find_mmc_device(dev_num);
Lei Wen01581262010-10-14 13:38:11 +0800182 if (!mmc)
Steve Sakomandef412b2010-10-28 09:00:26 -0700183 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800184
Steve Sakomandef412b2010-10-28 09:00:26 -0700185 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
186 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800187
188 do {
Matt Waddelce0fbcd2011-02-24 16:35:23 +0000189 cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ?
190 CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo;
Lei Wen01581262010-10-14 13:38:11 +0800191 if(mmc_write_blocks(mmc, start, cur, src) != cur)
Steve Sakomandef412b2010-10-28 09:00:26 -0700192 return 0;
Lei Wen01581262010-10-14 13:38:11 +0800193 blocks_todo -= cur;
194 start += cur;
195 src += cur * mmc->write_bl_len;
196 } while (blocks_todo > 0);
197
198 return blkcnt;
199}
200
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700201int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
Andy Fleming272cc702008-10-30 16:41:01 -0500202{
203 struct mmc_cmd cmd;
204 struct mmc_data data;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000205 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500206
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700207 if (blkcnt > 1)
208 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
209 else
210 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
Andy Fleming272cc702008-10-30 16:41:01 -0500211
212 if (mmc->high_capacity)
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700213 cmd.cmdarg = start;
Andy Fleming272cc702008-10-30 16:41:01 -0500214 else
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700215 cmd.cmdarg = start * mmc->read_bl_len;
Andy Fleming272cc702008-10-30 16:41:01 -0500216
217 cmd.resp_type = MMC_RSP_R1;
218 cmd.flags = 0;
219
220 data.dest = dst;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700221 data.blocks = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500222 data.blocksize = mmc->read_bl_len;
223 data.flags = MMC_DATA_READ;
224
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700225 if (mmc_send_cmd(mmc, &cmd, &data))
226 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500227
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700228 if (blkcnt > 1) {
229 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
230 cmd.cmdarg = 0;
231 cmd.resp_type = MMC_RSP_R1b;
232 cmd.flags = 0;
233 if (mmc_send_cmd(mmc, &cmd, NULL)) {
234 printf("mmc fail to send stop cmd\n");
235 return 0;
236 }
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000237
238 /* Waiting for the ready status */
239 mmc_send_status(mmc, timeout);
Andy Fleming272cc702008-10-30 16:41:01 -0500240 }
241
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700242 return blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500243}
244
245static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
246{
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700247 lbaint_t cur, blocks_todo = blkcnt;
Andy Fleming272cc702008-10-30 16:41:01 -0500248
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700249 if (blkcnt == 0)
250 return 0;
251
252 struct mmc *mmc = find_mmc_device(dev_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500253 if (!mmc)
254 return 0;
255
Lei Wend2bf29e2010-09-13 22:07:27 +0800256 if ((start + blkcnt) > mmc->block_dev.lba) {
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700257 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
Lei Wend2bf29e2010-09-13 22:07:27 +0800258 start + blkcnt, mmc->block_dev.lba);
259 return 0;
260 }
Andy Fleming272cc702008-10-30 16:41:01 -0500261
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700262 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
Andy Fleming272cc702008-10-30 16:41:01 -0500263 return 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500264
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700265 do {
Matt Waddelce0fbcd2011-02-24 16:35:23 +0000266 cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ?
267 CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo;
Alagu Sankar4a1a06b2010-10-25 07:23:56 -0700268 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
269 return 0;
270 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
278int mmc_go_idle(struct mmc* mmc)
279{
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;
288 cmd.flags = 0;
289
290 err = mmc_send_cmd(mmc, &cmd, NULL);
291
292 if (err)
293 return err;
294
295 udelay(2000);
296
297 return 0;
298}
299
300int
301sd_send_op_cond(struct mmc *mmc)
302{
303 int timeout = 1000;
304 int err;
305 struct mmc_cmd cmd;
306
307 do {
308 cmd.cmdidx = MMC_CMD_APP_CMD;
309 cmd.resp_type = MMC_RSP_R1;
310 cmd.cmdarg = 0;
311 cmd.flags = 0;
312
313 err = mmc_send_cmd(mmc, &cmd, NULL);
314
315 if (err)
316 return err;
317
318 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
319 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100320
321 /*
322 * Most cards do not answer if some reserved bits
323 * in the ocr are set. However, Some controller
324 * can set bit 7 (reserved for low voltages), but
325 * how to manage low voltages SD card is not yet
326 * specified.
327 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000328 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
329 (mmc->voltages & 0xff8000);
Andy Fleming272cc702008-10-30 16:41:01 -0500330
331 if (mmc->version == SD_VERSION_2)
332 cmd.cmdarg |= OCR_HCS;
333
334 err = mmc_send_cmd(mmc, &cmd, NULL);
335
336 if (err)
337 return err;
338
339 udelay(1000);
340 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
341
342 if (timeout <= 0)
343 return UNUSABLE_ERR;
344
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;
352 cmd.flags = 0;
353
354 err = mmc_send_cmd(mmc, &cmd, NULL);
355
356 if (err)
357 return err;
358 }
359
Rabin Vincent998be3d2009-04-05 13:30:56 +0530360 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500361
362 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
363 mmc->rca = 0;
364
365 return 0;
366}
367
368int mmc_send_op_cond(struct mmc *mmc)
369{
370 int timeout = 1000;
371 struct mmc_cmd cmd;
372 int err;
373
374 /* Some cards seem to need this */
375 mmc_go_idle(mmc);
376
377 do {
378 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
379 cmd.resp_type = MMC_RSP_R3;
Thomas Choud52ebf12010-12-24 13:12:21 +0000380 cmd.cmdarg = OCR_HCS | (mmc_host_is_spi(mmc) ? 0 :
381 mmc->voltages);
Andy Fleming272cc702008-10-30 16:41:01 -0500382 cmd.flags = 0;
383
384 err = mmc_send_cmd(mmc, &cmd, NULL);
385
386 if (err)
387 return err;
388
389 udelay(1000);
390 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
391
392 if (timeout <= 0)
393 return UNUSABLE_ERR;
394
Thomas Choud52ebf12010-12-24 13:12:21 +0000395 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
396 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
397 cmd.resp_type = MMC_RSP_R3;
398 cmd.cmdarg = 0;
399 cmd.flags = 0;
400
401 err = mmc_send_cmd(mmc, &cmd, NULL);
402
403 if (err)
404 return err;
405 }
406
Andy Fleming272cc702008-10-30 16:41:01 -0500407 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530408 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500409
410 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
411 mmc->rca = 0;
412
413 return 0;
414}
415
416
417int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
418{
419 struct mmc_cmd cmd;
420 struct mmc_data data;
421 int err;
422
423 /* Get the Card Status Register */
424 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
425 cmd.resp_type = MMC_RSP_R1;
426 cmd.cmdarg = 0;
427 cmd.flags = 0;
428
429 data.dest = ext_csd;
430 data.blocks = 1;
431 data.blocksize = 512;
432 data.flags = MMC_DATA_READ;
433
434 err = mmc_send_cmd(mmc, &cmd, &data);
435
436 return err;
437}
438
439
440int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
441{
442 struct mmc_cmd cmd;
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000443 int timeout = 1000;
444 int ret;
Andy Fleming272cc702008-10-30 16:41:01 -0500445
446 cmd.cmdidx = MMC_CMD_SWITCH;
447 cmd.resp_type = MMC_RSP_R1b;
448 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000449 (index << 16) |
450 (value << 8);
Andy Fleming272cc702008-10-30 16:41:01 -0500451 cmd.flags = 0;
452
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000453 ret = mmc_send_cmd(mmc, &cmd, NULL);
454
455 /* Waiting for the ready status */
456 mmc_send_status(mmc, timeout);
457
458 return ret;
459
Andy Fleming272cc702008-10-30 16:41:01 -0500460}
461
462int mmc_change_freq(struct mmc *mmc)
463{
464 char ext_csd[512];
465 char cardtype;
466 int err;
467
468 mmc->card_caps = 0;
469
Thomas Choud52ebf12010-12-24 13:12:21 +0000470 if (mmc_host_is_spi(mmc))
471 return 0;
472
Andy Fleming272cc702008-10-30 16:41:01 -0500473 /* Only version 4 supports high-speed */
474 if (mmc->version < MMC_VERSION_4)
475 return 0;
476
477 mmc->card_caps |= MMC_MODE_4BIT;
478
479 err = mmc_send_ext_csd(mmc, ext_csd);
480
481 if (err)
482 return err;
483
484 if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
485 mmc->high_capacity = 1;
486
487 cardtype = ext_csd[196] & 0xf;
488
489 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
490
491 if (err)
492 return err;
493
494 /* Now check to see that it worked */
495 err = mmc_send_ext_csd(mmc, ext_csd);
496
497 if (err)
498 return err;
499
500 /* No high-speed support */
501 if (!ext_csd[185])
502 return 0;
503
504 /* High Speed is set, there are two types: 52MHz and 26MHz */
505 if (cardtype & MMC_HS_52MHZ)
506 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
507 else
508 mmc->card_caps |= MMC_MODE_HS;
509
510 return 0;
511}
512
513int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
514{
515 struct mmc_cmd cmd;
516 struct mmc_data data;
517
518 /* Switch the frequency */
519 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
520 cmd.resp_type = MMC_RSP_R1;
521 cmd.cmdarg = (mode << 31) | 0xffffff;
522 cmd.cmdarg &= ~(0xf << (group * 4));
523 cmd.cmdarg |= value << (group * 4);
524 cmd.flags = 0;
525
526 data.dest = (char *)resp;
527 data.blocksize = 64;
528 data.blocks = 1;
529 data.flags = MMC_DATA_READ;
530
531 return mmc_send_cmd(mmc, &cmd, &data);
532}
533
534
535int sd_change_freq(struct mmc *mmc)
536{
537 int err;
538 struct mmc_cmd cmd;
539 uint scr[2];
540 uint switch_status[16];
541 struct mmc_data data;
542 int timeout;
543
544 mmc->card_caps = 0;
545
Thomas Choud52ebf12010-12-24 13:12:21 +0000546 if (mmc_host_is_spi(mmc))
547 return 0;
548
Andy Fleming272cc702008-10-30 16:41:01 -0500549 /* Read the SCR to find out if this card supports higher speeds */
550 cmd.cmdidx = MMC_CMD_APP_CMD;
551 cmd.resp_type = MMC_RSP_R1;
552 cmd.cmdarg = mmc->rca << 16;
553 cmd.flags = 0;
554
555 err = mmc_send_cmd(mmc, &cmd, NULL);
556
557 if (err)
558 return err;
559
560 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
561 cmd.resp_type = MMC_RSP_R1;
562 cmd.cmdarg = 0;
563 cmd.flags = 0;
564
565 timeout = 3;
566
567retry_scr:
568 data.dest = (char *)&scr;
569 data.blocksize = 8;
570 data.blocks = 1;
571 data.flags = MMC_DATA_READ;
572
573 err = mmc_send_cmd(mmc, &cmd, &data);
574
575 if (err) {
576 if (timeout--)
577 goto retry_scr;
578
579 return err;
580 }
581
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300582 mmc->scr[0] = __be32_to_cpu(scr[0]);
583 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500584
585 switch ((mmc->scr[0] >> 24) & 0xf) {
586 case 0:
587 mmc->version = SD_VERSION_1_0;
588 break;
589 case 1:
590 mmc->version = SD_VERSION_1_10;
591 break;
592 case 2:
593 mmc->version = SD_VERSION_2;
594 break;
595 default:
596 mmc->version = SD_VERSION_1_0;
597 break;
598 }
599
Alagu Sankarb44c7082010-05-12 15:08:24 +0530600 if (mmc->scr[0] & SD_DATA_4BIT)
601 mmc->card_caps |= MMC_MODE_4BIT;
602
Andy Fleming272cc702008-10-30 16:41:01 -0500603 /* Version 1.0 doesn't support switching */
604 if (mmc->version == SD_VERSION_1_0)
605 return 0;
606
607 timeout = 4;
608 while (timeout--) {
609 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
610 (u8 *)&switch_status);
611
612 if (err)
613 return err;
614
615 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300616 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500617 break;
618 }
619
Andy Fleming272cc702008-10-30 16:41:01 -0500620 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300621 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500622 return 0;
623
624 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
625
626 if (err)
627 return err;
628
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300629 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500630 mmc->card_caps |= MMC_MODE_HS;
631
632 return 0;
633}
634
635/* frequency bases */
636/* divided by 10 to be nice to platforms without floating point */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000637static const int fbase[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500638 10000,
639 100000,
640 1000000,
641 10000000,
642};
643
644/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
645 * to platforms without floating point.
646 */
Mike Frysinger5f837c22010-10-20 01:15:53 +0000647static const int multipliers[] = {
Andy Fleming272cc702008-10-30 16:41:01 -0500648 0, /* reserved */
649 10,
650 12,
651 13,
652 15,
653 20,
654 25,
655 30,
656 35,
657 40,
658 45,
659 50,
660 55,
661 60,
662 70,
663 80,
664};
665
666void mmc_set_ios(struct mmc *mmc)
667{
668 mmc->set_ios(mmc);
669}
670
671void mmc_set_clock(struct mmc *mmc, uint clock)
672{
673 if (clock > mmc->f_max)
674 clock = mmc->f_max;
675
676 if (clock < mmc->f_min)
677 clock = mmc->f_min;
678
679 mmc->clock = clock;
680
681 mmc_set_ios(mmc);
682}
683
684void mmc_set_bus_width(struct mmc *mmc, uint width)
685{
686 mmc->bus_width = width;
687
688 mmc_set_ios(mmc);
689}
690
691int mmc_startup(struct mmc *mmc)
692{
693 int err;
694 uint mult, freq;
695 u64 cmult, csize;
696 struct mmc_cmd cmd;
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +0530697 char ext_csd[512];
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000698 int timeout = 1000;
Andy Fleming272cc702008-10-30 16:41:01 -0500699
Thomas Choud52ebf12010-12-24 13:12:21 +0000700#ifdef CONFIG_MMC_SPI_CRC_ON
701 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
702 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
703 cmd.resp_type = MMC_RSP_R1;
704 cmd.cmdarg = 1;
705 cmd.flags = 0;
706 err = mmc_send_cmd(mmc, &cmd, NULL);
707
708 if (err)
709 return err;
710 }
711#endif
712
Andy Fleming272cc702008-10-30 16:41:01 -0500713 /* Put the Card in Identify Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000714 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
715 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
Andy Fleming272cc702008-10-30 16:41:01 -0500716 cmd.resp_type = MMC_RSP_R2;
717 cmd.cmdarg = 0;
718 cmd.flags = 0;
719
720 err = mmc_send_cmd(mmc, &cmd, NULL);
721
722 if (err)
723 return err;
724
725 memcpy(mmc->cid, cmd.response, 16);
726
727 /*
728 * For MMC cards, set the Relative Address.
729 * For SD cards, get the Relatvie Address.
730 * This also puts the cards into Standby State
731 */
Thomas Choud52ebf12010-12-24 13:12:21 +0000732 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
733 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
734 cmd.cmdarg = mmc->rca << 16;
735 cmd.resp_type = MMC_RSP_R6;
736 cmd.flags = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500737
Thomas Choud52ebf12010-12-24 13:12:21 +0000738 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500739
Thomas Choud52ebf12010-12-24 13:12:21 +0000740 if (err)
741 return err;
Andy Fleming272cc702008-10-30 16:41:01 -0500742
Thomas Choud52ebf12010-12-24 13:12:21 +0000743 if (IS_SD(mmc))
744 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
745 }
Andy Fleming272cc702008-10-30 16:41:01 -0500746
747 /* Get the Card-Specific Data */
748 cmd.cmdidx = MMC_CMD_SEND_CSD;
749 cmd.resp_type = MMC_RSP_R2;
750 cmd.cmdarg = mmc->rca << 16;
751 cmd.flags = 0;
752
753 err = mmc_send_cmd(mmc, &cmd, NULL);
754
Raffaele Recalcati5d4fc8d2011-03-11 02:01:12 +0000755 /* Waiting for the ready status */
756 mmc_send_status(mmc, timeout);
757
Andy Fleming272cc702008-10-30 16:41:01 -0500758 if (err)
759 return err;
760
Rabin Vincent998be3d2009-04-05 13:30:56 +0530761 mmc->csd[0] = cmd.response[0];
762 mmc->csd[1] = cmd.response[1];
763 mmc->csd[2] = cmd.response[2];
764 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -0500765
766 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530767 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500768
769 switch (version) {
770 case 0:
771 mmc->version = MMC_VERSION_1_2;
772 break;
773 case 1:
774 mmc->version = MMC_VERSION_1_4;
775 break;
776 case 2:
777 mmc->version = MMC_VERSION_2_2;
778 break;
779 case 3:
780 mmc->version = MMC_VERSION_3;
781 break;
782 case 4:
783 mmc->version = MMC_VERSION_4;
784 break;
785 default:
786 mmc->version = MMC_VERSION_1_2;
787 break;
788 }
789 }
790
791 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530792 freq = fbase[(cmd.response[0] & 0x7)];
793 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -0500794
795 mmc->tran_speed = freq * mult;
796
Rabin Vincent998be3d2009-04-05 13:30:56 +0530797 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500798
799 if (IS_SD(mmc))
800 mmc->write_bl_len = mmc->read_bl_len;
801 else
Rabin Vincent998be3d2009-04-05 13:30:56 +0530802 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500803
804 if (mmc->high_capacity) {
805 csize = (mmc->csd[1] & 0x3f) << 16
806 | (mmc->csd[2] & 0xffff0000) >> 16;
807 cmult = 8;
808 } else {
809 csize = (mmc->csd[1] & 0x3ff) << 2
810 | (mmc->csd[2] & 0xc0000000) >> 30;
811 cmult = (mmc->csd[2] & 0x00038000) >> 15;
812 }
813
814 mmc->capacity = (csize + 1) << (cmult + 2);
815 mmc->capacity *= mmc->read_bl_len;
816
817 if (mmc->read_bl_len > 512)
818 mmc->read_bl_len = 512;
819
820 if (mmc->write_bl_len > 512)
821 mmc->write_bl_len = 512;
822
823 /* Select the card, and put it into Transfer Mode */
Thomas Choud52ebf12010-12-24 13:12:21 +0000824 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
825 cmd.cmdidx = MMC_CMD_SELECT_CARD;
826 cmd.resp_type = MMC_RSP_R1b;
827 cmd.cmdarg = mmc->rca << 16;
828 cmd.flags = 0;
829 err = mmc_send_cmd(mmc, &cmd, NULL);
Andy Fleming272cc702008-10-30 16:41:01 -0500830
Thomas Choud52ebf12010-12-24 13:12:21 +0000831 if (err)
832 return err;
833 }
Andy Fleming272cc702008-10-30 16:41:01 -0500834
Sukumar Ghoraid23e2c02010-09-20 18:29:29 +0530835 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
836 /* check ext_csd version and capacity */
837 err = mmc_send_ext_csd(mmc, ext_csd);
838 if (!err & (ext_csd[192] >= 2)) {
839 mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
840 ext_csd[214] << 16 | ext_csd[215] << 24;
841 mmc->capacity *= 512;
842 }
843 }
844
Andy Fleming272cc702008-10-30 16:41:01 -0500845 if (IS_SD(mmc))
846 err = sd_change_freq(mmc);
847 else
848 err = mmc_change_freq(mmc);
849
850 if (err)
851 return err;
852
853 /* Restrict card's capabilities by what the host can do */
854 mmc->card_caps &= mmc->host_caps;
855
856 if (IS_SD(mmc)) {
857 if (mmc->card_caps & MMC_MODE_4BIT) {
858 cmd.cmdidx = MMC_CMD_APP_CMD;
859 cmd.resp_type = MMC_RSP_R1;
860 cmd.cmdarg = mmc->rca << 16;
861 cmd.flags = 0;
862
863 err = mmc_send_cmd(mmc, &cmd, NULL);
864 if (err)
865 return err;
866
867 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
868 cmd.resp_type = MMC_RSP_R1;
869 cmd.cmdarg = 2;
870 cmd.flags = 0;
871 err = mmc_send_cmd(mmc, &cmd, NULL);
872 if (err)
873 return err;
874
875 mmc_set_bus_width(mmc, 4);
876 }
877
878 if (mmc->card_caps & MMC_MODE_HS)
879 mmc_set_clock(mmc, 50000000);
880 else
881 mmc_set_clock(mmc, 25000000);
882 } else {
883 if (mmc->card_caps & MMC_MODE_4BIT) {
884 /* Set the card to use 4 bit*/
885 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
886 EXT_CSD_BUS_WIDTH,
887 EXT_CSD_BUS_WIDTH_4);
888
889 if (err)
890 return err;
891
892 mmc_set_bus_width(mmc, 4);
893 } else if (mmc->card_caps & MMC_MODE_8BIT) {
894 /* Set the card to use 8 bit*/
895 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
896 EXT_CSD_BUS_WIDTH,
897 EXT_CSD_BUS_WIDTH_8);
898
899 if (err)
900 return err;
901
902 mmc_set_bus_width(mmc, 8);
903 }
904
905 if (mmc->card_caps & MMC_MODE_HS) {
906 if (mmc->card_caps & MMC_MODE_HS_52MHz)
907 mmc_set_clock(mmc, 52000000);
908 else
909 mmc_set_clock(mmc, 26000000);
910 } else
911 mmc_set_clock(mmc, 20000000);
912 }
913
914 /* fill in device description */
915 mmc->block_dev.lun = 0;
916 mmc->block_dev.type = 0;
917 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +0530918 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530919 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
920 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
921 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
922 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
923 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
924 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
925 (mmc->cid[2] >> 24) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500926 init_part(&mmc->block_dev);
927
928 return 0;
929}
930
931int mmc_send_if_cond(struct mmc *mmc)
932{
933 struct mmc_cmd cmd;
934 int err;
935
936 cmd.cmdidx = SD_CMD_SEND_IF_COND;
937 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
938 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
939 cmd.resp_type = MMC_RSP_R7;
940 cmd.flags = 0;
941
942 err = mmc_send_cmd(mmc, &cmd, NULL);
943
944 if (err)
945 return err;
946
Rabin Vincent998be3d2009-04-05 13:30:56 +0530947 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -0500948 return UNUSABLE_ERR;
949 else
950 mmc->version = SD_VERSION_2;
951
952 return 0;
953}
954
955int mmc_register(struct mmc *mmc)
956{
957 /* Setup the universal parts of the block interface just once */
958 mmc->block_dev.if_type = IF_TYPE_MMC;
959 mmc->block_dev.dev = cur_dev_num++;
960 mmc->block_dev.removable = 1;
961 mmc->block_dev.block_read = mmc_bread;
962 mmc->block_dev.block_write = mmc_bwrite;
963
964 INIT_LIST_HEAD (&mmc->link);
965
966 list_add_tail (&mmc->link, &mmc_devices);
967
968 return 0;
969}
970
971block_dev_desc_t *mmc_get_dev(int dev)
972{
973 struct mmc *mmc = find_mmc_device(dev);
974
Rabin Vincente85649c2009-04-05 13:30:53 +0530975 return mmc ? &mmc->block_dev : NULL;
Andy Fleming272cc702008-10-30 16:41:01 -0500976}
977
978int mmc_init(struct mmc *mmc)
979{
980 int err;
981
982 err = mmc->init(mmc);
983
984 if (err)
985 return err;
986
Ilya Yanokb86b85e2009-06-29 17:53:16 +0400987 mmc_set_bus_width(mmc, 1);
988 mmc_set_clock(mmc, 1);
989
Andy Fleming272cc702008-10-30 16:41:01 -0500990 /* Reset the Card */
991 err = mmc_go_idle(mmc);
992
993 if (err)
994 return err;
995
996 /* Test for SD version 2 */
997 err = mmc_send_if_cond(mmc);
998
Andy Fleming272cc702008-10-30 16:41:01 -0500999 /* Now try to get the SD card's operating condition */
1000 err = sd_send_op_cond(mmc);
1001
1002 /* If the command timed out, we check for an MMC card */
1003 if (err == TIMEOUT) {
1004 err = mmc_send_op_cond(mmc);
1005
1006 if (err) {
1007 printf("Card did not respond to voltage select!\n");
1008 return UNUSABLE_ERR;
1009 }
1010 }
1011
1012 return mmc_startup(mmc);
1013}
1014
1015/*
1016 * CPU and board-specific MMC initializations. Aliased function
1017 * signals caller to move on
1018 */
1019static int __def_mmc_init(bd_t *bis)
1020{
1021 return -1;
1022}
1023
Peter Tyserf9a109b2009-04-20 11:08:46 -05001024int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1025int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -05001026
1027void print_mmc_devices(char separator)
1028{
1029 struct mmc *m;
1030 struct list_head *entry;
1031
1032 list_for_each(entry, &mmc_devices) {
1033 m = list_entry(entry, struct mmc, link);
1034
1035 printf("%s: %d", m->name, m->block_dev.dev);
1036
1037 if (entry->next != &mmc_devices)
1038 printf("%c ", separator);
1039 }
1040
1041 printf("\n");
1042}
1043
1044int mmc_initialize(bd_t *bis)
1045{
1046 INIT_LIST_HEAD (&mmc_devices);
1047 cur_dev_num = 0;
1048
1049 if (board_mmc_init(bis) < 0)
1050 cpu_mmc_init(bis);
1051
1052 print_mmc_devices(',');
1053
1054 return 0;
1055}