blob: 64b05c6270f2f3aef54d88e309f8772694c4347a [file] [log] [blame]
Pierre Ossmanda7fbe52006-12-24 22:46:55 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/core/mmc_ops.h
Pierre Ossmanda7fbe52006-12-24 22:46:55 +01003 *
4 * Copyright 2006-2007 Pierre Ossman
5 *
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.
10 */
11
12#include <linux/types.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010013#include <linux/scatterlist.h>
14
15#include <linux/mmc/host.h>
16#include <linux/mmc/card.h>
17#include <linux/mmc/mmc.h>
18
19#include "core.h"
20#include "mmc_ops.h"
21
22static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
23{
24 int err;
25 struct mmc_command cmd;
26
27 BUG_ON(!host);
28
29 memset(&cmd, 0, sizeof(struct mmc_command));
30
31 cmd.opcode = MMC_SELECT_CARD;
32
33 if (card) {
34 cmd.arg = card->rca << 16;
35 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
36 } else {
37 cmd.arg = 0;
38 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
39 }
40
41 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +020042 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043 return err;
44
Pierre Ossman17b04292007-07-22 22:18:46 +020045 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010046}
47
48int mmc_select_card(struct mmc_card *card)
49{
50 BUG_ON(!card);
51
52 return _mmc_select_card(card->host, card);
53}
54
55int mmc_deselect_cards(struct mmc_host *host)
56{
57 return _mmc_select_card(host, NULL);
58}
59
60int mmc_go_idle(struct mmc_host *host)
61{
62 int err;
63 struct mmc_command cmd;
64
David Brownellaf517152007-08-08 09:11:32 -070065 /*
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.
69 *
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.
73 */
74 if (!mmc_host_is_spi(host)) {
75 mmc_set_chip_select(host, MMC_CS_HIGH);
76 mmc_delay(1);
77 }
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010078
79 memset(&cmd, 0, sizeof(struct mmc_command));
80
81 cmd.opcode = MMC_GO_IDLE_STATE;
82 cmd.arg = 0;
David Brownellaf517152007-08-08 09:11:32 -070083 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010084
85 err = mmc_wait_for_cmd(host, &cmd, 0);
86
87 mmc_delay(1);
88
David Brownellaf517152007-08-08 09:11:32 -070089 if (!mmc_host_is_spi(host)) {
90 mmc_set_chip_select(host, MMC_CS_DONTCARE);
91 mmc_delay(1);
92 }
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010093
David Brownellaf517152007-08-08 09:11:32 -070094 host->use_spi_crc = 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010095
96 return err;
97}
98
99int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
100{
101 struct mmc_command cmd;
102 int i, err = 0;
103
104 BUG_ON(!host);
105
106 memset(&cmd, 0, sizeof(struct mmc_command));
107
108 cmd.opcode = MMC_SEND_OP_COND;
David Brownellaf517152007-08-08 09:11:32 -0700109 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
110 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100111
112 for (i = 100; i; i--) {
113 err = mmc_wait_for_cmd(host, &cmd, 0);
Pierre Ossman17b04292007-07-22 22:18:46 +0200114 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100115 break;
116
David Brownellaf517152007-08-08 09:11:32 -0700117 /* if we're just probing, do a single pass */
118 if (ocr == 0)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100119 break;
120
David Brownellaf517152007-08-08 09:11:32 -0700121 /* otherwise wait until reset completes */
122 if (mmc_host_is_spi(host)) {
123 if (!(cmd.resp[0] & R1_SPI_IDLE))
124 break;
125 } else {
126 if (cmd.resp[0] & MMC_CARD_BUSY)
127 break;
128 }
129
Pierre Ossman17b04292007-07-22 22:18:46 +0200130 err = -ETIMEDOUT;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100131
132 mmc_delay(10);
133 }
134
David Brownellaf517152007-08-08 09:11:32 -0700135 if (rocr && !mmc_host_is_spi(host))
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100136 *rocr = cmd.resp[0];
137
138 return err;
139}
140
141int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
142{
143 int err;
144 struct mmc_command cmd;
145
146 BUG_ON(!host);
147 BUG_ON(!cid);
148
149 memset(&cmd, 0, sizeof(struct mmc_command));
150
151 cmd.opcode = MMC_ALL_SEND_CID;
152 cmd.arg = 0;
153 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
154
155 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200156 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100157 return err;
158
159 memcpy(cid, cmd.resp, sizeof(u32) * 4);
160
Pierre Ossman17b04292007-07-22 22:18:46 +0200161 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100162}
163
164int mmc_set_relative_addr(struct mmc_card *card)
165{
166 int err;
167 struct mmc_command cmd;
168
169 BUG_ON(!card);
170 BUG_ON(!card->host);
171
172 memset(&cmd, 0, sizeof(struct mmc_command));
173
174 cmd.opcode = MMC_SET_RELATIVE_ADDR;
175 cmd.arg = card->rca << 16;
176 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
177
178 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200179 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100180 return err;
181
Pierre Ossman17b04292007-07-22 22:18:46 +0200182 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100183}
184
David Brownellaf517152007-08-08 09:11:32 -0700185static int
186mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100187{
188 int err;
189 struct mmc_command cmd;
190
David Brownellaf517152007-08-08 09:11:32 -0700191 BUG_ON(!host);
192 BUG_ON(!cxd);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100193
194 memset(&cmd, 0, sizeof(struct mmc_command));
195
David Brownellaf517152007-08-08 09:11:32 -0700196 cmd.opcode = opcode;
197 cmd.arg = arg;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100198 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
199
David Brownellaf517152007-08-08 09:11:32 -0700200 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200201 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100202 return err;
203
David Brownellaf517152007-08-08 09:11:32 -0700204 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100205
Pierre Ossman17b04292007-07-22 22:18:46 +0200206 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100207}
208
David Brownellaf517152007-08-08 09:11:32 -0700209static int
210mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
211 u32 opcode, void *buf, unsigned len)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100212{
213 struct mmc_request mrq;
214 struct mmc_command cmd;
215 struct mmc_data data;
216 struct scatterlist sg;
David Brownellaf517152007-08-08 09:11:32 -0700217 void *data_buf;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100218
David Brownellaf517152007-08-08 09:11:32 -0700219 /* dma onto stack is unsafe/nonportable, but callers to this
220 * routine normally provide temporary on-stack buffers ...
221 */
222 data_buf = kmalloc(len, GFP_KERNEL);
223 if (data_buf == NULL)
224 return -ENOMEM;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100225
226 memset(&mrq, 0, sizeof(struct mmc_request));
227 memset(&cmd, 0, sizeof(struct mmc_command));
228 memset(&data, 0, sizeof(struct mmc_data));
229
230 mrq.cmd = &cmd;
231 mrq.data = &data;
232
David Brownellaf517152007-08-08 09:11:32 -0700233 cmd.opcode = opcode;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100234 cmd.arg = 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100235
David Brownellaf517152007-08-08 09:11:32 -0700236 /* 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.
240 */
241 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
242
243 data.blksz = len;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100244 data.blocks = 1;
245 data.flags = MMC_DATA_READ;
246 data.sg = &sg;
247 data.sg_len = 1;
248
David Brownellaf517152007-08-08 09:11:32 -0700249 sg_init_one(&sg, data_buf, len);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100250
David Brownellaf517152007-08-08 09:11:32 -0700251 if (card)
252 mmc_set_data_timeout(&data, card);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100253
David Brownellaf517152007-08-08 09:11:32 -0700254 mmc_wait_for_req(host, &mrq);
255
256 memcpy(buf, data_buf, len);
257 kfree(data_buf);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100258
Pierre Ossman17b04292007-07-22 22:18:46 +0200259 if (cmd.error)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100260 return cmd.error;
Pierre Ossman17b04292007-07-22 22:18:46 +0200261 if (data.error)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100262 return data.error;
263
Pierre Ossman17b04292007-07-22 22:18:46 +0200264 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100265}
266
David Brownellaf517152007-08-08 09:11:32 -0700267int mmc_send_csd(struct mmc_card *card, u32 *csd)
268{
Pierre Ossman78e48072007-10-27 14:14:23 +0200269 int ret, i;
270
David Brownellaf517152007-08-08 09:11:32 -0700271 if (!mmc_host_is_spi(card->host))
272 return mmc_send_cxd_native(card->host, card->rca << 16,
273 csd, MMC_SEND_CSD);
274
Pierre Ossman78e48072007-10-27 14:14:23 +0200275 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
276 if (ret)
277 return ret;
278
279 for (i = 0;i < 4;i++)
280 csd[i] = be32_to_cpu(csd[i]);
281
282 return 0;
David Brownellaf517152007-08-08 09:11:32 -0700283}
284
285int mmc_send_cid(struct mmc_host *host, u32 *cid)
286{
Pierre Ossman78e48072007-10-27 14:14:23 +0200287 int ret, i;
288
David Brownellaf517152007-08-08 09:11:32 -0700289 if (!mmc_host_is_spi(host)) {
290 if (!host->card)
291 return -EINVAL;
292 return mmc_send_cxd_native(host, host->card->rca << 16,
293 cid, MMC_SEND_CID);
294 }
295
Pierre Ossman78e48072007-10-27 14:14:23 +0200296 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
297 if (ret)
298 return ret;
299
300 for (i = 0;i < 4;i++)
301 cid[i] = be32_to_cpu(cid[i]);
302
303 return 0;
David Brownellaf517152007-08-08 09:11:32 -0700304}
305
306int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
307{
308 return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
309 ext_csd, 512);
310}
311
312int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
313{
314 struct mmc_command cmd;
315 int err;
316
317 memset(&cmd, 0, sizeof(struct mmc_command));
318
319 cmd.opcode = MMC_SPI_READ_OCR;
320 cmd.arg = highcap ? (1 << 30) : 0;
321 cmd.flags = MMC_RSP_SPI_R3;
322
323 err = mmc_wait_for_cmd(host, &cmd, 0);
324
325 *ocrp = cmd.resp[1];
326 return err;
327}
328
329int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
330{
331 struct mmc_command cmd;
332 int err;
333
334 memset(&cmd, 0, sizeof(struct mmc_command));
335
336 cmd.opcode = MMC_SPI_CRC_ON_OFF;
337 cmd.flags = MMC_RSP_SPI_R1;
338 cmd.arg = use_crc;
339
340 err = mmc_wait_for_cmd(host, &cmd, 0);
341 if (!err)
342 host->use_spi_crc = use_crc;
343 return err;
344}
345
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100346int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
347{
348 int err;
349 struct mmc_command cmd;
350
351 BUG_ON(!card);
352 BUG_ON(!card->host);
353
354 memset(&cmd, 0, sizeof(struct mmc_command));
355
356 cmd.opcode = MMC_SWITCH;
357 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
358 (index << 16) |
359 (value << 8) |
360 set;
David Brownellaf517152007-08-08 09:11:32 -0700361 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100362
363 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200364 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100365 return err;
366
Pierre Ossman17b04292007-07-22 22:18:46 +0200367 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100368}
369
370int mmc_send_status(struct mmc_card *card, u32 *status)
371{
372 int err;
373 struct mmc_command cmd;
374
375 BUG_ON(!card);
376 BUG_ON(!card->host);
377
378 memset(&cmd, 0, sizeof(struct mmc_command));
379
380 cmd.opcode = MMC_SEND_STATUS;
David Brownellaf517152007-08-08 09:11:32 -0700381 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;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100384
385 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200386 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100387 return err;
388
David Brownellaf517152007-08-08 09:11:32 -0700389 /* NOTE: callers are required to understand the difference
390 * between "native" and SPI format status words!
391 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100392 if (status)
393 *status = cmd.resp[0];
394
Pierre Ossman17b04292007-07-22 22:18:46 +0200395 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100396}
397