2 * linux/drivers/mmc/sd_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/types.h>
13 #include <asm/scatterlist.h>
14 #include <linux/scatterlist.h>
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/sd.h>
25 * mmc_wait_for_app_cmd - start an application command and wait for
27 * @host: MMC host to start command
28 * @rca: RCA to send MMC_APP_CMD to
29 * @cmd: MMC command to start
30 * @retries: maximum number of retries
32 * Sends a MMC_APP_CMD, checks the card response, sends the command
33 * in the parameter and waits for it to complete. Return any error
34 * that occurred while the command was executing. Do not attempt to
37 int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
38 struct mmc_command *cmd, int retries)
40 struct mmc_request mrq;
47 err = MMC_ERR_INVALID;
50 * We have to resend MMC_APP_CMD for each attempt so
51 * we cannot use the retries field in mmc_command.
53 for (i = 0;i <= retries;i++) {
54 memset(&mrq, 0, sizeof(struct mmc_request));
56 err = mmc_app_cmd(host, card);
57 if (err != MMC_ERR_NONE)
60 memset(&mrq, 0, sizeof(struct mmc_request));
62 memset(cmd->resp, 0, sizeof(cmd->resp));
68 mmc_wait_for_req(host, &mrq);
71 if (cmd->error == MMC_ERR_NONE)
78 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
80 int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
83 struct mmc_command cmd;
86 BUG_ON(card && (card->host != host));
88 cmd.opcode = MMC_APP_CMD;
91 cmd.arg = card->rca << 16;
92 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
95 cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR;
98 err = mmc_wait_for_cmd(host, &cmd, 0);
99 if (err != MMC_ERR_NONE)
102 /* Check that card supported application commands */
103 if (!(cmd.resp[0] & R1_APP_CMD))
104 return MMC_ERR_FAILED;
109 int mmc_app_set_bus_width(struct mmc_card *card, int width)
112 struct mmc_command cmd;
117 memset(&cmd, 0, sizeof(struct mmc_command));
119 cmd.opcode = SD_APP_SET_BUS_WIDTH;
120 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
123 case MMC_BUS_WIDTH_1:
124 cmd.arg = SD_BUS_WIDTH_1;
126 case MMC_BUS_WIDTH_4:
127 cmd.arg = SD_BUS_WIDTH_4;
130 return MMC_ERR_INVALID;
133 err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
134 if (err != MMC_ERR_NONE)
140 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
142 struct mmc_command cmd;
147 memset(&cmd, 0, sizeof(struct mmc_command));
149 cmd.opcode = SD_APP_OP_COND;
151 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
153 for (i = 100; i; i--) {
154 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
155 if (err != MMC_ERR_NONE)
158 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
161 err = MMC_ERR_TIMEOUT;
172 int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
174 struct mmc_command cmd;
176 static const u8 test_pattern = 0xAA;
179 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
180 * before SD_APP_OP_COND. This command will harmlessly fail for
183 cmd.opcode = SD_SEND_IF_COND;
184 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
185 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
187 err = mmc_wait_for_cmd(host, &cmd, 0);
188 if (err != MMC_ERR_NONE)
191 if ((cmd.resp[0] & 0xFF) != test_pattern)
192 return MMC_ERR_FAILED;
197 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
200 struct mmc_command cmd;
205 memset(&cmd, 0, sizeof(struct mmc_command));
207 cmd.opcode = SD_SEND_RELATIVE_ADDR;
209 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
211 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
212 if (err != MMC_ERR_NONE)
215 *rca = cmd.resp[0] >> 16;
220 int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
223 struct mmc_request mrq;
224 struct mmc_command cmd;
225 struct mmc_data data;
226 struct scatterlist sg;
232 err = mmc_app_cmd(card->host, card);
233 if (err != MMC_ERR_NONE)
236 memset(&mrq, 0, sizeof(struct mmc_request));
237 memset(&cmd, 0, sizeof(struct mmc_command));
238 memset(&data, 0, sizeof(struct mmc_data));
243 cmd.opcode = SD_APP_SEND_SCR;
245 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
249 data.flags = MMC_DATA_READ;
253 sg_init_one(&sg, scr, 8);
255 mmc_set_data_timeout(&data, card, 0);
257 mmc_wait_for_req(card->host, &mrq);
259 if (cmd.error != MMC_ERR_NONE)
261 if (data.error != MMC_ERR_NONE)
264 scr[0] = ntohl(scr[0]);
265 scr[1] = ntohl(scr[1]);
270 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
273 struct mmc_request mrq;
274 struct mmc_command cmd;
275 struct mmc_data data;
276 struct scatterlist sg;
284 memset(&mrq, 0, sizeof(struct mmc_request));
285 memset(&cmd, 0, sizeof(struct mmc_command));
286 memset(&data, 0, sizeof(struct mmc_data));
291 cmd.opcode = SD_SWITCH;
292 cmd.arg = mode << 31 | 0x00FFFFFF;
293 cmd.arg &= ~(0xF << (group * 4));
294 cmd.arg |= value << (group * 4);
295 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
299 data.flags = MMC_DATA_READ;
303 sg_init_one(&sg, resp, 64);
305 mmc_set_data_timeout(&data, card, 0);
307 mmc_wait_for_req(card->host, &mrq);
309 if (cmd.error != MMC_ERR_NONE)
311 if (data.error != MMC_ERR_NONE)