1 /*
2 * Copyright 2022-2023 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
14 #include "sd_ops.h"
15 #include "sd_utils.h"
16
17 LOG_MODULE_DECLARE(sd, CONFIG_SD_LOG_LEVEL);
18
19 uint8_t cis_tuples[] = {
20 SDIO_TPL_CODE_MANIFID,
21 SDIO_TPL_CODE_FUNCID,
22 SDIO_TPL_CODE_FUNCE,
23 };
24
25 /*
26 * Send SDIO OCR using CMD5
27 */
sdio_send_ocr(struct sd_card * card,uint32_t ocr)28 static int sdio_send_ocr(struct sd_card *card, uint32_t ocr)
29 {
30 struct sdhc_command cmd = {0};
31 int ret;
32 int retries;
33
34 cmd.opcode = SDIO_SEND_OP_COND;
35 cmd.arg = ocr;
36 cmd.response_type = (SD_RSP_TYPE_R4 | SD_SPI_RSP_TYPE_R4);
37 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
38 /* Send OCR5 to initialize card */
39 for (retries = 0; retries < CONFIG_SD_OCR_RETRY_COUNT; retries++) {
40 ret = sdhc_request(card->sdhc, &cmd, NULL);
41 if (ret) {
42 if (ocr == 0) {
43 /* Just probing card, likely not SDIO */
44 return SD_NOT_SDIO;
45 }
46 return ret;
47 }
48 if (ocr == 0) {
49 /* We are probing card, check number of IO functions */
50 card->num_io = (cmd.response[0] & SDIO_OCR_IO_NUMBER)
51 >> SDIO_OCR_IO_NUMBER_SHIFT;
52 if ((card->num_io == 0) ||
53 ((cmd.response[0] & SDIO_IO_OCR_MASK) == 0)) {
54 if (cmd.response[0] & SDIO_OCR_MEM_PRESENT_FLAG) {
55 /* Card is not an SDIO card */
56 return SD_NOT_SDIO;
57 }
58 /* Card is not a supported SD device */
59 return -ENOTSUP;
60 }
61 /* Card has IO present, return zero to
62 * indicate SDIO card
63 */
64 return 0;
65 }
66 /* Check to see if card is busy with power up */
67 if (cmd.response[0] & SD_OCR_PWR_BUSY_FLAG) {
68 break;
69 }
70 /* Delay before retrying command */
71 sd_delay(10);
72 }
73 if (retries >= CONFIG_SD_OCR_RETRY_COUNT) {
74 /* OCR timed out */
75 LOG_ERR("Card never left busy state");
76 return -ETIMEDOUT;
77 }
78 LOG_DBG("SDIO responded to CMD5 after %d attempts", retries);
79 if (!card->host_props.is_spi) {
80 /* Save OCR */
81 card->ocr = cmd.response[0U];
82 }
83 return 0;
84 }
85
sdio_io_rw_direct(struct sd_card * card,enum sdio_io_dir direction,enum sdio_func_num func,uint32_t reg_addr,uint8_t data_in,uint8_t * data_out)86 static int sdio_io_rw_direct(struct sd_card *card,
87 enum sdio_io_dir direction,
88 enum sdio_func_num func,
89 uint32_t reg_addr,
90 uint8_t data_in,
91 uint8_t *data_out)
92 {
93 int ret;
94 struct sdhc_command cmd = {0};
95
96 cmd.opcode = SDIO_RW_DIRECT;
97 cmd.arg = (func << SDIO_CMD_ARG_FUNC_NUM_SHIFT) |
98 ((reg_addr & SDIO_CMD_ARG_REG_ADDR_MASK) << SDIO_CMD_ARG_REG_ADDR_SHIFT);
99 if (direction == SDIO_IO_WRITE) {
100 cmd.arg |= data_in & SDIO_DIRECT_CMD_DATA_MASK;
101 cmd.arg |= BIT(SDIO_CMD_ARG_RW_SHIFT);
102 if (data_out) {
103 cmd.arg |= BIT(SDIO_DIRECT_CMD_ARG_RAW_SHIFT);
104 }
105 }
106 cmd.response_type = (SD_RSP_TYPE_R5 | SD_SPI_RSP_TYPE_R5);
107 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
108
109 ret = sdhc_request(card->sdhc, &cmd, NULL);
110 if (ret) {
111 return ret;
112 }
113 if (data_out) {
114 if (card->host_props.is_spi) {
115 *data_out = (cmd.response[0U] >> 8) & SDIO_DIRECT_CMD_DATA_MASK;
116 } else {
117 *data_out = cmd.response[0U] & SDIO_DIRECT_CMD_DATA_MASK;
118 }
119 }
120 return ret;
121 }
122
123
sdio_io_rw_extended(struct sd_card * card,enum sdio_io_dir direction,enum sdio_func_num func,uint32_t reg_addr,bool increment,uint8_t * buf,uint32_t blocks,uint32_t block_size)124 static int sdio_io_rw_extended(struct sd_card *card,
125 enum sdio_io_dir direction,
126 enum sdio_func_num func,
127 uint32_t reg_addr,
128 bool increment,
129 uint8_t *buf,
130 uint32_t blocks,
131 uint32_t block_size)
132 {
133 struct sdhc_command cmd = {0};
134 struct sdhc_data data = {0};
135
136 cmd.opcode = SDIO_RW_EXTENDED;
137 cmd.arg = (func << SDIO_CMD_ARG_FUNC_NUM_SHIFT) |
138 ((reg_addr & SDIO_CMD_ARG_REG_ADDR_MASK) << SDIO_CMD_ARG_REG_ADDR_SHIFT);
139 cmd.arg |= (direction == SDIO_IO_WRITE) ? BIT(SDIO_CMD_ARG_RW_SHIFT) : 0;
140 cmd.arg |= increment ? BIT(SDIO_EXTEND_CMD_ARG_OP_CODE_SHIFT) : 0;
141 cmd.response_type = (SD_RSP_TYPE_R5 | SD_SPI_RSP_TYPE_R5);
142 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
143 if (blocks == 0) {
144 /* Byte mode */
145 cmd.arg |= (block_size == 512) ? 0 : block_size;
146 } else {
147 /* Block mode */
148 cmd.arg |= BIT(SDIO_EXTEND_CMD_ARG_BLK_SHIFT) | blocks;
149 }
150
151 data.block_size = block_size;
152 /* Host expects blocks to be at least 1 */
153 data.blocks = blocks ? blocks : 1;
154 data.data = buf;
155 data.timeout_ms = CONFIG_SD_DATA_TIMEOUT;
156 return sdhc_request(card->sdhc, &cmd, &data);
157 }
158
159 /*
160 * Helper for extended r/w. Splits the transfer into the minimum possible
161 * number of block r/w, then uses byte transfers for remaining data
162 */
sdio_io_rw_extended_helper(struct sdio_func * func,enum sdio_io_dir direction,uint32_t reg_addr,bool increment,uint8_t * buf,uint32_t len)163 static int sdio_io_rw_extended_helper(struct sdio_func *func,
164 enum sdio_io_dir direction,
165 uint32_t reg_addr,
166 bool increment,
167 uint8_t *buf,
168 uint32_t len)
169 {
170 int ret;
171 int remaining = len;
172 uint32_t blocks, size;
173
174 if (func->num > SDIO_MAX_IO_NUMS) {
175 return -EINVAL;
176 }
177
178 if ((func->card->cccr_flags & SDIO_SUPPORT_MULTIBLOCK) &&
179 ((len > func->block_size))) {
180 /* Use block I/O for r/w where possible */
181 while (remaining >= func->block_size) {
182 blocks = remaining / func->block_size;
183 size = blocks * func->block_size;
184 ret = sdio_io_rw_extended(func->card, direction,
185 func->num, reg_addr, increment, buf, blocks,
186 func->block_size);
187 if (ret) {
188 return ret;
189 }
190 /* Update remaining length and buffer pointer */
191 remaining -= size;
192 buf += size;
193 if (increment) {
194 reg_addr += size;
195 }
196 }
197 }
198 /* Remaining data must be written using byte I/O */
199 while (remaining > 0) {
200 size = MIN(remaining, func->cis.max_blk_size);
201
202 ret = sdio_io_rw_extended(func->card, direction, func->num,
203 reg_addr, increment, buf, 0, size);
204 if (ret) {
205 return ret;
206 }
207 remaining -= size;
208 buf += size;
209 if (increment) {
210 reg_addr += size;
211 }
212 }
213 return 0;
214 }
215
216 /*
217 * Read card capability register to determine features card supports.
218 */
sdio_read_cccr(struct sd_card * card)219 static int sdio_read_cccr(struct sd_card *card)
220 {
221 int ret;
222 uint8_t data;
223 uint32_t cccr_ver;
224
225 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
226 SDIO_CCCR_CCCR, 0, &data);
227 if (ret) {
228 LOG_DBG("CCCR read failed: %d", ret);
229 return ret;
230 }
231 cccr_ver = (data & SDIO_CCCR_CCCR_REV_MASK) >>
232 SDIO_CCCR_CCCR_REV_SHIFT;
233 LOG_DBG("SDIO cccr revision %u", cccr_ver);
234 /* Read SD spec version */
235 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
236 SDIO_CCCR_SD, 0, &data);
237 if (ret) {
238 return ret;
239 }
240 card->sd_version = (data & SDIO_CCCR_SD_SPEC_MASK) >> SDIO_CCCR_SD_SPEC_SHIFT;
241 /* Read CCCR capability flags */
242 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
243 SDIO_CCCR_CAPS, 0, &data);
244 if (ret) {
245 return ret;
246 }
247 card->cccr_flags = 0;
248 if (data & SDIO_CCCR_CAPS_BLS) {
249 card->cccr_flags |= SDIO_SUPPORT_4BIT_LS_BUS;
250 }
251 if (data & SDIO_CCCR_CAPS_SMB) {
252 card->cccr_flags |= SDIO_SUPPORT_MULTIBLOCK;
253 }
254 if (cccr_ver >= SDIO_CCCR_CCCR_REV_2_00) {
255 /* Read high speed properties */
256 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
257 SDIO_CCCR_SPEED, 0, &data);
258 if (ret) {
259 return ret;
260 }
261 if (data & SDIO_CCCR_SPEED_SHS) {
262 card->cccr_flags |= SDIO_SUPPORT_HS;
263 }
264 }
265 if (cccr_ver >= SDIO_CCCR_CCCR_REV_3_00 &&
266 (card->flags & SD_1800MV_FLAG)) {
267 /* Read UHS properties */
268 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
269 SDIO_CCCR_UHS, 0, &data);
270 if (ret) {
271 return ret;
272 }
273 if (sdmmc_host_uhs(&card->host_props)) {
274 if (data & SDIO_CCCR_UHS_SDR50) {
275 card->cccr_flags |= SDIO_SUPPORT_SDR50;
276 }
277 if (data & SDIO_CCCR_UHS_SDR104) {
278 card->cccr_flags |= SDIO_SUPPORT_SDR104;
279 }
280 if (data & SDIO_CCCR_UHS_DDR50) {
281 card->cccr_flags |= SDIO_SUPPORT_DDR50;
282 }
283 }
284
285 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
286 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
287 if (ret) {
288 return ret;
289 }
290 card->switch_caps.sd_drv_type = 0;
291 if (data & SDIO_CCCR_DRIVE_STRENGTH_A) {
292 card->switch_caps.sd_drv_type |= SD_DRIVER_TYPE_A;
293 }
294 if (data & SDIO_CCCR_DRIVE_STRENGTH_C) {
295 card->switch_caps.sd_drv_type |= SD_DRIVER_TYPE_C;
296 }
297 if (data & SDIO_CCCR_DRIVE_STRENGTH_D) {
298 card->switch_caps.sd_drv_type |= SD_DRIVER_TYPE_D;
299 }
300 }
301 return 0;
302 }
303
sdio_decode_cis(struct sdio_cis * cis,enum sdio_func_num func,uint8_t * data,uint8_t tpl_code,uint8_t tpl_link)304 static void sdio_decode_cis(struct sdio_cis *cis, enum sdio_func_num func,
305 uint8_t *data, uint8_t tpl_code, uint8_t tpl_link)
306 {
307 switch (tpl_code) {
308 case SDIO_TPL_CODE_MANIFID:
309 cis->manf_id = data[0] | ((uint16_t)data[1] << 8);
310 cis->manf_code = data[2] | ((uint16_t)data[3] << 8);
311 break;
312 case SDIO_TPL_CODE_FUNCID:
313 cis->func_id = data[0];
314 break;
315 case SDIO_TPL_CODE_FUNCE:
316 if (func == 0) {
317 cis->max_blk_size = data[1] | ((uint16_t)data[2] << 8);
318 cis->max_speed = data[3];
319 } else {
320 cis->max_blk_size = data[12] | ((uint16_t)data[13] << 8);
321 cis->rdy_timeout = data[28] | ((uint16_t)data[29] << 8);
322 }
323 break;
324 default:
325 LOG_WRN("Unknown CIS tuple %d", tpl_code);
326 break;
327 }
328 }
329
330 /*
331 * Read CIS for a given SDIO function.
332 * Tuples provides a list of tuples that should be decoded.
333 */
sdio_read_cis(struct sdio_func * func,uint8_t * tuples,uint32_t tuple_count)334 static int sdio_read_cis(struct sdio_func *func,
335 uint8_t *tuples,
336 uint32_t tuple_count)
337 {
338 int ret;
339 char *data = func->card->card_buffer;
340 uint32_t cis_ptr = 0, num = 0;
341 uint8_t tpl_code, tpl_link;
342 bool match_tpl = false;
343
344 memset(&func->cis, 0, sizeof(struct sdio_cis));
345 /* First find the CIS pointer for this function */
346 for (int i = 0; i < 3; i++) {
347 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
348 SDIO_FBR_BASE(func->num) + SDIO_FBR_CIS + i, 0, data);
349 if (ret) {
350 return ret;
351 }
352 cis_ptr |= *data << (i * 8);
353 }
354 /* Read CIS tuples until we have read all requested CIS tuple codes */
355 do {
356 /* Read tuple code */
357 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
358 cis_ptr++, 0, &tpl_code);
359 if (ret) {
360 return ret;
361 }
362 if (tpl_code == SDIO_TPL_CODE_END) {
363 /* End of tuple chain */
364 break;
365 }
366 if (tpl_code == SDIO_TPL_CODE_NULL) {
367 /* Skip NULL tuple */
368 continue;
369 }
370 /* Read tuple link */
371 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
372 cis_ptr++, 0, &tpl_link);
373 if (ret) {
374 return ret;
375 }
376 if (tpl_link == SDIO_TPL_CODE_END) {
377 /* End of tuple chain */
378 break;
379 }
380 /* Check to see if read tuple matches any we should look for */
381 for (int i = 0; i < tuple_count; i++) {
382 if (tpl_code == tuples[i]) {
383 match_tpl = true;
384 break;
385 }
386 }
387 if (match_tpl) {
388 /* tuple chains may be maximum of 255 bytes long */
389 memset(data, 0, 255);
390 for (int i = 0; i < tpl_link; i++) {
391 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ,
392 SDIO_FUNC_NUM_0, cis_ptr++, 0, data + i);
393 if (ret) {
394 return ret;
395 }
396 }
397 num++;
398 match_tpl = false;
399 /* Decode the CIS data we read */
400 sdio_decode_cis(&func->cis, func->num, data,
401 tpl_code, tpl_link);
402 } else {
403 /* Advance CIS pointer */
404 cis_ptr += tpl_link;
405 }
406 } while (num < tuple_count);
407 LOG_DBG("SDIO CIS max block size for func %d: %d", func->num,
408 func->cis.max_blk_size);
409 return ret;
410 }
411
sdio_set_bus_width(struct sd_card * card,enum sdhc_bus_width width)412 static int sdio_set_bus_width(struct sd_card *card, enum sdhc_bus_width width)
413 {
414 uint8_t reg_bus_interface = 0U;
415 int ret;
416
417 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
418 SDIO_CCCR_BUS_IF, 0, ®_bus_interface);
419 if (ret) {
420 return ret;
421 }
422 reg_bus_interface &= ~SDIO_CCCR_BUS_IF_WIDTH_MASK;
423 switch (width) {
424 case SDHC_BUS_WIDTH1BIT:
425 reg_bus_interface |= SDIO_CCCR_BUS_IF_WIDTH_1_BIT;
426 break;
427 case SDHC_BUS_WIDTH4BIT:
428 reg_bus_interface |= SDIO_CCCR_BUS_IF_WIDTH_4_BIT;
429 break;
430 case SDHC_BUS_WIDTH8BIT:
431 reg_bus_interface |= SDIO_CCCR_BUS_IF_WIDTH_8_BIT;
432 break;
433 default:
434 return -ENOTSUP;
435 }
436 ret = sdio_io_rw_direct(card, SDIO_IO_WRITE, SDIO_FUNC_NUM_0,
437 SDIO_CCCR_BUS_IF, reg_bus_interface, ®_bus_interface);
438 if (ret) {
439 return ret;
440 }
441 /* Card now has changed bus width. Change host bus width */
442 card->bus_io.bus_width = width;
443 ret = sdhc_set_io(card->sdhc, &card->bus_io);
444 if (ret) {
445 LOG_DBG("Could not change host bus width");
446 }
447 return ret;
448 }
449
sdio_select_bus_speed(struct sd_card * card)450 static inline void sdio_select_bus_speed(struct sd_card *card)
451 {
452 if (card->host_props.host_caps.sdr104_support &&
453 (card->cccr_flags & SDIO_SUPPORT_SDR104) &&
454 (card->host_props.f_max >= SD_CLOCK_208MHZ)) {
455 card->card_speed = SD_TIMING_SDR104;
456 } else if (card->host_props.host_caps.ddr50_support &&
457 (card->cccr_flags & SDIO_SUPPORT_DDR50) &&
458 (card->host_props.f_max >= SD_CLOCK_50MHZ)) {
459 card->card_speed = SD_TIMING_DDR50;
460 } else if (card->host_props.host_caps.sdr50_support &&
461 (card->cccr_flags & SDIO_SUPPORT_SDR50) &&
462 (card->host_props.f_max >= SD_CLOCK_100MHZ)) {
463 card->card_speed = SD_TIMING_SDR50;
464 } else if (card->host_props.host_caps.high_spd_support &&
465 (card->switch_caps.bus_speed & SDIO_SUPPORT_HS) &&
466 (card->host_props.f_max >= SD_CLOCK_50MHZ)) {
467 card->card_speed = SD_TIMING_SDR25;
468 } else {
469 card->card_speed = SD_TIMING_SDR12;
470 }
471 }
472
473 /* Applies selected card bus speed to card and host */
sdio_set_bus_speed(struct sd_card * card)474 static int sdio_set_bus_speed(struct sd_card *card)
475 {
476 int ret, timing, retries = CONFIG_SD_RETRY_COUNT;
477 uint8_t speed_reg, target_speed;
478
479 switch (card->card_speed) {
480 /* Set bus clock speed */
481 case SD_TIMING_SDR104:
482 card->switch_caps.uhs_max_dtr = SD_CLOCK_208MHZ;
483 target_speed = SDIO_CCCR_SPEED_SDR104;
484 timing = SDHC_TIMING_SDR104;
485 break;
486 case SD_TIMING_DDR50:
487 card->switch_caps.uhs_max_dtr = SD_CLOCK_50MHZ;
488 target_speed = SDIO_CCCR_SPEED_DDR50;
489 timing = SDHC_TIMING_DDR50;
490 break;
491 case SD_TIMING_SDR50:
492 card->switch_caps.uhs_max_dtr = SD_CLOCK_100MHZ;
493 target_speed = SDIO_CCCR_SPEED_SDR50;
494 timing = SDHC_TIMING_SDR50;
495 break;
496 case SD_TIMING_SDR25:
497 card->switch_caps.uhs_max_dtr = SD_CLOCK_50MHZ;
498 target_speed = SDIO_CCCR_SPEED_SDR25;
499 timing = SDHC_TIMING_SDR25;
500 break;
501 case SD_TIMING_SDR12:
502 card->switch_caps.uhs_max_dtr = SD_CLOCK_25MHZ;
503 target_speed = SDIO_CCCR_SPEED_SDR12;
504 timing = SDHC_TIMING_SDR12;
505 break;
506 default:
507 /* No need to change bus speed */
508 return 0;
509 }
510 /* Read the bus speed register */
511 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
512 SDIO_CCCR_SPEED, 0, &speed_reg);
513 if (ret) {
514 return ret;
515 }
516 /* Attempt to set speed several times */
517 do {
518 /* Set new speed */
519 speed_reg &= ~SDIO_CCCR_SPEED_MASK;
520 speed_reg |= (target_speed << SDIO_CCCR_SPEED_SHIFT);
521 ret = sdio_io_rw_direct(card, SDIO_IO_WRITE, SDIO_FUNC_NUM_0,
522 SDIO_CCCR_SPEED, speed_reg, &speed_reg);
523 if (ret) {
524 return ret;
525 }
526 } while (((speed_reg & target_speed) != target_speed) && retries-- > 0);
527 if (retries == 0) {
528 /* Don't error out, as card can still work */
529 LOG_WRN("Could not set target SDIO speed");
530 } else {
531 /* Set card bus clock and timing */
532 card->bus_io.timing = timing;
533 card->bus_io.clock = card->switch_caps.uhs_max_dtr;
534 LOG_DBG("Setting bus clock to: %d", card->bus_io.clock);
535 ret = sdhc_set_io(card->sdhc, &card->bus_io);
536 if (ret) {
537 LOG_ERR("Failed to change host bus speed");
538 return ret;
539 }
540 }
541 return ret;
542 }
543
544 /*
545 * Initialize an SDIO card for use with subsystem
546 */
sdio_card_init(struct sd_card * card)547 int sdio_card_init(struct sd_card *card)
548 {
549 int ret;
550 uint32_t ocr_arg = 0U;
551
552 /* Probe card with SDIO OCR CM5 */
553 ret = sdio_send_ocr(card, ocr_arg);
554 if (ret) {
555 return ret;
556 }
557 /* Card responded to CMD5, type is SDIO */
558 card->type = CARD_SDIO;
559 /* Set voltage window */
560 if (card->host_props.host_caps.vol_300_support) {
561 ocr_arg |= SD_OCR_VDD29_30FLAG;
562 }
563 ocr_arg |= (SD_OCR_VDD32_33FLAG | SD_OCR_VDD33_34FLAG);
564 if (IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE) &&
565 card->host_props.host_caps.vol_180_support) {
566 /* See if the card also supports 1.8V */
567 ocr_arg |= SD_OCR_SWITCH_18_REQ_FLAG;
568 }
569 ret = sdio_send_ocr(card, ocr_arg);
570 if (ret) {
571 return ret;
572 }
573 if (card->ocr & SD_OCR_SWITCH_18_ACCEPT_FLAG) {
574 LOG_DBG("Card supports 1.8V signalling");
575 card->flags |= SD_1800MV_FLAG;
576 }
577 /* Check OCR voltage window */
578 if (card->ocr & SD_OCR_VDD29_30FLAG) {
579 card->flags |= SD_3000MV_FLAG;
580 }
581 /* Check mem present flag */
582 if (card->ocr & SDIO_OCR_MEM_PRESENT_FLAG) {
583 card->flags |= SD_MEM_PRESENT_FLAG;
584 }
585 /* Following steps are only required when using native SD mode */
586 if (IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) {
587 /*
588 * If card and host support 1.8V, perform voltage switch sequence now.
589 * note that we skip this switch if the UHS protocol is not enabled.
590 */
591 if ((card->flags & SD_1800MV_FLAG) &&
592 (!card->host_props.is_spi) &&
593 (card->host_props.host_caps.vol_180_support) &&
594 IS_ENABLED(CONFIG_SD_UHS_PROTOCOL)) {
595 ret = sdmmc_switch_voltage(card);
596 if (ret) {
597 /* Disable host support for 1.8 V */
598 card->host_props.host_caps.vol_180_support = false;
599 /*
600 * The host or SD card may have already switched to
601 * 1.8V. Return SD_RESTART to indicate
602 * negotiation should be restarted.
603 */
604 card->status = CARD_ERROR;
605 return SD_RESTART;
606 }
607 }
608 if ((card->flags & SD_MEM_PRESENT_FLAG) &&
609 ((card->flags & SD_SDHC_FLAG) == 0)) {
610 /* We must send CMD2 to get card cid */
611 ret = card_read_cid(card);
612 if (ret) {
613 return ret;
614 }
615 }
616 /* Send CMD3 to get card relative address */
617 ret = sdmmc_request_rca(card);
618 if (ret) {
619 return ret;
620 }
621 /* Move the card to transfer state (with CMD7) to run
622 * remaining commands
623 */
624 ret = sdmmc_select_card(card);
625 if (ret) {
626 return ret;
627 }
628 }
629 /* Read SDIO card common control register */
630 ret = sdio_read_cccr(card);
631 if (ret) {
632 return ret;
633 }
634 /* Initialize internal card function 0 structure */
635 card->func0.num = SDIO_FUNC_NUM_0;
636 card->func0.card = card;
637 ret = sdio_read_cis(&card->func0, cis_tuples,
638 ARRAY_SIZE(cis_tuples));
639 if (ret) {
640 return ret;
641 }
642
643 /* If card and host support 4 bit bus, enable it */
644 if (IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE) &&
645 ((card->cccr_flags & SDIO_SUPPORT_HS) ||
646 (card->cccr_flags & SDIO_SUPPORT_4BIT_LS_BUS))) {
647 /* Raise bus width to 4 bits */
648 ret = sdio_set_bus_width(card, SDHC_BUS_WIDTH4BIT);
649 if (ret) {
650 return ret;
651 }
652 LOG_DBG("Raised card bus width to 4 bits");
653 }
654
655 /* Select and set bus speed */
656 sdio_select_bus_speed(card);
657 ret = sdio_set_bus_speed(card);
658 if (ret) {
659 return ret;
660 }
661 if (card->card_speed == SD_TIMING_SDR50 ||
662 card->card_speed == SD_TIMING_SDR104) {
663 /* SDR104, SDR50, and DDR50 mode need tuning */
664 ret = sdhc_execute_tuning(card->sdhc);
665 if (ret) {
666 LOG_ERR("SD tuning failed: %d", ret);
667 }
668 }
669 return ret;
670 }
671
672 /**
673 * @brief Initialize SDIO function.
674 *
675 * Initializes SDIO card function. The card function will not be enabled,
676 * but after this call returns the SDIO function structure can be used to read
677 * and write data from the card.
678 * @param func: function structure to initialize
679 * @param card: SD card to enable function on
680 * @param num: function number to initialize
681 * @retval 0 function was initialized successfully
682 * @retval -EIO: I/O error
683 */
sdio_init_func(struct sd_card * card,struct sdio_func * func,enum sdio_func_num num)684 int sdio_init_func(struct sd_card *card, struct sdio_func *func,
685 enum sdio_func_num num)
686 {
687 /* Initialize function structure */
688 func->num = num;
689 func->card = card;
690 func->block_size = 0;
691 /* Read function properties from CCCR */
692 return sdio_read_cis(func, cis_tuples, ARRAY_SIZE(cis_tuples));
693 }
694
695
696
697 /**
698 * @brief Enable SDIO function
699 *
700 * Enables SDIO card function. @ref sdio_init_func must be called to
701 * initialized the function structure before enabling it in the card.
702 * @param func: function to enable
703 * @retval 0 function was enabled successfully
704 * @retval -ETIMEDOUT: card I/O timed out
705 * @retval -EIO: I/O error
706 */
sdio_enable_func(struct sdio_func * func)707 int sdio_enable_func(struct sdio_func *func)
708 {
709 int ret;
710 uint8_t reg;
711 uint16_t retries = CONFIG_SD_RETRY_COUNT;
712
713 /* Enable the I/O function */
714 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, SDIO_FUNC_NUM_0,
715 SDIO_CCCR_IO_EN, 0, ®);
716 if (ret) {
717 return ret;
718 }
719 reg |= BIT(func->num);
720 ret = sdio_io_rw_direct(func->card, SDIO_IO_WRITE, SDIO_FUNC_NUM_0,
721 SDIO_CCCR_IO_EN, reg, ®);
722 if (ret) {
723 return ret;
724 }
725 /* Wait for I/O ready to be set */
726 if (func->cis.rdy_timeout) {
727 retries = 1U;
728 }
729 do {
730 /* Timeout is in units of 10ms */
731 sd_delay(((uint32_t)func->cis.rdy_timeout) * 10U);
732 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ,
733 SDIO_FUNC_NUM_0, SDIO_CCCR_IO_RD, 0, ®);
734 if (ret) {
735 return ret;
736 }
737 if (reg & BIT(func->num)) {
738 return 0;
739 }
740 } while (retries-- != 0);
741 return -ETIMEDOUT;
742 }
743
744 /**
745 * @brief Set block size of SDIO function
746 *
747 * Set desired block size for SDIO function, used by block transfers
748 * to SDIO registers.
749 * @param func: function to set block size for
750 * @param bsize: block size
751 * @retval 0 block size was set
752 * @retval -EINVAL: unsupported/invalid block size
753 * @retval -EIO: I/O error
754 */
sdio_set_block_size(struct sdio_func * func,uint16_t bsize)755 int sdio_set_block_size(struct sdio_func *func, uint16_t bsize)
756 {
757 int ret;
758 uint8_t reg;
759
760 if (func->cis.max_blk_size < bsize) {
761 return -EINVAL;
762 }
763 for (int i = 0; i < 2; i++) {
764 reg = (bsize >> (i * 8));
765 ret = sdio_io_rw_direct(func->card, SDIO_IO_WRITE, SDIO_FUNC_NUM_0,
766 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLK_SIZE + i, reg, NULL);
767 if (ret) {
768 return ret;
769 }
770 }
771 func->block_size = bsize;
772 return 0;
773 }
774
775 /**
776 * @brief Read byte from SDIO register
777 *
778 * Reads byte from SDIO register
779 * @param func: function to read from
780 * @param reg: register address to read from
781 * @param val: filled with byte value read from register
782 * @retval 0 read succeeded
783 * @retval -EBUSY: card is busy with another request
784 * @retval -ETIMEDOUT: card read timed out
785 * @retval -EIO: I/O error
786 */
sdio_read_byte(struct sdio_func * func,uint32_t reg,uint8_t * val)787 int sdio_read_byte(struct sdio_func *func, uint32_t reg, uint8_t *val)
788 {
789 int ret;
790
791 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) {
792 LOG_WRN("Card does not support SDIO commands");
793 return -ENOTSUP;
794 }
795 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT));
796 if (ret) {
797 LOG_WRN("Could not get SD card mutex");
798 return -EBUSY;
799 }
800 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, func->num, reg, 0, val);
801 k_mutex_unlock(&func->card->lock);
802 return ret;
803 }
804
805 /**
806 * @brief Write byte to SDIO register
807 *
808 * Writes byte to SDIO register
809 * @param func: function to write to
810 * @param reg: register address to write to
811 * @param write_val: value to write to register
812 * @retval 0 write succeeded
813 * @retval -EBUSY: card is busy with another request
814 * @retval -ETIMEDOUT: card write timed out
815 * @retval -EIO: I/O error
816 */
sdio_write_byte(struct sdio_func * func,uint32_t reg,uint8_t write_val)817 int sdio_write_byte(struct sdio_func *func, uint32_t reg, uint8_t write_val)
818 {
819 int ret;
820
821 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) {
822 LOG_WRN("Card does not support SDIO commands");
823 return -ENOTSUP;
824 }
825 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT));
826 if (ret) {
827 LOG_WRN("Could not get SD card mutex");
828 return -EBUSY;
829 }
830 ret = sdio_io_rw_direct(func->card, SDIO_IO_WRITE, func->num, reg,
831 write_val, NULL);
832 k_mutex_unlock(&func->card->lock);
833 return ret;
834 }
835
836 /**
837 * @brief Write byte to SDIO register, and read result
838 *
839 * Writes byte to SDIO register, and reads the register after write
840 * @param func: function to write to
841 * @param reg: register address to write to
842 * @param write_val: value to write to register
843 * @param read_val: filled with value read from register
844 * @retval 0 write succeeded
845 * @retval -EBUSY: card is busy with another request
846 * @retval -ETIMEDOUT: card write timed out
847 * @retval -EIO: I/O error
848 */
sdio_rw_byte(struct sdio_func * func,uint32_t reg,uint8_t write_val,uint8_t * read_val)849 int sdio_rw_byte(struct sdio_func *func, uint32_t reg, uint8_t write_val,
850 uint8_t *read_val)
851 {
852 int ret;
853
854 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) {
855 LOG_WRN("Card does not support SDIO commands");
856 return -ENOTSUP;
857 }
858 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT));
859 if (ret) {
860 LOG_WRN("Could not get SD card mutex");
861 return -EBUSY;
862 }
863 ret = sdio_io_rw_direct(func->card, SDIO_IO_WRITE, func->num, reg,
864 write_val, read_val);
865 k_mutex_unlock(&func->card->lock);
866 return ret;
867 }
868
869 /**
870 * @brief Read bytes from SDIO fifo
871 *
872 * Reads bytes from SDIO register, treating it as a fifo. Reads will
873 * all be done from same address.
874 * @param func: function to read from
875 * @param reg: register address of fifo
876 * @param data: filled with data read from fifo
877 * @param len: length of data to read from card
878 * @retval 0 read succeeded
879 * @retval -EBUSY: card is busy with another request
880 * @retval -ETIMEDOUT: card read timed out
881 * @retval -EIO: I/O error
882 */
sdio_read_fifo(struct sdio_func * func,uint32_t reg,uint8_t * data,uint32_t len)883 int sdio_read_fifo(struct sdio_func *func, uint32_t reg, uint8_t *data,
884 uint32_t len)
885 {
886 int ret;
887
888 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) {
889 LOG_WRN("Card does not support SDIO commands");
890 return -ENOTSUP;
891 }
892 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT));
893 if (ret) {
894 LOG_WRN("Could not get SD card mutex");
895 return -EBUSY;
896 }
897 ret = sdio_io_rw_extended_helper(func, SDIO_IO_READ, reg, false,
898 data, len);
899 k_mutex_unlock(&func->card->lock);
900 return ret;
901 }
902
903 /**
904 * @brief Write bytes to SDIO fifo
905 *
906 * Writes bytes to SDIO register, treating it as a fifo. Writes will
907 * all be done to same address.
908 * @param func: function to write to
909 * @param reg: register address of fifo
910 * @param data: data to write to fifo
911 * @param len: length of data to write to card
912 * @retval 0 write succeeded
913 * @retval -EBUSY: card is busy with another request
914 * @retval -ETIMEDOUT: card write timed out
915 * @retval -EIO: I/O error
916 */
sdio_write_fifo(struct sdio_func * func,uint32_t reg,uint8_t * data,uint32_t len)917 int sdio_write_fifo(struct sdio_func *func, uint32_t reg, uint8_t *data,
918 uint32_t len)
919 {
920 int ret;
921
922 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) {
923 LOG_WRN("Card does not support SDIO commands");
924 return -ENOTSUP;
925 }
926 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT));
927 if (ret) {
928 LOG_WRN("Could not get SD card mutex");
929 return -EBUSY;
930 }
931 ret = sdio_io_rw_extended_helper(func, SDIO_IO_WRITE, reg, false,
932 data, len);
933 k_mutex_unlock(&func->card->lock);
934 return ret;
935 }
936
937 /**
938 * @brief Read blocks from SDIO fifo
939 *
940 * Reads blocks from SDIO register, treating it as a fifo. Reads will
941 * all be done from same address.
942 * @param func: function to read from
943 * @param reg: register address of fifo
944 * @param data: filled with data read from fifo
945 * @param blocks: number of blocks to read from fifo
946 * @retval 0 read succeeded
947 * @retval -EBUSY: card is busy with another request
948 * @retval -ETIMEDOUT: card read timed out
949 * @retval -EIO: I/O error
950 */
sdio_read_blocks_fifo(struct sdio_func * func,uint32_t reg,uint8_t * data,uint32_t blocks)951 int sdio_read_blocks_fifo(struct sdio_func *func, uint32_t reg, uint8_t *data,
952 uint32_t blocks)
953 {
954 int ret;
955
956 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) {
957 LOG_WRN("Card does not support SDIO commands");
958 return -ENOTSUP;
959 }
960 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT));
961 if (ret) {
962 LOG_WRN("Could not get SD card mutex");
963 return -EBUSY;
964 }
965 ret = sdio_io_rw_extended(func->card, SDIO_IO_READ, func->num, reg,
966 false, data, blocks, func->block_size);
967 k_mutex_unlock(&func->card->lock);
968 return ret;
969 }
970
971 /**
972 * @brief Write blocks to SDIO fifo
973 *
974 * Writes blocks from SDIO register, treating it as a fifo. Writes will
975 * all be done to same address.
976 * @param func: function to write to
977 * @param reg: register address of fifo
978 * @param data: data to write to fifo
979 * @param blocks: number of blocks to write to fifo
980 * @retval 0 write succeeded
981 * @retval -EBUSY: card is busy with another request
982 * @retval -ETIMEDOUT: card write timed out
983 * @retval -EIO: I/O error
984 */
sdio_write_blocks_fifo(struct sdio_func * func,uint32_t reg,uint8_t * data,uint32_t blocks)985 int sdio_write_blocks_fifo(struct sdio_func *func, uint32_t reg, uint8_t *data,
986 uint32_t blocks)
987 {
988 int ret;
989
990 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) {
991 LOG_WRN("Card does not support SDIO commands");
992 return -ENOTSUP;
993 }
994 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT));
995 if (ret) {
996 LOG_WRN("Could not get SD card mutex");
997 return -EBUSY;
998 }
999 ret = sdio_io_rw_extended(func->card, SDIO_IO_WRITE, func->num, reg,
1000 false, data, blocks, func->block_size);
1001 k_mutex_unlock(&func->card->lock);
1002 return ret;
1003 }
1004
1005 /**
1006 * @brief Copy bytes from an SDIO card
1007 *
1008 * Copies bytes from an SDIO card, starting from provided address.
1009 * @param func: function to read from
1010 * @param reg: register address to start copy at
1011 * @param data: buffer to copy data into
1012 * @param len: length of data to read
1013 * @retval 0 read succeeded
1014 * @retval -EBUSY: card is busy with another request
1015 * @retval -ETIMEDOUT: card read timed out
1016 * @retval -EIO: I/O error
1017 */
sdio_read_addr(struct sdio_func * func,uint32_t reg,uint8_t * data,uint32_t len)1018 int sdio_read_addr(struct sdio_func *func, uint32_t reg, uint8_t *data,
1019 uint32_t len)
1020 {
1021 int ret;
1022
1023 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) {
1024 LOG_WRN("Card does not support SDIO commands");
1025 return -ENOTSUP;
1026 }
1027 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT));
1028 if (ret) {
1029 LOG_WRN("Could not get SD card mutex");
1030 return -EBUSY;
1031 }
1032 ret = sdio_io_rw_extended_helper(func, SDIO_IO_READ, reg, true,
1033 data, len);
1034 k_mutex_unlock(&func->card->lock);
1035 return ret;
1036 }
1037
1038 /**
1039 * @brief Copy bytes to an SDIO card
1040 *
1041 * Copies bytes to an SDIO card, starting from provided address.
1042 *
1043 * @param func: function to write to
1044 * @param reg: register address to start copy at
1045 * @param data: buffer to copy data from
1046 * @param len: length of data to write
1047 * @retval 0 write succeeded
1048 * @retval -EBUSY: card is busy with another request
1049 * @retval -ETIMEDOUT: card write timed out
1050 * @retval -EIO: I/O error
1051 */
sdio_write_addr(struct sdio_func * func,uint32_t reg,uint8_t * data,uint32_t len)1052 int sdio_write_addr(struct sdio_func *func, uint32_t reg, uint8_t *data,
1053 uint32_t len)
1054 {
1055 int ret;
1056
1057 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) {
1058 LOG_WRN("Card does not support SDIO commands");
1059 return -ENOTSUP;
1060 }
1061 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT));
1062 if (ret) {
1063 LOG_WRN("Could not get SD card mutex");
1064 return -EBUSY;
1065 }
1066 ret = sdio_io_rw_extended_helper(func, SDIO_IO_WRITE, reg, true,
1067 data, len);
1068 k_mutex_unlock(&func->card->lock);
1069 return ret;
1070 }
1071