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>
24 #define MMC_OPS_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
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,
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,
56 static inline int __mmc_send_status(struct mmc_card *card, u32 *status,
60 struct mmc_command cmd = {0};
65 cmd.opcode = MMC_SEND_STATUS;
66 if (!mmc_host_is_spi(card->host))
67 cmd.arg = card->rca << 16;
68 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
70 cmd.flags &= ~MMC_RSP_CRC;
72 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
76 /* NOTE: callers are required to understand the difference
77 * between "native" and SPI format status words!
80 *status = cmd.resp[0];
85 int mmc_send_status(struct mmc_card *card, u32 *status)
87 return __mmc_send_status(card, status, false);
90 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
93 struct mmc_command cmd = {0};
97 cmd.opcode = MMC_SELECT_CARD;
100 cmd.arg = card->rca << 16;
101 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
104 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
107 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
114 int mmc_select_card(struct mmc_card *card)
118 return _mmc_select_card(card->host, card);
121 int mmc_deselect_cards(struct mmc_host *host)
123 return _mmc_select_card(host, NULL);
127 * Write the value specified in the device tree or board code into the optional
128 * 16 bit Driver Stage Register. This can be used to tune raise/fall times and
129 * drive strength of the DAT and CMD outputs. The actual meaning of a given
130 * value is hardware dependant.
131 * The presence of the DSR register can be determined from the CSD register,
134 int mmc_set_dsr(struct mmc_host *host)
136 struct mmc_command cmd = {0};
138 cmd.opcode = MMC_SET_DSR;
140 cmd.arg = (host->dsr << 16) | 0xffff;
141 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
143 return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
146 int mmc_go_idle(struct mmc_host *host)
149 struct mmc_command cmd = {0};
152 * Non-SPI hosts need to prevent chipselect going active during
153 * GO_IDLE; that would put chips into SPI mode. Remind them of
154 * that in case of hardware that won't pull up DAT3/nCS otherwise.
156 * SPI hosts ignore ios.chip_select; it's managed according to
157 * rules that must accommodate non-MMC slaves which this layer
158 * won't even know about.
160 if (!mmc_host_is_spi(host)) {
161 mmc_set_chip_select(host, MMC_CS_HIGH);
165 cmd.opcode = MMC_GO_IDLE_STATE;
167 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
169 err = mmc_wait_for_cmd(host, &cmd, 0);
173 if (!mmc_host_is_spi(host)) {
174 mmc_set_chip_select(host, MMC_CS_DONTCARE);
178 host->use_spi_crc = 0;
183 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
185 struct mmc_command cmd = {0};
190 cmd.opcode = MMC_SEND_OP_COND;
191 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
192 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
194 for (i = 100; i; i--) {
195 err = mmc_wait_for_cmd(host, &cmd, 0);
199 /* if we're just probing, do a single pass */
203 /* otherwise wait until reset completes */
204 if (mmc_host_is_spi(host)) {
205 if (!(cmd.resp[0] & R1_SPI_IDLE))
208 if (cmd.resp[0] & MMC_CARD_BUSY)
217 if (rocr && !mmc_host_is_spi(host))
223 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
226 struct mmc_command cmd = {0};
231 cmd.opcode = MMC_ALL_SEND_CID;
233 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
235 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
239 memcpy(cid, cmd.resp, sizeof(u32) * 4);
244 int mmc_set_relative_addr(struct mmc_card *card)
247 struct mmc_command cmd = {0};
252 cmd.opcode = MMC_SET_RELATIVE_ADDR;
253 cmd.arg = card->rca << 16;
254 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
256 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
264 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
267 struct mmc_command cmd = {0};
274 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
276 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
280 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
286 * NOTE: void *buf, caller for the buf is required to use DMA-capable
287 * buffer or on-stack buffer (with some overhead in callee).
290 mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
291 u32 opcode, void *buf, unsigned len)
293 struct mmc_request mrq = {NULL};
294 struct mmc_command cmd = {0};
295 struct mmc_data data = {0};
296 struct scatterlist sg;
304 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
305 * rely on callers to never use this with "native" calls for reading
306 * CSD or CID. Native versions of those commands use the R2 type,
307 * not R1 plus a data block.
309 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
313 data.flags = MMC_DATA_READ;
317 sg_init_one(&sg, buf, len);
319 if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
321 * The spec states that CSR and CID accesses have a timeout
322 * of 64 clock cycles.
325 data.timeout_clks = 64;
327 mmc_set_data_timeout(&data, card);
329 mmc_wait_for_req(host, &mrq);
339 int mmc_send_csd(struct mmc_card *card, u32 *csd)
344 if (!mmc_host_is_spi(card->host))
345 return mmc_send_cxd_native(card->host, card->rca << 16,
348 csd_tmp = kzalloc(16, GFP_KERNEL);
352 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
356 for (i = 0;i < 4;i++)
357 csd[i] = be32_to_cpu(csd_tmp[i]);
364 int mmc_send_cid(struct mmc_host *host, u32 *cid)
369 if (!mmc_host_is_spi(host)) {
372 return mmc_send_cxd_native(host, host->card->rca << 16,
376 cid_tmp = kzalloc(16, GFP_KERNEL);
380 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
384 for (i = 0;i < 4;i++)
385 cid[i] = be32_to_cpu(cid_tmp[i]);
392 int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
397 if (!card || !new_ext_csd)
400 if (!mmc_can_ext_csd(card))
404 * As the ext_csd is so large and mostly unused, we don't store the
405 * raw block in mmc_card.
407 ext_csd = kzalloc(512, GFP_KERNEL);
411 err = mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, ext_csd,
416 *new_ext_csd = ext_csd;
420 EXPORT_SYMBOL_GPL(mmc_get_ext_csd);
422 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
424 struct mmc_command cmd = {0};
427 cmd.opcode = MMC_SPI_READ_OCR;
428 cmd.arg = highcap ? (1 << 30) : 0;
429 cmd.flags = MMC_RSP_SPI_R3;
431 err = mmc_wait_for_cmd(host, &cmd, 0);
437 int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
439 struct mmc_command cmd = {0};
442 cmd.opcode = MMC_SPI_CRC_ON_OFF;
443 cmd.flags = MMC_RSP_SPI_R1;
446 err = mmc_wait_for_cmd(host, &cmd, 0);
448 host->use_spi_crc = use_crc;
453 * __mmc_switch - modify EXT_CSD register
454 * @card: the MMC card associated with the data transfer
455 * @set: cmd set values
456 * @index: EXT_CSD register index
457 * @value: value to program into EXT_CSD register
458 * @timeout_ms: timeout (ms) for operation performed by register write,
459 * timeout of zero implies maximum possible timeout
460 * @use_busy_signal: use the busy signal as response type
461 * @send_status: send status cmd to poll for busy
462 * @ignore_crc: ignore CRC errors when sending status cmd to poll for busy
464 * Modifies the EXT_CSD register for selected card.
466 int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
467 unsigned int timeout_ms, bool use_busy_signal, bool send_status,
470 struct mmc_host *host = card->host;
472 struct mmc_command cmd = {0};
473 unsigned long timeout;
475 bool use_r1b_resp = use_busy_signal;
478 * If the cmd timeout and the max_busy_timeout of the host are both
479 * specified, let's validate them. A failure means we need to prevent
480 * the host from doing hw busy detection, which is done by converting
481 * to a R1 response instead of a R1B.
483 if (timeout_ms && host->max_busy_timeout &&
484 (timeout_ms > host->max_busy_timeout))
485 use_r1b_resp = false;
487 cmd.opcode = MMC_SWITCH;
488 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
492 cmd.flags = MMC_CMD_AC;
494 cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
496 * A busy_timeout of zero means the host can decide to use
497 * whatever value it finds suitable.
499 cmd.busy_timeout = timeout_ms;
501 cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
504 if (index == EXT_CSD_SANITIZE_START)
505 cmd.sanitize_busy = true;
507 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
511 /* No need to check card status in case of unblocking command */
512 if (!use_busy_signal)
516 * CRC errors shall only be ignored in cases were CMD13 is used to poll
517 * to detect busy completion.
519 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
522 /* We have an unspecified cmd timeout, use the fallback value. */
524 timeout_ms = MMC_OPS_TIMEOUT_MS;
526 /* Must check status to be sure of no errors. */
527 timeout = jiffies + msecs_to_jiffies(timeout_ms);
530 err = __mmc_send_status(card, &status, ignore_crc);
534 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
536 if (mmc_host_is_spi(host))
540 * We are not allowed to issue a status command and the host
541 * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only
542 * rely on waiting for the stated timeout to be sufficient.
545 mmc_delay(timeout_ms);
549 /* Timeout if the device never leaves the program state. */
550 if (time_after(jiffies, timeout)) {
551 pr_err("%s: Card stuck in programming state! %s\n",
552 mmc_hostname(host), __func__);
555 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
557 if (mmc_host_is_spi(host)) {
558 if (status & R1_SPI_ILLEGAL_COMMAND)
561 if (status & 0xFDFFA000)
562 pr_warn("%s: unexpected status %#x after switch\n",
563 mmc_hostname(host), status);
564 if (status & R1_SWITCH_ERROR)
570 EXPORT_SYMBOL_GPL(__mmc_switch);
572 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
573 unsigned int timeout_ms)
575 return __mmc_switch(card, set, index, value, timeout_ms, true, true,
578 EXPORT_SYMBOL_GPL(mmc_switch);
580 int mmc_send_tuning(struct mmc_host *host)
582 struct mmc_request mrq = {NULL};
583 struct mmc_command cmd = {0};
584 struct mmc_data data = {0};
585 struct scatterlist sg;
586 struct mmc_ios *ios = &host->ios;
587 const u8 *tuning_block_pattern;
592 if (ios->bus_width == MMC_BUS_WIDTH_8) {
593 tuning_block_pattern = tuning_blk_pattern_8bit;
594 size = sizeof(tuning_blk_pattern_8bit);
595 opcode = MMC_SEND_TUNING_BLOCK_HS200;
596 } else if (ios->bus_width == MMC_BUS_WIDTH_4) {
597 tuning_block_pattern = tuning_blk_pattern_4bit;
598 size = sizeof(tuning_blk_pattern_4bit);
599 opcode = MMC_SEND_TUNING_BLOCK;
603 data_buf = kzalloc(size, GFP_KERNEL);
611 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
615 data.flags = MMC_DATA_READ;
618 * According to the tuning specs, Tuning process
619 * is normally shorter 40 executions of CMD19,
620 * and timeout value should be shorter than 150 ms
622 data.timeout_ns = 150 * NSEC_PER_MSEC;
626 sg_init_one(&sg, data_buf, size);
628 mmc_wait_for_req(host, &mrq);
640 if (memcmp(data_buf, tuning_block_pattern, size))
647 EXPORT_SYMBOL_GPL(mmc_send_tuning);
650 mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
653 struct mmc_request mrq = {NULL};
654 struct mmc_command cmd = {0};
655 struct mmc_data data = {0};
656 struct scatterlist sg;
660 static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
661 static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
663 /* dma onto stack is unsafe/nonportable, but callers to this
664 * routine normally provide temporary on-stack buffers ...
666 data_buf = kmalloc(len, GFP_KERNEL);
671 test_buf = testdata_8bit;
673 test_buf = testdata_4bit;
675 pr_err("%s: Invalid bus_width %d\n",
676 mmc_hostname(host), len);
681 if (opcode == MMC_BUS_TEST_W)
682 memcpy(data_buf, test_buf, len);
689 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
690 * rely on callers to never use this with "native" calls for reading
691 * CSD or CID. Native versions of those commands use the R2 type,
692 * not R1 plus a data block.
694 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
698 if (opcode == MMC_BUS_TEST_R)
699 data.flags = MMC_DATA_READ;
701 data.flags = MMC_DATA_WRITE;
705 mmc_set_data_timeout(&data, card);
706 sg_init_one(&sg, data_buf, len);
707 mmc_wait_for_req(host, &mrq);
709 if (opcode == MMC_BUS_TEST_R) {
710 for (i = 0; i < len / 4; i++)
711 if ((test_buf[i] ^ data_buf[i]) != 0xff) {
726 int mmc_bus_test(struct mmc_card *card, u8 bus_width)
730 if (bus_width == MMC_BUS_WIDTH_8)
732 else if (bus_width == MMC_BUS_WIDTH_4)
734 else if (bus_width == MMC_BUS_WIDTH_1)
735 return 0; /* no need for test */
740 * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
741 * is a problem. This improves chances that the test will work.
743 mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
744 err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
748 int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
750 struct mmc_command cmd = {0};
754 if (!card->ext_csd.hpi) {
755 pr_warn("%s: Card didn't support HPI command\n",
756 mmc_hostname(card->host));
760 opcode = card->ext_csd.hpi_cmd;
761 if (opcode == MMC_STOP_TRANSMISSION)
762 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
763 else if (opcode == MMC_SEND_STATUS)
764 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
767 cmd.arg = card->rca << 16 | 1;
769 err = mmc_wait_for_cmd(card->host, &cmd, 0);
771 pr_warn("%s: error %d interrupting operation. "
772 "HPI command response %#x\n", mmc_hostname(card->host),
777 *status = cmd.resp[0];
782 int mmc_can_ext_csd(struct mmc_card *card)
784 return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3);