Lines Matching full:card
22 static inline int sd_idle(struct sd_card *card) in sd_idle() argument
26 /* Reset card with CMD0 */ in sd_idle()
32 return sdhc_request(card->sdhc, &cmd, NULL); in sd_idle()
41 static int sd_send_interface_condition(struct sd_card *card) in sd_send_interface_condition() argument
47 /* Reset card with CMD0 */ in sd_send_interface_condition()
48 ret = sd_idle(card); in sd_send_interface_condition()
50 LOG_ERR("Card error on CMD0"); in sd_send_interface_condition()
60 ret = sdhc_request(card->sdhc, &cmd, NULL); in sd_send_interface_condition()
66 if (card->host_props.is_spi) { in sd_send_interface_condition()
72 LOG_DBG("Legacy card detected, no CMD8 support"); in sd_send_interface_condition()
77 /* Card does not support 3.3V */ in sd_send_interface_condition()
81 card->flags |= SD_SDHC_FLAG; in sd_send_interface_condition()
85 /* Sends CMD59 to enable CRC checking for SD card in SPI mode */
86 static int sd_enable_crc(struct sd_card *card) in sd_enable_crc() argument
91 __ASSERT_NO_MSG(card->host_props.is_spi); in sd_enable_crc()
97 return sdhc_request(card->sdhc, &cmd, NULL); in sd_enable_crc()
100 /* Retries SD and SDIO initialisation until card has valid response to SD CMD8 */
101 static int sd_common_init(struct sd_card *card) in sd_common_init() argument
106 ret = sd_retry(sd_send_interface_condition, card, CONFIG_SD_RETRY_COUNT); in sd_common_init()
108 LOG_INF("Card does not support CMD8, assuming legacy card"); in sd_common_init()
109 return sd_idle(card); in sd_common_init()
111 LOG_ERR("Card error on CMD 8"); in sd_common_init()
114 if (card->host_props.is_spi && in sd_common_init()
117 ret = sd_enable_crc(card); in sd_common_init()
122 static int sd_init_io(struct sd_card *card) in sd_init_io() argument
124 struct sdhc_io *bus_io = &card->bus_io; in sd_init_io()
125 struct sdhc_host_props *host_props = &card->host_props; in sd_init_io()
151 /* Toggle power to card to reset it */ in sd_init_io()
152 LOG_DBG("Resetting power to card"); in sd_init_io()
154 ret = sdhc_set_io(card->sdhc, bus_io); in sd_init_io()
156 LOG_ERR("Could not disable card power via SDHC"); in sd_init_io()
159 sd_delay(card->host_props.power_delay); in sd_init_io()
161 ret = sdhc_set_io(card->sdhc, bus_io); in sd_init_io()
163 LOG_ERR("Could not disable card power via SDHC"); in sd_init_io()
166 /* After reset or init, card voltage should be max HC support */ in sd_init_io()
167 card->card_voltage = voltage; in sd_init_io()
168 /* Reset card flags */ in sd_init_io()
169 card->flags = 0U; in sd_init_io()
170 /* Delay so card can power up */ in sd_init_io()
171 sd_delay(card->host_props.power_delay); in sd_init_io()
174 ret = sdhc_set_io(card->sdhc, bus_io); in sd_init_io()
185 static int sd_command_init(struct sd_card *card) in sd_command_init() argument
190 * We must wait 74 clock cycles, per SD spec, to use card after power in sd_command_init()
197 * Start card initialization and identification in sd_command_init()
202 ret = sd_common_init(card); in sd_command_init()
208 /* Attempt to initialize SDIO card */ in sd_command_init()
209 if (!sdio_card_init(card)) { in sd_command_init()
214 /* Attempt to initialize SDMMC card */ in sd_command_init()
215 if (!sdmmc_card_init(card)) { in sd_command_init()
220 ret = sd_idle(card); in sd_command_init()
222 LOG_ERR("Card error on CMD0"); in sd_command_init()
225 if (!mmc_card_init(card)) { in sd_command_init()
229 /* Unknown card type */ in sd_command_init()
233 /* Initializes SD/SDIO card */
234 int sd_init(const struct device *sdhc_dev, struct sd_card *card) in sd_init() argument
241 card->sdhc = sdhc_dev; in sd_init()
242 ret = sdhc_get_host_props(card->sdhc, &card->host_props); in sd_init()
248 /* init and lock card mutex */ in sd_init()
249 ret = k_mutex_init(&card->lock); in sd_init()
251 LOG_DBG("Could not init card mutex"); in sd_init()
254 ret = k_mutex_lock(&card->lock, K_MSEC(CONFIG_SD_INIT_TIMEOUT)); in sd_init()
256 LOG_ERR("Timeout while trying to acquire card mutex"); in sd_init()
261 ret = sd_init_io(card); in sd_init()
263 k_mutex_unlock(&card->lock); in sd_init()
269 * that the card is in a bad state. The return code SD_RESTART in sd_init()
270 * indicates that the initialization left the card in a bad state. in sd_init()
272 * - set card status to error in sd_init()
273 * - re init host I/O (will also toggle power to the SD card) in sd_init()
276 * card is inaccessible in sd_init()
278 ret = sd_command_init(card); in sd_init()
281 card->status = CARD_ERROR; in sd_init()
283 ret = sd_init_io(card); in sd_init()
286 k_mutex_unlock(&card->lock); in sd_init()
289 ret = sd_command_init(card); in sd_init()
291 LOG_ERR("Failed to init SD card after I/O reset"); in sd_init()
292 k_mutex_unlock(&card->lock); in sd_init()
297 k_mutex_unlock(&card->lock); in sd_init()
298 card->status = CARD_ERROR; in sd_init()
301 /* Card initialization succeeded. */ in sd_init()
302 card->status = CARD_INITIALIZED; in sd_init()
303 /* Unlock card mutex */ in sd_init()
304 ret = k_mutex_unlock(&card->lock); in sd_init()
306 LOG_DBG("Could not unlock card mutex"); in sd_init()
312 /* Return true if card is present, false otherwise */