2 * Copyright 2008, Freescale Semiconductor, Inc
5 * Based vaguely on the Linux code
7 * SPDX-License-Identifier: GPL-2.0+
16 #include <linux/list.h>
18 #include "mmc_private.h"
20 static struct list_head mmc_devices
;
21 static int cur_dev_num
= -1;
23 int __weak
board_mmc_getwp(struct mmc
*mmc
)
28 int mmc_getwp(struct mmc
*mmc
)
32 wp
= board_mmc_getwp(mmc
);
35 if (mmc
->cfg
->ops
->getwp
)
36 wp
= mmc
->cfg
->ops
->getwp(mmc
);
44 int __board_mmc_getcd(struct mmc
*mmc
) {
48 int board_mmc_getcd(struct mmc
*mmc
)__attribute__((weak
,
49 alias("__board_mmc_getcd")));
51 int mmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
, struct mmc_data
*data
)
55 #ifdef CONFIG_MMC_TRACE
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
) {
64 printf("\t\tMMC_RSP_NONE\n");
67 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
71 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
75 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
77 printf("\t\t \t\t 0x%08X \n",
79 printf("\t\t \t\t 0x%08X \n",
81 printf("\t\t \t\t 0x%08X \n",
84 printf("\t\t\t\t\tDUMPING DATA\n");
85 for (i
= 0; i
< 4; i
++) {
87 printf("\t\t\t\t\t%03d - ", i
*4);
88 ptr
= (u8
*)&cmd
->response
[i
];
90 for (j
= 0; j
< 4; j
++)
91 printf("%02X ", *ptr
--);
96 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
100 printf("\t\tERROR MMC rsp not supported\n");
104 ret
= mmc
->cfg
->ops
->send_cmd(mmc
, cmd
, data
);
109 int mmc_send_status(struct mmc
*mmc
, int timeout
)
112 int err
, retries
= 5;
113 #ifdef CONFIG_MMC_TRACE
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;
123 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
125 if ((cmd
.response
[0] & MMC_STATUS_RDY_FOR_DATA
) &&
126 (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) !=
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",
136 } else if (--retries
< 0)
143 #ifdef CONFIG_MMC_TRACE
144 status
= (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) >> 9;
145 printf("CURR STATE:%d\n", status
);
148 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
149 printf("Timeout waiting card ready\n");
153 if (cmd
.response
[0] & MMC_STATUS_SWITCH_ERROR
)
159 int mmc_set_blocklen(struct mmc
*mmc
, int len
)
163 cmd
.cmdidx
= MMC_CMD_SET_BLOCKLEN
;
164 cmd
.resp_type
= MMC_RSP_R1
;
167 return mmc_send_cmd(mmc
, &cmd
, NULL
);
170 struct mmc
*find_mmc_device(int dev_num
)
173 struct list_head
*entry
;
175 list_for_each(entry
, &mmc_devices
) {
176 m
= list_entry(entry
, struct mmc
, link
);
178 if (m
->block_dev
.dev
== dev_num
)
182 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
183 printf("MMC Device %d not found\n", dev_num
);
189 static int mmc_read_blocks(struct mmc
*mmc
, void *dst
, lbaint_t start
,
193 struct mmc_data data
;
196 cmd
.cmdidx
= MMC_CMD_READ_MULTIPLE_BLOCK
;
198 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
200 if (mmc
->high_capacity
)
203 cmd
.cmdarg
= start
* mmc
->read_bl_len
;
205 cmd
.resp_type
= MMC_RSP_R1
;
208 data
.blocks
= blkcnt
;
209 data
.blocksize
= mmc
->read_bl_len
;
210 data
.flags
= MMC_DATA_READ
;
212 if (mmc_send_cmd(mmc
, &cmd
, &data
))
216 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
218 cmd
.resp_type
= MMC_RSP_R1b
;
219 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
220 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
221 printf("mmc fail to send stop cmd\n");
230 static ulong
mmc_bread(int dev_num
, lbaint_t start
, lbaint_t blkcnt
, void *dst
)
232 lbaint_t cur
, blocks_todo
= blkcnt
;
237 struct mmc
*mmc
= find_mmc_device(dev_num
);
241 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
242 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
243 printf("MMC: block number 0x" LBAF
" exceeds max(0x" LBAF
")\n",
244 start
+ blkcnt
, mmc
->block_dev
.lba
);
249 if (mmc_set_blocklen(mmc
, mmc
->read_bl_len
))
253 cur
= (blocks_todo
> mmc
->cfg
->b_max
) ?
254 mmc
->cfg
->b_max
: blocks_todo
;
255 if(mmc_read_blocks(mmc
, dst
, start
, cur
) != cur
)
259 dst
+= cur
* mmc
->read_bl_len
;
260 } while (blocks_todo
> 0);
265 static int mmc_go_idle(struct mmc
*mmc
)
272 cmd
.cmdidx
= MMC_CMD_GO_IDLE_STATE
;
274 cmd
.resp_type
= MMC_RSP_NONE
;
276 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
286 static int sd_send_op_cond(struct mmc
*mmc
)
293 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
294 cmd
.resp_type
= MMC_RSP_R1
;
297 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
302 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
303 cmd
.resp_type
= MMC_RSP_R3
;
306 * Most cards do not answer if some reserved bits
307 * in the ocr are set. However, Some controller
308 * can set bit 7 (reserved for low voltages), but
309 * how to manage low voltages SD card is not yet
312 cmd
.cmdarg
= mmc_host_is_spi(mmc
) ? 0 :
313 (mmc
->cfg
->voltages
& 0xff8000);
315 if (mmc
->version
== SD_VERSION_2
)
316 cmd
.cmdarg
|= OCR_HCS
;
318 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
324 } while ((!(cmd
.response
[0] & OCR_BUSY
)) && timeout
--);
329 if (mmc
->version
!= SD_VERSION_2
)
330 mmc
->version
= SD_VERSION_1_0
;
332 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
333 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
334 cmd
.resp_type
= MMC_RSP_R3
;
337 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
343 mmc
->ocr
= cmd
.response
[0];
345 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
351 /* We pass in the cmd since otherwise the init seems to fail */
352 static int mmc_send_op_cond_iter(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
357 cmd
->cmdidx
= MMC_CMD_SEND_OP_COND
;
358 cmd
->resp_type
= MMC_RSP_R3
;
360 if (use_arg
&& !mmc_host_is_spi(mmc
)) {
362 (mmc
->cfg
->voltages
&
363 (mmc
->op_cond_response
& OCR_VOLTAGE_MASK
)) |
364 (mmc
->op_cond_response
& OCR_ACCESS_MODE
);
366 if (mmc
->cfg
->host_caps
& MMC_MODE_HC
)
367 cmd
->cmdarg
|= OCR_HCS
;
369 err
= mmc_send_cmd(mmc
, cmd
, NULL
);
372 mmc
->op_cond_response
= cmd
->response
[0];
376 int mmc_send_op_cond(struct mmc
*mmc
)
381 /* Some cards seem to need this */
384 /* Asking to the card its capabilities */
385 mmc
->op_cond_pending
= 1;
386 for (i
= 0; i
< 2; i
++) {
387 err
= mmc_send_op_cond_iter(mmc
, &cmd
, i
!= 0);
391 /* exit if not busy (flag seems to be inverted) */
392 if (mmc
->op_cond_response
& OCR_BUSY
)
398 int mmc_complete_op_cond(struct mmc
*mmc
)
405 mmc
->op_cond_pending
= 0;
406 start
= get_timer(0);
408 err
= mmc_send_op_cond_iter(mmc
, &cmd
, 1);
411 if (get_timer(start
) > timeout
)
414 } while (!(mmc
->op_cond_response
& OCR_BUSY
));
416 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
417 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
418 cmd
.resp_type
= MMC_RSP_R3
;
421 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
427 mmc
->version
= MMC_VERSION_UNKNOWN
;
428 mmc
->ocr
= cmd
.response
[0];
430 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
437 static int mmc_send_ext_csd(struct mmc
*mmc
, u8
*ext_csd
)
440 struct mmc_data data
;
443 /* Get the Card Status Register */
444 cmd
.cmdidx
= MMC_CMD_SEND_EXT_CSD
;
445 cmd
.resp_type
= MMC_RSP_R1
;
448 data
.dest
= (char *)ext_csd
;
450 data
.blocksize
= MMC_MAX_BLOCK_LEN
;
451 data
.flags
= MMC_DATA_READ
;
453 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
459 static int mmc_switch(struct mmc
*mmc
, u8 set
, u8 index
, u8 value
)
465 cmd
.cmdidx
= MMC_CMD_SWITCH
;
466 cmd
.resp_type
= MMC_RSP_R1b
;
467 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
471 ret
= mmc_send_cmd(mmc
, &cmd
, NULL
);
473 /* Waiting for the ready status */
475 ret
= mmc_send_status(mmc
, timeout
);
481 static int mmc_change_freq(struct mmc
*mmc
)
483 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
489 if (mmc_host_is_spi(mmc
))
492 /* Only version 4 supports high-speed */
493 if (mmc
->version
< MMC_VERSION_4
)
496 err
= mmc_send_ext_csd(mmc
, ext_csd
);
501 cardtype
= ext_csd
[EXT_CSD_CARD_TYPE
] & 0xf;
503 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
, 1);
506 return err
== SWITCH_ERR
? 0 : err
;
508 /* Now check to see that it worked */
509 err
= mmc_send_ext_csd(mmc
, ext_csd
);
514 /* No high-speed support */
515 if (!ext_csd
[EXT_CSD_HS_TIMING
])
518 /* High Speed is set, there are two types: 52MHz and 26MHz */
519 if (cardtype
& MMC_HS_52MHZ
)
520 mmc
->card_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
522 mmc
->card_caps
|= MMC_MODE_HS
;
527 static int mmc_set_capacity(struct mmc
*mmc
, int part_num
)
531 mmc
->capacity
= mmc
->capacity_user
;
535 mmc
->capacity
= mmc
->capacity_boot
;
538 mmc
->capacity
= mmc
->capacity_rpmb
;
544 mmc
->capacity
= mmc
->capacity_gp
[part_num
- 4];
550 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
555 int mmc_select_hwpart(int dev_num
, int hwpart
)
557 struct mmc
*mmc
= find_mmc_device(dev_num
);
563 if (mmc
->part_num
== hwpart
)
566 if (mmc
->part_config
== MMCPART_NOAVAILABLE
) {
567 printf("Card doesn't support part_switch\n");
571 ret
= mmc_switch_part(dev_num
, hwpart
);
575 mmc
->part_num
= hwpart
;
581 int mmc_switch_part(int dev_num
, unsigned int part_num
)
583 struct mmc
*mmc
= find_mmc_device(dev_num
);
589 ret
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
590 (mmc
->part_config
& ~PART_ACCESS_MASK
)
591 | (part_num
& PART_ACCESS_MASK
));
595 return mmc_set_capacity(mmc
, part_num
);
598 int mmc_getcd(struct mmc
*mmc
)
602 cd
= board_mmc_getcd(mmc
);
605 if (mmc
->cfg
->ops
->getcd
)
606 cd
= mmc
->cfg
->ops
->getcd(mmc
);
614 static int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
617 struct mmc_data data
;
619 /* Switch the frequency */
620 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
621 cmd
.resp_type
= MMC_RSP_R1
;
622 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
623 cmd
.cmdarg
&= ~(0xf << (group
* 4));
624 cmd
.cmdarg
|= value
<< (group
* 4);
626 data
.dest
= (char *)resp
;
629 data
.flags
= MMC_DATA_READ
;
631 return mmc_send_cmd(mmc
, &cmd
, &data
);
635 static int sd_change_freq(struct mmc
*mmc
)
639 ALLOC_CACHE_ALIGN_BUFFER(uint
, scr
, 2);
640 ALLOC_CACHE_ALIGN_BUFFER(uint
, switch_status
, 16);
641 struct mmc_data data
;
646 if (mmc_host_is_spi(mmc
))
649 /* Read the SCR to find out if this card supports higher speeds */
650 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
651 cmd
.resp_type
= MMC_RSP_R1
;
652 cmd
.cmdarg
= mmc
->rca
<< 16;
654 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
659 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
660 cmd
.resp_type
= MMC_RSP_R1
;
666 data
.dest
= (char *)scr
;
669 data
.flags
= MMC_DATA_READ
;
671 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
680 mmc
->scr
[0] = __be32_to_cpu(scr
[0]);
681 mmc
->scr
[1] = __be32_to_cpu(scr
[1]);
683 switch ((mmc
->scr
[0] >> 24) & 0xf) {
685 mmc
->version
= SD_VERSION_1_0
;
688 mmc
->version
= SD_VERSION_1_10
;
691 mmc
->version
= SD_VERSION_2
;
692 if ((mmc
->scr
[0] >> 15) & 0x1)
693 mmc
->version
= SD_VERSION_3
;
696 mmc
->version
= SD_VERSION_1_0
;
700 if (mmc
->scr
[0] & SD_DATA_4BIT
)
701 mmc
->card_caps
|= MMC_MODE_4BIT
;
703 /* Version 1.0 doesn't support switching */
704 if (mmc
->version
== SD_VERSION_1_0
)
709 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
710 (u8
*)switch_status
);
715 /* The high-speed function is busy. Try again */
716 if (!(__be32_to_cpu(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
720 /* If high-speed isn't supported, we return */
721 if (!(__be32_to_cpu(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
))
725 * If the host doesn't support SD_HIGHSPEED, do not switch card to
726 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
727 * This can avoid furthur problem when the card runs in different
728 * mode between the host.
730 if (!((mmc
->cfg
->host_caps
& MMC_MODE_HS_52MHz
) &&
731 (mmc
->cfg
->host_caps
& MMC_MODE_HS
)))
734 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)switch_status
);
739 if ((__be32_to_cpu(switch_status
[4]) & 0x0f000000) == 0x01000000)
740 mmc
->card_caps
|= MMC_MODE_HS
;
745 /* frequency bases */
746 /* divided by 10 to be nice to platforms without floating point */
747 static const int fbase
[] = {
754 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
755 * to platforms without floating point.
757 static const int multipliers
[] = {
776 static void mmc_set_ios(struct mmc
*mmc
)
778 if (mmc
->cfg
->ops
->set_ios
)
779 mmc
->cfg
->ops
->set_ios(mmc
);
782 void mmc_set_clock(struct mmc
*mmc
, uint clock
)
784 if (clock
> mmc
->cfg
->f_max
)
785 clock
= mmc
->cfg
->f_max
;
787 if (clock
< mmc
->cfg
->f_min
)
788 clock
= mmc
->cfg
->f_min
;
795 static void mmc_set_bus_width(struct mmc
*mmc
, uint width
)
797 mmc
->bus_width
= width
;
802 static int mmc_startup(struct mmc
*mmc
)
806 u64 cmult
, csize
, capacity
;
808 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
809 ALLOC_CACHE_ALIGN_BUFFER(u8
, test_csd
, MMC_MAX_BLOCK_LEN
);
812 #ifdef CONFIG_MMC_SPI_CRC_ON
813 if (mmc_host_is_spi(mmc
)) { /* enable CRC check for spi */
814 cmd
.cmdidx
= MMC_CMD_SPI_CRC_ON_OFF
;
815 cmd
.resp_type
= MMC_RSP_R1
;
817 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
824 /* Put the Card in Identify Mode */
825 cmd
.cmdidx
= mmc_host_is_spi(mmc
) ? MMC_CMD_SEND_CID
:
826 MMC_CMD_ALL_SEND_CID
; /* cmd not supported in spi */
827 cmd
.resp_type
= MMC_RSP_R2
;
830 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
835 memcpy(mmc
->cid
, cmd
.response
, 16);
838 * For MMC cards, set the Relative Address.
839 * For SD cards, get the Relatvie Address.
840 * This also puts the cards into Standby State
842 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
843 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
844 cmd
.cmdarg
= mmc
->rca
<< 16;
845 cmd
.resp_type
= MMC_RSP_R6
;
847 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
853 mmc
->rca
= (cmd
.response
[0] >> 16) & 0xffff;
856 /* Get the Card-Specific Data */
857 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
858 cmd
.resp_type
= MMC_RSP_R2
;
859 cmd
.cmdarg
= mmc
->rca
<< 16;
861 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
863 /* Waiting for the ready status */
864 mmc_send_status(mmc
, timeout
);
869 mmc
->csd
[0] = cmd
.response
[0];
870 mmc
->csd
[1] = cmd
.response
[1];
871 mmc
->csd
[2] = cmd
.response
[2];
872 mmc
->csd
[3] = cmd
.response
[3];
874 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
875 int version
= (cmd
.response
[0] >> 26) & 0xf;
879 mmc
->version
= MMC_VERSION_1_2
;
882 mmc
->version
= MMC_VERSION_1_4
;
885 mmc
->version
= MMC_VERSION_2_2
;
888 mmc
->version
= MMC_VERSION_3
;
891 mmc
->version
= MMC_VERSION_4
;
894 mmc
->version
= MMC_VERSION_1_2
;
899 /* divide frequency by 10, since the mults are 10x bigger */
900 freq
= fbase
[(cmd
.response
[0] & 0x7)];
901 mult
= multipliers
[((cmd
.response
[0] >> 3) & 0xf)];
903 mmc
->tran_speed
= freq
* mult
;
905 mmc
->dsr_imp
= ((cmd
.response
[1] >> 12) & 0x1);
906 mmc
->read_bl_len
= 1 << ((cmd
.response
[1] >> 16) & 0xf);
909 mmc
->write_bl_len
= mmc
->read_bl_len
;
911 mmc
->write_bl_len
= 1 << ((cmd
.response
[3] >> 22) & 0xf);
913 if (mmc
->high_capacity
) {
914 csize
= (mmc
->csd
[1] & 0x3f) << 16
915 | (mmc
->csd
[2] & 0xffff0000) >> 16;
918 csize
= (mmc
->csd
[1] & 0x3ff) << 2
919 | (mmc
->csd
[2] & 0xc0000000) >> 30;
920 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
923 mmc
->capacity_user
= (csize
+ 1) << (cmult
+ 2);
924 mmc
->capacity_user
*= mmc
->read_bl_len
;
925 mmc
->capacity_boot
= 0;
926 mmc
->capacity_rpmb
= 0;
927 for (i
= 0; i
< 4; i
++)
928 mmc
->capacity_gp
[i
] = 0;
930 if (mmc
->read_bl_len
> MMC_MAX_BLOCK_LEN
)
931 mmc
->read_bl_len
= MMC_MAX_BLOCK_LEN
;
933 if (mmc
->write_bl_len
> MMC_MAX_BLOCK_LEN
)
934 mmc
->write_bl_len
= MMC_MAX_BLOCK_LEN
;
936 if ((mmc
->dsr_imp
) && (0xffffffff != mmc
->dsr
)) {
937 cmd
.cmdidx
= MMC_CMD_SET_DSR
;
938 cmd
.cmdarg
= (mmc
->dsr
& 0xffff) << 16;
939 cmd
.resp_type
= MMC_RSP_NONE
;
940 if (mmc_send_cmd(mmc
, &cmd
, NULL
))
941 printf("MMC: SET_DSR failed\n");
944 /* Select the card, and put it into Transfer Mode */
945 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
946 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
947 cmd
.resp_type
= MMC_RSP_R1
;
948 cmd
.cmdarg
= mmc
->rca
<< 16;
949 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
956 * For SD, its erase group is always one sector
958 mmc
->erase_grp_size
= 1;
959 mmc
->part_config
= MMCPART_NOAVAILABLE
;
960 if (!IS_SD(mmc
) && (mmc
->version
>= MMC_VERSION_4
)) {
961 /* check ext_csd version and capacity */
962 err
= mmc_send_ext_csd(mmc
, ext_csd
);
963 if (!err
&& (ext_csd
[EXT_CSD_REV
] >= 2)) {
965 * According to the JEDEC Standard, the value of
966 * ext_csd's capacity is valid if the value is more
969 capacity
= ext_csd
[EXT_CSD_SEC_CNT
] << 0
970 | ext_csd
[EXT_CSD_SEC_CNT
+ 1] << 8
971 | ext_csd
[EXT_CSD_SEC_CNT
+ 2] << 16
972 | ext_csd
[EXT_CSD_SEC_CNT
+ 3] << 24;
973 capacity
*= MMC_MAX_BLOCK_LEN
;
974 if ((capacity
>> 20) > 2 * 1024)
975 mmc
->capacity_user
= capacity
;
978 switch (ext_csd
[EXT_CSD_REV
]) {
980 mmc
->version
= MMC_VERSION_4_1
;
983 mmc
->version
= MMC_VERSION_4_2
;
986 mmc
->version
= MMC_VERSION_4_3
;
989 mmc
->version
= MMC_VERSION_4_41
;
992 mmc
->version
= MMC_VERSION_4_5
;
997 * Host needs to enable ERASE_GRP_DEF bit if device is
998 * partitioned. This bit will be lost every time after a reset
999 * or power off. This will affect erase size.
1001 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) &&
1002 (ext_csd
[EXT_CSD_PARTITIONS_ATTRIBUTE
] & PART_ENH_ATTRIB
)) {
1003 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1004 EXT_CSD_ERASE_GROUP_DEF
, 1);
1009 /* Read out group size from ext_csd */
1010 mmc
->erase_grp_size
=
1011 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] *
1012 MMC_MAX_BLOCK_LEN
* 1024;
1014 /* Calculate the group size from the csd value. */
1015 int erase_gsz
, erase_gmul
;
1016 erase_gsz
= (mmc
->csd
[2] & 0x00007c00) >> 10;
1017 erase_gmul
= (mmc
->csd
[2] & 0x000003e0) >> 5;
1018 mmc
->erase_grp_size
= (erase_gsz
+ 1)
1022 /* store the partition info of emmc */
1023 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) ||
1024 ext_csd
[EXT_CSD_BOOT_MULT
])
1025 mmc
->part_config
= ext_csd
[EXT_CSD_PART_CONF
];
1027 mmc
->capacity_boot
= ext_csd
[EXT_CSD_BOOT_MULT
] << 17;
1029 mmc
->capacity_rpmb
= ext_csd
[EXT_CSD_RPMB_MULT
] << 17;
1031 for (i
= 0; i
< 4; i
++) {
1032 int idx
= EXT_CSD_GP_SIZE_MULT
+ i
* 3;
1033 mmc
->capacity_gp
[i
] = (ext_csd
[idx
+ 2] << 16) +
1034 (ext_csd
[idx
+ 1] << 8) + ext_csd
[idx
];
1035 mmc
->capacity_gp
[i
] *=
1036 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
];
1037 mmc
->capacity_gp
[i
] *= ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
];
1041 err
= mmc_set_capacity(mmc
, mmc
->part_num
);
1046 err
= sd_change_freq(mmc
);
1048 err
= mmc_change_freq(mmc
);
1053 /* Restrict card's capabilities by what the host can do */
1054 mmc
->card_caps
&= mmc
->cfg
->host_caps
;
1057 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
1058 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
1059 cmd
.resp_type
= MMC_RSP_R1
;
1060 cmd
.cmdarg
= mmc
->rca
<< 16;
1062 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1066 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
1067 cmd
.resp_type
= MMC_RSP_R1
;
1069 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1073 mmc_set_bus_width(mmc
, 4);
1076 if (mmc
->card_caps
& MMC_MODE_HS
)
1077 mmc
->tran_speed
= 50000000;
1079 mmc
->tran_speed
= 25000000;
1083 /* An array of possible bus widths in order of preference */
1084 static unsigned ext_csd_bits
[] = {
1085 EXT_CSD_BUS_WIDTH_8
,
1086 EXT_CSD_BUS_WIDTH_4
,
1087 EXT_CSD_BUS_WIDTH_1
,
1090 /* An array to map CSD bus widths to host cap bits */
1091 static unsigned ext_to_hostcaps
[] = {
1092 [EXT_CSD_BUS_WIDTH_4
] = MMC_MODE_4BIT
,
1093 [EXT_CSD_BUS_WIDTH_8
] = MMC_MODE_8BIT
,
1096 /* An array to map chosen bus width to an integer */
1097 static unsigned widths
[] = {
1101 for (idx
=0; idx
< ARRAY_SIZE(ext_csd_bits
); idx
++) {
1102 unsigned int extw
= ext_csd_bits
[idx
];
1105 * Check to make sure the controller supports
1106 * this bus width, if it's more than 1
1108 if (extw
!= EXT_CSD_BUS_WIDTH_1
&&
1109 !(mmc
->cfg
->host_caps
& ext_to_hostcaps
[extw
]))
1112 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1113 EXT_CSD_BUS_WIDTH
, extw
);
1118 mmc_set_bus_width(mmc
, widths
[idx
]);
1120 err
= mmc_send_ext_csd(mmc
, test_csd
);
1121 if (!err
&& ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] \
1122 == test_csd
[EXT_CSD_PARTITIONING_SUPPORT
]
1123 && ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1124 == test_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1125 && ext_csd
[EXT_CSD_REV
] \
1126 == test_csd
[EXT_CSD_REV
]
1127 && ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] \
1128 == test_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
]
1129 && memcmp(&ext_csd
[EXT_CSD_SEC_CNT
], \
1130 &test_csd
[EXT_CSD_SEC_CNT
], 4) == 0) {
1132 mmc
->card_caps
|= ext_to_hostcaps
[extw
];
1137 if (mmc
->card_caps
& MMC_MODE_HS
) {
1138 if (mmc
->card_caps
& MMC_MODE_HS_52MHz
)
1139 mmc
->tran_speed
= 52000000;
1141 mmc
->tran_speed
= 26000000;
1145 mmc_set_clock(mmc
, mmc
->tran_speed
);
1147 /* fill in device description */
1148 mmc
->block_dev
.lun
= 0;
1149 mmc
->block_dev
.type
= 0;
1150 mmc
->block_dev
.blksz
= mmc
->read_bl_len
;
1151 mmc
->block_dev
.log2blksz
= LOG2(mmc
->block_dev
.blksz
);
1152 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
1153 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1154 sprintf(mmc
->block_dev
.vendor
, "Man %06x Snr %04x%04x",
1155 mmc
->cid
[0] >> 24, (mmc
->cid
[2] & 0xffff),
1156 (mmc
->cid
[3] >> 16) & 0xffff);
1157 sprintf(mmc
->block_dev
.product
, "%c%c%c%c%c%c", mmc
->cid
[0] & 0xff,
1158 (mmc
->cid
[1] >> 24), (mmc
->cid
[1] >> 16) & 0xff,
1159 (mmc
->cid
[1] >> 8) & 0xff, mmc
->cid
[1] & 0xff,
1160 (mmc
->cid
[2] >> 24) & 0xff);
1161 sprintf(mmc
->block_dev
.revision
, "%d.%d", (mmc
->cid
[2] >> 20) & 0xf,
1162 (mmc
->cid
[2] >> 16) & 0xf);
1164 mmc
->block_dev
.vendor
[0] = 0;
1165 mmc
->block_dev
.product
[0] = 0;
1166 mmc
->block_dev
.revision
[0] = 0;
1168 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1169 init_part(&mmc
->block_dev
);
1175 static int mmc_send_if_cond(struct mmc
*mmc
)
1180 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
1181 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1182 cmd
.cmdarg
= ((mmc
->cfg
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
1183 cmd
.resp_type
= MMC_RSP_R7
;
1185 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1190 if ((cmd
.response
[0] & 0xff) != 0xaa)
1191 return UNUSABLE_ERR
;
1193 mmc
->version
= SD_VERSION_2
;
1198 /* not used any more */
1199 int __deprecated
mmc_register(struct mmc
*mmc
)
1201 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1202 printf("%s is deprecated! use mmc_create() instead.\n", __func__
);
1207 struct mmc
*mmc_create(const struct mmc_config
*cfg
, void *priv
)
1211 /* quick validation */
1212 if (cfg
== NULL
|| cfg
->ops
== NULL
|| cfg
->ops
->send_cmd
== NULL
||
1213 cfg
->f_min
== 0 || cfg
->f_max
== 0 || cfg
->b_max
== 0)
1216 mmc
= calloc(1, sizeof(*mmc
));
1223 /* the following chunk was mmc_register() */
1225 /* Setup dsr related values */
1227 mmc
->dsr
= 0xffffffff;
1228 /* Setup the universal parts of the block interface just once */
1229 mmc
->block_dev
.if_type
= IF_TYPE_MMC
;
1230 mmc
->block_dev
.dev
= cur_dev_num
++;
1231 mmc
->block_dev
.removable
= 1;
1232 mmc
->block_dev
.block_read
= mmc_bread
;
1233 mmc
->block_dev
.block_write
= mmc_bwrite
;
1234 mmc
->block_dev
.block_erase
= mmc_berase
;
1236 /* setup initial part type */
1237 mmc
->block_dev
.part_type
= mmc
->cfg
->part_type
;
1239 INIT_LIST_HEAD(&mmc
->link
);
1241 list_add_tail(&mmc
->link
, &mmc_devices
);
1246 void mmc_destroy(struct mmc
*mmc
)
1248 /* only freeing memory for now */
1252 #ifdef CONFIG_PARTITIONS
1253 block_dev_desc_t
*mmc_get_dev(int dev
)
1255 struct mmc
*mmc
= find_mmc_device(dev
);
1256 if (!mmc
|| mmc_init(mmc
))
1259 return &mmc
->block_dev
;
1263 int mmc_start_init(struct mmc
*mmc
)
1267 /* we pretend there's no card when init is NULL */
1268 if (mmc_getcd(mmc
) == 0 || mmc
->cfg
->ops
->init
== NULL
) {
1270 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1271 printf("MMC: no card present\n");
1279 /* made sure it's not NULL earlier */
1280 err
= mmc
->cfg
->ops
->init(mmc
);
1285 mmc_set_bus_width(mmc
, 1);
1286 mmc_set_clock(mmc
, 1);
1288 /* Reset the Card */
1289 err
= mmc_go_idle(mmc
);
1294 /* The internal partition reset to user partition(0) at every CMD0*/
1297 /* Test for SD version 2 */
1298 err
= mmc_send_if_cond(mmc
);
1300 /* Now try to get the SD card's operating condition */
1301 err
= sd_send_op_cond(mmc
);
1303 /* If the command timed out, we check for an MMC card */
1304 if (err
== TIMEOUT
) {
1305 err
= mmc_send_op_cond(mmc
);
1307 if (err
&& err
!= IN_PROGRESS
) {
1308 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1309 printf("Card did not respond to voltage select!\n");
1311 return UNUSABLE_ERR
;
1315 if (err
== IN_PROGRESS
)
1316 mmc
->init_in_progress
= 1;
1321 static int mmc_complete_init(struct mmc
*mmc
)
1325 if (mmc
->op_cond_pending
)
1326 err
= mmc_complete_op_cond(mmc
);
1329 err
= mmc_startup(mmc
);
1334 mmc
->init_in_progress
= 0;
1338 int mmc_init(struct mmc
*mmc
)
1340 int err
= IN_PROGRESS
;
1346 start
= get_timer(0);
1348 if (!mmc
->init_in_progress
)
1349 err
= mmc_start_init(mmc
);
1351 if (!err
|| err
== IN_PROGRESS
)
1352 err
= mmc_complete_init(mmc
);
1353 debug("%s: %d, time %lu\n", __func__
, err
, get_timer(start
));
1357 int mmc_set_dsr(struct mmc
*mmc
, u16 val
)
1364 * CPU and board-specific MMC initializations. Aliased function
1365 * signals caller to move on
1367 static int __def_mmc_init(bd_t
*bis
)
1372 int cpu_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1373 int board_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1375 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1377 void print_mmc_devices(char separator
)
1380 struct list_head
*entry
;
1382 list_for_each(entry
, &mmc_devices
) {
1383 m
= list_entry(entry
, struct mmc
, link
);
1385 printf("%s: %d", m
->cfg
->name
, m
->block_dev
.dev
);
1387 if (entry
->next
!= &mmc_devices
)
1388 printf("%c ", separator
);
1395 void print_mmc_devices(char separator
) { }
1398 int get_mmc_num(void)
1403 void mmc_set_preinit(struct mmc
*mmc
, int preinit
)
1405 mmc
->preinit
= preinit
;
1408 static void do_preinit(void)
1411 struct list_head
*entry
;
1413 list_for_each(entry
, &mmc_devices
) {
1414 m
= list_entry(entry
, struct mmc
, link
);
1422 int mmc_initialize(bd_t
*bis
)
1424 INIT_LIST_HEAD (&mmc_devices
);
1427 if (board_mmc_init(bis
) < 0)
1430 #ifndef CONFIG_SPL_BUILD
1431 print_mmc_devices(',');
1438 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1440 * This function changes the size of boot partition and the size of rpmb
1441 * partition present on EMMC devices.
1444 * struct *mmc: pointer for the mmc device strcuture
1445 * bootsize: size of boot partition
1446 * rpmbsize: size of rpmb partition
1448 * Returns 0 on success.
1451 int mmc_boot_partition_size_change(struct mmc
*mmc
, unsigned long bootsize
,
1452 unsigned long rpmbsize
)
1457 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1458 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1459 cmd
.resp_type
= MMC_RSP_R1b
;
1460 cmd
.cmdarg
= MMC_CMD62_ARG1
;
1462 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1464 debug("mmc_boot_partition_size_change: Error1 = %d\n", err
);
1468 /* Boot partition changing mode */
1469 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1470 cmd
.resp_type
= MMC_RSP_R1b
;
1471 cmd
.cmdarg
= MMC_CMD62_ARG2
;
1473 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1475 debug("mmc_boot_partition_size_change: Error2 = %d\n", err
);
1478 /* boot partition size is multiple of 128KB */
1479 bootsize
= (bootsize
* 1024) / 128;
1481 /* Arg: boot partition size */
1482 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1483 cmd
.resp_type
= MMC_RSP_R1b
;
1484 cmd
.cmdarg
= bootsize
;
1486 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1488 debug("mmc_boot_partition_size_change: Error3 = %d\n", err
);
1491 /* RPMB partition size is multiple of 128KB */
1492 rpmbsize
= (rpmbsize
* 1024) / 128;
1493 /* Arg: RPMB partition size */
1494 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1495 cmd
.resp_type
= MMC_RSP_R1b
;
1496 cmd
.cmdarg
= rpmbsize
;
1498 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1500 debug("mmc_boot_partition_size_change: Error4 = %d\n", err
);
1507 * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
1508 * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
1511 * Returns 0 on success.
1513 int mmc_set_boot_bus_width(struct mmc
*mmc
, u8 width
, u8 reset
, u8 mode
)
1517 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_BOOT_BUS_WIDTH
,
1518 EXT_CSD_BOOT_BUS_WIDTH_MODE(mode
) |
1519 EXT_CSD_BOOT_BUS_WIDTH_RESET(reset
) |
1520 EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width
));
1528 * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1529 * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1532 * Returns 0 on success.
1534 int mmc_set_part_conf(struct mmc
*mmc
, u8 ack
, u8 part_num
, u8 access
)
1538 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
1539 EXT_CSD_BOOT_ACK(ack
) |
1540 EXT_CSD_BOOT_PART_NUM(part_num
) |
1541 EXT_CSD_PARTITION_ACCESS(access
));
1549 * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
1550 * for enable. Note that this is a write-once field for non-zero values.
1552 * Returns 0 on success.
1554 int mmc_set_rst_n_function(struct mmc
*mmc
, u8 enable
)
1556 return mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_RST_N_FUNCTION
,