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
, unsigned long 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 0x%lx~0x%lx\n\n",
270 mmc
->erase_grp_size
, start
& ~(mmc
->erase_grp_size
- 1),
271 ((start
+ blkcnt
+ mmc
->erase_grp_size
)
272 & ~(mmc
->erase_grp_size
- 1)) - 1);
274 while (blk
< blkcnt
) {
275 blk_r
= ((blkcnt
- blk
) > mmc
->erase_grp_size
) ?
276 mmc
->erase_grp_size
: (blkcnt
- blk
);
277 err
= mmc_erase_t(mmc
, start
+ blk
, blk_r
);
283 /* Waiting for the ready status */
284 if (mmc_send_status(mmc
, timeout
))
292 mmc_write_blocks(struct mmc
*mmc
, ulong start
, lbaint_t blkcnt
, const void*src
)
295 struct mmc_data data
;
298 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
299 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
300 start
+ blkcnt
, mmc
->block_dev
.lba
);
306 else if (blkcnt
== 1)
307 cmd
.cmdidx
= MMC_CMD_WRITE_SINGLE_BLOCK
;
309 cmd
.cmdidx
= MMC_CMD_WRITE_MULTIPLE_BLOCK
;
311 if (mmc
->high_capacity
)
314 cmd
.cmdarg
= start
* mmc
->write_bl_len
;
316 cmd
.resp_type
= MMC_RSP_R1
;
319 data
.blocks
= blkcnt
;
320 data
.blocksize
= mmc
->write_bl_len
;
321 data
.flags
= MMC_DATA_WRITE
;
323 if (mmc_send_cmd(mmc
, &cmd
, &data
)) {
324 printf("mmc write failed\n");
328 /* SPI multiblock writes terminate using a special
329 * token, not a STOP_TRANSMISSION request.
331 if (!mmc_host_is_spi(mmc
) && blkcnt
> 1) {
332 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
334 cmd
.resp_type
= MMC_RSP_R1b
;
335 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
336 printf("mmc fail to send stop cmd\n");
341 /* Waiting for the ready status */
342 if (mmc_send_status(mmc
, timeout
))
349 mmc_bwrite(int dev_num
, ulong start
, lbaint_t blkcnt
, const void*src
)
351 lbaint_t cur
, blocks_todo
= blkcnt
;
353 struct mmc
*mmc
= find_mmc_device(dev_num
);
357 if (mmc_set_blocklen(mmc
, mmc
->write_bl_len
))
361 cur
= (blocks_todo
> mmc
->b_max
) ? mmc
->b_max
: blocks_todo
;
362 if(mmc_write_blocks(mmc
, start
, cur
, src
) != cur
)
366 src
+= cur
* mmc
->write_bl_len
;
367 } while (blocks_todo
> 0);
372 static int mmc_read_blocks(struct mmc
*mmc
, void *dst
, ulong start
,
376 struct mmc_data data
;
379 cmd
.cmdidx
= MMC_CMD_READ_MULTIPLE_BLOCK
;
381 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
383 if (mmc
->high_capacity
)
386 cmd
.cmdarg
= start
* mmc
->read_bl_len
;
388 cmd
.resp_type
= MMC_RSP_R1
;
391 data
.blocks
= blkcnt
;
392 data
.blocksize
= mmc
->read_bl_len
;
393 data
.flags
= MMC_DATA_READ
;
395 if (mmc_send_cmd(mmc
, &cmd
, &data
))
399 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
401 cmd
.resp_type
= MMC_RSP_R1b
;
402 if (mmc_send_cmd(mmc
, &cmd
, NULL
)) {
403 printf("mmc fail to send stop cmd\n");
411 static ulong
mmc_bread(int dev_num
, ulong start
, lbaint_t blkcnt
, void *dst
)
413 lbaint_t cur
, blocks_todo
= blkcnt
;
418 struct mmc
*mmc
= find_mmc_device(dev_num
);
422 if ((start
+ blkcnt
) > mmc
->block_dev
.lba
) {
423 printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
424 start
+ blkcnt
, mmc
->block_dev
.lba
);
428 if (mmc_set_blocklen(mmc
, mmc
->read_bl_len
))
432 cur
= (blocks_todo
> mmc
->b_max
) ? mmc
->b_max
: blocks_todo
;
433 if(mmc_read_blocks(mmc
, dst
, start
, cur
) != cur
)
437 dst
+= cur
* mmc
->read_bl_len
;
438 } while (blocks_todo
> 0);
443 static int mmc_go_idle(struct mmc
*mmc
)
450 cmd
.cmdidx
= MMC_CMD_GO_IDLE_STATE
;
452 cmd
.resp_type
= MMC_RSP_NONE
;
454 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
464 static int sd_send_op_cond(struct mmc
*mmc
)
471 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
472 cmd
.resp_type
= MMC_RSP_R1
;
475 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
480 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
481 cmd
.resp_type
= MMC_RSP_R3
;
484 * Most cards do not answer if some reserved bits
485 * in the ocr are set. However, Some controller
486 * can set bit 7 (reserved for low voltages), but
487 * how to manage low voltages SD card is not yet
490 cmd
.cmdarg
= mmc_host_is_spi(mmc
) ? 0 :
491 (mmc
->voltages
& 0xff8000);
493 if (mmc
->version
== SD_VERSION_2
)
494 cmd
.cmdarg
|= OCR_HCS
;
496 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
502 } while ((!(cmd
.response
[0] & OCR_BUSY
)) && timeout
--);
507 if (mmc
->version
!= SD_VERSION_2
)
508 mmc
->version
= SD_VERSION_1_0
;
510 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
511 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
512 cmd
.resp_type
= MMC_RSP_R3
;
515 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
521 mmc
->ocr
= cmd
.response
[0];
523 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
529 /* We pass in the cmd since otherwise the init seems to fail */
530 static int mmc_send_op_cond_iter(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
535 cmd
->cmdidx
= MMC_CMD_SEND_OP_COND
;
536 cmd
->resp_type
= MMC_RSP_R3
;
538 if (use_arg
&& !mmc_host_is_spi(mmc
)) {
541 (mmc
->op_cond_response
& OCR_VOLTAGE_MASK
)) |
542 (mmc
->op_cond_response
& OCR_ACCESS_MODE
);
544 if (mmc
->host_caps
& MMC_MODE_HC
)
545 cmd
->cmdarg
|= OCR_HCS
;
547 err
= mmc_send_cmd(mmc
, cmd
, NULL
);
550 mmc
->op_cond_response
= cmd
->response
[0];
554 int mmc_send_op_cond(struct mmc
*mmc
)
559 /* Some cards seem to need this */
562 /* Asking to the card its capabilities */
563 mmc
->op_cond_pending
= 1;
564 for (i
= 0; i
< 2; i
++) {
565 err
= mmc_send_op_cond_iter(mmc
, &cmd
, i
!= 0);
569 /* exit if not busy (flag seems to be inverted) */
570 if (mmc
->op_cond_response
& OCR_BUSY
)
576 int mmc_complete_op_cond(struct mmc
*mmc
)
583 mmc
->op_cond_pending
= 0;
584 start
= get_timer(0);
586 err
= mmc_send_op_cond_iter(mmc
, &cmd
, 1);
589 if (get_timer(start
) > timeout
)
592 } while (!(mmc
->op_cond_response
& OCR_BUSY
));
594 if (mmc_host_is_spi(mmc
)) { /* read OCR for spi */
595 cmd
.cmdidx
= MMC_CMD_SPI_READ_OCR
;
596 cmd
.resp_type
= MMC_RSP_R3
;
599 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
605 mmc
->version
= MMC_VERSION_UNKNOWN
;
606 mmc
->ocr
= cmd
.response
[0];
608 mmc
->high_capacity
= ((mmc
->ocr
& OCR_HCS
) == OCR_HCS
);
615 static int mmc_send_ext_csd(struct mmc
*mmc
, u8
*ext_csd
)
618 struct mmc_data data
;
621 /* Get the Card Status Register */
622 cmd
.cmdidx
= MMC_CMD_SEND_EXT_CSD
;
623 cmd
.resp_type
= MMC_RSP_R1
;
626 data
.dest
= (char *)ext_csd
;
628 data
.blocksize
= MMC_MAX_BLOCK_LEN
;
629 data
.flags
= MMC_DATA_READ
;
631 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
637 static int mmc_switch(struct mmc
*mmc
, u8 set
, u8 index
, u8 value
)
643 cmd
.cmdidx
= MMC_CMD_SWITCH
;
644 cmd
.resp_type
= MMC_RSP_R1b
;
645 cmd
.cmdarg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
649 ret
= mmc_send_cmd(mmc
, &cmd
, NULL
);
651 /* Waiting for the ready status */
653 ret
= mmc_send_status(mmc
, timeout
);
659 static int mmc_change_freq(struct mmc
*mmc
)
661 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
667 if (mmc_host_is_spi(mmc
))
670 /* Only version 4 supports high-speed */
671 if (mmc
->version
< MMC_VERSION_4
)
674 err
= mmc_send_ext_csd(mmc
, ext_csd
);
679 cardtype
= ext_csd
[EXT_CSD_CARD_TYPE
] & 0xf;
681 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_HS_TIMING
, 1);
686 /* Now check to see that it worked */
687 err
= mmc_send_ext_csd(mmc
, ext_csd
);
692 /* No high-speed support */
693 if (!ext_csd
[EXT_CSD_HS_TIMING
])
696 /* High Speed is set, there are two types: 52MHz and 26MHz */
697 if (cardtype
& MMC_HS_52MHZ
)
698 mmc
->card_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
700 mmc
->card_caps
|= MMC_MODE_HS
;
705 static int mmc_set_capacity(struct mmc
*mmc
, int part_num
)
709 mmc
->capacity
= mmc
->capacity_user
;
713 mmc
->capacity
= mmc
->capacity_boot
;
716 mmc
->capacity
= mmc
->capacity_rpmb
;
722 mmc
->capacity
= mmc
->capacity_gp
[part_num
- 4];
728 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
733 int mmc_switch_part(int dev_num
, unsigned int part_num
)
735 struct mmc
*mmc
= find_mmc_device(dev_num
);
741 ret
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONF
,
742 (mmc
->part_config
& ~PART_ACCESS_MASK
)
743 | (part_num
& PART_ACCESS_MASK
));
747 return mmc_set_capacity(mmc
, part_num
);
750 int mmc_getcd(struct mmc
*mmc
)
754 cd
= board_mmc_getcd(mmc
);
758 cd
= mmc
->getcd(mmc
);
766 static int sd_switch(struct mmc
*mmc
, int mode
, int group
, u8 value
, u8
*resp
)
769 struct mmc_data data
;
771 /* Switch the frequency */
772 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
773 cmd
.resp_type
= MMC_RSP_R1
;
774 cmd
.cmdarg
= (mode
<< 31) | 0xffffff;
775 cmd
.cmdarg
&= ~(0xf << (group
* 4));
776 cmd
.cmdarg
|= value
<< (group
* 4);
778 data
.dest
= (char *)resp
;
781 data
.flags
= MMC_DATA_READ
;
783 return mmc_send_cmd(mmc
, &cmd
, &data
);
787 static int sd_change_freq(struct mmc
*mmc
)
791 ALLOC_CACHE_ALIGN_BUFFER(uint
, scr
, 2);
792 ALLOC_CACHE_ALIGN_BUFFER(uint
, switch_status
, 16);
793 struct mmc_data data
;
798 if (mmc_host_is_spi(mmc
))
801 /* Read the SCR to find out if this card supports higher speeds */
802 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
803 cmd
.resp_type
= MMC_RSP_R1
;
804 cmd
.cmdarg
= mmc
->rca
<< 16;
806 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
811 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
812 cmd
.resp_type
= MMC_RSP_R1
;
818 data
.dest
= (char *)scr
;
821 data
.flags
= MMC_DATA_READ
;
823 err
= mmc_send_cmd(mmc
, &cmd
, &data
);
832 mmc
->scr
[0] = __be32_to_cpu(scr
[0]);
833 mmc
->scr
[1] = __be32_to_cpu(scr
[1]);
835 switch ((mmc
->scr
[0] >> 24) & 0xf) {
837 mmc
->version
= SD_VERSION_1_0
;
840 mmc
->version
= SD_VERSION_1_10
;
843 mmc
->version
= SD_VERSION_2
;
844 if ((mmc
->scr
[0] >> 15) & 0x1)
845 mmc
->version
= SD_VERSION_3
;
848 mmc
->version
= SD_VERSION_1_0
;
852 if (mmc
->scr
[0] & SD_DATA_4BIT
)
853 mmc
->card_caps
|= MMC_MODE_4BIT
;
855 /* Version 1.0 doesn't support switching */
856 if (mmc
->version
== SD_VERSION_1_0
)
861 err
= sd_switch(mmc
, SD_SWITCH_CHECK
, 0, 1,
862 (u8
*)switch_status
);
867 /* The high-speed function is busy. Try again */
868 if (!(__be32_to_cpu(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
872 /* If high-speed isn't supported, we return */
873 if (!(__be32_to_cpu(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
))
877 * If the host doesn't support SD_HIGHSPEED, do not switch card to
878 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
879 * This can avoid furthur problem when the card runs in different
880 * mode between the host.
882 if (!((mmc
->host_caps
& MMC_MODE_HS_52MHz
) &&
883 (mmc
->host_caps
& MMC_MODE_HS
)))
886 err
= sd_switch(mmc
, SD_SWITCH_SWITCH
, 0, 1, (u8
*)switch_status
);
891 if ((__be32_to_cpu(switch_status
[4]) & 0x0f000000) == 0x01000000)
892 mmc
->card_caps
|= MMC_MODE_HS
;
897 /* frequency bases */
898 /* divided by 10 to be nice to platforms without floating point */
899 static const int fbase
[] = {
906 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
907 * to platforms without floating point.
909 static const int multipliers
[] = {
928 static void mmc_set_ios(struct mmc
*mmc
)
933 void mmc_set_clock(struct mmc
*mmc
, uint clock
)
935 if (clock
> mmc
->f_max
)
938 if (clock
< mmc
->f_min
)
946 static void mmc_set_bus_width(struct mmc
*mmc
, uint width
)
948 mmc
->bus_width
= width
;
953 static int mmc_startup(struct mmc
*mmc
)
957 u64 cmult
, csize
, capacity
;
959 ALLOC_CACHE_ALIGN_BUFFER(u8
, ext_csd
, MMC_MAX_BLOCK_LEN
);
960 ALLOC_CACHE_ALIGN_BUFFER(u8
, test_csd
, MMC_MAX_BLOCK_LEN
);
963 #ifdef CONFIG_MMC_SPI_CRC_ON
964 if (mmc_host_is_spi(mmc
)) { /* enable CRC check for spi */
965 cmd
.cmdidx
= MMC_CMD_SPI_CRC_ON_OFF
;
966 cmd
.resp_type
= MMC_RSP_R1
;
968 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
975 /* Put the Card in Identify Mode */
976 cmd
.cmdidx
= mmc_host_is_spi(mmc
) ? MMC_CMD_SEND_CID
:
977 MMC_CMD_ALL_SEND_CID
; /* cmd not supported in spi */
978 cmd
.resp_type
= MMC_RSP_R2
;
981 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
986 memcpy(mmc
->cid
, cmd
.response
, 16);
989 * For MMC cards, set the Relative Address.
990 * For SD cards, get the Relatvie Address.
991 * This also puts the cards into Standby State
993 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
994 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
995 cmd
.cmdarg
= mmc
->rca
<< 16;
996 cmd
.resp_type
= MMC_RSP_R6
;
998 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1004 mmc
->rca
= (cmd
.response
[0] >> 16) & 0xffff;
1007 /* Get the Card-Specific Data */
1008 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
1009 cmd
.resp_type
= MMC_RSP_R2
;
1010 cmd
.cmdarg
= mmc
->rca
<< 16;
1012 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1014 /* Waiting for the ready status */
1015 mmc_send_status(mmc
, timeout
);
1020 mmc
->csd
[0] = cmd
.response
[0];
1021 mmc
->csd
[1] = cmd
.response
[1];
1022 mmc
->csd
[2] = cmd
.response
[2];
1023 mmc
->csd
[3] = cmd
.response
[3];
1025 if (mmc
->version
== MMC_VERSION_UNKNOWN
) {
1026 int version
= (cmd
.response
[0] >> 26) & 0xf;
1030 mmc
->version
= MMC_VERSION_1_2
;
1033 mmc
->version
= MMC_VERSION_1_4
;
1036 mmc
->version
= MMC_VERSION_2_2
;
1039 mmc
->version
= MMC_VERSION_3
;
1042 mmc
->version
= MMC_VERSION_4
;
1045 mmc
->version
= MMC_VERSION_1_2
;
1050 /* divide frequency by 10, since the mults are 10x bigger */
1051 freq
= fbase
[(cmd
.response
[0] & 0x7)];
1052 mult
= multipliers
[((cmd
.response
[0] >> 3) & 0xf)];
1054 mmc
->tran_speed
= freq
* mult
;
1056 mmc
->read_bl_len
= 1 << ((cmd
.response
[1] >> 16) & 0xf);
1059 mmc
->write_bl_len
= mmc
->read_bl_len
;
1061 mmc
->write_bl_len
= 1 << ((cmd
.response
[3] >> 22) & 0xf);
1063 if (mmc
->high_capacity
) {
1064 csize
= (mmc
->csd
[1] & 0x3f) << 16
1065 | (mmc
->csd
[2] & 0xffff0000) >> 16;
1068 csize
= (mmc
->csd
[1] & 0x3ff) << 2
1069 | (mmc
->csd
[2] & 0xc0000000) >> 30;
1070 cmult
= (mmc
->csd
[2] & 0x00038000) >> 15;
1073 mmc
->capacity_user
= (csize
+ 1) << (cmult
+ 2);
1074 mmc
->capacity_user
*= mmc
->read_bl_len
;
1075 mmc
->capacity_boot
= 0;
1076 mmc
->capacity_rpmb
= 0;
1077 for (i
= 0; i
< 4; i
++)
1078 mmc
->capacity_gp
[i
] = 0;
1080 if (mmc
->read_bl_len
> MMC_MAX_BLOCK_LEN
)
1081 mmc
->read_bl_len
= MMC_MAX_BLOCK_LEN
;
1083 if (mmc
->write_bl_len
> MMC_MAX_BLOCK_LEN
)
1084 mmc
->write_bl_len
= MMC_MAX_BLOCK_LEN
;
1086 /* Select the card, and put it into Transfer Mode */
1087 if (!mmc_host_is_spi(mmc
)) { /* cmd not supported in spi */
1088 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
1089 cmd
.resp_type
= MMC_RSP_R1
;
1090 cmd
.cmdarg
= mmc
->rca
<< 16;
1091 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1098 * For SD, its erase group is always one sector
1100 mmc
->erase_grp_size
= 1;
1101 mmc
->part_config
= MMCPART_NOAVAILABLE
;
1102 if (!IS_SD(mmc
) && (mmc
->version
>= MMC_VERSION_4
)) {
1103 /* check ext_csd version and capacity */
1104 err
= mmc_send_ext_csd(mmc
, ext_csd
);
1105 if (!err
&& (ext_csd
[EXT_CSD_REV
] >= 2)) {
1107 * According to the JEDEC Standard, the value of
1108 * ext_csd's capacity is valid if the value is more
1111 capacity
= ext_csd
[EXT_CSD_SEC_CNT
] << 0
1112 | ext_csd
[EXT_CSD_SEC_CNT
+ 1] << 8
1113 | ext_csd
[EXT_CSD_SEC_CNT
+ 2] << 16
1114 | ext_csd
[EXT_CSD_SEC_CNT
+ 3] << 24;
1115 capacity
*= MMC_MAX_BLOCK_LEN
;
1116 if ((capacity
>> 20) > 2 * 1024)
1117 mmc
->capacity_user
= capacity
;
1120 switch (ext_csd
[EXT_CSD_REV
]) {
1122 mmc
->version
= MMC_VERSION_4_1
;
1125 mmc
->version
= MMC_VERSION_4_2
;
1128 mmc
->version
= MMC_VERSION_4_3
;
1131 mmc
->version
= MMC_VERSION_4_41
;
1134 mmc
->version
= MMC_VERSION_4_5
;
1139 * Check whether GROUP_DEF is set, if yes, read out
1140 * group size from ext_csd directly, or calculate
1141 * the group size from the csd value.
1143 if (ext_csd
[EXT_CSD_ERASE_GROUP_DEF
]) {
1144 mmc
->erase_grp_size
=
1145 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] *
1146 MMC_MAX_BLOCK_LEN
* 1024;
1148 int erase_gsz
, erase_gmul
;
1149 erase_gsz
= (mmc
->csd
[2] & 0x00007c00) >> 10;
1150 erase_gmul
= (mmc
->csd
[2] & 0x000003e0) >> 5;
1151 mmc
->erase_grp_size
= (erase_gsz
+ 1)
1155 /* store the partition info of emmc */
1156 if ((ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] & PART_SUPPORT
) ||
1157 ext_csd
[EXT_CSD_BOOT_MULT
])
1158 mmc
->part_config
= ext_csd
[EXT_CSD_PART_CONF
];
1160 mmc
->capacity_boot
= ext_csd
[EXT_CSD_BOOT_MULT
] << 17;
1162 mmc
->capacity_rpmb
= ext_csd
[EXT_CSD_RPMB_MULT
] << 17;
1164 for (i
= 0; i
< 4; i
++) {
1165 int idx
= EXT_CSD_GP_SIZE_MULT
+ i
* 3;
1166 mmc
->capacity_gp
[i
] = (ext_csd
[idx
+ 2] << 16) +
1167 (ext_csd
[idx
+ 1] << 8) + ext_csd
[idx
];
1168 mmc
->capacity_gp
[i
] *=
1169 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
];
1170 mmc
->capacity_gp
[i
] *= ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
];
1174 err
= mmc_set_capacity(mmc
, mmc
->part_num
);
1179 err
= sd_change_freq(mmc
);
1181 err
= mmc_change_freq(mmc
);
1186 /* Restrict card's capabilities by what the host can do */
1187 mmc
->card_caps
&= mmc
->host_caps
;
1190 if (mmc
->card_caps
& MMC_MODE_4BIT
) {
1191 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
1192 cmd
.resp_type
= MMC_RSP_R1
;
1193 cmd
.cmdarg
= mmc
->rca
<< 16;
1195 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1199 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
1200 cmd
.resp_type
= MMC_RSP_R1
;
1202 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1206 mmc_set_bus_width(mmc
, 4);
1209 if (mmc
->card_caps
& MMC_MODE_HS
)
1210 mmc
->tran_speed
= 50000000;
1212 mmc
->tran_speed
= 25000000;
1216 /* An array of possible bus widths in order of preference */
1217 static unsigned ext_csd_bits
[] = {
1218 EXT_CSD_BUS_WIDTH_8
,
1219 EXT_CSD_BUS_WIDTH_4
,
1220 EXT_CSD_BUS_WIDTH_1
,
1223 /* An array to map CSD bus widths to host cap bits */
1224 static unsigned ext_to_hostcaps
[] = {
1225 [EXT_CSD_BUS_WIDTH_4
] = MMC_MODE_4BIT
,
1226 [EXT_CSD_BUS_WIDTH_8
] = MMC_MODE_8BIT
,
1229 /* An array to map chosen bus width to an integer */
1230 static unsigned widths
[] = {
1234 for (idx
=0; idx
< ARRAY_SIZE(ext_csd_bits
); idx
++) {
1235 unsigned int extw
= ext_csd_bits
[idx
];
1238 * Check to make sure the controller supports
1239 * this bus width, if it's more than 1
1241 if (extw
!= EXT_CSD_BUS_WIDTH_1
&&
1242 !(mmc
->host_caps
& ext_to_hostcaps
[extw
]))
1245 err
= mmc_switch(mmc
, EXT_CSD_CMD_SET_NORMAL
,
1246 EXT_CSD_BUS_WIDTH
, extw
);
1251 mmc_set_bus_width(mmc
, widths
[idx
]);
1253 err
= mmc_send_ext_csd(mmc
, test_csd
);
1254 if (!err
&& ext_csd
[EXT_CSD_PARTITIONING_SUPPORT
] \
1255 == test_csd
[EXT_CSD_PARTITIONING_SUPPORT
]
1256 && ext_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1257 == test_csd
[EXT_CSD_ERASE_GROUP_DEF
] \
1258 && ext_csd
[EXT_CSD_REV
] \
1259 == test_csd
[EXT_CSD_REV
]
1260 && ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] \
1261 == test_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
]
1262 && memcmp(&ext_csd
[EXT_CSD_SEC_CNT
], \
1263 &test_csd
[EXT_CSD_SEC_CNT
], 4) == 0) {
1265 mmc
->card_caps
|= ext_to_hostcaps
[extw
];
1270 if (mmc
->card_caps
& MMC_MODE_HS
) {
1271 if (mmc
->card_caps
& MMC_MODE_HS_52MHz
)
1272 mmc
->tran_speed
= 52000000;
1274 mmc
->tran_speed
= 26000000;
1278 mmc_set_clock(mmc
, mmc
->tran_speed
);
1280 /* fill in device description */
1281 mmc
->block_dev
.lun
= 0;
1282 mmc
->block_dev
.type
= 0;
1283 mmc
->block_dev
.blksz
= mmc
->read_bl_len
;
1284 mmc
->block_dev
.log2blksz
= LOG2(mmc
->block_dev
.blksz
);
1285 mmc
->block_dev
.lba
= lldiv(mmc
->capacity
, mmc
->read_bl_len
);
1286 sprintf(mmc
->block_dev
.vendor
, "Man %06x Snr %04x%04x",
1287 mmc
->cid
[0] >> 24, (mmc
->cid
[2] & 0xffff),
1288 (mmc
->cid
[3] >> 16) & 0xffff);
1289 sprintf(mmc
->block_dev
.product
, "%c%c%c%c%c%c", mmc
->cid
[0] & 0xff,
1290 (mmc
->cid
[1] >> 24), (mmc
->cid
[1] >> 16) & 0xff,
1291 (mmc
->cid
[1] >> 8) & 0xff, mmc
->cid
[1] & 0xff,
1292 (mmc
->cid
[2] >> 24) & 0xff);
1293 sprintf(mmc
->block_dev
.revision
, "%d.%d", (mmc
->cid
[2] >> 20) & 0xf,
1294 (mmc
->cid
[2] >> 16) & 0xf);
1295 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1296 init_part(&mmc
->block_dev
);
1302 static int mmc_send_if_cond(struct mmc
*mmc
)
1307 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
1308 /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1309 cmd
.cmdarg
= ((mmc
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
1310 cmd
.resp_type
= MMC_RSP_R7
;
1312 err
= mmc_send_cmd(mmc
, &cmd
, NULL
);
1317 if ((cmd
.response
[0] & 0xff) != 0xaa)
1318 return UNUSABLE_ERR
;
1320 mmc
->version
= SD_VERSION_2
;
1325 int mmc_register(struct mmc
*mmc
)
1327 /* Setup the universal parts of the block interface just once */
1328 mmc
->block_dev
.if_type
= IF_TYPE_MMC
;
1329 mmc
->block_dev
.dev
= cur_dev_num
++;
1330 mmc
->block_dev
.removable
= 1;
1331 mmc
->block_dev
.block_read
= mmc_bread
;
1332 mmc
->block_dev
.block_write
= mmc_bwrite
;
1333 mmc
->block_dev
.block_erase
= mmc_berase
;
1335 mmc
->b_max
= CONFIG_SYS_MMC_MAX_BLK_COUNT
;
1337 INIT_LIST_HEAD (&mmc
->link
);
1339 list_add_tail (&mmc
->link
, &mmc_devices
);
1344 #ifdef CONFIG_PARTITIONS
1345 block_dev_desc_t
*mmc_get_dev(int dev
)
1347 struct mmc
*mmc
= find_mmc_device(dev
);
1348 if (!mmc
|| mmc_init(mmc
))
1351 return &mmc
->block_dev
;
1355 int mmc_start_init(struct mmc
*mmc
)
1359 if (mmc_getcd(mmc
) == 0) {
1361 printf("MMC: no card present\n");
1368 err
= mmc
->init(mmc
);
1373 mmc_set_bus_width(mmc
, 1);
1374 mmc_set_clock(mmc
, 1);
1376 /* Reset the Card */
1377 err
= mmc_go_idle(mmc
);
1382 /* The internal partition reset to user partition(0) at every CMD0*/
1385 /* Test for SD version 2 */
1386 err
= mmc_send_if_cond(mmc
);
1388 /* Now try to get the SD card's operating condition */
1389 err
= sd_send_op_cond(mmc
);
1391 /* If the command timed out, we check for an MMC card */
1392 if (err
== TIMEOUT
) {
1393 err
= mmc_send_op_cond(mmc
);
1395 if (err
&& err
!= IN_PROGRESS
) {
1396 printf("Card did not respond to voltage select!\n");
1397 return UNUSABLE_ERR
;
1401 if (err
== IN_PROGRESS
)
1402 mmc
->init_in_progress
= 1;
1407 static int mmc_complete_init(struct mmc
*mmc
)
1411 if (mmc
->op_cond_pending
)
1412 err
= mmc_complete_op_cond(mmc
);
1415 err
= mmc_startup(mmc
);
1420 mmc
->init_in_progress
= 0;
1424 int mmc_init(struct mmc
*mmc
)
1426 int err
= IN_PROGRESS
;
1427 unsigned start
= get_timer(0);
1431 if (!mmc
->init_in_progress
)
1432 err
= mmc_start_init(mmc
);
1434 if (!err
|| err
== IN_PROGRESS
)
1435 err
= mmc_complete_init(mmc
);
1436 debug("%s: %d, time %lu\n", __func__
, err
, get_timer(start
));
1441 * CPU and board-specific MMC initializations. Aliased function
1442 * signals caller to move on
1444 static int __def_mmc_init(bd_t
*bis
)
1449 int cpu_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1450 int board_mmc_init(bd_t
*bis
) __attribute__((weak
, alias("__def_mmc_init")));
1452 void print_mmc_devices(char separator
)
1455 struct list_head
*entry
;
1457 list_for_each(entry
, &mmc_devices
) {
1458 m
= list_entry(entry
, struct mmc
, link
);
1460 printf("%s: %d", m
->name
, m
->block_dev
.dev
);
1462 if (entry
->next
!= &mmc_devices
)
1463 printf("%c ", separator
);
1469 int get_mmc_num(void)
1474 void mmc_set_preinit(struct mmc
*mmc
, int preinit
)
1476 mmc
->preinit
= preinit
;
1479 static void do_preinit(void)
1482 struct list_head
*entry
;
1484 list_for_each(entry
, &mmc_devices
) {
1485 m
= list_entry(entry
, struct mmc
, link
);
1493 int mmc_initialize(bd_t
*bis
)
1495 INIT_LIST_HEAD (&mmc_devices
);
1498 if (board_mmc_init(bis
) < 0)
1501 print_mmc_devices(',');