1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/mmc/core/sd_ops.h
5 * Copyright 2006-2007 Pierre Ossman
8 #include <linux/slab.h>
9 #include <linux/types.h>
10 #include <linux/export.h>
11 #include <linux/scatterlist.h>
13 #include <linux/mmc/host.h>
14 #include <linux/mmc/card.h>
15 #include <linux/mmc/mmc.h>
16 #include <linux/mmc/sd.h>
23 * Extensive testing has shown that some specific SD cards
24 * require an increased command timeout to be successfully
27 #define SD_APP_OP_COND_PERIOD_US (10 * 1000) /* 10ms */
28 #define SD_APP_OP_COND_TIMEOUT_MS 2000 /* 2s */
30 struct sd_app_op_cond_busy_data {
31 struct mmc_host *host;
33 struct mmc_command *cmd;
36 int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
39 struct mmc_command cmd = {};
41 if (WARN_ON(card && card->host != host))
44 cmd.opcode = MMC_APP_CMD;
47 cmd.arg = card->rca << 16;
48 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
51 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
54 err = mmc_wait_for_cmd(host, &cmd, 0);
58 /* Check that card supported application commands */
59 if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
64 EXPORT_SYMBOL_GPL(mmc_app_cmd);
66 static int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
67 struct mmc_command *cmd)
69 struct mmc_request mrq = {};
73 * We have to resend MMC_APP_CMD for each attempt so
74 * we cannot use the retries field in mmc_command.
76 for (i = 0; i <= MMC_CMD_RETRIES; i++) {
77 err = mmc_app_cmd(host, card);
79 /* no point in retrying; no APP commands allowed */
80 if (mmc_host_is_spi(host)) {
81 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
87 memset(&mrq, 0, sizeof(struct mmc_request));
89 memset(cmd->resp, 0, sizeof(cmd->resp));
95 mmc_wait_for_req(host, &mrq);
101 /* no point in retrying illegal APP commands */
102 if (mmc_host_is_spi(host)) {
103 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
111 int mmc_app_set_bus_width(struct mmc_card *card, int width)
113 struct mmc_command cmd = {};
115 cmd.opcode = SD_APP_SET_BUS_WIDTH;
116 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
119 case MMC_BUS_WIDTH_1:
120 cmd.arg = SD_BUS_WIDTH_1;
122 case MMC_BUS_WIDTH_4:
123 cmd.arg = SD_BUS_WIDTH_4;
129 return mmc_wait_for_app_cmd(card->host, card, &cmd);
132 static int sd_app_op_cond_cb(void *cb_data, bool *busy)
134 struct sd_app_op_cond_busy_data *data = cb_data;
135 struct mmc_host *host = data->host;
136 struct mmc_command *cmd = data->cmd;
142 err = mmc_wait_for_app_cmd(host, NULL, cmd);
146 /* If we're just probing, do a single pass. */
150 /* Wait until reset completes. */
151 if (mmc_host_is_spi(host)) {
152 if (!(cmd->resp[0] & R1_SPI_IDLE))
154 } else if (cmd->resp[0] & MMC_CARD_BUSY) {
162 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
164 struct mmc_command cmd = {};
165 struct sd_app_op_cond_busy_data cb_data = {
172 cmd.opcode = SD_APP_OP_COND;
173 if (mmc_host_is_spi(host))
174 cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
177 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
179 err = __mmc_poll_for_busy(host, SD_APP_OP_COND_PERIOD_US,
180 SD_APP_OP_COND_TIMEOUT_MS, &sd_app_op_cond_cb,
185 if (rocr && !mmc_host_is_spi(host))
191 static int __mmc_send_if_cond(struct mmc_host *host, u32 ocr, u8 pcie_bits,
194 struct mmc_command cmd = {};
196 static const u8 test_pattern = 0xAA;
200 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
201 * before SD_APP_OP_COND. This command will harmlessly fail for
204 cmd.opcode = SD_SEND_IF_COND;
205 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | pcie_bits << 8 | test_pattern;
206 cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
208 err = mmc_wait_for_cmd(host, &cmd, 0);
212 if (mmc_host_is_spi(host))
213 result_pattern = cmd.resp[1] & 0xFF;
215 result_pattern = cmd.resp[0] & 0xFF;
217 if (result_pattern != test_pattern)
226 int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
228 return __mmc_send_if_cond(host, ocr, 0, NULL);
231 int mmc_send_if_cond_pcie(struct mmc_host *host, u32 ocr)
237 if (host->caps2 & MMC_CAP2_SD_EXP) {
238 /* Probe card for SD express support via PCIe. */
240 if (host->caps2 & MMC_CAP2_SD_EXP_1_2V)
241 /* Probe also for 1.2V support. */
245 ret = __mmc_send_if_cond(host, ocr, pcie_bits, &resp);
249 /* Continue with the SD express init, if the card supports it. */
251 if (pcie_bits && resp) {
253 host->ios.timing = MMC_TIMING_SD_EXP_1_2V;
255 host->ios.timing = MMC_TIMING_SD_EXP;
258 * According to the spec the clock shall also be gated, but
259 * let's leave this to the host driver for more flexibility.
261 return host->ops->init_sd_express(host, &host->ios);
267 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
270 struct mmc_command cmd = {};
272 cmd.opcode = SD_SEND_RELATIVE_ADDR;
274 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
276 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
280 *rca = cmd.resp[0] >> 16;
285 int mmc_app_send_scr(struct mmc_card *card)
288 struct mmc_request mrq = {};
289 struct mmc_command cmd = {};
290 struct mmc_data data = {};
291 struct scatterlist sg;
294 /* NOTE: caller guarantees scr is heap-allocated */
296 err = mmc_app_cmd(card->host, card);
300 /* dma onto stack is unsafe/nonportable, but callers to this
301 * routine normally provide temporary on-stack buffers ...
303 scr = kmalloc(sizeof(card->raw_scr), GFP_KERNEL);
310 cmd.opcode = SD_APP_SEND_SCR;
312 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
316 data.flags = MMC_DATA_READ;
320 sg_init_one(&sg, scr, 8);
322 mmc_set_data_timeout(&data, card);
324 mmc_wait_for_req(card->host, &mrq);
326 card->raw_scr[0] = be32_to_cpu(scr[0]);
327 card->raw_scr[1] = be32_to_cpu(scr[1]);
339 int mmc_sd_switch(struct mmc_card *card, bool mode, int group,
344 /* NOTE: caller guarantees resp is heap-allocated */
347 cmd_args = mode << 31 | 0x00FFFFFF;
348 cmd_args &= ~(0xF << (group * 4));
349 cmd_args |= value << (group * 4);
351 return mmc_send_adtc_data(card, card->host, SD_SWITCH, cmd_args, resp,
354 EXPORT_SYMBOL_GPL(mmc_sd_switch);
356 int mmc_app_sd_status(struct mmc_card *card, void *ssr)
359 struct mmc_request mrq = {};
360 struct mmc_command cmd = {};
361 struct mmc_data data = {};
362 struct scatterlist sg;
364 /* NOTE: caller guarantees ssr is heap-allocated */
366 err = mmc_app_cmd(card->host, card);
373 cmd.opcode = SD_APP_SD_STATUS;
375 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
379 data.flags = MMC_DATA_READ;
383 sg_init_one(&sg, ssr, 64);
385 mmc_set_data_timeout(&data, card);
387 mmc_wait_for_req(card->host, &mrq);