2 * linux/drivers/mmc/core/mmc_ops.h
4 * Copyright 2006-2007 Pierre Ossman
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.
12 #include <linux/slab.h>
13 #include <linux/export.h>
14 #include <linux/types.h>
15 #include <linux/scatterlist.h>
17 #include <linux/mmc/host.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/mmc.h>
25 static const u8 tuning_blk_pattern_4bit[] = {
26 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
27 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
28 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
29 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
30 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
31 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
32 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
33 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
36 static const u8 tuning_blk_pattern_8bit[] = {
37 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
38 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
39 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
40 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
41 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
42 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
43 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
44 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
45 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
46 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
47 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
48 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
49 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
50 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
51 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
52 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
55 static inline int __mmc_send_status(struct mmc_card *card, u32 *status,
59 struct mmc_command cmd = {0};
64 cmd.opcode = MMC_SEND_STATUS;
65 if (!mmc_host_is_spi(card->host))
66 cmd.arg = card->rca << 16;
67 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
69 cmd.flags &= ~MMC_RSP_CRC;
71 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
75 /* NOTE: callers are required to understand the difference
76 * between "native" and SPI format status words!
79 *status = cmd.resp[0];
84 int mmc_send_status(struct mmc_card *card, u32 *status)
86 return __mmc_send_status(card, status, false);
89 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
91 struct mmc_command cmd = {0};
95 cmd.opcode = MMC_SELECT_CARD;
98 cmd.arg = card->rca << 16;
99 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
102 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
105 return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
108 int mmc_select_card(struct mmc_card *card)
112 return _mmc_select_card(card->host, card);
115 int mmc_deselect_cards(struct mmc_host *host)
117 return _mmc_select_card(host, NULL);
121 * Write the value specified in the device tree or board code into the optional
122 * 16 bit Driver Stage Register. This can be used to tune raise/fall times and
123 * drive strength of the DAT and CMD outputs. The actual meaning of a given
124 * value is hardware dependant.
125 * The presence of the DSR register can be determined from the CSD register,
128 int mmc_set_dsr(struct mmc_host *host)
130 struct mmc_command cmd = {0};
132 cmd.opcode = MMC_SET_DSR;
134 cmd.arg = (host->dsr << 16) | 0xffff;
135 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
137 return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
140 int mmc_go_idle(struct mmc_host *host)
143 struct mmc_command cmd = {0};
146 * Non-SPI hosts need to prevent chipselect going active during
147 * GO_IDLE; that would put chips into SPI mode. Remind them of
148 * that in case of hardware that won't pull up DAT3/nCS otherwise.
150 * SPI hosts ignore ios.chip_select; it's managed according to
151 * rules that must accommodate non-MMC slaves which this layer
152 * won't even know about.
154 if (!mmc_host_is_spi(host)) {
155 mmc_set_chip_select(host, MMC_CS_HIGH);
159 cmd.opcode = MMC_GO_IDLE_STATE;
161 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
163 err = mmc_wait_for_cmd(host, &cmd, 0);
167 if (!mmc_host_is_spi(host)) {
168 mmc_set_chip_select(host, MMC_CS_DONTCARE);
172 host->use_spi_crc = 0;
177 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
179 struct mmc_command cmd = {0};
184 cmd.opcode = MMC_SEND_OP_COND;
185 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
186 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
188 for (i = 100; i; i--) {
189 err = mmc_wait_for_cmd(host, &cmd, 0);
193 /* if we're just probing, do a single pass */
197 /* otherwise wait until reset completes */
198 if (mmc_host_is_spi(host)) {
199 if (!(cmd.resp[0] & R1_SPI_IDLE))
202 if (cmd.resp[0] & MMC_CARD_BUSY)
211 if (rocr && !mmc_host_is_spi(host))
217 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
220 struct mmc_command cmd = {0};
225 cmd.opcode = MMC_ALL_SEND_CID;
227 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
229 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
233 memcpy(cid, cmd.resp, sizeof(u32) * 4);
238 int mmc_set_relative_addr(struct mmc_card *card)
240 struct mmc_command cmd = {0};
245 cmd.opcode = MMC_SET_RELATIVE_ADDR;
246 cmd.arg = card->rca << 16;
247 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
249 return mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
253 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
256 struct mmc_command cmd = {0};
263 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
265 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
269 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
275 * NOTE: void *buf, caller for the buf is required to use DMA-capable
276 * buffer or on-stack buffer (with some overhead in callee).
279 mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
280 u32 opcode, void *buf, unsigned len)
282 struct mmc_request mrq = {NULL};
283 struct mmc_command cmd = {0};
284 struct mmc_data data = {0};
285 struct scatterlist sg;
293 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
294 * rely on callers to never use this with "native" calls for reading
295 * CSD or CID. Native versions of those commands use the R2 type,
296 * not R1 plus a data block.
298 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
302 data.flags = MMC_DATA_READ;
306 sg_init_one(&sg, buf, len);
308 if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
310 * The spec states that CSR and CID accesses have a timeout
311 * of 64 clock cycles.
314 data.timeout_clks = 64;
316 mmc_set_data_timeout(&data, card);
318 mmc_wait_for_req(host, &mrq);
328 int mmc_send_csd(struct mmc_card *card, u32 *csd)
333 if (!mmc_host_is_spi(card->host))
334 return mmc_send_cxd_native(card->host, card->rca << 16,
337 csd_tmp = kzalloc(16, GFP_KERNEL);
341 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
345 for (i = 0;i < 4;i++)
346 csd[i] = be32_to_cpu(csd_tmp[i]);
353 int mmc_send_cid(struct mmc_host *host, u32 *cid)
358 if (!mmc_host_is_spi(host)) {
361 return mmc_send_cxd_native(host, host->card->rca << 16,
365 cid_tmp = kzalloc(16, GFP_KERNEL);
369 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
373 for (i = 0;i < 4;i++)
374 cid[i] = be32_to_cpu(cid_tmp[i]);
381 int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
386 if (!card || !new_ext_csd)
389 if (!mmc_can_ext_csd(card))
393 * As the ext_csd is so large and mostly unused, we don't store the
394 * raw block in mmc_card.
396 ext_csd = kzalloc(512, GFP_KERNEL);
400 err = mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, ext_csd,
405 *new_ext_csd = ext_csd;
409 EXPORT_SYMBOL_GPL(mmc_get_ext_csd);
411 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
413 struct mmc_command cmd = {0};
416 cmd.opcode = MMC_SPI_READ_OCR;
417 cmd.arg = highcap ? (1 << 30) : 0;
418 cmd.flags = MMC_RSP_SPI_R3;
420 err = mmc_wait_for_cmd(host, &cmd, 0);
426 int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
428 struct mmc_command cmd = {0};
431 cmd.opcode = MMC_SPI_CRC_ON_OFF;
432 cmd.flags = MMC_RSP_SPI_R1;
435 err = mmc_wait_for_cmd(host, &cmd, 0);
437 host->use_spi_crc = use_crc;
441 int mmc_switch_status_error(struct mmc_host *host, u32 status)
443 if (mmc_host_is_spi(host)) {
444 if (status & R1_SPI_ILLEGAL_COMMAND)
447 if (status & 0xFDFFA000)
448 pr_warn("%s: unexpected status %#x after switch\n",
449 mmc_hostname(host), status);
450 if (status & R1_SWITCH_ERROR)
457 * __mmc_switch - modify EXT_CSD register
458 * @card: the MMC card associated with the data transfer
459 * @set: cmd set values
460 * @index: EXT_CSD register index
461 * @value: value to program into EXT_CSD register
462 * @timeout_ms: timeout (ms) for operation performed by register write,
463 * timeout of zero implies maximum possible timeout
464 * @use_busy_signal: use the busy signal as response type
465 * @send_status: send status cmd to poll for busy
466 * @ignore_crc: ignore CRC errors when sending status cmd to poll for busy
468 * Modifies the EXT_CSD register for selected card.
470 int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
471 unsigned int timeout_ms, bool use_busy_signal, bool send_status,
474 struct mmc_host *host = card->host;
476 struct mmc_command cmd = {0};
477 unsigned long timeout;
479 bool use_r1b_resp = use_busy_signal;
480 bool expired = false;
483 mmc_retune_hold(host);
486 * If the cmd timeout and the max_busy_timeout of the host are both
487 * specified, let's validate them. A failure means we need to prevent
488 * the host from doing hw busy detection, which is done by converting
489 * to a R1 response instead of a R1B.
491 if (timeout_ms && host->max_busy_timeout &&
492 (timeout_ms > host->max_busy_timeout))
493 use_r1b_resp = false;
495 cmd.opcode = MMC_SWITCH;
496 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
500 cmd.flags = MMC_CMD_AC;
502 cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
504 * A busy_timeout of zero means the host can decide to use
505 * whatever value it finds suitable.
507 cmd.busy_timeout = timeout_ms;
509 cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
512 if (index == EXT_CSD_SANITIZE_START)
513 cmd.sanitize_busy = true;
515 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
519 /* No need to check card status in case of unblocking command */
520 if (!use_busy_signal)
524 * CRC errors shall only be ignored in cases were CMD13 is used to poll
525 * to detect busy completion.
527 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
530 /* We have an unspecified cmd timeout, use the fallback value. */
532 pr_warn("%s: unspecified timeout for CMD6 - use generic\n",
534 timeout_ms = card->ext_csd.generic_cmd6_time;
537 /* Must check status to be sure of no errors. */
538 timeout = jiffies + msecs_to_jiffies(timeout_ms) + 1;
541 * Due to the possibility of being preempted after
542 * sending the status command, check the expiration
545 expired = time_after(jiffies, timeout);
547 err = __mmc_send_status(card, &status, ignore_crc);
551 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
553 if (host->ops->card_busy) {
554 if (!host->ops->card_busy(host))
558 if (mmc_host_is_spi(host))
562 * We are not allowed to issue a status command and the host
563 * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only
564 * rely on waiting for the stated timeout to be sufficient.
566 if (!send_status && !host->ops->card_busy) {
567 mmc_delay(timeout_ms);
571 /* Timeout if the device never leaves the program state. */
573 (R1_CURRENT_STATE(status) == R1_STATE_PRG || busy)) {
574 pr_err("%s: Card stuck in programming state! %s\n",
575 mmc_hostname(host), __func__);
579 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG || busy);
581 err = mmc_switch_status_error(host, status);
583 mmc_retune_release(host);
588 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
589 unsigned int timeout_ms)
591 return __mmc_switch(card, set, index, value, timeout_ms, true, true,
594 EXPORT_SYMBOL_GPL(mmc_switch);
596 int mmc_send_tuning(struct mmc_host *host, u32 opcode, int *cmd_error)
598 struct mmc_request mrq = {NULL};
599 struct mmc_command cmd = {0};
600 struct mmc_data data = {0};
601 struct scatterlist sg;
602 struct mmc_ios *ios = &host->ios;
603 const u8 *tuning_block_pattern;
607 if (ios->bus_width == MMC_BUS_WIDTH_8) {
608 tuning_block_pattern = tuning_blk_pattern_8bit;
609 size = sizeof(tuning_blk_pattern_8bit);
610 } else if (ios->bus_width == MMC_BUS_WIDTH_4) {
611 tuning_block_pattern = tuning_blk_pattern_4bit;
612 size = sizeof(tuning_blk_pattern_4bit);
616 data_buf = kzalloc(size, GFP_KERNEL);
624 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
628 data.flags = MMC_DATA_READ;
631 * According to the tuning specs, Tuning process
632 * is normally shorter 40 executions of CMD19,
633 * and timeout value should be shorter than 150 ms
635 data.timeout_ns = 150 * NSEC_PER_MSEC;
639 sg_init_one(&sg, data_buf, size);
641 mmc_wait_for_req(host, &mrq);
644 *cmd_error = cmd.error;
656 if (memcmp(data_buf, tuning_block_pattern, size))
663 EXPORT_SYMBOL_GPL(mmc_send_tuning);
666 mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
669 struct mmc_request mrq = {NULL};
670 struct mmc_command cmd = {0};
671 struct mmc_data data = {0};
672 struct scatterlist sg;
676 static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
677 static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
679 /* dma onto stack is unsafe/nonportable, but callers to this
680 * routine normally provide temporary on-stack buffers ...
682 data_buf = kmalloc(len, GFP_KERNEL);
687 test_buf = testdata_8bit;
689 test_buf = testdata_4bit;
691 pr_err("%s: Invalid bus_width %d\n",
692 mmc_hostname(host), len);
697 if (opcode == MMC_BUS_TEST_W)
698 memcpy(data_buf, test_buf, len);
705 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
706 * rely on callers to never use this with "native" calls for reading
707 * CSD or CID. Native versions of those commands use the R2 type,
708 * not R1 plus a data block.
710 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
714 if (opcode == MMC_BUS_TEST_R)
715 data.flags = MMC_DATA_READ;
717 data.flags = MMC_DATA_WRITE;
721 mmc_set_data_timeout(&data, card);
722 sg_init_one(&sg, data_buf, len);
723 mmc_wait_for_req(host, &mrq);
725 if (opcode == MMC_BUS_TEST_R) {
726 for (i = 0; i < len / 4; i++)
727 if ((test_buf[i] ^ data_buf[i]) != 0xff) {
742 int mmc_bus_test(struct mmc_card *card, u8 bus_width)
746 if (bus_width == MMC_BUS_WIDTH_8)
748 else if (bus_width == MMC_BUS_WIDTH_4)
750 else if (bus_width == MMC_BUS_WIDTH_1)
751 return 0; /* no need for test */
756 * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
757 * is a problem. This improves chances that the test will work.
759 mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
760 return mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
763 int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
765 struct mmc_command cmd = {0};
769 if (!card->ext_csd.hpi) {
770 pr_warn("%s: Card didn't support HPI command\n",
771 mmc_hostname(card->host));
775 opcode = card->ext_csd.hpi_cmd;
776 if (opcode == MMC_STOP_TRANSMISSION)
777 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
778 else if (opcode == MMC_SEND_STATUS)
779 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
782 cmd.arg = card->rca << 16 | 1;
784 err = mmc_wait_for_cmd(card->host, &cmd, 0);
786 pr_warn("%s: error %d interrupting operation. "
787 "HPI command response %#x\n", mmc_hostname(card->host),
792 *status = cmd.resp[0];
797 int mmc_can_ext_csd(struct mmc_card *card)
799 return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3);