Merge branch 'u-boot-atmel/master' into 'u-boot-arm/master'
[platform/kernel/u-boot.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
489         if (timeout <= 0)
490                 return UNUSABLE_ERR;
491
492         if (mmc->version != SD_VERSION_2)
493                 mmc->version = SD_VERSION_1_0;
494
495         if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
496                 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
497                 cmd.resp_type = MMC_RSP_R3;
498                 cmd.cmdarg = 0;
499
500                 err = mmc_send_cmd(mmc, &cmd, NULL);
501
502                 if (err)
503                         return err;
504         }
505
506         mmc->ocr = cmd.response[0];
507
508         mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
509         mmc->rca = 0;
510
511         return 0;
512 }
513
514 /* We pass in the cmd since otherwise the init seems to fail */
515 static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
516                 int use_arg)
517 {
518         int err;
519
520         cmd->cmdidx = MMC_CMD_SEND_OP_COND;
521         cmd->resp_type = MMC_RSP_R3;
522         cmd->cmdarg = 0;
523         if (use_arg && !mmc_host_is_spi(mmc)) {
524                 cmd->cmdarg =
525                         (mmc->voltages &
526                         (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
527                         (mmc->op_cond_response & OCR_ACCESS_MODE);
528
529                 if (mmc->host_caps & MMC_MODE_HC)
530                         cmd->cmdarg |= OCR_HCS;
531         }
532         err = mmc_send_cmd(mmc, cmd, NULL);
533         if (err)
534                 return err;
535         mmc->op_cond_response = cmd->response[0];
536         return 0;
537 }
538
539 int mmc_send_op_cond(struct mmc *mmc)
540 {
541         struct mmc_cmd cmd;
542         int err, i;
543
544         /* Some cards seem to need this */
545         mmc_go_idle(mmc);
546
547         /* Asking to the card its capabilities */
548         mmc->op_cond_pending = 1;
549         for (i = 0; i < 2; i++) {
550                 err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
551                 if (err)
552                         return err;
553
554                 /* exit if not busy (flag seems to be inverted) */
555                 if (mmc->op_cond_response & OCR_BUSY)
556                         return 0;
557         }
558         return IN_PROGRESS;
559 }
560
561 int mmc_complete_op_cond(struct mmc *mmc)
562 {
563         struct mmc_cmd cmd;
564         int timeout = 1000;
565         uint start;
566         int err;
567
568         mmc->op_cond_pending = 0;
569         start = get_timer(0);
570         do {
571                 err = mmc_send_op_cond_iter(mmc, &cmd, 1);
572                 if (err)
573                         return err;
574                 if (get_timer(start) > timeout)
575                         return UNUSABLE_ERR;
576                 udelay(100);
577         } while (!(mmc->op_cond_response & OCR_BUSY));
578
579         if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
580                 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
581                 cmd.resp_type = MMC_RSP_R3;
582                 cmd.cmdarg = 0;
583
584                 err = mmc_send_cmd(mmc, &cmd, NULL);
585
586                 if (err)
587                         return err;
588         }
589
590         mmc->version = MMC_VERSION_UNKNOWN;
591         mmc->ocr = cmd.response[0];
592
593         mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
594         mmc->rca = 0;
595
596         return 0;
597 }
598
599
600 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
601 {
602         struct mmc_cmd cmd;
603         struct mmc_data data;
604         int err;
605
606         /* Get the Card Status Register */
607         cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
608         cmd.resp_type = MMC_RSP_R1;
609         cmd.cmdarg = 0;
610
611         data.dest = (char *)ext_csd;
612         data.blocks = 1;
613         data.blocksize = MMC_MAX_BLOCK_LEN;
614         data.flags = MMC_DATA_READ;
615
616         err = mmc_send_cmd(mmc, &cmd, &data);
617
618         return err;
619 }
620
621
622 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
623 {
624         struct mmc_cmd cmd;
625         int timeout = 1000;
626         int ret;
627
628         cmd.cmdidx = MMC_CMD_SWITCH;
629         cmd.resp_type = MMC_RSP_R1b;
630         cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
631                                  (index << 16) |
632                                  (value << 8);
633
634         ret = mmc_send_cmd(mmc, &cmd, NULL);
635
636         /* Waiting for the ready status */
637         if (!ret)
638                 ret = mmc_send_status(mmc, timeout);
639
640         return ret;
641
642 }
643
644 static int mmc_change_freq(struct mmc *mmc)
645 {
646         ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
647         char cardtype;
648         int err;
649
650         mmc->card_caps = 0;
651
652         if (mmc_host_is_spi(mmc))
653                 return 0;
654
655         /* Only version 4 supports high-speed */
656         if (mmc->version < MMC_VERSION_4)
657                 return 0;
658
659         err = mmc_send_ext_csd(mmc, ext_csd);
660
661         if (err)
662                 return err;
663
664         cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
665
666         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
667
668         if (err)
669                 return err;
670
671         /* Now check to see that it worked */
672         err = mmc_send_ext_csd(mmc, ext_csd);
673
674         if (err)
675                 return err;
676
677         /* No high-speed support */
678         if (!ext_csd[EXT_CSD_HS_TIMING])
679                 return 0;
680
681         /* High Speed is set, there are two types: 52MHz and 26MHz */
682         if (cardtype & MMC_HS_52MHZ)
683                 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
684         else
685                 mmc->card_caps |= MMC_MODE_HS;
686
687         return 0;
688 }
689
690 static int mmc_set_capacity(struct mmc *mmc, int part_num)
691 {
692         switch (part_num) {
693         case 0:
694                 mmc->capacity = mmc->capacity_user;
695                 break;
696         case 1:
697         case 2:
698                 mmc->capacity = mmc->capacity_boot;
699                 break;
700         case 3:
701                 mmc->capacity = mmc->capacity_rpmb;
702                 break;
703         case 4:
704         case 5:
705         case 6:
706         case 7:
707                 mmc->capacity = mmc->capacity_gp[part_num - 4];
708                 break;
709         default:
710                 return -1;
711         }
712
713         mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
714
715         return 0;
716 }
717
718 int mmc_switch_part(int dev_num, unsigned int part_num)
719 {
720         struct mmc *mmc = find_mmc_device(dev_num);
721         int ret;
722
723         if (!mmc)
724                 return -1;
725
726         ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
727                          (mmc->part_config & ~PART_ACCESS_MASK)
728                          | (part_num & PART_ACCESS_MASK));
729         if (ret)
730                 return ret;
731
732         return mmc_set_capacity(mmc, part_num);
733 }
734
735 int mmc_getcd(struct mmc *mmc)
736 {
737         int cd;
738
739         cd = board_mmc_getcd(mmc);
740
741         if (cd < 0) {
742                 if (mmc->getcd)
743                         cd = mmc->getcd(mmc);
744                 else
745                         cd = 1;
746         }
747
748         return cd;
749 }
750
751 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
752 {
753         struct mmc_cmd cmd;
754         struct mmc_data data;
755
756         /* Switch the frequency */
757         cmd.cmdidx = SD_CMD_SWITCH_FUNC;
758         cmd.resp_type = MMC_RSP_R1;
759         cmd.cmdarg = (mode << 31) | 0xffffff;
760         cmd.cmdarg &= ~(0xf << (group * 4));
761         cmd.cmdarg |= value << (group * 4);
762
763         data.dest = (char *)resp;
764         data.blocksize = 64;
765         data.blocks = 1;
766         data.flags = MMC_DATA_READ;
767
768         return mmc_send_cmd(mmc, &cmd, &data);
769 }
770
771
772 static int sd_change_freq(struct mmc *mmc)
773 {
774         int err;
775         struct mmc_cmd cmd;
776         ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
777         ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
778         struct mmc_data data;
779         int timeout;
780
781         mmc->card_caps = 0;
782
783         if (mmc_host_is_spi(mmc))
784                 return 0;
785
786         /* Read the SCR to find out if this card supports higher speeds */
787         cmd.cmdidx = MMC_CMD_APP_CMD;
788         cmd.resp_type = MMC_RSP_R1;
789         cmd.cmdarg = mmc->rca << 16;
790
791         err = mmc_send_cmd(mmc, &cmd, NULL);
792
793         if (err)
794                 return err;
795
796         cmd.cmdidx = SD_CMD_APP_SEND_SCR;
797         cmd.resp_type = MMC_RSP_R1;
798         cmd.cmdarg = 0;
799
800         timeout = 3;
801
802 retry_scr:
803         data.dest = (char *)scr;
804         data.blocksize = 8;
805         data.blocks = 1;
806         data.flags = MMC_DATA_READ;
807
808         err = mmc_send_cmd(mmc, &cmd, &data);
809
810         if (err) {
811                 if (timeout--)
812                         goto retry_scr;
813
814                 return err;
815         }
816
817         mmc->scr[0] = __be32_to_cpu(scr[0]);
818         mmc->scr[1] = __be32_to_cpu(scr[1]);
819
820         switch ((mmc->scr[0] >> 24) & 0xf) {
821                 case 0:
822                         mmc->version = SD_VERSION_1_0;
823                         break;
824                 case 1:
825                         mmc->version = SD_VERSION_1_10;
826                         break;
827                 case 2:
828                         mmc->version = SD_VERSION_2;
829                         if ((mmc->scr[0] >> 15) & 0x1)
830                                 mmc->version = SD_VERSION_3;
831                         break;
832                 default:
833                         mmc->version = SD_VERSION_1_0;
834                         break;
835         }
836
837         if (mmc->scr[0] & SD_DATA_4BIT)
838                 mmc->card_caps |= MMC_MODE_4BIT;
839
840         /* Version 1.0 doesn't support switching */
841         if (mmc->version == SD_VERSION_1_0)
842                 return 0;
843
844         timeout = 4;
845         while (timeout--) {
846                 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
847                                 (u8 *)switch_status);
848
849                 if (err)
850                         return err;
851
852                 /* The high-speed function is busy.  Try again */
853                 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
854                         break;
855         }
856
857         /* If high-speed isn't supported, we return */
858         if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
859                 return 0;
860
861         /*
862          * If the host doesn't support SD_HIGHSPEED, do not switch card to
863          * HIGHSPEED mode even if the card support SD_HIGHSPPED.
864          * This can avoid furthur problem when the card runs in different
865          * mode between the host.
866          */
867         if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
868                 (mmc->host_caps & MMC_MODE_HS)))
869                 return 0;
870
871         err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
872
873         if (err)
874                 return err;
875
876         if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
877                 mmc->card_caps |= MMC_MODE_HS;
878
879         return 0;
880 }
881
882 /* frequency bases */
883 /* divided by 10 to be nice to platforms without floating point */
884 static const int fbase[] = {
885         10000,
886         100000,
887         1000000,
888         10000000,
889 };
890
891 /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
892  * to platforms without floating point.
893  */
894 static const int multipliers[] = {
895         0,      /* reserved */
896         10,
897         12,
898         13,
899         15,
900         20,
901         25,
902         30,
903         35,
904         40,
905         45,
906         50,
907         55,
908         60,
909         70,
910         80,
911 };
912
913 static void mmc_set_ios(struct mmc *mmc)
914 {
915         mmc->set_ios(mmc);
916 }
917
918 void mmc_set_clock(struct mmc *mmc, uint clock)
919 {
920         if (clock > mmc->f_max)
921                 clock = mmc->f_max;
922
923         if (clock < mmc->f_min)
924                 clock = mmc->f_min;
925
926         mmc->clock = clock;
927
928         mmc_set_ios(mmc);
929 }
930
931 static void mmc_set_bus_width(struct mmc *mmc, uint width)
932 {
933         mmc->bus_width = width;
934
935         mmc_set_ios(mmc);
936 }
937
938 static int mmc_startup(struct mmc *mmc)
939 {
940         int err, i;
941         uint mult, freq;
942         u64 cmult, csize, capacity;
943         struct mmc_cmd cmd;
944         ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
945         ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
946         int timeout = 1000;
947
948 #ifdef CONFIG_MMC_SPI_CRC_ON
949         if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
950                 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
951                 cmd.resp_type = MMC_RSP_R1;
952                 cmd.cmdarg = 1;
953                 err = mmc_send_cmd(mmc, &cmd, NULL);
954
955                 if (err)
956                         return err;
957         }
958 #endif
959
960         /* Put the Card in Identify Mode */
961         cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
962                 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
963         cmd.resp_type = MMC_RSP_R2;
964         cmd.cmdarg = 0;
965
966         err = mmc_send_cmd(mmc, &cmd, NULL);
967
968         if (err)
969                 return err;
970
971         memcpy(mmc->cid, cmd.response, 16);
972
973         /*
974          * For MMC cards, set the Relative Address.
975          * For SD cards, get the Relatvie Address.
976          * This also puts the cards into Standby State
977          */
978         if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
979                 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
980                 cmd.cmdarg = mmc->rca << 16;
981                 cmd.resp_type = MMC_RSP_R6;
982
983                 err = mmc_send_cmd(mmc, &cmd, NULL);
984
985                 if (err)
986                         return err;
987
988                 if (IS_SD(mmc))
989                         mmc->rca = (cmd.response[0] >> 16) & 0xffff;
990         }
991
992         /* Get the Card-Specific Data */
993         cmd.cmdidx = MMC_CMD_SEND_CSD;
994         cmd.resp_type = MMC_RSP_R2;
995         cmd.cmdarg = mmc->rca << 16;
996
997         err = mmc_send_cmd(mmc, &cmd, NULL);
998
999         /* Waiting for the ready status */
1000         mmc_send_status(mmc, timeout);
1001
1002         if (err)
1003                 return err;
1004
1005         mmc->csd[0] = cmd.response[0];
1006         mmc->csd[1] = cmd.response[1];
1007         mmc->csd[2] = cmd.response[2];
1008         mmc->csd[3] = cmd.response[3];
1009
1010         if (mmc->version == MMC_VERSION_UNKNOWN) {
1011                 int version = (cmd.response[0] >> 26) & 0xf;
1012
1013                 switch (version) {
1014                         case 0:
1015                                 mmc->version = MMC_VERSION_1_2;
1016                                 break;
1017                         case 1:
1018                                 mmc->version = MMC_VERSION_1_4;
1019                                 break;
1020                         case 2:
1021                                 mmc->version = MMC_VERSION_2_2;
1022                                 break;
1023                         case 3:
1024                                 mmc->version = MMC_VERSION_3;
1025                                 break;
1026                         case 4:
1027                                 mmc->version = MMC_VERSION_4;
1028                                 break;
1029                         default:
1030                                 mmc->version = MMC_VERSION_1_2;
1031                                 break;
1032                 }
1033         }
1034
1035         /* divide frequency by 10, since the mults are 10x bigger */
1036         freq = fbase[(cmd.response[0] & 0x7)];
1037         mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
1038
1039         mmc->tran_speed = freq * mult;
1040
1041         mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1042
1043         if (IS_SD(mmc))
1044                 mmc->write_bl_len = mmc->read_bl_len;
1045         else
1046                 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1047
1048         if (mmc->high_capacity) {
1049                 csize = (mmc->csd[1] & 0x3f) << 16
1050                         | (mmc->csd[2] & 0xffff0000) >> 16;
1051                 cmult = 8;
1052         } else {
1053                 csize = (mmc->csd[1] & 0x3ff) << 2
1054                         | (mmc->csd[2] & 0xc0000000) >> 30;
1055                 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1056         }
1057
1058         mmc->capacity_user = (csize + 1) << (cmult + 2);
1059         mmc->capacity_user *= mmc->read_bl_len;
1060         mmc->capacity_boot = 0;
1061         mmc->capacity_rpmb = 0;
1062         for (i = 0; i < 4; i++)
1063                 mmc->capacity_gp[i] = 0;
1064
1065         if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1066                 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1067
1068         if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1069                 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1070
1071         /* Select the card, and put it into Transfer Mode */
1072         if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1073                 cmd.cmdidx = MMC_CMD_SELECT_CARD;
1074                 cmd.resp_type = MMC_RSP_R1;
1075                 cmd.cmdarg = mmc->rca << 16;
1076                 err = mmc_send_cmd(mmc, &cmd, NULL);
1077
1078                 if (err)
1079                         return err;
1080         }
1081
1082         /*
1083          * For SD, its erase group is always one sector
1084          */
1085         mmc->erase_grp_size = 1;
1086         mmc->part_config = MMCPART_NOAVAILABLE;
1087         if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1088                 /* check  ext_csd version and capacity */
1089                 err = mmc_send_ext_csd(mmc, ext_csd);
1090                 if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
1091                         /*
1092                          * According to the JEDEC Standard, the value of
1093                          * ext_csd's capacity is valid if the value is more
1094                          * than 2GB
1095                          */
1096                         capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1097                                         | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1098                                         | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1099                                         | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1100                         capacity *= MMC_MAX_BLOCK_LEN;
1101                         if ((capacity >> 20) > 2 * 1024)
1102                                 mmc->capacity_user = capacity;
1103                 }
1104
1105                 switch (ext_csd[EXT_CSD_REV]) {
1106                 case 1:
1107                         mmc->version = MMC_VERSION_4_1;
1108                         break;
1109                 case 2:
1110                         mmc->version = MMC_VERSION_4_2;
1111                         break;
1112                 case 3:
1113                         mmc->version = MMC_VERSION_4_3;
1114                         break;
1115                 case 5:
1116                         mmc->version = MMC_VERSION_4_41;
1117                         break;
1118                 case 6:
1119                         mmc->version = MMC_VERSION_4_5;
1120                         break;
1121                 }
1122
1123                 /*
1124                  * Check whether GROUP_DEF is set, if yes, read out
1125                  * group size from ext_csd directly, or calculate
1126                  * the group size from the csd value.
1127                  */
1128                 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF]) {
1129                         mmc->erase_grp_size =
1130                                 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
1131                                         MMC_MAX_BLOCK_LEN * 1024;
1132                 } else {
1133                         int erase_gsz, erase_gmul;
1134                         erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1135                         erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1136                         mmc->erase_grp_size = (erase_gsz + 1)
1137                                 * (erase_gmul + 1);
1138                 }
1139
1140                 /* store the partition info of emmc */
1141                 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1142                     ext_csd[EXT_CSD_BOOT_MULT])
1143                         mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1144
1145                 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1146
1147                 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1148
1149                 for (i = 0; i < 4; i++) {
1150                         int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1151                         mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1152                                 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1153                         mmc->capacity_gp[i] *=
1154                                 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1155                         mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1156                 }
1157         }
1158
1159         err = mmc_set_capacity(mmc, mmc->part_num);
1160         if (err)
1161                 return err;
1162
1163         if (IS_SD(mmc))
1164                 err = sd_change_freq(mmc);
1165         else
1166                 err = mmc_change_freq(mmc);
1167
1168         if (err)
1169                 return err;
1170
1171         /* Restrict card's capabilities by what the host can do */
1172         mmc->card_caps &= mmc->host_caps;
1173
1174         if (IS_SD(mmc)) {
1175                 if (mmc->card_caps & MMC_MODE_4BIT) {
1176                         cmd.cmdidx = MMC_CMD_APP_CMD;
1177                         cmd.resp_type = MMC_RSP_R1;
1178                         cmd.cmdarg = mmc->rca << 16;
1179
1180                         err = mmc_send_cmd(mmc, &cmd, NULL);
1181                         if (err)
1182                                 return err;
1183
1184                         cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1185                         cmd.resp_type = MMC_RSP_R1;
1186                         cmd.cmdarg = 2;
1187                         err = mmc_send_cmd(mmc, &cmd, NULL);
1188                         if (err)
1189                                 return err;
1190
1191                         mmc_set_bus_width(mmc, 4);
1192                 }
1193
1194                 if (mmc->card_caps & MMC_MODE_HS)
1195                         mmc->tran_speed = 50000000;
1196                 else
1197                         mmc->tran_speed = 25000000;
1198         } else {
1199                 int idx;
1200
1201                 /* An array of possible bus widths in order of preference */
1202                 static unsigned ext_csd_bits[] = {
1203                         EXT_CSD_BUS_WIDTH_8,
1204                         EXT_CSD_BUS_WIDTH_4,
1205                         EXT_CSD_BUS_WIDTH_1,
1206                 };
1207
1208                 /* An array to map CSD bus widths to host cap bits */
1209                 static unsigned ext_to_hostcaps[] = {
1210                         [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1211                         [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1212                 };
1213
1214                 /* An array to map chosen bus width to an integer */
1215                 static unsigned widths[] = {
1216                         8, 4, 1,
1217                 };
1218
1219                 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1220                         unsigned int extw = ext_csd_bits[idx];
1221
1222                         /*
1223                          * Check to make sure the controller supports
1224                          * this bus width, if it's more than 1
1225                          */
1226                         if (extw != EXT_CSD_BUS_WIDTH_1 &&
1227                                         !(mmc->host_caps & ext_to_hostcaps[extw]))
1228                                 continue;
1229
1230                         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1231                                         EXT_CSD_BUS_WIDTH, extw);
1232
1233                         if (err)
1234                                 continue;
1235
1236                         mmc_set_bus_width(mmc, widths[idx]);
1237
1238                         err = mmc_send_ext_csd(mmc, test_csd);
1239                         if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1240                                     == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1241                                  && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1242                                     == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1243                                  && ext_csd[EXT_CSD_REV] \
1244                                     == test_csd[EXT_CSD_REV]
1245                                  && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1246                                     == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1247                                  && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1248                                         &test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1249
1250                                 mmc->card_caps |= ext_to_hostcaps[extw];
1251                                 break;
1252                         }
1253                 }
1254
1255                 if (mmc->card_caps & MMC_MODE_HS) {
1256                         if (mmc->card_caps & MMC_MODE_HS_52MHz)
1257                                 mmc->tran_speed = 52000000;
1258                         else
1259                                 mmc->tran_speed = 26000000;
1260                 }
1261         }
1262
1263         mmc_set_clock(mmc, mmc->tran_speed);
1264
1265         /* fill in device description */
1266         mmc->block_dev.lun = 0;
1267         mmc->block_dev.type = 0;
1268         mmc->block_dev.blksz = mmc->read_bl_len;
1269         mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
1270         mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1271         sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1272                 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1273                 (mmc->cid[3] >> 16) & 0xffff);
1274         sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1275                 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1276                 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1277                 (mmc->cid[2] >> 24) & 0xff);
1278         sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1279                 (mmc->cid[2] >> 16) & 0xf);
1280 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1281         init_part(&mmc->block_dev);
1282 #endif
1283
1284         return 0;
1285 }
1286
1287 static int mmc_send_if_cond(struct mmc *mmc)
1288 {
1289         struct mmc_cmd cmd;
1290         int err;
1291
1292         cmd.cmdidx = SD_CMD_SEND_IF_COND;
1293         /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1294         cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1295         cmd.resp_type = MMC_RSP_R7;
1296
1297         err = mmc_send_cmd(mmc, &cmd, NULL);
1298
1299         if (err)
1300                 return err;
1301
1302         if ((cmd.response[0] & 0xff) != 0xaa)
1303                 return UNUSABLE_ERR;
1304         else
1305                 mmc->version = SD_VERSION_2;
1306
1307         return 0;
1308 }
1309
1310 int mmc_register(struct mmc *mmc)
1311 {
1312         /* Setup the universal parts of the block interface just once */
1313         mmc->block_dev.if_type = IF_TYPE_MMC;
1314         mmc->block_dev.dev = cur_dev_num++;
1315         mmc->block_dev.removable = 1;
1316         mmc->block_dev.block_read = mmc_bread;
1317         mmc->block_dev.block_write = mmc_bwrite;
1318         mmc->block_dev.block_erase = mmc_berase;
1319         if (!mmc->b_max)
1320                 mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1321
1322         INIT_LIST_HEAD (&mmc->link);
1323
1324         list_add_tail (&mmc->link, &mmc_devices);
1325
1326         return 0;
1327 }
1328
1329 #ifdef CONFIG_PARTITIONS
1330 block_dev_desc_t *mmc_get_dev(int dev)
1331 {
1332         struct mmc *mmc = find_mmc_device(dev);
1333         if (!mmc || mmc_init(mmc))
1334                 return NULL;
1335
1336         return &mmc->block_dev;
1337 }
1338 #endif
1339
1340 int mmc_start_init(struct mmc *mmc)
1341 {
1342         int err;
1343
1344         if (mmc_getcd(mmc) == 0) {
1345                 mmc->has_init = 0;
1346                 printf("MMC: no card present\n");
1347                 return NO_CARD_ERR;
1348         }
1349
1350         if (mmc->has_init)
1351                 return 0;
1352
1353         err = mmc->init(mmc);
1354
1355         if (err)
1356                 return err;
1357
1358         mmc_set_bus_width(mmc, 1);
1359         mmc_set_clock(mmc, 1);
1360
1361         /* Reset the Card */
1362         err = mmc_go_idle(mmc);
1363
1364         if (err)
1365                 return err;
1366
1367         /* The internal partition reset to user partition(0) at every CMD0*/
1368         mmc->part_num = 0;
1369
1370         /* Test for SD version 2 */
1371         err = mmc_send_if_cond(mmc);
1372
1373         /* Now try to get the SD card's operating condition */
1374         err = sd_send_op_cond(mmc);
1375
1376         /* If the command timed out, we check for an MMC card */
1377         if (err == TIMEOUT) {
1378                 err = mmc_send_op_cond(mmc);
1379
1380                 if (err && err != IN_PROGRESS) {
1381                         printf("Card did not respond to voltage select!\n");
1382                         return UNUSABLE_ERR;
1383                 }
1384         }
1385
1386         if (err == IN_PROGRESS)
1387                 mmc->init_in_progress = 1;
1388
1389         return err;
1390 }
1391
1392 static int mmc_complete_init(struct mmc *mmc)
1393 {
1394         int err = 0;
1395
1396         if (mmc->op_cond_pending)
1397                 err = mmc_complete_op_cond(mmc);
1398
1399         if (!err)
1400                 err = mmc_startup(mmc);
1401         if (err)
1402                 mmc->has_init = 0;
1403         else
1404                 mmc->has_init = 1;
1405         mmc->init_in_progress = 0;
1406         return err;
1407 }
1408
1409 int mmc_init(struct mmc *mmc)
1410 {
1411         int err = IN_PROGRESS;
1412         unsigned start = get_timer(0);
1413
1414         if (mmc->has_init)
1415                 return 0;
1416         if (!mmc->init_in_progress)
1417                 err = mmc_start_init(mmc);
1418
1419         if (!err || err == IN_PROGRESS)
1420                 err = mmc_complete_init(mmc);
1421         debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1422         return err;
1423 }
1424
1425 /*
1426  * CPU and board-specific MMC initializations.  Aliased function
1427  * signals caller to move on
1428  */
1429 static int __def_mmc_init(bd_t *bis)
1430 {
1431         return -1;
1432 }
1433
1434 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1435 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1436
1437 void print_mmc_devices(char separator)
1438 {
1439         struct mmc *m;
1440         struct list_head *entry;
1441
1442         list_for_each(entry, &mmc_devices) {
1443                 m = list_entry(entry, struct mmc, link);
1444
1445                 printf("%s: %d", m->name, m->block_dev.dev);
1446
1447                 if (entry->next != &mmc_devices)
1448                         printf("%c ", separator);
1449         }
1450
1451         printf("\n");
1452 }
1453
1454 int get_mmc_num(void)
1455 {
1456         return cur_dev_num;
1457 }
1458
1459 void mmc_set_preinit(struct mmc *mmc, int preinit)
1460 {
1461         mmc->preinit = preinit;
1462 }
1463
1464 static void do_preinit(void)
1465 {
1466         struct mmc *m;
1467         struct list_head *entry;
1468
1469         list_for_each(entry, &mmc_devices) {
1470                 m = list_entry(entry, struct mmc, link);
1471
1472                 if (m->preinit)
1473                         mmc_start_init(m);
1474         }
1475 }
1476
1477
1478 int mmc_initialize(bd_t *bis)
1479 {
1480         INIT_LIST_HEAD (&mmc_devices);
1481         cur_dev_num = 0;
1482
1483         if (board_mmc_init(bis) < 0)
1484                 cpu_mmc_init(bis);
1485
1486 #ifndef CONFIG_SPL_BUILD
1487         print_mmc_devices(',');
1488 #endif
1489
1490         do_preinit();
1491         return 0;
1492 }
1493
1494 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1495 /*
1496  * This function changes the size of boot partition and the size of rpmb
1497  * partition present on EMMC devices.
1498  *
1499  * Input Parameters:
1500  * struct *mmc: pointer for the mmc device strcuture
1501  * bootsize: size of boot partition
1502  * rpmbsize: size of rpmb partition
1503  *
1504  * Returns 0 on success.
1505  */
1506
1507 int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1508                                 unsigned long rpmbsize)
1509 {
1510         int err;
1511         struct mmc_cmd cmd;
1512
1513         /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1514         cmd.cmdidx = MMC_CMD_RES_MAN;
1515         cmd.resp_type = MMC_RSP_R1b;
1516         cmd.cmdarg = MMC_CMD62_ARG1;
1517
1518         err = mmc_send_cmd(mmc, &cmd, NULL);
1519         if (err) {
1520                 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1521                 return err;
1522         }
1523
1524         /* Boot partition changing mode */
1525         cmd.cmdidx = MMC_CMD_RES_MAN;
1526         cmd.resp_type = MMC_RSP_R1b;
1527         cmd.cmdarg = MMC_CMD62_ARG2;
1528
1529         err = mmc_send_cmd(mmc, &cmd, NULL);
1530         if (err) {
1531                 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1532                 return err;
1533         }
1534         /* boot partition size is multiple of 128KB */
1535         bootsize = (bootsize * 1024) / 128;
1536
1537         /* Arg: boot partition size */
1538         cmd.cmdidx = MMC_CMD_RES_MAN;
1539         cmd.resp_type = MMC_RSP_R1b;
1540         cmd.cmdarg = bootsize;
1541
1542         err = mmc_send_cmd(mmc, &cmd, NULL);
1543         if (err) {
1544                 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1545                 return err;
1546         }
1547         /* RPMB partition size is multiple of 128KB */
1548         rpmbsize = (rpmbsize * 1024) / 128;
1549         /* Arg: RPMB partition size */
1550         cmd.cmdidx = MMC_CMD_RES_MAN;
1551         cmd.resp_type = MMC_RSP_R1b;
1552         cmd.cmdarg = rpmbsize;
1553
1554         err = mmc_send_cmd(mmc, &cmd, NULL);
1555         if (err) {
1556                 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1557                 return err;
1558         }
1559         return 0;
1560 }
1561
1562 /*
1563  * This function shall form and send the commands to open / close the
1564  * boot partition specified by user.
1565  *
1566  * Input Parameters:
1567  * ack: 0x0 - No boot acknowledge sent (default)
1568  *      0x1 - Boot acknowledge sent during boot operation
1569  * part_num: User selects boot data that will be sent to master
1570  *      0x0 - Device not boot enabled (default)
1571  *      0x1 - Boot partition 1 enabled for boot
1572  *      0x2 - Boot partition 2 enabled for boot
1573  * access: User selects partitions to access
1574  *      0x0 : No access to boot partition (default)
1575  *      0x1 : R/W boot partition 1
1576  *      0x2 : R/W boot partition 2
1577  *      0x3 : R/W Replay Protected Memory Block (RPMB)
1578  *
1579  * Returns 0 on success.
1580  */
1581 int mmc_boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1582 {
1583         int err;
1584         struct mmc_cmd cmd;
1585
1586         /* Boot ack enable, boot partition enable , boot partition access */
1587         cmd.cmdidx = MMC_CMD_SWITCH;
1588         cmd.resp_type = MMC_RSP_R1b;
1589
1590         cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1591                         (EXT_CSD_PART_CONF << 16) |
1592                         ((EXT_CSD_BOOT_ACK(ack) |
1593                         EXT_CSD_BOOT_PART_NUM(part_num) |
1594                         EXT_CSD_PARTITION_ACCESS(access)) << 8);
1595
1596         err = mmc_send_cmd(mmc, &cmd, NULL);
1597         if (err) {
1598                 if (access) {
1599                         debug("mmc boot partition#%d open fail:Error1 = %d\n",
1600                               part_num, err);
1601                 } else {
1602                         debug("mmc boot partition#%d close fail:Error = %d\n",
1603                               part_num, err);
1604                 }
1605                 return err;
1606         }
1607
1608         if (access) {
1609                 /* 4bit transfer mode at booting time. */
1610                 cmd.cmdidx = MMC_CMD_SWITCH;
1611                 cmd.resp_type = MMC_RSP_R1b;
1612
1613                 cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1614                                 (EXT_CSD_BOOT_BUS_WIDTH << 16) |
1615                                 ((1 << 0) << 8);
1616
1617                 err = mmc_send_cmd(mmc, &cmd, NULL);
1618                 if (err) {
1619                         debug("mmc boot partition#%d open fail:Error2 = %d\n",
1620                               part_num, err);
1621                         return err;
1622                 }
1623         }
1624         return 0;
1625 }
1626 #endif