enic: Add new firmware devcmds
[linux/fpc-iii.git] / drivers / mmc / core / mmc_ops.c
blob326447c9ede8aa5a9153f8019a47f706f20917ec
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/slab.h>
13 #include <linux/types.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_card_sleepawake(struct mmc_host *host, int sleep)
63 struct mmc_command cmd;
64 struct mmc_card *card = host->card;
65 int err;
67 if (sleep)
68 mmc_deselect_cards(host);
70 memset(&cmd, 0, sizeof(struct mmc_command));
72 cmd.opcode = MMC_SLEEP_AWAKE;
73 cmd.arg = card->rca << 16;
74 if (sleep)
75 cmd.arg |= 1 << 15;
77 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
78 err = mmc_wait_for_cmd(host, &cmd, 0);
79 if (err)
80 return err;
83 * If the host does not wait while the card signals busy, then we will
84 * will have to wait the sleep/awake timeout. Note, we cannot use the
85 * SEND_STATUS command to poll the status because that command (and most
86 * others) is invalid while the card sleeps.
88 if (!(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
89 mmc_delay(DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000));
91 if (!sleep)
92 err = mmc_select_card(card);
94 return err;
97 int mmc_go_idle(struct mmc_host *host)
99 int err;
100 struct mmc_command cmd;
103 * Non-SPI hosts need to prevent chipselect going active during
104 * GO_IDLE; that would put chips into SPI mode. Remind them of
105 * that in case of hardware that won't pull up DAT3/nCS otherwise.
107 * SPI hosts ignore ios.chip_select; it's managed according to
108 * rules that must accomodate non-MMC slaves which this layer
109 * won't even know about.
111 if (!mmc_host_is_spi(host)) {
112 mmc_set_chip_select(host, MMC_CS_HIGH);
113 mmc_delay(1);
116 memset(&cmd, 0, sizeof(struct mmc_command));
118 cmd.opcode = MMC_GO_IDLE_STATE;
119 cmd.arg = 0;
120 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
122 err = mmc_wait_for_cmd(host, &cmd, 0);
124 mmc_delay(1);
126 if (!mmc_host_is_spi(host)) {
127 mmc_set_chip_select(host, MMC_CS_DONTCARE);
128 mmc_delay(1);
131 host->use_spi_crc = 0;
133 return err;
136 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
138 struct mmc_command cmd;
139 int i, err = 0;
141 BUG_ON(!host);
143 memset(&cmd, 0, sizeof(struct mmc_command));
145 cmd.opcode = MMC_SEND_OP_COND;
146 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
147 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
149 for (i = 100; i; i--) {
150 err = mmc_wait_for_cmd(host, &cmd, 0);
151 if (err)
152 break;
154 /* if we're just probing, do a single pass */
155 if (ocr == 0)
156 break;
158 /* otherwise wait until reset completes */
159 if (mmc_host_is_spi(host)) {
160 if (!(cmd.resp[0] & R1_SPI_IDLE))
161 break;
162 } else {
163 if (cmd.resp[0] & MMC_CARD_BUSY)
164 break;
167 err = -ETIMEDOUT;
169 mmc_delay(10);
172 if (rocr && !mmc_host_is_spi(host))
173 *rocr = cmd.resp[0];
175 return err;
178 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
180 int err;
181 struct mmc_command cmd;
183 BUG_ON(!host);
184 BUG_ON(!cid);
186 memset(&cmd, 0, sizeof(struct mmc_command));
188 cmd.opcode = MMC_ALL_SEND_CID;
189 cmd.arg = 0;
190 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
192 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
193 if (err)
194 return err;
196 memcpy(cid, cmd.resp, sizeof(u32) * 4);
198 return 0;
201 int mmc_set_relative_addr(struct mmc_card *card)
203 int err;
204 struct mmc_command cmd;
206 BUG_ON(!card);
207 BUG_ON(!card->host);
209 memset(&cmd, 0, sizeof(struct mmc_command));
211 cmd.opcode = MMC_SET_RELATIVE_ADDR;
212 cmd.arg = card->rca << 16;
213 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
215 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
216 if (err)
217 return err;
219 return 0;
222 static int
223 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
225 int err;
226 struct mmc_command cmd;
228 BUG_ON(!host);
229 BUG_ON(!cxd);
231 memset(&cmd, 0, sizeof(struct mmc_command));
233 cmd.opcode = opcode;
234 cmd.arg = arg;
235 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
237 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
238 if (err)
239 return err;
241 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
243 return 0;
246 static int
247 mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
248 u32 opcode, void *buf, unsigned len)
250 struct mmc_request mrq;
251 struct mmc_command cmd;
252 struct mmc_data data;
253 struct scatterlist sg;
254 void *data_buf;
256 /* dma onto stack is unsafe/nonportable, but callers to this
257 * routine normally provide temporary on-stack buffers ...
259 data_buf = kmalloc(len, GFP_KERNEL);
260 if (data_buf == NULL)
261 return -ENOMEM;
263 memset(&mrq, 0, sizeof(struct mmc_request));
264 memset(&cmd, 0, sizeof(struct mmc_command));
265 memset(&data, 0, sizeof(struct mmc_data));
267 mrq.cmd = &cmd;
268 mrq.data = &data;
270 cmd.opcode = opcode;
271 cmd.arg = 0;
273 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
274 * rely on callers to never use this with "native" calls for reading
275 * CSD or CID. Native versions of those commands use the R2 type,
276 * not R1 plus a data block.
278 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
280 data.blksz = len;
281 data.blocks = 1;
282 data.flags = MMC_DATA_READ;
283 data.sg = &sg;
284 data.sg_len = 1;
286 sg_init_one(&sg, data_buf, len);
288 if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
290 * The spec states that CSR and CID accesses have a timeout
291 * of 64 clock cycles.
293 data.timeout_ns = 0;
294 data.timeout_clks = 64;
295 } else
296 mmc_set_data_timeout(&data, card);
298 mmc_wait_for_req(host, &mrq);
300 memcpy(buf, data_buf, len);
301 kfree(data_buf);
303 if (cmd.error)
304 return cmd.error;
305 if (data.error)
306 return data.error;
308 return 0;
311 int mmc_send_csd(struct mmc_card *card, u32 *csd)
313 int ret, i;
315 if (!mmc_host_is_spi(card->host))
316 return mmc_send_cxd_native(card->host, card->rca << 16,
317 csd, MMC_SEND_CSD);
319 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
320 if (ret)
321 return ret;
323 for (i = 0;i < 4;i++)
324 csd[i] = be32_to_cpu(csd[i]);
326 return 0;
329 int mmc_send_cid(struct mmc_host *host, u32 *cid)
331 int ret, i;
333 if (!mmc_host_is_spi(host)) {
334 if (!host->card)
335 return -EINVAL;
336 return mmc_send_cxd_native(host, host->card->rca << 16,
337 cid, MMC_SEND_CID);
340 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
341 if (ret)
342 return ret;
344 for (i = 0;i < 4;i++)
345 cid[i] = be32_to_cpu(cid[i]);
347 return 0;
350 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
352 return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
353 ext_csd, 512);
356 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
358 struct mmc_command cmd;
359 int err;
361 memset(&cmd, 0, sizeof(struct mmc_command));
363 cmd.opcode = MMC_SPI_READ_OCR;
364 cmd.arg = highcap ? (1 << 30) : 0;
365 cmd.flags = MMC_RSP_SPI_R3;
367 err = mmc_wait_for_cmd(host, &cmd, 0);
369 *ocrp = cmd.resp[1];
370 return err;
373 int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
375 struct mmc_command cmd;
376 int err;
378 memset(&cmd, 0, sizeof(struct mmc_command));
380 cmd.opcode = MMC_SPI_CRC_ON_OFF;
381 cmd.flags = MMC_RSP_SPI_R1;
382 cmd.arg = use_crc;
384 err = mmc_wait_for_cmd(host, &cmd, 0);
385 if (!err)
386 host->use_spi_crc = use_crc;
387 return err;
390 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
392 int err;
393 struct mmc_command cmd;
394 u32 status;
396 BUG_ON(!card);
397 BUG_ON(!card->host);
399 memset(&cmd, 0, sizeof(struct mmc_command));
401 cmd.opcode = MMC_SWITCH;
402 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
403 (index << 16) |
404 (value << 8) |
405 set;
406 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
408 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
409 if (err)
410 return err;
412 /* Must check status to be sure of no errors */
413 do {
414 err = mmc_send_status(card, &status);
415 if (err)
416 return err;
417 if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
418 break;
419 if (mmc_host_is_spi(card->host))
420 break;
421 } while (R1_CURRENT_STATE(status) == 7);
423 if (mmc_host_is_spi(card->host)) {
424 if (status & R1_SPI_ILLEGAL_COMMAND)
425 return -EBADMSG;
426 } else {
427 if (status & 0xFDFFA000)
428 printk(KERN_WARNING "%s: unexpected status %#x after "
429 "switch", mmc_hostname(card->host), status);
430 if (status & R1_SWITCH_ERROR)
431 return -EBADMSG;
434 return 0;
437 int mmc_send_status(struct mmc_card *card, u32 *status)
439 int err;
440 struct mmc_command cmd;
442 BUG_ON(!card);
443 BUG_ON(!card->host);
445 memset(&cmd, 0, sizeof(struct mmc_command));
447 cmd.opcode = MMC_SEND_STATUS;
448 if (!mmc_host_is_spi(card->host))
449 cmd.arg = card->rca << 16;
450 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
452 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
453 if (err)
454 return err;
456 /* NOTE: callers are required to understand the difference
457 * between "native" and SPI format status words!
459 if (status)
460 *status = cmd.resp[0];
462 return 0;