]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mmc/mmc.c
85e9509ed19374115421bcc6fc5ba10c380f0a24
[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 <errno.h>
14 #include <mmc.h>
15 #include <part.h>
16 #include <malloc.h>
17 #include <linux/list.h>
18 #include <div64.h>
19 #include "mmc_private.h"
20
21 static struct list_head mmc_devices;
22 static int cur_dev_num = -1;
23
24 __weak int board_mmc_getwp(struct mmc *mmc)
25 {
26         return -1;
27 }
28
29 int mmc_getwp(struct mmc *mmc)
30 {
31         int wp;
32
33         wp = board_mmc_getwp(mmc);
34
35         if (wp < 0) {
36                 if (mmc->cfg->ops->getwp)
37                         wp = mmc->cfg->ops->getwp(mmc);
38                 else
39                         wp = 0;
40         }
41
42         return wp;
43 }
44
45 __weak int board_mmc_getcd(struct mmc *mmc)
46 {
47         return -1;
48 }
49
50 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
51 {
52         int ret;
53
54 #ifdef CONFIG_MMC_TRACE
55         int i;
56         u8 *ptr;
57
58         printf("CMD_SEND:%d\n", cmd->cmdidx);
59         printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
60         ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
61         switch (cmd->resp_type) {
62                 case MMC_RSP_NONE:
63                         printf("\t\tMMC_RSP_NONE\n");
64                         break;
65                 case MMC_RSP_R1:
66                         printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
67                                 cmd->response[0]);
68                         break;
69                 case MMC_RSP_R1b:
70                         printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
71                                 cmd->response[0]);
72                         break;
73                 case MMC_RSP_R2:
74                         printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
75                                 cmd->response[0]);
76                         printf("\t\t          \t\t 0x%08X \n",
77                                 cmd->response[1]);
78                         printf("\t\t          \t\t 0x%08X \n",
79                                 cmd->response[2]);
80                         printf("\t\t          \t\t 0x%08X \n",
81                                 cmd->response[3]);
82                         printf("\n");
83                         printf("\t\t\t\t\tDUMPING DATA\n");
84                         for (i = 0; i < 4; i++) {
85                                 int j;
86                                 printf("\t\t\t\t\t%03d - ", i*4);
87                                 ptr = (u8 *)&cmd->response[i];
88                                 ptr += 3;
89                                 for (j = 0; j < 4; j++)
90                                         printf("%02X ", *ptr--);
91                                 printf("\n");
92                         }
93                         break;
94                 case MMC_RSP_R3:
95                         printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
96                                 cmd->response[0]);
97                         break;
98                 default:
99                         printf("\t\tERROR MMC rsp not supported\n");
100                         break;
101         }
102 #else
103         ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
104 #endif
105         return ret;
106 }
107
108 int mmc_send_status(struct mmc *mmc, int timeout)
109 {
110         struct mmc_cmd cmd;
111         int err, retries = 5;
112 #ifdef CONFIG_MMC_TRACE
113         int status;
114 #endif
115
116         cmd.cmdidx = MMC_CMD_SEND_STATUS;
117         cmd.resp_type = MMC_RSP_R1;
118         if (!mmc_host_is_spi(mmc))
119                 cmd.cmdarg = mmc->rca << 16;
120
121         do {
122                 err = mmc_send_cmd(mmc, &cmd, NULL);
123                 if (!err) {
124                         if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
125                             (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
126                              MMC_STATE_PRG)
127                                 break;
128                         else if (cmd.response[0] & MMC_STATUS_MASK) {
129 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
130                                 printf("Status Error: 0x%08X\n",
131                                         cmd.response[0]);
132 #endif
133                                 return COMM_ERR;
134                         }
135                 } else if (--retries < 0)
136                         return err;
137
138                 udelay(1000);
139
140         } while (timeout--);
141
142 #ifdef CONFIG_MMC_TRACE
143         status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
144         printf("CURR STATE:%d\n", status);
145 #endif
146         if (timeout <= 0) {
147 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
148                 printf("Timeout waiting card ready\n");
149 #endif
150                 return TIMEOUT;
151         }
152         if (cmd.response[0] & MMC_STATUS_SWITCH_ERROR)
153                 return SWITCH_ERR;
154
155         return 0;
156 }
157
158 int mmc_set_blocklen(struct mmc *mmc, int len)
159 {
160         struct mmc_cmd cmd;
161
162         if (mmc->ddr_mode)
163                 return 0;
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 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
185         printf("MMC Device %d not found\n", dev_num);
186 #endif
187
188         return NULL;
189 }
190
191 static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
192                            lbaint_t blkcnt)
193 {
194         struct mmc_cmd cmd;
195         struct mmc_data data;
196
197         if (blkcnt > 1)
198                 cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
199         else
200                 cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
201
202         if (mmc->high_capacity)
203                 cmd.cmdarg = start;
204         else
205                 cmd.cmdarg = start * mmc->read_bl_len;
206
207         cmd.resp_type = MMC_RSP_R1;
208
209         data.dest = dst;
210         data.blocks = blkcnt;
211         data.blocksize = mmc->read_bl_len;
212         data.flags = MMC_DATA_READ;
213
214         if (mmc_send_cmd(mmc, &cmd, &data))
215                 return 0;
216
217         if (blkcnt > 1) {
218                 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
219                 cmd.cmdarg = 0;
220                 cmd.resp_type = MMC_RSP_R1b;
221                 if (mmc_send_cmd(mmc, &cmd, NULL)) {
222 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
223                         printf("mmc fail to send stop cmd\n");
224 #endif
225                         return 0;
226                 }
227         }
228
229         return blkcnt;
230 }
231
232 static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
233 {
234         lbaint_t cur, blocks_todo = blkcnt;
235
236         if (blkcnt == 0)
237                 return 0;
238
239         struct mmc *mmc = find_mmc_device(dev_num);
240         if (!mmc)
241                 return 0;
242
243         if ((start + blkcnt) > mmc->block_dev.lba) {
244 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
245                 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
246                         start + blkcnt, mmc->block_dev.lba);
247 #endif
248                 return 0;
249         }
250
251         if (mmc_set_blocklen(mmc, mmc->read_bl_len))
252                 return 0;
253
254         do {
255                 cur = (blocks_todo > mmc->cfg->b_max) ?
256                         mmc->cfg->b_max : blocks_todo;
257                 if(mmc_read_blocks(mmc, dst, start, cur) != cur)
258                         return 0;
259                 blocks_todo -= cur;
260                 start += cur;
261                 dst += cur * mmc->read_bl_len;
262         } while (blocks_todo > 0);
263
264         return blkcnt;
265 }
266
267 static int mmc_go_idle(struct mmc *mmc)
268 {
269         struct mmc_cmd cmd;
270         int err;
271
272         udelay(1000);
273
274         cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
275         cmd.cmdarg = 0;
276         cmd.resp_type = MMC_RSP_NONE;
277
278         err = mmc_send_cmd(mmc, &cmd, NULL);
279
280         if (err)
281                 return err;
282
283         udelay(2000);
284
285         return 0;
286 }
287
288 static int sd_send_op_cond(struct mmc *mmc)
289 {
290         int timeout = 1000;
291         int err;
292         struct mmc_cmd cmd;
293
294         do {
295                 cmd.cmdidx = MMC_CMD_APP_CMD;
296                 cmd.resp_type = MMC_RSP_R1;
297                 cmd.cmdarg = 0;
298
299                 err = mmc_send_cmd(mmc, &cmd, NULL);
300
301                 if (err)
302                         return err;
303
304                 cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
305                 cmd.resp_type = MMC_RSP_R3;
306
307                 /*
308                  * Most cards do not answer if some reserved bits
309                  * in the ocr are set. However, Some controller
310                  * can set bit 7 (reserved for low voltages), but
311                  * how to manage low voltages SD card is not yet
312                  * specified.
313                  */
314                 cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
315                         (mmc->cfg->voltages & 0xff8000);
316
317                 if (mmc->version == SD_VERSION_2)
318                         cmd.cmdarg |= OCR_HCS;
319
320                 err = mmc_send_cmd(mmc, &cmd, NULL);
321
322                 if (err)
323                         return err;
324
325                 udelay(1000);
326         } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
327         if (!(cmd.response[0] & OCR_BUSY))
328                 return UNUSABLE_ERR;
329
330         if (mmc->version != SD_VERSION_2)
331                 mmc->version = SD_VERSION_1_0;
332
333         if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
334                 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
335                 cmd.resp_type = MMC_RSP_R3;
336                 cmd.cmdarg = 0;
337
338                 err = mmc_send_cmd(mmc, &cmd, NULL);
339
340                 if (err)
341                         return err;
342         }
343
344         mmc->ocr = cmd.response[0];
345
346         mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
347         mmc->rca = 0;
348
349         return 0;
350 }
351
352 /* We pass in the cmd since otherwise the init seems to fail */
353 static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
354                 int use_arg)
355 {
356         int err;
357
358         cmd->cmdidx = MMC_CMD_SEND_OP_COND;
359         cmd->resp_type = MMC_RSP_R3;
360         cmd->cmdarg = 0;
361         if (use_arg && !mmc_host_is_spi(mmc)) {
362                 cmd->cmdarg =
363                         (mmc->cfg->voltages &
364                         (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
365                         (mmc->op_cond_response & OCR_ACCESS_MODE);
366
367                 if (mmc->cfg->host_caps & MMC_MODE_HC)
368                         cmd->cmdarg |= OCR_HCS;
369         }
370         err = mmc_send_cmd(mmc, cmd, NULL);
371         if (err)
372                 return err;
373         mmc->op_cond_response = cmd->response[0];
374         return 0;
375 }
376
377 static int mmc_send_op_cond(struct mmc *mmc)
378 {
379         struct mmc_cmd cmd;
380         int err, i;
381
382         /* Some cards seem to need this */
383         mmc_go_idle(mmc);
384
385         /* Asking to the card its capabilities */
386         mmc->op_cond_pending = 1;
387         for (i = 0; i < 2; i++) {
388                 err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
389                 if (err)
390                         return err;
391
392                 /* exit if not busy (flag seems to be inverted) */
393                 if (mmc->op_cond_response & OCR_BUSY)
394                         return 0;
395         }
396         return IN_PROGRESS;
397 }
398
399 static int mmc_complete_op_cond(struct mmc *mmc)
400 {
401         struct mmc_cmd cmd;
402         int timeout = 1000;
403         uint start;
404         int err;
405
406         mmc->op_cond_pending = 0;
407         start = get_timer(0);
408         do {
409                 err = mmc_send_op_cond_iter(mmc, &cmd, 1);
410                 if (err)
411                         return err;
412                 if (get_timer(start) > timeout)
413                         break;
414                 udelay(100);
415         } while (!(mmc->op_cond_response & OCR_BUSY));
416         if (!(mmc->op_cond_response & OCR_BUSY)) {
417                 debug("%s: timeout\n", __func__);
418                 return UNUSABLE_ERR;
419         }
420
421         if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
422                 cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
423                 cmd.resp_type = MMC_RSP_R3;
424                 cmd.cmdarg = 0;
425
426                 err = mmc_send_cmd(mmc, &cmd, NULL);
427
428                 if (err)
429                         return err;
430         }
431
432         mmc->version = MMC_VERSION_UNKNOWN;
433         mmc->ocr = cmd.response[0];
434
435         mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
436         mmc->rca = 1;
437
438         return 0;
439 }
440
441
442 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
443 {
444         struct mmc_cmd cmd;
445         struct mmc_data data;
446         int err;
447
448         /* Get the Card Status Register */
449         cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
450         cmd.resp_type = MMC_RSP_R1;
451         cmd.cmdarg = 0;
452
453         data.dest = (char *)ext_csd;
454         data.blocks = 1;
455         data.blocksize = MMC_MAX_BLOCK_LEN;
456         data.flags = MMC_DATA_READ;
457
458         err = mmc_send_cmd(mmc, &cmd, &data);
459
460         return err;
461 }
462
463
464 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
465 {
466         struct mmc_cmd cmd;
467         int timeout = 1000;
468         int ret;
469
470         cmd.cmdidx = MMC_CMD_SWITCH;
471         cmd.resp_type = MMC_RSP_R1b;
472         cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
473                                  (index << 16) |
474                                  (value << 8);
475
476         ret = mmc_send_cmd(mmc, &cmd, NULL);
477
478         /* Waiting for the ready status */
479         if (!ret)
480                 ret = mmc_send_status(mmc, timeout);
481
482         return ret;
483
484 }
485
486 static int mmc_change_freq(struct mmc *mmc)
487 {
488         ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
489         char cardtype;
490         int err;
491
492         mmc->card_caps = 0;
493
494         if (mmc_host_is_spi(mmc))
495                 return 0;
496
497         /* Only version 4 supports high-speed */
498         if (mmc->version < MMC_VERSION_4)
499                 return 0;
500
501         mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
502
503         err = mmc_send_ext_csd(mmc, ext_csd);
504
505         if (err)
506                 return err;
507
508         cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
509
510         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
511
512         if (err)
513                 return err == SWITCH_ERR ? 0 : err;
514
515         /* Now check to see that it worked */
516         err = mmc_send_ext_csd(mmc, ext_csd);
517
518         if (err)
519                 return err;
520
521         /* No high-speed support */
522         if (!ext_csd[EXT_CSD_HS_TIMING])
523                 return 0;
524
525         /* High Speed is set, there are two types: 52MHz and 26MHz */
526         if (cardtype & EXT_CSD_CARD_TYPE_52) {
527                 if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
528                         mmc->card_caps |= MMC_MODE_DDR_52MHz;
529                 mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
530         } else {
531                 mmc->card_caps |= MMC_MODE_HS;
532         }
533
534         return 0;
535 }
536
537 static int mmc_set_capacity(struct mmc *mmc, int part_num)
538 {
539         switch (part_num) {
540         case 0:
541                 mmc->capacity = mmc->capacity_user;
542                 break;
543         case 1:
544         case 2:
545                 mmc->capacity = mmc->capacity_boot;
546                 break;
547         case 3:
548                 mmc->capacity = mmc->capacity_rpmb;
549                 break;
550         case 4:
551         case 5:
552         case 6:
553         case 7:
554                 mmc->capacity = mmc->capacity_gp[part_num - 4];
555                 break;
556         default:
557                 return -1;
558         }
559
560         mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
561
562         return 0;
563 }
564
565 int mmc_select_hwpart(int dev_num, int hwpart)
566 {
567         struct mmc *mmc = find_mmc_device(dev_num);
568         int ret;
569
570         if (!mmc)
571                 return -ENODEV;
572
573         if (mmc->part_num == hwpart)
574                 return 0;
575
576         if (mmc->part_config == MMCPART_NOAVAILABLE) {
577                 printf("Card doesn't support part_switch\n");
578                 return -EMEDIUMTYPE;
579         }
580
581         ret = mmc_switch_part(dev_num, hwpart);
582         if (ret)
583                 return ret;
584
585         mmc->part_num = hwpart;
586
587         return 0;
588 }
589
590
591 int mmc_switch_part(int dev_num, unsigned int part_num)
592 {
593         struct mmc *mmc = find_mmc_device(dev_num);
594         int ret;
595
596         if (!mmc)
597                 return -1;
598
599         ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
600                          (mmc->part_config & ~PART_ACCESS_MASK)
601                          | (part_num & PART_ACCESS_MASK));
602
603         /*
604          * Set the capacity if the switch succeeded or was intended
605          * to return to representing the raw device.
606          */
607         if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0)))
608                 ret = mmc_set_capacity(mmc, part_num);
609
610         return ret;
611 }
612
613 int mmc_hwpart_config(struct mmc *mmc,
614                       const struct mmc_hwpart_conf *conf,
615                       enum mmc_hwpart_conf_mode mode)
616 {
617         u8 part_attrs = 0;
618         u32 enh_size_mult;
619         u32 enh_start_addr;
620         u32 gp_size_mult[4];
621         u32 max_enh_size_mult;
622         u32 tot_enh_size_mult = 0;
623         u8 wr_rel_set;
624         int i, pidx, err;
625         ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
626
627         if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
628                 return -EINVAL;
629
630         if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
631                 printf("eMMC >= 4.4 required for enhanced user data area\n");
632                 return -EMEDIUMTYPE;
633         }
634
635         if (!(mmc->part_support & PART_SUPPORT)) {
636                 printf("Card does not support partitioning\n");
637                 return -EMEDIUMTYPE;
638         }
639
640         if (!mmc->hc_wp_grp_size) {
641                 printf("Card does not define HC WP group size\n");
642                 return -EMEDIUMTYPE;
643         }
644
645         /* check partition alignment and total enhanced size */
646         if (conf->user.enh_size) {
647                 if (conf->user.enh_size % mmc->hc_wp_grp_size ||
648                     conf->user.enh_start % mmc->hc_wp_grp_size) {
649                         printf("User data enhanced area not HC WP group "
650                                "size aligned\n");
651                         return -EINVAL;
652                 }
653                 part_attrs |= EXT_CSD_ENH_USR;
654                 enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
655                 if (mmc->high_capacity) {
656                         enh_start_addr = conf->user.enh_start;
657                 } else {
658                         enh_start_addr = (conf->user.enh_start << 9);
659                 }
660         } else {
661                 enh_size_mult = 0;
662                 enh_start_addr = 0;
663         }
664         tot_enh_size_mult += enh_size_mult;
665
666         for (pidx = 0; pidx < 4; pidx++) {
667                 if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
668                         printf("GP%i partition not HC WP group size "
669                                "aligned\n", pidx+1);
670                         return -EINVAL;
671                 }
672                 gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
673                 if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
674                         part_attrs |= EXT_CSD_ENH_GP(pidx);
675                         tot_enh_size_mult += gp_size_mult[pidx];
676                 }
677         }
678
679         if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
680                 printf("Card does not support enhanced attribute\n");
681                 return -EMEDIUMTYPE;
682         }
683
684         err = mmc_send_ext_csd(mmc, ext_csd);
685         if (err)
686                 return err;
687
688         max_enh_size_mult =
689                 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
690                 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
691                 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
692         if (tot_enh_size_mult > max_enh_size_mult) {
693                 printf("Total enhanced size exceeds maximum (%u > %u)\n",
694                        tot_enh_size_mult, max_enh_size_mult);
695                 return -EMEDIUMTYPE;
696         }
697
698         /* The default value of EXT_CSD_WR_REL_SET is device
699          * dependent, the values can only be changed if the
700          * EXT_CSD_HS_CTRL_REL bit is set. The values can be
701          * changed only once and before partitioning is completed. */
702         wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
703         if (conf->user.wr_rel_change) {
704                 if (conf->user.wr_rel_set)
705                         wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
706                 else
707                         wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
708         }
709         for (pidx = 0; pidx < 4; pidx++) {
710                 if (conf->gp_part[pidx].wr_rel_change) {
711                         if (conf->gp_part[pidx].wr_rel_set)
712                                 wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
713                         else
714                                 wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
715                 }
716         }
717
718         if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
719             !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
720                 puts("Card does not support host controlled partition write "
721                      "reliability settings\n");
722                 return -EMEDIUMTYPE;
723         }
724
725         if (ext_csd[EXT_CSD_PARTITION_SETTING] &
726             EXT_CSD_PARTITION_SETTING_COMPLETED) {
727                 printf("Card already partitioned\n");
728                 return -EPERM;
729         }
730
731         if (mode == MMC_HWPART_CONF_CHECK)
732                 return 0;
733
734         /* Partitioning requires high-capacity size definitions */
735         if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
736                 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
737                                  EXT_CSD_ERASE_GROUP_DEF, 1);
738
739                 if (err)
740                         return err;
741
742                 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
743
744                 /* update erase group size to be high-capacity */
745                 mmc->erase_grp_size =
746                         ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
747
748         }
749
750         /* all OK, write the configuration */
751         for (i = 0; i < 4; i++) {
752                 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
753                                  EXT_CSD_ENH_START_ADDR+i,
754                                  (enh_start_addr >> (i*8)) & 0xFF);
755                 if (err)
756                         return err;
757         }
758         for (i = 0; i < 3; i++) {
759                 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
760                                  EXT_CSD_ENH_SIZE_MULT+i,
761                                  (enh_size_mult >> (i*8)) & 0xFF);
762                 if (err)
763                         return err;
764         }
765         for (pidx = 0; pidx < 4; pidx++) {
766                 for (i = 0; i < 3; i++) {
767                         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
768                                          EXT_CSD_GP_SIZE_MULT+pidx*3+i,
769                                          (gp_size_mult[pidx] >> (i*8)) & 0xFF);
770                         if (err)
771                                 return err;
772                 }
773         }
774         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
775                          EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
776         if (err)
777                 return err;
778
779         if (mode == MMC_HWPART_CONF_SET)
780                 return 0;
781
782         /* The WR_REL_SET is a write-once register but shall be
783          * written before setting PART_SETTING_COMPLETED. As it is
784          * write-once we can only write it when completing the
785          * partitioning. */
786         if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
787                 err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
788                                  EXT_CSD_WR_REL_SET, wr_rel_set);
789                 if (err)
790                         return err;
791         }
792
793         /* Setting PART_SETTING_COMPLETED confirms the partition
794          * configuration but it only becomes effective after power
795          * cycle, so we do not adjust the partition related settings
796          * in the mmc struct. */
797
798         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
799                          EXT_CSD_PARTITION_SETTING,
800                          EXT_CSD_PARTITION_SETTING_COMPLETED);
801         if (err)
802                 return err;
803
804         return 0;
805 }
806
807 int mmc_getcd(struct mmc *mmc)
808 {
809         int cd;
810
811         cd = board_mmc_getcd(mmc);
812
813         if (cd < 0) {
814                 if (mmc->cfg->ops->getcd)
815                         cd = mmc->cfg->ops->getcd(mmc);
816                 else
817                         cd = 1;
818         }
819
820         return cd;
821 }
822
823 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
824 {
825         struct mmc_cmd cmd;
826         struct mmc_data data;
827
828         /* Switch the frequency */
829         cmd.cmdidx = SD_CMD_SWITCH_FUNC;
830         cmd.resp_type = MMC_RSP_R1;
831         cmd.cmdarg = (mode << 31) | 0xffffff;
832         cmd.cmdarg &= ~(0xf << (group * 4));
833         cmd.cmdarg |= value << (group * 4);
834
835         data.dest = (char *)resp;
836         data.blocksize = 64;
837         data.blocks = 1;
838         data.flags = MMC_DATA_READ;
839
840         return mmc_send_cmd(mmc, &cmd, &data);
841 }
842
843
844 static int sd_change_freq(struct mmc *mmc)
845 {
846         int err;
847         struct mmc_cmd cmd;
848         ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
849         ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
850         struct mmc_data data;
851         int timeout;
852
853         mmc->card_caps = 0;
854
855         if (mmc_host_is_spi(mmc))
856                 return 0;
857
858         /* Read the SCR to find out if this card supports higher speeds */
859         cmd.cmdidx = MMC_CMD_APP_CMD;
860         cmd.resp_type = MMC_RSP_R1;
861         cmd.cmdarg = mmc->rca << 16;
862
863         err = mmc_send_cmd(mmc, &cmd, NULL);
864
865         if (err)
866                 return err;
867
868         cmd.cmdidx = SD_CMD_APP_SEND_SCR;
869         cmd.resp_type = MMC_RSP_R1;
870         cmd.cmdarg = 0;
871
872         timeout = 3;
873
874 retry_scr:
875         data.dest = (char *)scr;
876         data.blocksize = 8;
877         data.blocks = 1;
878         data.flags = MMC_DATA_READ;
879
880         err = mmc_send_cmd(mmc, &cmd, &data);
881
882         if (err) {
883                 if (timeout--)
884                         goto retry_scr;
885
886                 return err;
887         }
888
889         mmc->scr[0] = __be32_to_cpu(scr[0]);
890         mmc->scr[1] = __be32_to_cpu(scr[1]);
891
892         switch ((mmc->scr[0] >> 24) & 0xf) {
893                 case 0:
894                         mmc->version = SD_VERSION_1_0;
895                         break;
896                 case 1:
897                         mmc->version = SD_VERSION_1_10;
898                         break;
899                 case 2:
900                         mmc->version = SD_VERSION_2;
901                         if ((mmc->scr[0] >> 15) & 0x1)
902                                 mmc->version = SD_VERSION_3;
903                         break;
904                 default:
905                         mmc->version = SD_VERSION_1_0;
906                         break;
907         }
908
909         if (mmc->scr[0] & SD_DATA_4BIT)
910                 mmc->card_caps |= MMC_MODE_4BIT;
911
912         /* Version 1.0 doesn't support switching */
913         if (mmc->version == SD_VERSION_1_0)
914                 return 0;
915
916         timeout = 4;
917         while (timeout--) {
918                 err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
919                                 (u8 *)switch_status);
920
921                 if (err)
922                         return err;
923
924                 /* The high-speed function is busy.  Try again */
925                 if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
926                         break;
927         }
928
929         /* If high-speed isn't supported, we return */
930         if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
931                 return 0;
932
933         /*
934          * If the host doesn't support SD_HIGHSPEED, do not switch card to
935          * HIGHSPEED mode even if the card support SD_HIGHSPPED.
936          * This can avoid furthur problem when the card runs in different
937          * mode between the host.
938          */
939         if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
940                 (mmc->cfg->host_caps & MMC_MODE_HS)))
941                 return 0;
942
943         err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
944
945         if (err)
946                 return err;
947
948         if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
949                 mmc->card_caps |= MMC_MODE_HS;
950
951         return 0;
952 }
953
954 /* frequency bases */
955 /* divided by 10 to be nice to platforms without floating point */
956 static const int fbase[] = {
957         10000,
958         100000,
959         1000000,
960         10000000,
961 };
962
963 /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
964  * to platforms without floating point.
965  */
966 static const int multipliers[] = {
967         0,      /* reserved */
968         10,
969         12,
970         13,
971         15,
972         20,
973         25,
974         30,
975         35,
976         40,
977         45,
978         50,
979         55,
980         60,
981         70,
982         80,
983 };
984
985 static void mmc_set_ios(struct mmc *mmc)
986 {
987         if (mmc->cfg->ops->set_ios)
988                 mmc->cfg->ops->set_ios(mmc);
989 }
990
991 void mmc_set_clock(struct mmc *mmc, uint clock)
992 {
993         if (clock > mmc->cfg->f_max)
994                 clock = mmc->cfg->f_max;
995
996         if (clock < mmc->cfg->f_min)
997                 clock = mmc->cfg->f_min;
998
999         mmc->clock = clock;
1000
1001         mmc_set_ios(mmc);
1002 }
1003
1004 static void mmc_set_bus_width(struct mmc *mmc, uint width)
1005 {
1006         mmc->bus_width = width;
1007
1008         mmc_set_ios(mmc);
1009 }
1010
1011 static int mmc_startup(struct mmc *mmc)
1012 {
1013         int err, i;
1014         uint mult, freq;
1015         u64 cmult, csize, capacity;
1016         struct mmc_cmd cmd;
1017         ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1018         ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
1019         int timeout = 1000;
1020         bool has_parts = false;
1021         bool part_completed;
1022
1023 #ifdef CONFIG_MMC_SPI_CRC_ON
1024         if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1025                 cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1026                 cmd.resp_type = MMC_RSP_R1;
1027                 cmd.cmdarg = 1;
1028                 err = mmc_send_cmd(mmc, &cmd, NULL);
1029
1030                 if (err)
1031                         return err;
1032         }
1033 #endif
1034
1035         /* Put the Card in Identify Mode */
1036         cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1037                 MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
1038         cmd.resp_type = MMC_RSP_R2;
1039         cmd.cmdarg = 0;
1040
1041         err = mmc_send_cmd(mmc, &cmd, NULL);
1042
1043         if (err)
1044                 return err;
1045
1046         memcpy(mmc->cid, cmd.response, 16);
1047
1048         /*
1049          * For MMC cards, set the Relative Address.
1050          * For SD cards, get the Relatvie Address.
1051          * This also puts the cards into Standby State
1052          */
1053         if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1054                 cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1055                 cmd.cmdarg = mmc->rca << 16;
1056                 cmd.resp_type = MMC_RSP_R6;
1057
1058                 err = mmc_send_cmd(mmc, &cmd, NULL);
1059
1060                 if (err)
1061                         return err;
1062
1063                 if (IS_SD(mmc))
1064                         mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1065         }
1066
1067         /* Get the Card-Specific Data */
1068         cmd.cmdidx = MMC_CMD_SEND_CSD;
1069         cmd.resp_type = MMC_RSP_R2;
1070         cmd.cmdarg = mmc->rca << 16;
1071
1072         err = mmc_send_cmd(mmc, &cmd, NULL);
1073
1074         /* Waiting for the ready status */
1075         mmc_send_status(mmc, timeout);
1076
1077         if (err)
1078                 return err;
1079
1080         mmc->csd[0] = cmd.response[0];
1081         mmc->csd[1] = cmd.response[1];
1082         mmc->csd[2] = cmd.response[2];
1083         mmc->csd[3] = cmd.response[3];
1084
1085         if (mmc->version == MMC_VERSION_UNKNOWN) {
1086                 int version = (cmd.response[0] >> 26) & 0xf;
1087
1088                 switch (version) {
1089                         case 0:
1090                                 mmc->version = MMC_VERSION_1_2;
1091                                 break;
1092                         case 1:
1093                                 mmc->version = MMC_VERSION_1_4;
1094                                 break;
1095                         case 2:
1096                                 mmc->version = MMC_VERSION_2_2;
1097                                 break;
1098                         case 3:
1099                                 mmc->version = MMC_VERSION_3;
1100                                 break;
1101                         case 4:
1102                                 mmc->version = MMC_VERSION_4;
1103                                 break;
1104                         default:
1105                                 mmc->version = MMC_VERSION_1_2;
1106                                 break;
1107                 }
1108         }
1109
1110         /* divide frequency by 10, since the mults are 10x bigger */
1111         freq = fbase[(cmd.response[0] & 0x7)];
1112         mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
1113
1114         mmc->tran_speed = freq * mult;
1115
1116         mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
1117         mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1118
1119         if (IS_SD(mmc))
1120                 mmc->write_bl_len = mmc->read_bl_len;
1121         else
1122                 mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1123
1124         if (mmc->high_capacity) {
1125                 csize = (mmc->csd[1] & 0x3f) << 16
1126                         | (mmc->csd[2] & 0xffff0000) >> 16;
1127                 cmult = 8;
1128         } else {
1129                 csize = (mmc->csd[1] & 0x3ff) << 2
1130                         | (mmc->csd[2] & 0xc0000000) >> 30;
1131                 cmult = (mmc->csd[2] & 0x00038000) >> 15;
1132         }
1133
1134         mmc->capacity_user = (csize + 1) << (cmult + 2);
1135         mmc->capacity_user *= mmc->read_bl_len;
1136         mmc->capacity_boot = 0;
1137         mmc->capacity_rpmb = 0;
1138         for (i = 0; i < 4; i++)
1139                 mmc->capacity_gp[i] = 0;
1140
1141         if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
1142                 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1143
1144         if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
1145                 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1146
1147         if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
1148                 cmd.cmdidx = MMC_CMD_SET_DSR;
1149                 cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
1150                 cmd.resp_type = MMC_RSP_NONE;
1151                 if (mmc_send_cmd(mmc, &cmd, NULL))
1152                         printf("MMC: SET_DSR failed\n");
1153         }
1154
1155         /* Select the card, and put it into Transfer Mode */
1156         if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1157                 cmd.cmdidx = MMC_CMD_SELECT_CARD;
1158                 cmd.resp_type = MMC_RSP_R1;
1159                 cmd.cmdarg = mmc->rca << 16;
1160                 err = mmc_send_cmd(mmc, &cmd, NULL);
1161
1162                 if (err)
1163                         return err;
1164         }
1165
1166         /*
1167          * For SD, its erase group is always one sector
1168          */
1169         mmc->erase_grp_size = 1;
1170         mmc->part_config = MMCPART_NOAVAILABLE;
1171         if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1172                 /* check  ext_csd version and capacity */
1173                 err = mmc_send_ext_csd(mmc, ext_csd);
1174                 if (err)
1175                         return err;
1176                 if (ext_csd[EXT_CSD_REV] >= 2) {
1177                         /*
1178                          * According to the JEDEC Standard, the value of
1179                          * ext_csd's capacity is valid if the value is more
1180                          * than 2GB
1181                          */
1182                         capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1183                                         | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1184                                         | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1185                                         | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1186                         capacity *= MMC_MAX_BLOCK_LEN;
1187                         if ((capacity >> 20) > 2 * 1024)
1188                                 mmc->capacity_user = capacity;
1189                 }
1190
1191                 switch (ext_csd[EXT_CSD_REV]) {
1192                 case 1:
1193                         mmc->version = MMC_VERSION_4_1;
1194                         break;
1195                 case 2:
1196                         mmc->version = MMC_VERSION_4_2;
1197                         break;
1198                 case 3:
1199                         mmc->version = MMC_VERSION_4_3;
1200                         break;
1201                 case 5:
1202                         mmc->version = MMC_VERSION_4_41;
1203                         break;
1204                 case 6:
1205                         mmc->version = MMC_VERSION_4_5;
1206                         break;
1207                 case 7:
1208                         mmc->version = MMC_VERSION_5_0;
1209                         break;
1210                 }
1211
1212                 /* The partition data may be non-zero but it is only
1213                  * effective if PARTITION_SETTING_COMPLETED is set in
1214                  * EXT_CSD, so ignore any data if this bit is not set,
1215                  * except for enabling the high-capacity group size
1216                  * definition (see below). */
1217                 part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
1218                                     EXT_CSD_PARTITION_SETTING_COMPLETED);
1219
1220                 /* store the partition info of emmc */
1221                 mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
1222                 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1223                     ext_csd[EXT_CSD_BOOT_MULT])
1224                         mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1225                 if (part_completed &&
1226                     (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
1227                         mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
1228
1229                 mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1230
1231                 mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1232
1233                 for (i = 0; i < 4; i++) {
1234                         int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1235                         uint mult = (ext_csd[idx + 2] << 16) +
1236                                 (ext_csd[idx + 1] << 8) + ext_csd[idx];
1237                         if (mult)
1238                                 has_parts = true;
1239                         if (!part_completed)
1240                                 continue;
1241                         mmc->capacity_gp[i] = mult;
1242                         mmc->capacity_gp[i] *=
1243                                 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1244                         mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1245                         mmc->capacity_gp[i] <<= 19;
1246                 }
1247
1248                 if (part_completed) {
1249                         mmc->enh_user_size =
1250                                 (ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) +
1251                                 (ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) +
1252                                 ext_csd[EXT_CSD_ENH_SIZE_MULT];
1253                         mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1254                         mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1255                         mmc->enh_user_size <<= 19;
1256                         mmc->enh_user_start =
1257                                 (ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) +
1258                                 (ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) +
1259                                 (ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) +
1260                                 ext_csd[EXT_CSD_ENH_START_ADDR];
1261                         if (mmc->high_capacity)
1262                                 mmc->enh_user_start <<= 9;
1263                 }
1264
1265                 /*
1266                  * Host needs to enable ERASE_GRP_DEF bit if device is
1267                  * partitioned. This bit will be lost every time after a reset
1268                  * or power off. This will affect erase size.
1269                  */
1270                 if (part_completed)
1271                         has_parts = true;
1272                 if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
1273                     (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
1274                         has_parts = true;
1275                 if (has_parts) {
1276                         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1277                                 EXT_CSD_ERASE_GROUP_DEF, 1);
1278
1279                         if (err)
1280                                 return err;
1281                         else
1282                                 ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1283                 }
1284
1285                 if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
1286                         /* Read out group size from ext_csd */
1287                         mmc->erase_grp_size =
1288                                 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1289                         /*
1290                          * if high capacity and partition setting completed
1291                          * SEC_COUNT is valid even if it is smaller than 2 GiB
1292                          * JEDEC Standard JESD84-B45, 6.2.4
1293                          */
1294                         if (mmc->high_capacity && part_completed) {
1295                                 capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1296                                         (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1297                                         (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1298                                         (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1299                                 capacity *= MMC_MAX_BLOCK_LEN;
1300                                 mmc->capacity_user = capacity;
1301                         }
1302                 } else {
1303                         /* Calculate the group size from the csd value. */
1304                         int erase_gsz, erase_gmul;
1305                         erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1306                         erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1307                         mmc->erase_grp_size = (erase_gsz + 1)
1308                                 * (erase_gmul + 1);
1309                 }
1310
1311                 mmc->hc_wp_grp_size = 1024
1312                         * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1313                         * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1314
1315                 mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1316         }
1317
1318         err = mmc_set_capacity(mmc, mmc->part_num);
1319         if (err)
1320                 return err;
1321
1322         if (IS_SD(mmc))
1323                 err = sd_change_freq(mmc);
1324         else
1325                 err = mmc_change_freq(mmc);
1326
1327         if (err)
1328                 return err;
1329
1330         /* Restrict card's capabilities by what the host can do */
1331         mmc->card_caps &= mmc->cfg->host_caps;
1332
1333         if (IS_SD(mmc)) {
1334                 if (mmc->card_caps & MMC_MODE_4BIT) {
1335                         cmd.cmdidx = MMC_CMD_APP_CMD;
1336                         cmd.resp_type = MMC_RSP_R1;
1337                         cmd.cmdarg = mmc->rca << 16;
1338
1339                         err = mmc_send_cmd(mmc, &cmd, NULL);
1340                         if (err)
1341                                 return err;
1342
1343                         cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1344                         cmd.resp_type = MMC_RSP_R1;
1345                         cmd.cmdarg = 2;
1346                         err = mmc_send_cmd(mmc, &cmd, NULL);
1347                         if (err)
1348                                 return err;
1349
1350                         mmc_set_bus_width(mmc, 4);
1351                 }
1352
1353                 if (mmc->card_caps & MMC_MODE_HS)
1354                         mmc->tran_speed = 50000000;
1355                 else
1356                         mmc->tran_speed = 25000000;
1357         } else if (mmc->version >= MMC_VERSION_4) {
1358                 /* Only version 4 of MMC supports wider bus widths */
1359                 int idx;
1360
1361                 /* An array of possible bus widths in order of preference */
1362                 static unsigned ext_csd_bits[] = {
1363                         EXT_CSD_DDR_BUS_WIDTH_8,
1364                         EXT_CSD_DDR_BUS_WIDTH_4,
1365                         EXT_CSD_BUS_WIDTH_8,
1366                         EXT_CSD_BUS_WIDTH_4,
1367                         EXT_CSD_BUS_WIDTH_1,
1368                 };
1369
1370                 /* An array to map CSD bus widths to host cap bits */
1371                 static unsigned ext_to_hostcaps[] = {
1372                         [EXT_CSD_DDR_BUS_WIDTH_4] =
1373                                 MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1374                         [EXT_CSD_DDR_BUS_WIDTH_8] =
1375                                 MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
1376                         [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1377                         [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1378                 };
1379
1380                 /* An array to map chosen bus width to an integer */
1381                 static unsigned widths[] = {
1382                         8, 4, 8, 4, 1,
1383                 };
1384
1385                 for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1386                         unsigned int extw = ext_csd_bits[idx];
1387                         unsigned int caps = ext_to_hostcaps[extw];
1388
1389                         /*
1390                          * If the bus width is still not changed,
1391                          * don't try to set the default again.
1392                          * Otherwise, recover from switch attempts
1393                          * by switching to 1-bit bus width.
1394                          */
1395                         if (extw == EXT_CSD_BUS_WIDTH_1 &&
1396                                         mmc->bus_width == 1) {
1397                                 err = 0;
1398                                 break;
1399                         }
1400
1401                         /*
1402                          * Check to make sure the card and controller support
1403                          * these capabilities
1404                          */
1405                         if ((mmc->card_caps & caps) != caps)
1406                                 continue;
1407
1408                         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1409                                         EXT_CSD_BUS_WIDTH, extw);
1410
1411                         if (err)
1412                                 continue;
1413
1414                         mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
1415                         mmc_set_bus_width(mmc, widths[idx]);
1416
1417                         err = mmc_send_ext_csd(mmc, test_csd);
1418
1419                         if (err)
1420                                 continue;
1421
1422                         /* Only compare read only fields */
1423                         if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1424                                 == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1425                             ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1426                                 == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1427                             ext_csd[EXT_CSD_REV]
1428                                 == test_csd[EXT_CSD_REV] &&
1429                             ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1430                                 == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1431                             memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1432                                    &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
1433                                 break;
1434                         else
1435                                 err = SWITCH_ERR;
1436                 }
1437
1438                 if (err)
1439                         return err;
1440
1441                 if (mmc->card_caps & MMC_MODE_HS) {
1442                         if (mmc->card_caps & MMC_MODE_HS_52MHz)
1443                                 mmc->tran_speed = 52000000;
1444                         else
1445                                 mmc->tran_speed = 26000000;
1446                 }
1447         }
1448
1449         mmc_set_clock(mmc, mmc->tran_speed);
1450
1451         /* Fix the block length for DDR mode */
1452         if (mmc->ddr_mode) {
1453                 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1454                 mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1455         }
1456
1457         /* fill in device description */
1458         mmc->block_dev.lun = 0;
1459         mmc->block_dev.type = 0;
1460         mmc->block_dev.blksz = mmc->read_bl_len;
1461         mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
1462         mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1463 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1464         sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1465                 mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1466                 (mmc->cid[3] >> 16) & 0xffff);
1467         sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1468                 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1469                 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1470                 (mmc->cid[2] >> 24) & 0xff);
1471         sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1472                 (mmc->cid[2] >> 16) & 0xf);
1473 #else
1474         mmc->block_dev.vendor[0] = 0;
1475         mmc->block_dev.product[0] = 0;
1476         mmc->block_dev.revision[0] = 0;
1477 #endif
1478 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1479         init_part(&mmc->block_dev);
1480 #endif
1481
1482         return 0;
1483 }
1484
1485 static int mmc_send_if_cond(struct mmc *mmc)
1486 {
1487         struct mmc_cmd cmd;
1488         int err;
1489
1490         cmd.cmdidx = SD_CMD_SEND_IF_COND;
1491         /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1492         cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
1493         cmd.resp_type = MMC_RSP_R7;
1494
1495         err = mmc_send_cmd(mmc, &cmd, NULL);
1496
1497         if (err)
1498                 return err;
1499
1500         if ((cmd.response[0] & 0xff) != 0xaa)
1501                 return UNUSABLE_ERR;
1502         else
1503                 mmc->version = SD_VERSION_2;
1504
1505         return 0;
1506 }
1507
1508 /* not used any more */
1509 int __deprecated mmc_register(struct mmc *mmc)
1510 {
1511 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1512         printf("%s is deprecated! use mmc_create() instead.\n", __func__);
1513 #endif
1514         return -1;
1515 }
1516
1517 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
1518 {
1519         struct mmc *mmc;
1520
1521         /* quick validation */
1522         if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
1523                         cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
1524                 return NULL;
1525
1526         mmc = calloc(1, sizeof(*mmc));
1527         if (mmc == NULL)
1528                 return NULL;
1529
1530         mmc->cfg = cfg;
1531         mmc->priv = priv;
1532
1533         /* the following chunk was mmc_register() */
1534
1535         /* Setup dsr related values */
1536         mmc->dsr_imp = 0;
1537         mmc->dsr = 0xffffffff;
1538         /* Setup the universal parts of the block interface just once */
1539         mmc->block_dev.if_type = IF_TYPE_MMC;
1540         mmc->block_dev.dev = cur_dev_num++;
1541         mmc->block_dev.removable = 1;
1542         mmc->block_dev.block_read = mmc_bread;
1543         mmc->block_dev.block_write = mmc_bwrite;
1544         mmc->block_dev.block_erase = mmc_berase;
1545
1546         /* setup initial part type */
1547         mmc->block_dev.part_type = mmc->cfg->part_type;
1548
1549         INIT_LIST_HEAD(&mmc->link);
1550
1551         list_add_tail(&mmc->link, &mmc_devices);
1552
1553         return mmc;
1554 }
1555
1556 void mmc_destroy(struct mmc *mmc)
1557 {
1558         /* only freeing memory for now */
1559         free(mmc);
1560 }
1561
1562 #ifdef CONFIG_PARTITIONS
1563 block_dev_desc_t *mmc_get_dev(int dev)
1564 {
1565         struct mmc *mmc = find_mmc_device(dev);
1566         if (!mmc || mmc_init(mmc))
1567                 return NULL;
1568
1569         return &mmc->block_dev;
1570 }
1571 #endif
1572
1573 /* board-specific MMC power initializations. */
1574 __weak void board_mmc_power_init(void)
1575 {
1576 }
1577
1578 int mmc_start_init(struct mmc *mmc)
1579 {
1580         int err;
1581
1582         /* we pretend there's no card when init is NULL */
1583         if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
1584                 mmc->has_init = 0;
1585 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1586                 printf("MMC: no card present\n");
1587 #endif
1588                 return NO_CARD_ERR;
1589         }
1590
1591         if (mmc->has_init)
1592                 return 0;
1593
1594         board_mmc_power_init();
1595
1596         /* made sure it's not NULL earlier */
1597         err = mmc->cfg->ops->init(mmc);
1598
1599         if (err)
1600                 return err;
1601
1602         mmc->ddr_mode = 0;
1603         mmc_set_bus_width(mmc, 1);
1604         mmc_set_clock(mmc, 1);
1605
1606         /* Reset the Card */
1607         err = mmc_go_idle(mmc);
1608
1609         if (err)
1610                 return err;
1611
1612         /* The internal partition reset to user partition(0) at every CMD0*/
1613         mmc->part_num = 0;
1614
1615         /* Test for SD version 2 */
1616         err = mmc_send_if_cond(mmc);
1617
1618         /* Now try to get the SD card's operating condition */
1619         err = sd_send_op_cond(mmc);
1620
1621         /* If the command timed out, we check for an MMC card */
1622         if (err == TIMEOUT) {
1623                 err = mmc_send_op_cond(mmc);
1624
1625                 if (err && err != IN_PROGRESS) {
1626 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1627                         printf("Card did not respond to voltage select!\n");
1628 #endif
1629                         return UNUSABLE_ERR;
1630                 }
1631         }
1632
1633         if (err == IN_PROGRESS)
1634                 mmc->init_in_progress = 1;
1635
1636         return err;
1637 }
1638
1639 static int mmc_complete_init(struct mmc *mmc)
1640 {
1641         int err = 0;
1642
1643         if (mmc->op_cond_pending)
1644                 err = mmc_complete_op_cond(mmc);
1645
1646         if (!err)
1647                 err = mmc_startup(mmc);
1648         if (err)
1649                 mmc->has_init = 0;
1650         else
1651                 mmc->has_init = 1;
1652         mmc->init_in_progress = 0;
1653         return err;
1654 }
1655
1656 int mmc_init(struct mmc *mmc)
1657 {
1658         int err = IN_PROGRESS;
1659         unsigned start;
1660
1661         if (mmc->has_init)
1662                 return 0;
1663
1664         start = get_timer(0);
1665
1666         if (!mmc->init_in_progress)
1667                 err = mmc_start_init(mmc);
1668
1669         if (!err || err == IN_PROGRESS)
1670                 err = mmc_complete_init(mmc);
1671         debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1672         return err;
1673 }
1674
1675 int mmc_set_dsr(struct mmc *mmc, u16 val)
1676 {
1677         mmc->dsr = val;
1678         return 0;
1679 }
1680
1681 /* CPU-specific MMC initializations */
1682 __weak int cpu_mmc_init(bd_t *bis)
1683 {
1684         return -1;
1685 }
1686
1687 /* board-specific MMC initializations. */
1688 __weak int board_mmc_init(bd_t *bis)
1689 {
1690         return -1;
1691 }
1692
1693 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1694
1695 void print_mmc_devices(char separator)
1696 {
1697         struct mmc *m;
1698         struct list_head *entry;
1699
1700         list_for_each(entry, &mmc_devices) {
1701                 m = list_entry(entry, struct mmc, link);
1702
1703                 printf("%s: %d", m->cfg->name, m->block_dev.dev);
1704
1705                 if (entry->next != &mmc_devices) {
1706                         printf("%c", separator);
1707                         if (separator != '\n')
1708                                 puts (" ");
1709                 }
1710         }
1711
1712         printf("\n");
1713 }
1714
1715 #else
1716 void print_mmc_devices(char separator) { }
1717 #endif
1718
1719 int get_mmc_num(void)
1720 {
1721         return cur_dev_num;
1722 }
1723
1724 void mmc_set_preinit(struct mmc *mmc, int preinit)
1725 {
1726         mmc->preinit = preinit;
1727 }
1728
1729 static void do_preinit(void)
1730 {
1731         struct mmc *m;
1732         struct list_head *entry;
1733
1734         list_for_each(entry, &mmc_devices) {
1735                 m = list_entry(entry, struct mmc, link);
1736
1737                 if (m->preinit)
1738                         mmc_start_init(m);
1739         }
1740 }
1741
1742
1743 int mmc_initialize(bd_t *bis)
1744 {
1745         INIT_LIST_HEAD (&mmc_devices);
1746         cur_dev_num = 0;
1747
1748         if (board_mmc_init(bis) < 0)
1749                 cpu_mmc_init(bis);
1750
1751 #ifndef CONFIG_SPL_BUILD
1752         print_mmc_devices(',');
1753 #endif
1754
1755         do_preinit();
1756         return 0;
1757 }
1758
1759 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1760 /*
1761  * This function changes the size of boot partition and the size of rpmb
1762  * partition present on EMMC devices.
1763  *
1764  * Input Parameters:
1765  * struct *mmc: pointer for the mmc device strcuture
1766  * bootsize: size of boot partition
1767  * rpmbsize: size of rpmb partition
1768  *
1769  * Returns 0 on success.
1770  */
1771
1772 int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1773                                 unsigned long rpmbsize)
1774 {
1775         int err;
1776         struct mmc_cmd cmd;
1777
1778         /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1779         cmd.cmdidx = MMC_CMD_RES_MAN;
1780         cmd.resp_type = MMC_RSP_R1b;
1781         cmd.cmdarg = MMC_CMD62_ARG1;
1782
1783         err = mmc_send_cmd(mmc, &cmd, NULL);
1784         if (err) {
1785                 debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1786                 return err;
1787         }
1788
1789         /* Boot partition changing mode */
1790         cmd.cmdidx = MMC_CMD_RES_MAN;
1791         cmd.resp_type = MMC_RSP_R1b;
1792         cmd.cmdarg = MMC_CMD62_ARG2;
1793
1794         err = mmc_send_cmd(mmc, &cmd, NULL);
1795         if (err) {
1796                 debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1797                 return err;
1798         }
1799         /* boot partition size is multiple of 128KB */
1800         bootsize = (bootsize * 1024) / 128;
1801
1802         /* Arg: boot partition size */
1803         cmd.cmdidx = MMC_CMD_RES_MAN;
1804         cmd.resp_type = MMC_RSP_R1b;
1805         cmd.cmdarg = bootsize;
1806
1807         err = mmc_send_cmd(mmc, &cmd, NULL);
1808         if (err) {
1809                 debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1810                 return err;
1811         }
1812         /* RPMB partition size is multiple of 128KB */
1813         rpmbsize = (rpmbsize * 1024) / 128;
1814         /* Arg: RPMB partition size */
1815         cmd.cmdidx = MMC_CMD_RES_MAN;
1816         cmd.resp_type = MMC_RSP_R1b;
1817         cmd.cmdarg = rpmbsize;
1818
1819         err = mmc_send_cmd(mmc, &cmd, NULL);
1820         if (err) {
1821                 debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1822                 return err;
1823         }
1824         return 0;
1825 }
1826
1827 /*
1828  * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
1829  * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
1830  * and BOOT_MODE.
1831  *
1832  * Returns 0 on success.
1833  */
1834 int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
1835 {
1836         int err;
1837
1838         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
1839                          EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
1840                          EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
1841                          EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
1842
1843         if (err)
1844                 return err;
1845         return 0;
1846 }
1847
1848 /*
1849  * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1850  * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1851  * PARTITION_ACCESS.
1852  *
1853  * Returns 0 on success.
1854  */
1855 int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1856 {
1857         int err;
1858
1859         err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
1860                          EXT_CSD_BOOT_ACK(ack) |
1861                          EXT_CSD_BOOT_PART_NUM(part_num) |
1862                          EXT_CSD_PARTITION_ACCESS(access));
1863
1864         if (err)
1865                 return err;
1866         return 0;
1867 }
1868
1869 /*
1870  * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
1871  * for enable.  Note that this is a write-once field for non-zero values.
1872  *
1873  * Returns 0 on success.
1874  */
1875 int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
1876 {
1877         return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,
1878                           enable);
1879 }
1880 #endif