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