Home | History | Annotate | Line # | Download | only in sdmmc
sdmmcreg.h revision 1.1
      1 /*	$NetBSD: sdmmcreg.h,v 1.1 2009/04/21 03:00:31 nonaka Exp $	*/
      2 /*	$OpenBSD: sdmmcreg.h,v 1.4 2009/01/09 10:55:22 jsg Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #ifndef	_SDMMCREG_H_
     21 #define	_SDMMCREG_H_
     22 
     23 /* MMC commands */				/* response type */
     24 #define MMC_GO_IDLE_STATE		0	/* R0 */
     25 #define MMC_SEND_OP_COND		1	/* R3 */
     26 #define MMC_ALL_SEND_CID		2	/* R2 */
     27 #define MMC_SET_RELATIVE_ADDR   	3	/* R1 */
     28 #define MMC_SELECT_CARD			7	/* R1 */
     29 #define MMC_SEND_CSD			9	/* R2 */
     30 #define MMC_STOP_TRANSMISSION		12	/* R1b */
     31 #define MMC_SEND_STATUS			13	/* R1 */
     32 #define MMC_INACTIVE_STATE		15	/* R0 */
     33 #define MMC_SET_BLOCKLEN		16	/* R1 */
     34 #define MMC_READ_BLOCK_SINGLE		17	/* R1 */
     35 #define MMC_READ_BLOCK_MULTIPLE		18	/* R1 */
     36 #define MMC_SET_BLOCK_COUNT		23	/* R1 */
     37 #define MMC_WRITE_BLOCK_SINGLE		24	/* R1 */
     38 #define MMC_WRITE_BLOCK_MULTIPLE	25	/* R1 */
     39 #define	MMC_PROGRAM_CSD			27	/* R1 */
     40 #define	MMC_SET_WRITE_PROT		28	/* R1b */
     41 #define	MMC_SET_CLR_WRITE_PROT		29	/* R1b */
     42 #define	MMC_SET_SEND_WRITE_PROT		30	/* R1 */
     43 #define	MMC_TAG_SECTOR_START		32	/* R1 */
     44 #define	MMC_TAG_SECTOR_END		33	/* R1 */
     45 #define	MMC_UNTAG_SECTOR		34	/* R1 */
     46 #define	MMC_TAG_ERASE_GROUP_START	35	/* R1 */
     47 #define	MMC_TAG_ERASE_GROUP_END		36	/* R1 */
     48 #define	MMC_UNTAG_ERASE_GROUP		37	/* R1 */
     49 #define	MMC_ERASE			38	/* R1b */
     50 #define	MMC_LOCK_UNLOCK			42	/* R1b */
     51 #define MMC_APP_CMD			55	/* R1 */
     52 
     53 /* SD commands */				/* response type */
     54 #define SD_SEND_RELATIVE_ADDR		3	/* R6 */
     55 #define SD_SEND_IF_COND			8	/* R7 */
     56 
     57 /* SD application commands */			/* response type */
     58 #define SD_APP_SET_BUS_WIDTH		6	/* R1 */
     59 #define SD_APP_OP_COND			41	/* R3 */
     60 #define	SD_APP_SEND_SCR			51	/* R1 */
     61 
     62 /* OCR bits */
     63 #define MMC_OCR_MEM_READY		(1U<<31)/* memory power-up status bit */
     64 #define	MMC_OCR_HCS			(1<<30)
     65 #define MMC_OCR_3_5V_3_6V		(1<<23)
     66 #define MMC_OCR_3_4V_3_5V		(1<<22)
     67 #define MMC_OCR_3_3V_3_4V		(1<<21)
     68 #define MMC_OCR_3_2V_3_3V		(1<<20)
     69 #define MMC_OCR_3_1V_3_2V		(1<<19)
     70 #define MMC_OCR_3_0V_3_1V		(1<<18)
     71 #define MMC_OCR_2_9V_3_0V		(1<<17)
     72 #define MMC_OCR_2_8V_2_9V		(1<<16)
     73 #define MMC_OCR_2_7V_2_8V		(1<<15)
     74 #define MMC_OCR_2_6V_2_7V		(1<<14)
     75 #define MMC_OCR_2_5V_2_6V		(1<<13)
     76 #define MMC_OCR_2_4V_2_5V		(1<<12)
     77 #define MMC_OCR_2_3V_2_4V		(1<<11)
     78 #define MMC_OCR_2_2V_2_3V		(1<<10)
     79 #define MMC_OCR_2_1V_2_2V		(1<<9)
     80 #define MMC_OCR_2_0V_2_1V		(1<<8)
     81 #define MMC_OCR_1_9V_2_0V		(1<<7)
     82 #define MMC_OCR_1_8V_1_9V		(1<<6)
     83 #define MMC_OCR_1_7V_1_8V		(1<<5)
     84 #define MMC_OCR_1_6V_1_7V		(1<<4)
     85 
     86 /* R1 response type bits */
     87 #define MMC_R1_READY_FOR_DATA		(1<<8)	/* ready for next transfer */
     88 #define MMC_R1_APP_CMD			(1<<5)	/* app. commands supported */
     89 
     90 /* 48-bit response decoding (32 bits w/o CRC) */
     91 #define MMC_R1(resp)			((resp)[0])
     92 #define MMC_R3(resp)			((resp)[0])
     93 #define SD_R6(resp)			((resp)[0])
     94 #define MMC_R7(resp)			((resp)[0])
     95 
     96 /* RCA argument and response */
     97 #define MMC_ARG_RCA(rca)		((rca) << 16)
     98 #define SD_R6_RCA(resp)			(SD_R6((resp)) >> 16)
     99 
    100 /* bus width argument */
    101 #define SD_ARG_BUS_WIDTH_1		0
    102 #define SD_ARG_BUS_WIDTH_4		2
    103 
    104 /* MMC R2 response (CSD) */
    105 #define MMC_CSD_CSDVER(resp)		MMC_RSP_BITS((resp), 126, 2)
    106 #define  MMC_CSD_CSDVER_1_0		1
    107 #define  MMC_CSD_CSDVER_2_0		2
    108 #define MMC_CSD_MMCVER(resp)		MMC_RSP_BITS((resp), 122, 4)
    109 #define  MMC_CSD_MMCVER_1_0		0 /* MMC 1.0 - 1.2 */
    110 #define  MMC_CSD_MMCVER_1_4		1 /* MMC 1.4 */
    111 #define  MMC_CSD_MMCVER_2_0		2 /* MMC 2.0 - 2.2 */
    112 #define  MMC_CSD_MMCVER_3_1		3 /* MMC 3.1 - 3.3 */
    113 #define  MMC_CSD_MMCVER_4_0		4 /* MMC 4 */
    114 #define MMC_CSD_TAAC(resp)		MMC_RSP_BITS((resp), 112, 8)
    115 #define MMC_CSD_TAAC_MANT(resp)		MMC_RSP_BITS((resp), 115, 4)
    116 #define MMC_CSD_TAAC_EXP(resp)		MMC_RSP_BITS((resp), 112, 3)
    117 #define MMC_CSD_NSAC(resp)		MMC_RSP_BITS((resp), 104, 8)
    118 #define MMC_CSD_TRAN_SPEED(resp)	MMC_RSP_BITS((resp), 96, 8)
    119 #define MMC_CSD_TRAN_SPEED_MANT(resp)	MMC_RSP_BITS((resp), 99, 4)
    120 #define MMC_CSD_TRAN_SPEED_EXP(resp)	MMC_RSP_BITS((resp), 96, 3)
    121 #define MMC_CSD_READ_BL_LEN(resp)	MMC_RSP_BITS((resp), 80, 4)
    122 #define MMC_CSD_C_SIZE(resp)		MMC_RSP_BITS((resp), 62, 12)
    123 #define MMC_CSD_CAPACITY(resp)		((MMC_CSD_C_SIZE((resp))+1) << \
    124 					 (MMC_CSD_C_SIZE_MULT((resp))+2))
    125 #define MMC_CSD_C_SIZE_MULT(resp)	MMC_RSP_BITS((resp), 47, 3)
    126 #define MMC_CSD_R2W_FACTOR(resp)	MMC_RSP_BITS((resp), 26, 3)
    127 #define MMC_CSD_WRITE_BL_LEN(resp)	MMC_RSP_BITS((resp), 22, 4)
    128 
    129 /* MMC v1 R2 response (CID) */
    130 #define MMC_CID_MID_V1(resp)		MMC_RSP_BITS((resp), 104, 24)
    131 #define MMC_CID_PNM_V1_CPY(resp, pnm)					\
    132 	do {								\
    133 		(pnm)[0] = MMC_RSP_BITS((resp), 96, 8);			\
    134 		(pnm)[1] = MMC_RSP_BITS((resp), 88, 8);			\
    135 		(pnm)[2] = MMC_RSP_BITS((resp), 80, 8);			\
    136 		(pnm)[3] = MMC_RSP_BITS((resp), 72, 8);			\
    137 		(pnm)[4] = MMC_RSP_BITS((resp), 64, 8);			\
    138 		(pnm)[5] = MMC_RSP_BITS((resp), 56, 8);			\
    139 		(pnm)[6] = MMC_RSP_BITS((resp), 48, 8);			\
    140 		(pnm)[7] = '\0';					\
    141 	} while (/*CONSTCOND*/0)
    142 #define MMC_CID_REV_V1(resp)		MMC_RSP_BITS((resp), 40, 8)
    143 #define MMC_CID_PSN_V1(resp)		MMC_RSP_BITS((resp), 16, 24)
    144 #define MMC_CID_MDT_V1(resp)		MMC_RSP_BITS((resp), 8, 8)
    145 
    146 /* MMC v2 R2 response (CID) */
    147 #define MMC_CID_MID_V2(resp)		MMC_RSP_BITS((resp), 120, 8)
    148 #define MMC_CID_OID_V2(resp)		MMC_RSP_BITS((resp), 104, 16)
    149 #define MMC_CID_PNM_V2_CPY(resp, pnm)					\
    150 	do {								\
    151 		(pnm)[0] = MMC_RSP_BITS((resp), 96, 8);			\
    152 		(pnm)[1] = MMC_RSP_BITS((resp), 88, 8);			\
    153 		(pnm)[2] = MMC_RSP_BITS((resp), 80, 8);			\
    154 		(pnm)[3] = MMC_RSP_BITS((resp), 72, 8);			\
    155 		(pnm)[4] = MMC_RSP_BITS((resp), 64, 8);			\
    156 		(pnm)[5] = MMC_RSP_BITS((resp), 56, 8);			\
    157 		(pnm)[6] = '\0';					\
    158 	} while (/*CONSTCOND*/0)
    159 #define MMC_CID_PSN_V2(resp)		MMC_RSP_BITS((resp), 16, 32)
    160 
    161 /* SD R2 response (CSD) */
    162 #define SD_CSD_CSDVER(resp)		MMC_RSP_BITS((resp), 126, 2)
    163 #define  SD_CSD_CSDVER_1_0		0
    164 #define  SD_CSD_CSDVER_2_0		1
    165 #define SD_CSD_MMCVER(resp)		MMC_RSP_BITS((resp), 122, 4)
    166 #define SD_CSD_TAAC(resp)		MMC_RSP_BITS((resp), 112, 8)
    167 #define SD_CSD_TAAC_EXP(resp)		MMC_RSP_BITS((resp), 115, 4)
    168 #define SD_CSD_TAAC_MANT(resp)		MMC_RSP_BITS((resp), 112, 3)
    169 #define  SD_CSD_TAAC_1_5_MSEC		0x26
    170 #define SD_CSD_NSAC(resp)		MMC_RSP_BITS((resp), 104, 8)
    171 #define SD_CSD_SPEED(resp)		MMC_RSP_BITS((resp), 96, 8)
    172 #define SD_CSD_SPEED_MANT(resp)		MMC_RSP_BITS((resp), 99, 4)
    173 #define SD_CSD_SPEED_EXP(resp)		MMC_RSP_BITS((resp), 96, 3)
    174 #define  SD_CSD_SPEED_25_MHZ		0x32
    175 #define  SD_CSD_SPEED_50_MHZ		0x5a
    176 #define SD_CSD_CCC(resp)		MMC_RSP_BITS((resp), 84, 12)
    177 #define  SD_CSD_CCC_ALL			0x5f5
    178 #define SD_CSD_READ_BL_LEN(resp)	MMC_RSP_BITS((resp), 80, 4)
    179 #define SD_CSD_READ_BL_PARTIAL(resp)	MMC_RSP_BITS((resp), 79, 1)
    180 #define SD_CSD_WRITE_BLK_MISALIGN(resp)	MMC_RSP_BITS((resp), 78, 1)
    181 #define SD_CSD_READ_BLK_MISALIGN(resp)	MMC_RSP_BITS((resp), 77, 1)
    182 #define SD_CSD_DSR_IMP(resp)		MMC_RSP_BITS((resp), 76, 1)
    183 #define SD_CSD_C_SIZE(resp)		MMC_RSP_BITS((resp), 62, 12)
    184 #define SD_CSD_CAPACITY(resp)		((SD_CSD_C_SIZE((resp))+1) << \
    185 					 (SD_CSD_C_SIZE_MULT((resp))+2))
    186 #define SD_CSD_VDD_R_CURR_MIN(resp)	MMC_RSP_BITS((resp), 59, 3)
    187 #define SD_CSD_VDD_R_CURR_MAX(resp)	MMC_RSP_BITS((resp), 56, 3)
    188 #define SD_CSD_VDD_W_CURR_MIN(resp)	MMC_RSP_BITS((resp), 53, 3)
    189 #define SD_CSD_VDD_W_CURR_MAX(resp)	MMC_RSP_BITS((resp), 50, 3)
    190 #define  SD_CSD_VDD_RW_CURR_100mA	0x7
    191 #define  SD_CSD_VDD_RW_CURR_80mA	0x6
    192 #define SD_CSD_V2_C_SIZE(resp)		MMC_RSP_BITS((resp), 48, 22)
    193 #define SD_CSD_V2_CAPACITY(resp)	((SD_CSD_V2_C_SIZE((resp))+1) << 10)
    194 #define SD_CSD_V2_BL_LEN		0x9 /* 512 */
    195 #define SD_CSD_C_SIZE_MULT(resp)	MMC_RSP_BITS((resp), 47, 3)
    196 #define SD_CSD_ERASE_BLK_EN(resp)	MMC_RSP_BITS((resp), 46, 1)
    197 #define SD_CSD_SECTOR_SIZE(resp)	MMC_RSP_BITS((resp), 39, 7) /* +1 */
    198 #define SD_CSD_WP_GRP_SIZE(resp)	MMC_RSP_BITS((resp), 32, 7) /* +1 */
    199 #define SD_CSD_WP_GRP_ENABLE(resp)	MMC_RSP_BITS((resp), 31, 1)
    200 #define SD_CSD_R2W_FACTOR(resp)		MMC_RSP_BITS((resp), 26, 3)
    201 #define SD_CSD_WRITE_BL_LEN(resp)	MMC_RSP_BITS((resp), 22, 4)
    202 #define  SD_CSD_RW_BL_LEN_2G		0xa
    203 #define  SD_CSD_RW_BL_LEN_1G		0x9
    204 #define SD_CSD_WRITE_BL_PARTIAL(resp)	MMC_RSP_BITS((resp), 21, 1)
    205 #define SD_CSD_FILE_FORMAT_GRP(resp)	MMC_RSP_BITS((resp), 15, 1)
    206 #define SD_CSD_COPY(resp)		MMC_RSP_BITS((resp), 14, 1)
    207 #define SD_CSD_PERM_WRITE_PROTECT(resp)	MMC_RSP_BITS((resp), 13, 1)
    208 #define SD_CSD_TMP_WRITE_PROTECT(resp)	MMC_RSP_BITS((resp), 12, 1)
    209 #define SD_CSD_FILE_FORMAT(resp)	MMC_RSP_BITS((resp), 10, 2)
    210 
    211 /* SD R2 response (CID) */
    212 #define SD_CID_MID(resp)		MMC_RSP_BITS((resp), 120, 8)
    213 #define SD_CID_OID(resp)		MMC_RSP_BITS((resp), 104, 16)
    214 #define SD_CID_PNM_CPY(resp, pnm)					\
    215 	do {								\
    216 		(pnm)[0] = MMC_RSP_BITS((resp), 96, 8);			\
    217 		(pnm)[1] = MMC_RSP_BITS((resp), 88, 8);			\
    218 		(pnm)[2] = MMC_RSP_BITS((resp), 80, 8);			\
    219 		(pnm)[3] = MMC_RSP_BITS((resp), 72, 8);			\
    220 		(pnm)[4] = MMC_RSP_BITS((resp), 64, 8);			\
    221 		(pnm)[5] = '\0';					\
    222 	} while (/*CONSTCOND*/0)
    223 #define SD_CID_REV(resp)		MMC_RSP_BITS((resp), 56, 8)
    224 #define SD_CID_PSN(resp)		MMC_RSP_BITS((resp), 24, 32)
    225 #define SD_CID_MDT(resp)		MMC_RSP_BITS((resp), 8, 12)
    226 
    227 /* SCR (SD Configuration Register) */
    228 #define	SCR_STRUCTURE(scr)		MMC_RSP_BITS((scr), 60, 4)
    229 #define	 SCR_STRUCTURE_VER_1_0		0 /* Version 1.0 */
    230 #define	SCR_SD_SPEC(scr)		MMC_RSP_BITS((scr), 56, 4)
    231 #define	 SCR_SD_SPEC_VER_1_0		0 /* Version 1.0 */
    232 #define	SCR_DATA_STAT_AFTER_ERASE(scr)	MMC_RSP_BITS((scr), 55, 1)
    233 #define	SCR_SD_SECURITY(scr)		MMC_RSP_BITS((scr), 52, 3)
    234 #define	 SCR_SD_SECURITY_NONE		0 /* no security */
    235 #define	 SCR_SD_SECURITY_1_0		1 /* security protocol 1.0 */
    236 #define	 SCR_SD_SECURITY_1_0_2		2 /* security protocol 1.0 */
    237 #define	SCR_SD_BUS_WIDTHS(scr)		MMC_RSP_BITS((scr), 48, 4)
    238 #define	 SCR_SD_BUS_WIDTHS_1BIT		(1 << 0) /* 1bit (DAT0) */
    239 #define	 SCR_SD_BUS_WIDTHS_4BIT		(1 << 2) /* 4bit (DAT0-3) */
    240 #define	SCR_RESERVED(scr)		MMC_RSP_BITS((scr), 32, 16)
    241 #define	SCR_RESERVED2(scr)		MMC_RSP_BITS((scr), 0, 32)
    242 
    243 /* Might be slow, but it should work on big and little endian systems. */
    244 #define MMC_RSP_BITS(resp, start, len)	__bitfield((resp), (start)-8, (len))
    245 static inline int
    246 __bitfield(uint32_t *src, int start, int len)
    247 {
    248 	uint8_t *sp;
    249 	uint32_t dst, mask;
    250 	int shift, bs, bc;
    251 
    252 	if (start < 0 || len < 0 || len > 32)
    253 		return 0;
    254 
    255 	dst = 0;
    256 	mask = len % 32 ? UINT_MAX >> (32 - (len % 32)) : UINT_MAX;
    257 	shift = 0;
    258 
    259 	while (len > 0) {
    260 		sp = (uint8_t *)src + start / 8;
    261 		bs = start % 8;
    262 		bc = 8 - bs;
    263 		if (bc > len)
    264 			bc = len;
    265 		dst |= (*sp++ >> bs) << shift;
    266 		shift += bc;
    267 		start += bc;
    268 		len -= bc;
    269 	}
    270 
    271 	dst &= mask;
    272 	return (int)dst;
    273 }
    274 
    275 #endif	/* _SDMMCREG_H_ */
    276