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 #define MMC_OPS_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
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,
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,
57 static inline int __mmc_send_status(struct mmc_card *card, u32 *status,
61 struct mmc_command cmd = {0};
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;
71 cmd.flags &= ~MMC_RSP_CRC;
73 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
77 /* NOTE: callers are required to understand the difference
78 * between "native" and SPI format status words!
81 *status = cmd.resp[0];
86 int mmc_send_status(struct mmc_card *card, u32 *status)
88 return __mmc_send_status(card, status, false);
91 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
94 struct mmc_command cmd = {0};
98 cmd.opcode = MMC_SELECT_CARD;
101 cmd.arg = card->rca << 16;
102 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
105 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
108 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
115 int mmc_select_card(struct mmc_card *card)
119 return _mmc_select_card(card->host, card);
122 int mmc_deselect_cards(struct mmc_host *host)
124 return _mmc_select_card(host, NULL);
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,
135 int mmc_set_dsr(struct mmc_host *host)
137 struct mmc_command cmd = {0};
139 cmd.opcode = MMC_SET_DSR;
141 cmd.arg = (host->dsr << 16) | 0xffff;
142 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
144 return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
147 int mmc_go_idle(struct mmc_host *host)
150 struct mmc_command cmd = {0};
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.
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.
161 if (!mmc_host_is_spi(host)) {
162 mmc_set_chip_select(host, MMC_CS_HIGH);
166 cmd.opcode = MMC_GO_IDLE_STATE;
168 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
170 err = mmc_wait_for_cmd(host, &cmd, 0);
174 if (!mmc_host_is_spi(host)) {
175 mmc_set_chip_select(host, MMC_CS_DONTCARE);
179 host->use_spi_crc = 0;
184 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
186 struct mmc_command cmd = {0};
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;
195 for (i = 100; i; i--) {
196 err = mmc_wait_for_cmd(host, &cmd, 0);
200 /* if we're just probing, do a single pass */
204 /* otherwise wait until reset completes */
205 if (mmc_host_is_spi(host)) {
206 if (!(cmd.resp[0] & R1_SPI_IDLE))
209 if (cmd.resp[0] & MMC_CARD_BUSY)
218 if (rocr && !mmc_host_is_spi(host))
224 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
227 struct mmc_command cmd = {0};
232 cmd.opcode = MMC_ALL_SEND_CID;
234 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
236 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
240 memcpy(cid, cmd.resp, sizeof(u32) * 4);
245 int mmc_set_relative_addr(struct mmc_card *card)
248 struct mmc_command cmd = {0};
253 cmd.opcode = MMC_SET_RELATIVE_ADDR;
254 cmd.arg = card->rca << 16;
255 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
257 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
265 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
268 struct mmc_command cmd = {0};
275 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
277 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
281 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
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).
291 mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
292 u32 opcode, void *buf, unsigned len)
294 struct mmc_request mrq = {NULL};
295 struct mmc_command cmd = {0};
296 struct mmc_data data = {0};
297 struct scatterlist sg;
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.
310 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
314 data.flags = MMC_DATA_READ;
318 sg_init_one(&sg, buf, len);
320 if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
322 * The spec states that CSR and CID accesses have a timeout
323 * of 64 clock cycles.
326 data.timeout_clks = 64;
328 mmc_set_data_timeout(&data, card);
330 mmc_wait_for_req(host, &mrq);
340 int mmc_send_csd(struct mmc_card *card, u32 *csd)
345 if (!mmc_host_is_spi(card->host))
346 return mmc_send_cxd_native(card->host, card->rca << 16,
349 csd_tmp = kzalloc(16, GFP_KERNEL);
353 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
357 for (i = 0;i < 4;i++)
358 csd[i] = be32_to_cpu(csd_tmp[i]);
365 int mmc_send_cid(struct mmc_host *host, u32 *cid)
370 if (!mmc_host_is_spi(host)) {
373 return mmc_send_cxd_native(host, host->card->rca << 16,
377 cid_tmp = kzalloc(16, GFP_KERNEL);
381 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
385 for (i = 0;i < 4;i++)
386 cid[i] = be32_to_cpu(cid_tmp[i]);
393 int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
398 if (!card || !new_ext_csd)
401 if (!mmc_can_ext_csd(card))
405 * As the ext_csd is so large and mostly unused, we don't store the
406 * raw block in mmc_card.
408 ext_csd = kzalloc(512, GFP_KERNEL);
412 err = mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, ext_csd,
417 *new_ext_csd = ext_csd;
421 EXPORT_SYMBOL_GPL(mmc_get_ext_csd);
423 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
425 struct mmc_command cmd = {0};
428 cmd.opcode = MMC_SPI_READ_OCR;
429 cmd.arg = highcap ? (1 << 30) : 0;
430 cmd.flags = MMC_RSP_SPI_R3;
432 err = mmc_wait_for_cmd(host, &cmd, 0);
438 int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
440 struct mmc_command cmd = {0};
443 cmd.opcode = MMC_SPI_CRC_ON_OFF;
444 cmd.flags = MMC_RSP_SPI_R1;
447 err = mmc_wait_for_cmd(host, &cmd, 0);
449 host->use_spi_crc = use_crc;
453 int mmc_switch_status_error(struct mmc_host *host, u32 status)
455 if (mmc_host_is_spi(host)) {
456 if (status & R1_SPI_ILLEGAL_COMMAND)
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)
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
480 * Modifies the EXT_CSD register for selected card.
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,
486 struct mmc_host *host = card->host;
488 struct mmc_command cmd = {0};
489 unsigned long timeout;
491 bool use_r1b_resp = use_busy_signal;
493 mmc_retune_hold(host);
496 * If the cmd timeout and the max_busy_timeout of the host are both
497 * specified, let's validate them. A failure means we need to prevent
498 * the host from doing hw busy detection, which is done by converting
499 * to a R1 response instead of a R1B.
501 if (timeout_ms && host->max_busy_timeout &&
502 (timeout_ms > host->max_busy_timeout))
503 use_r1b_resp = false;
505 cmd.opcode = MMC_SWITCH;
506 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
510 cmd.flags = MMC_CMD_AC;
512 cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
514 * A busy_timeout of zero means the host can decide to use
515 * whatever value it finds suitable.
517 cmd.busy_timeout = timeout_ms;
519 cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
522 if (index == EXT_CSD_SANITIZE_START)
523 cmd.sanitize_busy = true;
525 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
529 /* No need to check card status in case of unblocking command */
530 if (!use_busy_signal)
534 * CRC errors shall only be ignored in cases were CMD13 is used to poll
535 * to detect busy completion.
537 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
540 /* We have an unspecified cmd timeout, use the fallback value. */
542 timeout_ms = MMC_OPS_TIMEOUT_MS;
544 /* Must check status to be sure of no errors. */
545 timeout = jiffies + msecs_to_jiffies(timeout_ms);
548 err = __mmc_send_status(card, &status, ignore_crc);
552 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
554 if (mmc_host_is_spi(host))
558 * We are not allowed to issue a status command and the host
559 * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only
560 * rely on waiting for the stated timeout to be sufficient.
563 mmc_delay(timeout_ms);
567 /* Timeout if the device never leaves the program state. */
568 if (time_after(jiffies, timeout)) {
569 pr_err("%s: Card stuck in programming state! %s\n",
570 mmc_hostname(host), __func__);
574 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
576 err = mmc_switch_status_error(host, status);
578 mmc_retune_release(host);
583 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
584 unsigned int timeout_ms)
586 return __mmc_switch(card, set, index, value, timeout_ms, true, true,
589 EXPORT_SYMBOL_GPL(mmc_switch);
591 int mmc_send_tuning(struct mmc_host *host, u32 opcode, int *cmd_error)
593 struct mmc_request mrq = {NULL};
594 struct mmc_command cmd = {0};
595 struct mmc_data data = {0};
596 struct scatterlist sg;
597 struct mmc_ios *ios = &host->ios;
598 const u8 *tuning_block_pattern;
602 if (ios->bus_width == MMC_BUS_WIDTH_8) {
603 tuning_block_pattern = tuning_blk_pattern_8bit;
604 size = sizeof(tuning_blk_pattern_8bit);
605 } else if (ios->bus_width == MMC_BUS_WIDTH_4) {
606 tuning_block_pattern = tuning_blk_pattern_4bit;
607 size = sizeof(tuning_blk_pattern_4bit);
611 data_buf = kzalloc(size, GFP_KERNEL);
619 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
623 data.flags = MMC_DATA_READ;
626 * According to the tuning specs, Tuning process
627 * is normally shorter 40 executions of CMD19,
628 * and timeout value should be shorter than 150 ms
630 data.timeout_ns = 150 * NSEC_PER_MSEC;
634 sg_init_one(&sg, data_buf, size);
636 mmc_wait_for_req(host, &mrq);
639 *cmd_error = cmd.error;
651 if (memcmp(data_buf, tuning_block_pattern, size))
658 EXPORT_SYMBOL_GPL(mmc_send_tuning);
661 mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
664 struct mmc_request mrq = {NULL};
665 struct mmc_command cmd = {0};
666 struct mmc_data data = {0};
667 struct scatterlist sg;
671 static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
672 static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
674 /* dma onto stack is unsafe/nonportable, but callers to this
675 * routine normally provide temporary on-stack buffers ...
677 data_buf = kmalloc(len, GFP_KERNEL);
682 test_buf = testdata_8bit;
684 test_buf = testdata_4bit;
686 pr_err("%s: Invalid bus_width %d\n",
687 mmc_hostname(host), len);
692 if (opcode == MMC_BUS_TEST_W)
693 memcpy(data_buf, test_buf, len);
700 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
701 * rely on callers to never use this with "native" calls for reading
702 * CSD or CID. Native versions of those commands use the R2 type,
703 * not R1 plus a data block.
705 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
709 if (opcode == MMC_BUS_TEST_R)
710 data.flags = MMC_DATA_READ;
712 data.flags = MMC_DATA_WRITE;
716 mmc_set_data_timeout(&data, card);
717 sg_init_one(&sg, data_buf, len);
718 mmc_wait_for_req(host, &mrq);
720 if (opcode == MMC_BUS_TEST_R) {
721 for (i = 0; i < len / 4; i++)
722 if ((test_buf[i] ^ data_buf[i]) != 0xff) {
737 int mmc_bus_test(struct mmc_card *card, u8 bus_width)
741 if (bus_width == MMC_BUS_WIDTH_8)
743 else if (bus_width == MMC_BUS_WIDTH_4)
745 else if (bus_width == MMC_BUS_WIDTH_1)
746 return 0; /* no need for test */
751 * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
752 * is a problem. This improves chances that the test will work.
754 mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
755 err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
759 int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
761 struct mmc_command cmd = {0};
765 if (!card->ext_csd.hpi) {
766 pr_warn("%s: Card didn't support HPI command\n",
767 mmc_hostname(card->host));
771 opcode = card->ext_csd.hpi_cmd;
772 if (opcode == MMC_STOP_TRANSMISSION)
773 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
774 else if (opcode == MMC_SEND_STATUS)
775 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
778 cmd.arg = card->rca << 16 | 1;
780 err = mmc_wait_for_cmd(card->host, &cmd, 0);
782 pr_warn("%s: error %d interrupting operation. "
783 "HPI command response %#x\n", mmc_hostname(card->host),
788 *status = cmd.resp[0];
793 int mmc_can_ext_csd(struct mmc_card *card)
795 return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3);