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