1 /* This file is part of the coreboot project. */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 * Secure Digital (SD) card specific support code
5 * This code is controller independent
8 #include <commonlib/sd_mmc_ctrlr.h>
9 #include <commonlib/storage.h>
16 int sd_send_if_cond(struct storage_media
*media
)
18 struct mmc_command cmd
;
19 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
21 cmd
.cmdidx
= SD_CMD_SEND_IF_COND
;
22 /* Set if controller supports voltages between 2.7 and 3.6 V. */
23 cmd
.cmdarg
= ((ctrlr
->voltages
& 0xff8000) != 0) << 8 | 0xaa;
24 cmd
.resp_type
= CARD_RSP_R7
;
26 int err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
30 if ((cmd
.response
[0] & 0xff) != 0xaa)
31 return CARD_UNUSABLE_ERR
;
32 media
->version
= SD_VERSION_2
;
36 int sd_send_op_cond(struct storage_media
*media
)
39 struct mmc_command cmd
;
40 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
42 int tries
= SD_MMC_IO_RETRIES
;
44 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
45 cmd
.resp_type
= CARD_RSP_R1
;
49 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
53 cmd
.cmdidx
= SD_CMD_APP_SEND_OP_COND
;
54 cmd
.resp_type
= CARD_RSP_R3
;
57 * Most cards do not answer if some reserved bits
58 * in the ocr are set. However, Some controller
59 * can set bit 7 (reserved for low voltages), but
60 * how to manage low voltages SD card is not yet
63 cmd
.cmdarg
= (ctrlr
->voltages
& 0xff8000);
65 if (media
->version
== SD_VERSION_2
)
66 cmd
.cmdarg
|= OCR_HCS
;
68 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
72 // OCR_BUSY means "initialization complete".
73 if (cmd
.response
[0] & OCR_BUSY
)
79 return CARD_UNUSABLE_ERR
;
81 if (media
->version
!= SD_VERSION_2
)
82 media
->version
= SD_VERSION_1_0
;
84 media
->ocr
= cmd
.response
[0];
85 media
->high_capacity
= ((media
->ocr
& OCR_HCS
) == OCR_HCS
);
90 static int sd_switch(struct sd_mmc_ctrlr
*ctrlr
, int mode
, int group
,
91 uint8_t value
, uint8_t *resp
)
93 /* Switch the frequency */
94 struct mmc_command cmd
;
95 cmd
.cmdidx
= SD_CMD_SWITCH_FUNC
;
96 cmd
.resp_type
= CARD_RSP_R1
;
97 cmd
.cmdarg
= (mode
<< 31) | (0xffffff & ~(0xf << (group
* 4))) |
98 (value
<< (group
* 4));
101 struct mmc_data data
;
102 data
.dest
= (char *)resp
;
105 data
.flags
= DATA_FLAG_READ
;
107 return ctrlr
->send_cmd(ctrlr
, &cmd
, &data
);
110 static void sd_recalculate_clock(struct storage_media
*media
)
114 if (media
->caps
& DRVR_CAP_HS
)
118 SET_CLOCK(media
->ctrlr
, clock
);
121 int sd_change_freq(struct storage_media
*media
)
125 struct mmc_command cmd
;
126 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
127 struct mmc_data data
;
128 ALLOC_CACHE_ALIGN_BUFFER(uint32_t, scr
, 2);
129 ALLOC_CACHE_ALIGN_BUFFER(uint32_t, switch_status
, 16);
133 /* Read the SCR to find out if this card supports higher speeds */
134 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
135 cmd
.resp_type
= CARD_RSP_R1
;
136 cmd
.cmdarg
= media
->rca
<< 16;
139 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
143 cmd
.cmdidx
= SD_CMD_APP_SEND_SCR
;
144 cmd
.resp_type
= CARD_RSP_R1
;
150 data
.dest
= (char *)scr
;
153 data
.flags
= DATA_FLAG_READ
;
154 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, &data
);
159 sd_mmc_error("%s returning %d\n", __func__
, err
);
163 media
->scr
[0] = be32toh(scr
[0]);
164 media
->scr
[1] = be32toh(scr
[1]);
166 switch ((media
->scr
[0] >> 24) & 0xf) {
168 media
->version
= SD_VERSION_1_0
;
171 media
->version
= SD_VERSION_1_10
;
174 media
->version
= SD_VERSION_2
;
177 media
->version
= SD_VERSION_1_0
;
181 if (media
->scr
[0] & SD_DATA_4BIT
)
182 media
->caps
|= DRVR_CAP_4BIT
;
184 /* Version 1.0 doesn't support switching */
185 if (media
->version
== SD_VERSION_1_0
)
190 err
= sd_switch(ctrlr
, SD_SWITCH_CHECK
, 0, 1,
191 (uint8_t *)switch_status
);
195 /* The high-speed function is busy. Try again */
196 if (!(ntohl(switch_status
[7]) & SD_HIGHSPEED_BUSY
))
200 /* If high-speed isn't supported, we return */
201 if (!(ntohl(switch_status
[3]) & SD_HIGHSPEED_SUPPORTED
))
205 * If the controller doesn't support SD_HIGHSPEED, do not switch the
206 * card to HIGHSPEED mode even if the card support SD_HIGHSPPED.
207 * This can avoid a further problem when the card runs in different
208 * mode than the controller.
210 if (!((ctrlr
->caps
& DRVR_CAP_HS52
) && (ctrlr
->caps
& DRVR_CAP_HS
)))
213 /* Give the card time to recover after the switch operation. Wait for
214 * 9 (>= 8) clock cycles receiving the switch status.
216 delay
= (9000000 + ctrlr
->bus_hz
- 1) / ctrlr
->bus_hz
;
219 /* Switch to high speed */
220 err
= sd_switch(ctrlr
, SD_SWITCH_SWITCH
, 0, 1,
221 (uint8_t *)switch_status
);
225 /* Give the card time to perform the switch operation. Wait for 9
226 * (>= 8) clock cycles receiving the switch status.
230 if ((ntohl(switch_status
[4]) & 0x0f000000) == 0x01000000) {
231 media
->caps
|= DRVR_CAP_HS
;
232 SET_TIMING(ctrlr
, BUS_TIMING_SD_HS
);
236 sd_recalculate_clock(media
);
240 int sd_set_bus_width(struct storage_media
*media
)
243 struct mmc_command cmd
;
244 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
246 if (media
->caps
& DRVR_CAP_4BIT
) {
247 cmd
.cmdidx
= MMC_CMD_APP_CMD
;
248 cmd
.resp_type
= CARD_RSP_R1
;
249 cmd
.cmdarg
= media
->rca
<< 16;
252 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
256 cmd
.cmdidx
= SD_CMD_APP_SET_BUS_WIDTH
;
257 cmd
.resp_type
= CARD_RSP_R1
;
260 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
264 SET_BUS_WIDTH(ctrlr
, 4);
270 int sd_set_partition(struct storage_media
*media
,
271 unsigned int partition_number
)
273 /* Validate the partition number */
274 if (partition_number
)
277 /* Update the partition number */
278 media
->partition_config
= partition_number
;
282 const char *sd_partition_name(struct storage_media
*media
,
283 unsigned int partition_number
)