mb/hardkernel/odroid-h4: Correct number of jacks in hda_verb.c
[coreboot.git] / src / commonlib / storage / sd.c
blobb27f3425dd8ef9a1c7f8ddd685a8c052a991b988
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Secure Digital (SD) card specific support code
4 * This code is controller independent
5 */
7 #include <commonlib/sd_mmc_ctrlr.h>
8 #include <commonlib/storage.h>
9 #include <delay.h>
10 #include <endian.h>
12 #include "sd_mmc.h"
13 #include "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;
24 cmd.flags = 0;
25 int err = ctrlr->send_cmd(ctrlr, &cmd, NULL);
26 if (err)
27 return err;
29 if ((cmd.response[0] & 0xff) != 0xaa)
30 return CARD_UNUSABLE_ERR;
31 media->version = SD_VERSION_2;
32 return 0;
35 int sd_send_op_cond(struct storage_media *media)
37 int err;
38 struct mmc_command cmd;
39 struct sd_mmc_ctrlr *ctrlr = media->ctrlr;
41 int tries = SD_MMC_IO_RETRIES;
42 while (tries--) {
43 cmd.cmdidx = MMC_CMD_APP_CMD;
44 cmd.resp_type = CARD_RSP_R1;
45 cmd.cmdarg = 0;
46 cmd.flags = 0;
48 err = ctrlr->send_cmd(ctrlr, &cmd, NULL);
49 if (err)
50 return err;
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
60 * specified.
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);
68 if (err)
69 return err;
71 // OCR_BUSY means "initialization complete".
72 if (cmd.response[0] & OCR_BUSY)
73 break;
75 udelay(100);
77 if (tries < 0)
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);
85 media->rca = 0;
86 return 0;
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));
98 cmd.flags = 0;
100 struct mmc_data data;
101 data.dest = (char *)resp;
102 data.blocksize = 64;
103 data.blocks = 1;
104 data.flags = DATA_FLAG_READ;
106 return ctrlr->send_cmd(ctrlr, &cmd, &data);
109 static void sd_recalculate_clock(struct storage_media *media)
111 uint32_t clock = 1;
113 if (media->caps & DRVR_CAP_HS)
114 clock = CLOCK_50MHZ;
115 else
116 clock = CLOCK_25MHZ;
117 SET_CLOCK(media->ctrlr, clock);
120 int sd_change_freq(struct storage_media *media)
122 int delay;
123 int err, timeout;
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);
130 media->caps = 0;
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;
136 cmd.flags = 0;
138 err = ctrlr->send_cmd(ctrlr, &cmd, NULL);
139 if (err)
140 return err;
142 cmd.cmdidx = SD_CMD_APP_SEND_SCR;
143 cmd.resp_type = CARD_RSP_R1;
144 cmd.cmdarg = 0;
145 cmd.flags = 0;
147 timeout = 3;
148 while (timeout--) {
149 data.dest = (char *)scr;
150 data.blocksize = 8;
151 data.blocks = 1;
152 data.flags = DATA_FLAG_READ;
153 err = ctrlr->send_cmd(ctrlr, &cmd, &data);
154 if (!err)
155 break;
157 if (err) {
158 sd_mmc_error("%s returning %d\n", __func__, err);
159 return err;
162 media->scr[0] = be32toh(scr[0]);
163 media->scr[1] = be32toh(scr[1]);
165 switch ((media->scr[0] >> 24) & 0xf) {
166 case 0:
167 media->version = SD_VERSION_1_0;
168 break;
169 case 1:
170 media->version = SD_VERSION_1_10;
171 break;
172 case 2:
173 media->version = SD_VERSION_2;
174 break;
175 default:
176 media->version = SD_VERSION_1_0;
177 break;
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)
185 goto out;
187 timeout = 4;
188 while (timeout--) {
189 err = sd_switch(ctrlr, SD_SWITCH_CHECK, 0, 1,
190 (uint8_t *)switch_status);
191 if (err)
192 return err;
194 /* The high-speed function is busy. Try again */
195 if (!(ntohl(switch_status[7]) & SD_HIGHSPEED_BUSY))
196 break;
199 /* If high-speed isn't supported, we return */
200 if (!(ntohl(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
201 goto out;
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)))
210 goto out;
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;
216 udelay(delay);
218 /* Switch to high speed */
219 err = sd_switch(ctrlr, SD_SWITCH_SWITCH, 0, 1,
220 (uint8_t *)switch_status);
221 if (err)
222 return err;
224 /* Give the card time to perform the switch operation. Wait for 9
225 * (>= 8) clock cycles receiving the switch status.
227 udelay(delay);
229 if ((ntohl(switch_status[4]) & 0x0f000000) == 0x01000000) {
230 media->caps |= DRVR_CAP_HS;
231 SET_TIMING(ctrlr, BUS_TIMING_SD_HS);
234 out:
235 sd_recalculate_clock(media);
236 return 0;
239 int sd_set_bus_width(struct storage_media *media)
241 int err;
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;
249 cmd.flags = 0;
251 err = ctrlr->send_cmd(ctrlr, &cmd, NULL);
252 if (err)
253 return err;
255 cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
256 cmd.resp_type = CARD_RSP_R1;
257 cmd.cmdarg = 2;
258 cmd.flags = 0;
259 err = ctrlr->send_cmd(ctrlr, &cmd, NULL);
260 if (err)
261 return err;
263 SET_BUS_WIDTH(ctrlr, 4);
265 return 0;
268 int sd_set_partition(struct storage_media *media,
269 unsigned int partition_number)
271 /* Validate the partition number */
272 if (partition_number)
273 return -1;
275 /* Update the partition number */
276 media->partition_config = partition_number;
277 return 0;
280 const char *sd_partition_name(struct storage_media *media,
281 unsigned int partition_number)
283 return "";