2 * Copyright 2008, Freescale Semiconductor, Inc
5 * Based vaguely on the Linux code
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <linux/list.h>
35 /* Set block count limit because of 16 bit register limit on some hardware*/
36 #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
37 #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
40 static struct list_head mmc_devices
;
41 static int cur_dev_num
= -1;
43 int __weak
board_mmc_getwp(struct mmc
*mmc
)
48 int mmc_getwp(struct mmc
*mmc
)
52 wp
= board_mmc_getwp(mmc
);
64 int __board_mmc_getcd(struct mmc
*mmc
) {
68 int board_mmc_getcd(struct mmc
*mmc
)__attribute__((weak
,
69 alias("__board_mmc_getcd")));
71 static int mmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
72 struct mmc_data
*data
)
74 struct mmc_data backup
;
77 memset(&backup
, 0, sizeof(backup
));
79 #ifdef CONFIG_MMC_TRACE
83 printf("CMD_SEND:%d\n", cmd
->cmdidx
);
84 printf("\t\tARG\t\t\t 0x%08X\n", cmd
->cmdarg
);
85 ret
= mmc
->send_cmd(mmc
, cmd
, data
);
86 switch (cmd
->resp_type
) {
88 printf("\t\tMMC_RSP_NONE\n");
91 printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
95 printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
99 printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
101 printf("\t\t \t\t 0x%08X \n",
103 printf("\t\t \t\t 0x%08X \n",
105 printf("\t\t \t\t 0x%08X \n",
108 printf("\t\t\t\t\tDUMPING DATA\n");
109 for (i
= 0; i
< 4; i
++) {
111 printf("\t\t\t\t\t%03d - ", i
*4);
112 ptr
= (u8
*)&cmd
->response
[i
];
114 for (j
= 0; j
< 4; j
++)
115 printf("%02X ", *ptr
--);
120 printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
124 printf("\t\tERROR MMC rsp not supported\n");
128 ret
= mmc
->send_cmd(mmc
, cmd
, data
);
133 static int mmc_send_status(struct mmc
*mmc
, int timeout
)
136 int err
, retries
= 5;
137 #ifdef CONFIG_MMC_TRACE
141 cmd
.cmdidx
= MMC_CMD_SEND_STATUS
;
142 cmd
.resp_type
= MMC_RSP_R1
;
143 if (!mmc_host_is_spi(mmc
))
144 cmd
.cmdarg
= mmc
->rca
<< 16;
147 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
149 if ((cmd
.response
[0] & MMC_STATUS_RDY_FOR_DATA
) &&
150 (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) !=
153 else if (cmd
.response
[0] & MMC_STATUS_MASK
) {
154 printf("Status Error: 0x%08X\n",
158 } else if (--retries
< 0)
165 #ifdef CONFIG_MMC_TRACE
166 status
= (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) >> 9;
167 printf("CURR STATE:%d\n", status
);
170 printf("Timeout waiting card ready\n");
177 static int mmc_set_blocklen(struct mmc
*mmc
, int len
)
181 cmd
.cmdidx
= MMC_CMD_SET_BLOCKLEN
;
182 cmd
.resp_type
= MMC_RSP_R1
;
185 return mmc_send_cmd(mmc
, &cmd
, NULL
);
188 struct mmc
*find_mmc_device(int dev_num
)
191 struct list_head
*entry
;
193 list_for_each(entry
, &mmc_devices
) {
194 m
= list_entry(entry
, struct mmc
, link
);
196 if (m
->block_dev
.dev
== dev_num
)
200 printf("MMC Device %d not found\n", dev_num
);
205 static ulong
mmc_erase_t(struct mmc
*mmc
, ulong start
, lbaint_t blkcnt
)
209 int err
, start_cmd
, end_cmd
;
211 if (mmc
->high_capacity
)
212 end
= start
+ blkcnt
- 1;
214 end
= (start
+ blkcnt
- 1) * mmc
->write_bl_len
;
215 start
*= mmc
->write_bl_len
;
219 start_cmd
= SD_CMD_ERASE_WR_BLK_START
;
220 end_cmd
= SD_CMD_ERASE_WR_BLK_END
;
222 start_cmd
= MMC_CMD_ERASE_GROUP_START
;
223 end_cmd
= MMC_CMD_ERASE_GROUP_END
;
226 cmd
.cmdidx
= start_cmd
;
228 cmd
.resp_type
= MMC_RSP_R1
;
230 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
234 cmd
.cmdidx
= end_cmd
;
237 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
241 cmd
.cmdidx
= MMC_CMD_ERASE
;
242 cmd
.cmdarg
= SECURE_ERASE
;
243 cmd
.resp_type
= MMC_RSP_R1b
;
245 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
252 puts("mmc erase failed\n");
257 mmc_berase(int dev_num
, lbaint_t start
, lbaint_t blkcnt
)
260 struct mmc
*mmc
= find_mmc_device(dev_num
);
261 lbaint_t blk
= 0, blk_r
= 0;
267 if ((start
% mmc
->erase_grp_size
) || (blkcnt
% mmc
->erase_grp_size
))
268 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
269 "The erase range would be change to "
270 "0x" LBAF
"~0x" LBAF
"\n\n",
271 mmc
->erase_grp_size
, start
& ~(mmc
->erase_grp_size
- 1),
272 ((start
+ blkcnt
+ mmc
->erase_grp_size
)
273 & ~(mmc
->erase_grp_size
- 1)) - 1);
275 while (blk
< blkcnt
) {
276 blk_r
= ((blkcnt
- blk
) > mmc
->erase_grp_size
) ?
277 mmc
->erase_grp_size
: (blkcnt
- blk
);
278 err
= mmc_erase_t(mmc
, start
+ blk
, blk_r
);
284 /* Waiting for the ready status */
285 if (mmc_send_status(mmc
, timeout
))
293 mmc_write_blocks(struct mmc
*mmc
, lbaint_t start
, lbaint_t blkcnt
, const void*src
)
296 struct mmc_data data
;
299 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
300 printf("MMC: block number 0x" LBAF
" exceeds max(0x" LBAF
")\n",
301 start
+ blkcnt
, mmc
->block_dev
.lba
);
307 else if (blkcnt
== 1)
308 cmd
.cmdidx
= MMC_CMD_WRITE_SINGLE_BLOCK
;
310 cmd
.cmdidx
= MMC_CMD_WRITE_MULTIPLE_BLOCK
;
312 if (mmc
->high_capacity
)
315 cmd
.cmdarg
= start
* mmc
->write_bl_len
;
317 cmd
.resp_type
= MMC_RSP_R1
;
320 data
.blocks
= blkcnt
;
321 data
.blocksize
= mmc
->write_bl_len
;
322 data
.flags
= MMC_DATA_WRITE
;
324 if (mmc_send_cmd(mmc
, &cmd
, &data
)) {
325 printf("mmc write failed\n");
329 /* SPI multiblock writes terminate using a special
330 * token, not a STOP_TRANSMISSION request.
332 if (!mmc_host_is_spi(mmc
) && blkcnt
> 1) {
333 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
335 cmd
.resp_type
= MMC_RSP_R1b
;
336 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
337 printf("mmc fail to send stop cmd\n");
342 /* Waiting for the ready status */
343 if (mmc_send_status(mmc
, timeout
))
350 mmc_bwrite(int dev_num
, lbaint_t start
, lbaint_t blkcnt
, const void*src
)
352 lbaint_t cur
, blocks_todo
= blkcnt
;
354 struct mmc
*mmc
= find_mmc_device(dev_num
);
358 if (mmc_set_blocklen(mmc
, mmc
->write_bl_len
))
362 cur
= (blocks_todo
> mmc
->b_max
) ? mmc
->b_max
: blocks_todo
;
363 if(mmc_write_blocks(mmc
, start
, cur
, src
) != cur
)
367 src
+= cur
* mmc
->write_bl_len
;
368 } while (blocks_todo
> 0);
373 static int mmc_read_blocks(struct mmc
*mmc
, void *dst
, lbaint_t start
,
377 struct mmc_data data
;
380 cmd
.cmdidx
= MMC_CMD_READ_MULTIPLE_BLOCK
;
382 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
384 if (mmc
->high_capacity
)
387 cmd
.cmdarg
= start
* mmc
->read_bl_len
;
389 cmd
.resp_type
= MMC_RSP_R1
;
392 data
.blocks
= blkcnt
;
393 data
.blocksize
= mmc
->read_bl_len
;
394 data
.flags
= MMC_DATA_READ
;
396 if (mmc_send_cmd(mmc
, &cmd
, &data
))
400 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
402 cmd
.resp_type
= MMC_RSP_R1b
;
403 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
404 printf("mmc fail to send stop cmd\n");
412 static ulong
mmc_bread(int dev_num
, lbaint_t start
, lbaint_t blkcnt
, void *dst
)
414 lbaint_t cur
, blocks_todo
= blkcnt
;
419 struct mmc
*mmc
= find_mmc_device(dev_num
);
423 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
424 printf("MMC: block number 0x" LBAF
" exceeds max(0x" LBAF
")\n",
425 start
+ blkcnt
, mmc
->block_dev
.lba
);
429 if (mmc_set_blocklen(mmc
, mmc
->read_bl_len
))
433 cur
= (blocks_todo
> mmc
->b_max
) ? mmc
->b_max
: blocks_todo
;
434 if(mmc_read_blocks(mmc
, dst
, start
, cur
) != cur
)
438 dst
+= cur
* mmc
->read_bl_len
;
439 } while (blocks_todo
> 0);
444 static int mmc_go_idle(struct mmc
*mmc
)
451 cmd
.cmdidx
= MMC_CMD_GO_IDLE_STATE
;
453 cmd
.resp_type
= MMC_RSP_NONE
;
455 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
465 static int sd_send_op_cond(struct mmc
*mmc
)
472 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
473 cmd
.resp_type
= MMC_RSP_R1
;
476 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
481 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
482 cmd
.resp_type
= MMC_RSP_R3
;
485 * Most cards do not answer if some reserved bits
486 * in the ocr are set. However, Some controller
487 * can set bit 7 (reserved for low voltages), but
488 * how to manage low voltages SD card is not yet
491 cmd
.cmdarg
= mmc_host_is_spi(mmc
) ? 0 :
492 (mmc
->voltages
& 0xff8000);
494 if (mmc
->version
== SD_VERSION_2
)
495 cmd
.cmdarg
|= OCR_HCS
;
497 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
503 } while ((!(cmd
.response
[0] & OCR_BUSY
)) && timeout
--);
508 if (mmc
->version
!= SD_VERSION_2
)
509 mmc
->version
= SD_VERSION_1_0
;
511 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
512 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
513 cmd
.resp_type
= MMC_RSP_R3
;
516 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
522 mmc
->ocr
= cmd
.response
[0];
524 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
530 /* We pass in the cmd since otherwise the init seems to fail */
531 static int mmc_send_op_cond_iter(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
536 cmd
->cmdidx
= MMC_CMD_SEND_OP_COND
;
537 cmd
->resp_type
= MMC_RSP_R3
;
539 if (use_arg
&& !mmc_host_is_spi(mmc
)) {
542 (mmc
->op_cond_response
& OCR_VOLTAGE_MASK
)) |
543 (mmc
->op_cond_response
& OCR_ACCESS_MODE
);
545 if (mmc
->host_caps
& MMC_MODE_HC
)
546 cmd
->cmdarg
|= OCR_HCS
;
548 err
= mmc_send_cmd(mmc
, cmd
, NULL
);
551 mmc
->op_cond_response
= cmd
->response
[0];
555 int mmc_send_op_cond(struct mmc
*mmc
)
560 /* Some cards seem to need this */
563 /* Asking to the card its capabilities */
564 mmc
->op_cond_pending
= 1;
565 for (i
= 0; i
< 2; i
++) {
566 err
= mmc_send_op_cond_iter(mmc
, &cmd
, i
!= 0);
570 /* exit if not busy (flag seems to be inverted) */
571 if (mmc
->op_cond_response
& OCR_BUSY
)
577 int mmc_complete_op_cond(struct mmc
*mmc
)
584 mmc
->op_cond_pending
= 0;
585 start
= get_timer(0);
587 err
= mmc_send_op_cond_iter(mmc
, &cmd
, 1);
590 if (get_timer(start
) > timeout
)
593 } while (!(mmc
->op_cond_response
& OCR_BUSY
));
595 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
596 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
597 cmd
.resp_type
= MMC_RSP_R3
;
600 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
606 mmc
->version
= MMC_VERSION_UNKNOWN
;
607 mmc
->ocr
= cmd
.response
[0];
609 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
616 static int mmc_send_ext_csd(struct mmc
*mmc
, u8
*ext_csd
)
619 struct mmc_data data
;
622 /* Get the Card Status Register */
623 cmd
.cmdidx
= MMC_CMD_SEND_EXT_CSD
;
624 cmd
.resp_type
= MMC_RSP_R1
;
627 data
.dest
= (char *)ext_csd
;
629 data
.blocksize
= MMC_MAX_BLOCK_LEN
;
630 data
.flags
= MMC_DATA_READ
;
632 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
638 static int mmc_switch(struct mmc
*mmc
, u8 set
, u8 index
, u8 value
)
644 cmd
.cmdidx
= MMC_CMD_SWITCH
;
645 cmd
.resp_type
= MMC_RSP_R1b
;
646 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
650 ret
= mmc_send_cmd(mmc
, &cmd
, NULL
);
652 /* Waiting for the ready status */
654 ret
= mmc_send_status(mmc
, timeout
);
660 static int mmc_change_freq(struct mmc
*mmc
)
662 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
668 if (mmc_host_is_spi(mmc
))
671 /* Only version 4 supports high-speed */
672 if (mmc
->version
< MMC_VERSION_4
)
675 err
= mmc_send_ext_csd(mmc
, ext_csd
);
680 cardtype
= ext_csd
[EXT_CSD_CARD_TYPE
] & 0xf;
682 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
, 1);
687 /* Now check to see that it worked */
688 err
= mmc_send_ext_csd(mmc
, ext_csd
);
693 /* No high-speed support */
694 if (!ext_csd
[EXT_CSD_HS_TIMING
])
697 /* High Speed is set, there are two types: 52MHz and 26MHz */
698 if (cardtype
& MMC_HS_52MHZ
)
699 mmc
->card_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
701 mmc
->card_caps
|= MMC_MODE_HS
;
706 static int mmc_set_capacity(struct mmc
*mmc
, int part_num
)
710 mmc
->capacity
= mmc
->capacity_user
;
714 mmc
->capacity
= mmc
->capacity_boot
;
717 mmc
->capacity
= mmc
->capacity_rpmb
;
723 mmc
->capacity
= mmc
->capacity_gp
[part_num
- 4];
729 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
734 int mmc_switch_part(int dev_num
, unsigned int part_num
)
736 struct mmc
*mmc
= find_mmc_device(dev_num
);
742 ret
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
743 (mmc
->part_config
& ~PART_ACCESS_MASK
)
744 | (part_num
& PART_ACCESS_MASK
));
748 return mmc_set_capacity(mmc
, part_num
);
751 int mmc_getcd(struct mmc
*mmc
)
755 cd
= board_mmc_getcd(mmc
);
759 cd
= mmc
->getcd(mmc
);
767 static int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
770 struct mmc_data data
;
772 /* Switch the frequency */
773 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
774 cmd
.resp_type
= MMC_RSP_R1
;
775 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
776 cmd
.cmdarg
&= ~(0xf << (group
* 4));
777 cmd
.cmdarg
|= value
<< (group
* 4);
779 data
.dest
= (char *)resp
;
782 data
.flags
= MMC_DATA_READ
;
784 return mmc_send_cmd(mmc
, &cmd
, &data
);
788 static int sd_change_freq(struct mmc
*mmc
)
792 ALLOC_CACHE_ALIGN_BUFFER(uint
, scr
, 2);
793 ALLOC_CACHE_ALIGN_BUFFER(uint
, switch_status
, 16);
794 struct mmc_data data
;
799 if (mmc_host_is_spi(mmc
))
802 /* Read the SCR to find out if this card supports higher speeds */
803 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
804 cmd
.resp_type
= MMC_RSP_R1
;
805 cmd
.cmdarg
= mmc
->rca
<< 16;
807 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
812 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
813 cmd
.resp_type
= MMC_RSP_R1
;
819 data
.dest
= (char *)scr
;
822 data
.flags
= MMC_DATA_READ
;
824 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
833 mmc
->scr
[0] = __be32_to_cpu(scr
[0]);
834 mmc
->scr
[1] = __be32_to_cpu(scr
[1]);
836 switch ((mmc
->scr
[0] >> 24) & 0xf) {
838 mmc
->version
= SD_VERSION_1_0
;
841 mmc
->version
= SD_VERSION_1_10
;
844 mmc
->version
= SD_VERSION_2
;
845 if ((mmc
->scr
[0] >> 15) & 0x1)
846 mmc
->version
= SD_VERSION_3
;
849 mmc
->version
= SD_VERSION_1_0
;
853 if (mmc
->scr
[0] & SD_DATA_4BIT
)
854 mmc
->card_caps
|= MMC_MODE_4BIT
;
856 /* Version 1.0 doesn't support switching */
857 if (mmc
->version
== SD_VERSION_1_0
)
862 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
863 (u8
*)switch_status
);
868 /* The high-speed function is busy. Try again */
869 if (!(__be32_to_cpu(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
873 /* If high-speed isn't supported, we return */
874 if (!(__be32_to_cpu(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
))
878 * If the host doesn't support SD_HIGHSPEED, do not switch card to
879 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
880 * This can avoid furthur problem when the card runs in different
881 * mode between the host.
883 if (!((mmc
->host_caps
& MMC_MODE_HS_52MHz
) &&
884 (mmc
->host_caps
& MMC_MODE_HS
)))
887 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)switch_status
);
892 if ((__be32_to_cpu(switch_status
[4]) & 0x0f000000) == 0x01000000)
893 mmc
->card_caps
|= MMC_MODE_HS
;
898 /* frequency bases */
899 /* divided by 10 to be nice to platforms without floating point */
900 static const int fbase
[] = {
907 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
908 * to platforms without floating point.
910 static const int multipliers
[] = {
929 static void mmc_set_ios(struct mmc
*mmc
)
934 void mmc_set_clock(struct mmc
*mmc
, uint clock
)
936 if (clock
> mmc
->f_max
)
939 if (clock
< mmc
->f_min
)
947 static void mmc_set_bus_width(struct mmc
*mmc
, uint width
)
949 mmc
->bus_width
= width
;
954 static int mmc_startup(struct mmc
*mmc
)
958 u64 cmult
, csize
, capacity
;
960 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
961 ALLOC_CACHE_ALIGN_BUFFER(u8
, test_csd
, MMC_MAX_BLOCK_LEN
);
964 #ifdef CONFIG_MMC_SPI_CRC_ON
965 if (mmc_host_is_spi(mmc
)) { /* enable CRC check for spi */
966 cmd
.cmdidx
= MMC_CMD_SPI_CRC_ON_OFF
;
967 cmd
.resp_type
= MMC_RSP_R1
;
969 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
976 /* Put the Card in Identify Mode */
977 cmd
.cmdidx
= mmc_host_is_spi(mmc
) ? MMC_CMD_SEND_CID
:
978 MMC_CMD_ALL_SEND_CID
; /* cmd not supported in spi */
979 cmd
.resp_type
= MMC_RSP_R2
;
982 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
987 memcpy(mmc
->cid
, cmd
.response
, 16);
990 * For MMC cards, set the Relative Address.
991 * For SD cards, get the Relatvie Address.
992 * This also puts the cards into Standby State
994 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
995 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
996 cmd
.cmdarg
= mmc
->rca
<< 16;
997 cmd
.resp_type
= MMC_RSP_R6
;
999 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1005 mmc
->rca
= (cmd
.response
[0] >> 16) & 0xffff;
1008 /* Get the Card-Specific Data */
1009 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
1010 cmd
.resp_type
= MMC_RSP_R2
;
1011 cmd
.cmdarg
= mmc
->rca
<< 16;
1013 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1015 /* Waiting for the ready status */
1016 mmc_send_status(mmc
, timeout
);
1021 mmc
->csd
[0] = cmd
.response
[0];
1022 mmc
->csd
[1] = cmd
.response
[1];
1023 mmc
->csd
[2] = cmd
.response
[2];
1024 mmc
->csd
[3] = cmd
.response
[3];
1026 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
1027 int version
= (cmd
.response
[0] >> 26) & 0xf;
1031 mmc
->version
= MMC_VERSION_1_2
;
1034 mmc
->version
= MMC_VERSION_1_4
;
1037 mmc
->version
= MMC_VERSION_2_2
;
1040 mmc
->version
= MMC_VERSION_3
;
1043 mmc
->version
= MMC_VERSION_4
;
1046 mmc
->version
= MMC_VERSION_1_2
;
1051 /* divide frequency by 10, since the mults are 10x bigger */
1052 freq
= fbase
[(cmd
.response
[0] & 0x7)];
1053 mult
= multipliers
[((cmd
.response
[0] >> 3) & 0xf)];
1055 mmc
->tran_speed
= freq
* mult
;
1057 mmc
->read_bl_len
= 1 << ((cmd
.response
[1] >> 16) & 0xf);
1060 mmc
->write_bl_len
= mmc
->read_bl_len
;
1062 mmc
->write_bl_len
= 1 << ((cmd
.response
[3] >> 22) & 0xf);
1064 if (mmc
->high_capacity
) {
1065 csize
= (mmc
->csd
[1] & 0x3f) << 16
1066 | (mmc
->csd
[2] & 0xffff0000) >> 16;
1069 csize
= (mmc
->csd
[1] & 0x3ff) << 2
1070 | (mmc
->csd
[2] & 0xc0000000) >> 30;
1071 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
1074 mmc
->capacity_user
= (csize
+ 1) << (cmult
+ 2);
1075 mmc
->capacity_user
*= mmc
->read_bl_len
;
1076 mmc
->capacity_boot
= 0;
1077 mmc
->capacity_rpmb
= 0;
1078 for (i
= 0; i
< 4; i
++)
1079 mmc
->capacity_gp
[i
] = 0;
1081 if (mmc
->read_bl_len
> MMC_MAX_BLOCK_LEN
)
1082 mmc
->read_bl_len
= MMC_MAX_BLOCK_LEN
;
1084 if (mmc
->write_bl_len
> MMC_MAX_BLOCK_LEN
)
1085 mmc
->write_bl_len
= MMC_MAX_BLOCK_LEN
;
1087 /* Select the card, and put it into Transfer Mode */
1088 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
1089 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
1090 cmd
.resp_type
= MMC_RSP_R1
;
1091 cmd
.cmdarg
= mmc
->rca
<< 16;
1092 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1099 * For SD, its erase group is always one sector
1101 mmc
->erase_grp_size
= 1;
1102 mmc
->part_config
= MMCPART_NOAVAILABLE
;
1103 if (!IS_SD(mmc
) && (mmc
->version
>= MMC_VERSION_4
)) {
1104 /* check ext_csd version and capacity */
1105 err
= mmc_send_ext_csd(mmc
, ext_csd
);
1106 if (!err
&& (ext_csd
[EXT_CSD_REV
] >= 2)) {
1108 * According to the JEDEC Standard, the value of
1109 * ext_csd's capacity is valid if the value is more
1112 capacity
= ext_csd
[EXT_CSD_SEC_CNT
] << 0
1113 | ext_csd
[EXT_CSD_SEC_CNT
+ 1] << 8
1114 | ext_csd
[EXT_CSD_SEC_CNT
+ 2] << 16
1115 | ext_csd
[EXT_CSD_SEC_CNT
+ 3] << 24;
1116 capacity
*= MMC_MAX_BLOCK_LEN
;
1117 if ((capacity
>> 20) > 2 * 1024)
1118 mmc
->capacity_user
= capacity
;
1121 switch (ext_csd
[EXT_CSD_REV
]) {
1123 mmc
->version
= MMC_VERSION_4_1
;
1126 mmc
->version
= MMC_VERSION_4_2
;
1129 mmc
->version
= MMC_VERSION_4_3
;
1132 mmc
->version
= MMC_VERSION_4_41
;
1135 mmc
->version
= MMC_VERSION_4_5
;
1140 * Check whether GROUP_DEF is set, if yes, read out
1141 * group size from ext_csd directly, or calculate
1142 * the group size from the csd value.
1144 if (ext_csd
[EXT_CSD_ERASE_GROUP_DEF
]) {
1145 mmc
->erase_grp_size
=
1146 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] *
1147 MMC_MAX_BLOCK_LEN
* 1024;
1149 int erase_gsz
, erase_gmul
;
1150 erase_gsz
= (mmc
->csd
[2] & 0x00007c00) >> 10;
1151 erase_gmul
= (mmc
->csd
[2] & 0x000003e0) >> 5;
1152 mmc
->erase_grp_size
= (erase_gsz
+ 1)
1156 /* store the partition info of emmc */
1157 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) ||
1158 ext_csd
[EXT_CSD_BOOT_MULT
])
1159 mmc
->part_config
= ext_csd
[EXT_CSD_PART_CONF
];
1161 mmc
->capacity_boot
= ext_csd
[EXT_CSD_BOOT_MULT
] << 17;
1163 mmc
->capacity_rpmb
= ext_csd
[EXT_CSD_RPMB_MULT
] << 17;
1165 for (i
= 0; i
< 4; i
++) {
1166 int idx
= EXT_CSD_GP_SIZE_MULT
+ i
* 3;
1167 mmc
->capacity_gp
[i
] = (ext_csd
[idx
+ 2] << 16) +
1168 (ext_csd
[idx
+ 1] << 8) + ext_csd
[idx
];
1169 mmc
->capacity_gp
[i
] *=
1170 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
];
1171 mmc
->capacity_gp
[i
] *= ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
];
1175 err
= mmc_set_capacity(mmc
, mmc
->part_num
);
1180 err
= sd_change_freq(mmc
);
1182 err
= mmc_change_freq(mmc
);
1187 /* Restrict card's capabilities by what the host can do */
1188 mmc
->card_caps
&= mmc
->host_caps
;
1191 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
1192 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
1193 cmd
.resp_type
= MMC_RSP_R1
;
1194 cmd
.cmdarg
= mmc
->rca
<< 16;
1196 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1200 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
1201 cmd
.resp_type
= MMC_RSP_R1
;
1203 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1207 mmc_set_bus_width(mmc
, 4);
1210 if (mmc
->card_caps
& MMC_MODE_HS
)
1211 mmc
->tran_speed
= 50000000;
1213 mmc
->tran_speed
= 25000000;
1217 /* An array of possible bus widths in order of preference */
1218 static unsigned ext_csd_bits
[] = {
1219 EXT_CSD_BUS_WIDTH_8
,
1220 EXT_CSD_BUS_WIDTH_4
,
1221 EXT_CSD_BUS_WIDTH_1
,
1224 /* An array to map CSD bus widths to host cap bits */
1225 static unsigned ext_to_hostcaps
[] = {
1226 [EXT_CSD_BUS_WIDTH_4
] = MMC_MODE_4BIT
,
1227 [EXT_CSD_BUS_WIDTH_8
] = MMC_MODE_8BIT
,
1230 /* An array to map chosen bus width to an integer */
1231 static unsigned widths
[] = {
1235 for (idx
=0; idx
< ARRAY_SIZE(ext_csd_bits
); idx
++) {
1236 unsigned int extw
= ext_csd_bits
[idx
];
1239 * Check to make sure the controller supports
1240 * this bus width, if it's more than 1
1242 if (extw
!= EXT_CSD_BUS_WIDTH_1
&&
1243 !(mmc
->host_caps
& ext_to_hostcaps
[extw
]))
1246 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1247 EXT_CSD_BUS_WIDTH
, extw
);
1252 mmc_set_bus_width(mmc
, widths
[idx
]);
1254 err
= mmc_send_ext_csd(mmc
, test_csd
);
1255 if (!err
&& ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] \
1256 == test_csd
[EXT_CSD_PARTITIONING_SUPPORT
]
1257 && ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1258 == test_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1259 && ext_csd
[EXT_CSD_REV
] \
1260 == test_csd
[EXT_CSD_REV
]
1261 && ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] \
1262 == test_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
]
1263 && memcmp(&ext_csd
[EXT_CSD_SEC_CNT
], \
1264 &test_csd
[EXT_CSD_SEC_CNT
], 4) == 0) {
1266 mmc
->card_caps
|= ext_to_hostcaps
[extw
];
1271 if (mmc
->card_caps
& MMC_MODE_HS
) {
1272 if (mmc
->card_caps
& MMC_MODE_HS_52MHz
)
1273 mmc
->tran_speed
= 52000000;
1275 mmc
->tran_speed
= 26000000;
1279 mmc_set_clock(mmc
, mmc
->tran_speed
);
1281 /* fill in device description */
1282 mmc
->block_dev
.lun
= 0;
1283 mmc
->block_dev
.type
= 0;
1284 mmc
->block_dev
.blksz
= mmc
->read_bl_len
;
1285 mmc
->block_dev
.log2blksz
= LOG2(mmc
->block_dev
.blksz
);
1286 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
1287 sprintf(mmc
->block_dev
.vendor
, "Man %06x Snr %04x%04x",
1288 mmc
->cid
[0] >> 24, (mmc
->cid
[2] & 0xffff),
1289 (mmc
->cid
[3] >> 16) & 0xffff);
1290 sprintf(mmc
->block_dev
.product
, "%c%c%c%c%c%c", mmc
->cid
[0] & 0xff,
1291 (mmc
->cid
[1] >> 24), (mmc
->cid
[1] >> 16) & 0xff,
1292 (mmc
->cid
[1] >> 8) & 0xff, mmc
->cid
[1] & 0xff,
1293 (mmc
->cid
[2] >> 24) & 0xff);
1294 sprintf(mmc
->block_dev
.revision
, "%d.%d", (mmc
->cid
[2] >> 20) & 0xf,
1295 (mmc
->cid
[2] >> 16) & 0xf);
1296 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1297 init_part(&mmc
->block_dev
);
1303 static int mmc_send_if_cond(struct mmc
*mmc
)
1308 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
1309 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1310 cmd
.cmdarg
= ((mmc
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
1311 cmd
.resp_type
= MMC_RSP_R7
;
1313 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1318 if ((cmd
.response
[0] & 0xff) != 0xaa)
1319 return UNUSABLE_ERR
;
1321 mmc
->version
= SD_VERSION_2
;
1326 int mmc_register(struct mmc
*mmc
)
1328 /* Setup the universal parts of the block interface just once */
1329 mmc
->block_dev
.if_type
= IF_TYPE_MMC
;
1330 mmc
->block_dev
.dev
= cur_dev_num
++;
1331 mmc
->block_dev
.removable
= 1;
1332 mmc
->block_dev
.block_read
= mmc_bread
;
1333 mmc
->block_dev
.block_write
= mmc_bwrite
;
1334 mmc
->block_dev
.block_erase
= mmc_berase
;
1336 mmc
->b_max
= CONFIG_SYS_MMC_MAX_BLK_COUNT
;
1338 INIT_LIST_HEAD (&mmc
->link
);
1340 list_add_tail (&mmc
->link
, &mmc_devices
);
1345 #ifdef CONFIG_PARTITIONS
1346 block_dev_desc_t
*mmc_get_dev(int dev
)
1348 struct mmc
*mmc
= find_mmc_device(dev
);
1349 if (!mmc
|| mmc_init(mmc
))
1352 return &mmc
->block_dev
;
1356 int mmc_start_init(struct mmc
*mmc
)
1360 if (mmc_getcd(mmc
) == 0) {
1362 printf("MMC: no card present\n");
1369 err
= mmc
->init(mmc
);
1374 mmc_set_bus_width(mmc
, 1);
1375 mmc_set_clock(mmc
, 1);
1377 /* Reset the Card */
1378 err
= mmc_go_idle(mmc
);
1383 /* The internal partition reset to user partition(0) at every CMD0*/
1386 /* Test for SD version 2 */
1387 err
= mmc_send_if_cond(mmc
);
1389 /* Now try to get the SD card's operating condition */
1390 err
= sd_send_op_cond(mmc
);
1392 /* If the command timed out, we check for an MMC card */
1393 if (err
== TIMEOUT
) {
1394 err
= mmc_send_op_cond(mmc
);
1396 if (err
&& err
!= IN_PROGRESS
) {
1397 printf("Card did not respond to voltage select!\n");
1398 return UNUSABLE_ERR
;
1402 if (err
== IN_PROGRESS
)
1403 mmc
->init_in_progress
= 1;
1408 static int mmc_complete_init(struct mmc
*mmc
)
1412 if (mmc
->op_cond_pending
)
1413 err
= mmc_complete_op_cond(mmc
);
1416 err
= mmc_startup(mmc
);
1421 mmc
->init_in_progress
= 0;
1425 int mmc_init(struct mmc
*mmc
)
1427 int err
= IN_PROGRESS
;
1428 unsigned start
= get_timer(0);
1432 if (!mmc
->init_in_progress
)
1433 err
= mmc_start_init(mmc
);
1435 if (!err
|| err
== IN_PROGRESS
)
1436 err
= mmc_complete_init(mmc
);
1437 debug("%s: %d, time %lu\n", __func__
, err
, get_timer(start
));
1442 * CPU and board-specific MMC initializations. Aliased function
1443 * signals caller to move on
1445 static int __def_mmc_init(bd_t
*bis
)
1450 int cpu_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1451 int board_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1453 void print_mmc_devices(char separator
)
1456 struct list_head
*entry
;
1458 list_for_each(entry
, &mmc_devices
) {
1459 m
= list_entry(entry
, struct mmc
, link
);
1461 printf("%s: %d", m
->name
, m
->block_dev
.dev
);
1463 if (entry
->next
!= &mmc_devices
)
1464 printf("%c ", separator
);
1470 int get_mmc_num(void)
1475 void mmc_set_preinit(struct mmc
*mmc
, int preinit
)
1477 mmc
->preinit
= preinit
;
1480 static void do_preinit(void)
1483 struct list_head
*entry
;
1485 list_for_each(entry
, &mmc_devices
) {
1486 m
= list_entry(entry
, struct mmc
, link
);
1494 int mmc_initialize(bd_t
*bis
)
1496 INIT_LIST_HEAD (&mmc_devices
);
1499 if (board_mmc_init(bis
) < 0)
1502 print_mmc_devices(',');
1508 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1510 * This function changes the size of boot partition and the size of rpmb
1511 * partition present on EMMC devices.
1514 * struct *mmc: pointer for the mmc device strcuture
1515 * bootsize: size of boot partition
1516 * rpmbsize: size of rpmb partition
1518 * Returns 0 on success.
1521 int mmc_boot_partition_size_change(struct mmc
*mmc
, unsigned long bootsize
,
1522 unsigned long rpmbsize
)
1527 /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1528 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1529 cmd
.resp_type
= MMC_RSP_R1b
;
1530 cmd
.cmdarg
= MMC_CMD62_ARG1
;
1532 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1534 debug("mmc_boot_partition_size_change: Error1 = %d\n", err
);
1538 /* Boot partition changing mode */
1539 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1540 cmd
.resp_type
= MMC_RSP_R1b
;
1541 cmd
.cmdarg
= MMC_CMD62_ARG2
;
1543 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1545 debug("mmc_boot_partition_size_change: Error2 = %d\n", err
);
1548 /* boot partition size is multiple of 128KB */
1549 bootsize
= (bootsize
* 1024) / 128;
1551 /* Arg: boot partition size */
1552 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1553 cmd
.resp_type
= MMC_RSP_R1b
;
1554 cmd
.cmdarg
= bootsize
;
1556 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1558 debug("mmc_boot_partition_size_change: Error3 = %d\n", err
);
1561 /* RPMB partition size is multiple of 128KB */
1562 rpmbsize
= (rpmbsize
* 1024) / 128;
1563 /* Arg: RPMB partition size */
1564 cmd
.cmdidx
= MMC_CMD_RES_MAN
;
1565 cmd
.resp_type
= MMC_RSP_R1b
;
1566 cmd
.cmdarg
= rpmbsize
;
1568 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1570 debug("mmc_boot_partition_size_change: Error4 = %d\n", err
);
1577 * This function shall form and send the commands to open / close the
1578 * boot partition specified by user.
1581 * ack: 0x0 - No boot acknowledge sent (default)
1582 * 0x1 - Boot acknowledge sent during boot operation
1583 * part_num: User selects boot data that will be sent to master
1584 * 0x0 - Device not boot enabled (default)
1585 * 0x1 - Boot partition 1 enabled for boot
1586 * 0x2 - Boot partition 2 enabled for boot
1587 * access: User selects partitions to access
1588 * 0x0 : No access to boot partition (default)
1589 * 0x1 : R/W boot partition 1
1590 * 0x2 : R/W boot partition 2
1591 * 0x3 : R/W Replay Protected Memory Block (RPMB)
1593 * Returns 0 on success.
1595 int mmc_boot_part_access(struct mmc
*mmc
, u8 ack
, u8 part_num
, u8 access
)
1600 /* Boot ack enable, boot partition enable , boot partition access */
1601 cmd
.cmdidx
= MMC_CMD_SWITCH
;
1602 cmd
.resp_type
= MMC_RSP_R1b
;
1604 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
1605 (EXT_CSD_PART_CONF
<< 16) |
1606 ((EXT_CSD_BOOT_ACK(ack
) |
1607 EXT_CSD_BOOT_PART_NUM(part_num
) |
1608 EXT_CSD_PARTITION_ACCESS(access
)) << 8);
1610 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1613 debug("mmc boot partition#%d open fail:Error1 = %d\n",
1616 debug("mmc boot partition#%d close fail:Error = %d\n",
1623 /* 4bit transfer mode at booting time. */
1624 cmd
.cmdidx
= MMC_CMD_SWITCH
;
1625 cmd
.resp_type
= MMC_RSP_R1b
;
1627 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
1628 (EXT_CSD_BOOT_BUS_WIDTH
<< 16) |
1631 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1633 debug("mmc boot partition#%d open fail:Error2 = %d\n",