Lines Matching full:card

28 static int sdio_send_ocr(struct sd_card *card, uint32_t ocr)  in sdio_send_ocr()  argument
38 /* Send OCR5 to initialize card */ in sdio_send_ocr()
40 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdio_send_ocr()
43 /* Just probing card, likely not SDIO */ in sdio_send_ocr()
49 /* We are probing card, check number of IO functions */ in sdio_send_ocr()
50 card->num_io = (cmd.response[0] & SDIO_OCR_IO_NUMBER) in sdio_send_ocr()
52 if ((card->num_io == 0) || in sdio_send_ocr()
55 /* Card is not an SDIO card */ in sdio_send_ocr()
58 /* Card is not a supported SD device */ in sdio_send_ocr()
61 /* Card has IO present, return zero to in sdio_send_ocr()
62 * indicate SDIO card in sdio_send_ocr()
66 /* Check to see if card is busy with power up */ in sdio_send_ocr()
75 LOG_ERR("Card never left busy state"); in sdio_send_ocr()
79 if (!card->host_props.is_spi) { in sdio_send_ocr()
81 card->ocr = cmd.response[0U]; in sdio_send_ocr()
86 static int sdio_io_rw_direct(struct sd_card *card, in sdio_io_rw_direct() argument
109 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdio_io_rw_direct()
114 if (card->host_props.is_spi) { in sdio_io_rw_direct()
124 static int sdio_io_rw_extended(struct sd_card *card, in sdio_io_rw_extended() argument
156 return sdhc_request(card->sdhc, &cmd, &data); in sdio_io_rw_extended()
178 if ((func->card->cccr_flags & SDIO_SUPPORT_MULTIBLOCK) && in sdio_io_rw_extended_helper()
184 ret = sdio_io_rw_extended(func->card, direction, in sdio_io_rw_extended_helper()
202 ret = sdio_io_rw_extended(func->card, direction, func->num, in sdio_io_rw_extended_helper()
217 * Read card capability register to determine features card supports.
219 static int sdio_read_cccr(struct sd_card *card) in sdio_read_cccr() argument
225 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_read_cccr()
235 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_read_cccr()
240 card->sd_version = (data & SDIO_CCCR_SD_SPEC_MASK) >> SDIO_CCCR_SD_SPEC_SHIFT; in sdio_read_cccr()
242 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_read_cccr()
247 card->cccr_flags = 0; in sdio_read_cccr()
249 card->cccr_flags |= SDIO_SUPPORT_4BIT_LS_BUS; in sdio_read_cccr()
252 card->cccr_flags |= SDIO_SUPPORT_MULTIBLOCK; in sdio_read_cccr()
256 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_read_cccr()
262 card->cccr_flags |= SDIO_SUPPORT_HS; in sdio_read_cccr()
266 (card->flags & SD_1800MV_FLAG)) { in sdio_read_cccr()
268 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_read_cccr()
273 if (sdmmc_host_uhs(&card->host_props)) { in sdio_read_cccr()
275 card->cccr_flags |= SDIO_SUPPORT_SDR50; in sdio_read_cccr()
278 card->cccr_flags |= SDIO_SUPPORT_SDR104; in sdio_read_cccr()
281 card->cccr_flags |= SDIO_SUPPORT_DDR50; in sdio_read_cccr()
285 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_read_cccr()
290 card->switch_caps.sd_drv_type = 0; in sdio_read_cccr()
292 card->switch_caps.sd_drv_type |= SD_DRIVER_TYPE_A; in sdio_read_cccr()
295 card->switch_caps.sd_drv_type |= SD_DRIVER_TYPE_C; in sdio_read_cccr()
298 card->switch_caps.sd_drv_type |= SD_DRIVER_TYPE_D; in sdio_read_cccr()
339 char *data = func->card->card_buffer; in sdio_read_cis()
347 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_read_cis()
357 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_read_cis()
371 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_read_cis()
391 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, in sdio_read_cis()
412 static int sdio_set_bus_width(struct sd_card *card, enum sdhc_bus_width width) in sdio_set_bus_width() argument
417 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_set_bus_width()
436 ret = sdio_io_rw_direct(card, SDIO_IO_WRITE, SDIO_FUNC_NUM_0, in sdio_set_bus_width()
441 /* Card now has changed bus width. Change host bus width */ in sdio_set_bus_width()
442 card->bus_io.bus_width = width; in sdio_set_bus_width()
443 ret = sdhc_set_io(card->sdhc, &card->bus_io); in sdio_set_bus_width()
450 static inline void sdio_select_bus_speed(struct sd_card *card) in sdio_select_bus_speed() argument
452 if (card->host_props.host_caps.sdr104_support && in sdio_select_bus_speed()
453 (card->cccr_flags & SDIO_SUPPORT_SDR104)) { in sdio_select_bus_speed()
454 card->card_speed = SD_TIMING_SDR104; in sdio_select_bus_speed()
455 card->switch_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR; in sdio_select_bus_speed()
456 } else if (card->host_props.host_caps.ddr50_support && in sdio_select_bus_speed()
457 (card->cccr_flags & SDIO_SUPPORT_DDR50)) { in sdio_select_bus_speed()
458 card->card_speed = SD_TIMING_DDR50; in sdio_select_bus_speed()
459 card->switch_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR; in sdio_select_bus_speed()
460 } else if (card->host_props.host_caps.sdr50_support && in sdio_select_bus_speed()
461 (card->cccr_flags & SDIO_SUPPORT_SDR50)) { in sdio_select_bus_speed()
462 card->card_speed = SD_TIMING_SDR50; in sdio_select_bus_speed()
463 card->switch_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR; in sdio_select_bus_speed()
464 } else if (card->host_props.host_caps.high_spd_support && in sdio_select_bus_speed()
465 (card->cccr_flags & SDIO_SUPPORT_HS)) { in sdio_select_bus_speed()
466 card->card_speed = SD_TIMING_HIGH_SPEED; in sdio_select_bus_speed()
467 card->switch_caps.hs_max_dtr = HS_MAX_DTR; in sdio_select_bus_speed()
469 card->card_speed = SD_TIMING_DEFAULT; in sdio_select_bus_speed()
473 /* Applies selected card bus speed to card and host */
474 static int sdio_set_bus_speed(struct sd_card *card) in sdio_set_bus_speed() argument
480 switch (card->card_speed) { in sdio_set_bus_speed()
483 bus_clock = MIN(card->host_props.f_max, card->switch_caps.uhs_max_dtr); in sdio_set_bus_speed()
488 bus_clock = MIN(card->host_props.f_max, card->switch_caps.uhs_max_dtr); in sdio_set_bus_speed()
493 bus_clock = MIN(card->host_props.f_max, card->switch_caps.uhs_max_dtr); in sdio_set_bus_speed()
498 bus_clock = MIN(card->host_props.f_max, card->switch_caps.hs_max_dtr); in sdio_set_bus_speed()
503 bus_clock = MIN(card->host_props.f_max, MHZ(25)); in sdio_set_bus_speed()
512 ret = sdio_io_rw_direct(card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_set_bus_speed()
522 ret = sdio_io_rw_direct(card, SDIO_IO_WRITE, SDIO_FUNC_NUM_0, in sdio_set_bus_speed()
529 /* Don't error out, as card can still work */ in sdio_set_bus_speed()
532 /* Set card bus clock and timing */ in sdio_set_bus_speed()
533 card->bus_io.timing = timing; in sdio_set_bus_speed()
534 card->bus_io.clock = bus_clock; in sdio_set_bus_speed()
535 LOG_DBG("Setting bus clock to: %d", card->bus_io.clock); in sdio_set_bus_speed()
536 ret = sdhc_set_io(card->sdhc, &card->bus_io); in sdio_set_bus_speed()
546 * Initialize an SDIO card for use with subsystem
548 int sdio_card_init(struct sd_card *card) in sdio_card_init() argument
553 /* Probe card with SDIO OCR CM5 */ in sdio_card_init()
554 ret = sdio_send_ocr(card, ocr_arg); in sdio_card_init()
558 /* Card responded to CMD5, type is SDIO */ in sdio_card_init()
559 card->type = CARD_SDIO; in sdio_card_init()
561 if (card->host_props.host_caps.vol_300_support) { in sdio_card_init()
566 card->host_props.host_caps.vol_180_support) { in sdio_card_init()
567 /* See if the card also supports 1.8V */ in sdio_card_init()
570 ret = sdio_send_ocr(card, ocr_arg); in sdio_card_init()
574 if (card->ocr & SD_OCR_SWITCH_18_ACCEPT_FLAG) { in sdio_card_init()
575 LOG_DBG("Card supports 1.8V signalling"); in sdio_card_init()
576 card->flags |= SD_1800MV_FLAG; in sdio_card_init()
579 if (card->ocr & SD_OCR_VDD29_30FLAG) { in sdio_card_init()
580 card->flags |= SD_3000MV_FLAG; in sdio_card_init()
583 if (card->ocr & SDIO_OCR_MEM_PRESENT_FLAG) { in sdio_card_init()
584 card->flags |= SD_MEM_PRESENT_FLAG; in sdio_card_init()
589 * If card and host support 1.8V, perform voltage switch sequence now. in sdio_card_init()
592 if ((card->flags & SD_1800MV_FLAG) && in sdio_card_init()
593 (!card->host_props.is_spi) && in sdio_card_init()
594 (card->host_props.host_caps.vol_180_support) && in sdio_card_init()
596 ret = sdmmc_switch_voltage(card); in sdio_card_init()
599 card->host_props.host_caps.vol_180_support = false; in sdio_card_init()
601 * The host or SD card may have already switched to in sdio_card_init()
605 card->status = CARD_ERROR; in sdio_card_init()
609 if ((card->flags & SD_MEM_PRESENT_FLAG) && in sdio_card_init()
610 ((card->flags & SD_SDHC_FLAG) == 0)) { in sdio_card_init()
611 /* We must send CMD2 to get card cid */ in sdio_card_init()
612 ret = card_read_cid(card); in sdio_card_init()
617 /* Send CMD3 to get card relative address */ in sdio_card_init()
618 ret = sdmmc_request_rca(card); in sdio_card_init()
622 /* Move the card to transfer state (with CMD7) to run in sdio_card_init()
625 ret = sdmmc_select_card(card); in sdio_card_init()
630 /* Read SDIO card common control register */ in sdio_card_init()
631 ret = sdio_read_cccr(card); in sdio_card_init()
635 /* Initialize internal card function 0 structure */ in sdio_card_init()
636 card->func0.num = SDIO_FUNC_NUM_0; in sdio_card_init()
637 card->func0.card = card; in sdio_card_init()
638 ret = sdio_read_cis(&card->func0, cis_tuples, in sdio_card_init()
644 /* If card and host support 4 bit bus, enable it */ in sdio_card_init()
646 ((card->cccr_flags & SDIO_SUPPORT_HS) || in sdio_card_init()
647 (card->cccr_flags & SDIO_SUPPORT_4BIT_LS_BUS))) { in sdio_card_init()
649 ret = sdio_set_bus_width(card, SDHC_BUS_WIDTH4BIT); in sdio_card_init()
653 LOG_DBG("Raised card bus width to 4 bits"); in sdio_card_init()
657 sdio_select_bus_speed(card); in sdio_card_init()
658 ret = sdio_set_bus_speed(card); in sdio_card_init()
662 if (card->card_speed == SD_TIMING_SDR50 || in sdio_card_init()
663 card->card_speed == SD_TIMING_SDR104) { in sdio_card_init()
665 ret = sdhc_execute_tuning(card->sdhc); in sdio_card_init()
676 * Initializes SDIO card function. The card function will not be enabled,
678 * and write data from the card.
680 * @param card: SD card to enable function on
685 int sdio_init_func(struct sd_card *card, struct sdio_func *func, in sdio_init_func() argument
690 func->card = card; in sdio_init_func()
701 * Enables SDIO card function. @ref sdio_init_func must be called to
702 * initialized the function structure before enabling it in the card.
705 * @retval -ETIMEDOUT: card I/O timed out
715 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, SDIO_FUNC_NUM_0, in sdio_enable_func()
721 ret = sdio_io_rw_direct(func->card, SDIO_IO_WRITE, SDIO_FUNC_NUM_0, in sdio_enable_func()
733 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, in sdio_enable_func()
766 ret = sdio_io_rw_direct(func->card, SDIO_IO_WRITE, SDIO_FUNC_NUM_0, in sdio_set_block_size()
784 * @retval -EBUSY: card is busy with another request
785 * @retval -ETIMEDOUT: card read timed out
792 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) { in sdio_read_byte()
793 LOG_WRN("Card does not support SDIO commands"); in sdio_read_byte()
796 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in sdio_read_byte()
798 LOG_WRN("Could not get SD card mutex"); in sdio_read_byte()
801 ret = sdio_io_rw_direct(func->card, SDIO_IO_READ, func->num, reg, 0, val); in sdio_read_byte()
802 k_mutex_unlock(&func->card->lock); in sdio_read_byte()
814 * @retval -EBUSY: card is busy with another request
815 * @retval -ETIMEDOUT: card write timed out
822 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) { in sdio_write_byte()
823 LOG_WRN("Card does not support SDIO commands"); in sdio_write_byte()
826 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in sdio_write_byte()
828 LOG_WRN("Could not get SD card mutex"); in sdio_write_byte()
831 ret = sdio_io_rw_direct(func->card, SDIO_IO_WRITE, func->num, reg, in sdio_write_byte()
833 k_mutex_unlock(&func->card->lock); in sdio_write_byte()
846 * @retval -EBUSY: card is busy with another request
847 * @retval -ETIMEDOUT: card write timed out
855 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) { in sdio_rw_byte()
856 LOG_WRN("Card does not support SDIO commands"); in sdio_rw_byte()
859 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in sdio_rw_byte()
861 LOG_WRN("Could not get SD card mutex"); in sdio_rw_byte()
864 ret = sdio_io_rw_direct(func->card, SDIO_IO_WRITE, func->num, reg, in sdio_rw_byte()
866 k_mutex_unlock(&func->card->lock); in sdio_rw_byte()
878 * @param len: length of data to read from card
880 * @retval -EBUSY: card is busy with another request
881 * @retval -ETIMEDOUT: card read timed out
889 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) { in sdio_read_fifo()
890 LOG_WRN("Card does not support SDIO commands"); in sdio_read_fifo()
893 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in sdio_read_fifo()
895 LOG_WRN("Could not get SD card mutex"); in sdio_read_fifo()
900 k_mutex_unlock(&func->card->lock); in sdio_read_fifo()
912 * @param len: length of data to write to card
914 * @retval -EBUSY: card is busy with another request
915 * @retval -ETIMEDOUT: card write timed out
923 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) { in sdio_write_fifo()
924 LOG_WRN("Card does not support SDIO commands"); in sdio_write_fifo()
927 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in sdio_write_fifo()
929 LOG_WRN("Could not get SD card mutex"); in sdio_write_fifo()
934 k_mutex_unlock(&func->card->lock); in sdio_write_fifo()
948 * @retval -EBUSY: card is busy with another request
949 * @retval -ETIMEDOUT: card read timed out
957 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) { in sdio_read_blocks_fifo()
958 LOG_WRN("Card does not support SDIO commands"); in sdio_read_blocks_fifo()
961 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in sdio_read_blocks_fifo()
963 LOG_WRN("Could not get SD card mutex"); in sdio_read_blocks_fifo()
966 ret = sdio_io_rw_extended(func->card, SDIO_IO_READ, func->num, reg, in sdio_read_blocks_fifo()
968 k_mutex_unlock(&func->card->lock); in sdio_read_blocks_fifo()
982 * @retval -EBUSY: card is busy with another request
983 * @retval -ETIMEDOUT: card write timed out
991 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) { in sdio_write_blocks_fifo()
992 LOG_WRN("Card does not support SDIO commands"); in sdio_write_blocks_fifo()
995 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in sdio_write_blocks_fifo()
997 LOG_WRN("Could not get SD card mutex"); in sdio_write_blocks_fifo()
1000 ret = sdio_io_rw_extended(func->card, SDIO_IO_WRITE, func->num, reg, in sdio_write_blocks_fifo()
1002 k_mutex_unlock(&func->card->lock); in sdio_write_blocks_fifo()
1007 * @brief Copy bytes from an SDIO card
1009 * Copies bytes from an SDIO card, starting from provided address.
1015 * @retval -EBUSY: card is busy with another request
1016 * @retval -ETIMEDOUT: card read timed out
1024 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) { in sdio_read_addr()
1025 LOG_WRN("Card does not support SDIO commands"); in sdio_read_addr()
1028 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in sdio_read_addr()
1030 LOG_WRN("Could not get SD card mutex"); in sdio_read_addr()
1035 k_mutex_unlock(&func->card->lock); in sdio_read_addr()
1040 * @brief Copy bytes to an SDIO card
1042 * Copies bytes to an SDIO card, starting from provided address.
1049 * @retval -EBUSY: card is busy with another request
1050 * @retval -ETIMEDOUT: card write timed out
1058 if ((func->card->type != CARD_SDIO) && (func->card->type != CARD_COMBO)) { in sdio_write_addr()
1059 LOG_WRN("Card does not support SDIO commands"); in sdio_write_addr()
1062 ret = k_mutex_lock(&func->card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in sdio_write_addr()
1064 LOG_WRN("Could not get SD card mutex"); in sdio_write_addr()
1069 k_mutex_unlock(&func->card->lock); in sdio_write_addr()