1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * MultiMediaCard (MMC), eMMC and Secure Digital (SD) common initialization
4 * code which brings the card into the standby state. This code is controller
8 #include <commonlib/storage.h>
17 uint64_t sd_mmc_extract_uint32_bits(const uint32_t *array
, int start
, int count
)
22 for (i
= 0; i
< count
; i
++, start
++) {
24 value
|= (array
[start
/ 32] >> (31 - (start
% 32))) & 0x1;
29 static uint32_t sd_mmc_calculate_transfer_speed(uint32_t csd0
)
33 /* frequency bases, divided by 10 to be nice to platforms without
35 static const int fbase
[] = {
41 /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
42 * to platforms without floating point. */
43 static const int multipliers
[] = {
62 /* divide frequency by 10, since the mults are 10x bigger */
63 freq
= fbase
[csd0
& 0x7];
64 mult
= multipliers
[(csd0
>> 3) & 0xf];
68 int sd_mmc_go_idle(struct storage_media
*media
)
70 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
72 // Some cards can't accept idle commands without delay.
73 if (ctrlr
->mdelay_before_cmd0
)
74 mdelay(ctrlr
->mdelay_before_cmd0
);
76 struct mmc_command cmd
;
77 cmd
.cmdidx
= MMC_CMD_GO_IDLE_STATE
;
79 cmd
.resp_type
= CARD_RSP_NONE
;
82 int err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
86 // Some cards need more than half second to respond to next command (ex,
88 if (ctrlr
->mdelay_after_cmd0
)
89 mdelay(ctrlr
->mdelay_after_cmd0
);
94 int sd_mmc_send_status(struct storage_media
*media
, ssize_t tries
)
96 struct mmc_command cmd
;
97 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
99 cmd
.cmdidx
= MMC_CMD_SEND_STATUS
;
100 cmd
.resp_type
= CARD_RSP_R1
;
101 cmd
.cmdarg
= media
->rca
<< 16;
105 int err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
108 else if (cmd
.response
[0] & MMC_STATUS_RDY_FOR_DATA
)
110 else if (cmd
.response
[0] & MMC_STATUS_MASK
) {
111 sd_mmc_error("Status Error: %#8.8x\n", cmd
.response
[0]);
112 return CARD_COMM_ERR
;
118 sd_mmc_trace("CURR STATE:%d\n",
119 (cmd
.response
[0] & MMC_STATUS_CURR_STATE
) >> 9);
122 sd_mmc_error("Timeout waiting card ready\n");
128 int sd_mmc_set_blocklen(struct sd_mmc_ctrlr
*ctrlr
, int len
)
130 struct mmc_command cmd
;
131 cmd
.cmdidx
= MMC_CMD_SET_BLOCKLEN
;
132 cmd
.resp_type
= CARD_RSP_R1
;
136 return ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
139 int sd_mmc_enter_standby(struct storage_media
*media
)
141 struct mmc_command cmd
;
142 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
145 SET_BUS_WIDTH(ctrlr
, 1);
149 err
= sd_mmc_go_idle(media
);
153 /* Test for SD version 2 */
155 if (CONFIG(COMMONLIB_STORAGE_SD
)) {
156 err
= sd_send_if_cond(media
);
158 /* Get SD card operating condition */
160 err
= sd_send_op_cond(media
);
163 /* If the command timed out, we check for an MMC card */
164 if (CONFIG(COMMONLIB_STORAGE_MMC
) && (err
== CARD_TIMEOUT
)) {
165 /* Some cards seem to need this */
166 sd_mmc_go_idle(media
);
168 err
= mmc_send_op_cond(media
);
169 if (err
== CARD_IN_PROGRESS
)
170 err
= mmc_complete_op_cond(media
);
175 "Card did not respond to voltage select!\n");
176 return CARD_UNUSABLE_ERR
;
179 /* Put the Card in Identify Mode */
180 cmd
.cmdidx
= MMC_CMD_ALL_SEND_CID
;
181 cmd
.resp_type
= CARD_RSP_R2
;
184 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
187 memcpy(media
->cid
, cmd
.response
, sizeof(media
->cid
));
190 * For MMC cards, set the Relative Address.
191 * For SD cards, get the Relative Address.
192 * This also puts the cards into Standby State
194 cmd
.cmdidx
= SD_CMD_SEND_RELATIVE_ADDR
;
195 cmd
.cmdarg
= media
->rca
<< 16;
196 cmd
.resp_type
= CARD_RSP_R6
;
198 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
202 media
->rca
= (cmd
.response
[0] >> 16) & 0xffff;
204 /* Get the Card-Specific Data */
205 cmd
.cmdidx
= MMC_CMD_SEND_CSD
;
206 cmd
.resp_type
= CARD_RSP_R2
;
207 cmd
.cmdarg
= media
->rca
<< 16;
209 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
211 /* Waiting for the ready status */
212 sd_mmc_send_status(media
, SD_MMC_IO_RETRIES
);
216 memcpy(media
->csd
, cmd
.response
, sizeof(media
->csd
));
217 if (media
->version
== MMC_VERSION_UNKNOWN
) {
218 int version
= sd_mmc_extract_uint32_bits(media
->csd
, 2, 4);
221 media
->version
= MMC_VERSION_1_2
;
224 media
->version
= MMC_VERSION_1_4
;
227 media
->version
= MMC_VERSION_2_2
;
230 media
->version
= MMC_VERSION_3
;
233 media
->version
= MMC_VERSION_4
;
236 media
->version
= MMC_VERSION_1_2
;
240 media
->tran_speed
= sd_mmc_calculate_transfer_speed(media
->csd
[0]);
242 /* Determine the read and write block lengths */
243 media
->read_bl_len
= 1 << sd_mmc_extract_uint32_bits(media
->csd
, 44, 4);
245 media
->write_bl_len
= media
->read_bl_len
;
247 media
->write_bl_len
=
248 1 << sd_mmc_extract_uint32_bits(media
->csd
, 102, 4);
250 sd_mmc_debug("mmc media info: version=%#x, tran_speed=%d\n",
251 media
->version
, (int)media
->tran_speed
);