1 /*
2  * Copyright 2022 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Public API for SD subsystem
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_SD_SD_H_
13 #define ZEPHYR_INCLUDE_SD_SD_H_
14 
15 #include <zephyr/device.h>
16 #include <zephyr/drivers/sdhc.h>
17 #include <zephyr/kernel.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /**
24  * @brief card status. Used interally by subsystem.
25  */
26 enum card_status {
27 	CARD_UNINITIALIZED = 0, /*!< card has not been initialized */
28 	CARD_ERROR = 1, /*!< card state is error */
29 	CARD_INITIALIZED = 2, /*!< card is in valid state */
30 };
31 
32 /**
33  * @brief card type. Used interally by subsystem.
34  */
35 enum card_type {
36 	CARD_SDMMC = 0, /*!< SD memory card */
37 	CARD_SDIO = 1, /*!< SD I/O card */
38 	CARD_COMBO = 2, /*!< SD memory and I/O card */
39 	CARD_MMC = 3, /*!< MMC memory card */
40 };
41 
42 
43 /**
44  * @brief SD card structure
45  *
46  * This structure is used by the subsystem to track an individual SD
47  * device connected to the system. The application may access these
48  * fields, but use caution when changing values.
49  */
50 struct sd_card {
51 	const struct device *sdhc; /*!< SD host controller for card */
52 	struct sdhc_io bus_io; /*!< Current bus I/O props for SDHC */
53 	enum sd_voltage card_voltage; /*!< Card signal voltage */
54 	struct k_mutex lock; /*!< card mutex */
55 	struct sdhc_host_props host_props; /*!< SDHC host properties */
56 	uint32_t ocr; /*!< Raw card OCR content */
57 	struct sd_switch_caps switch_caps; /*!< SD switch capabilities */
58 	unsigned int num_io: 3; /*!< I/O function count. 0 for SD cards */
59 	uint16_t relative_addr; /*!< Card relative address */
60 	uint32_t block_count; /*!< Number of blocks in SD card */
61 	uint16_t block_size; /*!< SD block size */
62 	uint8_t sd_version; /*!< SD specification version */
63 	uint8_t card_speed; /*!< Card timing mode */
64 	enum card_status status; /*!< Card status */
65 	enum card_type type; /*!< Card type */
66 	uint16_t flags; /*!< Card flags */
67 	uint8_t bus_width; /*!< Desired bus width */
68 	uint8_t card_buffer[CONFIG_SD_BUFFER_SIZE]
69 		__aligned(CONFIG_SDHC_BUFFER_ALIGNMENT); /* Card internal buffer */
70 };
71 
72 /**
73  * @brief Initialize an SD device
74  *
75  * Initializes an SD device to use with the subsystem. After this call,
76  * only the SD card structure is required to access the card.
77  * @param sdhc_dev SD host controller device for this card
78  * @param card SD card structure for this card
79  * @retval 0 card was initialized
80  * @retval -ETIMEDOUT: card initialization timed out
81  * @retval -EBUSY: card is busy
82  * @retval -EIO: IO error while starting card
83  */
84 int sd_init(const struct device *sdhc_dev, struct sd_card *card);
85 
86 /**
87  * @brief checks to see if card is present in the SD slot
88  *
89  * @param sdhc_dev SD host controller to check for card presence on
90  * @retval true card is present
91  * @retval false card is not present
92  */
93 bool sd_is_card_present(const struct device *sdhc_dev);
94 
95 
96 #ifdef __cplusplus
97 }
98 #endif
99 
100 #endif /* ZEPHYR_INCLUDE_SD_SD_H_ */
101