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>
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_go_idle(struct mmc_host *host)
64 struct mmc_command cmd;
67 * Non-SPI hosts need to prevent chipselect going active during
68 * GO_IDLE; that would put chips into SPI mode. Remind them of
69 * that in case of hardware that won't pull up DAT3/nCS otherwise.
71 * SPI hosts ignore ios.chip_select; it's managed according to
72 * rules that must accomodate non-MMC slaves which this layer
73 * won't even know about.
75 if (!mmc_host_is_spi(host)) {
76 mmc_set_chip_select(host, MMC_CS_HIGH);
80 memset(&cmd, 0, sizeof(struct mmc_command));
82 cmd.opcode = MMC_GO_IDLE_STATE;
84 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
86 err = mmc_wait_for_cmd(host, &cmd, 0);
90 if (!mmc_host_is_spi(host)) {
91 mmc_set_chip_select(host, MMC_CS_DONTCARE);
95 host->use_spi_crc = 0;
100 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
102 struct mmc_command cmd;
107 memset(&cmd, 0, sizeof(struct mmc_command));
109 cmd.opcode = MMC_SEND_OP_COND;
110 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
111 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
113 for (i = 100; i; i--) {
114 err = mmc_wait_for_cmd(host, &cmd, 0);
118 /* if we're just probing, do a single pass */
122 /* otherwise wait until reset completes */
123 if (mmc_host_is_spi(host)) {
124 if (!(cmd.resp[0] & R1_SPI_IDLE))
127 if (cmd.resp[0] & MMC_CARD_BUSY)
136 if (rocr && !mmc_host_is_spi(host))
142 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
145 struct mmc_command cmd;
150 memset(&cmd, 0, sizeof(struct mmc_command));
152 cmd.opcode = MMC_ALL_SEND_CID;
154 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
156 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
160 memcpy(cid, cmd.resp, sizeof(u32) * 4);
165 int mmc_set_relative_addr(struct mmc_card *card)
168 struct mmc_command cmd;
173 memset(&cmd, 0, sizeof(struct mmc_command));
175 cmd.opcode = MMC_SET_RELATIVE_ADDR;
176 cmd.arg = card->rca << 16;
177 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
179 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
187 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
190 struct mmc_command cmd;
195 memset(&cmd, 0, sizeof(struct mmc_command));
199 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
201 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
205 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
211 mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
212 u32 opcode, void *buf, unsigned len)
214 struct mmc_request mrq;
215 struct mmc_command cmd;
216 struct mmc_data data;
217 struct scatterlist sg;
220 /* dma onto stack is unsafe/nonportable, but callers to this
221 * routine normally provide temporary on-stack buffers ...
223 data_buf = kmalloc(len, GFP_KERNEL);
224 if (data_buf == NULL)
227 memset(&mrq, 0, sizeof(struct mmc_request));
228 memset(&cmd, 0, sizeof(struct mmc_command));
229 memset(&data, 0, sizeof(struct mmc_data));
237 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
238 * rely on callers to never use this with "native" calls for reading
239 * CSD or CID. Native versions of those commands use the R2 type,
240 * not R1 plus a data block.
242 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
246 data.flags = MMC_DATA_READ;
250 sg_init_one(&sg, data_buf, len);
253 mmc_set_data_timeout(&data, card);
255 mmc_wait_for_req(host, &mrq);
257 memcpy(buf, data_buf, len);
268 int mmc_send_csd(struct mmc_card *card, u32 *csd)
270 if (!mmc_host_is_spi(card->host))
271 return mmc_send_cxd_native(card->host, card->rca << 16,
274 return mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
277 int mmc_send_cid(struct mmc_host *host, u32 *cid)
279 if (!mmc_host_is_spi(host)) {
282 return mmc_send_cxd_native(host, host->card->rca << 16,
286 return mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
289 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
291 return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
295 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
297 struct mmc_command cmd;
300 memset(&cmd, 0, sizeof(struct mmc_command));
302 cmd.opcode = MMC_SPI_READ_OCR;
303 cmd.arg = highcap ? (1 << 30) : 0;
304 cmd.flags = MMC_RSP_SPI_R3;
306 err = mmc_wait_for_cmd(host, &cmd, 0);
312 int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
314 struct mmc_command cmd;
317 memset(&cmd, 0, sizeof(struct mmc_command));
319 cmd.opcode = MMC_SPI_CRC_ON_OFF;
320 cmd.flags = MMC_RSP_SPI_R1;
323 err = mmc_wait_for_cmd(host, &cmd, 0);
325 host->use_spi_crc = use_crc;
329 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
332 struct mmc_command cmd;
337 memset(&cmd, 0, sizeof(struct mmc_command));
339 cmd.opcode = MMC_SWITCH;
340 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
344 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
346 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
353 int mmc_send_status(struct mmc_card *card, u32 *status)
356 struct mmc_command cmd;
361 memset(&cmd, 0, sizeof(struct mmc_command));
363 cmd.opcode = MMC_SEND_STATUS;
364 if (!mmc_host_is_spi(card->host))
365 cmd.arg = card->rca << 16;
366 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
368 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
372 /* NOTE: callers are required to understand the difference
373 * between "native" and SPI format status words!
376 *status = cmd.resp[0];