1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Secure Digital (SD) card specific support code
4 * This code is controller independent
7 #include <commonlib/sd_mmc_ctrlr.h>
8 #include <commonlib/storage.h>
15 int sd_send_if_cond(struct storage_media
*media
)
17 struct mmc_command cmd
;
18 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
20 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
21 /* Set if controller supports voltages between 2.7 and 3.6 V. */
22 cmd
.cmdarg
= ((ctrlr
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
23 cmd
.resp_type
= CARD_RSP_R7
;
25 int err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
29 if ((cmd
.response
[0] & 0xff) != 0xaa)
30 return CARD_UNUSABLE_ERR
;
31 media
->version
= SD_VERSION_2
;
35 int sd_send_op_cond(struct storage_media
*media
)
38 struct mmc_command cmd
;
39 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
41 int tries
= SD_MMC_IO_RETRIES
;
43 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
44 cmd
.resp_type
= CARD_RSP_R1
;
48 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
52 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
53 cmd
.resp_type
= CARD_RSP_R3
;
56 * Most cards do not answer if some reserved bits
57 * in the ocr are set. However, Some controller
58 * can set bit 7 (reserved for low voltages), but
59 * how to manage low voltages SD card is not yet
62 cmd
.cmdarg
= (ctrlr
->voltages
& 0xff8000);
64 if (media
->version
== SD_VERSION_2
)
65 cmd
.cmdarg
|= OCR_HCS
;
67 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
71 // OCR_BUSY means "initialization complete".
72 if (cmd
.response
[0] & OCR_BUSY
)
78 return CARD_UNUSABLE_ERR
;
80 if (media
->version
!= SD_VERSION_2
)
81 media
->version
= SD_VERSION_1_0
;
83 media
->ocr
= cmd
.response
[0];
84 media
->high_capacity
= ((media
->ocr
& OCR_HCS
) == OCR_HCS
);
89 static int sd_switch(struct sd_mmc_ctrlr
*ctrlr
, int mode
, int group
,
90 uint8_t value
, uint8_t *resp
)
92 /* Switch the frequency */
93 struct mmc_command cmd
;
94 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
95 cmd
.resp_type
= CARD_RSP_R1
;
96 cmd
.cmdarg
= (mode
<< 31) | (0xffffff & ~(0xf << (group
* 4))) |
97 (value
<< (group
* 4));
100 struct mmc_data data
;
101 data
.dest
= (char *)resp
;
104 data
.flags
= DATA_FLAG_READ
;
106 return ctrlr
->send_cmd(ctrlr
, &cmd
, &data
);
109 static void sd_recalculate_clock(struct storage_media
*media
)
113 if (media
->caps
& DRVR_CAP_HS
)
117 SET_CLOCK(media
->ctrlr
, clock
);
120 int sd_change_freq(struct storage_media
*media
)
124 struct mmc_command cmd
;
125 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
126 struct mmc_data data
;
127 ALLOC_CACHE_ALIGN_BUFFER(uint32_t, scr
, 2);
128 ALLOC_CACHE_ALIGN_BUFFER(uint32_t, switch_status
, 16);
132 /* Read the SCR to find out if this card supports higher speeds */
133 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
134 cmd
.resp_type
= CARD_RSP_R1
;
135 cmd
.cmdarg
= media
->rca
<< 16;
138 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
142 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
143 cmd
.resp_type
= CARD_RSP_R1
;
149 data
.dest
= (char *)scr
;
152 data
.flags
= DATA_FLAG_READ
;
153 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, &data
);
158 sd_mmc_error("%s returning %d\n", __func__
, err
);
162 media
->scr
[0] = be32toh(scr
[0]);
163 media
->scr
[1] = be32toh(scr
[1]);
165 switch ((media
->scr
[0] >> 24) & 0xf) {
167 media
->version
= SD_VERSION_1_0
;
170 media
->version
= SD_VERSION_1_10
;
173 media
->version
= SD_VERSION_2
;
176 media
->version
= SD_VERSION_1_0
;
180 if (media
->scr
[0] & SD_DATA_4BIT
)
181 media
->caps
|= DRVR_CAP_4BIT
;
183 /* Version 1.0 doesn't support switching */
184 if (media
->version
== SD_VERSION_1_0
)
189 err
= sd_switch(ctrlr
, SD_SWITCH_CHECK
, 0, 1,
190 (uint8_t *)switch_status
);
194 /* The high-speed function is busy. Try again */
195 if (!(ntohl(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
199 /* If high-speed isn't supported, we return */
200 if (!(ntohl(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
))
204 * If the controller doesn't support SD_HIGHSPEED, do not switch the
205 * card to HIGHSPEED mode even if the card support SD_HIGHSPPED.
206 * This can avoid a further problem when the card runs in different
207 * mode than the controller.
209 if (!((ctrlr
->caps
& DRVR_CAP_HS52
) && (ctrlr
->caps
& DRVR_CAP_HS
)))
212 /* Give the card time to recover after the switch operation. Wait for
213 * 9 (>= 8) clock cycles receiving the switch status.
215 delay
= (9000000 + ctrlr
->bus_hz
- 1) / ctrlr
->bus_hz
;
218 /* Switch to high speed */
219 err
= sd_switch(ctrlr
, SD_SWITCH_SWITCH
, 0, 1,
220 (uint8_t *)switch_status
);
224 /* Give the card time to perform the switch operation. Wait for 9
225 * (>= 8) clock cycles receiving the switch status.
229 if ((ntohl(switch_status
[4]) & 0x0f000000) == 0x01000000) {
230 media
->caps
|= DRVR_CAP_HS
;
231 SET_TIMING(ctrlr
, BUS_TIMING_SD_HS
);
235 sd_recalculate_clock(media
);
239 int sd_set_bus_width(struct storage_media
*media
)
242 struct mmc_command cmd
;
243 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
245 if (media
->caps
& DRVR_CAP_4BIT
) {
246 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
247 cmd
.resp_type
= CARD_RSP_R1
;
248 cmd
.cmdarg
= media
->rca
<< 16;
251 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
255 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
256 cmd
.resp_type
= CARD_RSP_R1
;
259 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
263 SET_BUS_WIDTH(ctrlr
, 4);
268 int sd_set_partition(struct storage_media
*media
,
269 unsigned int partition_number
)
271 /* Validate the partition number */
272 if (partition_number
)
275 /* Update the partition number */
276 media
->partition_config
= partition_number
;
280 const char *sd_partition_name(struct storage_media
*media
,
281 unsigned int partition_number
)