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