Lines Matching full:card

63 static int sdmmc_app_command(struct sd_card *card, int relative_card_address)  in sdmmc_app_command()  argument
65 return card_app_command(card, relative_card_address); in sdmmc_app_command()
68 /* Reads OCR from SPI mode card using CMD58 */
69 static int sdmmc_spi_send_ocr(struct sd_card *card, uint32_t arg) in sdmmc_spi_send_ocr() argument
80 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_spi_send_ocr()
87 card->ocr = cmd.response[1]; in sdmmc_spi_send_ocr()
88 if (card->ocr == 0) { in sdmmc_spi_send_ocr()
96 /* Sends OCR to card using ACMD41 */
97 static int sdmmc_send_ocr(struct sd_card *card, int ocr) in sdmmc_send_ocr() argument
111 ret = sdmmc_app_command(card, 0U); in sdmmc_send_ocr()
118 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_send_ocr()
124 /* Just probing, don't wait for card to exit busy state */ in sdmmc_send_ocr()
128 * Check to see if card is busy with power up. PWR_BUSY in sdmmc_send_ocr()
129 * flag will be cleared when card finishes power up sequence in sdmmc_send_ocr()
131 if (card->host_props.is_spi) { in sdmmc_send_ocr()
144 LOG_ERR("Card never left busy state"); in sdmmc_send_ocr()
148 if (!card->host_props.is_spi) { in sdmmc_send_ocr()
150 card->ocr = cmd.response[0U]; in sdmmc_send_ocr()
156 static int sdmmc_read_scr(struct sd_card *card) in sdmmc_read_scr() argument
163 /* DMA onto stack is unsafe, so we use an internal card buffer */ in sdmmc_read_scr()
164 uint32_t *scr = (uint32_t *)card->card_buffer; in sdmmc_read_scr()
167 ret = sdmmc_app_command(card, card->relative_addr); in sdmmc_read_scr()
184 ret = sdhc_request(card->sdhc, &cmd, &data); in sdmmc_read_scr()
192 sdmmc_decode_scr(&card_scr, raw_scr, &card->sd_version); in sdmmc_read_scr()
193 LOG_DBG("SD reports specification version %d", card->sd_version); in sdmmc_read_scr()
194 /* Check card supported bus width */ in sdmmc_read_scr()
196 card->flags |= SD_4BITS_WIDTH; in sdmmc_read_scr()
198 /* Check if card supports speed class command (CMD20) */ in sdmmc_read_scr()
200 card->flags |= SD_SPEED_CLASS_CONTROL_FLAG; in sdmmc_read_scr()
204 card->flags |= SD_CMD23_FLAG; in sdmmc_read_scr()
209 /* Sets block length of SD card */
210 static int sdmmc_set_blocklen(struct sd_card *card, uint32_t block_len) in sdmmc_set_blocklen() argument
220 return sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_set_blocklen()
224 * Sets bus width of host and card, following section 3.4 of
227 static int sdmmc_set_bus_width(struct sd_card *card, enum sdhc_bus_width width) in sdmmc_set_bus_width() argument
233 * The specification strictly requires card interrupts to be masked, but in sdmmc_set_bus_width()
237 ret = sdmmc_app_command(card, card->relative_addr); in sdmmc_set_bus_width()
259 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_set_bus_width()
269 /* Card now has changed bus width. Change host bus width */ in sdmmc_set_bus_width()
270 card->bus_io.bus_width = width; in sdmmc_set_bus_width()
271 ret = sdhc_set_io(card->sdhc, &card->bus_io); in sdmmc_set_bus_width()
285 static int sdmmc_switch(struct sd_card *card, enum sd_switch_arg mode, enum sd_group_num group, in sdmmc_switch() argument
304 return sdhc_request(card->sdhc, &cmd, &data); in sdmmc_switch()
307 static int sdmmc_read_switch(struct sd_card *card) in sdmmc_read_switch() argument
312 if (card->sd_version < SD_SPEC_VER1_1) { in sdmmc_read_switch()
317 /* Use card internal buffer to read 64 byte switch data */ in sdmmc_read_switch()
318 status = card->card_buffer; in sdmmc_read_switch()
320 * Setting switch to zero will read card's support values, in sdmmc_read_switch()
323 ret = sdmmc_switch(card, SD_SWITCH_CHECK, 0, 0, status); in sdmmc_read_switch()
333 * number n on the card. (So 0x3 indicates support for functions 0 and 1) in sdmmc_read_switch()
337 card->switch_caps.hs_max_dtr = HS_MAX_DTR; in sdmmc_read_switch()
339 card->switch_caps.hs_max_dtr = HS_UNSUPPORTED; in sdmmc_read_switch()
343 card->switch_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR; in sdmmc_read_switch()
345 card->switch_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR; in sdmmc_read_switch()
347 card->switch_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR; in sdmmc_read_switch()
349 card->switch_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR; in sdmmc_read_switch()
351 card->switch_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR; in sdmmc_read_switch()
353 card->switch_caps.uhs_max_dtr = UHS_UNSUPPORTED; in sdmmc_read_switch()
355 if (card->sd_version >= SD_SPEC_VER3_0) { in sdmmc_read_switch()
356 card->switch_caps.bus_speed = status[13]; in sdmmc_read_switch()
357 card->switch_caps.sd_drv_type = status[9]; in sdmmc_read_switch()
358 card->switch_caps.sd_current_limit = status[7]; in sdmmc_read_switch()
363 static inline void sdmmc_select_bus_speed(struct sd_card *card) in sdmmc_select_bus_speed() argument
369 if ((card->flags & SD_1800MV_FLAG) && sdmmc_host_uhs(&card->host_props) && in sdmmc_select_bus_speed()
370 !(card->host_props.is_spi) && IS_ENABLED(CONFIG_SD_UHS_PROTOCOL)) { in sdmmc_select_bus_speed()
372 if (card->host_props.host_caps.sdr104_support && in sdmmc_select_bus_speed()
373 (card->switch_caps.bus_speed & UHS_SDR104_BUS_SPEED)) { in sdmmc_select_bus_speed()
374 card->card_speed = SD_TIMING_SDR104; in sdmmc_select_bus_speed()
375 } else if (card->host_props.host_caps.ddr50_support && in sdmmc_select_bus_speed()
376 (card->switch_caps.bus_speed & UHS_DDR50_BUS_SPEED)) { in sdmmc_select_bus_speed()
377 card->card_speed = SD_TIMING_DDR50; in sdmmc_select_bus_speed()
378 } else if (card->host_props.host_caps.sdr50_support && in sdmmc_select_bus_speed()
379 (card->switch_caps.bus_speed & UHS_SDR50_BUS_SPEED)) { in sdmmc_select_bus_speed()
380 card->card_speed = SD_TIMING_SDR50; in sdmmc_select_bus_speed()
381 } else if (card->switch_caps.bus_speed & UHS_SDR12_BUS_SPEED) { in sdmmc_select_bus_speed()
382 card->card_speed = SD_TIMING_SDR25; in sdmmc_select_bus_speed()
384 card->card_speed = SD_TIMING_SDR12; in sdmmc_select_bus_speed()
388 if (card->host_props.host_caps.high_spd_support && in sdmmc_select_bus_speed()
389 (card->switch_caps.bus_speed & HIGH_SPEED_BUS_SPEED)) { in sdmmc_select_bus_speed()
390 card->card_speed = SD_TIMING_HIGH_SPEED; in sdmmc_select_bus_speed()
392 card->card_speed = SD_TIMING_DEFAULT; in sdmmc_select_bus_speed()
397 /* Selects driver type for SD card */
398 static int sdmmc_select_driver_type(struct sd_card *card) in sdmmc_select_driver_type() argument
401 uint8_t *status = card->card_buffer; in sdmmc_select_driver_type()
407 if (card->host_props.host_caps.drv_type_c_support && in sdmmc_select_driver_type()
408 (card->switch_caps.sd_drv_type & SD_DRIVER_TYPE_C)) { in sdmmc_select_driver_type()
409 card->bus_io.driver_type = SD_DRIVER_TYPE_C; in sdmmc_select_driver_type()
411 ret = sdmmc_switch(card, SD_SWITCH_SET, SD_GRP_DRIVER_STRENGTH_MODE, in sdmmc_select_driver_type()
417 /* Sets current limit for SD card */
418 static int sdmmc_set_current_limit(struct sd_card *card) in sdmmc_set_current_limit() argument
422 uint8_t *status = card->card_buffer; in sdmmc_set_current_limit()
424 if ((card->card_speed != SD_TIMING_SDR50) && (card->card_speed != SD_TIMING_SDR104) && in sdmmc_set_current_limit()
425 (card->card_speed != SD_TIMING_DDR50)) { in sdmmc_set_current_limit()
427 } else if (card->host_props.max_current_180 >= 800 && in sdmmc_set_current_limit()
428 (card->switch_caps.sd_current_limit & SD_MAX_CURRENT_800MA)) { in sdmmc_set_current_limit()
430 } else if (card->host_props.max_current_180 >= 600 && in sdmmc_set_current_limit()
431 (card->switch_caps.sd_current_limit & SD_MAX_CURRENT_600MA)) { in sdmmc_set_current_limit()
433 } else if (card->host_props.max_current_180 >= 400 && in sdmmc_set_current_limit()
434 (card->switch_caps.sd_current_limit & SD_MAX_CURRENT_400MA)) { in sdmmc_set_current_limit()
436 } else if (card->host_props.max_current_180 >= 200 && in sdmmc_set_current_limit()
437 (card->switch_caps.sd_current_limit & SD_MAX_CURRENT_200MA)) { in sdmmc_set_current_limit()
443 ret = sdmmc_switch(card, SD_SWITCH_SET, SD_GRP_CURRENT_LIMIT_MODE, max_current, in sdmmc_set_current_limit()
450 /* Status response indicates card did not select request limit */ in sdmmc_set_current_limit()
451 LOG_WRN("Card did not accept current limit"); in sdmmc_set_current_limit()
457 /* Applies selected card bus speed to card and host */
458 static int sdmmc_set_bus_speed(struct sd_card *card) in sdmmc_set_bus_speed() argument
461 uint8_t *status = card->card_buffer; in sdmmc_set_bus_speed()
465 /* Set card clock and host timing. Since the card's maximum clock in sdmmc_set_bus_speed()
469 if ((card->flags & SD_1800MV_FLAG) && sdmmc_host_uhs(&card->host_props) && in sdmmc_set_bus_speed()
470 !(card->host_props.is_spi) && IS_ENABLED(CONFIG_SD_UHS_PROTOCOL)) { in sdmmc_set_bus_speed()
472 card_clock = MIN(card->host_props.f_max, card->switch_caps.uhs_max_dtr); in sdmmc_set_bus_speed()
473 switch (card->card_speed) { in sdmmc_set_bus_speed()
495 card_clock = MIN(card->host_props.f_max, card->switch_caps.hs_max_dtr); in sdmmc_set_bus_speed()
496 switch (card->card_speed) { in sdmmc_set_bus_speed()
510 ret = sdmmc_switch(card, SD_SWITCH_SET, SD_GRP_TIMING_MODE, card->card_speed, status); in sdmmc_set_bus_speed()
512 LOG_DBG("Failed to switch SD card speed"); in sdmmc_set_bus_speed()
515 if ((status[16] & 0xF) != card->card_speed) { in sdmmc_set_bus_speed()
516 LOG_WRN("Card did not accept new speed"); in sdmmc_set_bus_speed()
519 card->bus_io.timing = timing; in sdmmc_set_bus_speed()
520 card->bus_io.clock = card_clock; in sdmmc_set_bus_speed()
521 LOG_DBG("Setting bus clock to: %d", card->bus_io.clock); in sdmmc_set_bus_speed()
522 ret = sdhc_set_io(card->sdhc, &card->bus_io); in sdmmc_set_bus_speed()
532 * Init UHS capable SD card. Follows figure 3-16 in physical layer specification.
534 static int sdmmc_init_uhs(struct sd_card *card) in sdmmc_init_uhs() argument
539 ret = sdmmc_set_bus_width(card, SDHC_BUS_WIDTH4BIT); in sdmmc_init_uhs()
541 LOG_ERR("Failed to change card bus width to 4 bits"); in sdmmc_init_uhs()
545 /* Select bus speed for card depending on host and card capability*/ in sdmmc_init_uhs()
546 sdmmc_select_bus_speed(card); in sdmmc_init_uhs()
547 /* Now, set the driver strength for the card */ in sdmmc_init_uhs()
548 ret = sdmmc_select_driver_type(card); in sdmmc_init_uhs()
553 ret = sdmmc_set_current_limit(card); in sdmmc_init_uhs()
555 LOG_DBG("Failed to set card current limit"); in sdmmc_init_uhs()
559 ret = sdmmc_set_bus_speed(card); in sdmmc_init_uhs()
561 LOG_DBG("Failed to set card bus speed"); in sdmmc_init_uhs()
564 if (card->card_speed == SD_TIMING_SDR50 || card->card_speed == SD_TIMING_SDR104 || in sdmmc_init_uhs()
565 card->card_speed == SD_TIMING_DDR50) { in sdmmc_init_uhs()
567 ret = sdhc_execute_tuning(card->sdhc); in sdmmc_init_uhs()
576 static int sdmmc_init_hs(struct sd_card *card) in sdmmc_init_hs() argument
580 if ((!card->host_props.host_caps.high_spd_support) || in sdmmc_init_hs()
581 (card->sd_version < SD_SPEC_VER1_1) || in sdmmc_init_hs()
582 (card->switch_caps.hs_max_dtr == HS_UNSUPPORTED)) { in sdmmc_init_hs()
583 /* No high speed support. Leave card untouched */ in sdmmc_init_hs()
586 /* Select bus speed for card depending on host and card capability*/ in sdmmc_init_hs()
587 sdmmc_select_bus_speed(card); in sdmmc_init_hs()
589 ret = sdmmc_set_bus_speed(card); in sdmmc_init_hs()
591 LOG_ERR("Failed to switch card to HS mode"); in sdmmc_init_hs()
594 if (card->flags & SD_4BITS_WIDTH) { in sdmmc_init_hs()
596 ret = sdmmc_set_bus_width(card, SDHC_BUS_WIDTH4BIT); in sdmmc_init_hs()
598 LOG_ERR("Failed to change card bus width to 4 bits"); in sdmmc_init_hs()
606 * Initializes SDMMC card. Note that the common SD function has already
607 * sent CMD0 and CMD8 to the card at function entry.
609 int sdmmc_card_init(struct sd_card *card) in sdmmc_card_init() argument
615 if (card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) { in sdmmc_card_init()
616 /* Probe SPI card with CMD58*/ in sdmmc_card_init()
617 ret = sdmmc_spi_send_ocr(card, ocr_arg); in sdmmc_card_init()
619 /* Probe Native card with ACMD41 */ in sdmmc_card_init()
620 ret = sdmmc_send_ocr(card, ocr_arg); in sdmmc_card_init()
627 /* Card responded to ACMD41, type is SDMMC */ in sdmmc_card_init()
628 card->type = CARD_SDMMC; in sdmmc_card_init()
630 if (card->flags & SD_SDHC_FLAG) { in sdmmc_card_init()
632 /* High capacity card. See if host supports 1.8V */ in sdmmc_card_init()
633 if (card->host_props.host_caps.vol_180_support) { in sdmmc_card_init()
642 if (card->host_props.host_caps.vol_300_support) { in sdmmc_card_init()
652 /* Send SD OCR to card to initialize it */ in sdmmc_card_init()
653 ret = sdmmc_send_ocr(card, ocr_arg); in sdmmc_card_init()
655 LOG_ERR("Failed to query card OCR"); in sdmmc_card_init()
658 if (card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) { in sdmmc_card_init()
660 ret = sdmmc_spi_send_ocr(card, ocr_arg); in sdmmc_card_init()
666 if (card->ocr & SD_OCR_CARD_CAP_FLAG) { in sdmmc_card_init()
667 card->flags |= SD_HIGH_CAPACITY_FLAG; in sdmmc_card_init()
669 if (card->ocr & SD_OCR_SWITCH_18_ACCEPT_FLAG) { in sdmmc_card_init()
670 LOG_DBG("Card supports 1.8V signaling"); in sdmmc_card_init()
671 card->flags |= SD_1800MV_FLAG; in sdmmc_card_init()
674 if (card->ocr & SD_OCR_VDD29_30FLAG) { in sdmmc_card_init()
675 card->flags |= SD_3000MV_FLAG; in sdmmc_card_init()
678 * If card is high capacity (SDXC or SDHC), and supports 1.8V signaling, in sdmmc_card_init()
682 if ((card->flags & SD_1800MV_FLAG) && (card->host_props.host_caps.vol_180_support) && in sdmmc_card_init()
683 (!card->host_props.is_spi) && IS_ENABLED(CONFIG_SD_UHS_PROTOCOL)) { in sdmmc_card_init()
684 ret = sdmmc_switch_voltage(card); in sdmmc_card_init()
687 card->host_props.host_caps.vol_180_support = false; in sdmmc_card_init()
689 * The host or SD card may have already switched to in sdmmc_card_init()
693 card->status = CARD_ERROR; in sdmmc_card_init()
697 /* Read the card's CID (card identification register) */ in sdmmc_card_init()
698 ret = card_read_cid(card); in sdmmc_card_init()
702 if (!card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) { in sdmmc_card_init()
704 * Request new relative card address. This moves the card from in sdmmc_card_init()
707 ret = sdmmc_request_rca(card); in sdmmc_card_init()
712 /* Card has entered data transfer mode. Get card specific data register */ in sdmmc_card_init()
713 ret = sdmmc_read_csd(card); in sdmmc_card_init()
717 if (!card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) { in sdmmc_card_init()
718 /* Move the card to transfer state (with CMD7) to run remaining commands */ in sdmmc_card_init()
719 ret = sdmmc_select_card(card); in sdmmc_card_init()
725 * With card in data transfer state, we can set SD clock to maximum in sdmmc_card_init()
728 if (card->host_props.f_max < SD_CLOCK_25MHZ) { in sdmmc_card_init()
730 card->host_props.f_max); in sdmmc_card_init()
731 card->bus_io.clock = card->host_props.f_max; in sdmmc_card_init()
733 card->bus_io.clock = SD_CLOCK_25MHZ; in sdmmc_card_init()
735 ret = sdhc_set_io(card->sdhc, &card->bus_io); in sdmmc_card_init()
743 ret = sdmmc_read_scr(card); in sdmmc_card_init()
747 /* Read switch capabilities to determine what speeds card supports */ in sdmmc_card_init()
748 if (!card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) { in sdmmc_card_init()
749 ret = sdmmc_read_switch(card); in sdmmc_card_init()
751 LOG_ERR("Failed to read card functions"); in sdmmc_card_init()
755 if ((card->flags & SD_1800MV_FLAG) && sdmmc_host_uhs(&card->host_props) && in sdmmc_card_init()
756 !(card->host_props.is_spi) && IS_ENABLED(CONFIG_SD_UHS_PROTOCOL)) { in sdmmc_card_init()
757 ret = sdmmc_init_uhs(card); in sdmmc_card_init()
759 LOG_ERR("UHS card init failed"); in sdmmc_card_init()
762 if ((card->flags & SD_HIGH_CAPACITY_FLAG) == 0) { in sdmmc_card_init()
763 /* Standard capacity SDSC card. set block length to 512 */ in sdmmc_card_init()
764 ret = sdmmc_set_blocklen(card, SDMMC_DEFAULT_BLOCK_SIZE); in sdmmc_card_init()
769 card->block_size = 512; in sdmmc_card_init()
771 /* Card is not UHS. Try to use high speed mode */ in sdmmc_card_init()
772 ret = sdmmc_init_hs(card); in sdmmc_card_init()
774 LOG_ERR("HS card init failed"); in sdmmc_card_init()
780 int sdmmc_ioctl(struct sd_card *card, uint8_t cmd, void *buf) in sdmmc_ioctl() argument
782 return card_ioctl(card, cmd, buf); in sdmmc_ioctl()
785 int sdmmc_read_blocks(struct sd_card *card, uint8_t *rbuf, uint32_t start_block, in sdmmc_read_blocks() argument
788 return card_read_blocks(card, rbuf, start_block, num_blocks); in sdmmc_read_blocks()
791 int sdmmc_write_blocks(struct sd_card *card, const uint8_t *wbuf, uint32_t start_block, in sdmmc_write_blocks() argument
794 return card_write_blocks(card, wbuf, start_block, num_blocks); in sdmmc_write_blocks()