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 if (mmc
->card_caps
& MMC_MODE_DDR_52MHz
)
166 cmd
.cmdidx
= MMC_CMD_SET_BLOCKLEN
;
167 cmd
.resp_type
= MMC_RSP_R1
;
170 return mmc_send_cmd(mmc
, &cmd
, NULL
);
173 struct mmc
*find_mmc_device(int dev_num
)
176 struct list_head
*entry
;
178 list_for_each(entry
, &mmc_devices
) {
179 m
= list_entry(entry
, struct mmc
, link
);
181 if (m
->block_dev
.dev
== dev_num
)
185 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
186 printf("MMC Device %d not found\n", dev_num
);
192 static int mmc_read_blocks(struct mmc
*mmc
, void *dst
, lbaint_t start
,
196 struct mmc_data data
;
199 cmd
.cmdidx
= MMC_CMD_READ_MULTIPLE_BLOCK
;
201 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
203 if (mmc
->high_capacity
)
206 cmd
.cmdarg
= start
* mmc
->read_bl_len
;
208 cmd
.resp_type
= MMC_RSP_R1
;
211 data
.blocks
= blkcnt
;
212 data
.blocksize
= mmc
->read_bl_len
;
213 data
.flags
= MMC_DATA_READ
;
215 if (mmc_send_cmd(mmc
, &cmd
, &data
))
219 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
221 cmd
.resp_type
= MMC_RSP_R1b
;
222 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
223 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
224 printf("mmc fail to send stop cmd\n");
233 static ulong
mmc_bread(int dev_num
, lbaint_t start
, lbaint_t blkcnt
, void *dst
)
235 lbaint_t cur
, blocks_todo
= blkcnt
;
240 struct mmc
*mmc
= find_mmc_device(dev_num
);
244 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
245 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
246 printf("MMC: block number 0x" LBAF
" exceeds max(0x" LBAF
")\n",
247 start
+ blkcnt
, mmc
->block_dev
.lba
);
252 if (mmc_set_blocklen(mmc
, mmc
->read_bl_len
))
256 cur
= (blocks_todo
> mmc
->cfg
->b_max
) ?
257 mmc
->cfg
->b_max
: blocks_todo
;
258 if(mmc_read_blocks(mmc
, dst
, start
, cur
) != cur
)
262 dst
+= cur
* mmc
->read_bl_len
;
263 } while (blocks_todo
> 0);
268 static int mmc_go_idle(struct mmc
*mmc
)
275 cmd
.cmdidx
= MMC_CMD_GO_IDLE_STATE
;
277 cmd
.resp_type
= MMC_RSP_NONE
;
279 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
289 static int sd_send_op_cond(struct mmc
*mmc
)
296 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
297 cmd
.resp_type
= MMC_RSP_R1
;
300 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
305 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
306 cmd
.resp_type
= MMC_RSP_R3
;
309 * Most cards do not answer if some reserved bits
310 * in the ocr are set. However, Some controller
311 * can set bit 7 (reserved for low voltages), but
312 * how to manage low voltages SD card is not yet
315 cmd
.cmdarg
= mmc_host_is_spi(mmc
) ? 0 :
316 (mmc
->cfg
->voltages
& 0xff8000);
318 if (mmc
->version
== SD_VERSION_2
)
319 cmd
.cmdarg
|= OCR_HCS
;
321 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
327 } while ((!(cmd
.response
[0] & OCR_BUSY
)) && timeout
--);
332 if (mmc
->version
!= SD_VERSION_2
)
333 mmc
->version
= SD_VERSION_1_0
;
335 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
336 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
337 cmd
.resp_type
= MMC_RSP_R3
;
340 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
346 mmc
->ocr
= cmd
.response
[0];
348 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
354 /* We pass in the cmd since otherwise the init seems to fail */
355 static int mmc_send_op_cond_iter(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
360 cmd
->cmdidx
= MMC_CMD_SEND_OP_COND
;
361 cmd
->resp_type
= MMC_RSP_R3
;
363 if (use_arg
&& !mmc_host_is_spi(mmc
)) {
365 (mmc
->cfg
->voltages
&
366 (mmc
->op_cond_response
& OCR_VOLTAGE_MASK
)) |
367 (mmc
->op_cond_response
& OCR_ACCESS_MODE
);
369 if (mmc
->cfg
->host_caps
& MMC_MODE_HC
)
370 cmd
->cmdarg
|= OCR_HCS
;
372 err
= mmc_send_cmd(mmc
, cmd
, NULL
);
375 mmc
->op_cond_response
= cmd
->response
[0];
379 int mmc_send_op_cond(struct mmc
*mmc
)
384 /* Some cards seem to need this */
387 /* Asking to the card its capabilities */
388 mmc
->op_cond_pending
= 1;
389 for (i
= 0; i
< 2; i
++) {
390 err
= mmc_send_op_cond_iter(mmc
, &cmd
, i
!= 0);
394 /* exit if not busy (flag seems to be inverted) */
395 if (mmc
->op_cond_response
& OCR_BUSY
)
401 int mmc_complete_op_cond(struct mmc
*mmc
)
408 mmc
->op_cond_pending
= 0;
409 start
= get_timer(0);
411 err
= mmc_send_op_cond_iter(mmc
, &cmd
, 1);
414 if (get_timer(start
) > timeout
)
417 } while (!(mmc
->op_cond_response
& OCR_BUSY
));
419 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
420 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
421 cmd
.resp_type
= MMC_RSP_R3
;
424 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
430 mmc
->version
= MMC_VERSION_UNKNOWN
;
431 mmc
->ocr
= cmd
.response
[0];
433 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
440 static int mmc_send_ext_csd(struct mmc
*mmc
, u8
*ext_csd
)
443 struct mmc_data data
;
446 /* Get the Card Status Register */
447 cmd
.cmdidx
= MMC_CMD_SEND_EXT_CSD
;
448 cmd
.resp_type
= MMC_RSP_R1
;
451 data
.dest
= (char *)ext_csd
;
453 data
.blocksize
= MMC_MAX_BLOCK_LEN
;
454 data
.flags
= MMC_DATA_READ
;
456 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
462 static int mmc_switch(struct mmc
*mmc
, u8 set
, u8 index
, u8 value
)
468 cmd
.cmdidx
= MMC_CMD_SWITCH
;
469 cmd
.resp_type
= MMC_RSP_R1b
;
470 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
474 ret
= mmc_send_cmd(mmc
, &cmd
, NULL
);
476 /* Waiting for the ready status */
478 ret
= mmc_send_status(mmc
, timeout
);
484 static int mmc_change_freq(struct mmc
*mmc
)
486 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
492 if (mmc_host_is_spi(mmc
))
495 /* Only version 4 supports high-speed */
496 if (mmc
->version
< MMC_VERSION_4
)
499 err
= mmc_send_ext_csd(mmc
, ext_csd
);
504 cardtype
= ext_csd
[EXT_CSD_CARD_TYPE
] & 0xf;
506 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
, 1);
509 return err
== SWITCH_ERR
? 0 : err
;
511 /* Now check to see that it worked */
512 err
= mmc_send_ext_csd(mmc
, ext_csd
);
517 /* No high-speed support */
518 if (!ext_csd
[EXT_CSD_HS_TIMING
])
521 /* High Speed is set, there are two types: 52MHz and 26MHz */
522 if (cardtype
& EXT_CSD_CARD_TYPE_52
) {
523 if (cardtype
& EXT_CSD_CARD_TYPE_DDR_52
)
524 mmc
->card_caps
|= MMC_MODE_DDR_52MHz
;
525 mmc
->card_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
527 mmc
->card_caps
|= MMC_MODE_HS
;
533 static int mmc_set_capacity(struct mmc
*mmc
, int part_num
)
537 mmc
->capacity
= mmc
->capacity_user
;
541 mmc
->capacity
= mmc
->capacity_boot
;
544 mmc
->capacity
= mmc
->capacity_rpmb
;
550 mmc
->capacity
= mmc
->capacity_gp
[part_num
- 4];
556 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
561 int mmc_select_hwpart(int dev_num
, int hwpart
)
563 struct mmc
*mmc
= find_mmc_device(dev_num
);
569 if (mmc
->part_num
== hwpart
)
572 if (mmc
->part_config
== MMCPART_NOAVAILABLE
) {
573 printf("Card doesn't support part_switch\n");
577 ret
= mmc_switch_part(dev_num
, hwpart
);
581 mmc
->part_num
= hwpart
;
587 int mmc_switch_part(int dev_num
, unsigned int part_num
)
589 struct mmc
*mmc
= find_mmc_device(dev_num
);
595 ret
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
596 (mmc
->part_config
& ~PART_ACCESS_MASK
)
597 | (part_num
& PART_ACCESS_MASK
));
601 return mmc_set_capacity(mmc
, part_num
);
604 int mmc_getcd(struct mmc
*mmc
)
608 cd
= board_mmc_getcd(mmc
);
611 if (mmc
->cfg
->ops
->getcd
)
612 cd
= mmc
->cfg
->ops
->getcd(mmc
);
620 static int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
623 struct mmc_data data
;
625 /* Switch the frequency */
626 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
627 cmd
.resp_type
= MMC_RSP_R1
;
628 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
629 cmd
.cmdarg
&= ~(0xf << (group
* 4));
630 cmd
.cmdarg
|= value
<< (group
* 4);
632 data
.dest
= (char *)resp
;
635 data
.flags
= MMC_DATA_READ
;
637 return mmc_send_cmd(mmc
, &cmd
, &data
);
641 static int sd_change_freq(struct mmc
*mmc
)
645 ALLOC_CACHE_ALIGN_BUFFER(uint
, scr
, 2);
646 ALLOC_CACHE_ALIGN_BUFFER(uint
, switch_status
, 16);
647 struct mmc_data data
;
652 if (mmc_host_is_spi(mmc
))
655 /* Read the SCR to find out if this card supports higher speeds */
656 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
657 cmd
.resp_type
= MMC_RSP_R1
;
658 cmd
.cmdarg
= mmc
->rca
<< 16;
660 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
665 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
666 cmd
.resp_type
= MMC_RSP_R1
;
672 data
.dest
= (char *)scr
;
675 data
.flags
= MMC_DATA_READ
;
677 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
686 mmc
->scr
[0] = __be32_to_cpu(scr
[0]);
687 mmc
->scr
[1] = __be32_to_cpu(scr
[1]);
689 switch ((mmc
->scr
[0] >> 24) & 0xf) {
691 mmc
->version
= SD_VERSION_1_0
;
694 mmc
->version
= SD_VERSION_1_10
;
697 mmc
->version
= SD_VERSION_2
;
698 if ((mmc
->scr
[0] >> 15) & 0x1)
699 mmc
->version
= SD_VERSION_3
;
702 mmc
->version
= SD_VERSION_1_0
;
706 if (mmc
->scr
[0] & SD_DATA_4BIT
)
707 mmc
->card_caps
|= MMC_MODE_4BIT
;
709 /* Version 1.0 doesn't support switching */
710 if (mmc
->version
== SD_VERSION_1_0
)
715 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
716 (u8
*)switch_status
);
721 /* The high-speed function is busy. Try again */
722 if (!(__be32_to_cpu(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
726 /* If high-speed isn't supported, we return */
727 if (!(__be32_to_cpu(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
))
731 * If the host doesn't support SD_HIGHSPEED, do not switch card to
732 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
733 * This can avoid furthur problem when the card runs in different
734 * mode between the host.
736 if (!((mmc
->cfg
->host_caps
& MMC_MODE_HS_52MHz
) &&
737 (mmc
->cfg
->host_caps
& MMC_MODE_HS
)))
740 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)switch_status
);
745 if ((__be32_to_cpu(switch_status
[4]) & 0x0f000000) == 0x01000000)
746 mmc
->card_caps
|= MMC_MODE_HS
;
751 /* frequency bases */
752 /* divided by 10 to be nice to platforms without floating point */
753 static const int fbase
[] = {
760 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
761 * to platforms without floating point.
763 static const int multipliers
[] = {
782 static void mmc_set_ios(struct mmc
*mmc
)
784 if (mmc
->cfg
->ops
->set_ios
)
785 mmc
->cfg
->ops
->set_ios(mmc
);
788 void mmc_set_clock(struct mmc
*mmc
, uint clock
)
790 if (clock
> mmc
->cfg
->f_max
)
791 clock
= mmc
->cfg
->f_max
;
793 if (clock
< mmc
->cfg
->f_min
)
794 clock
= mmc
->cfg
->f_min
;
801 static void mmc_set_bus_width(struct mmc
*mmc
, uint width
)
803 mmc
->bus_width
= width
;
808 static int mmc_startup(struct mmc
*mmc
)
812 u64 cmult
, csize
, capacity
;
814 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
815 ALLOC_CACHE_ALIGN_BUFFER(u8
, test_csd
, MMC_MAX_BLOCK_LEN
);
818 #ifdef CONFIG_MMC_SPI_CRC_ON
819 if (mmc_host_is_spi(mmc
)) { /* enable CRC check for spi */
820 cmd
.cmdidx
= MMC_CMD_SPI_CRC_ON_OFF
;
821 cmd
.resp_type
= MMC_RSP_R1
;
823 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
830 /* Put the Card in Identify Mode */
831 cmd
.cmdidx
= mmc_host_is_spi(mmc
) ? MMC_CMD_SEND_CID
:
832 MMC_CMD_ALL_SEND_CID
; /* cmd not supported in spi */
833 cmd
.resp_type
= MMC_RSP_R2
;
836 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
841 memcpy(mmc
->cid
, cmd
.response
, 16);
844 * For MMC cards, set the Relative Address.
845 * For SD cards, get the Relatvie Address.
846 * This also puts the cards into Standby State
848 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
849 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
850 cmd
.cmdarg
= mmc
->rca
<< 16;
851 cmd
.resp_type
= MMC_RSP_R6
;
853 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
859 mmc
->rca
= (cmd
.response
[0] >> 16) & 0xffff;
862 /* Get the Card-Specific Data */
863 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
864 cmd
.resp_type
= MMC_RSP_R2
;
865 cmd
.cmdarg
= mmc
->rca
<< 16;
867 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
869 /* Waiting for the ready status */
870 mmc_send_status(mmc
, timeout
);
875 mmc
->csd
[0] = cmd
.response
[0];
876 mmc
->csd
[1] = cmd
.response
[1];
877 mmc
->csd
[2] = cmd
.response
[2];
878 mmc
->csd
[3] = cmd
.response
[3];
880 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
881 int version
= (cmd
.response
[0] >> 26) & 0xf;
885 mmc
->version
= MMC_VERSION_1_2
;
888 mmc
->version
= MMC_VERSION_1_4
;
891 mmc
->version
= MMC_VERSION_2_2
;
894 mmc
->version
= MMC_VERSION_3
;
897 mmc
->version
= MMC_VERSION_4
;
900 mmc
->version
= MMC_VERSION_1_2
;
905 /* divide frequency by 10, since the mults are 10x bigger */
906 freq
= fbase
[(cmd
.response
[0] & 0x7)];
907 mult
= multipliers
[((cmd
.response
[0] >> 3) & 0xf)];
909 mmc
->tran_speed
= freq
* mult
;
911 mmc
->dsr_imp
= ((cmd
.response
[1] >> 12) & 0x1);
912 mmc
->read_bl_len
= 1 << ((cmd
.response
[1] >> 16) & 0xf);
915 mmc
->write_bl_len
= mmc
->read_bl_len
;
917 mmc
->write_bl_len
= 1 << ((cmd
.response
[3] >> 22) & 0xf);
919 if (mmc
->high_capacity
) {
920 csize
= (mmc
->csd
[1] & 0x3f) << 16
921 | (mmc
->csd
[2] & 0xffff0000) >> 16;
924 csize
= (mmc
->csd
[1] & 0x3ff) << 2
925 | (mmc
->csd
[2] & 0xc0000000) >> 30;
926 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
929 mmc
->capacity_user
= (csize
+ 1) << (cmult
+ 2);
930 mmc
->capacity_user
*= mmc
->read_bl_len
;
931 mmc
->capacity_boot
= 0;
932 mmc
->capacity_rpmb
= 0;
933 for (i
= 0; i
< 4; i
++)
934 mmc
->capacity_gp
[i
] = 0;
936 if (mmc
->read_bl_len
> MMC_MAX_BLOCK_LEN
)
937 mmc
->read_bl_len
= MMC_MAX_BLOCK_LEN
;
939 if (mmc
->write_bl_len
> MMC_MAX_BLOCK_LEN
)
940 mmc
->write_bl_len
= MMC_MAX_BLOCK_LEN
;
942 if ((mmc
->dsr_imp
) && (0xffffffff != mmc
->dsr
)) {
943 cmd
.cmdidx
= MMC_CMD_SET_DSR
;
944 cmd
.cmdarg
= (mmc
->dsr
& 0xffff) << 16;
945 cmd
.resp_type
= MMC_RSP_NONE
;
946 if (mmc_send_cmd(mmc
, &cmd
, NULL
))
947 printf("MMC: SET_DSR failed\n");
950 /* Select the card, and put it into Transfer Mode */
951 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
952 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
953 cmd
.resp_type
= MMC_RSP_R1
;
954 cmd
.cmdarg
= mmc
->rca
<< 16;
955 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
962 * For SD, its erase group is always one sector
964 mmc
->erase_grp_size
= 1;
965 mmc
->part_config
= MMCPART_NOAVAILABLE
;
966 if (!IS_SD(mmc
) && (mmc
->version
>= MMC_VERSION_4
)) {
967 /* check ext_csd version and capacity */
968 err
= mmc_send_ext_csd(mmc
, ext_csd
);
969 if (!err
&& (ext_csd
[EXT_CSD_REV
] >= 2)) {
971 * According to the JEDEC Standard, the value of
972 * ext_csd's capacity is valid if the value is more
975 capacity
= ext_csd
[EXT_CSD_SEC_CNT
] << 0
976 | ext_csd
[EXT_CSD_SEC_CNT
+ 1] << 8
977 | ext_csd
[EXT_CSD_SEC_CNT
+ 2] << 16
978 | ext_csd
[EXT_CSD_SEC_CNT
+ 3] << 24;
979 capacity
*= MMC_MAX_BLOCK_LEN
;
980 if ((capacity
>> 20) > 2 * 1024)
981 mmc
->capacity_user
= capacity
;
984 switch (ext_csd
[EXT_CSD_REV
]) {
986 mmc
->version
= MMC_VERSION_4_1
;
989 mmc
->version
= MMC_VERSION_4_2
;
992 mmc
->version
= MMC_VERSION_4_3
;
995 mmc
->version
= MMC_VERSION_4_41
;
998 mmc
->version
= MMC_VERSION_4_5
;
1003 * Host needs to enable ERASE_GRP_DEF bit if device is
1004 * partitioned. This bit will be lost every time after a reset
1005 * or power off. This will affect erase size.
1007 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) &&
1008 (ext_csd
[EXT_CSD_PARTITIONS_ATTRIBUTE
] & PART_ENH_ATTRIB
)) {
1009 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1010 EXT_CSD_ERASE_GROUP_DEF
, 1);
1015 /* Read out group size from ext_csd */
1016 mmc
->erase_grp_size
=
1017 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] *
1018 MMC_MAX_BLOCK_LEN
* 1024;
1020 /* Calculate the group size from the csd value. */
1021 int erase_gsz
, erase_gmul
;
1022 erase_gsz
= (mmc
->csd
[2] & 0x00007c00) >> 10;
1023 erase_gmul
= (mmc
->csd
[2] & 0x000003e0) >> 5;
1024 mmc
->erase_grp_size
= (erase_gsz
+ 1)
1028 /* store the partition info of emmc */
1029 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) ||
1030 ext_csd
[EXT_CSD_BOOT_MULT
])
1031 mmc
->part_config
= ext_csd
[EXT_CSD_PART_CONF
];
1033 mmc
->capacity_boot
= ext_csd
[EXT_CSD_BOOT_MULT
] << 17;
1035 mmc
->capacity_rpmb
= ext_csd
[EXT_CSD_RPMB_MULT
] << 17;
1037 for (i
= 0; i
< 4; i
++) {
1038 int idx
= EXT_CSD_GP_SIZE_MULT
+ i
* 3;
1039 mmc
->capacity_gp
[i
] = (ext_csd
[idx
+ 2] << 16) +
1040 (ext_csd
[idx
+ 1] << 8) + ext_csd
[idx
];
1041 mmc
->capacity_gp
[i
] *=
1042 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
];
1043 mmc
->capacity_gp
[i
] *= ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
];
1047 err
= mmc_set_capacity(mmc
, mmc
->part_num
);
1052 err
= sd_change_freq(mmc
);
1054 err
= mmc_change_freq(mmc
);
1059 /* Restrict card's capabilities by what the host can do */
1060 mmc
->card_caps
&= mmc
->cfg
->host_caps
;
1063 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
1064 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
1065 cmd
.resp_type
= MMC_RSP_R1
;
1066 cmd
.cmdarg
= mmc
->rca
<< 16;
1068 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1072 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
1073 cmd
.resp_type
= MMC_RSP_R1
;
1075 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1079 mmc_set_bus_width(mmc
, 4);
1082 if (mmc
->card_caps
& MMC_MODE_HS
)
1083 mmc
->tran_speed
= 50000000;
1085 mmc
->tran_speed
= 25000000;
1089 /* An array of possible bus widths in order of preference */
1090 static unsigned ext_csd_bits
[] = {
1091 EXT_CSD_DDR_BUS_WIDTH_8
,
1092 EXT_CSD_DDR_BUS_WIDTH_4
,
1093 EXT_CSD_BUS_WIDTH_8
,
1094 EXT_CSD_BUS_WIDTH_4
,
1095 EXT_CSD_BUS_WIDTH_1
,
1098 /* An array to map CSD bus widths to host cap bits */
1099 static unsigned ext_to_hostcaps
[] = {
1100 [EXT_CSD_DDR_BUS_WIDTH_4
] = MMC_MODE_DDR_52MHz
,
1101 [EXT_CSD_DDR_BUS_WIDTH_8
] = MMC_MODE_DDR_52MHz
,
1102 [EXT_CSD_BUS_WIDTH_4
] = MMC_MODE_4BIT
,
1103 [EXT_CSD_BUS_WIDTH_8
] = MMC_MODE_8BIT
,
1106 /* An array to map chosen bus width to an integer */
1107 static unsigned widths
[] = {
1111 for (idx
=0; idx
< ARRAY_SIZE(ext_csd_bits
); idx
++) {
1112 unsigned int extw
= ext_csd_bits
[idx
];
1115 * Check to make sure the controller supports
1116 * this bus width, if it's more than 1
1118 if (extw
!= EXT_CSD_BUS_WIDTH_1
&&
1119 !(mmc
->cfg
->host_caps
& ext_to_hostcaps
[extw
]))
1122 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1123 EXT_CSD_BUS_WIDTH
, extw
);
1128 mmc_set_bus_width(mmc
, widths
[idx
]);
1130 err
= mmc_send_ext_csd(mmc
, test_csd
);
1131 if (!err
&& ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] \
1132 == test_csd
[EXT_CSD_PARTITIONING_SUPPORT
]
1133 && ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1134 == test_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1135 && ext_csd
[EXT_CSD_REV
] \
1136 == test_csd
[EXT_CSD_REV
]
1137 && ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] \
1138 == test_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
]
1139 && memcmp(&ext_csd
[EXT_CSD_SEC_CNT
], \
1140 &test_csd
[EXT_CSD_SEC_CNT
], 4) == 0) {
1142 mmc
->card_caps
|= ext_to_hostcaps
[extw
];
1147 if (mmc
->card_caps
& MMC_MODE_HS
) {
1148 if (mmc
->card_caps
& MMC_MODE_HS_52MHz
)
1149 mmc
->tran_speed
= 52000000;
1151 mmc
->tran_speed
= 26000000;
1155 mmc_set_clock(mmc
, mmc
->tran_speed
);
1157 /* fill in device description */
1158 mmc
->block_dev
.lun
= 0;
1159 mmc
->block_dev
.type
= 0;
1160 mmc
->block_dev
.blksz
= mmc
->read_bl_len
;
1161 mmc
->block_dev
.log2blksz
= LOG2(mmc
->block_dev
.blksz
);
1162 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
1163 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1164 sprintf(mmc
->block_dev
.vendor
, "Man %06x Snr %04x%04x",
1165 mmc
->cid
[0] >> 24, (mmc
->cid
[2] & 0xffff),
1166 (mmc
->cid
[3] >> 16) & 0xffff);
1167 sprintf(mmc
->block_dev
.product
, "%c%c%c%c%c%c", mmc
->cid
[0] & 0xff,
1168 (mmc
->cid
[1] >> 24), (mmc
->cid
[1] >> 16) & 0xff,
1169 (mmc
->cid
[1] >> 8) & 0xff, mmc
->cid
[1] & 0xff,
1170 (mmc
->cid
[2] >> 24) & 0xff);
1171 sprintf(mmc
->block_dev
.revision
, "%d.%d", (mmc
->cid
[2] >> 20) & 0xf,
1172 (mmc
->cid
[2] >> 16) & 0xf);
1174 mmc
->block_dev
.vendor
[0] = 0;
1175 mmc
->block_dev
.product
[0] = 0;
1176 mmc
->block_dev
.revision
[0] = 0;
1178 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1179 init_part(&mmc
->block_dev
);
1185 static int mmc_send_if_cond(struct mmc
*mmc
)
1190 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
1191 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1192 cmd
.cmdarg
= ((mmc
->cfg
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
1193 cmd
.resp_type
= MMC_RSP_R7
;
1195 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1200 if ((cmd
.response
[0] & 0xff) != 0xaa)
1201 return UNUSABLE_ERR
;
1203 mmc
->version
= SD_VERSION_2
;
1208 /* not used any more */
1209 int __deprecated
mmc_register(struct mmc
*mmc
)
1211 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1212 printf("%s is deprecated! use mmc_create() instead.\n", __func__
);
1217 struct mmc
*mmc_create(const struct mmc_config
*cfg
, void *priv
)
1221 /* quick validation */
1222 if (cfg
== NULL
|| cfg
->ops
== NULL
|| cfg
->ops
->send_cmd
== NULL
||
1223 cfg
->f_min
== 0 || cfg
->f_max
== 0 || cfg
->b_max
== 0)
1226 mmc
= calloc(1, sizeof(*mmc
));
1233 /* the following chunk was mmc_register() */
1235 /* Setup dsr related values */
1237 mmc
->dsr
= 0xffffffff;
1238 /* Setup the universal parts of the block interface just once */
1239 mmc
->block_dev
.if_type
= IF_TYPE_MMC
;
1240 mmc
->block_dev
.dev
= cur_dev_num
++;
1241 mmc
->block_dev
.removable
= 1;
1242 mmc
->block_dev
.block_read
= mmc_bread
;
1243 mmc
->block_dev
.block_write
= mmc_bwrite
;
1244 mmc
->block_dev
.block_erase
= mmc_berase
;
1246 /* setup initial part type */
1247 mmc
->block_dev
.part_type
= mmc
->cfg
->part_type
;
1249 INIT_LIST_HEAD(&mmc
->link
);
1251 list_add_tail(&mmc
->link
, &mmc_devices
);
1256 void mmc_destroy(struct mmc
*mmc
)
1258 /* only freeing memory for now */
1262 #ifdef CONFIG_PARTITIONS
1263 block_dev_desc_t
*mmc_get_dev(int dev
)
1265 struct mmc
*mmc
= find_mmc_device(dev
);
1266 if (!mmc
|| mmc_init(mmc
))
1269 return &mmc
->block_dev
;
1273 int mmc_start_init(struct mmc
*mmc
)
1277 /* we pretend there's no card when init is NULL */
1278 if (mmc_getcd(mmc
) == 0 || mmc
->cfg
->ops
->init
== NULL
) {
1280 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1281 printf("MMC: no card present\n");
1289 /* made sure it's not NULL earlier */
1290 err
= mmc
->cfg
->ops
->init(mmc
);
1295 mmc_set_bus_width(mmc
, 1);
1296 mmc_set_clock(mmc
, 1);
1298 /* Reset the Card */
1299 err
= mmc_go_idle(mmc
);
1304 /* The internal partition reset to user partition(0) at every CMD0*/
1307 /* Test for SD version 2 */
1308 err
= mmc_send_if_cond(mmc
);
1310 /* Now try to get the SD card's operating condition */
1311 err
= sd_send_op_cond(mmc
);
1313 /* If the command timed out, we check for an MMC card */
1314 if (err
== TIMEOUT
) {
1315 err
= mmc_send_op_cond(mmc
);
1317 if (err
&& err
!= IN_PROGRESS
) {
1318 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1319 printf("Card did not respond to voltage select!\n");
1321 return UNUSABLE_ERR
;
1325 if (err
== IN_PROGRESS
)
1326 mmc
->init_in_progress
= 1;
1331 static int mmc_complete_init(struct mmc
*mmc
)
1335 if (mmc
->op_cond_pending
)
1336 err
= mmc_complete_op_cond(mmc
);
1339 err
= mmc_startup(mmc
);
1344 mmc
->init_in_progress
= 0;
1348 int mmc_init(struct mmc
*mmc
)
1350 int err
= IN_PROGRESS
;
1356 start
= get_timer(0);
1358 if (!mmc
->init_in_progress
)
1359 err
= mmc_start_init(mmc
);
1361 if (!err
|| err
== IN_PROGRESS
)
1362 err
= mmc_complete_init(mmc
);
1363 debug("%s: %d, time %lu\n", __func__
, err
, get_timer(start
));
1367 int mmc_set_dsr(struct mmc
*mmc
, u16 val
)
1374 * CPU and board-specific MMC initializations. Aliased function
1375 * signals caller to move on
1377 static int __def_mmc_init(bd_t
*bis
)
1382 int cpu_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1383 int board_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1385 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1387 void print_mmc_devices(char separator
)
1390 struct list_head
*entry
;
1392 list_for_each(entry
, &mmc_devices
) {
1393 m
= list_entry(entry
, struct mmc
, link
);
1395 printf("%s: %d", m
->cfg
->name
, m
->block_dev
.dev
);
1397 if (entry
->next
!= &mmc_devices
)
1398 printf("%c ", separator
);
1405 void print_mmc_devices(char separator
) { }
1408 int get_mmc_num(void)
1413 void mmc_set_preinit(struct mmc
*mmc
, int preinit
)
1415 mmc
->preinit
= preinit
;
1418 static void do_preinit(void)
1421 struct list_head
*entry
;
1423 list_for_each(entry
, &mmc_devices
) {
1424 m
= list_entry(entry
, struct mmc
, link
);
1432 int mmc_initialize(bd_t
*bis
)
1434 INIT_LIST_HEAD (&mmc_devices
);
1437 if (board_mmc_init(bis
) < 0)
1440 #ifndef CONFIG_SPL_BUILD
1441 print_mmc_devices(',');
1448 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1450 * This function changes the size of boot partition and the size of rpmb
1451 * partition present on EMMC devices.
1454 * struct *mmc: pointer for the mmc device strcuture
1455 * bootsize: size of boot partition
1456 * rpmbsize: size of rpmb partition
1458 * Returns 0 on success.
1461 int mmc_boot_partition_size_change(struct mmc
*mmc
, unsigned long bootsize
,
1462 unsigned long rpmbsize
)
1467 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1468 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1469 cmd
.resp_type
= MMC_RSP_R1b
;
1470 cmd
.cmdarg
= MMC_CMD62_ARG1
;
1472 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1474 debug("mmc_boot_partition_size_change: Error1 = %d\n", err
);
1478 /* Boot partition changing mode */
1479 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1480 cmd
.resp_type
= MMC_RSP_R1b
;
1481 cmd
.cmdarg
= MMC_CMD62_ARG2
;
1483 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1485 debug("mmc_boot_partition_size_change: Error2 = %d\n", err
);
1488 /* boot partition size is multiple of 128KB */
1489 bootsize
= (bootsize
* 1024) / 128;
1491 /* Arg: boot partition size */
1492 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1493 cmd
.resp_type
= MMC_RSP_R1b
;
1494 cmd
.cmdarg
= bootsize
;
1496 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1498 debug("mmc_boot_partition_size_change: Error3 = %d\n", err
);
1501 /* RPMB partition size is multiple of 128KB */
1502 rpmbsize
= (rpmbsize
* 1024) / 128;
1503 /* Arg: RPMB partition size */
1504 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1505 cmd
.resp_type
= MMC_RSP_R1b
;
1506 cmd
.cmdarg
= rpmbsize
;
1508 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1510 debug("mmc_boot_partition_size_change: Error4 = %d\n", err
);
1517 * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
1518 * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
1521 * Returns 0 on success.
1523 int mmc_set_boot_bus_width(struct mmc
*mmc
, u8 width
, u8 reset
, u8 mode
)
1527 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_BOOT_BUS_WIDTH
,
1528 EXT_CSD_BOOT_BUS_WIDTH_MODE(mode
) |
1529 EXT_CSD_BOOT_BUS_WIDTH_RESET(reset
) |
1530 EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width
));
1538 * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1539 * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1542 * Returns 0 on success.
1544 int mmc_set_part_conf(struct mmc
*mmc
, u8 ack
, u8 part_num
, u8 access
)
1548 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
1549 EXT_CSD_BOOT_ACK(ack
) |
1550 EXT_CSD_BOOT_PART_NUM(part_num
) |
1551 EXT_CSD_PARTITION_ACCESS(access
));
1559 * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
1560 * for enable. Note that this is a write-once field for non-zero values.
1562 * Returns 0 on success.
1564 int mmc_set_rst_n_function(struct mmc
*mmc
, u8 enable
)
1566 return mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_RST_N_FUNCTION
,