Lines Matching full:card

19 /* Read card status. Return 0 if card is inactive */
20 int sdmmc_read_status(struct sd_card *card) in sdmmc_read_status() argument
27 if (!card->host_props.is_spi) { in sdmmc_read_status()
28 cmd.arg = (card->relative_addr << 16U); in sdmmc_read_status()
34 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_read_status()
38 if (card->host_props.is_spi) { in sdmmc_read_status()
55 /* Otherwise, check native card response */ in sdmmc_read_status()
60 /* Valid response, the card is busy */ in sdmmc_read_status()
64 /* Waits for SD card to be ready for data. Returns 0 if card is ready */
65 int sdmmc_wait_ready(struct sd_card *card) in sdmmc_wait_ready() argument
70 if (!sdhc_card_busy(card->sdhc)) { in sdmmc_wait_ready()
71 /* Check card status */ in sdmmc_wait_ready()
72 ret = sd_retry(sdmmc_read_status, card, CONFIG_SD_RETRY_COUNT); in sdmmc_wait_ready()
127 /* Get card total block count and block size. */ in sdmmc_decode_csd()
204 /* Reads card id/csd register (in SPI mode) */
205 static int sdmmc_spi_read_cxd(struct sd_card *card, uint32_t opcode, uint32_t *cxd) in sdmmc_spi_read_cxd() argument
210 /* Use internal card buffer for data transfer */ in sdmmc_spi_read_cxd()
211 uint32_t *cxd_be = (uint32_t *)card->card_buffer; in sdmmc_spi_read_cxd()
226 ret = sdhc_request(card->sdhc, &cmd, &data); in sdmmc_spi_read_cxd()
237 /* Reads card id/csd register (native SD mode */
238 static int sdmmc_read_cxd(struct sd_card *card, uint32_t opcode, uint32_t rca, uint32_t *cxd) in sdmmc_read_cxd() argument
249 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_read_cxd()
259 /* Read card specific data register */
260 int sdmmc_read_csd(struct sd_card *card) in sdmmc_read_csd() argument
267 if (card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) { in sdmmc_read_csd()
268 ret = sdmmc_spi_read_cxd(card, SD_SEND_CSD, csd); in sdmmc_read_csd()
270 ret = sdmmc_read_cxd(card, SD_SEND_CSD, card->relative_addr, csd); in sdmmc_read_csd()
278 sdmmc_decode_csd(&card_csd, csd, &card->block_count, &card->block_size); in sdmmc_read_csd()
279 LOG_DBG("Card block count %d, block size %d", card->block_count, card->block_size); in sdmmc_read_csd()
283 /* Reads card identification register, and decodes it */
284 int card_read_cid(struct sd_card *card) in card_read_cid() argument
293 if (card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) { in card_read_cid()
294 ret = sdmmc_spi_read_cxd(card, SD_SEND_CID, cid); in card_read_cid()
296 ret = sdmmc_read_cxd(card, SD_ALL_SEND_CID, 0, cid); in card_read_cid()
306 if (card->type == CARD_MMC) { in card_read_cid()
314 LOG_DBG("Card MID: 0x%x, OID: %c%c", card_cid.manufacturer, in card_read_cid()
325 int sdmmc_switch_voltage(struct sd_card *card) in sdmmc_switch_voltage() argument
330 /* Check to make sure card supports 1.8V */ in sdmmc_switch_voltage()
331 if (!(card->flags & SD_1800MV_FLAG)) { in sdmmc_switch_voltage()
333 LOG_WRN("SD card reports as SDHC/SDXC, but does not support 1.8V"); in sdmmc_switch_voltage()
342 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_switch_voltage()
354 * Card should drive CMD and DAT[3:0] signals low at the next clock in sdmmc_switch_voltage()
358 if (!(sdhc_card_busy(card->sdhc))) { in sdmmc_switch_voltage()
359 /* Delay 1ms to allow card to drive lines low */ in sdmmc_switch_voltage()
361 if (!sdhc_card_busy(card->sdhc)) { in sdmmc_switch_voltage()
362 /* Card did not drive CMD and DAT lines low */ in sdmmc_switch_voltage()
363 LOG_DBG("Card did not drive DAT lines low"); in sdmmc_switch_voltage()
371 sd_clock = card->bus_io.clock; in sdmmc_switch_voltage()
372 card->bus_io.clock = 0; in sdmmc_switch_voltage()
373 ret = sdhc_set_io(card->sdhc, &card->bus_io); in sdmmc_switch_voltage()
379 card->bus_io.signal_voltage = SD_VOL_1_8_V; in sdmmc_switch_voltage()
380 ret = sdhc_set_io(card->sdhc, &card->bus_io); in sdmmc_switch_voltage()
387 card->bus_io.clock = sd_clock; in sdmmc_switch_voltage()
388 ret = sdhc_set_io(card->sdhc, &card->bus_io); in sdmmc_switch_voltage()
398 if (sdhc_card_busy(card->sdhc)) { in sdmmc_switch_voltage()
399 LOG_DBG("Card failed to switch voltages"); in sdmmc_switch_voltage()
402 card->card_voltage = SD_VOL_1_8_V; in sdmmc_switch_voltage()
403 LOG_INF("Card switched to 1.8V signaling"); in sdmmc_switch_voltage()
408 * Requests card to publish a new relative card address, and move from
411 int sdmmc_request_rca(struct sd_card *card) in sdmmc_request_rca() argument
421 /* Issue CMD3 until card responds with nonzero RCA */ in sdmmc_request_rca()
423 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_request_rca()
428 /* Card RCA is in upper 16 bits of response */ in sdmmc_request_rca()
429 card->relative_addr = ((cmd.response[0U] & 0xFFFF0000) >> 16U); in sdmmc_request_rca()
430 } while (card->relative_addr == 0U); in sdmmc_request_rca()
431 LOG_DBG("Card relative addr: %d", card->relative_addr); in sdmmc_request_rca()
436 * Selects card, moving it into data transfer mode
438 int sdmmc_select_card(struct sd_card *card) in sdmmc_select_card() argument
444 cmd.arg = ((card->relative_addr) << 16U); in sdmmc_select_card()
449 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_select_card()
463 int card_app_command(struct sd_card *card, int relative_card_address) in card_app_command() argument
473 ret = sdhc_request(card->sdhc, &cmd, NULL); in card_app_command()
483 /* Check application command flag to determine if card is ready for APP CMD */ in card_app_command()
484 if ((!card->host_props.is_spi) && !(cmd.response[0U] & SD_R1_APP_CMD)) { in card_app_command()
485 /* Command succeeded, but card not ready for app command. No APP CMD support */ in card_app_command()
491 static int card_read(struct sd_card *card, uint8_t *rbuf, uint32_t start_block, uint32_t num_blocks) in card_read() argument
510 if (!(card->flags & SD_HIGH_CAPACITY_FLAG)) { in card_read()
512 cmd.arg = start_block * card->block_size; in card_read()
521 data.block_size = card->block_size; in card_read()
528 ret = sdhc_request(card->sdhc, &cmd, &data); in card_read()
534 /* Verify card is back in transfer state after read */ in card_read()
535 ret = sdmmc_wait_ready(card); in card_read()
537 LOG_ERR("Card did not return to ready state"); in card_read()
543 /* Reads data from SD card memory card */
544 int card_read_blocks(struct sd_card *card, uint8_t *rbuf, uint32_t start_block, uint32_t num_blocks) in card_read_blocks() argument
551 if ((start_block + num_blocks) > card->block_count) { in card_read_blocks()
554 if (card->type == CARD_SDIO) { in card_read_blocks()
558 ret = k_mutex_lock(&card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in card_read_blocks()
560 LOG_WRN("Could not get SD card mutex"); in card_read_blocks()
566 * directly. Otherwise, we need to use the card's internal buffer in card_read_blocks()
571 LOG_DBG("Unaligned buffer access to SD card may incur performance penalty"); in card_read_blocks()
572 if (sizeof(card->card_buffer) < card->block_size) { in card_read_blocks()
573 LOG_ERR("Card buffer size needs to be increased for " in card_read_blocks()
575 k_mutex_unlock(&card->lock); in card_read_blocks()
578 rlen = sizeof(card->card_buffer) / card->block_size; in card_read_blocks()
582 /* Read from disk to card buffer */ in card_read_blocks()
583 ret = card_read(card, card->card_buffer, sector + start_block, rlen); in card_read_blocks()
586 k_mutex_unlock(&card->lock); in card_read_blocks()
589 /* Copy data from card buffer */ in card_read_blocks()
590 memcpy(buf_offset, card->card_buffer, rlen * card->block_size); in card_read_blocks()
593 buf_offset += rlen * card->block_size; in card_read_blocks()
597 ret = card_read(card, rbuf, start_block, num_blocks); in card_read_blocks()
599 LOG_ERR("Card read failed"); in card_read_blocks()
600 k_mutex_unlock(&card->lock); in card_read_blocks()
604 k_mutex_unlock(&card->lock); in card_read_blocks()
610 * to a card
612 static int card_query_written(struct sd_card *card, uint32_t *num_written) in card_query_written() argument
617 uint32_t *blocks = (uint32_t *)card->card_buffer; in card_query_written()
619 ret = card_app_command(card, card->relative_addr); in card_query_written()
637 ret = sdhc_request(card->sdhc, &cmd, &data); in card_query_written()
653 static int card_write(struct sd_card *card, const uint8_t *wbuf, uint32_t start_block, in card_write() argument
666 if (!(card->flags & SD_HIGH_CAPACITY_FLAG)) { in card_write()
668 cmd.arg = start_block * card->block_size; in card_write()
677 data.block_size = card->block_size; in card_write()
684 ret = sdhc_request(card->sdhc, &cmd, &data); in card_write()
687 ret = sdmmc_wait_ready(card); in card_write()
691 /* Query card to see how many blocks were actually written */ in card_write()
692 ret = card_query_written(card, &blocks); in card_write()
699 /* Verify card is back in transfer state after write */ in card_write()
700 ret = sdmmc_wait_ready(card); in card_write()
702 LOG_ERR("Card did not return to ready state"); in card_write()
708 /* Writes data to SD card memory card */
709 int card_write_blocks(struct sd_card *card, const uint8_t *wbuf, uint32_t start_block, in card_write_blocks() argument
717 if ((start_block + num_blocks) > card->block_count) { in card_write_blocks()
720 if (card->type == CARD_SDIO) { in card_write_blocks()
724 ret = k_mutex_lock(&card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in card_write_blocks()
726 LOG_WRN("Could not get SD card mutex"); in card_write_blocks()
731 * directly. Otherwise, we need to use the card's internal buffer in card_write_blocks()
736 LOG_DBG("Unaligned buffer access to SD card may incur performance penalty"); in card_write_blocks()
737 if (sizeof(card->card_buffer) < card->block_size) { in card_write_blocks()
738 LOG_ERR("Card buffer size needs to be increased for " in card_write_blocks()
740 k_mutex_unlock(&card->lock); in card_write_blocks()
743 wlen = sizeof(card->card_buffer) / card->block_size; in card_write_blocks()
747 /* Copy data into card buffer */ in card_write_blocks()
748 memcpy(card->card_buffer, buf_offset, wlen * card->block_size); in card_write_blocks()
749 /* Write card buffer to disk */ in card_write_blocks()
750 ret = card_write(card, card->card_buffer, sector + start_block, wlen); in card_write_blocks()
753 k_mutex_unlock(&card->lock); in card_write_blocks()
758 buf_offset += wlen * card->block_size; in card_write_blocks()
762 ret = card_write(card, wbuf, start_block, num_blocks); in card_write_blocks()
765 k_mutex_unlock(&card->lock); in card_write_blocks()
769 k_mutex_unlock(&card->lock); in card_write_blocks()
774 int card_ioctl(struct sd_card *card, uint8_t cmd, void *buf) in card_ioctl() argument
778 ret = k_mutex_lock(&card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in card_ioctl()
780 LOG_WRN("Could not get SD card mutex"); in card_ioctl()
785 (*(uint32_t *)buf) = card->block_count; in card_ioctl()
789 (*(uint32_t *)buf) = card->block_size; in card_ioctl()
792 /* Ensure card is not busy with data write. in card_ioctl()
796 ret = sdmmc_wait_ready(card); in card_ioctl()
799 /* Ensure card is not busy with data write */ in card_ioctl()
800 ret = sdmmc_wait_ready(card); in card_ioctl()
802 LOG_WRN("Card busy when powering off"); in card_ioctl()
804 /* Power down the card */ in card_ioctl()
805 card->bus_io.power_mode = SDHC_POWER_OFF; in card_ioctl()
806 ret = sdhc_set_io(card->sdhc, &card->bus_io); in card_ioctl()
811 k_mutex_unlock(&card->lock); in card_ioctl()