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>
23 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
26 struct mmc_command cmd;
30 memset(&cmd, 0, sizeof(struct mmc_command));
32 cmd.opcode = MMC_SELECT_CARD;
35 cmd.arg = card->rca << 16;
36 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
39 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
42 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
49 int mmc_select_card(struct mmc_card *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;
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;
77 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
78 err = mmc_wait_for_cmd(host, &cmd, 0);
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));
92 err = mmc_select_card(card);
97 int mmc_go_idle(struct mmc_host *host)
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);
116 memset(&cmd, 0, sizeof(struct mmc_command));
118 cmd.opcode = MMC_GO_IDLE_STATE;
120 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
122 err = mmc_wait_for_cmd(host, &cmd, 0);
126 if (!mmc_host_is_spi(host)) {
127 mmc_set_chip_select(host, MMC_CS_DONTCARE);
131 host->use_spi_crc = 0;
136 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
138 struct mmc_command cmd;
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);
154 /* if we're just probing, do a single pass */
158 /* otherwise wait until reset completes */
159 if (mmc_host_is_spi(host)) {
160 if (!(cmd.resp[0] & R1_SPI_IDLE))
163 if (cmd.resp[0] & MMC_CARD_BUSY)
172 if (rocr && !mmc_host_is_spi(host))
178 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
181 struct mmc_command cmd;
186 memset(&cmd, 0, sizeof(struct mmc_command));
188 cmd.opcode = MMC_ALL_SEND_CID;
190 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
192 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
196 memcpy(cid, cmd.resp, sizeof(u32) * 4);
201 int mmc_set_relative_addr(struct mmc_card *card)
204 struct mmc_command cmd;
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);
223 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
226 struct mmc_command cmd;
231 memset(&cmd, 0, sizeof(struct mmc_command));
235 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
237 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
241 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
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;
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)
263 memset(&mrq, 0, sizeof(struct mmc_request));
264 memset(&cmd, 0, sizeof(struct mmc_command));
265 memset(&data, 0, sizeof(struct mmc_data));
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;
282 data.flags = MMC_DATA_READ;
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.
294 data.timeout_clks = 64;
296 mmc_set_data_timeout(&data, card);
298 mmc_wait_for_req(host, &mrq);
300 memcpy(buf, data_buf, len);
311 int mmc_send_csd(struct mmc_card *card, u32 *csd)
315 if (!mmc_host_is_spi(card->host))
316 return mmc_send_cxd_native(card->host, card->rca << 16,
319 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
323 for (i = 0;i < 4;i++)
324 csd[i] = be32_to_cpu(csd[i]);
329 int mmc_send_cid(struct mmc_host *host, u32 *cid)
333 if (!mmc_host_is_spi(host)) {
336 return mmc_send_cxd_native(host, host->card->rca << 16,
340 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
344 for (i = 0;i < 4;i++)
345 cid[i] = be32_to_cpu(cid[i]);
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,
356 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
358 struct mmc_command cmd;
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);
373 int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
375 struct mmc_command cmd;
378 memset(&cmd, 0, sizeof(struct mmc_command));
380 cmd.opcode = MMC_SPI_CRC_ON_OFF;
381 cmd.flags = MMC_RSP_SPI_R1;
384 err = mmc_wait_for_cmd(host, &cmd, 0);
386 host->use_spi_crc = use_crc;
391 * mmc_switch - modify EXT_CSD register
392 * @card: the MMC card associated with the data transfer
393 * @set: cmd set values
394 * @index: EXT_CSD register index
395 * @value: value to program into EXT_CSD register
397 * Modifies the EXT_CSD register for selected card.
399 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
402 struct mmc_command cmd;
408 memset(&cmd, 0, sizeof(struct mmc_command));
410 cmd.opcode = MMC_SWITCH;
411 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
415 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
417 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
421 /* Must check status to be sure of no errors */
423 err = mmc_send_status(card, &status);
426 if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
428 if (mmc_host_is_spi(card->host))
430 } while (R1_CURRENT_STATE(status) == 7);
432 if (mmc_host_is_spi(card->host)) {
433 if (status & R1_SPI_ILLEGAL_COMMAND)
436 if (status & 0xFDFFA000)
437 printk(KERN_WARNING "%s: unexpected status %#x after "
438 "switch", mmc_hostname(card->host), status);
439 if (status & R1_SWITCH_ERROR)
446 EXPORT_SYMBOL(mmc_switch);
448 int mmc_send_status(struct mmc_card *card, u32 *status)
451 struct mmc_command cmd;
456 memset(&cmd, 0, sizeof(struct mmc_command));
458 cmd.opcode = MMC_SEND_STATUS;
459 if (!mmc_host_is_spi(card->host))
460 cmd.arg = card->rca << 16;
461 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
463 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
467 /* NOTE: callers are required to understand the difference
468 * between "native" and SPI format status words!
471 *status = cmd.resp[0];