xref: /linux/drivers/mmc/core/sd_ops.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  linux/drivers/mmc/core/sd_ops.h
4  *
5  *  Copyright 2006-2007 Pierre Ossman
6  */
7 
8 #include <linux/slab.h>
9 #include <linux/types.h>
10 #include <linux/export.h>
11 #include <linux/scatterlist.h>
12 
13 #include <linux/mmc/host.h>
14 #include <linux/mmc/card.h>
15 #include <linux/mmc/mmc.h>
16 #include <linux/mmc/sd.h>
17 
18 #include "core.h"
19 #include "card.h"
20 #include "sd_ops.h"
21 #include "mmc_ops.h"
22 
23 /*
24  * Extensive testing has shown that some specific SD cards
25  * require an increased command timeout to be successfully
26  * initialized.
27  */
28 #define SD_APP_OP_COND_PERIOD_US	(10 * 1000) /* 10ms */
29 #define SD_APP_OP_COND_TIMEOUT_MS	2000 /* 2s */
30 
31 struct sd_app_op_cond_busy_data {
32 	struct mmc_host *host;
33 	u32 ocr;
34 	struct mmc_command *cmd;
35 };
36 
37 int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
38 {
39 	int err;
40 	struct mmc_command cmd = {};
41 
42 	if (WARN_ON(card && card->host != host))
43 		return -EINVAL;
44 
45 	/*
46 	 * UHS2 packet has APP bit so only set APP_CMD flag here.
47 	 * Will set the APP bit when assembling UHS2 packet.
48 	 */
49 	if (host->uhs2_sd_tran) {
50 		host->uhs2_app_cmd = true;
51 		return 0;
52 	}
53 
54 	cmd.opcode = MMC_APP_CMD;
55 
56 	if (card) {
57 		cmd.arg = card->rca << 16;
58 		cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
59 	} else {
60 		cmd.arg = 0;
61 		cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
62 	}
63 
64 	err = mmc_wait_for_cmd(host, &cmd, 0);
65 	if (err)
66 		return err;
67 
68 	/* Check that card supported application commands */
69 	if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
70 		return -EOPNOTSUPP;
71 
72 	return 0;
73 }
74 EXPORT_SYMBOL_GPL(mmc_app_cmd);
75 
76 static int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
77 				struct mmc_command *cmd)
78 {
79 	struct mmc_request mrq = {};
80 	int i, err = -EIO;
81 
82 	/*
83 	 * We have to resend MMC_APP_CMD for each attempt so
84 	 * we cannot use the retries field in mmc_command.
85 	 */
86 	for (i = 0; i <= MMC_CMD_RETRIES; i++) {
87 		err = mmc_app_cmd(host, card);
88 		if (err) {
89 			/* no point in retrying; no APP commands allowed */
90 			if (mmc_host_is_spi(host)) {
91 				if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
92 					break;
93 			}
94 			continue;
95 		}
96 
97 		memset(&mrq, 0, sizeof(struct mmc_request));
98 
99 		memset(cmd->resp, 0, sizeof(cmd->resp));
100 		cmd->retries = 0;
101 
102 		mrq.cmd = cmd;
103 		cmd->data = NULL;
104 
105 		mmc_wait_for_req(host, &mrq);
106 
107 		err = cmd->error;
108 		if (!cmd->error)
109 			break;
110 
111 		/* no point in retrying illegal APP commands */
112 		if (mmc_host_is_spi(host)) {
113 			if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
114 				break;
115 		}
116 	}
117 
118 	return err;
119 }
120 
121 int mmc_app_set_bus_width(struct mmc_card *card, int width)
122 {
123 	struct mmc_command cmd = {};
124 
125 	cmd.opcode = SD_APP_SET_BUS_WIDTH;
126 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
127 
128 	switch (width) {
129 	case MMC_BUS_WIDTH_1:
130 		cmd.arg = SD_BUS_WIDTH_1;
131 		break;
132 	case MMC_BUS_WIDTH_4:
133 		cmd.arg = SD_BUS_WIDTH_4;
134 		break;
135 	default:
136 		return -EINVAL;
137 	}
138 
139 	return mmc_wait_for_app_cmd(card->host, card, &cmd);
140 }
141 
142 static int sd_app_op_cond_cb(void *cb_data, bool *busy)
143 {
144 	struct sd_app_op_cond_busy_data *data = cb_data;
145 	struct mmc_host *host = data->host;
146 	struct mmc_command *cmd = data->cmd;
147 	u32 ocr = data->ocr;
148 	int err;
149 
150 	*busy = false;
151 
152 	err = mmc_wait_for_app_cmd(host, NULL, cmd);
153 	if (err)
154 		return err;
155 
156 	/* If we're just probing, do a single pass. */
157 	if (ocr == 0)
158 		return 0;
159 
160 	/* Wait until reset completes. */
161 	if (mmc_host_is_spi(host)) {
162 		if (!(cmd->resp[0] & R1_SPI_IDLE))
163 			return 0;
164 	} else if (cmd->resp[0] & MMC_CARD_BUSY) {
165 		return 0;
166 	}
167 
168 	*busy = true;
169 	return 0;
170 }
171 
172 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
173 {
174 	struct mmc_command cmd = {};
175 	struct sd_app_op_cond_busy_data cb_data = {
176 		.host = host,
177 		.ocr = ocr,
178 		.cmd = &cmd
179 	};
180 	int err;
181 
182 	cmd.opcode = SD_APP_OP_COND;
183 	if (mmc_host_is_spi(host))
184 		cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
185 	else
186 		cmd.arg = ocr;
187 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
188 
189 	err = __mmc_poll_for_busy(host, SD_APP_OP_COND_PERIOD_US,
190 				  SD_APP_OP_COND_TIMEOUT_MS, &sd_app_op_cond_cb,
191 				  &cb_data);
192 	if (err)
193 		return err;
194 
195 	if (rocr && !mmc_host_is_spi(host))
196 		*rocr = cmd.resp[0];
197 
198 	return 0;
199 }
200 
201 int mmc_send_ext_addr(struct mmc_host *host, u32 addr)
202 {
203 	struct mmc_command cmd = {
204 		.opcode = SD_ADDR_EXT,
205 		.arg = addr,
206 		.flags = MMC_RSP_R1 | MMC_CMD_AC,
207 	};
208 
209 	if (!mmc_card_ult_capacity(host->card))
210 		return 0;
211 
212 	return mmc_wait_for_cmd(host, &cmd, 0);
213 }
214 
215 static int __mmc_send_if_cond(struct mmc_host *host, u32 ocr, u8 pcie_bits,
216 			      u32 *resp)
217 {
218 	struct mmc_command cmd = {};
219 	int err;
220 	static const u8 test_pattern = 0xAA;
221 	u8 result_pattern;
222 
223 	/*
224 	 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
225 	 * before SD_APP_OP_COND. This command will harmlessly fail for
226 	 * SD 1.0 cards.
227 	 */
228 	cmd.opcode = SD_SEND_IF_COND;
229 	cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | pcie_bits << 8 | test_pattern;
230 	cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
231 
232 	err = mmc_wait_for_cmd(host, &cmd, 0);
233 	if (err)
234 		return err;
235 
236 	if (mmc_host_is_spi(host))
237 		result_pattern = cmd.resp[1] & 0xFF;
238 	else
239 		result_pattern = cmd.resp[0] & 0xFF;
240 
241 	if (result_pattern != test_pattern)
242 		return -EIO;
243 
244 	if (resp)
245 		*resp = cmd.resp[0];
246 
247 	return 0;
248 }
249 
250 int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
251 {
252 	return __mmc_send_if_cond(host, ocr, 0, NULL);
253 }
254 
255 int mmc_send_if_cond_pcie(struct mmc_host *host, u32 ocr)
256 {
257 	u32 resp = 0;
258 	u8 pcie_bits = 0;
259 	int ret;
260 
261 	if (host->caps2 & MMC_CAP2_SD_EXP) {
262 		/* Probe card for SD express support via PCIe. */
263 		pcie_bits = 0x10;
264 		if (host->caps2 & MMC_CAP2_SD_EXP_1_2V)
265 			/* Probe also for 1.2V support. */
266 			pcie_bits = 0x30;
267 	}
268 
269 	ret = __mmc_send_if_cond(host, ocr, pcie_bits, &resp);
270 	if (ret)
271 		return 0;
272 
273 	/* Continue with the SD express init, if the card supports it. */
274 	resp &= 0x3000;
275 	if (pcie_bits && resp) {
276 		if (resp == 0x3000)
277 			host->ios.timing = MMC_TIMING_SD_EXP_1_2V;
278 		else
279 			host->ios.timing = MMC_TIMING_SD_EXP;
280 
281 		/*
282 		 * According to the spec the clock shall also be gated, but
283 		 * let's leave this to the host driver for more flexibility.
284 		 */
285 		return host->ops->init_sd_express(host, &host->ios);
286 	}
287 
288 	return 0;
289 }
290 
291 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
292 {
293 	int err;
294 	struct mmc_command cmd = {};
295 
296 	cmd.opcode = SD_SEND_RELATIVE_ADDR;
297 	cmd.arg = 0;
298 	cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
299 
300 	err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
301 	if (err)
302 		return err;
303 
304 	*rca = cmd.resp[0] >> 16;
305 
306 	return 0;
307 }
308 
309 int mmc_app_send_scr(struct mmc_card *card)
310 {
311 	int err;
312 	struct mmc_request mrq = {};
313 	struct mmc_command cmd = {};
314 	struct mmc_data data = {};
315 	struct scatterlist sg;
316 	__be32 *scr;
317 
318 	/* NOTE: caller guarantees scr is heap-allocated */
319 
320 	err = mmc_app_cmd(card->host, card);
321 	if (err)
322 		return err;
323 
324 	/* dma onto stack is unsafe/nonportable, but callers to this
325 	 * routine normally provide temporary on-stack buffers ...
326 	 */
327 	scr = kmalloc(sizeof(card->raw_scr), GFP_KERNEL);
328 	if (!scr)
329 		return -ENOMEM;
330 
331 	mrq.cmd = &cmd;
332 	mrq.data = &data;
333 
334 	cmd.opcode = SD_APP_SEND_SCR;
335 	cmd.arg = 0;
336 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
337 
338 	data.blksz = 8;
339 	data.blocks = 1;
340 	data.flags = MMC_DATA_READ;
341 	data.sg = &sg;
342 	data.sg_len = 1;
343 
344 	sg_init_one(&sg, scr, 8);
345 
346 	mmc_set_data_timeout(&data, card);
347 
348 	mmc_wait_for_req(card->host, &mrq);
349 
350 	card->raw_scr[0] = be32_to_cpu(scr[0]);
351 	card->raw_scr[1] = be32_to_cpu(scr[1]);
352 
353 	kfree(scr);
354 
355 	if (cmd.error)
356 		return cmd.error;
357 	if (data.error)
358 		return data.error;
359 
360 	return 0;
361 }
362 
363 int mmc_sd_switch(struct mmc_card *card, bool mode, int group,
364 	u8 value, u8 *resp)
365 {
366 	u32 cmd_args;
367 
368 	/* NOTE: caller guarantees resp is heap-allocated */
369 
370 	value &= 0xF;
371 	cmd_args = mode << 31 | 0x00FFFFFF;
372 	cmd_args &= ~(0xF << (group * 4));
373 	cmd_args |= value << (group * 4);
374 
375 	return mmc_send_adtc_data(card, card->host, SD_SWITCH, cmd_args, resp,
376 				  64);
377 }
378 EXPORT_SYMBOL_GPL(mmc_sd_switch);
379 
380 int mmc_app_sd_status(struct mmc_card *card, void *ssr)
381 {
382 	int err;
383 	struct mmc_request mrq = {};
384 	struct mmc_command cmd = {};
385 	struct mmc_data data = {};
386 	struct scatterlist sg;
387 
388 	/* NOTE: caller guarantees ssr is heap-allocated */
389 
390 	err = mmc_app_cmd(card->host, card);
391 	if (err)
392 		return err;
393 
394 	mrq.cmd = &cmd;
395 	mrq.data = &data;
396 
397 	cmd.opcode = SD_APP_SD_STATUS;
398 	cmd.arg = 0;
399 	cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
400 
401 	data.blksz = 64;
402 	data.blocks = 1;
403 	data.flags = MMC_DATA_READ;
404 	data.sg = &sg;
405 	data.sg_len = 1;
406 
407 	sg_init_one(&sg, ssr, 64);
408 
409 	mmc_set_data_timeout(&data, card);
410 
411 	mmc_wait_for_req(card->host, &mrq);
412 
413 	if (cmd.error)
414 		return cmd.error;
415 	if (data.error)
416 		return data.error;
417 
418 	return 0;
419 }
420