summaryrefslogtreecommitdiffstats
path: root/src/commonlib/include/commonlib/sd_mmc_ctrlr.h
blob: a0b2e2b218379e7848493b0316638af722a9b382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* This file is part of the coreboot project. */
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Controller independent definitions
 */
#ifndef __COMMONLIB_SD_MMC_CTRLR_H__
#define __COMMONLIB_SD_MMC_CTRLR_H__

#include <stdint.h>

/* Error values returned by the storage drivers */
#define CARD_UNUSABLE_ERR	-17 /* Unusable Card */
#define CARD_COMM_ERR		-18 /* Communications Error */
#define CARD_TIMEOUT		-19
#define CARD_IN_PROGRESS	-20 /* operation is in progress */

struct mmc_command {
	uint16_t cmdidx;

/* Common commands */
#define MMC_CMD_GO_IDLE_STATE		0
#define MMC_CMD_SEND_OP_COND		1
#define MMC_CMD_ALL_SEND_CID		2
#define MMC_CMD_SET_DSR			4
#define MMC_CMD_SELECT_CARD		7
#define MMC_CMD_SEND_CSD		9
#define MMC_CMD_SEND_CID		10
#define MMC_CMD_STOP_TRANSMISSION	12
#define MMC_CMD_SEND_STATUS		13
#define MMC_CMD_SET_BLOCKLEN		16
#define MMC_CMD_READ_SINGLE_BLOCK	17
#define MMC_CMD_READ_MULTIPLE_BLOCK	18
#define MMC_CMD_WRITE_SINGLE_BLOCK	24
#define MMC_CMD_WRITE_MULTIPLE_BLOCK	25
#define MMC_CMD_APP_CMD			55

/* MMC specific commands */
#define MMC_CMD_SET_RELATIVE_ADDR	3
#define MMC_CMD_SWITCH			6
#define MMC_CMD_SEND_EXT_CSD		8
#define MMC_CMD_AUTO_TUNING_SEQUENCE	21
#define MMC_CMD_ERASE_GROUP_START	35
#define MMC_CMD_ERASE_GROUP_END		36
#define MMC_CMD_ERASE			38
#define MMC_CMD_SPI_READ_OCR		58
#define MMC_CMD_SPI_CRC_ON_OFF		59

/* SD specific commands */
#define SD_CMD_SEND_RELATIVE_ADDR	3
#define SD_CMD_SWITCH_FUNC		6
#define SD_CMD_SEND_IF_COND		8
#define SD_CMD_ERASE_WR_BLK_START	32
#define SD_CMD_ERASE_WR_BLK_END		33

/* SD specific APP commands */
#define SD_CMD_APP_SET_BUS_WIDTH	6
#define SD_CMD_APP_SEND_OP_COND		41
#define SD_CMD_APP_SEND_SCR		51

	uint32_t resp_type;

#define CARD_RSP_PRESENT (1 << 0)
#define CARD_RSP_136	(1 << 1)		/* 136 bit response */
#define CARD_RSP_CRC	(1 << 2)		/* expect valid crc */
#define CARD_RSP_BUSY	(1 << 3)		/* card may send busy */
#define CARD_RSP_OPCODE	(1 << 4)		/* response contains opcode */

#define CARD_RSP_NONE	(0)
#define CARD_RSP_R1	(CARD_RSP_PRESENT|CARD_RSP_CRC|CARD_RSP_OPCODE)
#define CARD_RSP_R1b	(CARD_RSP_PRESENT|CARD_RSP_CRC|CARD_RSP_OPCODE| \
			CARD_RSP_BUSY)
#define CARD_RSP_R2	(CARD_RSP_PRESENT|CARD_RSP_136|CARD_RSP_CRC)
#define CARD_RSP_R3	(CARD_RSP_PRESENT)
#define CARD_RSP_R4	(CARD_RSP_PRESENT)
#define CARD_RSP_R5	(CARD_RSP_PRESENT|CARD_RSP_CRC|CARD_RSP_OPCODE)
#define CARD_RSP_R6	(CARD_RSP_PRESENT|CARD_RSP_CRC|CARD_RSP_OPCODE)
#define CARD_RSP_R7	(CARD_RSP_PRESENT|CARD_RSP_CRC|CARD_RSP_OPCODE)

	uint32_t cmdarg;

#define MMC_TRIM_ARG			0x1
#define MMC_SECURE_ERASE_ARG		0x80000000

	uint32_t response[4];
	uint32_t flags;

#define CMD_FLAG_IGNORE_INHIBIT	1
};

#define SD_SWITCH_CHECK		0
#define SD_SWITCH_SWITCH	1

#define SD_DATA_4BIT		0x00040000

/* SCR definitions in different words */
#define SD_HIGHSPEED_BUSY	0x00020000
#define SD_HIGHSPEED_SUPPORTED	0x00020000

struct mmc_data {
	union {
		char *dest;
		const char *src;
	};
	uint32_t flags;

#define DATA_FLAG_READ		1
#define DATA_FLAG_WRITE		2

	uint32_t blocks;
	uint32_t blocksize;
};

struct sd_mmc_ctrlr {
	int (*send_cmd)(struct sd_mmc_ctrlr *ctrlr,
		struct mmc_command *cmd, struct mmc_data *data);
	void (*set_ios)(struct sd_mmc_ctrlr *ctrlr);
	void (*tuning_start)(struct sd_mmc_ctrlr *ctrlr, int retune);
	int (*is_tuning_complete)(struct sd_mmc_ctrlr *ctrlr, int *successful);

	int initialized;
	unsigned int version;
	uint32_t voltages;

#define MMC_VDD_165_195		0x00000080	/* VDD voltage 1.65 - 1.95 */
#define MMC_VDD_20_21		0x00000100	/* VDD voltage 2.0 ~ 2.1 */
#define MMC_VDD_21_22		0x00000200	/* VDD voltage 2.1 ~ 2.2 */
#define MMC_VDD_22_23		0x00000400	/* VDD voltage 2.2 ~ 2.3 */
#define MMC_VDD_23_24		0x00000800	/* VDD voltage 2.3 ~ 2.4 */
#define MMC_VDD_24_25		0x00001000	/* VDD voltage 2.4 ~ 2.5 */
#define MMC_VDD_25_26		0x00002000	/* VDD voltage 2.5 ~ 2.6 */
#define MMC_VDD_26_27		0x00004000	/* VDD voltage 2.6 ~ 2.7 */
#define MMC_VDD_27_28		0x00008000	/* VDD voltage 2.7 ~ 2.8 */
#define MMC_VDD_28_29		0x00010000	/* VDD voltage 2.8 ~ 2.9 */
#define MMC_VDD_29_30		0x00020000	/* VDD voltage 2.9 ~ 3.0 */
#define MMC_VDD_30_31		0x00040000	/* VDD voltage 3.0 ~ 3.1 */
#define MMC_VDD_31_32		0x00080000	/* VDD voltage 3.1 ~ 3.2 */
#define MMC_VDD_32_33		0x00100000	/* VDD voltage 3.2 ~ 3.3 */
#define MMC_VDD_33_34		0x00200000	/* VDD voltage 3.3 ~ 3.4 */
#define MMC_VDD_34_35		0x00400000	/* VDD voltage 3.4 ~ 3.5 */
#define MMC_VDD_35_36		0x00800000	/* VDD voltage 3.5 ~ 3.6 */

#define MMC_VDD_165_195_SHIFT   7

	uint32_t clock_base;	/* Controller's base clock */
	uint32_t f_min;
	uint32_t f_max;
	uint32_t request_hz;	/* Desired clock frequency */
	uint32_t bus_hz;	/* Actual bus clock frequency */

#define CLOCK_KHZ		1000
#define CLOCK_MHZ		(1000 * CLOCK_KHZ)
#define CLOCK_20MHZ		(20 * CLOCK_MHZ)
#define CLOCK_25MHZ		(25 * CLOCK_MHZ)
#define CLOCK_26MHZ		(26 * CLOCK_MHZ)
#define CLOCK_50MHZ		(50 * CLOCK_MHZ)
#define CLOCK_52MHZ		(52 * CLOCK_MHZ)
#define CLOCK_200MHZ		(200 * CLOCK_MHZ)

	uint32_t bus_width;
	uint32_t caps;

/* Generic controller & driver capabilities.  Controller specific capabilities
 * start at 0x00010000
 */
#define DRVR_CAP_4BIT				0x00000001
#define DRVR_CAP_8BIT				0x00000002
#define DRVR_CAP_AUTO_CMD12			0x00000004
#define DRVR_CAP_HC				0x00000008
#define DRVR_CAP_HS				0x00000010
#define DRVR_CAP_HS52				0x00000020
#define DRVR_CAP_HS200				0x00000040
#define DRVR_CAP_HS400				0x00000080
#define DRVR_CAP_ENHANCED_STROBE		0x00000100
#define DRVR_CAP_REMOVABLE			0x00000200
#define DRVR_CAP_DMA_64BIT			0x00000400
#define DRVR_CAP_HS200_TUNING			0x00000800

	uint32_t b_max;
	uint32_t timing;

#define BUS_TIMING_LEGACY	0
#define BUS_TIMING_MMC_HS	1
#define BUS_TIMING_SD_HS	2
#define BUS_TIMING_UHS_SDR12	3
#define BUS_TIMING_UHS_SDR25	4
#define BUS_TIMING_UHS_SDR50	5
#define BUS_TIMING_UHS_SDR104	6
#define BUS_TIMING_UHS_DDR50	7
#define BUS_TIMING_MMC_DDR52	8
#define BUS_TIMING_MMC_HS200	9
#define BUS_TIMING_MMC_HS400	10
#define BUS_TIMING_MMC_HS400ES	11

	uint32_t mdelay_before_cmd0;
	uint32_t mdelay_after_cmd0;
	uint32_t udelay_wait_after_cmd;
};

/* SOC specific routine to override ctrlr->caps and .voltages
 *
 * Set/clear the necessary DRVR_CAP_xxx bits in ctrlr->caps to specify the
 * controllers capabilities and driver workarounds.
 *
 * Set/clear the necessary MMC_VDD_xxx bits in ctrlr->voltages to specify the
 * controllers power support.
 */
void soc_sd_mmc_controller_quirks(struct sd_mmc_ctrlr *ctrlr);

/* Optional routines to support logging */
void sdhc_log_command(struct mmc_command *cmd);
void sdhc_log_command_issued(void);
void sdhc_log_response(uint32_t entries, uint32_t *response);
void sdhc_log_ret(int ret);

#endif /* __COMMONLIB_SD_MMC_CTRLR_H__ */