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 <linux/scatterlist.h>
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/mmc.h>
22 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
25 struct mmc_command cmd;
29 memset(&cmd, 0, sizeof(struct mmc_command));
31 cmd.opcode = MMC_SELECT_CARD;
34 cmd.arg = card->rca << 16;
35 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
38 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
41 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
48 int mmc_select_card(struct mmc_card *card)
52 return _mmc_select_card(card->host, card);
55 int mmc_deselect_cards(struct mmc_host *host)
57 return _mmc_select_card(host, NULL);
60 int mmc_go_idle(struct mmc_host *host)
63 struct mmc_command cmd;
66 * Non-SPI hosts need to prevent chipselect going active during
67 * GO_IDLE; that would put chips into SPI mode. Remind them of
68 * that in case of hardware that won't pull up DAT3/nCS otherwise.
70 * SPI hosts ignore ios.chip_select; it's managed according to
71 * rules that must accomodate non-MMC slaves which this layer
72 * won't even know about.
74 if (!mmc_host_is_spi(host)) {
75 mmc_set_chip_select(host, MMC_CS_HIGH);
79 memset(&cmd, 0, sizeof(struct mmc_command));
81 cmd.opcode = MMC_GO_IDLE_STATE;
83 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
85 err = mmc_wait_for_cmd(host, &cmd, 0);
89 if (!mmc_host_is_spi(host)) {
90 mmc_set_chip_select(host, MMC_CS_DONTCARE);
94 host->use_spi_crc = 0;
99 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
101 struct mmc_command cmd;
106 memset(&cmd, 0, sizeof(struct mmc_command));
108 cmd.opcode = MMC_SEND_OP_COND;
109 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
110 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
112 for (i = 100; i; i--) {
113 err = mmc_wait_for_cmd(host, &cmd, 0);
117 /* if we're just probing, do a single pass */
121 /* otherwise wait until reset completes */
122 if (mmc_host_is_spi(host)) {
123 if (!(cmd.resp[0] & R1_SPI_IDLE))
126 if (cmd.resp[0] & MMC_CARD_BUSY)
135 if (rocr && !mmc_host_is_spi(host))
141 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
144 struct mmc_command cmd;
149 memset(&cmd, 0, sizeof(struct mmc_command));
151 cmd.opcode = MMC_ALL_SEND_CID;
153 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
155 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
159 memcpy(cid, cmd.resp, sizeof(u32) * 4);
164 int mmc_set_relative_addr(struct mmc_card *card)
167 struct mmc_command cmd;
172 memset(&cmd, 0, sizeof(struct mmc_command));
174 cmd.opcode = MMC_SET_RELATIVE_ADDR;
175 cmd.arg = card->rca << 16;
176 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
178 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
186 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
189 struct mmc_command cmd;
194 memset(&cmd, 0, sizeof(struct mmc_command));
198 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
200 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
204 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
210 mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
211 u32 opcode, void *buf, unsigned len)
213 struct mmc_request mrq;
214 struct mmc_command cmd;
215 struct mmc_data data;
216 struct scatterlist sg;
219 /* dma onto stack is unsafe/nonportable, but callers to this
220 * routine normally provide temporary on-stack buffers ...
222 data_buf = kmalloc(len, GFP_KERNEL);
223 if (data_buf == NULL)
226 memset(&mrq, 0, sizeof(struct mmc_request));
227 memset(&cmd, 0, sizeof(struct mmc_command));
228 memset(&data, 0, sizeof(struct mmc_data));
236 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
237 * rely on callers to never use this with "native" calls for reading
238 * CSD or CID. Native versions of those commands use the R2 type,
239 * not R1 plus a data block.
241 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
245 data.flags = MMC_DATA_READ;
249 sg_init_one(&sg, data_buf, len);
252 mmc_set_data_timeout(&data, card);
254 mmc_wait_for_req(host, &mrq);
256 memcpy(buf, data_buf, len);
267 int mmc_send_csd(struct mmc_card *card, u32 *csd)
271 if (!mmc_host_is_spi(card->host))
272 return mmc_send_cxd_native(card->host, card->rca << 16,
275 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
279 for (i = 0;i < 4;i++)
280 csd[i] = be32_to_cpu(csd[i]);
285 int mmc_send_cid(struct mmc_host *host, u32 *cid)
289 if (!mmc_host_is_spi(host)) {
292 return mmc_send_cxd_native(host, host->card->rca << 16,
296 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
300 for (i = 0;i < 4;i++)
301 cid[i] = be32_to_cpu(cid[i]);
306 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
308 return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
312 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
314 struct mmc_command cmd;
317 memset(&cmd, 0, sizeof(struct mmc_command));
319 cmd.opcode = MMC_SPI_READ_OCR;
320 cmd.arg = highcap ? (1 << 30) : 0;
321 cmd.flags = MMC_RSP_SPI_R3;
323 err = mmc_wait_for_cmd(host, &cmd, 0);
329 int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
331 struct mmc_command cmd;
334 memset(&cmd, 0, sizeof(struct mmc_command));
336 cmd.opcode = MMC_SPI_CRC_ON_OFF;
337 cmd.flags = MMC_RSP_SPI_R1;
340 err = mmc_wait_for_cmd(host, &cmd, 0);
342 host->use_spi_crc = use_crc;
346 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
349 struct mmc_command cmd;
354 memset(&cmd, 0, sizeof(struct mmc_command));
356 cmd.opcode = MMC_SWITCH;
357 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
361 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
363 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
370 int mmc_send_status(struct mmc_card *card, u32 *status)
373 struct mmc_command cmd;
378 memset(&cmd, 0, sizeof(struct mmc_command));
380 cmd.opcode = MMC_SEND_STATUS;
381 if (!mmc_host_is_spi(card->host))
382 cmd.arg = card->rca << 16;
383 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
385 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
389 /* NOTE: callers are required to understand the difference
390 * between "native" and SPI format status words!
393 *status = cmd.resp[0];