blob: 23928c1356e76fcb472a138ad4e6a3e51f864f63 [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>
33#include <mmc.h>
Rabin Vincent9b1f9422009-04-05 13:30:54 +053034#include <div64.h>
Andy Fleming272cc702008-10-30 16:41:01 -050035
36static struct list_head mmc_devices;
37static int cur_dev_num = -1;
38
Stefano Babic11fdade2010-02-05 15:04:43 +010039int __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
40 return -1;
41}
42
43int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
44 alias("__board_mmc_getcd")));
45
Andy Fleming272cc702008-10-30 16:41:01 -050046int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
47{
48 return mmc->send_cmd(mmc, cmd, data);
49}
50
51int mmc_set_blocklen(struct mmc *mmc, int len)
52{
53 struct mmc_cmd cmd;
54
55 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
56 cmd.resp_type = MMC_RSP_R1;
57 cmd.cmdarg = len;
58 cmd.flags = 0;
59
60 return mmc_send_cmd(mmc, &cmd, NULL);
61}
62
63struct mmc *find_mmc_device(int dev_num)
64{
65 struct mmc *m;
66 struct list_head *entry;
67
68 list_for_each(entry, &mmc_devices) {
69 m = list_entry(entry, struct mmc, link);
70
71 if (m->block_dev.dev == dev_num)
72 return m;
73 }
74
75 printf("MMC Device %d not found\n", dev_num);
76
77 return NULL;
78}
79
80static ulong
81mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
82{
83 struct mmc_cmd cmd;
84 struct mmc_data data;
85 int err;
86 int stoperr = 0;
87 struct mmc *mmc = find_mmc_device(dev_num);
88 int blklen;
89
90 if (!mmc)
91 return -1;
92
93 blklen = mmc->write_bl_len;
94
Lei Wend2bf29e2010-09-13 22:07:27 +080095 if ((start + blkcnt) > mmc->block_dev.lba) {
96 printf("MMC: block number 0x%x exceeds max(0x%x)",
97 start + blkcnt, mmc->block_dev.lba);
98 return 0;
99 }
Andy Fleming272cc702008-10-30 16:41:01 -0500100 err = mmc_set_blocklen(mmc, mmc->write_bl_len);
101
102 if (err) {
103 printf("set write bl len failed\n\r");
104 return err;
105 }
106
107 if (blkcnt > 1)
108 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
109 else
110 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
111
112 if (mmc->high_capacity)
113 cmd.cmdarg = start;
114 else
115 cmd.cmdarg = start * blklen;
116
117 cmd.resp_type = MMC_RSP_R1;
118 cmd.flags = 0;
119
120 data.src = src;
121 data.blocks = blkcnt;
122 data.blocksize = blklen;
123 data.flags = MMC_DATA_WRITE;
124
125 err = mmc_send_cmd(mmc, &cmd, &data);
126
127 if (err) {
128 printf("mmc write failed\n\r");
129 return err;
130 }
131
132 if (blkcnt > 1) {
133 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
134 cmd.cmdarg = 0;
135 cmd.resp_type = MMC_RSP_R1b;
136 cmd.flags = 0;
137 stoperr = mmc_send_cmd(mmc, &cmd, NULL);
138 }
139
140 return blkcnt;
141}
142
143int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum)
144{
145 struct mmc_cmd cmd;
146 struct mmc_data data;
147
148 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
149
150 if (mmc->high_capacity)
151 cmd.cmdarg = blocknum;
152 else
153 cmd.cmdarg = blocknum * mmc->read_bl_len;
154
155 cmd.resp_type = MMC_RSP_R1;
156 cmd.flags = 0;
157
158 data.dest = dst;
159 data.blocks = 1;
160 data.blocksize = mmc->read_bl_len;
161 data.flags = MMC_DATA_READ;
162
163 return mmc_send_cmd(mmc, &cmd, &data);
164}
165
166int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size)
167{
168 char *buffer;
169 int i;
170 int blklen = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +0530171 int startblock = lldiv(src, mmc->read_bl_len);
172 int endblock = lldiv(src + size - 1, mmc->read_bl_len);
Andy Fleming272cc702008-10-30 16:41:01 -0500173 int err = 0;
174
175 /* Make a buffer big enough to hold all the blocks we might read */
176 buffer = malloc(blklen);
177
178 if (!buffer) {
179 printf("Could not allocate buffer for MMC read!\n");
180 return -1;
181 }
182
183 /* We always do full block reads from the card */
184 err = mmc_set_blocklen(mmc, mmc->read_bl_len);
185
186 if (err)
Wolfgang Denk8c4444f2010-03-11 23:35:43 +0100187 goto free_buffer;
Andy Fleming272cc702008-10-30 16:41:01 -0500188
189 for (i = startblock; i <= endblock; i++) {
190 int segment_size;
191 int offset;
192
193 err = mmc_read_block(mmc, buffer, i);
194
195 if (err)
196 goto free_buffer;
197
198 /*
199 * The first block may not be aligned, so we
200 * copy from the desired point in the block
201 */
202 offset = (src & (blklen - 1));
203 segment_size = MIN(blklen - offset, size);
204
205 memcpy(dst, buffer + offset, segment_size);
206
207 dst += segment_size;
208 src += segment_size;
209 size -= segment_size;
210 }
211
212free_buffer:
213 free(buffer);
214
215 return err;
216}
217
218static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
219{
220 int err;
221 int i;
222 struct mmc *mmc = find_mmc_device(dev_num);
223
224 if (!mmc)
225 return 0;
226
Lei Wend2bf29e2010-09-13 22:07:27 +0800227 if ((start + blkcnt) > mmc->block_dev.lba) {
228 printf("MMC: block number 0x%x exceeds max(0x%x)",
229 start + blkcnt, mmc->block_dev.lba);
230 return 0;
231 }
Andy Fleming272cc702008-10-30 16:41:01 -0500232 /* We always do full block reads from the card */
233 err = mmc_set_blocklen(mmc, mmc->read_bl_len);
234
235 if (err) {
236 return 0;
237 }
238
239 for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) {
240 err = mmc_read_block(mmc, dst, i);
241
242 if (err) {
243 printf("block read failed: %d\n", err);
244 return i - start;
245 }
246 }
247
248 return blkcnt;
249}
250
251int mmc_go_idle(struct mmc* mmc)
252{
253 struct mmc_cmd cmd;
254 int err;
255
256 udelay(1000);
257
258 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
259 cmd.cmdarg = 0;
260 cmd.resp_type = MMC_RSP_NONE;
261 cmd.flags = 0;
262
263 err = mmc_send_cmd(mmc, &cmd, NULL);
264
265 if (err)
266 return err;
267
268 udelay(2000);
269
270 return 0;
271}
272
273int
274sd_send_op_cond(struct mmc *mmc)
275{
276 int timeout = 1000;
277 int err;
278 struct mmc_cmd cmd;
279
280 do {
281 cmd.cmdidx = MMC_CMD_APP_CMD;
282 cmd.resp_type = MMC_RSP_R1;
283 cmd.cmdarg = 0;
284 cmd.flags = 0;
285
286 err = mmc_send_cmd(mmc, &cmd, NULL);
287
288 if (err)
289 return err;
290
291 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
292 cmd.resp_type = MMC_RSP_R3;
Stefano Babic250de122010-01-20 18:20:39 +0100293
294 /*
295 * Most cards do not answer if some reserved bits
296 * in the ocr are set. However, Some controller
297 * can set bit 7 (reserved for low voltages), but
298 * how to manage low voltages SD card is not yet
299 * specified.
300 */
301 cmd.cmdarg = mmc->voltages & 0xff8000;
Andy Fleming272cc702008-10-30 16:41:01 -0500302
303 if (mmc->version == SD_VERSION_2)
304 cmd.cmdarg |= OCR_HCS;
305
306 err = mmc_send_cmd(mmc, &cmd, NULL);
307
308 if (err)
309 return err;
310
311 udelay(1000);
312 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
313
314 if (timeout <= 0)
315 return UNUSABLE_ERR;
316
317 if (mmc->version != SD_VERSION_2)
318 mmc->version = SD_VERSION_1_0;
319
Rabin Vincent998be3d2009-04-05 13:30:56 +0530320 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500321
322 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
323 mmc->rca = 0;
324
325 return 0;
326}
327
328int mmc_send_op_cond(struct mmc *mmc)
329{
330 int timeout = 1000;
331 struct mmc_cmd cmd;
332 int err;
333
334 /* Some cards seem to need this */
335 mmc_go_idle(mmc);
336
337 do {
338 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
339 cmd.resp_type = MMC_RSP_R3;
340 cmd.cmdarg = OCR_HCS | mmc->voltages;
341 cmd.flags = 0;
342
343 err = mmc_send_cmd(mmc, &cmd, NULL);
344
345 if (err)
346 return err;
347
348 udelay(1000);
349 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
350
351 if (timeout <= 0)
352 return UNUSABLE_ERR;
353
354 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530355 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500356
357 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
358 mmc->rca = 0;
359
360 return 0;
361}
362
363
364int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
365{
366 struct mmc_cmd cmd;
367 struct mmc_data data;
368 int err;
369
370 /* Get the Card Status Register */
371 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
372 cmd.resp_type = MMC_RSP_R1;
373 cmd.cmdarg = 0;
374 cmd.flags = 0;
375
376 data.dest = ext_csd;
377 data.blocks = 1;
378 data.blocksize = 512;
379 data.flags = MMC_DATA_READ;
380
381 err = mmc_send_cmd(mmc, &cmd, &data);
382
383 return err;
384}
385
386
387int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
388{
389 struct mmc_cmd cmd;
390
391 cmd.cmdidx = MMC_CMD_SWITCH;
392 cmd.resp_type = MMC_RSP_R1b;
393 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
394 (index << 16) |
395 (value << 8);
396 cmd.flags = 0;
397
398 return mmc_send_cmd(mmc, &cmd, NULL);
399}
400
401int mmc_change_freq(struct mmc *mmc)
402{
403 char ext_csd[512];
404 char cardtype;
405 int err;
406
407 mmc->card_caps = 0;
408
409 /* Only version 4 supports high-speed */
410 if (mmc->version < MMC_VERSION_4)
411 return 0;
412
413 mmc->card_caps |= MMC_MODE_4BIT;
414
415 err = mmc_send_ext_csd(mmc, ext_csd);
416
417 if (err)
418 return err;
419
420 if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
421 mmc->high_capacity = 1;
422
423 cardtype = ext_csd[196] & 0xf;
424
425 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
426
427 if (err)
428 return err;
429
430 /* Now check to see that it worked */
431 err = mmc_send_ext_csd(mmc, ext_csd);
432
433 if (err)
434 return err;
435
436 /* No high-speed support */
437 if (!ext_csd[185])
438 return 0;
439
440 /* High Speed is set, there are two types: 52MHz and 26MHz */
441 if (cardtype & MMC_HS_52MHZ)
442 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
443 else
444 mmc->card_caps |= MMC_MODE_HS;
445
446 return 0;
447}
448
449int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
450{
451 struct mmc_cmd cmd;
452 struct mmc_data data;
453
454 /* Switch the frequency */
455 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
456 cmd.resp_type = MMC_RSP_R1;
457 cmd.cmdarg = (mode << 31) | 0xffffff;
458 cmd.cmdarg &= ~(0xf << (group * 4));
459 cmd.cmdarg |= value << (group * 4);
460 cmd.flags = 0;
461
462 data.dest = (char *)resp;
463 data.blocksize = 64;
464 data.blocks = 1;
465 data.flags = MMC_DATA_READ;
466
467 return mmc_send_cmd(mmc, &cmd, &data);
468}
469
470
471int sd_change_freq(struct mmc *mmc)
472{
473 int err;
474 struct mmc_cmd cmd;
475 uint scr[2];
476 uint switch_status[16];
477 struct mmc_data data;
478 int timeout;
479
480 mmc->card_caps = 0;
481
482 /* Read the SCR to find out if this card supports higher speeds */
483 cmd.cmdidx = MMC_CMD_APP_CMD;
484 cmd.resp_type = MMC_RSP_R1;
485 cmd.cmdarg = mmc->rca << 16;
486 cmd.flags = 0;
487
488 err = mmc_send_cmd(mmc, &cmd, NULL);
489
490 if (err)
491 return err;
492
493 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
494 cmd.resp_type = MMC_RSP_R1;
495 cmd.cmdarg = 0;
496 cmd.flags = 0;
497
498 timeout = 3;
499
500retry_scr:
501 data.dest = (char *)&scr;
502 data.blocksize = 8;
503 data.blocks = 1;
504 data.flags = MMC_DATA_READ;
505
506 err = mmc_send_cmd(mmc, &cmd, &data);
507
508 if (err) {
509 if (timeout--)
510 goto retry_scr;
511
512 return err;
513 }
514
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300515 mmc->scr[0] = __be32_to_cpu(scr[0]);
516 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500517
518 switch ((mmc->scr[0] >> 24) & 0xf) {
519 case 0:
520 mmc->version = SD_VERSION_1_0;
521 break;
522 case 1:
523 mmc->version = SD_VERSION_1_10;
524 break;
525 case 2:
526 mmc->version = SD_VERSION_2;
527 break;
528 default:
529 mmc->version = SD_VERSION_1_0;
530 break;
531 }
532
533 /* Version 1.0 doesn't support switching */
534 if (mmc->version == SD_VERSION_1_0)
535 return 0;
536
537 timeout = 4;
538 while (timeout--) {
539 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
540 (u8 *)&switch_status);
541
542 if (err)
543 return err;
544
545 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300546 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500547 break;
548 }
549
550 if (mmc->scr[0] & SD_DATA_4BIT)
551 mmc->card_caps |= MMC_MODE_4BIT;
552
553 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300554 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500555 return 0;
556
557 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
558
559 if (err)
560 return err;
561
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300562 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500563 mmc->card_caps |= MMC_MODE_HS;
564
565 return 0;
566}
567
568/* frequency bases */
569/* divided by 10 to be nice to platforms without floating point */
570int fbase[] = {
571 10000,
572 100000,
573 1000000,
574 10000000,
575};
576
577/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
578 * to platforms without floating point.
579 */
580int multipliers[] = {
581 0, /* reserved */
582 10,
583 12,
584 13,
585 15,
586 20,
587 25,
588 30,
589 35,
590 40,
591 45,
592 50,
593 55,
594 60,
595 70,
596 80,
597};
598
599void mmc_set_ios(struct mmc *mmc)
600{
601 mmc->set_ios(mmc);
602}
603
604void mmc_set_clock(struct mmc *mmc, uint clock)
605{
606 if (clock > mmc->f_max)
607 clock = mmc->f_max;
608
609 if (clock < mmc->f_min)
610 clock = mmc->f_min;
611
612 mmc->clock = clock;
613
614 mmc_set_ios(mmc);
615}
616
617void mmc_set_bus_width(struct mmc *mmc, uint width)
618{
619 mmc->bus_width = width;
620
621 mmc_set_ios(mmc);
622}
623
624int mmc_startup(struct mmc *mmc)
625{
626 int err;
627 uint mult, freq;
628 u64 cmult, csize;
629 struct mmc_cmd cmd;
630
631 /* Put the Card in Identify Mode */
632 cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
633 cmd.resp_type = MMC_RSP_R2;
634 cmd.cmdarg = 0;
635 cmd.flags = 0;
636
637 err = mmc_send_cmd(mmc, &cmd, NULL);
638
639 if (err)
640 return err;
641
642 memcpy(mmc->cid, cmd.response, 16);
643
644 /*
645 * For MMC cards, set the Relative Address.
646 * For SD cards, get the Relatvie Address.
647 * This also puts the cards into Standby State
648 */
649 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
650 cmd.cmdarg = mmc->rca << 16;
651 cmd.resp_type = MMC_RSP_R6;
652 cmd.flags = 0;
653
654 err = mmc_send_cmd(mmc, &cmd, NULL);
655
656 if (err)
657 return err;
658
659 if (IS_SD(mmc))
Rabin Vincent998be3d2009-04-05 13:30:56 +0530660 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
Andy Fleming272cc702008-10-30 16:41:01 -0500661
662 /* Get the Card-Specific Data */
663 cmd.cmdidx = MMC_CMD_SEND_CSD;
664 cmd.resp_type = MMC_RSP_R2;
665 cmd.cmdarg = mmc->rca << 16;
666 cmd.flags = 0;
667
668 err = mmc_send_cmd(mmc, &cmd, NULL);
669
670 if (err)
671 return err;
672
Rabin Vincent998be3d2009-04-05 13:30:56 +0530673 mmc->csd[0] = cmd.response[0];
674 mmc->csd[1] = cmd.response[1];
675 mmc->csd[2] = cmd.response[2];
676 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -0500677
678 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530679 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500680
681 switch (version) {
682 case 0:
683 mmc->version = MMC_VERSION_1_2;
684 break;
685 case 1:
686 mmc->version = MMC_VERSION_1_4;
687 break;
688 case 2:
689 mmc->version = MMC_VERSION_2_2;
690 break;
691 case 3:
692 mmc->version = MMC_VERSION_3;
693 break;
694 case 4:
695 mmc->version = MMC_VERSION_4;
696 break;
697 default:
698 mmc->version = MMC_VERSION_1_2;
699 break;
700 }
701 }
702
703 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530704 freq = fbase[(cmd.response[0] & 0x7)];
705 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -0500706
707 mmc->tran_speed = freq * mult;
708
Rabin Vincent998be3d2009-04-05 13:30:56 +0530709 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500710
711 if (IS_SD(mmc))
712 mmc->write_bl_len = mmc->read_bl_len;
713 else
Rabin Vincent998be3d2009-04-05 13:30:56 +0530714 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500715
716 if (mmc->high_capacity) {
717 csize = (mmc->csd[1] & 0x3f) << 16
718 | (mmc->csd[2] & 0xffff0000) >> 16;
719 cmult = 8;
720 } else {
721 csize = (mmc->csd[1] & 0x3ff) << 2
722 | (mmc->csd[2] & 0xc0000000) >> 30;
723 cmult = (mmc->csd[2] & 0x00038000) >> 15;
724 }
725
726 mmc->capacity = (csize + 1) << (cmult + 2);
727 mmc->capacity *= mmc->read_bl_len;
728
729 if (mmc->read_bl_len > 512)
730 mmc->read_bl_len = 512;
731
732 if (mmc->write_bl_len > 512)
733 mmc->write_bl_len = 512;
734
735 /* Select the card, and put it into Transfer Mode */
736 cmd.cmdidx = MMC_CMD_SELECT_CARD;
737 cmd.resp_type = MMC_RSP_R1b;
738 cmd.cmdarg = mmc->rca << 16;
739 cmd.flags = 0;
740 err = mmc_send_cmd(mmc, &cmd, NULL);
741
742 if (err)
743 return err;
744
745 if (IS_SD(mmc))
746 err = sd_change_freq(mmc);
747 else
748 err = mmc_change_freq(mmc);
749
750 if (err)
751 return err;
752
753 /* Restrict card's capabilities by what the host can do */
754 mmc->card_caps &= mmc->host_caps;
755
756 if (IS_SD(mmc)) {
757 if (mmc->card_caps & MMC_MODE_4BIT) {
758 cmd.cmdidx = MMC_CMD_APP_CMD;
759 cmd.resp_type = MMC_RSP_R1;
760 cmd.cmdarg = mmc->rca << 16;
761 cmd.flags = 0;
762
763 err = mmc_send_cmd(mmc, &cmd, NULL);
764 if (err)
765 return err;
766
767 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
768 cmd.resp_type = MMC_RSP_R1;
769 cmd.cmdarg = 2;
770 cmd.flags = 0;
771 err = mmc_send_cmd(mmc, &cmd, NULL);
772 if (err)
773 return err;
774
775 mmc_set_bus_width(mmc, 4);
776 }
777
778 if (mmc->card_caps & MMC_MODE_HS)
779 mmc_set_clock(mmc, 50000000);
780 else
781 mmc_set_clock(mmc, 25000000);
782 } else {
783 if (mmc->card_caps & MMC_MODE_4BIT) {
784 /* Set the card to use 4 bit*/
785 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
786 EXT_CSD_BUS_WIDTH,
787 EXT_CSD_BUS_WIDTH_4);
788
789 if (err)
790 return err;
791
792 mmc_set_bus_width(mmc, 4);
793 } else if (mmc->card_caps & MMC_MODE_8BIT) {
794 /* Set the card to use 8 bit*/
795 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
796 EXT_CSD_BUS_WIDTH,
797 EXT_CSD_BUS_WIDTH_8);
798
799 if (err)
800 return err;
801
802 mmc_set_bus_width(mmc, 8);
803 }
804
805 if (mmc->card_caps & MMC_MODE_HS) {
806 if (mmc->card_caps & MMC_MODE_HS_52MHz)
807 mmc_set_clock(mmc, 52000000);
808 else
809 mmc_set_clock(mmc, 26000000);
810 } else
811 mmc_set_clock(mmc, 20000000);
812 }
813
814 /* fill in device description */
815 mmc->block_dev.lun = 0;
816 mmc->block_dev.type = 0;
817 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +0530818 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530819 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
820 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
821 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
822 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
823 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
824 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
825 (mmc->cid[2] >> 24) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500826 init_part(&mmc->block_dev);
827
828 return 0;
829}
830
831int mmc_send_if_cond(struct mmc *mmc)
832{
833 struct mmc_cmd cmd;
834 int err;
835
836 cmd.cmdidx = SD_CMD_SEND_IF_COND;
837 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
838 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
839 cmd.resp_type = MMC_RSP_R7;
840 cmd.flags = 0;
841
842 err = mmc_send_cmd(mmc, &cmd, NULL);
843
844 if (err)
845 return err;
846
Rabin Vincent998be3d2009-04-05 13:30:56 +0530847 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -0500848 return UNUSABLE_ERR;
849 else
850 mmc->version = SD_VERSION_2;
851
852 return 0;
853}
854
855int mmc_register(struct mmc *mmc)
856{
857 /* Setup the universal parts of the block interface just once */
858 mmc->block_dev.if_type = IF_TYPE_MMC;
859 mmc->block_dev.dev = cur_dev_num++;
860 mmc->block_dev.removable = 1;
861 mmc->block_dev.block_read = mmc_bread;
862 mmc->block_dev.block_write = mmc_bwrite;
863
864 INIT_LIST_HEAD (&mmc->link);
865
866 list_add_tail (&mmc->link, &mmc_devices);
867
868 return 0;
869}
870
871block_dev_desc_t *mmc_get_dev(int dev)
872{
873 struct mmc *mmc = find_mmc_device(dev);
874
Rabin Vincente85649c2009-04-05 13:30:53 +0530875 return mmc ? &mmc->block_dev : NULL;
Andy Fleming272cc702008-10-30 16:41:01 -0500876}
877
878int mmc_init(struct mmc *mmc)
879{
880 int err;
881
882 err = mmc->init(mmc);
883
884 if (err)
885 return err;
886
Ilya Yanokb86b85e2009-06-29 17:53:16 +0400887 mmc_set_bus_width(mmc, 1);
888 mmc_set_clock(mmc, 1);
889
Andy Fleming272cc702008-10-30 16:41:01 -0500890 /* Reset the Card */
891 err = mmc_go_idle(mmc);
892
893 if (err)
894 return err;
895
896 /* Test for SD version 2 */
897 err = mmc_send_if_cond(mmc);
898
Andy Fleming272cc702008-10-30 16:41:01 -0500899 /* Now try to get the SD card's operating condition */
900 err = sd_send_op_cond(mmc);
901
902 /* If the command timed out, we check for an MMC card */
903 if (err == TIMEOUT) {
904 err = mmc_send_op_cond(mmc);
905
906 if (err) {
907 printf("Card did not respond to voltage select!\n");
908 return UNUSABLE_ERR;
909 }
910 }
911
912 return mmc_startup(mmc);
913}
914
915/*
916 * CPU and board-specific MMC initializations. Aliased function
917 * signals caller to move on
918 */
919static int __def_mmc_init(bd_t *bis)
920{
921 return -1;
922}
923
Peter Tyserf9a109b2009-04-20 11:08:46 -0500924int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
925int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -0500926
927void print_mmc_devices(char separator)
928{
929 struct mmc *m;
930 struct list_head *entry;
931
932 list_for_each(entry, &mmc_devices) {
933 m = list_entry(entry, struct mmc, link);
934
935 printf("%s: %d", m->name, m->block_dev.dev);
936
937 if (entry->next != &mmc_devices)
938 printf("%c ", separator);
939 }
940
941 printf("\n");
942}
943
944int mmc_initialize(bd_t *bis)
945{
946 INIT_LIST_HEAD (&mmc_devices);
947 cur_dev_num = 0;
948
949 if (board_mmc_init(bis) < 0)
950 cpu_mmc_init(bis);
951
952 print_mmc_devices(',');
953
954 return 0;
955}