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