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>
24 * Extensive testing has shown that some specific SD cards
25 * require an increased command timeout to be successfully
28 #define SD_APP_OP_COND_PERIOD_US (10 * 1000) /* 10ms */
29 #define SD_APP_OP_COND_TIMEOUT_MS 2000 /* 2s */
31 struct sd_app_op_cond_busy_data {
32 struct mmc_host *host;
34 struct mmc_command *cmd;
37 int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
40 struct mmc_command cmd = {};
42 if (WARN_ON(card && card->host != host))
46 * UHS2 packet has APP bit so only set APP_CMD flag here.
47 * Will set the APP bit when assembling UHS2 packet.
49 if (host->uhs2_sd_tran) {
50 host->uhs2_app_cmd = true;
54 cmd.opcode = MMC_APP_CMD;
57 cmd.arg = card->rca << 16;
58 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
61 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
64 err = mmc_wait_for_cmd(host, &cmd, 0);
68 /* Check that card supported application commands */
69 if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
74 EXPORT_SYMBOL_GPL(mmc_app_cmd);
76 static int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
77 struct mmc_command *cmd)
79 struct mmc_request mrq = {};
83 * We have to resend MMC_APP_CMD for each attempt so
84 * we cannot use the retries field in mmc_command.
86 for (i = 0; i <= MMC_CMD_RETRIES; i++) {
87 err = mmc_app_cmd(host, card);
89 /* no point in retrying; no APP commands allowed */
90 if (mmc_host_is_spi(host)) {
91 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
97 memset(&mrq, 0, sizeof(struct mmc_request));
99 memset(cmd->resp, 0, sizeof(cmd->resp));
105 mmc_wait_for_req(host, &mrq);
111 /* no point in retrying illegal APP commands */
112 if (mmc_host_is_spi(host)) {
113 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
121 int mmc_app_set_bus_width(struct mmc_card *card, int width)
123 struct mmc_command cmd = {};
125 cmd.opcode = SD_APP_SET_BUS_WIDTH;
126 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
129 case MMC_BUS_WIDTH_1:
130 cmd.arg = SD_BUS_WIDTH_1;
132 case MMC_BUS_WIDTH_4:
133 cmd.arg = SD_BUS_WIDTH_4;
139 return mmc_wait_for_app_cmd(card->host, card, &cmd);
142 static int sd_app_op_cond_cb(void *cb_data, bool *busy)
144 struct sd_app_op_cond_busy_data *data = cb_data;
145 struct mmc_host *host = data->host;
146 struct mmc_command *cmd = data->cmd;
152 err = mmc_wait_for_app_cmd(host, NULL, cmd);
156 /* If we're just probing, do a single pass. */
160 /* Wait until reset completes. */
161 if (mmc_host_is_spi(host)) {
162 if (!(cmd->resp[0] & R1_SPI_IDLE))
164 } else if (cmd->resp[0] & MMC_CARD_BUSY) {
172 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
174 struct mmc_command cmd = {};
175 struct sd_app_op_cond_busy_data cb_data = {
182 cmd.opcode = SD_APP_OP_COND;
183 if (mmc_host_is_spi(host))
184 cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
187 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
189 err = __mmc_poll_for_busy(host, SD_APP_OP_COND_PERIOD_US,
190 SD_APP_OP_COND_TIMEOUT_MS, &sd_app_op_cond_cb,
195 if (rocr && !mmc_host_is_spi(host))
201 int mmc_send_ext_addr(struct mmc_host *host, u32 addr)
203 struct mmc_command cmd = {
204 .opcode = SD_ADDR_EXT,
206 .flags = MMC_RSP_R1 | MMC_CMD_AC,
209 if (!mmc_card_ult_capacity(host->card))
212 return mmc_wait_for_cmd(host, &cmd, 0);
215 static int __mmc_send_if_cond(struct mmc_host *host, u32 ocr, u8 pcie_bits,
218 struct mmc_command cmd = {};
220 static const u8 test_pattern = 0xAA;
224 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
225 * before SD_APP_OP_COND. This command will harmlessly fail for
228 cmd.opcode = SD_SEND_IF_COND;
229 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | pcie_bits << 8 | test_pattern;
230 cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
232 err = mmc_wait_for_cmd(host, &cmd, 0);
236 if (mmc_host_is_spi(host))
237 result_pattern = cmd.resp[1] & 0xFF;
239 result_pattern = cmd.resp[0] & 0xFF;
241 if (result_pattern != test_pattern)
250 int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
252 return __mmc_send_if_cond(host, ocr, 0, NULL);
255 int mmc_send_if_cond_pcie(struct mmc_host *host, u32 ocr)
261 if (host->caps2 & MMC_CAP2_SD_EXP) {
262 /* Probe card for SD express support via PCIe. */
264 if (host->caps2 & MMC_CAP2_SD_EXP_1_2V)
265 /* Probe also for 1.2V support. */
269 ret = __mmc_send_if_cond(host, ocr, pcie_bits, &resp);
273 /* Continue with the SD express init, if the card supports it. */
275 if (pcie_bits && resp) {
277 host->ios.timing = MMC_TIMING_SD_EXP_1_2V;
279 host->ios.timing = MMC_TIMING_SD_EXP;
282 * According to the spec the clock shall also be gated, but
283 * let's leave this to the host driver for more flexibility.
285 return host->ops->init_sd_express(host, &host->ios);
291 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
294 struct mmc_command cmd = {};
296 cmd.opcode = SD_SEND_RELATIVE_ADDR;
298 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
300 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
304 *rca = cmd.resp[0] >> 16;
309 int mmc_app_send_scr(struct mmc_card *card)
312 struct mmc_request mrq = {};
313 struct mmc_command cmd = {};
314 struct mmc_data data = {};
315 struct scatterlist sg;
318 /* NOTE: caller guarantees scr is heap-allocated */
320 err = mmc_app_cmd(card->host, card);
324 /* dma onto stack is unsafe/nonportable, but callers to this
325 * routine normally provide temporary on-stack buffers ...
327 scr = kmalloc(sizeof(card->raw_scr), GFP_KERNEL);
334 cmd.opcode = SD_APP_SEND_SCR;
336 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
340 data.flags = MMC_DATA_READ;
344 sg_init_one(&sg, scr, 8);
346 mmc_set_data_timeout(&data, card);
348 mmc_wait_for_req(card->host, &mrq);
350 card->raw_scr[0] = be32_to_cpu(scr[0]);
351 card->raw_scr[1] = be32_to_cpu(scr[1]);
363 int mmc_sd_switch(struct mmc_card *card, bool mode, int group,
368 /* NOTE: caller guarantees resp is heap-allocated */
371 cmd_args = mode << 31 | 0x00FFFFFF;
372 cmd_args &= ~(0xF << (group * 4));
373 cmd_args |= value << (group * 4);
375 return mmc_send_adtc_data(card, card->host, SD_SWITCH, cmd_args, resp,
378 EXPORT_SYMBOL_GPL(mmc_sd_switch);
380 int mmc_app_sd_status(struct mmc_card *card, void *ssr)
383 struct mmc_request mrq = {};
384 struct mmc_command cmd = {};
385 struct mmc_data data = {};
386 struct scatterlist sg;
388 /* NOTE: caller guarantees ssr is heap-allocated */
390 err = mmc_app_cmd(card->host, card);
397 cmd.opcode = SD_APP_SD_STATUS;
399 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
403 data.flags = MMC_DATA_READ;
407 sg_init_one(&sg, ssr, 64);
409 mmc_set_data_timeout(&data, card);
411 mmc_wait_for_req(card->host, &mrq);