MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mmc / core / sd_ops.c
blob687878490dc189263a09d264ac4f930a89aa7423
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 #if 1 // add by Victor Yu. 12-02-2008
14 #include <linux/pagemap.h>
15 #endif
16 #include <asm/scatterlist.h>
17 #include <linux/scatterlist.h>
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/mmc/sd.h>
24 #include "core.h"
25 #include "sd_ops.h"
27 static int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
29 int err;
30 struct mmc_command cmd;
32 BUG_ON(!host);
33 BUG_ON(card && (card->host != host));
35 cmd.opcode = MMC_APP_CMD;
37 if (card) {
38 cmd.arg = card->rca << 16;
39 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
40 } else {
41 cmd.arg = 0;
42 cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR;
45 err = mmc_wait_for_cmd(host, &cmd, 0);
46 if (err != MMC_ERR_NONE)
47 return err;
49 /* Check that card supported application commands */
50 if (!(cmd.resp[0] & R1_APP_CMD))
51 return MMC_ERR_FAILED;
53 return MMC_ERR_NONE;
56 /**
57 * mmc_wait_for_app_cmd - start an application command and wait for
58 completion
59 * @host: MMC host to start command
60 * @card: Card to send MMC_APP_CMD to
61 * @cmd: MMC command to start
62 * @retries: maximum number of retries
64 * Sends a MMC_APP_CMD, checks the card response, sends the command
65 * in the parameter and waits for it to complete. Return any error
66 * that occurred while the command was executing. Do not attempt to
67 * parse the response.
69 int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
70 struct mmc_command *cmd, int retries)
72 struct mmc_request mrq;
74 int i, err;
76 BUG_ON(!cmd);
77 BUG_ON(retries < 0);
79 err = MMC_ERR_INVALID;
82 * We have to resend MMC_APP_CMD for each attempt so
83 * we cannot use the retries field in mmc_command.
85 for (i = 0;i <= retries;i++) {
86 memset(&mrq, 0, sizeof(struct mmc_request));
88 err = mmc_app_cmd(host, card);
89 if (err != MMC_ERR_NONE)
90 continue;
92 memset(&mrq, 0, sizeof(struct mmc_request));
94 memset(cmd->resp, 0, sizeof(cmd->resp));
95 cmd->retries = 0;
97 mrq.cmd = cmd;
98 cmd->data = NULL;
100 mmc_wait_for_req(host, &mrq);
102 err = cmd->error;
103 if (cmd->error == MMC_ERR_NONE)
104 break;
107 return err;
110 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
112 int mmc_app_set_bus_width(struct mmc_card *card, int width)
114 int err;
115 struct mmc_command cmd;
117 BUG_ON(!card);
118 BUG_ON(!card->host);
120 memset(&cmd, 0, sizeof(struct mmc_command));
122 cmd.opcode = SD_APP_SET_BUS_WIDTH;
123 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
125 switch (width) {
126 case MMC_BUS_WIDTH_1:
127 cmd.arg = SD_BUS_WIDTH_1;
128 break;
129 case MMC_BUS_WIDTH_4:
130 cmd.arg = SD_BUS_WIDTH_4;
131 break;
132 default:
133 return MMC_ERR_INVALID;
136 err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
137 if (err != MMC_ERR_NONE)
138 return err;
140 return MMC_ERR_NONE;
143 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
145 struct mmc_command cmd;
146 int i, err = 0;
148 BUG_ON(!host);
150 memset(&cmd, 0, sizeof(struct mmc_command));
152 cmd.opcode = SD_APP_OP_COND;
153 cmd.arg = ocr;
154 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
156 for (i = 100; i; i--) {
157 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
158 if (err != MMC_ERR_NONE)
159 break;
161 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
162 break;
164 err = MMC_ERR_TIMEOUT;
166 mmc_delay(10);
169 if (rocr)
170 *rocr = cmd.resp[0];
172 return err;
175 int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
177 struct mmc_command cmd;
178 int err;
179 static const u8 test_pattern = 0xAA;
182 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
183 * before SD_APP_OP_COND. This command will harmlessly fail for
184 * SD 1.0 cards.
186 cmd.opcode = SD_SEND_IF_COND;
187 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
188 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
190 err = mmc_wait_for_cmd(host, &cmd, 0);
191 if (err != MMC_ERR_NONE)
192 return err;
194 if ((cmd.resp[0] & 0xFF) != test_pattern)
195 return MMC_ERR_FAILED;
197 return MMC_ERR_NONE;
200 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
202 int err;
203 struct mmc_command cmd;
205 BUG_ON(!host);
206 BUG_ON(!rca);
208 memset(&cmd, 0, sizeof(struct mmc_command));
210 cmd.opcode = SD_SEND_RELATIVE_ADDR;
211 cmd.arg = 0;
212 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
214 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
215 if (err != MMC_ERR_NONE)
216 return err;
218 *rca = cmd.resp[0] >> 16;
220 return MMC_ERR_NONE;
223 int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
225 int err;
226 struct mmc_request mrq;
227 struct mmc_command cmd;
228 struct mmc_data data;
229 struct scatterlist sg;
231 BUG_ON(!card);
232 BUG_ON(!card->host);
233 BUG_ON(!scr);
235 err = mmc_app_cmd(card->host, card);
236 if (err != MMC_ERR_NONE)
237 return err;
239 memset(&mrq, 0, sizeof(struct mmc_request));
240 memset(&cmd, 0, sizeof(struct mmc_command));
241 memset(&data, 0, sizeof(struct mmc_data));
243 mrq.cmd = &cmd;
244 mrq.data = &data;
246 cmd.opcode = SD_APP_SEND_SCR;
247 cmd.arg = 0;
248 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
250 data.blksz = 8;
251 data.blocks = 1;
252 data.flags = MMC_DATA_READ;
253 data.sg = &sg;
254 data.sg_len = 1;
256 #if 0 // mask by Victor Yu. 12-03-2008
257 sg_init_one(&sg, scr, 8);
258 #else
259 sg_init_one(&sg, (u8 *)scr, 8);
260 #endif
262 mmc_set_data_timeout(&data, card, 0);
264 mmc_wait_for_req(card->host, &mrq);
266 if (cmd.error != MMC_ERR_NONE)
267 return cmd.error;
268 if (data.error != MMC_ERR_NONE)
269 return data.error;
271 scr[0] = ntohl(scr[0]);
272 scr[1] = ntohl(scr[1]);
274 return MMC_ERR_NONE;
277 int mmc_sd_switch(struct mmc_card *card, int mode, int group, u8 value, u8 *resp)
279 struct mmc_request mrq;
280 struct mmc_command cmd;
281 struct mmc_data data;
282 struct scatterlist sg;
284 BUG_ON(!card);
285 BUG_ON(!card->host);
287 mode = !!mode;
288 value &= 0xF;
290 memset(&mrq, 0, sizeof(struct mmc_request));
291 memset(&cmd, 0, sizeof(struct mmc_command));
292 memset(&data, 0, sizeof(struct mmc_data));
294 mrq.cmd = &cmd;
295 mrq.data = &data;
297 cmd.opcode = SD_SWITCH;
298 cmd.arg = mode << 31 | 0x00FFFFFF;
299 cmd.arg &= ~(0xF << (group * 4));
300 cmd.arg |= value << (group * 4);
301 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
303 data.blksz = 64;
304 data.blocks = 1;
305 data.flags = MMC_DATA_READ;
306 data.sg = &sg;
307 data.sg_len = 1;
309 sg_init_one(&sg, resp, 64);
311 mmc_set_data_timeout(&data, card, 0);
313 mmc_wait_for_req(card->host, &mrq);
315 if (cmd.error != MMC_ERR_NONE)
316 return cmd.error;
317 if (data.error != MMC_ERR_NONE)
318 return data.error;
320 return MMC_ERR_NONE;