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/byteorder.h>
14 #include <zephyr/drivers/disk.h>
15 
16 #include "sd_utils.h"
17 #include "sd_ops.h"
18 
19 LOG_MODULE_DECLARE(sd, CONFIG_SD_LOG_LEVEL);
20 
sdmmc_decode_scr(struct sd_scr * scr,uint32_t * raw_scr,uint8_t * version)21 static inline void sdmmc_decode_scr(struct sd_scr *scr,
22 	uint32_t *raw_scr, uint8_t *version)
23 {
24 	uint32_t tmp_version = 0;
25 
26 	scr->flags = 0U;
27 	scr->scr_structure = (uint8_t)((raw_scr[0U] & 0xF0000000U) >> 28U);
28 	scr->sd_spec = (uint8_t)((raw_scr[0U] & 0xF000000U) >> 24U);
29 	if ((uint8_t)((raw_scr[0U] & 0x800000U) >> 23U)) {
30 		scr->flags |= SD_SCR_DATA_STATUS_AFTER_ERASE;
31 	}
32 	scr->sd_sec = (uint8_t)((raw_scr[0U] & 0x700000U) >> 20U);
33 	scr->sd_width = (uint8_t)((raw_scr[0U] & 0xF0000U) >> 16U);
34 	if ((uint8_t)((raw_scr[0U] & 0x8000U) >> 15U)) {
35 		scr->flags |= SD_SCR_SPEC3;
36 	}
37 	scr->sd_ext_sec = (uint8_t)((raw_scr[0U] & 0x7800U) >> 10U);
38 	scr->cmd_support = (uint8_t)(raw_scr[0U] & 0x3U);
39 	scr->rsvd = raw_scr[1U];
40 	/* Get specification version. */
41 	switch (scr->sd_spec) {
42 	case 0U:
43 		tmp_version = SD_SPEC_VER1_0;
44 		break;
45 	case 1U:
46 		tmp_version = SD_SPEC_VER1_1;
47 		break;
48 	case 2U:
49 		tmp_version = SD_SPEC_VER2_0;
50 		if (scr->flags & SD_SCR_SPEC3) {
51 			tmp_version = SD_SPEC_VER3_0;
52 		}
53 		break;
54 	default:
55 		break;
56 	}
57 
58 	if (version && tmp_version) {
59 		*version = tmp_version;
60 	}
61 }
62 
63 /* Helper to send SD app command */
sdmmc_app_command(struct sd_card * card,int relative_card_address)64 static int sdmmc_app_command(struct sd_card *card, int relative_card_address)
65 {
66 	return card_app_command(card, relative_card_address);
67 }
68 
69 /* Reads OCR from SPI mode card using CMD58 */
sdmmc_spi_send_ocr(struct sd_card * card,uint32_t arg)70 static int sdmmc_spi_send_ocr(struct sd_card *card, uint32_t arg)
71 {
72 	struct sdhc_command cmd;
73 	int ret;
74 
75 	cmd.opcode = SD_SPI_READ_OCR;
76 	cmd.arg = arg;
77 	cmd.response_type = SD_SPI_RSP_TYPE_R3;
78 
79 	ret = sdhc_request(card->sdhc, &cmd, NULL);
80 
81 	if (ret) {
82 		LOG_DBG("CMD58 failed: %d", ret);
83 		return ret;
84 	}
85 
86 	card->ocr = cmd.response[1];
87 	if (card->ocr == 0) {
88 		LOG_DBG("No OCR detected");
89 		return -ENOTSUP;
90 	}
91 
92 	return ret;
93 }
94 
95 /* Sends OCR to card using ACMD41 */
sdmmc_send_ocr(struct sd_card * card,int ocr)96 static int sdmmc_send_ocr(struct sd_card *card, int ocr)
97 {
98 	struct sdhc_command cmd;
99 	int ret;
100 	int retries;
101 
102 	cmd.opcode = SD_APP_SEND_OP_COND;
103 	cmd.arg = ocr;
104 	cmd.response_type = (SD_RSP_TYPE_R3 | SD_SPI_RSP_TYPE_R1);
105 	cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
106 	/* Send initialization ACMD41 */
107 	for (retries = 0; retries < CONFIG_SD_OCR_RETRY_COUNT; retries++) {
108 		ret = sdmmc_app_command(card, 0U);
109 		if (ret == SD_RETRY) {
110 			/* Retry */
111 			continue;
112 		} else if (ret) {
113 			return ret;
114 		}
115 		ret = sdhc_request(card->sdhc, &cmd, NULL);
116 		if (ret) {
117 			/* OCR failed */
118 			return ret;
119 		}
120 		if (ocr == 0) {
121 			/* Just probing, don't wait for card to exit busy state */
122 			return 0;
123 		}
124 		/*
125 		 * Check to see if card is busy with power up. PWR_BUSY
126 		 * flag will be cleared when card finishes power up sequence
127 		 */
128 		if (card->host_props.is_spi) {
129 			if (!(cmd.response[0] & SD_SPI_R1IDLE_STATE)) {
130 				break;
131 			}
132 		} else {
133 			if ((cmd.response[0U] & SD_OCR_PWR_BUSY_FLAG)) {
134 				break;
135 			}
136 		}
137 		sd_delay(10);
138 	}
139 	if (retries >= CONFIG_SD_OCR_RETRY_COUNT) {
140 		/* OCR timed out */
141 		LOG_ERR("Card never left busy state");
142 		return -ETIMEDOUT;
143 	}
144 	LOG_DBG("SDMMC responded to ACMD41 after %d attempts", retries);
145 	if (!card->host_props.is_spi) {
146 		/* Save OCR */
147 		card->ocr = cmd.response[0U];
148 	}
149 	return 0;
150 }
151 
152 /* Reads SD configuration register */
sdmmc_read_scr(struct sd_card * card)153 static int sdmmc_read_scr(struct sd_card *card)
154 {
155 	struct sdhc_command cmd = {0};
156 	struct sdhc_data data = {0};
157 	/* Place SCR struct on stack to reduce flash usage */
158 	struct sd_scr card_scr;
159 	int ret;
160 	/* DMA onto stack is unsafe, so we use an internal card buffer */
161 	uint32_t *scr = (uint32_t *)card->card_buffer;
162 	uint32_t raw_scr[2];
163 
164 	ret = sdmmc_app_command(card, card->relative_addr);
165 	if (ret) {
166 		LOG_DBG("SD app command failed for SD SCR");
167 		return ret;
168 	}
169 
170 	cmd.opcode = SD_APP_SEND_SCR;
171 	cmd.arg = 0;
172 	cmd.response_type =  (SD_RSP_TYPE_R1 | SD_SPI_RSP_TYPE_R1);
173 	cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
174 
175 	data.block_size = 8U;
176 	data.blocks = 1U;
177 	data.data = scr;
178 	data.timeout_ms = CONFIG_SD_DATA_TIMEOUT;
179 
180 	ret = sdhc_request(card->sdhc, &cmd, &data);
181 	if (ret) {
182 		LOG_DBG("ACMD51 failed: %d", ret);
183 		return ret;
184 	}
185 	/* Decode SCR */
186 	raw_scr[0] = sys_be32_to_cpu(scr[0]);
187 	raw_scr[1] = sys_be32_to_cpu(scr[1]);
188 	sdmmc_decode_scr(&card_scr, raw_scr, &card->sd_version);
189 	LOG_DBG("SD reports specification version %d", card->sd_version);
190 	/* Check card supported bus width */
191 	if (card_scr.sd_width & 0x4U) {
192 		card->flags |= SD_4BITS_WIDTH;
193 	}
194 	/* Check if card supports speed class command (CMD20) */
195 	if (card_scr.cmd_support & 0x1U) {
196 		card->flags |= SD_SPEED_CLASS_CONTROL_FLAG;
197 	}
198 	/* Check for set block count (CMD 23) support */
199 	if (card_scr.cmd_support & 0x2U) {
200 		card->flags |= SD_CMD23_FLAG;
201 	}
202 	return 0;
203 }
204 
205 /* Sets block length of SD card */
sdmmc_set_blocklen(struct sd_card * card,uint32_t block_len)206 static int sdmmc_set_blocklen(struct sd_card *card, uint32_t block_len)
207 {
208 	struct sdhc_command cmd = {0};
209 
210 	cmd.opcode = SD_SET_BLOCK_SIZE;
211 	cmd.arg = block_len;
212 	cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
213 	cmd.response_type =  (SD_RSP_TYPE_R1 | SD_SPI_RSP_TYPE_R1);
214 
215 	return sdhc_request(card->sdhc, &cmd, NULL);
216 }
217 
218 /*
219  * Sets bus width of host and card, following section 3.4 of
220  * SD host controller specification
221  */
sdmmc_set_bus_width(struct sd_card * card,enum sdhc_bus_width width)222 static int sdmmc_set_bus_width(struct sd_card *card, enum sdhc_bus_width width)
223 {
224 	struct sdhc_command cmd = {0};
225 	int ret;
226 
227 	/*
228 	 * The specification strictly requires card interrupts to be masked, but
229 	 * Linux does not do so, so we won't either.
230 	 */
231 	/* Send ACMD6 to change bus width */
232 	ret = sdmmc_app_command(card, card->relative_addr);
233 	if (ret) {
234 		LOG_DBG("SD app command failed for ACMD6");
235 		return ret;
236 	}
237 	cmd.opcode = SD_APP_SET_BUS_WIDTH;
238 	cmd.response_type = SD_RSP_TYPE_R1;
239 	cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
240 	switch (width) {
241 	case SDHC_BUS_WIDTH1BIT:
242 		cmd.arg = 0U;
243 		break;
244 	case SDHC_BUS_WIDTH4BIT:
245 		cmd.arg = 2U;
246 		break;
247 	default:
248 		return -ENOTSUP;
249 	}
250 	/* Send app command */
251 	ret = sdhc_request(card->sdhc, &cmd, NULL);
252 	if (ret) {
253 		LOG_DBG("Error on ACMD6: %d", ret);
254 		return ret;
255 	}
256 	ret = sd_check_response(&cmd);
257 	if (ret) {
258 		LOG_DBG("ACMD6 reports error, response 0x%x", cmd.response[0U]);
259 		return ret;
260 	}
261 	/* Card now has changed bus width. Change host bus width */
262 	card->bus_io.bus_width = width;
263 	ret = sdhc_set_io(card->sdhc, &card->bus_io);
264 	if (ret) {
265 		LOG_DBG("Could not change host bus width");
266 	}
267 	return ret;
268 }
269 
270 /*
271  * Sends SD switch function CMD6.
272  * See table 4-32 in SD physical specification for argument details.
273  * When setting a function, we should set the 4 bit block of the command
274  * argument corresponding to that function to "value", and all other 4 bit
275  * blocks should be left as 0xF (no effect on current function)
276  */
sdmmc_switch(struct sd_card * card,enum sd_switch_arg mode,enum sd_group_num group,uint8_t value,uint8_t * response)277 static int sdmmc_switch(struct sd_card *card, enum sd_switch_arg mode,
278 	enum sd_group_num group, uint8_t value, uint8_t *response)
279 {
280 	struct sdhc_command cmd = {0};
281 	struct sdhc_data data = {0};
282 
283 	cmd.opcode = SD_SWITCH;
284 	cmd.arg = ((mode & 0x1) << 31) | 0x00FFFFFF;
285 	cmd.arg &= ~(0xFU << (group * 4));
286 	cmd.arg |= (value & 0xF) << (group * 4);
287 	cmd.response_type = (SD_RSP_TYPE_R1 | SD_SPI_RSP_TYPE_R1);
288 	cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
289 
290 	data.block_size = 64U;
291 	data.blocks = 1;
292 	data.data = response;
293 	data.timeout_ms = CONFIG_SD_DATA_TIMEOUT;
294 
295 	return sdhc_request(card->sdhc, &cmd, &data);
296 }
297 
sdmmc_read_switch(struct sd_card * card)298 static int sdmmc_read_switch(struct sd_card *card)
299 {
300 	uint8_t *status;
301 	int ret;
302 
303 	if (card->sd_version < SD_SPEC_VER1_1) {
304 		/* Switch not supported */
305 		LOG_INF("SD spec 1.01 does not support CMD6");
306 		return 0;
307 	}
308 	/* Use card internal buffer to read 64 byte switch data */
309 	status = card->card_buffer;
310 	/*
311 	 * Setting switch to zero will read card's support values,
312 	 * otherwise known as SD "check function"
313 	 */
314 	ret = sdmmc_switch(card, SD_SWITCH_CHECK, 0, 0, status);
315 	if (ret) {
316 		LOG_DBG("CMD6 failed %d", ret);
317 		return ret;
318 	}
319 	/*
320 	 * See table 4-11 and 4.3.10.4 of physical layer specification for
321 	 * bit definitions. Note that response is big endian, so index 13 will
322 	 * read bits 400-408.
323 	 * Bit n being set in support bit field indicates support for function
324 	 * number n on the card. (So 0x3 indicates support for functions 0 and 1)
325 	 */
326 	if (status[13] & HIGH_SPEED_BUS_SPEED) {
327 		card->switch_caps.hs_max_dtr = HS_MAX_DTR;
328 	}
329 	if (card->sd_version >= SD_SPEC_VER3_0) {
330 		card->switch_caps.bus_speed = status[13];
331 		card->switch_caps.sd_drv_type = status[9];
332 		card->switch_caps.sd_current_limit = status[7];
333 	}
334 	return 0;
335 }
336 
sdmmc_select_bus_speed(struct sd_card * card)337 static inline void sdmmc_select_bus_speed(struct sd_card *card)
338 {
339 	/*
340 	 * Note that function support is defined using bitfields, but function
341 	 * selection is defined using values 0x0-0xF.
342 	 */
343 	if (card->host_props.host_caps.sdr104_support &&
344 		(card->switch_caps.bus_speed & UHS_SDR104_BUS_SPEED) &&
345 		(card->host_props.f_max >= SD_CLOCK_208MHZ)) {
346 		card->card_speed = SD_TIMING_SDR104;
347 	} else if (card->host_props.host_caps.ddr50_support &&
348 		(card->switch_caps.bus_speed & UHS_DDR50_BUS_SPEED) &&
349 		(card->host_props.f_max >= SD_CLOCK_50MHZ)) {
350 		card->card_speed = SD_TIMING_DDR50;
351 	} else if (card->host_props.host_caps.sdr50_support &&
352 		(card->switch_caps.bus_speed & UHS_SDR50_BUS_SPEED) &&
353 		(card->host_props.f_max >= SD_CLOCK_100MHZ)) {
354 		card->card_speed = SD_TIMING_SDR50;
355 	} else if (card->host_props.host_caps.high_spd_support &&
356 		(card->switch_caps.bus_speed & UHS_SDR12_BUS_SPEED) &&
357 		(card->host_props.f_max >= SD_CLOCK_25MHZ)) {
358 		card->card_speed = SD_TIMING_SDR12;
359 	}
360 }
361 
362 /* Selects driver type for SD card */
sdmmc_select_driver_type(struct sd_card * card)363 static int sdmmc_select_driver_type(struct sd_card *card)
364 {
365 	int ret = 0;
366 	uint8_t *status = card->card_buffer;
367 
368 	/*
369 	 * We will only attempt to use driver type C over the default of type B,
370 	 * since it should result in lower current consumption if supported.
371 	 */
372 	if (card->host_props.host_caps.drv_type_c_support &&
373 		(card->switch_caps.sd_drv_type & SD_DRIVER_TYPE_C)) {
374 		card->bus_io.driver_type = SD_DRIVER_TYPE_C;
375 		/* Change drive strength */
376 		ret = sdmmc_switch(card, SD_SWITCH_SET,
377 			SD_GRP_DRIVER_STRENGTH_MODE,
378 			(find_msb_set(SD_DRIVER_TYPE_C) - 1), status);
379 	}
380 	return ret;
381 }
382 
383 /* Sets current limit for SD card */
sdmmc_set_current_limit(struct sd_card * card)384 static int sdmmc_set_current_limit(struct sd_card *card)
385 {
386 	int ret;
387 	int max_current = -1;
388 	uint8_t *status = card->card_buffer;
389 
390 	if ((card->card_speed != SD_TIMING_SDR50) &&
391 		(card->card_speed != SD_TIMING_SDR104) &&
392 		(card->card_speed != SD_TIMING_DDR50)) {
393 		return 0; /* Cannot set current limit */
394 	} else if (card->host_props.max_current_180 >= 800 &&
395 		(card->switch_caps.sd_current_limit & SD_MAX_CURRENT_800MA)) {
396 		max_current = SD_SET_CURRENT_800MA;
397 	} else if (card->host_props.max_current_180 >= 600 &&
398 		(card->switch_caps.sd_current_limit & SD_MAX_CURRENT_600MA)) {
399 		max_current = SD_SET_CURRENT_600MA;
400 	} else if (card->host_props.max_current_180 >= 400 &&
401 		(card->switch_caps.sd_current_limit & SD_MAX_CURRENT_400MA)) {
402 		max_current = SD_SET_CURRENT_400MA;
403 	} else if (card->host_props.max_current_180 >= 200 &&
404 		(card->switch_caps.sd_current_limit & SD_MAX_CURRENT_200MA)) {
405 		max_current = SD_SET_CURRENT_200MA;
406 	}
407 	if (max_current != -1) {
408 		LOG_DBG("Changing SD current limit: %d", max_current);
409 		/* Switch SD current */
410 		ret = sdmmc_switch(card, SD_SWITCH_SET, SD_GRP_CURRENT_LIMIT_MODE,
411 			max_current, status);
412 		if (ret) {
413 			LOG_DBG("Failed to set SD current limit");
414 			return ret;
415 		}
416 		if (((status[15] >> 4) & 0x0F) != max_current) {
417 			/* Status response indicates card did not select request limit */
418 			LOG_WRN("Card did not accept current limit");
419 		}
420 	}
421 	return 0;
422 }
423 
424 /* Applies selected card bus speed to card and host */
sdmmc_set_bus_speed(struct sd_card * card)425 static int sdmmc_set_bus_speed(struct sd_card *card)
426 {
427 	int ret;
428 	int timing = 0;
429 	uint8_t *status = card->card_buffer;
430 
431 	switch (card->card_speed) {
432 	/* Set bus clock speed */
433 	case SD_TIMING_SDR104:
434 		card->switch_caps.uhs_max_dtr = SD_CLOCK_208MHZ;
435 		timing = SDHC_TIMING_SDR104;
436 		break;
437 	case SD_TIMING_DDR50:
438 		card->switch_caps.uhs_max_dtr = SD_CLOCK_50MHZ;
439 		timing = SDHC_TIMING_DDR50;
440 		break;
441 	case SD_TIMING_SDR50:
442 		card->switch_caps.uhs_max_dtr = SD_CLOCK_100MHZ;
443 		timing = SDHC_TIMING_SDR50;
444 		break;
445 	case SD_TIMING_SDR25:
446 		card->switch_caps.uhs_max_dtr = SD_CLOCK_50MHZ;
447 		timing = SDHC_TIMING_SDR25;
448 		break;
449 	case SD_TIMING_SDR12:
450 		card->switch_caps.uhs_max_dtr = SD_CLOCK_25MHZ;
451 		timing = SDHC_TIMING_SDR12;
452 		break;
453 	default:
454 		/* No need to change bus speed */
455 		return 0;
456 	}
457 
458 	/* Switch bus speed */
459 	ret = sdmmc_switch(card, SD_SWITCH_SET, SD_GRP_TIMING_MODE,
460 		card->card_speed, status);
461 	if (ret) {
462 		LOG_DBG("Failed to switch SD card speed");
463 		return ret;
464 	}
465 	if ((status[16] & 0xF) != card->card_speed) {
466 		LOG_WRN("Card did not accept new speed");
467 	} else {
468 		/* Change host bus speed */
469 		card->bus_io.timing = timing;
470 		card->bus_io.clock = card->switch_caps.uhs_max_dtr;
471 		LOG_DBG("Setting bus clock to: %d", card->bus_io.clock);
472 		ret = sdhc_set_io(card->sdhc, &card->bus_io);
473 		if (ret) {
474 			LOG_ERR("Failed to change host bus speed");
475 			return ret;
476 		}
477 	}
478 	return 0;
479 }
480 
481 /*
482  * Init UHS capable SD card. Follows figure 3-16 in physical layer specification.
483  */
sdmmc_init_uhs(struct sd_card * card)484 static int sdmmc_init_uhs(struct sd_card *card)
485 {
486 	int ret;
487 
488 	/* Raise bus width to 4 bits */
489 	ret = sdmmc_set_bus_width(card, SDHC_BUS_WIDTH4BIT);
490 	if (ret) {
491 		LOG_ERR("Failed to change card bus width to 4 bits");
492 		return ret;
493 	}
494 
495 	/* Select bus speed for card depending on host and card capability*/
496 	sdmmc_select_bus_speed(card);
497 	/* Now, set the driver strength for the card */
498 	ret = sdmmc_select_driver_type(card);
499 	if (ret) {
500 		LOG_DBG("Failed to select new driver type");
501 		return ret;
502 	}
503 	ret = sdmmc_set_current_limit(card);
504 	if (ret) {
505 		LOG_DBG("Failed to set card current limit");
506 		return ret;
507 	}
508 	/* Apply the bus speed selected earlier */
509 	ret = sdmmc_set_bus_speed(card);
510 	if (ret) {
511 		LOG_DBG("Failed to set card bus speed");
512 		return ret;
513 	}
514 	if (card->card_speed == SD_TIMING_SDR50 ||
515 		card->card_speed == SD_TIMING_SDR104 ||
516 		card->card_speed == SD_TIMING_DDR50) {
517 		/* SDR104, SDR50, and DDR50 mode need tuning */
518 		ret = sdhc_execute_tuning(card->sdhc);
519 		if (ret) {
520 			LOG_ERR("SD tuning failed: %d", ret);
521 		}
522 	}
523 	return ret;
524 }
525 
526 /* Performs initialization for SD high speed cards */
sdmmc_init_hs(struct sd_card * card)527 static int sdmmc_init_hs(struct sd_card *card)
528 {
529 	int ret;
530 
531 	if ((!card->host_props.host_caps.high_spd_support) ||
532 		(card->sd_version < SD_SPEC_VER1_1) ||
533 		(card->switch_caps.hs_max_dtr == 0)) {
534 		/* No high speed support. Leave card untouched */
535 		return 0;
536 	}
537 	card->card_speed = SD_TIMING_SDR25;
538 	ret = sdmmc_set_bus_speed(card);
539 	if (ret) {
540 		LOG_ERR("Failed to switch card to HS mode");
541 		return ret;
542 	}
543 	if (card->flags & SD_4BITS_WIDTH) {
544 		/* Raise bus width to 4 bits */
545 		ret = sdmmc_set_bus_width(card, SDHC_BUS_WIDTH4BIT);
546 		if (ret) {
547 			LOG_ERR("Failed to change card bus width to 4 bits");
548 			return ret;
549 		}
550 	}
551 	return 0;
552 }
553 
554 /*
555  * Initializes SDMMC card. Note that the common SD function has already
556  * sent CMD0 and CMD8 to the card at function entry.
557  */
sdmmc_card_init(struct sd_card * card)558 int sdmmc_card_init(struct sd_card *card)
559 {
560 	int ret;
561 	uint32_t ocr_arg = 0U;
562 
563 	/* First send a probing OCR */
564 	if (card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) {
565 		/* Probe SPI card with CMD58*/
566 		ret = sdmmc_spi_send_ocr(card, ocr_arg);
567 	} else if (IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) {
568 		/* Probe Native card with ACMD41 */
569 		ret = sdmmc_send_ocr(card, ocr_arg);
570 	} else {
571 		return -ENOTSUP;
572 	}
573 	if (ret) {
574 		return ret;
575 	}
576 	/* Card responded to ACMD41, type is SDMMC */
577 	card->type = CARD_SDMMC;
578 
579 	if (card->flags & SD_SDHC_FLAG) {
580 		if (IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) {
581 			/* High capacity card. See if host supports 1.8V */
582 			if (card->host_props.host_caps.vol_180_support) {
583 				ocr_arg |= SD_OCR_SWITCH_18_REQ_FLAG;
584 			}
585 		}
586 		/* Set host high capacity support flag */
587 		ocr_arg |= SD_OCR_HOST_CAP_FLAG;
588 	}
589 	if (IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) {
590 		/* Set voltage window */
591 		if (card->host_props.host_caps.vol_300_support) {
592 			ocr_arg |= SD_OCR_VDD29_30FLAG;
593 		}
594 		ocr_arg |= (SD_OCR_VDD32_33FLAG | SD_OCR_VDD33_34FLAG);
595 	}
596 	/* Momentary delay before initialization OCR. Some cards will
597 	 * never leave busy state if init OCR is sent too soon after
598 	 * probing OCR
599 	 */
600 	k_busy_wait(100);
601 	/* Send SD OCR to card to initialize it */
602 	ret = sdmmc_send_ocr(card, ocr_arg);
603 	if (ret) {
604 		LOG_ERR("Failed to query card OCR");
605 		return ret;
606 	}
607 	if (card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) {
608 		/* Send second CMD58 to get CCS bit */
609 		ret = sdmmc_spi_send_ocr(card, ocr_arg);
610 		if (ret) {
611 			return ret;
612 		}
613 	}
614 	/* Check SD high capacity and 1.8V support flags */
615 	if (card->ocr & SD_OCR_CARD_CAP_FLAG) {
616 		card->flags |= SD_HIGH_CAPACITY_FLAG;
617 	}
618 	if (card->ocr & SD_OCR_SWITCH_18_ACCEPT_FLAG) {
619 		LOG_DBG("Card supports 1.8V signaling");
620 		card->flags |= SD_1800MV_FLAG;
621 	}
622 	/* Check OCR voltage window */
623 	if (card->ocr & SD_OCR_VDD29_30FLAG) {
624 		card->flags |= SD_3000MV_FLAG;
625 	}
626 	/*
627 	 * If card is high capacity (SDXC or SDHC), and supports 1.8V signaling,
628 	 * switch to new signal voltage using "signal voltage switch procedure"
629 	 * described in SD specification
630 	 */
631 	if ((card->flags & SD_1800MV_FLAG) &&
632 		(card->host_props.host_caps.vol_180_support) &&
633 		(!card->host_props.is_spi) &&
634 		IS_ENABLED(CONFIG_SD_UHS_PROTOCOL)) {
635 		ret = sdmmc_switch_voltage(card);
636 		if (ret) {
637 			/* Disable host support for 1.8 V */
638 			card->host_props.host_caps.vol_180_support = false;
639 			/*
640 			 * The host or SD card may have already switched to
641 			 * 1.8V. Return SD_RESTART to indicate
642 			 * negotiation should be restarted.
643 			 */
644 			card->status = CARD_ERROR;
645 			return SD_RESTART;
646 		}
647 	}
648 	/* Read the card's CID (card identification register) */
649 	ret = card_read_cid(card);
650 	if (ret) {
651 		return ret;
652 	}
653 	if (!card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) {
654 		/*
655 		 * Request new relative card address. This moves the card from
656 		 * identification mode to data transfer mode
657 		 */
658 		ret = sdmmc_request_rca(card);
659 		if (ret) {
660 			return ret;
661 		}
662 	}
663 	/* Card has entered data transfer mode. Get card specific data register */
664 	ret = sdmmc_read_csd(card);
665 	if (ret) {
666 		return ret;
667 	}
668 	if (!card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) {
669 		/* Move the card to transfer state (with CMD7) to run remaining commands */
670 		ret = sdmmc_select_card(card);
671 		if (ret) {
672 			return ret;
673 		}
674 	}
675 	/*
676 	 * With card in data transfer state, we can set SD clock to maximum
677 	 * frequency for non high speed mode (25Mhz)
678 	 */
679 	if (card->host_props.f_max < SD_CLOCK_25MHZ) {
680 		LOG_INF("Maximum SD clock is under 25MHz, using clock of %dHz",
681 			card->host_props.f_max);
682 		card->bus_io.clock = card->host_props.f_max;
683 	} else {
684 		card->bus_io.clock = SD_CLOCK_25MHZ;
685 	}
686 	ret = sdhc_set_io(card->sdhc, &card->bus_io);
687 	if (ret) {
688 		LOG_ERR("Failed to raise bus frequency to 25MHz");
689 		return ret;
690 	}
691 	/* Read SD SCR (SD configuration register),
692 	 * to get supported bus width
693 	 */
694 	ret = sdmmc_read_scr(card);
695 	if (ret) {
696 		return ret;
697 	}
698 	/* Read switch capabilities to determine what speeds card supports */
699 	if (!card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) {
700 		ret = sdmmc_read_switch(card);
701 		if (ret) {
702 			LOG_ERR("Failed to read card functions");
703 			return ret;
704 		}
705 	}
706 	if ((card->flags & SD_1800MV_FLAG) &&
707 		sdmmc_host_uhs(&card->host_props) &&
708 		!(card->host_props.is_spi) &&
709 		IS_ENABLED(CONFIG_SD_UHS_PROTOCOL)) {
710 		ret = sdmmc_init_uhs(card);
711 		if (ret) {
712 			LOG_ERR("UHS card init failed");
713 		}
714 	} else {
715 		if ((card->flags & SD_HIGH_CAPACITY_FLAG) == 0) {
716 			/* Standard capacity SDSC card. set block length to 512 */
717 			ret = sdmmc_set_blocklen(card, SDMMC_DEFAULT_BLOCK_SIZE);
718 			if (ret) {
719 				LOG_ERR("Could not set SD blocklen to 512");
720 				return ret;
721 			}
722 			card->block_size = 512;
723 		}
724 		/* Card is not UHS. Try to use high speed mode */
725 		ret = sdmmc_init_hs(card);
726 		if (ret) {
727 			LOG_ERR("HS card init failed");
728 		}
729 	}
730 	return ret;
731 }
732 
sdmmc_ioctl(struct sd_card * card,uint8_t cmd,void * buf)733 int sdmmc_ioctl(struct sd_card *card, uint8_t cmd, void *buf)
734 {
735 	return card_ioctl(card, cmd, buf);
736 }
737 
sdmmc_read_blocks(struct sd_card * card,uint8_t * rbuf,uint32_t start_block,uint32_t num_blocks)738 int sdmmc_read_blocks(struct sd_card *card, uint8_t *rbuf,
739 	uint32_t start_block, uint32_t num_blocks)
740 {
741 	return card_read_blocks(card, rbuf, start_block, num_blocks);
742 }
743 
sdmmc_write_blocks(struct sd_card * card,const uint8_t * wbuf,uint32_t start_block,uint32_t num_blocks)744 int sdmmc_write_blocks(struct sd_card *card, const uint8_t *wbuf,
745 	uint32_t start_block, uint32_t num_blocks)
746 {
747 	return card_write_blocks(card, wbuf, start_block, num_blocks);
748 }
749