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