blob: d91b9b7f3a3d9d659027c45c2f2e1825d59286af [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
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 Vincent9b1f9422009-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 Fleming272cc702008-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;
Stefano Babic250de122010-01-20 18:20:39 +0100276
277 /*
278 * Most cards do not answer if some reserved bits
279 * in the ocr are set. However, Some controller
280 * can set bit 7 (reserved for low voltages), but
281 * how to manage low voltages SD card is not yet
282 * specified.
283 */
284 cmd.cmdarg = mmc->voltages & 0xff8000;
Andy Fleming272cc702008-10-30 16:41:01 -0500285
286 if (mmc->version == SD_VERSION_2)
287 cmd.cmdarg |= OCR_HCS;
288
289 err = mmc_send_cmd(mmc, &cmd, NULL);
290
291 if (err)
292 return err;
293
294 udelay(1000);
295 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
296
297 if (timeout <= 0)
298 return UNUSABLE_ERR;
299
300 if (mmc->version != SD_VERSION_2)
301 mmc->version = SD_VERSION_1_0;
302
Rabin Vincent998be3d2009-04-05 13:30:56 +0530303 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500304
305 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
306 mmc->rca = 0;
307
308 return 0;
309}
310
311int mmc_send_op_cond(struct mmc *mmc)
312{
313 int timeout = 1000;
314 struct mmc_cmd cmd;
315 int err;
316
317 /* Some cards seem to need this */
318 mmc_go_idle(mmc);
319
320 do {
321 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
322 cmd.resp_type = MMC_RSP_R3;
323 cmd.cmdarg = OCR_HCS | mmc->voltages;
324 cmd.flags = 0;
325
326 err = mmc_send_cmd(mmc, &cmd, NULL);
327
328 if (err)
329 return err;
330
331 udelay(1000);
332 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
333
334 if (timeout <= 0)
335 return UNUSABLE_ERR;
336
337 mmc->version = MMC_VERSION_UNKNOWN;
Rabin Vincent998be3d2009-04-05 13:30:56 +0530338 mmc->ocr = cmd.response[0];
Andy Fleming272cc702008-10-30 16:41:01 -0500339
340 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
341 mmc->rca = 0;
342
343 return 0;
344}
345
346
347int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
348{
349 struct mmc_cmd cmd;
350 struct mmc_data data;
351 int err;
352
353 /* Get the Card Status Register */
354 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
355 cmd.resp_type = MMC_RSP_R1;
356 cmd.cmdarg = 0;
357 cmd.flags = 0;
358
359 data.dest = ext_csd;
360 data.blocks = 1;
361 data.blocksize = 512;
362 data.flags = MMC_DATA_READ;
363
364 err = mmc_send_cmd(mmc, &cmd, &data);
365
366 return err;
367}
368
369
370int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
371{
372 struct mmc_cmd cmd;
373
374 cmd.cmdidx = MMC_CMD_SWITCH;
375 cmd.resp_type = MMC_RSP_R1b;
376 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
377 (index << 16) |
378 (value << 8);
379 cmd.flags = 0;
380
381 return mmc_send_cmd(mmc, &cmd, NULL);
382}
383
384int mmc_change_freq(struct mmc *mmc)
385{
386 char ext_csd[512];
387 char cardtype;
388 int err;
389
390 mmc->card_caps = 0;
391
392 /* Only version 4 supports high-speed */
393 if (mmc->version < MMC_VERSION_4)
394 return 0;
395
396 mmc->card_caps |= MMC_MODE_4BIT;
397
398 err = mmc_send_ext_csd(mmc, ext_csd);
399
400 if (err)
401 return err;
402
403 if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
404 mmc->high_capacity = 1;
405
406 cardtype = ext_csd[196] & 0xf;
407
408 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
409
410 if (err)
411 return err;
412
413 /* Now check to see that it worked */
414 err = mmc_send_ext_csd(mmc, ext_csd);
415
416 if (err)
417 return err;
418
419 /* No high-speed support */
420 if (!ext_csd[185])
421 return 0;
422
423 /* High Speed is set, there are two types: 52MHz and 26MHz */
424 if (cardtype & MMC_HS_52MHZ)
425 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
426 else
427 mmc->card_caps |= MMC_MODE_HS;
428
429 return 0;
430}
431
432int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
433{
434 struct mmc_cmd cmd;
435 struct mmc_data data;
436
437 /* Switch the frequency */
438 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
439 cmd.resp_type = MMC_RSP_R1;
440 cmd.cmdarg = (mode << 31) | 0xffffff;
441 cmd.cmdarg &= ~(0xf << (group * 4));
442 cmd.cmdarg |= value << (group * 4);
443 cmd.flags = 0;
444
445 data.dest = (char *)resp;
446 data.blocksize = 64;
447 data.blocks = 1;
448 data.flags = MMC_DATA_READ;
449
450 return mmc_send_cmd(mmc, &cmd, &data);
451}
452
453
454int sd_change_freq(struct mmc *mmc)
455{
456 int err;
457 struct mmc_cmd cmd;
458 uint scr[2];
459 uint switch_status[16];
460 struct mmc_data data;
461 int timeout;
462
463 mmc->card_caps = 0;
464
465 /* Read the SCR to find out if this card supports higher speeds */
466 cmd.cmdidx = MMC_CMD_APP_CMD;
467 cmd.resp_type = MMC_RSP_R1;
468 cmd.cmdarg = mmc->rca << 16;
469 cmd.flags = 0;
470
471 err = mmc_send_cmd(mmc, &cmd, NULL);
472
473 if (err)
474 return err;
475
476 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
477 cmd.resp_type = MMC_RSP_R1;
478 cmd.cmdarg = 0;
479 cmd.flags = 0;
480
481 timeout = 3;
482
483retry_scr:
484 data.dest = (char *)&scr;
485 data.blocksize = 8;
486 data.blocks = 1;
487 data.flags = MMC_DATA_READ;
488
489 err = mmc_send_cmd(mmc, &cmd, &data);
490
491 if (err) {
492 if (timeout--)
493 goto retry_scr;
494
495 return err;
496 }
497
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300498 mmc->scr[0] = __be32_to_cpu(scr[0]);
499 mmc->scr[1] = __be32_to_cpu(scr[1]);
Andy Fleming272cc702008-10-30 16:41:01 -0500500
501 switch ((mmc->scr[0] >> 24) & 0xf) {
502 case 0:
503 mmc->version = SD_VERSION_1_0;
504 break;
505 case 1:
506 mmc->version = SD_VERSION_1_10;
507 break;
508 case 2:
509 mmc->version = SD_VERSION_2;
510 break;
511 default:
512 mmc->version = SD_VERSION_1_0;
513 break;
514 }
515
516 /* Version 1.0 doesn't support switching */
517 if (mmc->version == SD_VERSION_1_0)
518 return 0;
519
520 timeout = 4;
521 while (timeout--) {
522 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
523 (u8 *)&switch_status);
524
525 if (err)
526 return err;
527
528 /* The high-speed function is busy. Try again */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300529 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
Andy Fleming272cc702008-10-30 16:41:01 -0500530 break;
531 }
532
533 if (mmc->scr[0] & SD_DATA_4BIT)
534 mmc->card_caps |= MMC_MODE_4BIT;
535
536 /* If high-speed isn't supported, we return */
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300537 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
Andy Fleming272cc702008-10-30 16:41:01 -0500538 return 0;
539
540 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
541
542 if (err)
543 return err;
544
Yauhen Kharuzhy4e3d89b2009-05-07 00:43:30 +0300545 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
Andy Fleming272cc702008-10-30 16:41:01 -0500546 mmc->card_caps |= MMC_MODE_HS;
547
548 return 0;
549}
550
551/* frequency bases */
552/* divided by 10 to be nice to platforms without floating point */
553int fbase[] = {
554 10000,
555 100000,
556 1000000,
557 10000000,
558};
559
560/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
561 * to platforms without floating point.
562 */
563int multipliers[] = {
564 0, /* reserved */
565 10,
566 12,
567 13,
568 15,
569 20,
570 25,
571 30,
572 35,
573 40,
574 45,
575 50,
576 55,
577 60,
578 70,
579 80,
580};
581
582void mmc_set_ios(struct mmc *mmc)
583{
584 mmc->set_ios(mmc);
585}
586
587void mmc_set_clock(struct mmc *mmc, uint clock)
588{
589 if (clock > mmc->f_max)
590 clock = mmc->f_max;
591
592 if (clock < mmc->f_min)
593 clock = mmc->f_min;
594
595 mmc->clock = clock;
596
597 mmc_set_ios(mmc);
598}
599
600void mmc_set_bus_width(struct mmc *mmc, uint width)
601{
602 mmc->bus_width = width;
603
604 mmc_set_ios(mmc);
605}
606
607int mmc_startup(struct mmc *mmc)
608{
609 int err;
610 uint mult, freq;
611 u64 cmult, csize;
612 struct mmc_cmd cmd;
613
614 /* Put the Card in Identify Mode */
615 cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
616 cmd.resp_type = MMC_RSP_R2;
617 cmd.cmdarg = 0;
618 cmd.flags = 0;
619
620 err = mmc_send_cmd(mmc, &cmd, NULL);
621
622 if (err)
623 return err;
624
625 memcpy(mmc->cid, cmd.response, 16);
626
627 /*
628 * For MMC cards, set the Relative Address.
629 * For SD cards, get the Relatvie Address.
630 * This also puts the cards into Standby State
631 */
632 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
633 cmd.cmdarg = mmc->rca << 16;
634 cmd.resp_type = MMC_RSP_R6;
635 cmd.flags = 0;
636
637 err = mmc_send_cmd(mmc, &cmd, NULL);
638
639 if (err)
640 return err;
641
642 if (IS_SD(mmc))
Rabin Vincent998be3d2009-04-05 13:30:56 +0530643 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
Andy Fleming272cc702008-10-30 16:41:01 -0500644
645 /* Get the Card-Specific Data */
646 cmd.cmdidx = MMC_CMD_SEND_CSD;
647 cmd.resp_type = MMC_RSP_R2;
648 cmd.cmdarg = mmc->rca << 16;
649 cmd.flags = 0;
650
651 err = mmc_send_cmd(mmc, &cmd, NULL);
652
653 if (err)
654 return err;
655
Rabin Vincent998be3d2009-04-05 13:30:56 +0530656 mmc->csd[0] = cmd.response[0];
657 mmc->csd[1] = cmd.response[1];
658 mmc->csd[2] = cmd.response[2];
659 mmc->csd[3] = cmd.response[3];
Andy Fleming272cc702008-10-30 16:41:01 -0500660
661 if (mmc->version == MMC_VERSION_UNKNOWN) {
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530662 int version = (cmd.response[0] >> 26) & 0xf;
Andy Fleming272cc702008-10-30 16:41:01 -0500663
664 switch (version) {
665 case 0:
666 mmc->version = MMC_VERSION_1_2;
667 break;
668 case 1:
669 mmc->version = MMC_VERSION_1_4;
670 break;
671 case 2:
672 mmc->version = MMC_VERSION_2_2;
673 break;
674 case 3:
675 mmc->version = MMC_VERSION_3;
676 break;
677 case 4:
678 mmc->version = MMC_VERSION_4;
679 break;
680 default:
681 mmc->version = MMC_VERSION_1_2;
682 break;
683 }
684 }
685
686 /* divide frequency by 10, since the mults are 10x bigger */
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530687 freq = fbase[(cmd.response[0] & 0x7)];
688 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
Andy Fleming272cc702008-10-30 16:41:01 -0500689
690 mmc->tran_speed = freq * mult;
691
Rabin Vincent998be3d2009-04-05 13:30:56 +0530692 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500693
694 if (IS_SD(mmc))
695 mmc->write_bl_len = mmc->read_bl_len;
696 else
Rabin Vincent998be3d2009-04-05 13:30:56 +0530697 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500698
699 if (mmc->high_capacity) {
700 csize = (mmc->csd[1] & 0x3f) << 16
701 | (mmc->csd[2] & 0xffff0000) >> 16;
702 cmult = 8;
703 } else {
704 csize = (mmc->csd[1] & 0x3ff) << 2
705 | (mmc->csd[2] & 0xc0000000) >> 30;
706 cmult = (mmc->csd[2] & 0x00038000) >> 15;
707 }
708
709 mmc->capacity = (csize + 1) << (cmult + 2);
710 mmc->capacity *= mmc->read_bl_len;
711
712 if (mmc->read_bl_len > 512)
713 mmc->read_bl_len = 512;
714
715 if (mmc->write_bl_len > 512)
716 mmc->write_bl_len = 512;
717
718 /* Select the card, and put it into Transfer Mode */
719 cmd.cmdidx = MMC_CMD_SELECT_CARD;
720 cmd.resp_type = MMC_RSP_R1b;
721 cmd.cmdarg = mmc->rca << 16;
722 cmd.flags = 0;
723 err = mmc_send_cmd(mmc, &cmd, NULL);
724
725 if (err)
726 return err;
727
728 if (IS_SD(mmc))
729 err = sd_change_freq(mmc);
730 else
731 err = mmc_change_freq(mmc);
732
733 if (err)
734 return err;
735
736 /* Restrict card's capabilities by what the host can do */
737 mmc->card_caps &= mmc->host_caps;
738
739 if (IS_SD(mmc)) {
740 if (mmc->card_caps & MMC_MODE_4BIT) {
741 cmd.cmdidx = MMC_CMD_APP_CMD;
742 cmd.resp_type = MMC_RSP_R1;
743 cmd.cmdarg = mmc->rca << 16;
744 cmd.flags = 0;
745
746 err = mmc_send_cmd(mmc, &cmd, NULL);
747 if (err)
748 return err;
749
750 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
751 cmd.resp_type = MMC_RSP_R1;
752 cmd.cmdarg = 2;
753 cmd.flags = 0;
754 err = mmc_send_cmd(mmc, &cmd, NULL);
755 if (err)
756 return err;
757
758 mmc_set_bus_width(mmc, 4);
759 }
760
761 if (mmc->card_caps & MMC_MODE_HS)
762 mmc_set_clock(mmc, 50000000);
763 else
764 mmc_set_clock(mmc, 25000000);
765 } else {
766 if (mmc->card_caps & MMC_MODE_4BIT) {
767 /* Set the card to use 4 bit*/
768 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
769 EXT_CSD_BUS_WIDTH,
770 EXT_CSD_BUS_WIDTH_4);
771
772 if (err)
773 return err;
774
775 mmc_set_bus_width(mmc, 4);
776 } else if (mmc->card_caps & MMC_MODE_8BIT) {
777 /* Set the card to use 8 bit*/
778 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
779 EXT_CSD_BUS_WIDTH,
780 EXT_CSD_BUS_WIDTH_8);
781
782 if (err)
783 return err;
784
785 mmc_set_bus_width(mmc, 8);
786 }
787
788 if (mmc->card_caps & MMC_MODE_HS) {
789 if (mmc->card_caps & MMC_MODE_HS_52MHz)
790 mmc_set_clock(mmc, 52000000);
791 else
792 mmc_set_clock(mmc, 26000000);
793 } else
794 mmc_set_clock(mmc, 20000000);
795 }
796
797 /* fill in device description */
798 mmc->block_dev.lun = 0;
799 mmc->block_dev.type = 0;
800 mmc->block_dev.blksz = mmc->read_bl_len;
Rabin Vincent9b1f9422009-04-05 13:30:54 +0530801 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
Rabin Vincent0b453ff2009-04-05 13:30:55 +0530802 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
803 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
804 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
805 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
806 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
807 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
808 (mmc->cid[2] >> 24) & 0xf);
Andy Fleming272cc702008-10-30 16:41:01 -0500809 init_part(&mmc->block_dev);
810
811 return 0;
812}
813
814int mmc_send_if_cond(struct mmc *mmc)
815{
816 struct mmc_cmd cmd;
817 int err;
818
819 cmd.cmdidx = SD_CMD_SEND_IF_COND;
820 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
821 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
822 cmd.resp_type = MMC_RSP_R7;
823 cmd.flags = 0;
824
825 err = mmc_send_cmd(mmc, &cmd, NULL);
826
827 if (err)
828 return err;
829
Rabin Vincent998be3d2009-04-05 13:30:56 +0530830 if ((cmd.response[0] & 0xff) != 0xaa)
Andy Fleming272cc702008-10-30 16:41:01 -0500831 return UNUSABLE_ERR;
832 else
833 mmc->version = SD_VERSION_2;
834
835 return 0;
836}
837
838int mmc_register(struct mmc *mmc)
839{
840 /* Setup the universal parts of the block interface just once */
841 mmc->block_dev.if_type = IF_TYPE_MMC;
842 mmc->block_dev.dev = cur_dev_num++;
843 mmc->block_dev.removable = 1;
844 mmc->block_dev.block_read = mmc_bread;
845 mmc->block_dev.block_write = mmc_bwrite;
846
847 INIT_LIST_HEAD (&mmc->link);
848
849 list_add_tail (&mmc->link, &mmc_devices);
850
851 return 0;
852}
853
854block_dev_desc_t *mmc_get_dev(int dev)
855{
856 struct mmc *mmc = find_mmc_device(dev);
857
Rabin Vincente85649c2009-04-05 13:30:53 +0530858 return mmc ? &mmc->block_dev : NULL;
Andy Fleming272cc702008-10-30 16:41:01 -0500859}
860
861int mmc_init(struct mmc *mmc)
862{
863 int err;
864
865 err = mmc->init(mmc);
866
867 if (err)
868 return err;
869
Ilya Yanokb86b85e2009-06-29 17:53:16 +0400870 mmc_set_bus_width(mmc, 1);
871 mmc_set_clock(mmc, 1);
872
Andy Fleming272cc702008-10-30 16:41:01 -0500873 /* Reset the Card */
874 err = mmc_go_idle(mmc);
875
876 if (err)
877 return err;
878
879 /* Test for SD version 2 */
880 err = mmc_send_if_cond(mmc);
881
Andy Fleming272cc702008-10-30 16:41:01 -0500882 /* Now try to get the SD card's operating condition */
883 err = sd_send_op_cond(mmc);
884
885 /* If the command timed out, we check for an MMC card */
886 if (err == TIMEOUT) {
887 err = mmc_send_op_cond(mmc);
888
889 if (err) {
890 printf("Card did not respond to voltage select!\n");
891 return UNUSABLE_ERR;
892 }
893 }
894
895 return mmc_startup(mmc);
896}
897
898/*
899 * CPU and board-specific MMC initializations. Aliased function
900 * signals caller to move on
901 */
902static int __def_mmc_init(bd_t *bis)
903{
904 return -1;
905}
906
Peter Tyserf9a109b2009-04-20 11:08:46 -0500907int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
908int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
Andy Fleming272cc702008-10-30 16:41:01 -0500909
910void print_mmc_devices(char separator)
911{
912 struct mmc *m;
913 struct list_head *entry;
914
915 list_for_each(entry, &mmc_devices) {
916 m = list_entry(entry, struct mmc, link);
917
918 printf("%s: %d", m->name, m->block_dev.dev);
919
920 if (entry->next != &mmc_devices)
921 printf("%c ", separator);
922 }
923
924 printf("\n");
925}
926
927int mmc_initialize(bd_t *bis)
928{
929 INIT_LIST_HEAD (&mmc_devices);
930 cur_dev_num = 0;
931
932 if (board_mmc_init(bis) < 0)
933 cpu_mmc_init(bis);
934
935 print_mmc_devices(',');
936
937 return 0;
938}