mmcreg.h revision 183468
1163516Simp/*-
2163516Simp * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3163516Simp *
4163516Simp * Redistribution and use in source and binary forms, with or without
5163516Simp * modification, are permitted provided that the following conditions
6163516Simp * are met:
7163516Simp * 1. Redistributions of source code must retain the above copyright
8163516Simp *    notice, this list of conditions and the following disclaimer.
9163516Simp * 2. Redistributions in binary form must reproduce the above copyright
10163516Simp *    notice, this list of conditions and the following disclaimer in the
11163516Simp *    documentation and/or other materials provided with the distribution.
12163516Simp *
13163516Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14163516Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15163516Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16163516Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17163516Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18163516Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19163516Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20163516Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21163516Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22163516Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23163516Simp *
24170002Simp * Portions of this software may have been developed with reference to
25170002Simp * the SD Simplified Specification.  The following disclaimer may apply:
26170002Simp *
27170002Simp * The following conditions apply to the release of the simplified
28170002Simp * specification ("Simplified Specification") by the SD Card Association and
29170002Simp * the SD Group. The Simplified Specification is a subset of the complete SD
30170002Simp * Specification which is owned by the SD Card Association and the SD
31170002Simp * Group. This Simplified Specification is provided on a non-confidential
32170002Simp * basis subject to the disclaimers below. Any implementation of the
33170002Simp * Simplified Specification may require a license from the SD Card
34170002Simp * Association, SD Group, SD-3C LLC or other third parties.
35170002Simp *
36170002Simp * Disclaimers:
37170002Simp *
38170002Simp * The information contained in the Simplified Specification is presented only
39170002Simp * as a standard specification for SD Cards and SD Host/Ancillary products and
40170002Simp * is provided "AS-IS" without any representations or warranties of any
41170002Simp * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
42170002Simp * Card Association for any damages, any infringements of patents or other
43170002Simp * right of the SD Group, SD-3C LLC, the SD Card Association or any third
44170002Simp * parties, which may result from its use. No license is granted by
45170002Simp * implication, estoppel or otherwise under any patent or other rights of the
46170002Simp * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
47170002Simp * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
48170002Simp * or the SD Card Association to disclose or distribute any technical
49170002Simp * information, know-how or other confidential information to any third party.
50170002Simp *
51163516Simp * $FreeBSD: head/sys/dev/mmc/mmcreg.h 183468 2008-09-29 18:17:23Z imp $
52163516Simp */
53163516Simp
54163516Simp#ifndef DEV_MMC_MMCREG_H
55163516Simp#define	DEV_MMC_MMCREG_H
56163516Simp
57163516Simp/*
58163516Simp * This file contains the register definitions for the mmc and sd busses.
59163516Simp * They are taken from publicly available sources.
60163516Simp */
61163516Simp
62163516Simpstruct mmc_data;
63163516Simpstruct mmc_request;
64163516Simp
65163516Simpstruct mmc_command {
66163516Simp	uint32_t	opcode;
67163516Simp	uint32_t	arg;
68163516Simp	uint32_t	resp[4];
69163516Simp	uint32_t	flags;		/* Expected responses */
70163516Simp#define	MMC_RSP_PRESENT	(1ul << 0)	/* Response */
71163516Simp#define	MMC_RSP_136	(1ul << 1)	/* 136 bit response */
72163516Simp#define	MMC_RSP_CRC	(1ul << 2)	/* Expect valid crc */
73163516Simp#define	MMC_RSP_BUSY	(1ul << 3)	/* Card may send busy */
74163516Simp#define	MMC_RSP_OPCODE	(1ul << 4)	/* Response include opcode */
75163516Simp#define	MMC_RSP_MASK	0x1ful
76163516Simp#define	MMC_CMD_AC	(0ul << 5)	/* Addressed Command, no data */
77163516Simp#define	MMC_CMD_ADTC	(1ul << 5)	/* Addressed Data transfer cmd */
78163516Simp#define	MMC_CMD_BC	(2ul << 5)	/* Broadcast command, no response */
79163516Simp#define	MMC_CMD_BCR	(3ul << 5)	/* Broadcast command with response */
80163516Simp#define	MMC_CMD_MASK	(3ul << 5)
81163516Simp
82163516Simp/* Possible response types defined in the standard: */
83163516Simp#define	MMC_RSP_NONE	(0)
84163516Simp#define	MMC_RSP_R1	(MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
85163516Simp#define	MMC_RSP_R1B	(MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE | MMC_RSP_BUSY)
86163516Simp#define	MMC_RSP_R2	(MMC_RSP_PRESENT | MMC_RSP_136 | MMC_RSP_CRC)
87163516Simp#define	MMC_RSP_R3	(MMC_RSP_PRESENT)
88163516Simp#define	MMC_RSP_R6	(MMC_RSP_PRESENT | MMC_RSP_CRC)
89163516Simp/* R7 -- new in sd 2.0 */
90163516Simp#define	MMC_RSP(x)	((x) & MMC_RSP_MASK)
91163516Simp	uint32_t	retries;
92163516Simp	uint32_t	error;
93163516Simp#define	MMC_ERR_NONE	0
94163516Simp#define	MMC_ERR_TIMEOUT	1
95163516Simp#define	MMC_ERR_BADCRC	2
96163516Simp#define	MMC_ERR_FIFO	3
97163516Simp#define	MMC_ERR_FAILED	4
98163516Simp#define	MMC_ERR_INVALID	5
99183468Simp#define	MMC_ERR_NO_MEMORY 6
100163516Simp	struct mmc_data	*data;		/* Data segment with cmd */
101163516Simp	struct mmc_request *mrq;	/* backpointer to request */
102163516Simp};
103163516Simp
104163516Simp/*
105163516Simp * R1 responses
106163516Simp *
107163516Simp * Types (per SD 2.0 standard)
108163516Simp *	e : error bit
109163516Simp *	s : status bit
110163516Simp *	r : detected and set for the actual command response
111163516Simp *	x : Detected and set during command execution.  The host can get
112163516Simp *	    the status by issuing a command with R1 response.
113163516Simp *
114163516Simp * Clear Condition (per SD 2.0 standard)
115163516Simp *	a : according to the card current state.
116163516Simp *	b : always related to the previous command.  reception of a valid
117163516Simp *	    command will clear it (with a delay of one command).
118163516Simp *	c : clear by read
119163516Simp */
120163516Simp#define	R1_OUT_OF_RANGE (1u << 31)		/* erx, c */
121163516Simp#define	R1_ADDRESS_ERROR (1u << 30)		/* erx, c */
122163516Simp#define	R1_BLOCK_LEN_ERROR (1u << 29)		/* erx, c */
123163516Simp#define	R1_ERASE_SEQ_ERROR (1u << 28)		/* er, c */
124163516Simp#define	R1_ERASE_PARAM (1u << 27)		/* erx, c */
125163516Simp#define	R1_WP_VIOLATION (1u << 26)		/* erx, c */
126163516Simp#define	R1_CARD_IS_LOCKED (1u << 25)		/* sx, a */
127163516Simp#define	R1_LOCK_UNLOCK_FAILED (1u << 24)	/* erx, c */
128163516Simp#define	R1_COM_CRC_ERROR (1u << 23)		/* er, b */
129163516Simp#define	R1_ILLEGAL_COMMAND (1u << 22)		/* er, b */
130163516Simp#define	R1_CARD_ECC_FAILED (1u << 21)		/* erx, c */
131163516Simp#define	R1_CC_ERROR (1u << 20)			/* erx, c */
132163516Simp#define	R1_ERROR (1u << 19)			/* erx, c */
133163516Simp#define	R1_CSD_OVERWRITE (1u << 16)		/* erx, c */
134163516Simp#define	R1_WP_ERASE_SKIP (1u << 15)		/* erx, c */
135163516Simp#define	R1_CARD_ECC_DISABLED (1u << 14)		/* sx, a */
136163516Simp#define	R1_ERASE_RESET (1u << 13)		/* sr, c */
137163516Simp#define	R1_CURRENT_STATE_MASK (0xfu << 9)	/* sx, b */
138163516Simp#define	R1_READY_FOR_DATA (1u << 8)		/* sx, a */
139163516Simp#define	R1_APP_CMD (1u << 5)			/* sr, c */
140163516Simp#define	R1_AKE_SEQ_ERROR (1u << 3)		/* er, c */
141163516Simp#define	R1_STATUS(x)            (x & 0xFFFFE000
142163516Simp#define	R1_CURRENT_STATE(x) ((x) & R1_CURRENT_STATE_MASK) >> 9
143163516Simp#define	R1_STATE_IDLE	0
144163516Simp#define	R1_STATE_READY	1
145163516Simp#define	R1_STATE_IDENT	2
146163516Simp#define	R1_STATE_STBY	3
147163516Simp#define	R1_STATE_TRAN	4
148163516Simp#define	R1_STATE_DATA	5
149163516Simp#define	R1_STATE_RCV	6
150163516Simp#define	R1_STATE_PRG	7
151163516Simp#define	R1_STATE_DIS	8
152163516Simp
153163516Simpstruct mmc_data {
154163516Simp	size_t len;		/* size of the data */
155163516Simp	size_t xfer_len;
156163516Simp	void *data;		/* data buffer */
157163516Simp	uint32_t	flags;
158163516Simp#define	MMC_DATA_WRITE	(1UL << 0)
159163516Simp#define	MMC_DATA_READ	(1UL << 1)
160163516Simp#define	MMC_DATA_STREAM	(1UL << 2)
161163516Simp#define	MMC_DATA_MULTI	(1UL << 3)
162163516Simp	struct mmc_request *mrq;
163163516Simp};
164163516Simp
165163516Simpstruct mmc_request {
166163516Simp	struct mmc_command *cmd;
167163516Simp	struct mmc_command *stop;
168163516Simp	void (*done)(struct mmc_request *); /* Completion function */
169163516Simp	void *done_data;		/* requestor set data */
170163516Simp	uint32_t flags;
171163516Simp#define	MMC_REQ_DONE	1
172163516Simp};
173163516Simp
174163516Simp/* Command definitions */
175163516Simp
176163516Simp/* Class 0 and 1: Basic commands & read stream commands */
177163516Simp#define	MMC_GO_IDLE_STATE	0
178163516Simp#define	MMC_SEND_OP_COND	1
179163516Simp#define	MMC_ALL_SEND_CID	2
180163516Simp#define	MMC_SET_RELATIVE_ADDR	3
181163516Simp#define	SD_SEND_RELATIVE_ADDR	3
182163516Simp#define	MMC_SET_DSR		4
183163516Simp			/* reserved: 5 */
184163516Simp#define	MMC_SELECT_CARD		7
185163516Simp#define	MMC_DESELECT_CARD	7
186163516Simp#define	MMC_SEND_IF_COND	8
187163516Simp#define	MMC_SEND_CSD		9
188163516Simp#define	MMC_SEND_CID		10
189163516Simp#define	MMC_READ_DAT_UNTIL_STOP	11
190163516Simp#define	MMC_STOP_TRANSMISSION	12
191163516Simp#define	MMC_SEND_STATUS		13
192163516Simp			/* reserved: 14 */
193163516Simp#define	MMC_GO_INACTIVE_STATE	15
194163516Simp
195163516Simp/* Class 2: Block oriented read commands */
196163516Simp#define	MMC_SET_BLOCKLEN	16
197163516Simp#define	MMC_READ_SINGLE_BLOCK	17
198163516Simp#define	MMC_READ_MULTIPLE_BLOCK	18
199163516Simp			/* reserved: 19 */
200163516Simp
201163516Simp/* Class 3: Stream write commands */
202163516Simp#define	MMC_WRITE_DAT_UNTIL_STOP 20
203163516Simp			/* reserved: 21 */
204163516Simp			/* reserved: 22 */
205163516Simp
206163516Simp/* Class 4: Block oriented write commands */
207163516Simp#define	MMC_SET_BLOCK_COUNT	23
208163516Simp#define	MMC_WRITE_BLOCK		24
209163516Simp#define	MMC_WRITE_MULTIPLE_BLOCK 25
210163516Simp#define	MMC_PROGARM_CID		26
211163516Simp#define	MMC_PROGRAM_CSD		27
212163516Simp
213163516Simp/* Class 6: Block oriented write protection commands */
214163516Simp#define	MMC_SET_WRITE_PROT	28
215163516Simp#define	MMC_CLR_WRITE_PROT	29
216163516Simp#define	MMC_SEND_WRITE_PROT	30
217163516Simp			/* reserved: 31 */
218163516Simp
219163516Simp/* Class 5: Erase commands */
220163516Simp#define	SD_ERASE_WR_BLK_START	32
221163516Simp#define	SD_ERASE_WR_BLK_END	33
222163516Simp			/* 34 -- reserved old command */
223163516Simp#define	MMC_ERASE_GROUP_START	35
224163516Simp#define	MMC_ERASE_GROUP_END	36
225163516Simp			/* 37 -- reserved old command */
226163516Simp#define	MMC_ERASE		38
227163516Simp
228163516Simp/* Class 9: I/O mode commands */
229163516Simp#define	MMC_FAST_IO		39
230163516Simp#define	MMC_GO_IRQ_STATE	40
231163516Simp			/* reserved: 41 */
232163516Simp
233163516Simp/* Class 7: Lock card */
234163516Simp#define	MMC_LOCK_UNLOCK		42
235163516Simp			/* reserved: 43 */
236163516Simp			/* reserved: 44 */
237163516Simp			/* reserved: 45 */
238163516Simp			/* reserved: 46 */
239163516Simp			/* reserved: 47 */
240163516Simp			/* reserved: 48 */
241163516Simp			/* reserved: 49 */
242163516Simp			/* reserved: 50 */
243163516Simp			/* reserved: 51 */
244163516Simp			/* reserved: 54 */
245163516Simp
246163516Simp/* Class 8: Application specific commands */
247163516Simp#define	MMC_APP_CMD		55
248163516Simp#define	MMC_GEN_CMD		56
249163516Simp			/* reserved: 57 */
250163516Simp			/* reserved: 58 */
251163516Simp			/* reserved: 59 */
252163516Simp			/* reserved for mfg: 60 */
253163516Simp			/* reserved for mfg: 61 */
254163516Simp			/* reserved for mfg: 62 */
255163516Simp			/* reserved for mfg: 63 */
256163516Simp
257163516Simp/* Class 9: I/O cards (sd) */
258163516Simp#define	SD_IO_RW_DIRECT		52
259163516Simp#define	SD_IO_RW_EXTENDED	53
260163516Simp
261163516Simp/* Class 10: Switch function commands */
262163516Simp#define	SD_SWITCH_FUNC		6
263163516Simp			/* reserved: 34 */
264163516Simp			/* reserved: 35 */
265163516Simp			/* reserved: 36 */
266163516Simp			/* reserved: 37 */
267163516Simp			/* reserved: 50 */
268163516Simp			/* reserved: 57 */
269163516Simp
270163516Simp
271163516Simp/* Application specific commands for SD */
272163516Simp#define	ACMD_SET_BUS_WIDTH	6
273163516Simp#define	ACMD_SD_STATUS		13
274163516Simp#define	ACMD_SEND_NUM_WR_BLOCKS	22
275163516Simp#define	ACMD_SET_WR_BLK_ERASE_COUNT 23
276163516Simp#define	ACMD_SD_SEND_OP_COND	41
277163516Simp#define	ACMD_SET_CLR_CARD_DETECT 42
278163516Simp#define	ACMD_SEND_SCR		51
279163516Simp
280163516Simp/* OCR bits */
281163516Simp
282163516Simp/*
283163516Simp * in SD 2.0 spec, bits 8-14 are now marked reserved
284163516Simp * Low voltage in SD2.0 spec is bit 7, TBD voltage
285163516Simp * Low voltage in MC 3.31 spec is bit 7, 1.65-1.95V
286163516Simp * Specs prior to  MMC 3.31 defined bits 0-7 as voltages down to 1.5V.
287163516Simp * 3.31 redefined them to be reserved and also said that cards had to
288163516Simp * support the 2.7-3.6V and fixed the OCR to be 0xfff8000 for high voltage
289163516Simp * cards.  MMC 4.0 says that a dual voltage card responds with 0xfff8080.
290163516Simp * Looks like the fine-grained control of the voltage tolerance ranges
291163516Simp * was abandoned.
292163516Simp *
293163516Simp * The MMC_OCR_CCS appears to be valid for only SD cards.
294163516Simp */
295183468Simp#define	MMC_OCR_VOLTAGE	0x3fffffffU	/* Vdd Voltage mask */
296163516Simp#define	MMC_OCR_LOW_VOLTAGE (1u << 7)	/* Low Voltage Range -- tbd */
297163516Simp#define	MMC_OCR_200_210	(1U << 8)	/* Vdd voltage 2.00 ~ 2.10 */
298163516Simp#define	MMC_OCR_210_220	(1U << 9)	/* Vdd voltage 2.10 ~ 2.20 */
299163516Simp#define	MMC_OCR_220_230	(1U << 10)	/* Vdd voltage 2.20 ~ 2.30 */
300163516Simp#define	MMC_OCR_230_240	(1U << 11)	/* Vdd voltage 2.30 ~ 2.40 */
301163516Simp#define	MMC_OCR_240_250	(1U << 12)	/* Vdd voltage 2.40 ~ 2.50 */
302163516Simp#define	MMC_OCR_250_260	(1U << 13)	/* Vdd voltage 2.50 ~ 2.60 */
303163516Simp#define	MMC_OCR_260_270	(1U << 14)	/* Vdd voltage 2.60 ~ 2.70 */
304163516Simp#define	MMC_OCR_270_280	(1U << 15)	/* Vdd voltage 2.70 ~ 2.80 */
305163516Simp#define	MMC_OCR_280_290	(1U << 16)	/* Vdd voltage 2.80 ~ 2.90 */
306163516Simp#define	MMC_OCR_290_300	(1U << 17)	/* Vdd voltage 2.90 ~ 3.00 */
307163516Simp#define	MMC_OCR_300_310	(1U << 18)	/* Vdd voltage 3.00 ~ 3.10 */
308163516Simp#define	MMC_OCR_310_320	(1U << 19)	/* Vdd voltage 3.10 ~ 3.20 */
309163516Simp#define	MMC_OCR_320_330	(1U << 20)	/* Vdd voltage 3.20 ~ 3.30 */
310163516Simp#define	MMC_OCR_330_340	(1U << 21)	/* Vdd voltage 3.30 ~ 3.40 */
311163516Simp#define	MMC_OCR_340_350	(1U << 22)	/* Vdd voltage 3.40 ~ 3.50 */
312163516Simp#define	MMC_OCR_350_360	(1U << 23)	/* Vdd voltage 3.50 ~ 3.60 */
313163516Simp#define	MMC_OCR_CCS	(1u << 30)	/* Card Capacity status (SD vs SDHC) */
314163516Simp#define	MMC_OCR_CARD_BUSY (1U << 31)	/* Card Power up status */
315163516Simp
316163516Simp/* CSD -- decoded structure */
317163516Simpstruct mmc_cid {
318163516Simp	uint32_t mid;
319163516Simp	char pnm[8];
320163516Simp	uint32_t psn;
321163516Simp	uint16_t oid;
322163516Simp	uint16_t mdt_year;
323163516Simp	uint8_t mdt_month;
324163516Simp	uint8_t prv;
325163516Simp	uint8_t fwrev;
326163516Simp};
327163516Simp
328163516Simpstruct mmc_csd
329163516Simp{
330163516Simp	uint8_t csd_structure;
331163516Simp	uint16_t ccc;
332163516Simp	uint16_t tacc;
333163516Simp	uint32_t nsac;
334163516Simp	uint32_t r2w_factor;
335163516Simp	uint32_t tran_speed;
336163516Simp	uint32_t read_bl_len;
337163516Simp	uint32_t write_bl_len;
338163516Simp	uint32_t vdd_r_curr_min;
339163516Simp	uint32_t vdd_r_curr_max;
340163516Simp	uint32_t vdd_w_curr_min;
341163516Simp	uint32_t vdd_w_curr_max;
342163516Simp	uint32_t wp_grp_size;
343163516Simp	uint32_t sector_size;	/* Erase sector size! */
344163516Simp	uint64_t capacity;
345163516Simp	unsigned int read_bl_partial:1,
346163516Simp	    read_blk_misalign:1,
347163516Simp	    write_bl_partial:1,
348163516Simp	    write_blk_misalign:1,
349163516Simp	    dsr_imp:1,
350163516Simp	    erase_blk_en:1,
351163516Simp	    wp_grp_enable:1;
352163516Simp};
353163516Simp
354163516Simp#endif /* DEV_MMCREG_H */
355