xref: /openbmc/linux/drivers/mmc/core/sd_ops.c (revision 9a8f3203)
1 /*
2  *  linux/drivers/mmc/core/sd_ops.h
3  *
4  *  Copyright 2006-2007 Pierre Ossman
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  */
11 
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/export.h>
15 #include <linux/scatterlist.h>
16 
17 #include <linux/mmc/host.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/mmc.h>
20 #include <linux/mmc/sd.h>
21 
22 #include "core.h"
23 #include "sd_ops.h"
24 
25 int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
26 {
27 	int err;
28 	struct mmc_command cmd = {};
29 
30 	if (WARN_ON(card && card->host != host))
31 		return -EINVAL;
32 
33 	cmd.opcode = MMC_APP_CMD;
34 
35 	if (card) {
36 		cmd.arg = card->rca << 16;
37 		cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
38 	} else {
39 		cmd.arg = 0;
40 		cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
41 	}
42 
43 	err = mmc_wait_for_cmd(host, &cmd, 0);
44 	if (err)
45 		return err;
46 
47 	/* Check that card supported application commands */
48 	if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
49 		return -EOPNOTSUPP;
50 
51 	return 0;
52 }
53 EXPORT_SYMBOL_GPL(mmc_app_cmd);
54 
55 static int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
56 				struct mmc_command *cmd)
57 {
58 	struct mmc_request mrq = {};
59 	int i, err = -EIO;
60 
61 	/*
62 	 * We have to resend MMC_APP_CMD for each attempt so
63 	 * we cannot use the retries field in mmc_command.
64 	 */
65 	for (i = 0; i <= MMC_CMD_RETRIES; i++) {
66 		err = mmc_app_cmd(host, card);
67 		if (err) {
68 			/* no point in retrying; no APP commands allowed */
69 			if (mmc_host_is_spi(host)) {
70 				if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
71 					break;
72 			}
73 			continue;
74 		}
75 
76 		memset(&mrq, 0, sizeof(struct mmc_request));
77 
78 		memset(cmd->resp, 0, sizeof(cmd->resp));
79 		cmd->retries = 0;
80 
81 		mrq.cmd = cmd;
82 		cmd->data = NULL;
83 
84 		mmc_wait_for_req(host, &mrq);
85 
86 		err = cmd->error;
87 		if (!cmd->error)
88 			break;
89 
90 		/* no point in retrying illegal APP commands */
91 		if (mmc_host_is_spi(host)) {
92 			if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
93 				break;
94 		}
95 	}
96 
97 	return err;
98 }
99 
100 int mmc_app_set_bus_width(struct mmc_card *card, int width)
101 {
102 	struct mmc_command cmd = {};
103 
104 	cmd.opcode = SD_APP_SET_BUS_WIDTH;
105 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
106 
107 	switch (width) {
108 	case MMC_BUS_WIDTH_1:
109 		cmd.arg = SD_BUS_WIDTH_1;
110 		break;
111 	case MMC_BUS_WIDTH_4:
112 		cmd.arg = SD_BUS_WIDTH_4;
113 		break;
114 	default:
115 		return -EINVAL;
116 	}
117 
118 	return mmc_wait_for_app_cmd(card->host, card, &cmd);
119 }
120 
121 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
122 {
123 	struct mmc_command cmd = {};
124 	int i, err = 0;
125 
126 	cmd.opcode = SD_APP_OP_COND;
127 	if (mmc_host_is_spi(host))
128 		cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
129 	else
130 		cmd.arg = ocr;
131 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
132 
133 	for (i = 100; i; i--) {
134 		err = mmc_wait_for_app_cmd(host, NULL, &cmd);
135 		if (err)
136 			break;
137 
138 		/* if we're just probing, do a single pass */
139 		if (ocr == 0)
140 			break;
141 
142 		/* otherwise wait until reset completes */
143 		if (mmc_host_is_spi(host)) {
144 			if (!(cmd.resp[0] & R1_SPI_IDLE))
145 				break;
146 		} else {
147 			if (cmd.resp[0] & MMC_CARD_BUSY)
148 				break;
149 		}
150 
151 		err = -ETIMEDOUT;
152 
153 		mmc_delay(10);
154 	}
155 
156 	if (!i)
157 		pr_err("%s: card never left busy state\n", mmc_hostname(host));
158 
159 	if (rocr && !mmc_host_is_spi(host))
160 		*rocr = cmd.resp[0];
161 
162 	return err;
163 }
164 
165 int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
166 {
167 	struct mmc_command cmd = {};
168 	int err;
169 	static const u8 test_pattern = 0xAA;
170 	u8 result_pattern;
171 
172 	/*
173 	 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
174 	 * before SD_APP_OP_COND. This command will harmlessly fail for
175 	 * SD 1.0 cards.
176 	 */
177 	cmd.opcode = SD_SEND_IF_COND;
178 	cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
179 	cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
180 
181 	err = mmc_wait_for_cmd(host, &cmd, 0);
182 	if (err)
183 		return err;
184 
185 	if (mmc_host_is_spi(host))
186 		result_pattern = cmd.resp[1] & 0xFF;
187 	else
188 		result_pattern = cmd.resp[0] & 0xFF;
189 
190 	if (result_pattern != test_pattern)
191 		return -EIO;
192 
193 	return 0;
194 }
195 
196 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
197 {
198 	int err;
199 	struct mmc_command cmd = {};
200 
201 	cmd.opcode = SD_SEND_RELATIVE_ADDR;
202 	cmd.arg = 0;
203 	cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
204 
205 	err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
206 	if (err)
207 		return err;
208 
209 	*rca = cmd.resp[0] >> 16;
210 
211 	return 0;
212 }
213 
214 int mmc_app_send_scr(struct mmc_card *card)
215 {
216 	int err;
217 	struct mmc_request mrq = {};
218 	struct mmc_command cmd = {};
219 	struct mmc_data data = {};
220 	struct scatterlist sg;
221 	__be32 *scr;
222 
223 	/* NOTE: caller guarantees scr is heap-allocated */
224 
225 	err = mmc_app_cmd(card->host, card);
226 	if (err)
227 		return err;
228 
229 	/* dma onto stack is unsafe/nonportable, but callers to this
230 	 * routine normally provide temporary on-stack buffers ...
231 	 */
232 	scr = kmalloc(sizeof(card->raw_scr), GFP_KERNEL);
233 	if (!scr)
234 		return -ENOMEM;
235 
236 	mrq.cmd = &cmd;
237 	mrq.data = &data;
238 
239 	cmd.opcode = SD_APP_SEND_SCR;
240 	cmd.arg = 0;
241 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
242 
243 	data.blksz = 8;
244 	data.blocks = 1;
245 	data.flags = MMC_DATA_READ;
246 	data.sg = &sg;
247 	data.sg_len = 1;
248 
249 	sg_init_one(&sg, scr, 8);
250 
251 	mmc_set_data_timeout(&data, card);
252 
253 	mmc_wait_for_req(card->host, &mrq);
254 
255 	card->raw_scr[0] = be32_to_cpu(scr[0]);
256 	card->raw_scr[1] = be32_to_cpu(scr[1]);
257 
258 	kfree(scr);
259 
260 	if (cmd.error)
261 		return cmd.error;
262 	if (data.error)
263 		return data.error;
264 
265 	return 0;
266 }
267 
268 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
269 	u8 value, u8 *resp)
270 {
271 	struct mmc_request mrq = {};
272 	struct mmc_command cmd = {};
273 	struct mmc_data data = {};
274 	struct scatterlist sg;
275 
276 	/* NOTE: caller guarantees resp is heap-allocated */
277 
278 	mode = !!mode;
279 	value &= 0xF;
280 
281 	mrq.cmd = &cmd;
282 	mrq.data = &data;
283 
284 	cmd.opcode = SD_SWITCH;
285 	cmd.arg = mode << 31 | 0x00FFFFFF;
286 	cmd.arg &= ~(0xF << (group * 4));
287 	cmd.arg |= value << (group * 4);
288 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
289 
290 	data.blksz = 64;
291 	data.blocks = 1;
292 	data.flags = MMC_DATA_READ;
293 	data.sg = &sg;
294 	data.sg_len = 1;
295 
296 	sg_init_one(&sg, resp, 64);
297 
298 	mmc_set_data_timeout(&data, card);
299 
300 	mmc_wait_for_req(card->host, &mrq);
301 
302 	if (cmd.error)
303 		return cmd.error;
304 	if (data.error)
305 		return data.error;
306 
307 	return 0;
308 }
309 
310 int mmc_app_sd_status(struct mmc_card *card, void *ssr)
311 {
312 	int err;
313 	struct mmc_request mrq = {};
314 	struct mmc_command cmd = {};
315 	struct mmc_data data = {};
316 	struct scatterlist sg;
317 
318 	/* NOTE: caller guarantees ssr is heap-allocated */
319 
320 	err = mmc_app_cmd(card->host, card);
321 	if (err)
322 		return err;
323 
324 	mrq.cmd = &cmd;
325 	mrq.data = &data;
326 
327 	cmd.opcode = SD_APP_SD_STATUS;
328 	cmd.arg = 0;
329 	cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
330 
331 	data.blksz = 64;
332 	data.blocks = 1;
333 	data.flags = MMC_DATA_READ;
334 	data.sg = &sg;
335 	data.sg_len = 1;
336 
337 	sg_init_one(&sg, ssr, 64);
338 
339 	mmc_set_data_timeout(&data, card);
340 
341 	mmc_wait_for_req(card->host, &mrq);
342 
343 	if (cmd.error)
344 		return cmd.error;
345 	if (data.error)
346 		return data.error;
347 
348 	return 0;
349 }
350