mmc: remove custom error codes
[linux-2.6/mini2440.git] / drivers / mmc / core / mmc_ops.c
blob15cd575effaaea177e9151b9b5ea9e85f48b2d47
1 /*
2 * linux/drivers/mmc/core/mmc_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>
20 #include "core.h"
21 #include "mmc_ops.h"
23 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
25 int err;
26 struct mmc_command cmd;
28 BUG_ON(!host);
30 memset(&cmd, 0, sizeof(struct mmc_command));
32 cmd.opcode = MMC_SELECT_CARD;
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_NONE | MMC_CMD_AC;
42 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
43 if (err)
44 return err;
46 return 0;
49 int mmc_select_card(struct mmc_card *card)
51 BUG_ON(!card);
53 return _mmc_select_card(card->host, card);
56 int mmc_deselect_cards(struct mmc_host *host)
58 return _mmc_select_card(host, NULL);
61 int mmc_go_idle(struct mmc_host *host)
63 int err;
64 struct mmc_command cmd;
66 mmc_set_chip_select(host, MMC_CS_HIGH);
68 mmc_delay(1);
70 memset(&cmd, 0, sizeof(struct mmc_command));
72 cmd.opcode = MMC_GO_IDLE_STATE;
73 cmd.arg = 0;
74 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
76 err = mmc_wait_for_cmd(host, &cmd, 0);
78 mmc_delay(1);
80 mmc_set_chip_select(host, MMC_CS_DONTCARE);
82 mmc_delay(1);
84 return err;
87 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
89 struct mmc_command cmd;
90 int i, err = 0;
92 BUG_ON(!host);
94 memset(&cmd, 0, sizeof(struct mmc_command));
96 cmd.opcode = MMC_SEND_OP_COND;
97 cmd.arg = ocr;
98 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
100 for (i = 100; i; i--) {
101 err = mmc_wait_for_cmd(host, &cmd, 0);
102 if (err)
103 break;
105 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
106 break;
108 err = -ETIMEDOUT;
110 mmc_delay(10);
113 if (rocr)
114 *rocr = cmd.resp[0];
116 return err;
119 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
121 int err;
122 struct mmc_command cmd;
124 BUG_ON(!host);
125 BUG_ON(!cid);
127 memset(&cmd, 0, sizeof(struct mmc_command));
129 cmd.opcode = MMC_ALL_SEND_CID;
130 cmd.arg = 0;
131 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
133 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
134 if (err)
135 return err;
137 memcpy(cid, cmd.resp, sizeof(u32) * 4);
139 return 0;
142 int mmc_set_relative_addr(struct mmc_card *card)
144 int err;
145 struct mmc_command cmd;
147 BUG_ON(!card);
148 BUG_ON(!card->host);
150 memset(&cmd, 0, sizeof(struct mmc_command));
152 cmd.opcode = MMC_SET_RELATIVE_ADDR;
153 cmd.arg = card->rca << 16;
154 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
156 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
157 if (err)
158 return err;
160 return 0;
163 int mmc_send_csd(struct mmc_card *card, u32 *csd)
165 int err;
166 struct mmc_command cmd;
168 BUG_ON(!card);
169 BUG_ON(!card->host);
170 BUG_ON(!csd);
172 memset(&cmd, 0, sizeof(struct mmc_command));
174 cmd.opcode = MMC_SEND_CSD;
175 cmd.arg = card->rca << 16;
176 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
178 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
179 if (err)
180 return err;
182 memcpy(csd, cmd.resp, sizeof(u32) * 4);
184 return 0;
187 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
189 struct mmc_request mrq;
190 struct mmc_command cmd;
191 struct mmc_data data;
192 struct scatterlist sg;
194 BUG_ON(!card);
195 BUG_ON(!card->host);
196 BUG_ON(!ext_csd);
198 memset(&mrq, 0, sizeof(struct mmc_request));
199 memset(&cmd, 0, sizeof(struct mmc_command));
200 memset(&data, 0, sizeof(struct mmc_data));
202 mrq.cmd = &cmd;
203 mrq.data = &data;
205 cmd.opcode = MMC_SEND_EXT_CSD;
206 cmd.arg = 0;
207 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
209 data.blksz = 512;
210 data.blocks = 1;
211 data.flags = MMC_DATA_READ;
212 data.sg = &sg;
213 data.sg_len = 1;
215 sg_init_one(&sg, ext_csd, 512);
217 mmc_set_data_timeout(&data, card, 0);
219 mmc_wait_for_req(card->host, &mrq);
221 if (cmd.error)
222 return cmd.error;
223 if (data.error)
224 return data.error;
226 return 0;
229 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
231 int err;
232 struct mmc_command cmd;
234 BUG_ON(!card);
235 BUG_ON(!card->host);
237 memset(&cmd, 0, sizeof(struct mmc_command));
239 cmd.opcode = MMC_SWITCH;
240 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
241 (index << 16) |
242 (value << 8) |
243 set;
244 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
246 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
247 if (err)
248 return err;
250 return 0;
253 int mmc_send_status(struct mmc_card *card, u32 *status)
255 int err;
256 struct mmc_command cmd;
258 BUG_ON(!card);
259 BUG_ON(!card->host);
261 memset(&cmd, 0, sizeof(struct mmc_command));
263 cmd.opcode = MMC_SEND_STATUS;
264 cmd.arg = card->rca << 16;
265 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
267 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
268 if (err)
269 return err;
271 if (status)
272 *status = cmd.resp[0];
274 return 0;