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 inline int __mmc_send_status(struct mmc_card
*card
, u32
*status
,
30 struct mmc_command cmd
= {0};
35 cmd
.opcode
= MMC_SEND_STATUS
;
36 if (!mmc_host_is_spi(card
->host
))
37 cmd
.arg
= card
->rca
<< 16;
38 cmd
.flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
40 cmd
.flags
&= ~MMC_RSP_CRC
;
42 err
= mmc_wait_for_cmd(card
->host
, &cmd
, MMC_CMD_RETRIES
);
46 /* NOTE: callers are required to understand the difference
47 * between "native" and SPI format status words!
50 *status
= cmd
.resp
[0];
55 int mmc_send_status(struct mmc_card
*card
, u32
*status
)
57 return __mmc_send_status(card
, status
, false);
60 static int _mmc_select_card(struct mmc_host
*host
, struct mmc_card
*card
)
63 struct mmc_command cmd
= {0};
67 cmd
.opcode
= MMC_SELECT_CARD
;
70 cmd
.arg
= card
->rca
<< 16;
71 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
74 cmd
.flags
= MMC_RSP_NONE
| MMC_CMD_AC
;
77 err
= mmc_wait_for_cmd(host
, &cmd
, MMC_CMD_RETRIES
);
84 int mmc_select_card(struct mmc_card
*card
)
88 return _mmc_select_card(card
->host
, card
);
91 int mmc_deselect_cards(struct mmc_host
*host
)
93 return _mmc_select_card(host
, NULL
);
97 * Write the value specified in the device tree or board code into the optional
98 * 16 bit Driver Stage Register. This can be used to tune raise/fall times and
99 * drive strength of the DAT and CMD outputs. The actual meaning of a given
100 * value is hardware dependant.
101 * The presence of the DSR register can be determined from the CSD register,
104 int mmc_set_dsr(struct mmc_host
*host
)
106 struct mmc_command cmd
= {0};
108 cmd
.opcode
= MMC_SET_DSR
;
110 cmd
.arg
= (host
->dsr
<< 16) | 0xffff;
111 cmd
.flags
= MMC_RSP_NONE
| MMC_CMD_AC
;
113 return mmc_wait_for_cmd(host
, &cmd
, MMC_CMD_RETRIES
);
116 int mmc_go_idle(struct mmc_host
*host
)
119 struct mmc_command cmd
= {0};
122 * Non-SPI hosts need to prevent chipselect going active during
123 * GO_IDLE; that would put chips into SPI mode. Remind them of
124 * that in case of hardware that won't pull up DAT3/nCS otherwise.
126 * SPI hosts ignore ios.chip_select; it's managed according to
127 * rules that must accommodate non-MMC slaves which this layer
128 * won't even know about.
130 if (!mmc_host_is_spi(host
)) {
131 mmc_set_chip_select(host
, MMC_CS_HIGH
);
135 cmd
.opcode
= MMC_GO_IDLE_STATE
;
137 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_NONE
| MMC_CMD_BC
;
139 err
= mmc_wait_for_cmd(host
, &cmd
, 0);
143 if (!mmc_host_is_spi(host
)) {
144 mmc_set_chip_select(host
, MMC_CS_DONTCARE
);
148 host
->use_spi_crc
= 0;
153 int mmc_send_op_cond(struct mmc_host
*host
, u32 ocr
, u32
*rocr
)
155 struct mmc_command cmd
= {0};
160 cmd
.opcode
= MMC_SEND_OP_COND
;
161 cmd
.arg
= mmc_host_is_spi(host
) ? 0 : ocr
;
162 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R3
| MMC_CMD_BCR
;
164 for (i
= 100; i
; i
--) {
165 err
= mmc_wait_for_cmd(host
, &cmd
, 0);
169 /* if we're just probing, do a single pass */
173 /* otherwise wait until reset completes */
174 if (mmc_host_is_spi(host
)) {
175 if (!(cmd
.resp
[0] & R1_SPI_IDLE
))
178 if (cmd
.resp
[0] & MMC_CARD_BUSY
)
187 if (rocr
&& !mmc_host_is_spi(host
))
193 int mmc_all_send_cid(struct mmc_host
*host
, u32
*cid
)
196 struct mmc_command cmd
= {0};
201 cmd
.opcode
= MMC_ALL_SEND_CID
;
203 cmd
.flags
= MMC_RSP_R2
| MMC_CMD_BCR
;
205 err
= mmc_wait_for_cmd(host
, &cmd
, MMC_CMD_RETRIES
);
209 memcpy(cid
, cmd
.resp
, sizeof(u32
) * 4);
214 int mmc_set_relative_addr(struct mmc_card
*card
)
217 struct mmc_command cmd
= {0};
222 cmd
.opcode
= MMC_SET_RELATIVE_ADDR
;
223 cmd
.arg
= card
->rca
<< 16;
224 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
226 err
= mmc_wait_for_cmd(card
->host
, &cmd
, MMC_CMD_RETRIES
);
234 mmc_send_cxd_native(struct mmc_host
*host
, u32 arg
, u32
*cxd
, int opcode
)
237 struct mmc_command cmd
= {0};
244 cmd
.flags
= MMC_RSP_R2
| MMC_CMD_AC
;
246 err
= mmc_wait_for_cmd(host
, &cmd
, MMC_CMD_RETRIES
);
250 memcpy(cxd
, cmd
.resp
, sizeof(u32
) * 4);
256 * NOTE: void *buf, caller for the buf is required to use DMA-capable
257 * buffer or on-stack buffer (with some overhead in callee).
260 mmc_send_cxd_data(struct mmc_card
*card
, struct mmc_host
*host
,
261 u32 opcode
, void *buf
, unsigned len
)
263 struct mmc_request mrq
= {NULL
};
264 struct mmc_command cmd
= {0};
265 struct mmc_data data
= {0};
266 struct scatterlist sg
;
274 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
275 * rely on callers to never use this with "native" calls for reading
276 * CSD or CID. Native versions of those commands use the R2 type,
277 * not R1 plus a data block.
279 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
283 data
.flags
= MMC_DATA_READ
;
287 sg_init_one(&sg
, buf
, len
);
289 if (opcode
== MMC_SEND_CSD
|| opcode
== MMC_SEND_CID
) {
291 * The spec states that CSR and CID accesses have a timeout
292 * of 64 clock cycles.
295 data
.timeout_clks
= 64;
297 mmc_set_data_timeout(&data
, card
);
299 mmc_wait_for_req(host
, &mrq
);
309 int mmc_send_csd(struct mmc_card
*card
, u32
*csd
)
314 if (!mmc_host_is_spi(card
->host
))
315 return mmc_send_cxd_native(card
->host
, card
->rca
<< 16,
318 csd_tmp
= kzalloc(16, GFP_KERNEL
);
322 ret
= mmc_send_cxd_data(card
, card
->host
, MMC_SEND_CSD
, csd_tmp
, 16);
326 for (i
= 0;i
< 4;i
++)
327 csd
[i
] = be32_to_cpu(csd_tmp
[i
]);
334 int mmc_send_cid(struct mmc_host
*host
, u32
*cid
)
339 if (!mmc_host_is_spi(host
)) {
342 return mmc_send_cxd_native(host
, host
->card
->rca
<< 16,
346 cid_tmp
= kzalloc(16, GFP_KERNEL
);
350 ret
= mmc_send_cxd_data(NULL
, host
, MMC_SEND_CID
, cid_tmp
, 16);
354 for (i
= 0;i
< 4;i
++)
355 cid
[i
] = be32_to_cpu(cid_tmp
[i
]);
362 int mmc_get_ext_csd(struct mmc_card
*card
, u8
**new_ext_csd
)
367 if (!card
|| !new_ext_csd
)
370 if (!mmc_can_ext_csd(card
))
374 * As the ext_csd is so large and mostly unused, we don't store the
375 * raw block in mmc_card.
377 ext_csd
= kzalloc(512, GFP_KERNEL
);
381 err
= mmc_send_cxd_data(card
, card
->host
, MMC_SEND_EXT_CSD
, ext_csd
,
386 *new_ext_csd
= ext_csd
;
390 EXPORT_SYMBOL_GPL(mmc_get_ext_csd
);
392 int mmc_spi_read_ocr(struct mmc_host
*host
, int highcap
, u32
*ocrp
)
394 struct mmc_command cmd
= {0};
397 cmd
.opcode
= MMC_SPI_READ_OCR
;
398 cmd
.arg
= highcap
? (1 << 30) : 0;
399 cmd
.flags
= MMC_RSP_SPI_R3
;
401 err
= mmc_wait_for_cmd(host
, &cmd
, 0);
407 int mmc_spi_set_crc(struct mmc_host
*host
, int use_crc
)
409 struct mmc_command cmd
= {0};
412 cmd
.opcode
= MMC_SPI_CRC_ON_OFF
;
413 cmd
.flags
= MMC_RSP_SPI_R1
;
416 err
= mmc_wait_for_cmd(host
, &cmd
, 0);
418 host
->use_spi_crc
= use_crc
;
423 * __mmc_switch - modify EXT_CSD register
424 * @card: the MMC card associated with the data transfer
425 * @set: cmd set values
426 * @index: EXT_CSD register index
427 * @value: value to program into EXT_CSD register
428 * @timeout_ms: timeout (ms) for operation performed by register write,
429 * timeout of zero implies maximum possible timeout
430 * @use_busy_signal: use the busy signal as response type
431 * @send_status: send status cmd to poll for busy
432 * @ignore_crc: ignore CRC errors when sending status cmd to poll for busy
434 * Modifies the EXT_CSD register for selected card.
436 int __mmc_switch(struct mmc_card
*card
, u8 set
, u8 index
, u8 value
,
437 unsigned int timeout_ms
, bool use_busy_signal
, bool send_status
,
440 struct mmc_host
*host
= card
->host
;
442 struct mmc_command cmd
= {0};
443 unsigned long timeout
;
445 bool use_r1b_resp
= use_busy_signal
;
448 * If the cmd timeout and the max_busy_timeout of the host are both
449 * specified, let's validate them. A failure means we need to prevent
450 * the host from doing hw busy detection, which is done by converting
451 * to a R1 response instead of a R1B.
453 if (timeout_ms
&& host
->max_busy_timeout
&&
454 (timeout_ms
> host
->max_busy_timeout
))
455 use_r1b_resp
= false;
457 cmd
.opcode
= MMC_SWITCH
;
458 cmd
.arg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
462 cmd
.flags
= MMC_CMD_AC
;
464 cmd
.flags
|= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
;
466 * A busy_timeout of zero means the host can decide to use
467 * whatever value it finds suitable.
469 cmd
.busy_timeout
= timeout_ms
;
471 cmd
.flags
|= MMC_RSP_SPI_R1
| MMC_RSP_R1
;
474 if (index
== EXT_CSD_SANITIZE_START
)
475 cmd
.sanitize_busy
= true;
477 err
= mmc_wait_for_cmd(host
, &cmd
, MMC_CMD_RETRIES
);
481 /* No need to check card status in case of unblocking command */
482 if (!use_busy_signal
)
486 * CRC errors shall only be ignored in cases were CMD13 is used to poll
487 * to detect busy completion.
489 if ((host
->caps
& MMC_CAP_WAIT_WHILE_BUSY
) && use_r1b_resp
)
492 /* We have an unspecified cmd timeout, use the fallback value. */
494 timeout_ms
= MMC_OPS_TIMEOUT_MS
;
496 /* Must check status to be sure of no errors. */
497 timeout
= jiffies
+ msecs_to_jiffies(timeout_ms
);
500 err
= __mmc_send_status(card
, &status
, ignore_crc
);
504 if ((host
->caps
& MMC_CAP_WAIT_WHILE_BUSY
) && use_r1b_resp
)
506 if (mmc_host_is_spi(host
))
510 * We are not allowed to issue a status command and the host
511 * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only
512 * rely on waiting for the stated timeout to be sufficient.
515 mmc_delay(timeout_ms
);
519 /* Timeout if the device never leaves the program state. */
520 if (time_after(jiffies
, timeout
)) {
521 pr_err("%s: Card stuck in programming state! %s\n",
522 mmc_hostname(host
), __func__
);
525 } while (R1_CURRENT_STATE(status
) == R1_STATE_PRG
);
527 if (mmc_host_is_spi(host
)) {
528 if (status
& R1_SPI_ILLEGAL_COMMAND
)
531 if (status
& 0xFDFFA000)
532 pr_warn("%s: unexpected status %#x after switch\n",
533 mmc_hostname(host
), status
);
534 if (status
& R1_SWITCH_ERROR
)
540 EXPORT_SYMBOL_GPL(__mmc_switch
);
542 int mmc_switch(struct mmc_card
*card
, u8 set
, u8 index
, u8 value
,
543 unsigned int timeout_ms
)
545 return __mmc_switch(card
, set
, index
, value
, timeout_ms
, true, true,
548 EXPORT_SYMBOL_GPL(mmc_switch
);
550 int mmc_send_tuning(struct mmc_host
*host
)
552 struct mmc_request mrq
= {NULL
};
553 struct mmc_command cmd
= {0};
554 struct mmc_data data
= {0};
555 struct scatterlist sg
;
556 struct mmc_ios
*ios
= &host
->ios
;
557 const u8
*tuning_block_pattern
;
562 if (ios
->bus_width
== MMC_BUS_WIDTH_8
) {
563 tuning_block_pattern
= tuning_blk_pattern_8bit
;
564 size
= sizeof(tuning_blk_pattern_8bit
);
565 opcode
= MMC_SEND_TUNING_BLOCK_HS200
;
566 } else if (ios
->bus_width
== MMC_BUS_WIDTH_4
) {
567 tuning_block_pattern
= tuning_blk_pattern_4bit
;
568 size
= sizeof(tuning_blk_pattern_4bit
);
569 opcode
= MMC_SEND_TUNING_BLOCK
;
573 data_buf
= kzalloc(size
, GFP_KERNEL
);
581 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_ADTC
;
585 data
.flags
= MMC_DATA_READ
;
588 * According to the tuning specs, Tuning process
589 * is normally shorter 40 executions of CMD19,
590 * and timeout value should be shorter than 150 ms
592 data
.timeout_ns
= 150 * NSEC_PER_MSEC
;
596 sg_init_one(&sg
, data_buf
, size
);
598 mmc_wait_for_req(host
, &mrq
);
610 if (memcmp(data_buf
, tuning_block_pattern
, size
))
617 EXPORT_SYMBOL_GPL(mmc_send_tuning
);
620 mmc_send_bus_test(struct mmc_card
*card
, struct mmc_host
*host
, u8 opcode
,
623 struct mmc_request mrq
= {NULL
};
624 struct mmc_command cmd
= {0};
625 struct mmc_data data
= {0};
626 struct scatterlist sg
;
630 static u8 testdata_8bit
[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
631 static u8 testdata_4bit
[4] = { 0x5a, 0, 0, 0 };
633 /* dma onto stack is unsafe/nonportable, but callers to this
634 * routine normally provide temporary on-stack buffers ...
636 data_buf
= kmalloc(len
, GFP_KERNEL
);
641 test_buf
= testdata_8bit
;
643 test_buf
= testdata_4bit
;
645 pr_err("%s: Invalid bus_width %d\n",
646 mmc_hostname(host
), len
);
651 if (opcode
== MMC_BUS_TEST_W
)
652 memcpy(data_buf
, test_buf
, len
);
659 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
660 * rely on callers to never use this with "native" calls for reading
661 * CSD or CID. Native versions of those commands use the R2 type,
662 * not R1 plus a data block.
664 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
668 if (opcode
== MMC_BUS_TEST_R
)
669 data
.flags
= MMC_DATA_READ
;
671 data
.flags
= MMC_DATA_WRITE
;
675 mmc_set_data_timeout(&data
, card
);
676 sg_init_one(&sg
, data_buf
, len
);
677 mmc_wait_for_req(host
, &mrq
);
679 if (opcode
== MMC_BUS_TEST_R
) {
680 for (i
= 0; i
< len
/ 4; i
++)
681 if ((test_buf
[i
] ^ data_buf
[i
]) != 0xff) {
696 int mmc_bus_test(struct mmc_card
*card
, u8 bus_width
)
700 if (bus_width
== MMC_BUS_WIDTH_8
)
702 else if (bus_width
== MMC_BUS_WIDTH_4
)
704 else if (bus_width
== MMC_BUS_WIDTH_1
)
705 return 0; /* no need for test */
710 * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
711 * is a problem. This improves chances that the test will work.
713 mmc_send_bus_test(card
, card
->host
, MMC_BUS_TEST_W
, width
);
714 err
= mmc_send_bus_test(card
, card
->host
, MMC_BUS_TEST_R
, width
);
718 int mmc_send_hpi_cmd(struct mmc_card
*card
, u32
*status
)
720 struct mmc_command cmd
= {0};
724 if (!card
->ext_csd
.hpi
) {
725 pr_warn("%s: Card didn't support HPI command\n",
726 mmc_hostname(card
->host
));
730 opcode
= card
->ext_csd
.hpi_cmd
;
731 if (opcode
== MMC_STOP_TRANSMISSION
)
732 cmd
.flags
= MMC_RSP_R1B
| MMC_CMD_AC
;
733 else if (opcode
== MMC_SEND_STATUS
)
734 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
737 cmd
.arg
= card
->rca
<< 16 | 1;
739 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
741 pr_warn("%s: error %d interrupting operation. "
742 "HPI command response %#x\n", mmc_hostname(card
->host
),
747 *status
= cmd
.resp
[0];
752 int mmc_can_ext_csd(struct mmc_card
*card
)
754 return (card
&& card
->csd
.mmca_vsn
> CSD_SPEC_VER_3
);