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 int ret;
121
122 /* SD clock should start gated */
123 bus_io->clock = 0;
124 /* SPI requires SDHC PUSH PULL, and open drain buses use more power */
125 bus_io->bus_mode = SDHC_BUSMODE_PUSHPULL;
126 bus_io->power_mode = SDHC_POWER_ON;
127 bus_io->bus_width = SDHC_BUS_WIDTH1BIT;
128 /* Cards start with legacy timing and 3.3V signalling at power on */
129 bus_io->timing = SDHC_TIMING_LEGACY;
130 bus_io->signal_voltage = SD_VOL_3_3_V;
131
132 /* Toggle power to card to reset it */
133 LOG_DBG("Resetting power to card");
134 bus_io->power_mode = SDHC_POWER_OFF;
135 ret = sdhc_set_io(card->sdhc, bus_io);
136 if (ret) {
137 LOG_ERR("Could not disable card power via SDHC");
138 return ret;
139 }
140 sd_delay(card->host_props.power_delay);
141 bus_io->power_mode = SDHC_POWER_ON;
142 ret = sdhc_set_io(card->sdhc, bus_io);
143 if (ret) {
144 LOG_ERR("Could not disable card power via SDHC");
145 return ret;
146 }
147 /* After reset or init, card voltage should be 3.3V */
148 card->card_voltage = SD_VOL_3_3_V;
149 /* Reset card flags */
150 card->flags = 0U;
151 /* Delay so card can power up */
152 sd_delay(card->host_props.power_delay);
153 /* Start bus clock */
154 bus_io->clock = SDMMC_CLOCK_400KHZ;
155 ret = sdhc_set_io(card->sdhc, bus_io);
156 if (ret) {
157 LOG_ERR("Could not start bus clock");
158 return ret;
159 }
160 return 0;
161 }
162
163 /*
164 * Performs init flow described in section 3.6 of SD specification.
165 */
sd_command_init(struct sd_card * card)166 static int sd_command_init(struct sd_card *card)
167 {
168 int ret;
169
170 /*
171 * We must wait 74 clock cycles, per SD spec, to use card after power
172 * on. At 400000KHz, this is a 185us delay. Wait 1ms to be safe.
173 */
174 sd_delay(1);
175
176
177 /*
178 * Start card initialization and identification
179 * flow described in section 3.6 of SD specification
180 * Common to SDIO and SDMMC. Some eMMC chips break the
181 * specification and expect something like this too.
182 */
183 ret = sd_common_init(card);
184 if (ret) {
185 return ret;
186 }
187
188 #ifdef CONFIG_SDIO_STACK
189 /* Attempt to initialize SDIO card */
190 if (!sdio_card_init(card)) {
191 return 0;
192 }
193 #endif /* CONFIG_SDIO_STACK */
194 #ifdef CONFIG_SDMMC_STACK
195 /* Attempt to initialize SDMMC card */
196 if (!sdmmc_card_init(card)) {
197 return 0;
198 }
199 #endif /* CONFIG_SDIO_STACK */
200 #ifdef CONFIG_MMC_STACK
201 ret = sd_idle(card);
202 if (ret) {
203 LOG_ERR("Card error on CMD0");
204 return ret;
205 }
206 if (!mmc_card_init(card)) {
207 return 0;
208 }
209 #endif /* CONFIG_MMC_STACK */
210 /* Unknown card type */
211 return -ENOTSUP;
212 }
213
214 /* Initializes SD/SDIO card */
sd_init(const struct device * sdhc_dev,struct sd_card * card)215 int sd_init(const struct device *sdhc_dev, struct sd_card *card)
216 {
217 int ret;
218
219 if (!sdhc_dev) {
220 return -ENODEV;
221 }
222 card->sdhc = sdhc_dev;
223 ret = sdhc_get_host_props(card->sdhc, &card->host_props);
224 if (ret) {
225 LOG_ERR("SD host controller returned invalid properties");
226 return ret;
227 }
228
229 /* init and lock card mutex */
230 ret = k_mutex_init(&card->lock);
231 if (ret) {
232 LOG_DBG("Could not init card mutex");
233 return ret;
234 }
235 ret = k_mutex_lock(&card->lock, K_MSEC(CONFIG_SD_INIT_TIMEOUT));
236 if (ret) {
237 LOG_ERR("Timeout while trying to acquire card mutex");
238 return ret;
239 }
240
241 /* Initialize SDHC IO with defaults */
242 ret = sd_init_io(card);
243 if (ret) {
244 k_mutex_unlock(&card->lock);
245 return ret;
246 }
247
248 /*
249 * SD protocol is stateful, so we must account for the possibility
250 * that the card is in a bad state. The return code SD_RESTART
251 * indicates that the initialization left the card in a bad state.
252 * In this case the subsystem takes the following steps:
253 * - set card status to error
254 * - re init host I/O (will also toggle power to the SD card)
255 * - retry initialization once more
256 * If initialization then fails, the sd_init routine will assume the
257 * card is inaccessible
258 */
259 ret = sd_command_init(card);
260 if (ret == SD_RESTART) {
261 /* Reset I/O, and retry sd initialization once more */
262 card->status = CARD_ERROR;
263 /* Reset I/O to default */
264 ret = sd_init_io(card);
265 if (ret) {
266 LOG_ERR("Failed to reset SDHC I/O");
267 k_mutex_unlock(&card->lock);
268 return ret;
269 }
270 ret = sd_command_init(card);
271 if (ret) {
272 LOG_ERR("Failed to init SD card after I/O reset");
273 k_mutex_unlock(&card->lock);
274 return ret;
275 }
276 } else if (ret != 0) {
277 /* Initialization failed */
278 k_mutex_unlock(&card->lock);
279 card->status = CARD_ERROR;
280 return ret;
281 }
282 /* Card initialization succeeded. */
283 card->status = CARD_INITIALIZED;
284 /* Unlock card mutex */
285 ret = k_mutex_unlock(&card->lock);
286 if (ret) {
287 LOG_DBG("Could not unlock card mutex");
288 return ret;
289 }
290 return ret;
291 }
292
293 /* Return true if card is present, false otherwise */
sd_is_card_present(const struct device * sdhc_dev)294 bool sd_is_card_present(const struct device *sdhc_dev)
295 {
296 if (!sdhc_dev) {
297 return false;
298 }
299 return sdhc_card_present(sdhc_dev) == 1;
300 }
301