]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/mmc/mmc.c
MMC: add marvell sdhci driver
[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>
9b1f942c 33#include <div64.h>
272cc70b 34
ce0fbcd2
MW
35/* 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
272cc70b
AF
40static struct list_head mmc_devices;
41static int cur_dev_num = -1;
42
11fdade2
SB
43int __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
272cc70b
AF
50int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
51{
5db2fe3a
RR
52#ifdef CONFIG_MMC_TRACE
53 int ret;
54 int i;
55 u8 *ptr;
56
57 printf("CMD_SEND:%d\n", cmd->cmdidx);
58 printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
59 printf("\t\tFLAG\t\t\t %d\n", cmd->flags);
60 ret = mmc->send_cmd(mmc, cmd, data);
61 switch (cmd->resp_type) {
62 case MMC_RSP_NONE:
63 printf("\t\tMMC_RSP_NONE\n");
64 break;
65 case MMC_RSP_R1:
66 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
67 cmd->response[0]);
68 break;
69 case MMC_RSP_R1b:
70 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
71 cmd->response[0]);
72 break;
73 case MMC_RSP_R2:
74 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
75 cmd->response[0]);
76 printf("\t\t \t\t 0x%08X \n",
77 cmd->response[1]);
78 printf("\t\t \t\t 0x%08X \n",
79 cmd->response[2]);
80 printf("\t\t \t\t 0x%08X \n",
81 cmd->response[3]);
82 printf("\n");
83 printf("\t\t\t\t\tDUMPING DATA\n");
84 for (i = 0; i < 4; i++) {
85 int j;
86 printf("\t\t\t\t\t%03d - ", i*4);
87 ptr = &cmd->response[i];
88 ptr += 3;
89 for (j = 0; j < 4; j++)
90 printf("%02X ", *ptr--);
91 printf("\n");
92 }
93 break;
94 case MMC_RSP_R3:
95 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
96 cmd->response[0]);
97 break;
98 default:
99 printf("\t\tERROR MMC rsp not supported\n");
100 break;
101 }
102 return ret;
103#else
272cc70b 104 return mmc->send_cmd(mmc, cmd, data);
5db2fe3a 105#endif
272cc70b
AF
106}
107
5d4fc8d9
RR
108int mmc_send_status(struct mmc *mmc, int timeout)
109{
110 struct mmc_cmd cmd;
111 int err;
112#ifdef CONFIG_MMC_TRACE
113 int status;
114#endif
115
116 cmd.cmdidx = MMC_CMD_SEND_STATUS;
117 cmd.resp_type = MMC_RSP_R1;
118 cmd.cmdarg = 0;
119 cmd.flags = 0;
120
121 do {
122 err = mmc_send_cmd(mmc, &cmd, NULL);
123 if (err)
124 return err;
125 else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
126 break;
127
128 udelay(1000);
129
130 if (cmd.response[0] & MMC_STATUS_MASK) {
131 printf("Status Error: 0x%08X\n", cmd.response[0]);
132 return COMM_ERR;
133 }
134 } while (timeout--);
135
5db2fe3a
RR
136#ifdef CONFIG_MMC_TRACE
137 status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
138 printf("CURR STATE:%d\n", status);
139#endif
5d4fc8d9
RR
140 if (!timeout) {
141 printf("Timeout waiting card ready\n");
142 return TIMEOUT;
143 }
144
145 return 0;
146}
147
272cc70b
AF
148int mmc_set_blocklen(struct mmc *mmc, int len)
149{
150 struct mmc_cmd cmd;
151
152 cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
153 cmd.resp_type = MMC_RSP_R1;
154 cmd.cmdarg = len;
155 cmd.flags = 0;
156
157 return mmc_send_cmd(mmc, &cmd, NULL);
158}
159
160struct mmc *find_mmc_device(int dev_num)
161{
162 struct mmc *m;
163 struct list_head *entry;
164
165 list_for_each(entry, &mmc_devices) {
166 m = list_entry(entry, struct mmc, link);
167
168 if (m->block_dev.dev == dev_num)
169 return m;
170 }
171
172 printf("MMC Device %d not found\n", dev_num);
173
174 return NULL;
175}
176
e6f99a56
LW
177static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
178{
179 struct mmc_cmd cmd;
180 ulong end;
181 int err, start_cmd, end_cmd;
182
183 if (mmc->high_capacity)
184 end = start + blkcnt - 1;
185 else {
186 end = (start + blkcnt - 1) * mmc->write_bl_len;
187 start *= mmc->write_bl_len;
188 }
189
190 if (IS_SD(mmc)) {
191 start_cmd = SD_CMD_ERASE_WR_BLK_START;
192 end_cmd = SD_CMD_ERASE_WR_BLK_END;
193 } else {
194 start_cmd = MMC_CMD_ERASE_GROUP_START;
195 end_cmd = MMC_CMD_ERASE_GROUP_END;
196 }
197
198 cmd.cmdidx = start_cmd;
199 cmd.cmdarg = start;
200 cmd.resp_type = MMC_RSP_R1;
201 cmd.flags = 0;
202
203 err = mmc_send_cmd(mmc, &cmd, NULL);
204 if (err)
205 goto err_out;
206
207 cmd.cmdidx = end_cmd;
208 cmd.cmdarg = end;
209
210 err = mmc_send_cmd(mmc, &cmd, NULL);
211 if (err)
212 goto err_out;
213
214 cmd.cmdidx = MMC_CMD_ERASE;
215 cmd.cmdarg = SECURE_ERASE;
216 cmd.resp_type = MMC_RSP_R1b;
217
218 err = mmc_send_cmd(mmc, &cmd, NULL);
219 if (err)
220 goto err_out;
221
222 return 0;
223
224err_out:
225 puts("mmc erase failed\n");
226 return err;
227}
228
229static unsigned long
230mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
231{
232 int err = 0;
233 struct mmc *mmc = find_mmc_device(dev_num);
234 lbaint_t blk = 0, blk_r = 0;
235
236 if (!mmc)
237 return -1;
238
239 if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
240 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
241 "The erase range would be change to 0x%lx~0x%lx\n\n",
242 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
243 ((start + blkcnt + mmc->erase_grp_size)
244 & ~(mmc->erase_grp_size - 1)) - 1);
245
246 while (blk < blkcnt) {
247 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
248 mmc->erase_grp_size : (blkcnt - blk);
249 err = mmc_erase_t(mmc, start + blk, blk_r);
250 if (err)
251 break;
252
253 blk += blk_r;
254 }
255
256 return blk;
257}
258
272cc70b 259static ulong
0158126e 260mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
272cc70b
AF
261{
262 struct mmc_cmd cmd;
263 struct mmc_data data;
5d4fc8d9 264 int timeout = 1000;
272cc70b 265
d2bf29e3 266 if ((start + blkcnt) > mmc->block_dev.lba) {
def412b6 267 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
d2bf29e3
LW
268 start + blkcnt, mmc->block_dev.lba);
269 return 0;
270 }
272cc70b
AF
271
272 if (blkcnt > 1)
273 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
274 else
275 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
276
277 if (mmc->high_capacity)
278 cmd.cmdarg = start;
279 else
def412b6 280 cmd.cmdarg = start * mmc->write_bl_len;
272cc70b
AF
281
282 cmd.resp_type = MMC_RSP_R1;
283 cmd.flags = 0;
284
285 data.src = src;
286 data.blocks = blkcnt;
def412b6 287 data.blocksize = mmc->write_bl_len;
272cc70b
AF
288 data.flags = MMC_DATA_WRITE;
289
def412b6
SS
290 if (mmc_send_cmd(mmc, &cmd, &data)) {
291 printf("mmc write failed\n");
292 return 0;
272cc70b
AF
293 }
294
d52ebf10
TC
295 /* SPI multiblock writes terminate using a special
296 * token, not a STOP_TRANSMISSION request.
297 */
298 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
272cc70b
AF
299 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
300 cmd.cmdarg = 0;
301 cmd.resp_type = MMC_RSP_R1b;
302 cmd.flags = 0;
def412b6
SS
303 if (mmc_send_cmd(mmc, &cmd, NULL)) {
304 printf("mmc fail to send stop cmd\n");
305 return 0;
0158126e 306 }
5d4fc8d9
RR
307
308 /* Waiting for the ready status */
309 mmc_send_status(mmc, timeout);
272cc70b
AF
310 }
311
312 return blkcnt;
313}
314
0158126e
LW
315static ulong
316mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
317{
0158126e
LW
318 lbaint_t cur, blocks_todo = blkcnt;
319
def412b6 320 struct mmc *mmc = find_mmc_device(dev_num);
0158126e 321 if (!mmc)
def412b6 322 return 0;
0158126e 323
def412b6
SS
324 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
325 return 0;
0158126e
LW
326
327 do {
8feafcc4 328 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
0158126e 329 if(mmc_write_blocks(mmc, start, cur, src) != cur)
def412b6 330 return 0;
0158126e
LW
331 blocks_todo -= cur;
332 start += cur;
333 src += cur * mmc->write_bl_len;
334 } while (blocks_todo > 0);
335
336 return blkcnt;
337}
338
4a1a06bc 339int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
272cc70b
AF
340{
341 struct mmc_cmd cmd;
342 struct mmc_data data;
5d4fc8d9 343 int timeout = 1000;
272cc70b 344
4a1a06bc
AS
345 if (blkcnt > 1)
346 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
347 else
348 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
272cc70b
AF
349
350 if (mmc->high_capacity)
4a1a06bc 351 cmd.cmdarg = start;
272cc70b 352 else
4a1a06bc 353 cmd.cmdarg = start * mmc->read_bl_len;
272cc70b
AF
354
355 cmd.resp_type = MMC_RSP_R1;
356 cmd.flags = 0;
357
358 data.dest = dst;
4a1a06bc 359 data.blocks = blkcnt;
272cc70b
AF
360 data.blocksize = mmc->read_bl_len;
361 data.flags = MMC_DATA_READ;
362
4a1a06bc
AS
363 if (mmc_send_cmd(mmc, &cmd, &data))
364 return 0;
272cc70b 365
4a1a06bc
AS
366 if (blkcnt > 1) {
367 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
368 cmd.cmdarg = 0;
369 cmd.resp_type = MMC_RSP_R1b;
370 cmd.flags = 0;
371 if (mmc_send_cmd(mmc, &cmd, NULL)) {
372 printf("mmc fail to send stop cmd\n");
373 return 0;
374 }
5d4fc8d9
RR
375
376 /* Waiting for the ready status */
377 mmc_send_status(mmc, timeout);
272cc70b
AF
378 }
379
4a1a06bc 380 return blkcnt;
272cc70b
AF
381}
382
383static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
384{
4a1a06bc
AS
385 lbaint_t cur, blocks_todo = blkcnt;
386
387 if (blkcnt == 0)
388 return 0;
272cc70b 389
4a1a06bc 390 struct mmc *mmc = find_mmc_device(dev_num);
272cc70b
AF
391 if (!mmc)
392 return 0;
393
d2bf29e3 394 if ((start + blkcnt) > mmc->block_dev.lba) {
4a1a06bc 395 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
d2bf29e3
LW
396 start + blkcnt, mmc->block_dev.lba);
397 return 0;
398 }
272cc70b 399
4a1a06bc 400 if (mmc_set_blocklen(mmc, mmc->read_bl_len))
272cc70b 401 return 0;
272cc70b 402
4a1a06bc 403 do {
8feafcc4 404 cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo;
4a1a06bc
AS
405 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
406 return 0;
407 blocks_todo -= cur;
408 start += cur;
409 dst += cur * mmc->read_bl_len;
410 } while (blocks_todo > 0);
272cc70b
AF
411
412 return blkcnt;
413}
414
415int mmc_go_idle(struct mmc* mmc)
416{
417 struct mmc_cmd cmd;
418 int err;
419
420 udelay(1000);
421
422 cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
423 cmd.cmdarg = 0;
424 cmd.resp_type = MMC_RSP_NONE;
425 cmd.flags = 0;
426
427 err = mmc_send_cmd(mmc, &cmd, NULL);
428
429 if (err)
430 return err;
431
432 udelay(2000);
433
434 return 0;
435}
436
437int
438sd_send_op_cond(struct mmc *mmc)
439{
440 int timeout = 1000;
441 int err;
442 struct mmc_cmd cmd;
443
444 do {
445 cmd.cmdidx = MMC_CMD_APP_CMD;
446 cmd.resp_type = MMC_RSP_R1;
447 cmd.cmdarg = 0;
448 cmd.flags = 0;
449
450 err = mmc_send_cmd(mmc, &cmd, NULL);
451
452 if (err)
453 return err;
454
455 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
456 cmd.resp_type = MMC_RSP_R3;
250de12b
SB
457
458 /*
459 * Most cards do not answer if some reserved bits
460 * in the ocr are set. However, Some controller
461 * can set bit 7 (reserved for low voltages), but
462 * how to manage low voltages SD card is not yet
463 * specified.
464 */
d52ebf10
TC
465 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
466 (mmc->voltages & 0xff8000);
272cc70b
AF
467
468 if (mmc->version == SD_VERSION_2)
469 cmd.cmdarg |= OCR_HCS;
470
471 err = mmc_send_cmd(mmc, &cmd, NULL);
472
473 if (err)
474 return err;
475
476 udelay(1000);
477 } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
478
479 if (timeout <= 0)
480 return UNUSABLE_ERR;
481
482 if (mmc->version != SD_VERSION_2)
483 mmc->version = SD_VERSION_1_0;
484
d52ebf10
TC
485 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
486 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
487 cmd.resp_type = MMC_RSP_R3;
488 cmd.cmdarg = 0;
489 cmd.flags = 0;
490
491 err = mmc_send_cmd(mmc, &cmd, NULL);
492
493 if (err)
494 return err;
495 }
496
998be3dd 497 mmc->ocr = cmd.response[0];
272cc70b
AF
498
499 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
500 mmc->rca = 0;
501
502 return 0;
503}
504
505int mmc_send_op_cond(struct mmc *mmc)
506{
31cacbab 507 int timeout = 10000;
272cc70b
AF
508 struct mmc_cmd cmd;
509 int err;
510
511 /* Some cards seem to need this */
512 mmc_go_idle(mmc);
513
31cacbab
RR
514 /* Asking to the card its capabilities */
515 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
516 cmd.resp_type = MMC_RSP_R3;
517 cmd.cmdarg = 0;
518 cmd.flags = 0;
cd6881b5 519
31cacbab 520 err = mmc_send_cmd(mmc, &cmd, NULL);
cd6881b5 521
31cacbab
RR
522 if (err)
523 return err;
cd6881b5 524
31cacbab 525 udelay(1000);
cd6881b5 526
272cc70b
AF
527 do {
528 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
529 cmd.resp_type = MMC_RSP_R3;
31cacbab
RR
530 cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
531 (mmc->voltages &
532 (cmd.response[0] & OCR_VOLTAGE_MASK)) |
533 (cmd.response[0] & OCR_ACCESS_MODE));
272cc70b
AF
534 cmd.flags = 0;
535
536 err = mmc_send_cmd(mmc, &cmd, NULL);
537
538 if (err)
539 return err;
540
541 udelay(1000);
542 } while (!(cmd.response[0] & OCR_BUSY) && timeout--);
543
544 if (timeout <= 0)
545 return UNUSABLE_ERR;
546
d52ebf10
TC
547 if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
548 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
549 cmd.resp_type = MMC_RSP_R3;
550 cmd.cmdarg = 0;
551 cmd.flags = 0;
552
553 err = mmc_send_cmd(mmc, &cmd, NULL);
554
555 if (err)
556 return err;
557 }
558
272cc70b 559 mmc->version = MMC_VERSION_UNKNOWN;
998be3dd 560 mmc->ocr = cmd.response[0];
272cc70b
AF
561
562 mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
563 mmc->rca = 0;
564
565 return 0;
566}
567
568
569int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
570{
571 struct mmc_cmd cmd;
572 struct mmc_data data;
573 int err;
574
575 /* Get the Card Status Register */
576 cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
577 cmd.resp_type = MMC_RSP_R1;
578 cmd.cmdarg = 0;
579 cmd.flags = 0;
580
581 data.dest = ext_csd;
582 data.blocks = 1;
583 data.blocksize = 512;
584 data.flags = MMC_DATA_READ;
585
586 err = mmc_send_cmd(mmc, &cmd, &data);
587
588 return err;
589}
590
591
592int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
593{
594 struct mmc_cmd cmd;
5d4fc8d9
RR
595 int timeout = 1000;
596 int ret;
272cc70b
AF
597
598 cmd.cmdidx = MMC_CMD_SWITCH;
599 cmd.resp_type = MMC_RSP_R1b;
600 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
5d4fc8d9
RR
601 (index << 16) |
602 (value << 8);
272cc70b
AF
603 cmd.flags = 0;
604
5d4fc8d9
RR
605 ret = mmc_send_cmd(mmc, &cmd, NULL);
606
607 /* Waiting for the ready status */
608 mmc_send_status(mmc, timeout);
609
610 return ret;
611
272cc70b
AF
612}
613
614int mmc_change_freq(struct mmc *mmc)
615{
616 char ext_csd[512];
617 char cardtype;
618 int err;
619
620 mmc->card_caps = 0;
621
d52ebf10
TC
622 if (mmc_host_is_spi(mmc))
623 return 0;
624
272cc70b
AF
625 /* Only version 4 supports high-speed */
626 if (mmc->version < MMC_VERSION_4)
627 return 0;
628
629 mmc->card_caps |= MMC_MODE_4BIT;
630
631 err = mmc_send_ext_csd(mmc, ext_csd);
632
633 if (err)
634 return err;
635
272cc70b
AF
636 cardtype = ext_csd[196] & 0xf;
637
638 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
639
640 if (err)
641 return err;
642
643 /* Now check to see that it worked */
644 err = mmc_send_ext_csd(mmc, ext_csd);
645
646 if (err)
647 return err;
648
649 /* No high-speed support */
650 if (!ext_csd[185])
651 return 0;
652
653 /* High Speed is set, there are two types: 52MHz and 26MHz */
654 if (cardtype & MMC_HS_52MHZ)
655 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
656 else
657 mmc->card_caps |= MMC_MODE_HS;
658
659 return 0;
660}
661
bc897b1d
LW
662int mmc_switch_part(int dev_num, unsigned int part_num)
663{
664 struct mmc *mmc = find_mmc_device(dev_num);
665
666 if (!mmc)
667 return -1;
668
669 return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
670 (mmc->part_config & ~PART_ACCESS_MASK)
671 | (part_num & PART_ACCESS_MASK));
672}
673
272cc70b
AF
674int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
675{
676 struct mmc_cmd cmd;
677 struct mmc_data data;
678
679 /* Switch the frequency */
680 cmd.cmdidx = SD_CMD_SWITCH_FUNC;
681 cmd.resp_type = MMC_RSP_R1;
682 cmd.cmdarg = (mode << 31) | 0xffffff;
683 cmd.cmdarg &= ~(0xf << (group * 4));
684 cmd.cmdarg |= value << (group * 4);
685 cmd.flags = 0;
686
687 data.dest = (char *)resp;
688 data.blocksize = 64;
689 data.blocks = 1;
690 data.flags = MMC_DATA_READ;
691
692 return mmc_send_cmd(mmc, &cmd, &data);
693}
694
695
696int sd_change_freq(struct mmc *mmc)
697{
698 int err;
699 struct mmc_cmd cmd;
700 uint scr[2];
701 uint switch_status[16];
702 struct mmc_data data;
703 int timeout;
704
705 mmc->card_caps = 0;
706
d52ebf10
TC
707 if (mmc_host_is_spi(mmc))
708 return 0;
709
272cc70b
AF
710 /* Read the SCR to find out if this card supports higher speeds */
711 cmd.cmdidx = MMC_CMD_APP_CMD;
712 cmd.resp_type = MMC_RSP_R1;
713 cmd.cmdarg = mmc->rca << 16;
714 cmd.flags = 0;
715
716 err = mmc_send_cmd(mmc, &cmd, NULL);
717
718 if (err)
719 return err;
720
721 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
722 cmd.resp_type = MMC_RSP_R1;
723 cmd.cmdarg = 0;
724 cmd.flags = 0;
725
726 timeout = 3;
727
728retry_scr:
729 data.dest = (char *)&scr;
730 data.blocksize = 8;
731 data.blocks = 1;
732 data.flags = MMC_DATA_READ;
733
734 err = mmc_send_cmd(mmc, &cmd, &data);
735
736 if (err) {
737 if (timeout--)
738 goto retry_scr;
739
740 return err;
741 }
742
4e3d89ba
YK
743 mmc->scr[0] = __be32_to_cpu(scr[0]);
744 mmc->scr[1] = __be32_to_cpu(scr[1]);
272cc70b
AF
745
746 switch ((mmc->scr[0] >> 24) & 0xf) {
747 case 0:
748 mmc->version = SD_VERSION_1_0;
749 break;
750 case 1:
751 mmc->version = SD_VERSION_1_10;
752 break;
753 case 2:
754 mmc->version = SD_VERSION_2;
755 break;
756 default:
757 mmc->version = SD_VERSION_1_0;
758 break;
759 }
760
b44c7083
AS
761 if (mmc->scr[0] & SD_DATA_4BIT)
762 mmc->card_caps |= MMC_MODE_4BIT;
763
272cc70b
AF
764 /* Version 1.0 doesn't support switching */
765 if (mmc->version == SD_VERSION_1_0)
766 return 0;
767
768 timeout = 4;
769 while (timeout--) {
770 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
771 (u8 *)&switch_status);
772
773 if (err)
774 return err;
775
776 /* The high-speed function is busy. Try again */
4e3d89ba 777 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
272cc70b
AF
778 break;
779 }
780
272cc70b 781 /* If high-speed isn't supported, we return */
4e3d89ba 782 if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
272cc70b
AF
783 return 0;
784
785 err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
786
787 if (err)
788 return err;
789
4e3d89ba 790 if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
272cc70b
AF
791 mmc->card_caps |= MMC_MODE_HS;
792
793 return 0;
794}
795
796/* frequency bases */
797/* divided by 10 to be nice to platforms without floating point */
5f837c2c 798static const int fbase[] = {
272cc70b
AF
799 10000,
800 100000,
801 1000000,
802 10000000,
803};
804
805/* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
806 * to platforms without floating point.
807 */
5f837c2c 808static const int multipliers[] = {
272cc70b
AF
809 0, /* reserved */
810 10,
811 12,
812 13,
813 15,
814 20,
815 25,
816 30,
817 35,
818 40,
819 45,
820 50,
821 55,
822 60,
823 70,
824 80,
825};
826
827void mmc_set_ios(struct mmc *mmc)
828{
829 mmc->set_ios(mmc);
830}
831
832void mmc_set_clock(struct mmc *mmc, uint clock)
833{
834 if (clock > mmc->f_max)
835 clock = mmc->f_max;
836
837 if (clock < mmc->f_min)
838 clock = mmc->f_min;
839
840 mmc->clock = clock;
841
842 mmc_set_ios(mmc);
843}
844
845void mmc_set_bus_width(struct mmc *mmc, uint width)
846{
847 mmc->bus_width = width;
848
849 mmc_set_ios(mmc);
850}
851
852int mmc_startup(struct mmc *mmc)
853{
854 int err;
855 uint mult, freq;
856 u64 cmult, csize;
857 struct mmc_cmd cmd;
d23e2c09 858 char ext_csd[512];
5d4fc8d9 859 int timeout = 1000;
272cc70b 860
d52ebf10
TC
861#ifdef CONFIG_MMC_SPI_CRC_ON
862 if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
863 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
864 cmd.resp_type = MMC_RSP_R1;
865 cmd.cmdarg = 1;
866 cmd.flags = 0;
867 err = mmc_send_cmd(mmc, &cmd, NULL);
868
869 if (err)
870 return err;
871 }
872#endif
873
272cc70b 874 /* Put the Card in Identify Mode */
d52ebf10
TC
875 cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
876 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
272cc70b
AF
877 cmd.resp_type = MMC_RSP_R2;
878 cmd.cmdarg = 0;
879 cmd.flags = 0;
880
881 err = mmc_send_cmd(mmc, &cmd, NULL);
882
883 if (err)
884 return err;
885
886 memcpy(mmc->cid, cmd.response, 16);
887
888 /*
889 * For MMC cards, set the Relative Address.
890 * For SD cards, get the Relatvie Address.
891 * This also puts the cards into Standby State
892 */
d52ebf10
TC
893 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
894 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
895 cmd.cmdarg = mmc->rca << 16;
896 cmd.resp_type = MMC_RSP_R6;
897 cmd.flags = 0;
272cc70b 898
d52ebf10 899 err = mmc_send_cmd(mmc, &cmd, NULL);
272cc70b 900
d52ebf10
TC
901 if (err)
902 return err;
272cc70b 903
d52ebf10
TC
904 if (IS_SD(mmc))
905 mmc->rca = (cmd.response[0] >> 16) & 0xffff;
906 }
272cc70b
AF
907
908 /* Get the Card-Specific Data */
909 cmd.cmdidx = MMC_CMD_SEND_CSD;
910 cmd.resp_type = MMC_RSP_R2;
911 cmd.cmdarg = mmc->rca << 16;
912 cmd.flags = 0;
913
914 err = mmc_send_cmd(mmc, &cmd, NULL);
915
5d4fc8d9
RR
916 /* Waiting for the ready status */
917 mmc_send_status(mmc, timeout);
918
272cc70b
AF
919 if (err)
920 return err;
921
998be3dd
RV
922 mmc->csd[0] = cmd.response[0];
923 mmc->csd[1] = cmd.response[1];
924 mmc->csd[2] = cmd.response[2];
925 mmc->csd[3] = cmd.response[3];
272cc70b
AF
926
927 if (mmc->version == MMC_VERSION_UNKNOWN) {
0b453ffe 928 int version = (cmd.response[0] >> 26) & 0xf;
272cc70b
AF
929
930 switch (version) {
931 case 0:
932 mmc->version = MMC_VERSION_1_2;
933 break;
934 case 1:
935 mmc->version = MMC_VERSION_1_4;
936 break;
937 case 2:
938 mmc->version = MMC_VERSION_2_2;
939 break;
940 case 3:
941 mmc->version = MMC_VERSION_3;
942 break;
943 case 4:
944 mmc->version = MMC_VERSION_4;
945 break;
946 default:
947 mmc->version = MMC_VERSION_1_2;
948 break;
949 }
950 }
951
952 /* divide frequency by 10, since the mults are 10x bigger */
0b453ffe
RV
953 freq = fbase[(cmd.response[0] & 0x7)];
954 mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
272cc70b
AF
955
956 mmc->tran_speed = freq * mult;
957
998be3dd 958 mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
272cc70b
AF
959
960 if (IS_SD(mmc))
961 mmc->write_bl_len = mmc->read_bl_len;
962 else
998be3dd 963 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
272cc70b
AF
964
965 if (mmc->high_capacity) {
966 csize = (mmc->csd[1] & 0x3f) << 16
967 | (mmc->csd[2] & 0xffff0000) >> 16;
968 cmult = 8;
969 } else {
970 csize = (mmc->csd[1] & 0x3ff) << 2
971 | (mmc->csd[2] & 0xc0000000) >> 30;
972 cmult = (mmc->csd[2] & 0x00038000) >> 15;
973 }
974
975 mmc->capacity = (csize + 1) << (cmult + 2);
976 mmc->capacity *= mmc->read_bl_len;
977
978 if (mmc->read_bl_len > 512)
979 mmc->read_bl_len = 512;
980
981 if (mmc->write_bl_len > 512)
982 mmc->write_bl_len = 512;
983
984 /* Select the card, and put it into Transfer Mode */
d52ebf10
TC
985 if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
986 cmd.cmdidx = MMC_CMD_SELECT_CARD;
987 cmd.resp_type = MMC_RSP_R1b;
988 cmd.cmdarg = mmc->rca << 16;
989 cmd.flags = 0;
990 err = mmc_send_cmd(mmc, &cmd, NULL);
272cc70b 991
d52ebf10
TC
992 if (err)
993 return err;
994 }
272cc70b 995
e6f99a56
LW
996 /*
997 * For SD, its erase group is always one sector
998 */
999 mmc->erase_grp_size = 1;
bc897b1d 1000 mmc->part_config = MMCPART_NOAVAILABLE;
d23e2c09
SG
1001 if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1002 /* check ext_csd version and capacity */
1003 err = mmc_send_ext_csd(mmc, ext_csd);
1004 if (!err & (ext_csd[192] >= 2)) {
1005 mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
1006 ext_csd[214] << 16 | ext_csd[215] << 24;
1007 mmc->capacity *= 512;
1008 }
bc897b1d 1009
e6f99a56
LW
1010 /*
1011 * Check whether GROUP_DEF is set, if yes, read out
1012 * group size from ext_csd directly, or calculate
1013 * the group size from the csd value.
1014 */
1015 if (ext_csd[175])
1016 mmc->erase_grp_size = ext_csd[224] * 512 * 1024;
1017 else {
1018 int erase_gsz, erase_gmul;
1019 erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1020 erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1021 mmc->erase_grp_size = (erase_gsz + 1)
1022 * (erase_gmul + 1);
1023 }
1024
bc897b1d
LW
1025 /* store the partition info of emmc */
1026 if (ext_csd[160] & PART_SUPPORT)
1027 mmc->part_config = ext_csd[179];
d23e2c09
SG
1028 }
1029
272cc70b
AF
1030 if (IS_SD(mmc))
1031 err = sd_change_freq(mmc);
1032 else
1033 err = mmc_change_freq(mmc);
1034
1035 if (err)
1036 return err;
1037
1038 /* Restrict card's capabilities by what the host can do */
1039 mmc->card_caps &= mmc->host_caps;
1040
1041 if (IS_SD(mmc)) {
1042 if (mmc->card_caps & MMC_MODE_4BIT) {
1043 cmd.cmdidx = MMC_CMD_APP_CMD;
1044 cmd.resp_type = MMC_RSP_R1;
1045 cmd.cmdarg = mmc->rca << 16;
1046 cmd.flags = 0;
1047
1048 err = mmc_send_cmd(mmc, &cmd, NULL);
1049 if (err)
1050 return err;
1051
1052 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1053 cmd.resp_type = MMC_RSP_R1;
1054 cmd.cmdarg = 2;
1055 cmd.flags = 0;
1056 err = mmc_send_cmd(mmc, &cmd, NULL);
1057 if (err)
1058 return err;
1059
1060 mmc_set_bus_width(mmc, 4);
1061 }
1062
1063 if (mmc->card_caps & MMC_MODE_HS)
1064 mmc_set_clock(mmc, 50000000);
1065 else
1066 mmc_set_clock(mmc, 25000000);
1067 } else {
1068 if (mmc->card_caps & MMC_MODE_4BIT) {
1069 /* Set the card to use 4 bit*/
1070 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1071 EXT_CSD_BUS_WIDTH,
1072 EXT_CSD_BUS_WIDTH_4);
1073
1074 if (err)
1075 return err;
1076
1077 mmc_set_bus_width(mmc, 4);
1078 } else if (mmc->card_caps & MMC_MODE_8BIT) {
1079 /* Set the card to use 8 bit*/
1080 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1081 EXT_CSD_BUS_WIDTH,
1082 EXT_CSD_BUS_WIDTH_8);
1083
1084 if (err)
1085 return err;
1086
1087 mmc_set_bus_width(mmc, 8);
1088 }
1089
1090 if (mmc->card_caps & MMC_MODE_HS) {
1091 if (mmc->card_caps & MMC_MODE_HS_52MHz)
1092 mmc_set_clock(mmc, 52000000);
1093 else
1094 mmc_set_clock(mmc, 26000000);
1095 } else
1096 mmc_set_clock(mmc, 20000000);
1097 }
1098
1099 /* fill in device description */
1100 mmc->block_dev.lun = 0;
1101 mmc->block_dev.type = 0;
1102 mmc->block_dev.blksz = mmc->read_bl_len;
9b1f942c 1103 mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
0b453ffe
RV
1104 sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
1105 (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
1106 sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
1107 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1108 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
1109 sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
1110 (mmc->cid[2] >> 24) & 0xf);
272cc70b
AF
1111 init_part(&mmc->block_dev);
1112
1113 return 0;
1114}
1115
1116int mmc_send_if_cond(struct mmc *mmc)
1117{
1118 struct mmc_cmd cmd;
1119 int err;
1120
1121 cmd.cmdidx = SD_CMD_SEND_IF_COND;
1122 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1123 cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1124 cmd.resp_type = MMC_RSP_R7;
1125 cmd.flags = 0;
1126
1127 err = mmc_send_cmd(mmc, &cmd, NULL);
1128
1129 if (err)
1130 return err;
1131
998be3dd 1132 if ((cmd.response[0] & 0xff) != 0xaa)
272cc70b
AF
1133 return UNUSABLE_ERR;
1134 else
1135 mmc->version = SD_VERSION_2;
1136
1137 return 0;
1138}
1139
1140int mmc_register(struct mmc *mmc)
1141{
1142 /* Setup the universal parts of the block interface just once */
1143 mmc->block_dev.if_type = IF_TYPE_MMC;
1144 mmc->block_dev.dev = cur_dev_num++;
1145 mmc->block_dev.removable = 1;
1146 mmc->block_dev.block_read = mmc_bread;
1147 mmc->block_dev.block_write = mmc_bwrite;
e6f99a56 1148 mmc->block_dev.block_erase = mmc_berase;
8feafcc4
JR
1149 if (!mmc->b_max)
1150 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
272cc70b
AF
1151
1152 INIT_LIST_HEAD (&mmc->link);
1153
1154 list_add_tail (&mmc->link, &mmc_devices);
1155
1156 return 0;
1157}
1158
1159block_dev_desc_t *mmc_get_dev(int dev)
1160{
1161 struct mmc *mmc = find_mmc_device(dev);
1162
e85649c7 1163 return mmc ? &mmc->block_dev : NULL;
272cc70b
AF
1164}
1165
1166int mmc_init(struct mmc *mmc)
1167{
1168 int err;
1169
bc897b1d
LW
1170 if (mmc->has_init)
1171 return 0;
1172
272cc70b
AF
1173 err = mmc->init(mmc);
1174
1175 if (err)
1176 return err;
1177
b86b85e2
IY
1178 mmc_set_bus_width(mmc, 1);
1179 mmc_set_clock(mmc, 1);
1180
272cc70b
AF
1181 /* Reset the Card */
1182 err = mmc_go_idle(mmc);
1183
1184 if (err)
1185 return err;
1186
bc897b1d
LW
1187 /* The internal partition reset to user partition(0) at every CMD0*/
1188 mmc->part_num = 0;
1189
272cc70b
AF
1190 /* Test for SD version 2 */
1191 err = mmc_send_if_cond(mmc);
1192
272cc70b
AF
1193 /* Now try to get the SD card's operating condition */
1194 err = sd_send_op_cond(mmc);
1195
1196 /* If the command timed out, we check for an MMC card */
1197 if (err == TIMEOUT) {
1198 err = mmc_send_op_cond(mmc);
1199
1200 if (err) {
1201 printf("Card did not respond to voltage select!\n");
1202 return UNUSABLE_ERR;
1203 }
1204 }
1205
bc897b1d
LW
1206 err = mmc_startup(mmc);
1207 if (err)
1208 mmc->has_init = 0;
1209 else
1210 mmc->has_init = 1;
1211 return err;
272cc70b
AF
1212}
1213
1214/*
1215 * CPU and board-specific MMC initializations. Aliased function
1216 * signals caller to move on
1217 */
1218static int __def_mmc_init(bd_t *bis)
1219{
1220 return -1;
1221}
1222
f9a109b3
PT
1223int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1224int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
272cc70b
AF
1225
1226void print_mmc_devices(char separator)
1227{
1228 struct mmc *m;
1229 struct list_head *entry;
1230
1231 list_for_each(entry, &mmc_devices) {
1232 m = list_entry(entry, struct mmc, link);
1233
1234 printf("%s: %d", m->name, m->block_dev.dev);
1235
1236 if (entry->next != &mmc_devices)
1237 printf("%c ", separator);
1238 }
1239
1240 printf("\n");
1241}
1242
ea6ebe21
LW
1243int get_mmc_num(void)
1244{
1245 return cur_dev_num;
1246}
1247
272cc70b
AF
1248int mmc_initialize(bd_t *bis)
1249{
1250 INIT_LIST_HEAD (&mmc_devices);
1251 cur_dev_num = 0;
1252
1253 if (board_mmc_init(bis) < 0)
1254 cpu_mmc_init(bis);
1255
1256 print_mmc_devices(',');
1257
1258 return 0;
1259}