xref: /openbmc/linux/drivers/mmc/core/mmc_ops.c (revision a06c488d)
1 /*
2  *  linux/drivers/mmc/core/mmc_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/export.h>
14 #include <linux/types.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 
21 #include "core.h"
22 #include "host.h"
23 #include "mmc_ops.h"
24 
25 #define MMC_OPS_TIMEOUT_MS	(10 * 60 * 1000) /* 10 minute timeout */
26 
27 static const u8 tuning_blk_pattern_4bit[] = {
28 	0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
29 	0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
30 	0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
31 	0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
32 	0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
33 	0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
34 	0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
35 	0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
36 };
37 
38 static const u8 tuning_blk_pattern_8bit[] = {
39 	0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
40 	0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
41 	0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
42 	0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
43 	0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
44 	0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
45 	0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
46 	0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
47 	0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
48 	0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
49 	0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
50 	0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
51 	0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
52 	0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
53 	0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
54 	0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
55 };
56 
57 static inline int __mmc_send_status(struct mmc_card *card, u32 *status,
58 				    bool ignore_crc)
59 {
60 	int err;
61 	struct mmc_command cmd = {0};
62 
63 	BUG_ON(!card);
64 	BUG_ON(!card->host);
65 
66 	cmd.opcode = MMC_SEND_STATUS;
67 	if (!mmc_host_is_spi(card->host))
68 		cmd.arg = card->rca << 16;
69 	cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
70 	if (ignore_crc)
71 		cmd.flags &= ~MMC_RSP_CRC;
72 
73 	err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
74 	if (err)
75 		return err;
76 
77 	/* NOTE: callers are required to understand the difference
78 	 * between "native" and SPI format status words!
79 	 */
80 	if (status)
81 		*status = cmd.resp[0];
82 
83 	return 0;
84 }
85 
86 int mmc_send_status(struct mmc_card *card, u32 *status)
87 {
88 	return __mmc_send_status(card, status, false);
89 }
90 
91 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
92 {
93 	int err;
94 	struct mmc_command cmd = {0};
95 
96 	BUG_ON(!host);
97 
98 	cmd.opcode = MMC_SELECT_CARD;
99 
100 	if (card) {
101 		cmd.arg = card->rca << 16;
102 		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
103 	} else {
104 		cmd.arg = 0;
105 		cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
106 	}
107 
108 	err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
109 	if (err)
110 		return err;
111 
112 	return 0;
113 }
114 
115 int mmc_select_card(struct mmc_card *card)
116 {
117 	BUG_ON(!card);
118 
119 	return _mmc_select_card(card->host, card);
120 }
121 
122 int mmc_deselect_cards(struct mmc_host *host)
123 {
124 	return _mmc_select_card(host, NULL);
125 }
126 
127 /*
128  * Write the value specified in the device tree or board code into the optional
129  * 16 bit Driver Stage Register. This can be used to tune raise/fall times and
130  * drive strength of the DAT and CMD outputs. The actual meaning of a given
131  * value is hardware dependant.
132  * The presence of the DSR register can be determined from the CSD register,
133  * bit 76.
134  */
135 int mmc_set_dsr(struct mmc_host *host)
136 {
137 	struct mmc_command cmd = {0};
138 
139 	cmd.opcode = MMC_SET_DSR;
140 
141 	cmd.arg = (host->dsr << 16) | 0xffff;
142 	cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
143 
144 	return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
145 }
146 
147 int mmc_go_idle(struct mmc_host *host)
148 {
149 	int err;
150 	struct mmc_command cmd = {0};
151 
152 	/*
153 	 * Non-SPI hosts need to prevent chipselect going active during
154 	 * GO_IDLE; that would put chips into SPI mode.  Remind them of
155 	 * that in case of hardware that won't pull up DAT3/nCS otherwise.
156 	 *
157 	 * SPI hosts ignore ios.chip_select; it's managed according to
158 	 * rules that must accommodate non-MMC slaves which this layer
159 	 * won't even know about.
160 	 */
161 	if (!mmc_host_is_spi(host)) {
162 		mmc_set_chip_select(host, MMC_CS_HIGH);
163 		mmc_delay(1);
164 	}
165 
166 	cmd.opcode = MMC_GO_IDLE_STATE;
167 	cmd.arg = 0;
168 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
169 
170 	err = mmc_wait_for_cmd(host, &cmd, 0);
171 
172 	mmc_delay(1);
173 
174 	if (!mmc_host_is_spi(host)) {
175 		mmc_set_chip_select(host, MMC_CS_DONTCARE);
176 		mmc_delay(1);
177 	}
178 
179 	host->use_spi_crc = 0;
180 
181 	return err;
182 }
183 
184 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
185 {
186 	struct mmc_command cmd = {0};
187 	int i, err = 0;
188 
189 	BUG_ON(!host);
190 
191 	cmd.opcode = MMC_SEND_OP_COND;
192 	cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
193 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
194 
195 	for (i = 100; i; i--) {
196 		err = mmc_wait_for_cmd(host, &cmd, 0);
197 		if (err)
198 			break;
199 
200 		/* if we're just probing, do a single pass */
201 		if (ocr == 0)
202 			break;
203 
204 		/* otherwise wait until reset completes */
205 		if (mmc_host_is_spi(host)) {
206 			if (!(cmd.resp[0] & R1_SPI_IDLE))
207 				break;
208 		} else {
209 			if (cmd.resp[0] & MMC_CARD_BUSY)
210 				break;
211 		}
212 
213 		err = -ETIMEDOUT;
214 
215 		mmc_delay(10);
216 	}
217 
218 	if (rocr && !mmc_host_is_spi(host))
219 		*rocr = cmd.resp[0];
220 
221 	return err;
222 }
223 
224 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
225 {
226 	int err;
227 	struct mmc_command cmd = {0};
228 
229 	BUG_ON(!host);
230 	BUG_ON(!cid);
231 
232 	cmd.opcode = MMC_ALL_SEND_CID;
233 	cmd.arg = 0;
234 	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
235 
236 	err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
237 	if (err)
238 		return err;
239 
240 	memcpy(cid, cmd.resp, sizeof(u32) * 4);
241 
242 	return 0;
243 }
244 
245 int mmc_set_relative_addr(struct mmc_card *card)
246 {
247 	int err;
248 	struct mmc_command cmd = {0};
249 
250 	BUG_ON(!card);
251 	BUG_ON(!card->host);
252 
253 	cmd.opcode = MMC_SET_RELATIVE_ADDR;
254 	cmd.arg = card->rca << 16;
255 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
256 
257 	err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
258 	if (err)
259 		return err;
260 
261 	return 0;
262 }
263 
264 static int
265 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
266 {
267 	int err;
268 	struct mmc_command cmd = {0};
269 
270 	BUG_ON(!host);
271 	BUG_ON(!cxd);
272 
273 	cmd.opcode = opcode;
274 	cmd.arg = arg;
275 	cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
276 
277 	err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
278 	if (err)
279 		return err;
280 
281 	memcpy(cxd, cmd.resp, sizeof(u32) * 4);
282 
283 	return 0;
284 }
285 
286 /*
287  * NOTE: void *buf, caller for the buf is required to use DMA-capable
288  * buffer or on-stack buffer (with some overhead in callee).
289  */
290 static int
291 mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
292 		u32 opcode, void *buf, unsigned len)
293 {
294 	struct mmc_request mrq = {NULL};
295 	struct mmc_command cmd = {0};
296 	struct mmc_data data = {0};
297 	struct scatterlist sg;
298 
299 	mrq.cmd = &cmd;
300 	mrq.data = &data;
301 
302 	cmd.opcode = opcode;
303 	cmd.arg = 0;
304 
305 	/* NOTE HACK:  the MMC_RSP_SPI_R1 is always correct here, but we
306 	 * rely on callers to never use this with "native" calls for reading
307 	 * CSD or CID.  Native versions of those commands use the R2 type,
308 	 * not R1 plus a data block.
309 	 */
310 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
311 
312 	data.blksz = len;
313 	data.blocks = 1;
314 	data.flags = MMC_DATA_READ;
315 	data.sg = &sg;
316 	data.sg_len = 1;
317 
318 	sg_init_one(&sg, buf, len);
319 
320 	if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
321 		/*
322 		 * The spec states that CSR and CID accesses have a timeout
323 		 * of 64 clock cycles.
324 		 */
325 		data.timeout_ns = 0;
326 		data.timeout_clks = 64;
327 	} else
328 		mmc_set_data_timeout(&data, card);
329 
330 	mmc_wait_for_req(host, &mrq);
331 
332 	if (cmd.error)
333 		return cmd.error;
334 	if (data.error)
335 		return data.error;
336 
337 	return 0;
338 }
339 
340 int mmc_send_csd(struct mmc_card *card, u32 *csd)
341 {
342 	int ret, i;
343 	u32 *csd_tmp;
344 
345 	if (!mmc_host_is_spi(card->host))
346 		return mmc_send_cxd_native(card->host, card->rca << 16,
347 				csd, MMC_SEND_CSD);
348 
349 	csd_tmp = kzalloc(16, GFP_KERNEL);
350 	if (!csd_tmp)
351 		return -ENOMEM;
352 
353 	ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
354 	if (ret)
355 		goto err;
356 
357 	for (i = 0;i < 4;i++)
358 		csd[i] = be32_to_cpu(csd_tmp[i]);
359 
360 err:
361 	kfree(csd_tmp);
362 	return ret;
363 }
364 
365 int mmc_send_cid(struct mmc_host *host, u32 *cid)
366 {
367 	int ret, i;
368 	u32 *cid_tmp;
369 
370 	if (!mmc_host_is_spi(host)) {
371 		if (!host->card)
372 			return -EINVAL;
373 		return mmc_send_cxd_native(host, host->card->rca << 16,
374 				cid, MMC_SEND_CID);
375 	}
376 
377 	cid_tmp = kzalloc(16, GFP_KERNEL);
378 	if (!cid_tmp)
379 		return -ENOMEM;
380 
381 	ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
382 	if (ret)
383 		goto err;
384 
385 	for (i = 0;i < 4;i++)
386 		cid[i] = be32_to_cpu(cid_tmp[i]);
387 
388 err:
389 	kfree(cid_tmp);
390 	return ret;
391 }
392 
393 int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
394 {
395 	int err;
396 	u8 *ext_csd;
397 
398 	if (!card || !new_ext_csd)
399 		return -EINVAL;
400 
401 	if (!mmc_can_ext_csd(card))
402 		return -EOPNOTSUPP;
403 
404 	/*
405 	 * As the ext_csd is so large and mostly unused, we don't store the
406 	 * raw block in mmc_card.
407 	 */
408 	ext_csd = kzalloc(512, GFP_KERNEL);
409 	if (!ext_csd)
410 		return -ENOMEM;
411 
412 	err = mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, ext_csd,
413 				512);
414 	if (err)
415 		kfree(ext_csd);
416 	else
417 		*new_ext_csd = ext_csd;
418 
419 	return err;
420 }
421 EXPORT_SYMBOL_GPL(mmc_get_ext_csd);
422 
423 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
424 {
425 	struct mmc_command cmd = {0};
426 	int err;
427 
428 	cmd.opcode = MMC_SPI_READ_OCR;
429 	cmd.arg = highcap ? (1 << 30) : 0;
430 	cmd.flags = MMC_RSP_SPI_R3;
431 
432 	err = mmc_wait_for_cmd(host, &cmd, 0);
433 
434 	*ocrp = cmd.resp[1];
435 	return err;
436 }
437 
438 int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
439 {
440 	struct mmc_command cmd = {0};
441 	int err;
442 
443 	cmd.opcode = MMC_SPI_CRC_ON_OFF;
444 	cmd.flags = MMC_RSP_SPI_R1;
445 	cmd.arg = use_crc;
446 
447 	err = mmc_wait_for_cmd(host, &cmd, 0);
448 	if (!err)
449 		host->use_spi_crc = use_crc;
450 	return err;
451 }
452 
453 int mmc_switch_status_error(struct mmc_host *host, u32 status)
454 {
455 	if (mmc_host_is_spi(host)) {
456 		if (status & R1_SPI_ILLEGAL_COMMAND)
457 			return -EBADMSG;
458 	} else {
459 		if (status & 0xFDFFA000)
460 			pr_warn("%s: unexpected status %#x after switch\n",
461 				mmc_hostname(host), status);
462 		if (status & R1_SWITCH_ERROR)
463 			return -EBADMSG;
464 	}
465 	return 0;
466 }
467 
468 /**
469  *	__mmc_switch - modify EXT_CSD register
470  *	@card: the MMC card associated with the data transfer
471  *	@set: cmd set values
472  *	@index: EXT_CSD register index
473  *	@value: value to program into EXT_CSD register
474  *	@timeout_ms: timeout (ms) for operation performed by register write,
475  *                   timeout of zero implies maximum possible timeout
476  *	@use_busy_signal: use the busy signal as response type
477  *	@send_status: send status cmd to poll for busy
478  *	@ignore_crc: ignore CRC errors when sending status cmd to poll for busy
479  *
480  *	Modifies the EXT_CSD register for selected card.
481  */
482 int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
483 		unsigned int timeout_ms, bool use_busy_signal, bool send_status,
484 		bool ignore_crc)
485 {
486 	struct mmc_host *host = card->host;
487 	int err;
488 	struct mmc_command cmd = {0};
489 	unsigned long timeout;
490 	u32 status = 0;
491 	bool use_r1b_resp = use_busy_signal;
492 	bool expired = false;
493 
494 	mmc_retune_hold(host);
495 
496 	/*
497 	 * If the cmd timeout and the max_busy_timeout of the host are both
498 	 * specified, let's validate them. A failure means we need to prevent
499 	 * the host from doing hw busy detection, which is done by converting
500 	 * to a R1 response instead of a R1B.
501 	 */
502 	if (timeout_ms && host->max_busy_timeout &&
503 		(timeout_ms > host->max_busy_timeout))
504 		use_r1b_resp = false;
505 
506 	cmd.opcode = MMC_SWITCH;
507 	cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
508 		  (index << 16) |
509 		  (value << 8) |
510 		  set;
511 	cmd.flags = MMC_CMD_AC;
512 	if (use_r1b_resp) {
513 		cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
514 		/*
515 		 * A busy_timeout of zero means the host can decide to use
516 		 * whatever value it finds suitable.
517 		 */
518 		cmd.busy_timeout = timeout_ms;
519 	} else {
520 		cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
521 	}
522 
523 	if (index == EXT_CSD_SANITIZE_START)
524 		cmd.sanitize_busy = true;
525 
526 	err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
527 	if (err)
528 		goto out;
529 
530 	/* No need to check card status in case of unblocking command */
531 	if (!use_busy_signal)
532 		goto out;
533 
534 	/*
535 	 * CRC errors shall only be ignored in cases were CMD13 is used to poll
536 	 * to detect busy completion.
537 	 */
538 	if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
539 		ignore_crc = false;
540 
541 	/* We have an unspecified cmd timeout, use the fallback value. */
542 	if (!timeout_ms)
543 		timeout_ms = MMC_OPS_TIMEOUT_MS;
544 
545 	/* Must check status to be sure of no errors. */
546 	timeout = jiffies + msecs_to_jiffies(timeout_ms);
547 	do {
548 		if (send_status) {
549 			/*
550 			 * Due to the possibility of being preempted after
551 			 * sending the status command, check the expiration
552 			 * time first.
553 			 */
554 			expired = time_after(jiffies, timeout);
555 			err = __mmc_send_status(card, &status, ignore_crc);
556 			if (err)
557 				goto out;
558 		}
559 		if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
560 			break;
561 		if (mmc_host_is_spi(host))
562 			break;
563 
564 		/*
565 		 * We are not allowed to issue a status command and the host
566 		 * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only
567 		 * rely on waiting for the stated timeout to be sufficient.
568 		 */
569 		if (!send_status) {
570 			mmc_delay(timeout_ms);
571 			goto out;
572 		}
573 
574 		/* Timeout if the device never leaves the program state. */
575 		if (expired && R1_CURRENT_STATE(status) == R1_STATE_PRG) {
576 			pr_err("%s: Card stuck in programming state! %s\n",
577 				mmc_hostname(host), __func__);
578 			err = -ETIMEDOUT;
579 			goto out;
580 		}
581 	} while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
582 
583 	err = mmc_switch_status_error(host, status);
584 out:
585 	mmc_retune_release(host);
586 
587 	return err;
588 }
589 
590 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
591 		unsigned int timeout_ms)
592 {
593 	return __mmc_switch(card, set, index, value, timeout_ms, true, true,
594 				false);
595 }
596 EXPORT_SYMBOL_GPL(mmc_switch);
597 
598 int mmc_send_tuning(struct mmc_host *host, u32 opcode, int *cmd_error)
599 {
600 	struct mmc_request mrq = {NULL};
601 	struct mmc_command cmd = {0};
602 	struct mmc_data data = {0};
603 	struct scatterlist sg;
604 	struct mmc_ios *ios = &host->ios;
605 	const u8 *tuning_block_pattern;
606 	int size, err = 0;
607 	u8 *data_buf;
608 
609 	if (ios->bus_width == MMC_BUS_WIDTH_8) {
610 		tuning_block_pattern = tuning_blk_pattern_8bit;
611 		size = sizeof(tuning_blk_pattern_8bit);
612 	} else if (ios->bus_width == MMC_BUS_WIDTH_4) {
613 		tuning_block_pattern = tuning_blk_pattern_4bit;
614 		size = sizeof(tuning_blk_pattern_4bit);
615 	} else
616 		return -EINVAL;
617 
618 	data_buf = kzalloc(size, GFP_KERNEL);
619 	if (!data_buf)
620 		return -ENOMEM;
621 
622 	mrq.cmd = &cmd;
623 	mrq.data = &data;
624 
625 	cmd.opcode = opcode;
626 	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
627 
628 	data.blksz = size;
629 	data.blocks = 1;
630 	data.flags = MMC_DATA_READ;
631 
632 	/*
633 	 * According to the tuning specs, Tuning process
634 	 * is normally shorter 40 executions of CMD19,
635 	 * and timeout value should be shorter than 150 ms
636 	 */
637 	data.timeout_ns = 150 * NSEC_PER_MSEC;
638 
639 	data.sg = &sg;
640 	data.sg_len = 1;
641 	sg_init_one(&sg, data_buf, size);
642 
643 	mmc_wait_for_req(host, &mrq);
644 
645 	if (cmd_error)
646 		*cmd_error = cmd.error;
647 
648 	if (cmd.error) {
649 		err = cmd.error;
650 		goto out;
651 	}
652 
653 	if (data.error) {
654 		err = data.error;
655 		goto out;
656 	}
657 
658 	if (memcmp(data_buf, tuning_block_pattern, size))
659 		err = -EIO;
660 
661 out:
662 	kfree(data_buf);
663 	return err;
664 }
665 EXPORT_SYMBOL_GPL(mmc_send_tuning);
666 
667 static int
668 mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
669 		  u8 len)
670 {
671 	struct mmc_request mrq = {NULL};
672 	struct mmc_command cmd = {0};
673 	struct mmc_data data = {0};
674 	struct scatterlist sg;
675 	u8 *data_buf;
676 	u8 *test_buf;
677 	int i, err;
678 	static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
679 	static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
680 
681 	/* dma onto stack is unsafe/nonportable, but callers to this
682 	 * routine normally provide temporary on-stack buffers ...
683 	 */
684 	data_buf = kmalloc(len, GFP_KERNEL);
685 	if (!data_buf)
686 		return -ENOMEM;
687 
688 	if (len == 8)
689 		test_buf = testdata_8bit;
690 	else if (len == 4)
691 		test_buf = testdata_4bit;
692 	else {
693 		pr_err("%s: Invalid bus_width %d\n",
694 		       mmc_hostname(host), len);
695 		kfree(data_buf);
696 		return -EINVAL;
697 	}
698 
699 	if (opcode == MMC_BUS_TEST_W)
700 		memcpy(data_buf, test_buf, len);
701 
702 	mrq.cmd = &cmd;
703 	mrq.data = &data;
704 	cmd.opcode = opcode;
705 	cmd.arg = 0;
706 
707 	/* NOTE HACK:  the MMC_RSP_SPI_R1 is always correct here, but we
708 	 * rely on callers to never use this with "native" calls for reading
709 	 * CSD or CID.  Native versions of those commands use the R2 type,
710 	 * not R1 plus a data block.
711 	 */
712 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
713 
714 	data.blksz = len;
715 	data.blocks = 1;
716 	if (opcode == MMC_BUS_TEST_R)
717 		data.flags = MMC_DATA_READ;
718 	else
719 		data.flags = MMC_DATA_WRITE;
720 
721 	data.sg = &sg;
722 	data.sg_len = 1;
723 	mmc_set_data_timeout(&data, card);
724 	sg_init_one(&sg, data_buf, len);
725 	mmc_wait_for_req(host, &mrq);
726 	err = 0;
727 	if (opcode == MMC_BUS_TEST_R) {
728 		for (i = 0; i < len / 4; i++)
729 			if ((test_buf[i] ^ data_buf[i]) != 0xff) {
730 				err = -EIO;
731 				break;
732 			}
733 	}
734 	kfree(data_buf);
735 
736 	if (cmd.error)
737 		return cmd.error;
738 	if (data.error)
739 		return data.error;
740 
741 	return err;
742 }
743 
744 int mmc_bus_test(struct mmc_card *card, u8 bus_width)
745 {
746 	int err, width;
747 
748 	if (bus_width == MMC_BUS_WIDTH_8)
749 		width = 8;
750 	else if (bus_width == MMC_BUS_WIDTH_4)
751 		width = 4;
752 	else if (bus_width == MMC_BUS_WIDTH_1)
753 		return 0; /* no need for test */
754 	else
755 		return -EINVAL;
756 
757 	/*
758 	 * Ignore errors from BUS_TEST_W.  BUS_TEST_R will fail if there
759 	 * is a problem.  This improves chances that the test will work.
760 	 */
761 	mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
762 	err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
763 	return err;
764 }
765 
766 int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
767 {
768 	struct mmc_command cmd = {0};
769 	unsigned int opcode;
770 	int err;
771 
772 	if (!card->ext_csd.hpi) {
773 		pr_warn("%s: Card didn't support HPI command\n",
774 			mmc_hostname(card->host));
775 		return -EINVAL;
776 	}
777 
778 	opcode = card->ext_csd.hpi_cmd;
779 	if (opcode == MMC_STOP_TRANSMISSION)
780 		cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
781 	else if (opcode == MMC_SEND_STATUS)
782 		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
783 
784 	cmd.opcode = opcode;
785 	cmd.arg = card->rca << 16 | 1;
786 
787 	err = mmc_wait_for_cmd(card->host, &cmd, 0);
788 	if (err) {
789 		pr_warn("%s: error %d interrupting operation. "
790 			"HPI command response %#x\n", mmc_hostname(card->host),
791 			err, cmd.resp[0]);
792 		return err;
793 	}
794 	if (status)
795 		*status = cmd.resp[0];
796 
797 	return 0;
798 }
799 
800 int mmc_can_ext_csd(struct mmc_card *card)
801 {
802 	return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3);
803 }
804