xref: /OK3568_Linux_fs/kernel/drivers/mmc/core/mmc_ops.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  linux/drivers/mmc/core/mmc_ops.h
4  *
5  *  Copyright 2006-2007 Pierre Ossman
6  */
7 
8 #include <linux/slab.h>
9 #include <linux/export.h>
10 #include <linux/types.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 
17 #include "core.h"
18 #include "card.h"
19 #include "host.h"
20 #include "mmc_ops.h"
21 
22 #define MMC_BKOPS_TIMEOUT_MS		(120 * 1000) /* 120s */
23 #define MMC_CACHE_FLUSH_TIMEOUT_MS	(30 * 1000) /* 30s */
24 #define MMC_SANITIZE_TIMEOUT_MS		(240 * 1000) /* 240s */
25 
26 static const u8 tuning_blk_pattern_4bit[] = {
27 	0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
28 	0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
29 	0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
30 	0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
31 	0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
32 	0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
33 	0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
34 	0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
35 };
36 
37 static const u8 tuning_blk_pattern_8bit[] = {
38 	0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
39 	0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
40 	0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
41 	0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
42 	0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
43 	0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
44 	0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
45 	0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
46 	0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
47 	0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
48 	0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
49 	0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
50 	0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
51 	0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
52 	0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
53 	0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
54 };
55 
__mmc_send_status(struct mmc_card * card,u32 * status,unsigned int retries)56 int __mmc_send_status(struct mmc_card *card, u32 *status, unsigned int retries)
57 {
58 	int err;
59 	struct mmc_command cmd = {};
60 
61 	cmd.opcode = MMC_SEND_STATUS;
62 	if (!mmc_host_is_spi(card->host))
63 		cmd.arg = card->rca << 16;
64 	cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
65 
66 	err = mmc_wait_for_cmd(card->host, &cmd, retries);
67 	if (err)
68 		return err;
69 
70 	/* NOTE: callers are required to understand the difference
71 	 * between "native" and SPI format status words!
72 	 */
73 	if (status)
74 		*status = cmd.resp[0];
75 
76 	return 0;
77 }
78 EXPORT_SYMBOL_GPL(__mmc_send_status);
79 
mmc_send_status(struct mmc_card * card,u32 * status)80 int mmc_send_status(struct mmc_card *card, u32 *status)
81 {
82 	return __mmc_send_status(card, status, MMC_CMD_RETRIES);
83 }
84 EXPORT_SYMBOL_GPL(mmc_send_status);
85 
_mmc_select_card(struct mmc_host * host,struct mmc_card * card)86 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
87 {
88 	struct mmc_command cmd = {};
89 
90 	cmd.opcode = MMC_SELECT_CARD;
91 
92 	if (card) {
93 		cmd.arg = card->rca << 16;
94 		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
95 	} else {
96 		cmd.arg = 0;
97 		cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
98 	}
99 
100 	return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
101 }
102 
mmc_select_card(struct mmc_card * card)103 int mmc_select_card(struct mmc_card *card)
104 {
105 
106 	return _mmc_select_card(card->host, card);
107 }
108 
mmc_deselect_cards(struct mmc_host * host)109 int mmc_deselect_cards(struct mmc_host *host)
110 {
111 	return _mmc_select_card(host, NULL);
112 }
113 
114 /*
115  * Write the value specified in the device tree or board code into the optional
116  * 16 bit Driver Stage Register. This can be used to tune raise/fall times and
117  * drive strength of the DAT and CMD outputs. The actual meaning of a given
118  * value is hardware dependant.
119  * The presence of the DSR register can be determined from the CSD register,
120  * bit 76.
121  */
mmc_set_dsr(struct mmc_host * host)122 int mmc_set_dsr(struct mmc_host *host)
123 {
124 	struct mmc_command cmd = {};
125 
126 	cmd.opcode = MMC_SET_DSR;
127 
128 	cmd.arg = (host->dsr << 16) | 0xffff;
129 	cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
130 
131 	return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
132 }
133 
mmc_go_idle(struct mmc_host * host)134 int mmc_go_idle(struct mmc_host *host)
135 {
136 	int err;
137 	struct mmc_command cmd = {};
138 
139 	/*
140 	 * Non-SPI hosts need to prevent chipselect going active during
141 	 * GO_IDLE; that would put chips into SPI mode.  Remind them of
142 	 * that in case of hardware that won't pull up DAT3/nCS otherwise.
143 	 *
144 	 * SPI hosts ignore ios.chip_select; it's managed according to
145 	 * rules that must accommodate non-MMC slaves which this layer
146 	 * won't even know about.
147 	 */
148 #ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
149 	if (!mmc_host_is_spi(host)) {
150 		mmc_set_chip_select(host, MMC_CS_HIGH);
151 		mmc_delay(1);
152 	}
153 #endif
154 	cmd.opcode = MMC_GO_IDLE_STATE;
155 	cmd.arg = 0;
156 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
157 
158 	err = mmc_wait_for_cmd(host, &cmd, 0);
159 
160 #ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
161 	mmc_delay(1);
162 
163 	if (!mmc_host_is_spi(host)) {
164 		mmc_set_chip_select(host, MMC_CS_DONTCARE);
165 		mmc_delay(1);
166 	}
167 #endif
168 	host->use_spi_crc = 0;
169 
170 	return err;
171 }
172 
mmc_send_op_cond(struct mmc_host * host,u32 ocr,u32 * rocr)173 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
174 {
175 	struct mmc_command cmd = {};
176 	int i, err = 0;
177 
178 	cmd.opcode = MMC_SEND_OP_COND;
179 	cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
180 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
181 
182 	for (i = 1000; i; i--) {
183 		err = mmc_wait_for_cmd(host, &cmd, 0);
184 		if (err)
185 			break;
186 
187 #ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
188 		/* if we're just probing, do a single pass */
189 		if (ocr == 0)
190 			break;
191 #endif
192 
193 		/*
194 		 * According to eMMC specification v5.1 section A6.1, the R3
195 		 * response value should be 0x00FF8080, 0x40FF8080, 0x80FF8080
196 		 * or 0xC0FF8080. The EMMC device may be abnormal if a wrong
197 		 * OCR data is configured.
198 		 */
199 		if ((cmd.resp[0] & 0xFFFFFF) != 0x00FF8080)
200 			continue;
201 
202 		/* wait until reset completes */
203 		if (mmc_host_is_spi(host)) {
204 			if (!(cmd.resp[0] & R1_SPI_IDLE))
205 				break;
206 		} else {
207 			if (cmd.resp[0] & MMC_CARD_BUSY)
208 				break;
209 		}
210 
211 		err = -ETIMEDOUT;
212 
213 		/*
214 		 * According to eMMC specification v5.1 section 6.4.3, we
215 		 * should issue CMD1 repeatedly in the idle state until
216 		 * the eMMC is ready. Otherwise some eMMC devices seem to enter
217 		 * the inactive mode after mmc_init_card() issued CMD0 when
218 		 * the eMMC device is busy.
219 		 */
220 		if (!ocr && !mmc_host_is_spi(host))
221 			cmd.arg = cmd.resp[0] | BIT(30);
222 #ifndef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
223 		mmc_delay(1);
224 #else
225 		udelay(1);
226 #endif
227 	}
228 
229 	if (rocr && !mmc_host_is_spi(host))
230 		*rocr = cmd.resp[0];
231 
232 	return err;
233 }
234 
mmc_set_relative_addr(struct mmc_card * card)235 int mmc_set_relative_addr(struct mmc_card *card)
236 {
237 	struct mmc_command cmd = {};
238 
239 	cmd.opcode = MMC_SET_RELATIVE_ADDR;
240 	cmd.arg = card->rca << 16;
241 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
242 
243 	return mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
244 }
245 
246 static int
mmc_send_cxd_native(struct mmc_host * host,u32 arg,u32 * cxd,int opcode)247 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
248 {
249 	int err;
250 	struct mmc_command cmd = {};
251 
252 	cmd.opcode = opcode;
253 	cmd.arg = arg;
254 	cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
255 
256 	err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
257 	if (err)
258 		return err;
259 
260 	memcpy(cxd, cmd.resp, sizeof(u32) * 4);
261 
262 	return 0;
263 }
264 
265 /*
266  * NOTE: void *buf, caller for the buf is required to use DMA-capable
267  * buffer or on-stack buffer (with some overhead in callee).
268  */
269 static int
mmc_send_cxd_data(struct mmc_card * card,struct mmc_host * host,u32 opcode,void * buf,unsigned len)270 mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
271 		u32 opcode, void *buf, unsigned len)
272 {
273 	struct mmc_request mrq = {};
274 	struct mmc_command cmd = {};
275 	struct mmc_data data = {};
276 	struct scatterlist sg;
277 
278 	mrq.cmd = &cmd;
279 	mrq.data = &data;
280 
281 	cmd.opcode = opcode;
282 	cmd.arg = 0;
283 
284 	/* NOTE HACK:  the MMC_RSP_SPI_R1 is always correct here, but we
285 	 * rely on callers to never use this with "native" calls for reading
286 	 * CSD or CID.  Native versions of those commands use the R2 type,
287 	 * not R1 plus a data block.
288 	 */
289 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
290 
291 	data.blksz = len;
292 	data.blocks = 1;
293 	data.flags = MMC_DATA_READ;
294 	data.sg = &sg;
295 	data.sg_len = 1;
296 
297 	sg_init_one(&sg, buf, len);
298 
299 	if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
300 		/*
301 		 * The spec states that CSR and CID accesses have a timeout
302 		 * of 64 clock cycles.
303 		 */
304 		data.timeout_ns = 0;
305 		data.timeout_clks = 64;
306 	} else
307 		mmc_set_data_timeout(&data, card);
308 
309 	mmc_wait_for_req(host, &mrq);
310 
311 	if (cmd.error)
312 		return cmd.error;
313 	if (data.error)
314 		return data.error;
315 
316 	return 0;
317 }
318 
mmc_spi_send_csd(struct mmc_card * card,u32 * csd)319 static int mmc_spi_send_csd(struct mmc_card *card, u32 *csd)
320 {
321 	int ret, i;
322 	__be32 *csd_tmp;
323 
324 	csd_tmp = kzalloc(16, GFP_KERNEL);
325 	if (!csd_tmp)
326 		return -ENOMEM;
327 
328 	ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
329 	if (ret)
330 		goto err;
331 
332 	for (i = 0; i < 4; i++)
333 		csd[i] = be32_to_cpu(csd_tmp[i]);
334 
335 err:
336 	kfree(csd_tmp);
337 	return ret;
338 }
339 
mmc_send_csd(struct mmc_card * card,u32 * csd)340 int mmc_send_csd(struct mmc_card *card, u32 *csd)
341 {
342 	if (mmc_host_is_spi(card->host))
343 		return mmc_spi_send_csd(card, csd);
344 
345 	return mmc_send_cxd_native(card->host, card->rca << 16,	csd,
346 				MMC_SEND_CSD);
347 }
348 
mmc_spi_send_cid(struct mmc_host * host,u32 * cid)349 static int mmc_spi_send_cid(struct mmc_host *host, u32 *cid)
350 {
351 	int ret, i;
352 	__be32 *cid_tmp;
353 
354 	cid_tmp = kzalloc(16, GFP_KERNEL);
355 	if (!cid_tmp)
356 		return -ENOMEM;
357 
358 	ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
359 	if (ret)
360 		goto err;
361 
362 	for (i = 0; i < 4; i++)
363 		cid[i] = be32_to_cpu(cid_tmp[i]);
364 
365 err:
366 	kfree(cid_tmp);
367 	return ret;
368 }
369 
mmc_send_cid(struct mmc_host * host,u32 * cid)370 int mmc_send_cid(struct mmc_host *host, u32 *cid)
371 {
372 	if (mmc_host_is_spi(host))
373 		return mmc_spi_send_cid(host, cid);
374 
375 	return mmc_send_cxd_native(host, 0, cid, MMC_ALL_SEND_CID);
376 }
377 
mmc_get_ext_csd(struct mmc_card * card,u8 ** new_ext_csd)378 int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
379 {
380 	int err;
381 	u8 *ext_csd;
382 
383 	if (!card || !new_ext_csd)
384 		return -EINVAL;
385 
386 	if (!mmc_can_ext_csd(card))
387 		return -EOPNOTSUPP;
388 
389 	/*
390 	 * As the ext_csd is so large and mostly unused, we don't store the
391 	 * raw block in mmc_card.
392 	 */
393 	ext_csd = kzalloc(512, GFP_KERNEL);
394 	if (!ext_csd)
395 		return -ENOMEM;
396 
397 	err = mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, ext_csd,
398 				512);
399 	if (err)
400 		kfree(ext_csd);
401 	else
402 		*new_ext_csd = ext_csd;
403 
404 	return err;
405 }
406 EXPORT_SYMBOL_GPL(mmc_get_ext_csd);
407 
mmc_spi_read_ocr(struct mmc_host * host,int highcap,u32 * ocrp)408 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
409 {
410 	struct mmc_command cmd = {};
411 	int err;
412 
413 	cmd.opcode = MMC_SPI_READ_OCR;
414 	cmd.arg = highcap ? (1 << 30) : 0;
415 	cmd.flags = MMC_RSP_SPI_R3;
416 
417 	err = mmc_wait_for_cmd(host, &cmd, 0);
418 
419 	*ocrp = cmd.resp[1];
420 	return err;
421 }
422 
mmc_spi_set_crc(struct mmc_host * host,int use_crc)423 int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
424 {
425 	struct mmc_command cmd = {};
426 	int err;
427 
428 	cmd.opcode = MMC_SPI_CRC_ON_OFF;
429 	cmd.flags = MMC_RSP_SPI_R1;
430 	cmd.arg = use_crc;
431 
432 	err = mmc_wait_for_cmd(host, &cmd, 0);
433 	if (!err)
434 		host->use_spi_crc = use_crc;
435 	return err;
436 }
437 
mmc_switch_status_error(struct mmc_host * host,u32 status)438 static int mmc_switch_status_error(struct mmc_host *host, u32 status)
439 {
440 	if (mmc_host_is_spi(host)) {
441 		if (status & R1_SPI_ILLEGAL_COMMAND)
442 			return -EBADMSG;
443 	} else {
444 		if (R1_STATUS(status))
445 			pr_warn("%s: unexpected status %#x after switch\n",
446 				mmc_hostname(host), status);
447 		if (status & R1_SWITCH_ERROR)
448 			return -EBADMSG;
449 	}
450 	return 0;
451 }
452 
453 /* Caller must hold re-tuning */
mmc_switch_status(struct mmc_card * card,bool crc_err_fatal)454 int mmc_switch_status(struct mmc_card *card, bool crc_err_fatal)
455 {
456 	u32 status;
457 	int err;
458 
459 	err = mmc_send_status(card, &status);
460 	if (!crc_err_fatal && err == -EILSEQ)
461 		return 0;
462 	if (err)
463 		return err;
464 
465 	return mmc_switch_status_error(card->host, status);
466 }
467 
mmc_busy_status(struct mmc_card * card,bool retry_crc_err,enum mmc_busy_cmd busy_cmd,bool * busy)468 static int mmc_busy_status(struct mmc_card *card, bool retry_crc_err,
469 			   enum mmc_busy_cmd busy_cmd, bool *busy)
470 {
471 	struct mmc_host *host = card->host;
472 	u32 status = 0;
473 	int err;
474 
475 	if (host->ops->card_busy) {
476 		*busy = host->ops->card_busy(host);
477 		return 0;
478 	}
479 
480 	err = mmc_send_status(card, &status);
481 	if (retry_crc_err && err == -EILSEQ) {
482 		*busy = true;
483 		return 0;
484 	}
485 	if (err)
486 		return err;
487 
488 	switch (busy_cmd) {
489 	case MMC_BUSY_CMD6:
490 		err = mmc_switch_status_error(card->host, status);
491 		break;
492 	case MMC_BUSY_ERASE:
493 		err = R1_STATUS(status) ? -EIO : 0;
494 		break;
495 	case MMC_BUSY_HPI:
496 		break;
497 	default:
498 		err = -EINVAL;
499 	}
500 
501 	if (err)
502 		return err;
503 
504 	*busy = !mmc_ready_for_data(status);
505 	return 0;
506 }
507 
__mmc_poll_for_busy(struct mmc_card * card,unsigned int timeout_ms,bool send_status,bool retry_crc_err,enum mmc_busy_cmd busy_cmd)508 static int __mmc_poll_for_busy(struct mmc_card *card, unsigned int timeout_ms,
509 			       bool send_status, bool retry_crc_err,
510 			       enum mmc_busy_cmd busy_cmd)
511 {
512 	struct mmc_host *host = card->host;
513 	int err;
514 	unsigned long timeout;
515 	unsigned int udelay = 32, udelay_max = 32768;
516 	bool expired = false;
517 	bool busy = false;
518 
519 	/*
520 	 * In cases when not allowed to poll by using CMD13 or because we aren't
521 	 * capable of polling by using ->card_busy(), then rely on waiting the
522 	 * stated timeout to be sufficient.
523 	 */
524 	if (!send_status && !host->ops->card_busy) {
525 		mmc_delay(timeout_ms);
526 		return 0;
527 	}
528 
529 	timeout = jiffies + msecs_to_jiffies(timeout_ms) + 1;
530 	do {
531 		/*
532 		 * Due to the possibility of being preempted while polling,
533 		 * check the expiration time first.
534 		 */
535 		expired = time_after(jiffies, timeout);
536 
537 		err = mmc_busy_status(card, retry_crc_err, busy_cmd, &busy);
538 		if (err)
539 			return err;
540 
541 		/* Timeout if the device still remains busy. */
542 		if (expired && busy) {
543 			pr_err("%s: Card stuck being busy! %s\n",
544 				mmc_hostname(host), __func__);
545 			return -ETIMEDOUT;
546 		}
547 
548 		/* Throttle the polling rate to avoid hogging the CPU. */
549 		if (busy) {
550 			usleep_range(udelay, udelay * 2);
551 			if (udelay < udelay_max)
552 				udelay *= 2;
553 		}
554 	} while (busy);
555 
556 	return 0;
557 }
558 
mmc_poll_for_busy(struct mmc_card * card,unsigned int timeout_ms,enum mmc_busy_cmd busy_cmd)559 int mmc_poll_for_busy(struct mmc_card *card, unsigned int timeout_ms,
560 		      enum mmc_busy_cmd busy_cmd)
561 {
562 	return __mmc_poll_for_busy(card, timeout_ms, true, false, busy_cmd);
563 }
564 
565 /**
566  *	__mmc_switch - modify EXT_CSD register
567  *	@card: the MMC card associated with the data transfer
568  *	@set: cmd set values
569  *	@index: EXT_CSD register index
570  *	@value: value to program into EXT_CSD register
571  *	@timeout_ms: timeout (ms) for operation performed by register write,
572  *                   timeout of zero implies maximum possible timeout
573  *	@timing: new timing to change to
574  *	@send_status: send status cmd to poll for busy
575  *	@retry_crc_err: retry when CRC errors when polling with CMD13 for busy
576  *
577  *	Modifies the EXT_CSD register for selected card.
578  */
__mmc_switch(struct mmc_card * card,u8 set,u8 index,u8 value,unsigned int timeout_ms,unsigned char timing,bool send_status,bool retry_crc_err)579 int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
580 		unsigned int timeout_ms, unsigned char timing,
581 		bool send_status, bool retry_crc_err)
582 {
583 	struct mmc_host *host = card->host;
584 	int err;
585 	struct mmc_command cmd = {};
586 	bool use_r1b_resp = true;
587 	unsigned char old_timing = host->ios.timing;
588 
589 	mmc_retune_hold(host);
590 
591 	if (!timeout_ms) {
592 		pr_warn("%s: unspecified timeout for CMD6 - use generic\n",
593 			mmc_hostname(host));
594 		timeout_ms = card->ext_csd.generic_cmd6_time;
595 	}
596 
597 	/*
598 	 * If the max_busy_timeout of the host is specified, make sure it's
599 	 * enough to fit the used timeout_ms. In case it's not, let's instruct
600 	 * the host to avoid HW busy detection, by converting to a R1 response
601 	 * instead of a R1B. Note, some hosts requires R1B, which also means
602 	 * they are on their own when it comes to deal with the busy timeout.
603 	 */
604 	if (!(host->caps & MMC_CAP_NEED_RSP_BUSY) && host->max_busy_timeout &&
605 	    (timeout_ms > host->max_busy_timeout))
606 		use_r1b_resp = false;
607 
608 	cmd.opcode = MMC_SWITCH;
609 	cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
610 		  (index << 16) |
611 		  (value << 8) |
612 		  set;
613 	cmd.flags = MMC_CMD_AC;
614 	if (use_r1b_resp) {
615 		cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
616 		cmd.busy_timeout = timeout_ms;
617 	} else {
618 		cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
619 	}
620 
621 	err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
622 	if (err)
623 		goto out;
624 
625 	/*If SPI or used HW busy detection above, then we don't need to poll. */
626 	if (((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) ||
627 		mmc_host_is_spi(host))
628 		goto out_tim;
629 
630 	/* Let's try to poll to find out when the command is completed. */
631 	err = __mmc_poll_for_busy(card, timeout_ms, send_status, retry_crc_err,
632 				  MMC_BUSY_CMD6);
633 	if (err)
634 		goto out;
635 
636 out_tim:
637 	/* Switch to new timing before check switch status. */
638 	if (timing)
639 		mmc_set_timing(host, timing);
640 
641 	if (send_status) {
642 		err = mmc_switch_status(card, true);
643 		if (err && timing)
644 			mmc_set_timing(host, old_timing);
645 	}
646 out:
647 	mmc_retune_release(host);
648 
649 	return err;
650 }
651 
mmc_switch(struct mmc_card * card,u8 set,u8 index,u8 value,unsigned int timeout_ms)652 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
653 		unsigned int timeout_ms)
654 {
655 	return __mmc_switch(card, set, index, value, timeout_ms, 0,
656 			    true, false);
657 }
658 EXPORT_SYMBOL_GPL(mmc_switch);
659 
mmc_send_tuning(struct mmc_host * host,u32 opcode,int * cmd_error)660 int mmc_send_tuning(struct mmc_host *host, u32 opcode, int *cmd_error)
661 {
662 	struct mmc_request mrq = {};
663 	struct mmc_command cmd = {};
664 	struct mmc_data data = {};
665 	struct scatterlist sg;
666 	struct mmc_ios *ios = &host->ios;
667 	const u8 *tuning_block_pattern;
668 	int size, err = 0;
669 	u8 *data_buf;
670 
671 	if (ios->bus_width == MMC_BUS_WIDTH_8) {
672 		tuning_block_pattern = tuning_blk_pattern_8bit;
673 		size = sizeof(tuning_blk_pattern_8bit);
674 	} else if (ios->bus_width == MMC_BUS_WIDTH_4) {
675 		tuning_block_pattern = tuning_blk_pattern_4bit;
676 		size = sizeof(tuning_blk_pattern_4bit);
677 	} else
678 		return -EINVAL;
679 
680 	data_buf = kzalloc(size, GFP_KERNEL);
681 	if (!data_buf)
682 		return -ENOMEM;
683 
684 	mrq.cmd = &cmd;
685 	mrq.data = &data;
686 
687 	cmd.opcode = opcode;
688 	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
689 
690 	data.blksz = size;
691 	data.blocks = 1;
692 	data.flags = MMC_DATA_READ;
693 
694 	/*
695 	 * According to the tuning specs, Tuning process
696 	 * is normally shorter 40 executions of CMD19,
697 	 * and timeout value should be shorter than 150 ms
698 	 */
699 	data.timeout_ns = 150 * NSEC_PER_MSEC;
700 
701 	data.sg = &sg;
702 	data.sg_len = 1;
703 	sg_init_one(&sg, data_buf, size);
704 
705 	mmc_wait_for_req(host, &mrq);
706 
707 	if (cmd_error)
708 		*cmd_error = cmd.error;
709 
710 	if (cmd.error) {
711 		err = cmd.error;
712 		goto out;
713 	}
714 
715 	if (data.error) {
716 		err = data.error;
717 		goto out;
718 	}
719 
720 	if (memcmp(data_buf, tuning_block_pattern, size))
721 		err = -EIO;
722 
723 out:
724 	kfree(data_buf);
725 	return err;
726 }
727 EXPORT_SYMBOL_GPL(mmc_send_tuning);
728 
mmc_abort_tuning(struct mmc_host * host,u32 opcode)729 int mmc_abort_tuning(struct mmc_host *host, u32 opcode)
730 {
731 	struct mmc_command cmd = {};
732 
733 	/*
734 	 * eMMC specification specifies that CMD12 can be used to stop a tuning
735 	 * command, but SD specification does not, so do nothing unless it is
736 	 * eMMC.
737 	 */
738 	if (opcode != MMC_SEND_TUNING_BLOCK_HS200)
739 		return 0;
740 
741 	cmd.opcode = MMC_STOP_TRANSMISSION;
742 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
743 
744 	/*
745 	 * For drivers that override R1 to R1b, set an arbitrary timeout based
746 	 * on the tuning timeout i.e. 150ms.
747 	 */
748 	cmd.busy_timeout = 150;
749 
750 	return mmc_wait_for_cmd(host, &cmd, 0);
751 }
752 EXPORT_SYMBOL_GPL(mmc_abort_tuning);
753 
754 static int
mmc_send_bus_test(struct mmc_card * card,struct mmc_host * host,u8 opcode,u8 len)755 mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
756 		  u8 len)
757 {
758 	struct mmc_request mrq = {};
759 	struct mmc_command cmd = {};
760 	struct mmc_data data = {};
761 	struct scatterlist sg;
762 	u8 *data_buf;
763 	u8 *test_buf;
764 	int i, err;
765 	static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
766 	static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
767 
768 	/* dma onto stack is unsafe/nonportable, but callers to this
769 	 * routine normally provide temporary on-stack buffers ...
770 	 */
771 	data_buf = kmalloc(len, GFP_KERNEL);
772 	if (!data_buf)
773 		return -ENOMEM;
774 
775 	if (len == 8)
776 		test_buf = testdata_8bit;
777 	else if (len == 4)
778 		test_buf = testdata_4bit;
779 	else {
780 		pr_err("%s: Invalid bus_width %d\n",
781 		       mmc_hostname(host), len);
782 		kfree(data_buf);
783 		return -EINVAL;
784 	}
785 
786 	if (opcode == MMC_BUS_TEST_W)
787 		memcpy(data_buf, test_buf, len);
788 
789 	mrq.cmd = &cmd;
790 	mrq.data = &data;
791 	cmd.opcode = opcode;
792 	cmd.arg = 0;
793 
794 	/* NOTE HACK:  the MMC_RSP_SPI_R1 is always correct here, but we
795 	 * rely on callers to never use this with "native" calls for reading
796 	 * CSD or CID.  Native versions of those commands use the R2 type,
797 	 * not R1 plus a data block.
798 	 */
799 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
800 
801 	data.blksz = len;
802 	data.blocks = 1;
803 	if (opcode == MMC_BUS_TEST_R)
804 		data.flags = MMC_DATA_READ;
805 	else
806 		data.flags = MMC_DATA_WRITE;
807 
808 	data.sg = &sg;
809 	data.sg_len = 1;
810 	mmc_set_data_timeout(&data, card);
811 	sg_init_one(&sg, data_buf, len);
812 	mmc_wait_for_req(host, &mrq);
813 	err = 0;
814 	if (opcode == MMC_BUS_TEST_R) {
815 		for (i = 0; i < len / 4; i++)
816 			if ((test_buf[i] ^ data_buf[i]) != 0xff) {
817 				err = -EIO;
818 				break;
819 			}
820 	}
821 	kfree(data_buf);
822 
823 	if (cmd.error)
824 		return cmd.error;
825 	if (data.error)
826 		return data.error;
827 
828 	return err;
829 }
830 
mmc_bus_test(struct mmc_card * card,u8 bus_width)831 int mmc_bus_test(struct mmc_card *card, u8 bus_width)
832 {
833 	int width;
834 
835 	if (bus_width == MMC_BUS_WIDTH_8)
836 		width = 8;
837 	else if (bus_width == MMC_BUS_WIDTH_4)
838 		width = 4;
839 	else if (bus_width == MMC_BUS_WIDTH_1)
840 		return 0; /* no need for test */
841 	else
842 		return -EINVAL;
843 
844 	/*
845 	 * Ignore errors from BUS_TEST_W.  BUS_TEST_R will fail if there
846 	 * is a problem.  This improves chances that the test will work.
847 	 */
848 	mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
849 	return mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
850 }
851 
mmc_send_hpi_cmd(struct mmc_card * card)852 static int mmc_send_hpi_cmd(struct mmc_card *card)
853 {
854 	unsigned int busy_timeout_ms = card->ext_csd.out_of_int_time;
855 	struct mmc_host *host = card->host;
856 	bool use_r1b_resp = true;
857 	struct mmc_command cmd = {};
858 	int err;
859 
860 	cmd.opcode = card->ext_csd.hpi_cmd;
861 	cmd.arg = card->rca << 16 | 1;
862 
863 	/*
864 	 * Make sure the host's max_busy_timeout fit the needed timeout for HPI.
865 	 * In case it doesn't, let's instruct the host to avoid HW busy
866 	 * detection, by using a R1 response instead of R1B.
867 	 */
868 	if (host->max_busy_timeout && busy_timeout_ms > host->max_busy_timeout)
869 		use_r1b_resp = false;
870 
871 	if (cmd.opcode == MMC_STOP_TRANSMISSION && use_r1b_resp) {
872 		cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
873 		cmd.busy_timeout = busy_timeout_ms;
874 	} else {
875 		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
876 		use_r1b_resp = false;
877 	}
878 
879 	err = mmc_wait_for_cmd(host, &cmd, 0);
880 	if (err) {
881 		pr_warn("%s: HPI error %d. Command response %#x\n",
882 			mmc_hostname(host), err, cmd.resp[0]);
883 		return err;
884 	}
885 
886 	/* No need to poll when using HW busy detection. */
887 	if (host->caps & MMC_CAP_WAIT_WHILE_BUSY && use_r1b_resp)
888 		return 0;
889 
890 	/* Let's poll to find out when the HPI request completes. */
891 	return mmc_poll_for_busy(card, busy_timeout_ms, MMC_BUSY_HPI);
892 }
893 
894 /**
895  *	mmc_interrupt_hpi - Issue for High priority Interrupt
896  *	@card: the MMC card associated with the HPI transfer
897  *
898  *	Issued High Priority Interrupt, and check for card status
899  *	until out-of prg-state.
900  */
mmc_interrupt_hpi(struct mmc_card * card)901 static int mmc_interrupt_hpi(struct mmc_card *card)
902 {
903 	int err;
904 	u32 status;
905 
906 	if (!card->ext_csd.hpi_en) {
907 		pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
908 		return 1;
909 	}
910 
911 	err = mmc_send_status(card, &status);
912 	if (err) {
913 		pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
914 		goto out;
915 	}
916 
917 	switch (R1_CURRENT_STATE(status)) {
918 	case R1_STATE_IDLE:
919 	case R1_STATE_READY:
920 	case R1_STATE_STBY:
921 	case R1_STATE_TRAN:
922 		/*
923 		 * In idle and transfer states, HPI is not needed and the caller
924 		 * can issue the next intended command immediately
925 		 */
926 		goto out;
927 	case R1_STATE_PRG:
928 		break;
929 	default:
930 		/* In all other states, it's illegal to issue HPI */
931 		pr_debug("%s: HPI cannot be sent. Card state=%d\n",
932 			mmc_hostname(card->host), R1_CURRENT_STATE(status));
933 		err = -EINVAL;
934 		goto out;
935 	}
936 
937 	err = mmc_send_hpi_cmd(card);
938 out:
939 	return err;
940 }
941 
mmc_can_ext_csd(struct mmc_card * card)942 int mmc_can_ext_csd(struct mmc_card *card)
943 {
944 	return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3);
945 }
946 
mmc_read_bkops_status(struct mmc_card * card)947 static int mmc_read_bkops_status(struct mmc_card *card)
948 {
949 	int err;
950 	u8 *ext_csd;
951 
952 	err = mmc_get_ext_csd(card, &ext_csd);
953 	if (err)
954 		return err;
955 
956 	card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
957 	card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
958 	kfree(ext_csd);
959 	return 0;
960 }
961 
962 /**
963  *	mmc_run_bkops - Run BKOPS for supported cards
964  *	@card: MMC card to run BKOPS for
965  *
966  *	Run background operations synchronously for cards having manual BKOPS
967  *	enabled and in case it reports urgent BKOPS level.
968 */
mmc_run_bkops(struct mmc_card * card)969 void mmc_run_bkops(struct mmc_card *card)
970 {
971 	int err;
972 
973 	if (!card->ext_csd.man_bkops_en)
974 		return;
975 
976 	err = mmc_read_bkops_status(card);
977 	if (err) {
978 		pr_err("%s: Failed to read bkops status: %d\n",
979 		       mmc_hostname(card->host), err);
980 		return;
981 	}
982 
983 	if (!card->ext_csd.raw_bkops_status ||
984 	    card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2)
985 		return;
986 
987 	mmc_retune_hold(card->host);
988 
989 	/*
990 	 * For urgent BKOPS status, LEVEL_2 and higher, let's execute
991 	 * synchronously. Future wise, we may consider to start BKOPS, for less
992 	 * urgent levels by using an asynchronous background task, when idle.
993 	 */
994 	err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
995 			 EXT_CSD_BKOPS_START, 1, MMC_BKOPS_TIMEOUT_MS);
996 	if (err)
997 		pr_warn("%s: Error %d starting bkops\n",
998 			mmc_hostname(card->host), err);
999 
1000 	mmc_retune_release(card->host);
1001 }
1002 EXPORT_SYMBOL(mmc_run_bkops);
1003 
1004 /*
1005  * Flush the cache to the non-volatile storage.
1006  */
mmc_flush_cache(struct mmc_card * card)1007 int mmc_flush_cache(struct mmc_card *card)
1008 {
1009 	int err = 0;
1010 
1011 	if (mmc_cache_enabled(card->host)) {
1012 		err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1013 				   EXT_CSD_FLUSH_CACHE, 1,
1014 				   MMC_CACHE_FLUSH_TIMEOUT_MS, 0,
1015 				   false, false);
1016 		if (err)
1017 			pr_err("%s: cache flush error %d\n",
1018 					mmc_hostname(card->host), err);
1019 	}
1020 
1021 	return err;
1022 }
1023 EXPORT_SYMBOL(mmc_flush_cache);
1024 
mmc_cmdq_switch(struct mmc_card * card,bool enable)1025 static int mmc_cmdq_switch(struct mmc_card *card, bool enable)
1026 {
1027 	u8 val = enable ? EXT_CSD_CMDQ_MODE_ENABLED : 0;
1028 	int err;
1029 
1030 	if (!card->ext_csd.cmdq_support)
1031 		return -EOPNOTSUPP;
1032 
1033 	err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_CMDQ_MODE_EN,
1034 			 val, card->ext_csd.generic_cmd6_time);
1035 	if (!err)
1036 		card->ext_csd.cmdq_en = enable;
1037 
1038 	return err;
1039 }
1040 
mmc_cmdq_enable(struct mmc_card * card)1041 int mmc_cmdq_enable(struct mmc_card *card)
1042 {
1043 	return mmc_cmdq_switch(card, true);
1044 }
1045 EXPORT_SYMBOL_GPL(mmc_cmdq_enable);
1046 
mmc_cmdq_disable(struct mmc_card * card)1047 int mmc_cmdq_disable(struct mmc_card *card)
1048 {
1049 	return mmc_cmdq_switch(card, false);
1050 }
1051 EXPORT_SYMBOL_GPL(mmc_cmdq_disable);
1052 
mmc_sanitize(struct mmc_card * card)1053 int mmc_sanitize(struct mmc_card *card)
1054 {
1055 	struct mmc_host *host = card->host;
1056 	int err;
1057 
1058 	if (!mmc_can_sanitize(card)) {
1059 		pr_warn("%s: Sanitize not supported\n", mmc_hostname(host));
1060 		return -EOPNOTSUPP;
1061 	}
1062 
1063 	pr_debug("%s: Sanitize in progress...\n", mmc_hostname(host));
1064 
1065 	mmc_retune_hold(host);
1066 
1067 	err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_SANITIZE_START,
1068 			 1, MMC_SANITIZE_TIMEOUT_MS);
1069 	if (err)
1070 		pr_err("%s: Sanitize failed err=%d\n", mmc_hostname(host), err);
1071 
1072 	/*
1073 	 * If the sanitize operation timed out, the card is probably still busy
1074 	 * in the R1_STATE_PRG. Rather than continue to wait, let's try to abort
1075 	 * it with a HPI command to get back into R1_STATE_TRAN.
1076 	 */
1077 	if (err == -ETIMEDOUT && !mmc_interrupt_hpi(card))
1078 		pr_warn("%s: Sanitize aborted\n", mmc_hostname(host));
1079 
1080 	mmc_retune_release(host);
1081 
1082 	pr_debug("%s: Sanitize completed\n", mmc_hostname(host));
1083 	return err;
1084 }
1085 EXPORT_SYMBOL_GPL(mmc_sanitize);
1086