Lines Matching +full:ready +full:- +full:high +full:- +full:drive

4  * SPDX-License-Identifier: Apache-2.0
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()
42 return -EACCES; in sdmmc_read_status()
46 return -EINVAL; in sdmmc_read_status()
50 return -EIO; in sdmmc_read_status()
61 return -EBUSY; in sdmmc_read_status()
64 /* Waits for SD card to be ready for data. Returns 0 if card is ready */
70 if (!sdhc_card_busy(card->sdhc)) { in sdmmc_wait_ready()
76 if (ret == -ETIMEDOUT) { in sdmmc_wait_ready()
81 timeout -= (CONFIG_SD_CMD_TIMEOUT * in sdmmc_wait_ready()
87 timeout -= 125; in sdmmc_wait_ready()
89 return -EBUSY; in sdmmc_wait_ready()
98 csd->csd_structure = (uint8_t)((raw_csd[3U] & 0xC0000000U) >> 30U); in sdmmc_decode_csd()
99 csd->read_time1 = (uint8_t)((raw_csd[3U] & 0xFF0000U) >> 16U); in sdmmc_decode_csd()
100 csd->read_time2 = (uint8_t)((raw_csd[3U] & 0xFF00U) >> 8U); in sdmmc_decode_csd()
101 csd->xfer_rate = (uint8_t)(raw_csd[3U] & 0xFFU); in sdmmc_decode_csd()
102 csd->cmd_class = (uint16_t)((raw_csd[2U] & 0xFFF00000U) >> 20U); in sdmmc_decode_csd()
103 csd->read_blk_len = (uint8_t)((raw_csd[2U] & 0xF0000U) >> 16U); in sdmmc_decode_csd()
105 csd->flags |= SD_CSD_READ_BLK_PARTIAL_FLAG; in sdmmc_decode_csd()
108 csd->flags |= SD_CSD_READ_BLK_PARTIAL_FLAG; in sdmmc_decode_csd()
111 csd->flags |= SD_CSD_READ_BLK_MISALIGN_FLAG; in sdmmc_decode_csd()
114 csd->flags |= SD_CSD_DSR_IMPLEMENTED_FLAG; in sdmmc_decode_csd()
117 switch (csd->csd_structure) { in sdmmc_decode_csd()
119 csd->device_size = (uint32_t)((raw_csd[2U] & 0x3FFU) << 2U); in sdmmc_decode_csd()
120 csd->device_size |= (uint32_t)((raw_csd[1U] & 0xC0000000U) >> 30U); in sdmmc_decode_csd()
121 csd->read_current_min = (uint8_t)((raw_csd[1U] & 0x38000000U) >> 27U); in sdmmc_decode_csd()
122 csd->read_current_max = (uint8_t)((raw_csd[1U] & 0x7000000U) >> 24U); in sdmmc_decode_csd()
123 csd->write_current_min = (uint8_t)((raw_csd[1U] & 0xE00000U) >> 20U); in sdmmc_decode_csd()
124 csd->write_current_max = (uint8_t)((raw_csd[1U] & 0x1C0000U) >> 18U); in sdmmc_decode_csd()
125 csd->dev_size_mul = (uint8_t)((raw_csd[1U] & 0x38000U) >> 15U); in sdmmc_decode_csd()
128 tmp_blk_count = ((csd->device_size + 1U) << (csd->dev_size_mul + 2U)); in sdmmc_decode_csd()
129 tmp_blk_size = (1U << (csd->read_blk_len)); in sdmmc_decode_csd()
145 csd->device_size = (uint32_t)((raw_csd[2U] & 0x3FU) << 16U); in sdmmc_decode_csd()
146 csd->device_size |= (uint32_t)((raw_csd[1U] & 0xFFFF0000U) >> 16U); in sdmmc_decode_csd()
148 tmp_blk_count = ((csd->device_size + 1U) * 1024U); in sdmmc_decode_csd()
161 csd->flags |= SD_CSD_ERASE_BLK_EN_FLAG; in sdmmc_decode_csd()
163 csd->erase_size = (uint8_t)((raw_csd[1U] & 0x3F80U) >> 7U); in sdmmc_decode_csd()
164 csd->write_prtect_size = (uint8_t)(raw_csd[1U] & 0x7FU); in sdmmc_decode_csd()
165 csd->write_speed_factor = (uint8_t)((raw_csd[0U] & 0x1C000000U) >> 26U); in sdmmc_decode_csd()
166 csd->write_blk_len = (uint8_t)((raw_csd[0U] & 0x3C00000U) >> 22U); in sdmmc_decode_csd()
168 csd->flags |= SD_CSD_WRITE_BLK_PARTIAL_FLAG; in sdmmc_decode_csd()
171 csd->flags |= SD_CSD_FILE_FMT_GRP_FLAG; in sdmmc_decode_csd()
174 csd->flags |= SD_CSD_COPY_FLAG; in sdmmc_decode_csd()
177 csd->flags |= SD_CSD_PERMANENT_WRITE_PROTECT_FLAG; in sdmmc_decode_csd()
180 csd->flags |= SD_CSD_TMP_WRITE_PROTECT_FLAG; in sdmmc_decode_csd()
182 csd->file_fmt = (uint8_t)((raw_csd[0U] & 0xC00U) >> 10U); in sdmmc_decode_csd()
187 cid->manufacturer = (uint8_t)((raw_cid[3U] & 0xFF000000U) >> 24U); in sdmmc_decode_cid()
188 cid->application = (uint16_t)((raw_cid[3U] & 0xFFFF00U) >> 8U); in sdmmc_decode_cid()
190 cid->name[0U] = (uint8_t)((raw_cid[3U] & 0xFFU)); in sdmmc_decode_cid()
191 cid->name[1U] = (uint8_t)((raw_cid[2U] & 0xFF000000U) >> 24U); in sdmmc_decode_cid()
192 cid->name[2U] = (uint8_t)((raw_cid[2U] & 0xFF0000U) >> 16U); in sdmmc_decode_cid()
193 cid->name[3U] = (uint8_t)((raw_cid[2U] & 0xFF00U) >> 8U); in sdmmc_decode_cid()
194 cid->name[4U] = (uint8_t)((raw_cid[2U] & 0xFFU)); in sdmmc_decode_cid()
196 cid->version = (uint8_t)((raw_cid[1U] & 0xFF000000U) >> 24U); in sdmmc_decode_cid()
198 cid->ser_num = (uint32_t)((raw_cid[1U] & 0xFFFFFFU) << 8U); in sdmmc_decode_cid()
199 cid->ser_num |= (uint32_t)((raw_cid[0U] & 0xFF000000U) >> 24U); in sdmmc_decode_cid()
201 cid->date = (uint16_t)((raw_cid[0U] & 0xFFF00U) >> 8U); in sdmmc_decode_cid()
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()
232 cxd[3 - i] = sys_be32_to_cpu(cxd_be[i]); in sdmmc_spi_read_cxd()
249 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_read_cxd()
267 if (card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) { in sdmmc_read_csd()
270 ret = sdmmc_read_cxd(card, SD_SEND_CSD, card->relative_addr, csd); in sdmmc_read_csd()
273 return -ENOTSUP; 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()
293 if (card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) { in card_read_cid()
299 return -ENOTSUP; in card_read_cid()
306 if (card->type == CARD_MMC) { in card_read_cid()
331 if (!(card->flags & SD_1800MV_FLAG)) { 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()
355 * cycle. Some cards will only drive these 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()
364 return -EAGAIN; 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()
394 * If SD does not drive at least one of in sdmmc_switch_voltage()
395 * DAT[3:0] high within 1ms, switch failed in sdmmc_switch_voltage()
398 if (sdhc_card_busy(card->sdhc)) { in sdmmc_switch_voltage()
400 return -EAGAIN; in sdmmc_switch_voltage()
402 card->card_voltage = SD_VOL_1_8_V; in sdmmc_switch_voltage()
423 ret = sdhc_request(card->sdhc, &cmd, NULL); 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()
444 cmd.arg = ((card->relative_addr) << 16U); in sdmmc_select_card()
449 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_select_card()
473 ret = sdhc_request(card->sdhc, &cmd, NULL); in card_app_command()
481 return -EIO; 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()
486 return -ENOTSUP; in card_app_command()
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()
537 LOG_ERR("Card did not return to ready state"); in card_read()
538 return -ETIMEDOUT; in card_read()
551 if ((start_block + num_blocks) > card->block_count) { in card_read_blocks()
552 return -EINVAL; in card_read_blocks()
554 if (card->type == CARD_SDIO) { in card_read_blocks()
556 return -ENOTSUP; in card_read_blocks()
558 ret = k_mutex_lock(&card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in card_read_blocks()
561 return -EBUSY; in card_read_blocks()
569 if ((((uintptr_t)rbuf) & (CONFIG_SDHC_BUFFER_ALIGNMENT - 1)) != 0) { in card_read_blocks()
572 if (sizeof(card->card_buffer) < card->block_size) { in card_read_blocks()
575 k_mutex_unlock(&card->lock); in card_read_blocks()
576 return -ENOBUFS; in card_read_blocks()
578 rlen = sizeof(card->card_buffer) / card->block_size; 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()
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()
600 k_mutex_unlock(&card->lock); in card_read_blocks()
604 k_mutex_unlock(&card->lock); in card_read_blocks()
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()
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()
697 return -EIO; in card_write()
702 LOG_ERR("Card did not return to ready state"); in card_write()
703 return -ETIMEDOUT; in card_write()
717 if ((start_block + num_blocks) > card->block_count) { in card_write_blocks()
718 return -EINVAL; in card_write_blocks()
720 if (card->type == CARD_SDIO) { in card_write_blocks()
722 return -ENOTSUP; in card_write_blocks()
724 ret = k_mutex_lock(&card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in card_write_blocks()
727 return -EBUSY; in card_write_blocks()
734 if ((((uintptr_t)wbuf) & (CONFIG_SDHC_BUFFER_ALIGNMENT - 1)) != 0) { in card_write_blocks()
737 if (sizeof(card->card_buffer) < card->block_size) { in card_write_blocks()
740 k_mutex_unlock(&card->lock); in card_write_blocks()
741 return -ENOBUFS; in card_write_blocks()
743 wlen = sizeof(card->card_buffer) / card->block_size; in card_write_blocks()
748 memcpy(card->card_buffer, buf_offset, wlen * card->block_size); 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()
765 k_mutex_unlock(&card->lock); in card_write_blocks()
769 k_mutex_unlock(&card->lock); in card_write_blocks()
778 ret = k_mutex_lock(&card->lock, K_MSEC(CONFIG_SD_DATA_TIMEOUT)); in card_ioctl()
785 (*(uint32_t *)buf) = card->block_count; in card_ioctl()
789 (*(uint32_t *)buf) = card->block_size; 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()
809 ret = -ENOTSUP; in card_ioctl()
811 k_mutex_unlock(&card->lock); in card_ioctl()