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