1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *  linux/include/linux/mmc/core.h
4  */
5 #ifndef LINUX_MMC_CORE_H
6 #define LINUX_MMC_CORE_H
7 
8 #include <linux/completion.h>
9 #include <linux/types.h>
10 
11 struct mmc_data;
12 struct mmc_request;
13 
14 struct mmc_command {
15 	u32			opcode;
16 	u32			arg;
17 #define MMC_CMD23_ARG_REL_WR	(1 << 31)
18 #define MMC_CMD23_ARG_TAG_REQ	(1 << 29)
19 	u32			resp[4];
20 	unsigned int		flags;		/* expected response type */
21 #define MMC_RSP_PRESENT	(1 << 0)
22 #define MMC_RSP_136	(1 << 1)		/* 136 bit response */
23 #define MMC_RSP_CRC	(1 << 2)		/* expect valid crc */
24 #define MMC_RSP_BUSY	(1 << 3)		/* card may send busy */
25 #define MMC_RSP_OPCODE	(1 << 4)		/* response contains opcode */
26 
27 #define MMC_CMD_MASK	(3 << 5)		/* non-SPI command type */
28 #define MMC_CMD_AC	(0 << 5)
29 #define MMC_CMD_ADTC	(1 << 5)
30 #define MMC_CMD_BC	(2 << 5)
31 #define MMC_CMD_BCR	(3 << 5)
32 
33 #define MMC_RSP_SPI_S1	(1 << 7)		/* one status byte */
34 #define MMC_RSP_SPI_S2	(1 << 8)		/* second byte */
35 #define MMC_RSP_SPI_B4	(1 << 9)		/* four data bytes */
36 #define MMC_RSP_SPI_BUSY (1 << 10)		/* card may send busy */
37 
38 /*
39  * These are the native response types, and correspond to valid bit
40  * patterns of the above flags.  One additional valid pattern
41  * is all zeros, which means we don't expect a response.
42  */
43 #define MMC_RSP_NONE	(0)
44 #define MMC_RSP_R1	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
45 #define MMC_RSP_R1B	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)
46 #define MMC_RSP_R2	(MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
47 #define MMC_RSP_R3	(MMC_RSP_PRESENT)
48 #define MMC_RSP_R4	(MMC_RSP_PRESENT)
49 #define MMC_RSP_R5	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
50 #define MMC_RSP_R6	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
51 #define MMC_RSP_R7	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
52 
53 /* Can be used by core to poll after switch to MMC HS mode */
54 #define MMC_RSP_R1_NO_CRC	(MMC_RSP_PRESENT|MMC_RSP_OPCODE)
55 
56 #define mmc_resp_type(cmd)	((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE))
57 
58 /*
59  * These are the SPI response types for MMC, SD, and SDIO cards.
60  * Commands return R1, with maybe more info.  Zero is an error type;
61  * callers must always provide the appropriate MMC_RSP_SPI_Rx flags.
62  */
63 #define MMC_RSP_SPI_R1	(MMC_RSP_SPI_S1)
64 #define MMC_RSP_SPI_R1B	(MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY)
65 #define MMC_RSP_SPI_R2	(MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
66 #define MMC_RSP_SPI_R3	(MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
67 #define MMC_RSP_SPI_R4	(MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
68 #define MMC_RSP_SPI_R5	(MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
69 #define MMC_RSP_SPI_R7	(MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
70 
71 #define mmc_spi_resp_type(cmd)	((cmd)->flags & \
72 		(MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY|MMC_RSP_SPI_S2|MMC_RSP_SPI_B4))
73 
74 /*
75  * These are the command types.
76  */
77 #define mmc_cmd_type(cmd)	((cmd)->flags & MMC_CMD_MASK)
78 
79 	unsigned int		retries;	/* max number of retries */
80 	int			error;		/* command error */
81 
82 /*
83  * Standard errno values are used for errors, but some have specific
84  * meaning in the MMC layer:
85  *
86  * ETIMEDOUT    Card took too long to respond
87  * EILSEQ       Basic format problem with the received or sent data
88  *              (e.g. CRC check failed, incorrect opcode in response
89  *              or bad end bit)
90  * EINVAL       Request cannot be performed because of restrictions
91  *              in hardware and/or the driver
92  * ENOMEDIUM    Host can determine that the slot is empty and is
93  *              actively failing requests
94  */
95 
96 	unsigned int		busy_timeout;	/* busy detect timeout in ms */
97 	struct mmc_data		*data;		/* data segment associated with cmd */
98 	struct mmc_request	*mrq;		/* associated request */
99 };
100 
101 struct mmc_data {
102 	unsigned int		timeout_ns;	/* data timeout (in ns, max 80ms) */
103 	unsigned int		timeout_clks;	/* data timeout (in clocks) */
104 	unsigned int		blksz;		/* data block size */
105 	unsigned int		blocks;		/* number of blocks */
106 	unsigned int		blk_addr;	/* block address */
107 	int			error;		/* data error */
108 	unsigned int		flags;
109 
110 #define MMC_DATA_WRITE		BIT(8)
111 #define MMC_DATA_READ		BIT(9)
112 /* Extra flags used by CQE */
113 #define MMC_DATA_QBR		BIT(10)		/* CQE queue barrier*/
114 #define MMC_DATA_PRIO		BIT(11)		/* CQE high priority */
115 #define MMC_DATA_REL_WR		BIT(12)		/* Reliable write */
116 #define MMC_DATA_DAT_TAG	BIT(13)		/* Tag request */
117 #define MMC_DATA_FORCED_PRG	BIT(14)		/* Forced programming */
118 
119 	unsigned int		bytes_xfered;
120 
121 	struct mmc_command	*stop;		/* stop command */
122 	struct mmc_request	*mrq;		/* associated request */
123 
124 	unsigned int		sg_len;		/* size of scatter list */
125 	int			sg_count;	/* mapped sg entries */
126 	struct scatterlist	*sg;		/* I/O scatter list */
127 	s32			host_cookie;	/* host private data */
128 };
129 
130 struct mmc_host;
131 struct mmc_request {
132 	struct mmc_command	*sbc;		/* SET_BLOCK_COUNT for multiblock */
133 	struct mmc_command	*cmd;
134 	struct mmc_data		*data;
135 	struct mmc_command	*stop;
136 
137 	struct completion	completion;
138 	struct completion	cmd_completion;
139 	void			(*done)(struct mmc_request *);/* completion function */
140 	/*
141 	 * Notify uppers layers (e.g. mmc block driver) that recovery is needed
142 	 * due to an error associated with the mmc_request. Currently used only
143 	 * by CQE.
144 	 */
145 	void			(*recovery_notifier)(struct mmc_request *);
146 	struct mmc_host		*host;
147 
148 	/* Allow other commands during this ongoing data transfer or busy wait */
149 	bool			cap_cmd_during_tfr;
150 
151 	int			tag;
152 
153 #ifdef CONFIG_MMC_CRYPTO
154 	const struct bio_crypt_ctx *crypto_ctx;
155 	int			crypto_key_slot;
156 #endif
157 };
158 
159 struct mmc_card;
160 
161 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq);
162 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd,
163 		int retries);
164 
165 int mmc_hw_reset(struct mmc_card *card);
166 int mmc_sw_reset(struct mmc_card *card);
167 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card);
168 
169 #endif /* LINUX_MMC_CORE_H */
170