xref: /freebsd/sys/dev/mmc/mmcreg.h (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
1 /*-
2  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * Portions of this software may have been developed with reference to
25  * the SD Simplified Specification.  The following disclaimer may apply:
26  *
27  * The following conditions apply to the release of the simplified
28  * specification ("Simplified Specification") by the SD Card Association and
29  * the SD Group. The Simplified Specification is a subset of the complete SD
30  * Specification which is owned by the SD Card Association and the SD
31  * Group. This Simplified Specification is provided on a non-confidential
32  * basis subject to the disclaimers below. Any implementation of the
33  * Simplified Specification may require a license from the SD Card
34  * Association, SD Group, SD-3C LLC or other third parties.
35  *
36  * Disclaimers:
37  *
38  * The information contained in the Simplified Specification is presented only
39  * as a standard specification for SD Cards and SD Host/Ancillary products and
40  * is provided "AS-IS" without any representations or warranties of any
41  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
42  * Card Association for any damages, any infringements of patents or other
43  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
44  * parties, which may result from its use. No license is granted by
45  * implication, estoppel or otherwise under any patent or other rights of the
46  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
47  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
48  * or the SD Card Association to disclose or distribute any technical
49  * information, know-how or other confidential information to any third party.
50  *
51  * $FreeBSD$
52  */
53 
54 #ifndef DEV_MMC_MMCREG_H
55 #define	DEV_MMC_MMCREG_H
56 
57 /*
58  * This file contains the register definitions for the mmc and sd busses.
59  * They are taken from publicly available sources.
60  */
61 
62 struct mmc_data;
63 struct mmc_request;
64 
65 struct mmc_command {
66 	uint32_t	opcode;
67 	uint32_t	arg;
68 	uint32_t	resp[4];
69 	uint32_t	flags;		/* Expected responses */
70 #define	MMC_RSP_PRESENT	(1ul << 0)	/* Response */
71 #define	MMC_RSP_136	(1ul << 1)	/* 136 bit response */
72 #define	MMC_RSP_CRC	(1ul << 2)	/* Expect valid crc */
73 #define	MMC_RSP_BUSY	(1ul << 3)	/* Card may send busy */
74 #define	MMC_RSP_OPCODE	(1ul << 4)	/* Response include opcode */
75 #define	MMC_RSP_MASK	0x1ful
76 #define	MMC_CMD_AC	(0ul << 5)	/* Addressed Command, no data */
77 #define	MMC_CMD_ADTC	(1ul << 5)	/* Addressed Data transfer cmd */
78 #define	MMC_CMD_BC	(2ul << 5)	/* Broadcast command, no response */
79 #define	MMC_CMD_BCR	(3ul << 5)	/* Broadcast command with response */
80 #define	MMC_CMD_MASK	(3ul << 5)
81 
82 /* Possible response types defined in the standard: */
83 #define	MMC_RSP_NONE	(0)
84 #define	MMC_RSP_R1	(MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
85 #define	MMC_RSP_R1B	(MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE | MMC_RSP_BUSY)
86 #define	MMC_RSP_R2	(MMC_RSP_PRESENT | MMC_RSP_136 | MMC_RSP_CRC)
87 #define	MMC_RSP_R3	(MMC_RSP_PRESENT)
88 #define	MMC_RSP_R4	(MMC_RSP_PRESENT)
89 #define	MMC_RSP_R5	(MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
90 #define	MMC_RSP_R5B	(MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE | MMC_RSP_BUSY)
91 #define	MMC_RSP_R6	(MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
92 #define	MMC_RSP_R7	(MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
93 #define	MMC_RSP(x)	((x) & MMC_RSP_MASK)
94 	uint32_t	retries;
95 	uint32_t	error;
96 #define	MMC_ERR_NONE	0
97 #define	MMC_ERR_TIMEOUT	1
98 #define	MMC_ERR_BADCRC	2
99 #define	MMC_ERR_FIFO	3
100 #define	MMC_ERR_FAILED	4
101 #define	MMC_ERR_INVALID	5
102 #define	MMC_ERR_NO_MEMORY 6
103 #define MMC_ERR_MAX	6
104 	struct mmc_data	*data;		/* Data segment with cmd */
105 	struct mmc_request *mrq;	/* backpointer to request */
106 };
107 
108 /*
109  * R1 responses
110  *
111  * Types (per SD 2.0 standard)
112  *	e : error bit
113  *	s : status bit
114  *	r : detected and set for the actual command response
115  *	x : Detected and set during command execution.  The host can get
116  *	    the status by issuing a command with R1 response.
117  *
118  * Clear Condition (per SD 2.0 standard)
119  *	a : according to the card current state.
120  *	b : always related to the previous command.  reception of a valid
121  *	    command will clear it (with a delay of one command).
122  *	c : clear by read
123  */
124 #define	R1_OUT_OF_RANGE (1u << 31)		/* erx, c */
125 #define	R1_ADDRESS_ERROR (1u << 30)		/* erx, c */
126 #define	R1_BLOCK_LEN_ERROR (1u << 29)		/* erx, c */
127 #define	R1_ERASE_SEQ_ERROR (1u << 28)		/* er, c */
128 #define	R1_ERASE_PARAM (1u << 27)		/* erx, c */
129 #define	R1_WP_VIOLATION (1u << 26)		/* erx, c */
130 #define	R1_CARD_IS_LOCKED (1u << 25)		/* sx, a */
131 #define	R1_LOCK_UNLOCK_FAILED (1u << 24)	/* erx, c */
132 #define	R1_COM_CRC_ERROR (1u << 23)		/* er, b */
133 #define	R1_ILLEGAL_COMMAND (1u << 22)		/* er, b */
134 #define	R1_CARD_ECC_FAILED (1u << 21)		/* erx, c */
135 #define	R1_CC_ERROR (1u << 20)			/* erx, c */
136 #define	R1_ERROR (1u << 19)			/* erx, c */
137 #define	R1_CSD_OVERWRITE (1u << 16)		/* erx, c */
138 #define	R1_WP_ERASE_SKIP (1u << 15)		/* erx, c */
139 #define	R1_CARD_ECC_DISABLED (1u << 14)		/* sx, a */
140 #define	R1_ERASE_RESET (1u << 13)		/* sr, c */
141 #define	R1_CURRENT_STATE_MASK (0xfu << 9)	/* sx, b */
142 #define	R1_READY_FOR_DATA (1u << 8)		/* sx, a */
143 #define	R1_APP_CMD (1u << 5)			/* sr, c */
144 #define	R1_AKE_SEQ_ERROR (1u << 3)		/* er, c */
145 #define	R1_STATUS(x)		((x) & 0xFFFFE000)
146 #define	R1_CURRENT_STATE(x)	(((x) & R1_CURRENT_STATE_MASK) >> 9)
147 #define	R1_STATE_IDLE	0
148 #define	R1_STATE_READY	1
149 #define	R1_STATE_IDENT	2
150 #define	R1_STATE_STBY	3
151 #define	R1_STATE_TRAN	4
152 #define	R1_STATE_DATA	5
153 #define	R1_STATE_RCV	6
154 #define	R1_STATE_PRG	7
155 #define	R1_STATE_DIS	8
156 
157 struct mmc_data {
158 	size_t len;		/* size of the data */
159 	size_t xfer_len;
160 	void *data;		/* data buffer */
161 	uint32_t	flags;
162 #define	MMC_DATA_WRITE	(1UL << 0)
163 #define	MMC_DATA_READ	(1UL << 1)
164 #define	MMC_DATA_STREAM	(1UL << 2)
165 #define	MMC_DATA_MULTI	(1UL << 3)
166 	struct mmc_request *mrq;
167 };
168 
169 struct mmc_request {
170 	struct mmc_command *cmd;
171 	struct mmc_command *stop;
172 	void (*done)(struct mmc_request *); /* Completion function */
173 	void *done_data;		/* requestor set data */
174 	uint32_t flags;
175 #define	MMC_REQ_DONE	1
176 };
177 
178 /* Command definitions */
179 
180 /* Class 0 and 1: Basic commands & read stream commands */
181 #define	MMC_GO_IDLE_STATE	0
182 #define	MMC_SEND_OP_COND	1
183 #define	MMC_ALL_SEND_CID	2
184 #define	MMC_SET_RELATIVE_ADDR	3
185 #define	SD_SEND_RELATIVE_ADDR	3
186 #define	MMC_SET_DSR		4
187 			/* reserved: 5 */
188 #define	MMC_SWITCH_FUNC		6
189 #define	 MMC_SWITCH_FUNC_CMDS	 0
190 #define	 MMC_SWITCH_FUNC_SET	 1
191 #define	 MMC_SWITCH_FUNC_CLR	 2
192 #define	 MMC_SWITCH_FUNC_WR	 3
193 #define	MMC_SELECT_CARD		7
194 #define	MMC_DESELECT_CARD	7
195 #define	MMC_SEND_EXT_CSD	8
196 #define	SD_SEND_IF_COND		8
197 #define	MMC_SEND_CSD		9
198 #define	MMC_SEND_CID		10
199 #define	MMC_READ_DAT_UNTIL_STOP	11
200 #define	MMC_STOP_TRANSMISSION	12
201 #define	MMC_SEND_STATUS		13
202 #define	MMC_BUSTEST_R		14
203 #define	MMC_GO_INACTIVE_STATE	15
204 #define	MMC_BUSTEST_W		19
205 
206 /* Class 2: Block oriented read commands */
207 #define	MMC_SET_BLOCKLEN	16
208 #define	MMC_READ_SINGLE_BLOCK	17
209 #define	MMC_READ_MULTIPLE_BLOCK	18
210 			/* reserved: 19 */
211 
212 /* Class 3: Stream write commands */
213 #define	MMC_WRITE_DAT_UNTIL_STOP 20
214 			/* reserved: 21 */
215 			/* reserved: 22 */
216 
217 /* Class 4: Block oriented write commands */
218 #define	MMC_SET_BLOCK_COUNT	23
219 #define	MMC_WRITE_BLOCK		24
220 #define	MMC_WRITE_MULTIPLE_BLOCK 25
221 #define	MMC_PROGARM_CID		26
222 #define	MMC_PROGRAM_CSD		27
223 
224 /* Class 6: Block oriented write protection commands */
225 #define	MMC_SET_WRITE_PROT	28
226 #define	MMC_CLR_WRITE_PROT	29
227 #define	MMC_SEND_WRITE_PROT	30
228 			/* reserved: 31 */
229 
230 /* Class 5: Erase commands */
231 #define	SD_ERASE_WR_BLK_START	32
232 #define	SD_ERASE_WR_BLK_END	33
233 			/* 34 -- reserved old command */
234 #define	MMC_ERASE_GROUP_START	35
235 #define	MMC_ERASE_GROUP_END	36
236 			/* 37 -- reserved old command */
237 #define	MMC_ERASE		38
238 
239 /* Class 9: I/O mode commands */
240 #define	MMC_FAST_IO		39
241 #define	MMC_GO_IRQ_STATE	40
242 			/* reserved: 41 */
243 
244 /* Class 7: Lock card */
245 #define	MMC_LOCK_UNLOCK		42
246 			/* reserved: 43 */
247 			/* reserved: 44 */
248 			/* reserved: 45 */
249 			/* reserved: 46 */
250 			/* reserved: 47 */
251 			/* reserved: 48 */
252 			/* reserved: 49 */
253 			/* reserved: 50 */
254 			/* reserved: 51 */
255 			/* reserved: 54 */
256 
257 /* Class 8: Application specific commands */
258 #define	MMC_APP_CMD		55
259 #define	MMC_GEN_CMD		56
260 			/* reserved: 57 */
261 			/* reserved: 58 */
262 			/* reserved: 59 */
263 			/* reserved for mfg: 60 */
264 			/* reserved for mfg: 61 */
265 			/* reserved for mfg: 62 */
266 			/* reserved for mfg: 63 */
267 
268 /* Class 9: I/O cards (sd) */
269 #define	SD_IO_RW_DIRECT		52
270 #define	SD_IO_RW_EXTENDED	53
271 
272 /* Class 10: Switch function commands */
273 #define	SD_SWITCH_FUNC		6
274 			/* reserved: 34 */
275 			/* reserved: 35 */
276 			/* reserved: 36 */
277 			/* reserved: 37 */
278 			/* reserved: 50 */
279 			/* reserved: 57 */
280 
281 
282 /* Application specific commands for SD */
283 #define	ACMD_SET_BUS_WIDTH	6
284 #define	ACMD_SD_STATUS		13
285 #define	ACMD_SEND_NUM_WR_BLOCKS	22
286 #define	ACMD_SET_WR_BLK_ERASE_COUNT 23
287 #define	ACMD_SD_SEND_OP_COND	41
288 #define	ACMD_SET_CLR_CARD_DETECT 42
289 #define	ACMD_SEND_SCR		51
290 
291 /*
292  * EXT_CSD fields
293  */
294 #define EXT_CSD_ERASE_GRP_DEF	175	/* R/W */
295 #define EXT_CSD_BUS_WIDTH	183	/* R/W */
296 #define EXT_CSD_HS_TIMING	185	/* R/W */
297 #define EXT_CSD_CARD_TYPE	196	/* RO */
298 #define EXT_CSD_REV		192	/* RO */
299 #define EXT_CSD_SEC_CNT		212	/* RO, 4 bytes */
300 #define EXT_CSD_ERASE_TO_MULT	223	/* RO */
301 #define EXT_CSD_ERASE_GRP_SIZE	224	/* RO */
302 
303 /*
304  * EXT_CSD field definitions
305  */
306 #define EXT_CSD_CMD_SET_NORMAL		1
307 #define EXT_CSD_CMD_SET_SECURE		2
308 #define EXT_CSD_CMD_SET_CPSECURE	4
309 
310 #define EXT_CSD_CARD_TYPE_26	1
311 #define EXT_CSD_CARD_TYPE_52	2
312 
313 #define EXT_CSD_BUS_WIDTH_1	0
314 #define EXT_CSD_BUS_WIDTH_4	1
315 #define EXT_CSD_BUS_WIDTH_8	2
316 
317 #define MMC_TYPE_26_MAX_HS	26000000
318 #define MMC_TYPE_52_MAX_HS	52000000
319 
320 /*
321  * SD bus widths
322  */
323 #define SD_BUS_WIDTH_1		0
324 #define SD_BUS_WIDTH_4		2
325 
326 /*
327  * SD Switch
328  */
329 #define SD_SWITCH_MODE_CHECK	0
330 #define SD_SWITCH_MODE_SET	1
331 #define SD_SWITCH_GROUP1	0
332 #define SD_SWITCH_NORMAL_MODE	0
333 #define SD_SWITCH_HS_MODE	1
334 #define SD_SWITCH_NOCHANGE	0xF
335 
336 #define	SD_CLR_CARD_DETECT	0
337 #define	SD_SET_CARD_DETECT	1
338 
339 #define	SD_MAX_HS		50000000
340 
341 /* OCR bits */
342 
343 /*
344  * in SD 2.0 spec, bits 8-14 are now marked reserved
345  * Low voltage in SD2.0 spec is bit 7, TBD voltage
346  * Low voltage in MC 3.31 spec is bit 7, 1.65-1.95V
347  * Specs prior to  MMC 3.31 defined bits 0-7 as voltages down to 1.5V.
348  * 3.31 redefined them to be reserved and also said that cards had to
349  * support the 2.7-3.6V and fixed the OCR to be 0xfff8000 for high voltage
350  * cards.  MMC 4.0 says that a dual voltage card responds with 0xfff8080.
351  * Looks like the fine-grained control of the voltage tolerance ranges
352  * was abandoned.
353  *
354  * The MMC_OCR_CCS appears to be valid for only SD cards.
355  */
356 #define	MMC_OCR_VOLTAGE	0x3fffffffU	/* Vdd Voltage mask */
357 #define	MMC_OCR_LOW_VOLTAGE (1u << 7)	/* Low Voltage Range -- tbd */
358 #define	MMC_OCR_200_210	(1U << 8)	/* Vdd voltage 2.00 ~ 2.10 */
359 #define	MMC_OCR_MIN_VOLTAGE_SHIFT	8
360 #define	MMC_OCR_210_220	(1U << 9)	/* Vdd voltage 2.10 ~ 2.20 */
361 #define	MMC_OCR_220_230	(1U << 10)	/* Vdd voltage 2.20 ~ 2.30 */
362 #define	MMC_OCR_230_240	(1U << 11)	/* Vdd voltage 2.30 ~ 2.40 */
363 #define	MMC_OCR_240_250	(1U << 12)	/* Vdd voltage 2.40 ~ 2.50 */
364 #define	MMC_OCR_250_260	(1U << 13)	/* Vdd voltage 2.50 ~ 2.60 */
365 #define	MMC_OCR_260_270	(1U << 14)	/* Vdd voltage 2.60 ~ 2.70 */
366 #define	MMC_OCR_270_280	(1U << 15)	/* Vdd voltage 2.70 ~ 2.80 */
367 #define	MMC_OCR_280_290	(1U << 16)	/* Vdd voltage 2.80 ~ 2.90 */
368 #define	MMC_OCR_290_300	(1U << 17)	/* Vdd voltage 2.90 ~ 3.00 */
369 #define	MMC_OCR_300_310	(1U << 18)	/* Vdd voltage 3.00 ~ 3.10 */
370 #define	MMC_OCR_310_320	(1U << 19)	/* Vdd voltage 3.10 ~ 3.20 */
371 #define	MMC_OCR_320_330	(1U << 20)	/* Vdd voltage 3.20 ~ 3.30 */
372 #define	MMC_OCR_330_340	(1U << 21)	/* Vdd voltage 3.30 ~ 3.40 */
373 #define	MMC_OCR_340_350	(1U << 22)	/* Vdd voltage 3.40 ~ 3.50 */
374 #define	MMC_OCR_350_360	(1U << 23)	/* Vdd voltage 3.50 ~ 3.60 */
375 #define	MMC_OCR_MAX_VOLTAGE_SHIFT	23
376 #define	MMC_OCR_CCS	(1u << 30)	/* Card Capacity status (SD vs SDHC) */
377 #define	MMC_OCR_CARD_BUSY (1U << 31)	/* Card Power up status */
378 
379 /* CSD -- decoded structure */
380 struct mmc_cid {
381 	uint32_t mid;
382 	char pnm[8];
383 	uint32_t psn;
384 	uint16_t oid;
385 	uint16_t mdt_year;
386 	uint8_t mdt_month;
387 	uint8_t prv;
388 	uint8_t fwrev;
389 };
390 
391 struct mmc_csd
392 {
393 	uint8_t csd_structure;
394 	uint8_t spec_vers;
395 	uint16_t ccc;
396 	uint16_t tacc;
397 	uint32_t nsac;
398 	uint32_t r2w_factor;
399 	uint32_t tran_speed;
400 	uint32_t read_bl_len;
401 	uint32_t write_bl_len;
402 	uint32_t vdd_r_curr_min;
403 	uint32_t vdd_r_curr_max;
404 	uint32_t vdd_w_curr_min;
405 	uint32_t vdd_w_curr_max;
406 	uint32_t wp_grp_size;
407 	uint32_t erase_sector;
408 	uint64_t capacity;
409 	unsigned int read_bl_partial:1,
410 	    read_blk_misalign:1,
411 	    write_bl_partial:1,
412 	    write_blk_misalign:1,
413 	    dsr_imp:1,
414 	    erase_blk_en:1,
415 	    wp_grp_enable:1;
416 };
417 
418 struct mmc_scr
419 {
420 	unsigned char		sda_vsn;
421 	unsigned char		bus_widths;
422 #define SD_SCR_BUS_WIDTH_1	(1<<0)
423 #define SD_SCR_BUS_WIDTH_4	(1<<2)
424 };
425 
426 struct mmc_sd_status
427 {
428 	uint8_t			bus_width;
429 	uint8_t			secured_mode;
430 	uint16_t		card_type;
431 	uint16_t		prot_area;
432 	uint8_t			speed_class;
433 	uint8_t			perf_move;
434 	uint8_t			au_size;
435 	uint16_t		erase_size;
436 	uint8_t			erase_timeout;
437 	uint8_t			erase_offset;
438 };
439 
440 /*
441  * Older versions of the MMC standard had a variable sector size.  However,
442  * I've been able to find no old MMC or SD cards that have a non 512
443  * byte sector size anywhere, so we assume that such cards are very rare
444  * and only note their existance in passing here...
445  */
446 #define MMC_SECTOR_SIZE	512
447 
448 #endif /* DEV_MMCREG_H */
449