x86_64: Zero extend all registers after ptrace in 32bit entry path.
[wrt350n-kernel.git] / drivers / mmc / core / sd_ops.c
blob342f340ebc2579d8c81dd231f526b140f87552d5
1 /*
2 * linux/drivers/mmc/core/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>
21 #include "core.h"
22 #include "sd_ops.h"
24 static int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
26 int err;
27 struct mmc_command cmd;
29 BUG_ON(!host);
30 BUG_ON(card && (card->host != host));
32 cmd.opcode = MMC_APP_CMD;
34 if (card) {
35 cmd.arg = card->rca << 16;
36 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
37 } else {
38 cmd.arg = 0;
39 cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR;
42 err = mmc_wait_for_cmd(host, &cmd, 0);
43 if (err != MMC_ERR_NONE)
44 return err;
46 /* Check that card supported application commands */
47 if (!(cmd.resp[0] & R1_APP_CMD))
48 return MMC_ERR_FAILED;
50 return MMC_ERR_NONE;
53 /**
54 * mmc_wait_for_app_cmd - start an application command and wait for
55 completion
56 * @host: MMC host to start command
57 * @card: Card to send MMC_APP_CMD to
58 * @cmd: MMC command to start
59 * @retries: maximum number of retries
61 * Sends a MMC_APP_CMD, checks the card response, sends the command
62 * in the parameter and waits for it to complete. Return any error
63 * that occurred while the command was executing. Do not attempt to
64 * parse the response.
66 int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
67 struct mmc_command *cmd, int retries)
69 struct mmc_request mrq;
71 int i, err;
73 BUG_ON(!cmd);
74 BUG_ON(retries < 0);
76 err = MMC_ERR_INVALID;
79 * We have to resend MMC_APP_CMD for each attempt so
80 * we cannot use the retries field in mmc_command.
82 for (i = 0;i <= retries;i++) {
83 memset(&mrq, 0, sizeof(struct mmc_request));
85 err = mmc_app_cmd(host, card);
86 if (err != MMC_ERR_NONE)
87 continue;
89 memset(&mrq, 0, sizeof(struct mmc_request));
91 memset(cmd->resp, 0, sizeof(cmd->resp));
92 cmd->retries = 0;
94 mrq.cmd = cmd;
95 cmd->data = NULL;
97 mmc_wait_for_req(host, &mrq);
99 err = cmd->error;
100 if (cmd->error == MMC_ERR_NONE)
101 break;
104 return err;
107 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
109 int mmc_app_set_bus_width(struct mmc_card *card, int width)
111 int err;
112 struct mmc_command cmd;
114 BUG_ON(!card);
115 BUG_ON(!card->host);
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;
122 switch (width) {
123 case MMC_BUS_WIDTH_1:
124 cmd.arg = SD_BUS_WIDTH_1;
125 break;
126 case MMC_BUS_WIDTH_4:
127 cmd.arg = SD_BUS_WIDTH_4;
128 break;
129 default:
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)
135 return err;
137 return MMC_ERR_NONE;
140 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
142 struct mmc_command cmd;
143 int i, err = 0;
145 BUG_ON(!host);
147 memset(&cmd, 0, sizeof(struct mmc_command));
149 cmd.opcode = SD_APP_OP_COND;
150 cmd.arg = ocr;
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)
156 break;
158 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
159 break;
161 err = MMC_ERR_TIMEOUT;
163 mmc_delay(10);
166 if (rocr)
167 *rocr = cmd.resp[0];
169 return err;
172 int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
174 struct mmc_command cmd;
175 int err;
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
181 * SD 1.0 cards.
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)
189 return err;
191 if ((cmd.resp[0] & 0xFF) != test_pattern)
192 return MMC_ERR_FAILED;
194 return MMC_ERR_NONE;
197 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
199 int err;
200 struct mmc_command cmd;
202 BUG_ON(!host);
203 BUG_ON(!rca);
205 memset(&cmd, 0, sizeof(struct mmc_command));
207 cmd.opcode = SD_SEND_RELATIVE_ADDR;
208 cmd.arg = 0;
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)
213 return err;
215 *rca = cmd.resp[0] >> 16;
217 return MMC_ERR_NONE;
220 int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
222 int err;
223 struct mmc_request mrq;
224 struct mmc_command cmd;
225 struct mmc_data data;
226 struct scatterlist sg;
228 BUG_ON(!card);
229 BUG_ON(!card->host);
230 BUG_ON(!scr);
232 err = mmc_app_cmd(card->host, card);
233 if (err != MMC_ERR_NONE)
234 return err;
236 memset(&mrq, 0, sizeof(struct mmc_request));
237 memset(&cmd, 0, sizeof(struct mmc_command));
238 memset(&data, 0, sizeof(struct mmc_data));
240 mrq.cmd = &cmd;
241 mrq.data = &data;
243 cmd.opcode = SD_APP_SEND_SCR;
244 cmd.arg = 0;
245 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
247 data.blksz = 8;
248 data.blocks = 1;
249 data.flags = MMC_DATA_READ;
250 data.sg = &sg;
251 data.sg_len = 1;
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)
260 return cmd.error;
261 if (data.error != MMC_ERR_NONE)
262 return data.error;
264 scr[0] = ntohl(scr[0]);
265 scr[1] = ntohl(scr[1]);
267 return MMC_ERR_NONE;
270 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
271 u8 value, u8 *resp)
273 struct mmc_request mrq;
274 struct mmc_command cmd;
275 struct mmc_data data;
276 struct scatterlist sg;
278 BUG_ON(!card);
279 BUG_ON(!card->host);
281 mode = !!mode;
282 value &= 0xF;
284 memset(&mrq, 0, sizeof(struct mmc_request));
285 memset(&cmd, 0, sizeof(struct mmc_command));
286 memset(&data, 0, sizeof(struct mmc_data));
288 mrq.cmd = &cmd;
289 mrq.data = &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;
297 data.blksz = 64;
298 data.blocks = 1;
299 data.flags = MMC_DATA_READ;
300 data.sg = &sg;
301 data.sg_len = 1;
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)
310 return cmd.error;
311 if (data.error != MMC_ERR_NONE)
312 return data.error;
314 return MMC_ERR_NONE;