1 /*
2 * Copyright 2022 NXP
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/sdhc.h>
9 #include <zephyr/sd/sd.h>
10 #include <zephyr/sd/sdmmc.h>
11 #include <zephyr/sd/sd_spec.h>
12 #include <zephyr/logging/log.h>
13 #include <zephyr/sys/__assert.h>
14
15 #include "sd_utils.h"
16 #include "sd_init.h"
17
18
19 LOG_MODULE_REGISTER(sd, CONFIG_SD_LOG_LEVEL);
20
21 /* Idle all cards on bus. Can be used to clear errors on cards */
sd_idle(struct sd_card * card)22 static inline int sd_idle(struct sd_card *card)
23 {
24 struct sdhc_command cmd = {0};
25
26 /* Reset card with CMD0 */
27 cmd.opcode = SD_GO_IDLE_STATE;
28 cmd.arg = 0x0;
29 cmd.response_type = (SD_RSP_TYPE_NONE | SD_SPI_RSP_TYPE_R1);
30 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
31 return sdhc_request(card->sdhc, &cmd, NULL);
32 }
33
34 /* Sends CMD8 during SD initialization */
sd_send_interface_condition(struct sd_card * card)35 static int sd_send_interface_condition(struct sd_card *card)
36 {
37 struct sdhc_command cmd = {0};
38 int ret;
39 uint32_t resp;
40
41 cmd.opcode = SD_SEND_IF_COND;
42 cmd.arg = SD_IF_COND_VHS_3V3 | SD_IF_COND_CHECK;
43 cmd.response_type = (SD_RSP_TYPE_R7 | SD_SPI_RSP_TYPE_R7);
44 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
45 ret = sdhc_request(card->sdhc, &cmd, NULL);
46 if (ret) {
47 LOG_DBG("SD CMD8 failed with error %d", ret);
48 /* Retry */
49 return SD_RETRY;
50 }
51 if (card->host_props.is_spi) {
52 resp = cmd.response[1];
53 } else {
54 resp = cmd.response[0];
55 }
56 if ((resp & 0xFF) != SD_IF_COND_CHECK) {
57 LOG_INF("Legacy card detected, no CMD8 support");
58 /* Retry probe */
59 return SD_RETRY;
60 }
61 if ((resp & SD_IF_COND_VHS_MASK) != SD_IF_COND_VHS_3V3) {
62 /* Card does not support 3.3V */
63 return -ENOTSUP;
64 }
65 LOG_DBG("Found SDHC with CMD8 support");
66 card->flags |= SD_SDHC_FLAG;
67 return 0;
68 }
69
70 /* Sends CMD59 to enable CRC checking for SD card in SPI mode */
sd_enable_crc(struct sd_card * card)71 static int sd_enable_crc(struct sd_card *card)
72 {
73 struct sdhc_command cmd = {0};
74
75 /* CMD59 for CRC mode is only valid for SPI hosts */
76 __ASSERT_NO_MSG(card->host_props.is_spi);
77 cmd.opcode = SD_SPI_CRC_ON_OFF;
78 cmd.arg = 0x1; /* Enable CRC */
79 cmd.response_type = SD_SPI_RSP_TYPE_R1;
80 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
81 return sdhc_request(card->sdhc, &cmd, NULL);
82 }
83
84 /*
85 * Perform init required for both SD and SDIO cards.
86 * This function performs the following portions of SD initialization
87 * - CMD0 (SD reset)
88 * - CMD8 (SD voltage check)
89 */
sd_common_init(struct sd_card * card)90 static int sd_common_init(struct sd_card *card)
91 {
92 int ret;
93
94 /* Reset card with CMD0 */
95 ret = sd_idle(card);
96 if (ret) {
97 LOG_ERR("Card error on CMD0");
98 return ret;
99 }
100 /* Perform voltage check using SD CMD8 */
101 ret = sd_retry(sd_send_interface_condition, card, CONFIG_SD_RETRY_COUNT);
102 if (ret == -ETIMEDOUT) {
103 LOG_INF("Card does not support CMD8, assuming legacy card");
104 return sd_idle(card);
105 } else if (ret) {
106 LOG_ERR("Card error on CMD 8");
107 return ret;
108 }
109 if (card->host_props.is_spi &&
110 IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) {
111 /* Enable CRC for spi commands using CMD59 */
112 ret = sd_enable_crc(card);
113 }
114 return ret;
115 }
116
sd_init_io(struct sd_card * card)117 static int sd_init_io(struct sd_card *card)
118 {
119 struct sdhc_io *bus_io = &card->bus_io;
120 struct sdhc_host_props *host_props = &card->host_props;
121 int ret, voltage;
122
123 /* SD clock should start gated */
124 bus_io->clock = 0;
125 /* SPI requires SDHC PUSH PULL, and open drain buses use more power */
126 bus_io->bus_mode = SDHC_BUSMODE_PUSHPULL;
127 bus_io->power_mode = SDHC_POWER_ON;
128 bus_io->bus_width = SDHC_BUS_WIDTH1BIT;
129 /* Cards start with legacy timing and Maximum voltage Host controller support */
130 bus_io->timing = SDHC_TIMING_LEGACY;
131
132 if (host_props->host_caps.vol_330_support) {
133 LOG_DBG("Host controller support 3.3V max");
134 voltage = SD_VOL_3_3_V;
135 } else if (host_props->host_caps.vol_300_support) {
136 LOG_DBG("Host controller support 3.0V max");
137 voltage = SD_VOL_3_0_V;
138 } else {
139 LOG_DBG("Host controller support 1.8V max");
140 voltage = SD_VOL_1_8_V;
141 }
142
143 /* Set to maximum voltage support by Host controller */
144 bus_io->signal_voltage = voltage;
145
146 /* Toggle power to card to reset it */
147 LOG_DBG("Resetting power to card");
148 bus_io->power_mode = SDHC_POWER_OFF;
149 ret = sdhc_set_io(card->sdhc, bus_io);
150 if (ret) {
151 LOG_ERR("Could not disable card power via SDHC");
152 return ret;
153 }
154 sd_delay(card->host_props.power_delay);
155 bus_io->power_mode = SDHC_POWER_ON;
156 ret = sdhc_set_io(card->sdhc, bus_io);
157 if (ret) {
158 LOG_ERR("Could not disable card power via SDHC");
159 return ret;
160 }
161 /* After reset or init, card voltage should be max HC support */
162 card->card_voltage = voltage;
163 /* Reset card flags */
164 card->flags = 0U;
165 /* Delay so card can power up */
166 sd_delay(card->host_props.power_delay);
167 /* Start bus clock */
168 bus_io->clock = SDMMC_CLOCK_400KHZ;
169 ret = sdhc_set_io(card->sdhc, bus_io);
170 if (ret) {
171 LOG_ERR("Could not start bus clock");
172 return ret;
173 }
174 return 0;
175 }
176
177 /*
178 * Performs init flow described in section 3.6 of SD specification.
179 */
sd_command_init(struct sd_card * card)180 static int sd_command_init(struct sd_card *card)
181 {
182 int ret;
183
184 /*
185 * We must wait 74 clock cycles, per SD spec, to use card after power
186 * on. At 400000KHz, this is a 185us delay. Wait 1ms to be safe.
187 */
188 sd_delay(1);
189
190
191 /*
192 * Start card initialization and identification
193 * flow described in section 3.6 of SD specification
194 * Common to SDIO and SDMMC. Some eMMC chips break the
195 * specification and expect something like this too.
196 */
197 ret = sd_common_init(card);
198 if (ret) {
199 return ret;
200 }
201
202 #ifdef CONFIG_SDIO_STACK
203 /* Attempt to initialize SDIO card */
204 if (!sdio_card_init(card)) {
205 return 0;
206 }
207 #endif /* CONFIG_SDIO_STACK */
208 #ifdef CONFIG_SDMMC_STACK
209 /* Attempt to initialize SDMMC card */
210 if (!sdmmc_card_init(card)) {
211 return 0;
212 }
213 #endif /* CONFIG_SDIO_STACK */
214 #ifdef CONFIG_MMC_STACK
215 ret = sd_idle(card);
216 if (ret) {
217 LOG_ERR("Card error on CMD0");
218 return ret;
219 }
220 if (!mmc_card_init(card)) {
221 return 0;
222 }
223 #endif /* CONFIG_MMC_STACK */
224 /* Unknown card type */
225 return -ENOTSUP;
226 }
227
228 /* Initializes SD/SDIO card */
sd_init(const struct device * sdhc_dev,struct sd_card * card)229 int sd_init(const struct device *sdhc_dev, struct sd_card *card)
230 {
231 int ret;
232
233 if (!sdhc_dev) {
234 return -ENODEV;
235 }
236 card->sdhc = sdhc_dev;
237 ret = sdhc_get_host_props(card->sdhc, &card->host_props);
238 if (ret) {
239 LOG_ERR("SD host controller returned invalid properties");
240 return ret;
241 }
242
243 /* init and lock card mutex */
244 ret = k_mutex_init(&card->lock);
245 if (ret) {
246 LOG_DBG("Could not init card mutex");
247 return ret;
248 }
249 ret = k_mutex_lock(&card->lock, K_MSEC(CONFIG_SD_INIT_TIMEOUT));
250 if (ret) {
251 LOG_ERR("Timeout while trying to acquire card mutex");
252 return ret;
253 }
254
255 /* Initialize SDHC IO with defaults */
256 ret = sd_init_io(card);
257 if (ret) {
258 k_mutex_unlock(&card->lock);
259 return ret;
260 }
261
262 /*
263 * SD protocol is stateful, so we must account for the possibility
264 * that the card is in a bad state. The return code SD_RESTART
265 * indicates that the initialization left the card in a bad state.
266 * In this case the subsystem takes the following steps:
267 * - set card status to error
268 * - re init host I/O (will also toggle power to the SD card)
269 * - retry initialization once more
270 * If initialization then fails, the sd_init routine will assume the
271 * card is inaccessible
272 */
273 ret = sd_command_init(card);
274 if (ret == SD_RESTART) {
275 /* Reset I/O, and retry sd initialization once more */
276 card->status = CARD_ERROR;
277 /* Reset I/O to default */
278 ret = sd_init_io(card);
279 if (ret) {
280 LOG_ERR("Failed to reset SDHC I/O");
281 k_mutex_unlock(&card->lock);
282 return ret;
283 }
284 ret = sd_command_init(card);
285 if (ret) {
286 LOG_ERR("Failed to init SD card after I/O reset");
287 k_mutex_unlock(&card->lock);
288 return ret;
289 }
290 } else if (ret != 0) {
291 /* Initialization failed */
292 k_mutex_unlock(&card->lock);
293 card->status = CARD_ERROR;
294 return ret;
295 }
296 /* Card initialization succeeded. */
297 card->status = CARD_INITIALIZED;
298 /* Unlock card mutex */
299 ret = k_mutex_unlock(&card->lock);
300 if (ret) {
301 LOG_DBG("Could not unlock card mutex");
302 return ret;
303 }
304 return ret;
305 }
306
307 /* Return true if card is present, false otherwise */
sd_is_card_present(const struct device * sdhc_dev)308 bool sd_is_card_present(const struct device *sdhc_dev)
309 {
310 if (!sdhc_dev) {
311 return false;
312 }
313 return sdhc_card_present(sdhc_dev) == 1;
314 }
315