fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / drivers / mmc / core / mmc_ops.c
blobbf4bc6adcfef8fbed951a274ee2cc4ca12dae36f
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;
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);
77 mmc_delay(1);
80 memset(&cmd, 0, sizeof(struct mmc_command));
82 cmd.opcode = MMC_GO_IDLE_STATE;
83 cmd.arg = 0;
84 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
86 err = mmc_wait_for_cmd(host, &cmd, 0);
88 mmc_delay(1);
90 if (!mmc_host_is_spi(host)) {
91 mmc_set_chip_select(host, MMC_CS_DONTCARE);
92 mmc_delay(1);
95 host->use_spi_crc = 0;
97 return err;
100 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
102 struct mmc_command cmd;
103 int i, err = 0;
105 BUG_ON(!host);
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);
115 if (err)
116 break;
118 /* if we're just probing, do a single pass */
119 if (ocr == 0)
120 break;
122 /* otherwise wait until reset completes */
123 if (mmc_host_is_spi(host)) {
124 if (!(cmd.resp[0] & R1_SPI_IDLE))
125 break;
126 } else {
127 if (cmd.resp[0] & MMC_CARD_BUSY)
128 break;
131 err = -ETIMEDOUT;
133 mmc_delay(10);
136 if (rocr && !mmc_host_is_spi(host))
137 *rocr = cmd.resp[0];
139 return err;
142 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
144 int err;
145 struct mmc_command cmd;
147 BUG_ON(!host);
148 BUG_ON(!cid);
150 memset(&cmd, 0, sizeof(struct mmc_command));
152 cmd.opcode = MMC_ALL_SEND_CID;
153 cmd.arg = 0;
154 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
156 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
157 if (err)
158 return err;
160 memcpy(cid, cmd.resp, sizeof(u32) * 4);
162 return 0;
165 int mmc_set_relative_addr(struct mmc_card *card)
167 int err;
168 struct mmc_command cmd;
170 BUG_ON(!card);
171 BUG_ON(!card->host);
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);
180 if (err)
181 return err;
183 return 0;
186 static int
187 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
189 int err;
190 struct mmc_command cmd;
192 BUG_ON(!host);
193 BUG_ON(!cxd);
195 memset(&cmd, 0, sizeof(struct mmc_command));
197 cmd.opcode = opcode;
198 cmd.arg = arg;
199 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
201 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
202 if (err)
203 return err;
205 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
207 return 0;
210 static int
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;
218 void *data_buf;
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)
225 return -ENOMEM;
227 memset(&mrq, 0, sizeof(struct mmc_request));
228 memset(&cmd, 0, sizeof(struct mmc_command));
229 memset(&data, 0, sizeof(struct mmc_data));
231 mrq.cmd = &cmd;
232 mrq.data = &data;
234 cmd.opcode = opcode;
235 cmd.arg = 0;
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;
244 data.blksz = len;
245 data.blocks = 1;
246 data.flags = MMC_DATA_READ;
247 data.sg = &sg;
248 data.sg_len = 1;
250 sg_init_one(&sg, data_buf, len);
252 if (card)
253 mmc_set_data_timeout(&data, card);
255 mmc_wait_for_req(host, &mrq);
257 memcpy(buf, data_buf, len);
258 kfree(data_buf);
260 if (cmd.error)
261 return cmd.error;
262 if (data.error)
263 return data.error;
265 return 0;
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,
272 csd, MMC_SEND_CSD);
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)) {
280 if (!host->card)
281 return -EINVAL;
282 return mmc_send_cxd_native(host, host->card->rca << 16,
283 cid, MMC_SEND_CID);
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,
292 ext_csd, 512);
295 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
297 struct mmc_command cmd;
298 int err;
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);
308 *ocrp = cmd.resp[1];
309 return err;
312 int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
314 struct mmc_command cmd;
315 int err;
317 memset(&cmd, 0, sizeof(struct mmc_command));
319 cmd.opcode = MMC_SPI_CRC_ON_OFF;
320 cmd.flags = MMC_RSP_SPI_R1;
321 cmd.arg = use_crc;
323 err = mmc_wait_for_cmd(host, &cmd, 0);
324 if (!err)
325 host->use_spi_crc = use_crc;
326 return err;
329 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
331 int err;
332 struct mmc_command cmd;
334 BUG_ON(!card);
335 BUG_ON(!card->host);
337 memset(&cmd, 0, sizeof(struct mmc_command));
339 cmd.opcode = MMC_SWITCH;
340 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
341 (index << 16) |
342 (value << 8) |
343 set;
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);
347 if (err)
348 return err;
350 return 0;
353 int mmc_send_status(struct mmc_card *card, u32 *status)
355 int err;
356 struct mmc_command cmd;
358 BUG_ON(!card);
359 BUG_ON(!card->host);
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);
369 if (err)
370 return err;
372 /* NOTE: callers are required to understand the difference
373 * between "native" and SPI format status words!
375 if (status)
376 *status = cmd.resp[0];
378 return 0;