]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mmc/mmc.c
mmc: fix the condition for MMC version 4
[karo-tx-uboot.git] / drivers / mmc / mmc.c
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 <div64.h>
34
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
40 static struct list_head mmc_devices;
41 static int cur_dev_num = -1;
42
43 int __board_mmc_getcd(u8 *cd, struct mmc *mmc) {
44         return -1;
45 }
46
47 int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak,
48         alias("__board_mmc_getcd")));
49
50 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
51 {
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
104         return mmc->send_cmd(mmc, cmd, data);
105 #endif
106 }
107
108 int 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
136 #ifdef CONFIG_MMC_TRACE
137         status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
138         printf("CURR STATE:%d\n", status);
139 #endif
140         if (!timeout) {
141                 printf("Timeout waiting card ready\n");
142                 return TIMEOUT;
143         }
144
145         return 0;
146 }
147
148 int 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
160 struct 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
177 static 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
224 err_out:
225         puts("mmc erase failed\n");
226         return err;
227 }
228
229 static unsigned long
230 mmc_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
259 static ulong
260 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
261 {
262         struct mmc_cmd cmd;
263         struct mmc_data data;
264         int timeout = 1000;
265
266         if ((start + blkcnt) > mmc->block_dev.lba) {
267                 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
268                         start + blkcnt, mmc->block_dev.lba);
269                 return 0;
270         }
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
280                 cmd.cmdarg = start * mmc->write_bl_len;
281
282         cmd.resp_type = MMC_RSP_R1;
283         cmd.flags = 0;
284
285         data.src = src;
286         data.blocks = blkcnt;
287         data.blocksize = mmc->write_bl_len;
288         data.flags = MMC_DATA_WRITE;
289
290         if (mmc_send_cmd(mmc, &cmd, &data)) {
291                 printf("mmc write failed\n");
292                 return 0;
293         }
294
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) {
299                 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
300                 cmd.cmdarg = 0;
301                 cmd.resp_type = MMC_RSP_R1b;
302                 cmd.flags = 0;
303                 if (mmc_send_cmd(mmc, &cmd, NULL)) {
304                         printf("mmc fail to send stop cmd\n");
305                         return 0;
306                 }
307
308                 /* Waiting for the ready status */
309                 mmc_send_status(mmc, timeout);
310         }
311
312         return blkcnt;
313 }
314
315 static ulong
316 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
317 {
318         lbaint_t cur, blocks_todo = blkcnt;
319
320         struct mmc *mmc = find_mmc_device(dev_num);
321         if (!mmc)
322                 return 0;
323
324         if (mmc_set_blocklen(mmc, mmc->write_bl_len))
325                 return 0;
326
327         do {
328                 cur = (blocks_todo > mmc->b_max) ?  mmc->b_max : blocks_todo;
329                 if(mmc_write_blocks(mmc, start, cur, src) != cur)
330                         return 0;
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
339 int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
340 {
341         struct mmc_cmd cmd;
342         struct mmc_data data;
343         int timeout = 1000;
344
345         if (blkcnt > 1)
346                 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
347         else
348                 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
349
350         if (mmc->high_capacity)
351                 cmd.cmdarg = start;
352         else
353                 cmd.cmdarg = start * mmc->read_bl_len;
354
355         cmd.resp_type = MMC_RSP_R1;
356         cmd.flags = 0;
357
358         data.dest = dst;
359         data.blocks = blkcnt;
360         data.blocksize = mmc->read_bl_len;
361         data.flags = MMC_DATA_READ;
362
363         if (mmc_send_cmd(mmc, &cmd, &data))
364                 return 0;
365
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                 }
375
376                 /* Waiting for the ready status */
377                 mmc_send_status(mmc, timeout);
378         }
379
380         return blkcnt;
381 }
382
383 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
384 {
385         lbaint_t cur, blocks_todo = blkcnt;
386
387         if (blkcnt == 0)
388                 return 0;
389
390         struct mmc *mmc = find_mmc_device(dev_num);
391         if (!mmc)
392                 return 0;
393
394         if ((start + blkcnt) > mmc->block_dev.lba) {
395                 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
396                         start + blkcnt, mmc->block_dev.lba);
397                 return 0;
398         }
399
400         if (mmc_set_blocklen(mmc, mmc->read_bl_len))
401                 return 0;
402
403         do {
404                 cur = (blocks_todo > mmc->b_max) ?  mmc->b_max : blocks_todo;
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);
411
412         return blkcnt;
413 }
414
415 int 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
437 int
438 sd_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;
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                  */
465                 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
466                         (mmc->voltages & 0xff8000);
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
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
497         mmc->ocr = cmd.response[0];
498
499         mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
500         mmc->rca = 0;
501
502         return 0;
503 }
504
505 int mmc_send_op_cond(struct mmc *mmc)
506 {
507         int timeout = 10000;
508         struct mmc_cmd cmd;
509         int err;
510
511         /* Some cards seem to need this */
512         mmc_go_idle(mmc);
513
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;
519
520         err = mmc_send_cmd(mmc, &cmd, NULL);
521
522         if (err)
523                 return err;
524
525         udelay(1000);
526
527         do {
528                 cmd.cmdidx = MMC_CMD_SEND_OP_COND;
529                 cmd.resp_type = MMC_RSP_R3;
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));
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
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
559         mmc->version = MMC_VERSION_UNKNOWN;
560         mmc->ocr = cmd.response[0];
561
562         mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
563         mmc->rca = 0;
564
565         return 0;
566 }
567
568
569 int 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
592 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
593 {
594         struct mmc_cmd cmd;
595         int timeout = 1000;
596         int ret;
597
598         cmd.cmdidx = MMC_CMD_SWITCH;
599         cmd.resp_type = MMC_RSP_R1b;
600         cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
601                                  (index << 16) |
602                                  (value << 8);
603         cmd.flags = 0;
604
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
612 }
613
614 int 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
622         if (mmc_host_is_spi(mmc))
623                 return 0;
624
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
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
662 int 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
674 int 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
696 int 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
707         if (mmc_host_is_spi(mmc))
708                 return 0;
709
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
728 retry_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
743         mmc->scr[0] = __be32_to_cpu(scr[0]);
744         mmc->scr[1] = __be32_to_cpu(scr[1]);
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
761         if (mmc->scr[0] & SD_DATA_4BIT)
762                 mmc->card_caps |= MMC_MODE_4BIT;
763
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 */
777                 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
778                         break;
779         }
780
781         /* If high-speed isn't supported, we return */
782         if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
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
790         if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
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 */
798 static const int fbase[] = {
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  */
808 static const int multipliers[] = {
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
827 void mmc_set_ios(struct mmc *mmc)
828 {
829         mmc->set_ios(mmc);
830 }
831
832 void 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
845 void mmc_set_bus_width(struct mmc *mmc, uint width)
846 {
847         mmc->bus_width = width;
848
849         mmc_set_ios(mmc);
850 }
851
852 int mmc_startup(struct mmc *mmc)
853 {
854         int err;
855         uint mult, freq;
856         u64 cmult, csize, capacity;
857         struct mmc_cmd cmd;
858         char ext_csd[512];
859         int timeout = 1000;
860
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
874         /* Put the Card in Identify Mode */
875         cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
876                 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
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          */
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;
898
899                 err = mmc_send_cmd(mmc, &cmd, NULL);
900
901                 if (err)
902                         return err;
903
904                 if (IS_SD(mmc))
905                         mmc->rca = (cmd.response[0] >> 16) & 0xffff;
906         }
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
916         /* Waiting for the ready status */
917         mmc_send_status(mmc, timeout);
918
919         if (err)
920                 return err;
921
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];
926
927         if (mmc->version == MMC_VERSION_UNKNOWN) {
928                 int version = (cmd.response[0] >> 26) & 0xf;
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 */
953         freq = fbase[(cmd.response[0] & 0x7)];
954         mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
955
956         mmc->tran_speed = freq * mult;
957
958         mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
959
960         if (IS_SD(mmc))
961                 mmc->write_bl_len = mmc->read_bl_len;
962         else
963                 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
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 */
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);
991
992                 if (err)
993                         return err;
994         }
995
996         /*
997          * For SD, its erase group is always one sector
998          */
999         mmc->erase_grp_size = 1;
1000         mmc->part_config = MMCPART_NOAVAILABLE;
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                         /*
1006                          * According to the JEDEC Standard, the value of
1007                          * ext_csd's capacity is valid if the value is more
1008                          * than 2GB
1009                          */
1010                         capacity = ext_csd[212] << 0 | ext_csd[213] << 8 |
1011                                    ext_csd[214] << 16 | ext_csd[215] << 24;
1012                         capacity *= 512;
1013                         if (capacity > 2 * 1024 * 1024 * 1024)
1014                                 mmc->capacity = capacity;
1015                 }
1016
1017                 /*
1018                  * Check whether GROUP_DEF is set, if yes, read out
1019                  * group size from ext_csd directly, or calculate
1020                  * the group size from the csd value.
1021                  */
1022                 if (ext_csd[175])
1023                         mmc->erase_grp_size = ext_csd[224] * 512 * 1024;
1024                 else {
1025                         int erase_gsz, erase_gmul;
1026                         erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1027                         erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1028                         mmc->erase_grp_size = (erase_gsz + 1)
1029                                 * (erase_gmul + 1);
1030                 }
1031
1032                 /* store the partition info of emmc */
1033                 if (ext_csd[160] & PART_SUPPORT)
1034                         mmc->part_config = ext_csd[179];
1035         }
1036
1037         if (IS_SD(mmc))
1038                 err = sd_change_freq(mmc);
1039         else
1040                 err = mmc_change_freq(mmc);
1041
1042         if (err)
1043                 return err;
1044
1045         /* Restrict card's capabilities by what the host can do */
1046         mmc->card_caps &= mmc->host_caps;
1047
1048         if (IS_SD(mmc)) {
1049                 if (mmc->card_caps & MMC_MODE_4BIT) {
1050                         cmd.cmdidx = MMC_CMD_APP_CMD;
1051                         cmd.resp_type = MMC_RSP_R1;
1052                         cmd.cmdarg = mmc->rca << 16;
1053                         cmd.flags = 0;
1054
1055                         err = mmc_send_cmd(mmc, &cmd, NULL);
1056                         if (err)
1057                                 return err;
1058
1059                         cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1060                         cmd.resp_type = MMC_RSP_R1;
1061                         cmd.cmdarg = 2;
1062                         cmd.flags = 0;
1063                         err = mmc_send_cmd(mmc, &cmd, NULL);
1064                         if (err)
1065                                 return err;
1066
1067                         mmc_set_bus_width(mmc, 4);
1068                 }
1069
1070                 if (mmc->card_caps & MMC_MODE_HS)
1071                         mmc_set_clock(mmc, 50000000);
1072                 else
1073                         mmc_set_clock(mmc, 25000000);
1074         } else {
1075                 if (mmc->card_caps & MMC_MODE_4BIT) {
1076                         /* Set the card to use 4 bit*/
1077                         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1078                                         EXT_CSD_BUS_WIDTH,
1079                                         EXT_CSD_BUS_WIDTH_4);
1080
1081                         if (err)
1082                                 return err;
1083
1084                         mmc_set_bus_width(mmc, 4);
1085                 } else if (mmc->card_caps & MMC_MODE_8BIT) {
1086                         /* Set the card to use 8 bit*/
1087                         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1088                                         EXT_CSD_BUS_WIDTH,
1089                                         EXT_CSD_BUS_WIDTH_8);
1090
1091                         if (err)
1092                                 return err;
1093
1094                         mmc_set_bus_width(mmc, 8);
1095                 }
1096
1097                 if (mmc->card_caps & MMC_MODE_HS) {
1098                         if (mmc->card_caps & MMC_MODE_HS_52MHz)
1099                                 mmc_set_clock(mmc, 52000000);
1100                         else
1101                                 mmc_set_clock(mmc, 26000000);
1102                 } else
1103                         mmc_set_clock(mmc, 20000000);
1104         }
1105
1106         /* fill in device description */
1107         mmc->block_dev.lun = 0;
1108         mmc->block_dev.type = 0;
1109         mmc->block_dev.blksz = mmc->read_bl_len;
1110         mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1111         sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
1112                         (mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
1113         sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
1114                         (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1115                         (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
1116         sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
1117                         (mmc->cid[2] >> 24) & 0xf);
1118         init_part(&mmc->block_dev);
1119
1120         return 0;
1121 }
1122
1123 int mmc_send_if_cond(struct mmc *mmc)
1124 {
1125         struct mmc_cmd cmd;
1126         int err;
1127
1128         cmd.cmdidx = SD_CMD_SEND_IF_COND;
1129         /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1130         cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1131         cmd.resp_type = MMC_RSP_R7;
1132         cmd.flags = 0;
1133
1134         err = mmc_send_cmd(mmc, &cmd, NULL);
1135
1136         if (err)
1137                 return err;
1138
1139         if ((cmd.response[0] & 0xff) != 0xaa)
1140                 return UNUSABLE_ERR;
1141         else
1142                 mmc->version = SD_VERSION_2;
1143
1144         return 0;
1145 }
1146
1147 int mmc_register(struct mmc *mmc)
1148 {
1149         /* Setup the universal parts of the block interface just once */
1150         mmc->block_dev.if_type = IF_TYPE_MMC;
1151         mmc->block_dev.dev = cur_dev_num++;
1152         mmc->block_dev.removable = 1;
1153         mmc->block_dev.block_read = mmc_bread;
1154         mmc->block_dev.block_write = mmc_bwrite;
1155         mmc->block_dev.block_erase = mmc_berase;
1156         if (!mmc->b_max)
1157                 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1158
1159         INIT_LIST_HEAD (&mmc->link);
1160
1161         list_add_tail (&mmc->link, &mmc_devices);
1162
1163         return 0;
1164 }
1165
1166 block_dev_desc_t *mmc_get_dev(int dev)
1167 {
1168         struct mmc *mmc = find_mmc_device(dev);
1169
1170         return mmc ? &mmc->block_dev : NULL;
1171 }
1172
1173 int mmc_init(struct mmc *mmc)
1174 {
1175         int err;
1176
1177         if (mmc->has_init)
1178                 return 0;
1179
1180         err = mmc->init(mmc);
1181
1182         if (err)
1183                 return err;
1184
1185         mmc_set_bus_width(mmc, 1);
1186         mmc_set_clock(mmc, 1);
1187
1188         /* Reset the Card */
1189         err = mmc_go_idle(mmc);
1190
1191         if (err)
1192                 return err;
1193
1194         /* The internal partition reset to user partition(0) at every CMD0*/
1195         mmc->part_num = 0;
1196
1197         /* Test for SD version 2 */
1198         err = mmc_send_if_cond(mmc);
1199
1200         /* Now try to get the SD card's operating condition */
1201         err = sd_send_op_cond(mmc);
1202
1203         /* If the command timed out, we check for an MMC card */
1204         if (err == TIMEOUT) {
1205                 err = mmc_send_op_cond(mmc);
1206
1207                 if (err) {
1208                         printf("Card did not respond to voltage select!\n");
1209                         return UNUSABLE_ERR;
1210                 }
1211         }
1212
1213         err = mmc_startup(mmc);
1214         if (err)
1215                 mmc->has_init = 0;
1216         else
1217                 mmc->has_init = 1;
1218         return err;
1219 }
1220
1221 /*
1222  * CPU and board-specific MMC initializations.  Aliased function
1223  * signals caller to move on
1224  */
1225 static int __def_mmc_init(bd_t *bis)
1226 {
1227         return -1;
1228 }
1229
1230 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1231 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1232
1233 void print_mmc_devices(char separator)
1234 {
1235         struct mmc *m;
1236         struct list_head *entry;
1237
1238         list_for_each(entry, &mmc_devices) {
1239                 m = list_entry(entry, struct mmc, link);
1240
1241                 printf("%s: %d", m->name, m->block_dev.dev);
1242
1243                 if (entry->next != &mmc_devices)
1244                         printf("%c ", separator);
1245         }
1246
1247         printf("\n");
1248 }
1249
1250 int get_mmc_num(void)
1251 {
1252         return cur_dev_num;
1253 }
1254
1255 int mmc_initialize(bd_t *bis)
1256 {
1257         INIT_LIST_HEAD (&mmc_devices);
1258         cur_dev_num = 0;
1259
1260         if (board_mmc_init(bis) < 0)
1261                 cpu_mmc_init(bis);
1262
1263         print_mmc_devices(',');
1264
1265         return 0;
1266 }