xref: /linux-6.15/include/linux/mmc/core.h (revision aff985fd)
1 /*
2  *  linux/include/linux/mmc/core.h
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #ifndef LINUX_MMC_CORE_H
9 #define LINUX_MMC_CORE_H
10 
11 #include <linux/interrupt.h>
12 #include <linux/completion.h>
13 
14 struct request;
15 struct mmc_data;
16 struct mmc_request;
17 
18 enum mmc_blk_status {
19 	MMC_BLK_SUCCESS = 0,
20 	MMC_BLK_PARTIAL,
21 	MMC_BLK_CMD_ERR,
22 	MMC_BLK_RETRY,
23 	MMC_BLK_ABORT,
24 	MMC_BLK_DATA_ERR,
25 	MMC_BLK_ECC_ERR,
26 	MMC_BLK_NOMEDIUM,
27 	MMC_BLK_NEW_REQUEST,
28 };
29 
30 struct mmc_command {
31 	u32			opcode;
32 	u32			arg;
33 #define MMC_CMD23_ARG_REL_WR	(1 << 31)
34 #define MMC_CMD23_ARG_PACKED	((0 << 31) | (1 << 30))
35 #define MMC_CMD23_ARG_TAG_REQ	(1 << 29)
36 	u32			resp[4];
37 	unsigned int		flags;		/* expected response type */
38 #define MMC_RSP_PRESENT	(1 << 0)
39 #define MMC_RSP_136	(1 << 1)		/* 136 bit response */
40 #define MMC_RSP_CRC	(1 << 2)		/* expect valid crc */
41 #define MMC_RSP_BUSY	(1 << 3)		/* card may send busy */
42 #define MMC_RSP_OPCODE	(1 << 4)		/* response contains opcode */
43 
44 #define MMC_CMD_MASK	(3 << 5)		/* non-SPI command type */
45 #define MMC_CMD_AC	(0 << 5)
46 #define MMC_CMD_ADTC	(1 << 5)
47 #define MMC_CMD_BC	(2 << 5)
48 #define MMC_CMD_BCR	(3 << 5)
49 
50 #define MMC_RSP_SPI_S1	(1 << 7)		/* one status byte */
51 #define MMC_RSP_SPI_S2	(1 << 8)		/* second byte */
52 #define MMC_RSP_SPI_B4	(1 << 9)		/* four data bytes */
53 #define MMC_RSP_SPI_BUSY (1 << 10)		/* card may send busy */
54 
55 /*
56  * These are the native response types, and correspond to valid bit
57  * patterns of the above flags.  One additional valid pattern
58  * is all zeros, which means we don't expect a response.
59  */
60 #define MMC_RSP_NONE	(0)
61 #define MMC_RSP_R1	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
62 #define MMC_RSP_R1B	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)
63 #define MMC_RSP_R2	(MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
64 #define MMC_RSP_R3	(MMC_RSP_PRESENT)
65 #define MMC_RSP_R4	(MMC_RSP_PRESENT)
66 #define MMC_RSP_R5	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
67 #define MMC_RSP_R6	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
68 #define MMC_RSP_R7	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
69 
70 /* Can be used by core to poll after switch to MMC HS mode */
71 #define MMC_RSP_R1_NO_CRC	(MMC_RSP_PRESENT|MMC_RSP_OPCODE)
72 
73 #define mmc_resp_type(cmd)	((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE))
74 
75 /*
76  * These are the SPI response types for MMC, SD, and SDIO cards.
77  * Commands return R1, with maybe more info.  Zero is an error type;
78  * callers must always provide the appropriate MMC_RSP_SPI_Rx flags.
79  */
80 #define MMC_RSP_SPI_R1	(MMC_RSP_SPI_S1)
81 #define MMC_RSP_SPI_R1B	(MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY)
82 #define MMC_RSP_SPI_R2	(MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
83 #define MMC_RSP_SPI_R3	(MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
84 #define MMC_RSP_SPI_R4	(MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
85 #define MMC_RSP_SPI_R5	(MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
86 #define MMC_RSP_SPI_R7	(MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
87 
88 #define mmc_spi_resp_type(cmd)	((cmd)->flags & \
89 		(MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY|MMC_RSP_SPI_S2|MMC_RSP_SPI_B4))
90 
91 /*
92  * These are the command types.
93  */
94 #define mmc_cmd_type(cmd)	((cmd)->flags & MMC_CMD_MASK)
95 
96 	unsigned int		retries;	/* max number of retries */
97 	int			error;		/* command error */
98 
99 /*
100  * Standard errno values are used for errors, but some have specific
101  * meaning in the MMC layer:
102  *
103  * ETIMEDOUT    Card took too long to respond
104  * EILSEQ       Basic format problem with the received or sent data
105  *              (e.g. CRC check failed, incorrect opcode in response
106  *              or bad end bit)
107  * EINVAL       Request cannot be performed because of restrictions
108  *              in hardware and/or the driver
109  * ENOMEDIUM    Host can determine that the slot is empty and is
110  *              actively failing requests
111  */
112 
113 	unsigned int		busy_timeout;	/* busy detect timeout in ms */
114 	/* Set this flag only for blocking sanitize request */
115 	bool			sanitize_busy;
116 
117 	struct mmc_data		*data;		/* data segment associated with cmd */
118 	struct mmc_request	*mrq;		/* associated request */
119 };
120 
121 struct mmc_data {
122 	unsigned int		timeout_ns;	/* data timeout (in ns, max 80ms) */
123 	unsigned int		timeout_clks;	/* data timeout (in clocks) */
124 	unsigned int		blksz;		/* data block size */
125 	unsigned int		blocks;		/* number of blocks */
126 	int			error;		/* data error */
127 	unsigned int		flags;
128 
129 #define MMC_DATA_WRITE	(1 << 8)
130 #define MMC_DATA_READ	(1 << 9)
131 
132 	unsigned int		bytes_xfered;
133 
134 	struct mmc_command	*stop;		/* stop command */
135 	struct mmc_request	*mrq;		/* associated request */
136 
137 	unsigned int		sg_len;		/* size of scatter list */
138 	int			sg_count;	/* mapped sg entries */
139 	struct scatterlist	*sg;		/* I/O scatter list */
140 	s32			host_cookie;	/* host private data */
141 };
142 
143 struct mmc_host;
144 struct mmc_request {
145 	struct mmc_command	*sbc;		/* SET_BLOCK_COUNT for multiblock */
146 	struct mmc_command	*cmd;
147 	struct mmc_data		*data;
148 	struct mmc_command	*stop;
149 
150 	struct completion	completion;
151 	struct completion	cmd_completion;
152 	void			(*done)(struct mmc_request *);/* completion function */
153 	struct mmc_host		*host;
154 
155 	/* Allow other commands during this ongoing data transfer or busy wait */
156 	bool			cap_cmd_during_tfr;
157 };
158 
159 struct mmc_card;
160 struct mmc_async_req;
161 
162 extern int mmc_stop_bkops(struct mmc_card *);
163 extern int mmc_read_bkops_status(struct mmc_card *);
164 extern struct mmc_async_req *mmc_start_req(struct mmc_host *,
165 					   struct mmc_async_req *,
166 					   enum mmc_blk_status *);
167 extern int mmc_interrupt_hpi(struct mmc_card *);
168 extern void mmc_wait_for_req(struct mmc_host *, struct mmc_request *);
169 extern void mmc_wait_for_req_done(struct mmc_host *host,
170 				  struct mmc_request *mrq);
171 extern bool mmc_is_req_done(struct mmc_host *host, struct mmc_request *mrq);
172 extern int mmc_wait_for_cmd(struct mmc_host *, struct mmc_command *, int);
173 extern int mmc_app_cmd(struct mmc_host *, struct mmc_card *);
174 extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *,
175 	struct mmc_command *, int);
176 extern void mmc_start_bkops(struct mmc_card *card, bool from_exception);
177 extern int mmc_switch(struct mmc_card *, u8, u8, u8, unsigned int);
178 extern int mmc_send_tuning(struct mmc_host *host, u32 opcode, int *cmd_error);
179 extern int mmc_abort_tuning(struct mmc_host *host, u32 opcode);
180 extern int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd);
181 
182 #define MMC_ERASE_ARG		0x00000000
183 #define MMC_SECURE_ERASE_ARG	0x80000000
184 #define MMC_TRIM_ARG		0x00000001
185 #define MMC_DISCARD_ARG		0x00000003
186 #define MMC_SECURE_TRIM1_ARG	0x80000001
187 #define MMC_SECURE_TRIM2_ARG	0x80008000
188 
189 #define MMC_SECURE_ARGS		0x80000000
190 #define MMC_TRIM_ARGS		0x00008001
191 
192 extern int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
193 		     unsigned int arg);
194 extern int mmc_can_erase(struct mmc_card *card);
195 extern int mmc_can_trim(struct mmc_card *card);
196 extern int mmc_can_discard(struct mmc_card *card);
197 extern int mmc_can_sanitize(struct mmc_card *card);
198 extern int mmc_can_secure_erase_trim(struct mmc_card *card);
199 extern int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
200 				   unsigned int nr);
201 extern unsigned int mmc_calc_max_discard(struct mmc_card *card);
202 
203 extern int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen);
204 extern int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
205 			      bool is_rel_write);
206 extern int mmc_hw_reset(struct mmc_host *host);
207 extern int mmc_can_reset(struct mmc_card *card);
208 
209 extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *);
210 extern unsigned int mmc_align_data_size(struct mmc_card *, unsigned int);
211 
212 extern int __mmc_claim_host(struct mmc_host *host, atomic_t *abort);
213 extern void mmc_release_host(struct mmc_host *host);
214 
215 extern void mmc_get_card(struct mmc_card *card);
216 extern void mmc_put_card(struct mmc_card *card);
217 
218 extern int mmc_flush_cache(struct mmc_card *);
219 
220 extern int mmc_detect_card_removed(struct mmc_host *host);
221 
222 /**
223  *	mmc_claim_host - exclusively claim a host
224  *	@host: mmc host to claim
225  *
226  *	Claim a host for a set of operations.
227  */
228 static inline void mmc_claim_host(struct mmc_host *host)
229 {
230 	__mmc_claim_host(host, NULL);
231 }
232 
233 struct device_node;
234 extern u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max);
235 extern int mmc_of_parse_voltage(struct device_node *np, u32 *mask);
236 
237 #endif /* LINUX_MMC_CORE_H */
238