Lines Matching +full:read +full:- +full:cmd
4 * SPDX-License-Identifier: Apache-2.0
25 scr->flags = 0U; in sdmmc_decode_scr()
26 scr->scr_structure = (uint8_t)((raw_scr[0U] & 0xF0000000U) >> 28U); in sdmmc_decode_scr()
27 scr->sd_spec = (uint8_t)((raw_scr[0U] & 0xF000000U) >> 24U); in sdmmc_decode_scr()
29 scr->flags |= SD_SCR_DATA_STATUS_AFTER_ERASE; in sdmmc_decode_scr()
31 scr->sd_sec = (uint8_t)((raw_scr[0U] & 0x700000U) >> 20U); in sdmmc_decode_scr()
32 scr->sd_width = (uint8_t)((raw_scr[0U] & 0xF0000U) >> 16U); in sdmmc_decode_scr()
34 scr->flags |= SD_SCR_SPEC3; in sdmmc_decode_scr()
36 scr->sd_ext_sec = (uint8_t)((raw_scr[0U] & 0x7800U) >> 10U); in sdmmc_decode_scr()
37 scr->cmd_support = (uint8_t)(raw_scr[0U] & 0x3U); in sdmmc_decode_scr()
38 scr->rsvd = raw_scr[1U]; in sdmmc_decode_scr()
40 switch (scr->sd_spec) { in sdmmc_decode_scr()
49 if (scr->flags & SD_SCR_SPEC3) { in sdmmc_decode_scr()
71 struct sdhc_command cmd; in sdmmc_spi_send_ocr() local
74 cmd.opcode = SD_SPI_READ_OCR; in sdmmc_spi_send_ocr()
75 cmd.arg = arg; in sdmmc_spi_send_ocr()
76 cmd.response_type = SD_SPI_RSP_TYPE_R3; in sdmmc_spi_send_ocr()
77 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT; in sdmmc_spi_send_ocr()
78 cmd.retries = CONFIG_SD_CMD_RETRIES; in sdmmc_spi_send_ocr()
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()
90 return -ENOTSUP; in sdmmc_spi_send_ocr()
99 struct sdhc_command cmd; in sdmmc_send_ocr() local
103 cmd.opcode = SD_APP_SEND_OP_COND; in sdmmc_send_ocr()
104 cmd.arg = ocr; in sdmmc_send_ocr()
105 cmd.response_type = (SD_RSP_TYPE_R3 | SD_SPI_RSP_TYPE_R1); in sdmmc_send_ocr()
106 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT; in sdmmc_send_ocr()
107 cmd.retries = CONFIG_SD_CMD_RETRIES; in sdmmc_send_ocr()
118 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_send_ocr()
131 if (card->host_props.is_spi) { in sdmmc_send_ocr()
132 if (!(cmd.response[0] & SD_SPI_R1IDLE_STATE)) { in sdmmc_send_ocr()
136 if ((cmd.response[0U] & SD_OCR_PWR_BUSY_FLAG)) { in sdmmc_send_ocr()
145 return -ETIMEDOUT; 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()
158 struct sdhc_command cmd = {0}; in sdmmc_read_scr() local
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()
173 cmd.opcode = SD_APP_SEND_SCR; in sdmmc_read_scr()
174 cmd.arg = 0; in sdmmc_read_scr()
175 cmd.response_type = (SD_RSP_TYPE_R1 | SD_SPI_RSP_TYPE_R1); in sdmmc_read_scr()
176 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT; in sdmmc_read_scr()
177 cmd.retries = CONFIG_SD_CMD_RETRIES; 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()
196 card->flags |= SD_4BITS_WIDTH; in sdmmc_read_scr()
200 card->flags |= SD_SPEED_CLASS_CONTROL_FLAG; in sdmmc_read_scr()
202 /* Check for set block count (CMD 23) support */ in sdmmc_read_scr()
204 card->flags |= SD_CMD23_FLAG; in sdmmc_read_scr()
212 struct sdhc_command cmd = {0}; in sdmmc_set_blocklen() local
214 cmd.opcode = SD_SET_BLOCK_SIZE; in sdmmc_set_blocklen()
215 cmd.arg = block_len; in sdmmc_set_blocklen()
216 cmd.response_type = (SD_RSP_TYPE_R1 | SD_SPI_RSP_TYPE_R1); in sdmmc_set_blocklen()
217 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT; in sdmmc_set_blocklen()
218 cmd.retries = CONFIG_SD_CMD_RETRIES; in sdmmc_set_blocklen()
220 return sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_set_blocklen()
229 struct sdhc_command cmd = {0}; in sdmmc_set_bus_width() local
237 ret = sdmmc_app_command(card, card->relative_addr); in sdmmc_set_bus_width()
243 cmd.opcode = SD_APP_SET_BUS_WIDTH; in sdmmc_set_bus_width()
244 cmd.response_type = SD_RSP_TYPE_R1; in sdmmc_set_bus_width()
245 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT; in sdmmc_set_bus_width()
246 cmd.retries = CONFIG_SD_CMD_RETRIES; in sdmmc_set_bus_width()
250 cmd.arg = 0U; in sdmmc_set_bus_width()
253 cmd.arg = 2U; in sdmmc_set_bus_width()
256 return -ENOTSUP; in sdmmc_set_bus_width()
259 ret = sdhc_request(card->sdhc, &cmd, NULL); in sdmmc_set_bus_width()
264 ret = sd_check_response(&cmd); in sdmmc_set_bus_width()
266 LOG_DBG("ACMD6 reports error, response 0x%x", cmd.response[0U]); 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()
280 * See table 4-32 in SD physical specification for argument details.
288 struct sdhc_command cmd = {0}; in sdmmc_switch() local
291 cmd.opcode = SD_SWITCH; in sdmmc_switch()
292 cmd.arg = ((mode & 0x1) << 31) | 0x00FFFFFF; in sdmmc_switch()
293 cmd.arg &= ~(0xFU << (group * 4)); in sdmmc_switch()
294 cmd.arg |= (value & 0xF) << (group * 4); in sdmmc_switch()
295 cmd.response_type = (SD_RSP_TYPE_R1 | SD_SPI_RSP_TYPE_R1); in sdmmc_switch()
296 cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT; in sdmmc_switch()
297 cmd.retries = CONFIG_SD_CMD_RETRIES; in sdmmc_switch()
304 return sdhc_request(card->sdhc, &cmd, &data); in sdmmc_switch()
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()
329 * See table 4-11 and 4.3.10.4 of physical layer specification for in sdmmc_read_switch()
331 * read bits 400-408. 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()
367 * selection is defined using values 0x0-0xF. in sdmmc_select_bus_speed()
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()
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()
412 (find_msb_set(SD_DRIVER_TYPE_C) - 1), status); in sdmmc_select_driver_type()
421 int max_current = -1; in sdmmc_set_current_limit()
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()
440 if (max_current != -1) { in sdmmc_set_current_limit()
461 uint8_t *status = card->card_buffer; 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()
515 if ((status[16] & 0xF) != card->card_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.
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()
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()
594 if (card->flags & SD_4BITS_WIDTH) { in sdmmc_init_hs()
615 if (card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) { in sdmmc_card_init()
622 return -ENOTSUP; in sdmmc_card_init()
628 card->type = CARD_SDMMC; in sdmmc_card_init()
630 if (card->flags & SD_SDHC_FLAG) { 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()
658 if (card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_SPI_MODE)) { 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()
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()
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()
687 card->host_props.host_caps.vol_180_support = false; 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()
702 if (!card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) { in sdmmc_card_init()
717 if (!card->host_props.is_spi && IS_ENABLED(CONFIG_SDHC_SUPPORTS_NATIVE_MODE)) { 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()
740 /* Read SD SCR (SD configuration register), 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()
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()
762 if ((card->flags & SD_HIGH_CAPACITY_FLAG) == 0) { in sdmmc_card_init()
769 card->block_size = 512; 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()