blob: 04ecdb913aa0e04cb204603ba59a2a5993843d8b [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Paul Gortmaker3ef77af2011-07-10 12:42:00 -040013#include <linux/export.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010014#include <linux/types.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010015#include <linux/scatterlist.h>
16
17#include <linux/mmc/host.h>
18#include <linux/mmc/card.h>
19#include <linux/mmc/mmc.h>
20
21#include "core.h"
22#include "mmc_ops.h"
23
Trey Ramsay8fee4762012-11-16 09:31:41 -060024#define MMC_OPS_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
25
Seungwon Jeona27fbf22013-09-04 21:21:05 +090026static inline int __mmc_send_status(struct mmc_card *card, u32 *status,
27 bool ignore_crc)
28{
29 int err;
30 struct mmc_command cmd = {0};
31
32 BUG_ON(!card);
33 BUG_ON(!card->host);
34
35 cmd.opcode = MMC_SEND_STATUS;
36 if (!mmc_host_is_spi(card->host))
37 cmd.arg = card->rca << 16;
38 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
39 if (ignore_crc)
40 cmd.flags &= ~MMC_RSP_CRC;
41
42 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
43 if (err)
44 return err;
45
46 /* NOTE: callers are required to understand the difference
47 * between "native" and SPI format status words!
48 */
49 if (status)
50 *status = cmd.resp[0];
51
52 return 0;
53}
54
55int mmc_send_status(struct mmc_card *card, u32 *status)
56{
57 return __mmc_send_status(card, status, false);
58}
59
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010060static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
61{
62 int err;
Chris Ball1278dba2011-04-13 23:40:30 -040063 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010064
65 BUG_ON(!host);
66
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010067 cmd.opcode = MMC_SELECT_CARD;
68
69 if (card) {
70 cmd.arg = card->rca << 16;
71 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
72 } else {
73 cmd.arg = 0;
74 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
75 }
76
77 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +020078 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010079 return err;
80
Pierre Ossman17b04292007-07-22 22:18:46 +020081 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010082}
83
84int mmc_select_card(struct mmc_card *card)
85{
86 BUG_ON(!card);
87
88 return _mmc_select_card(card->host, card);
89}
90
91int mmc_deselect_cards(struct mmc_host *host)
92{
93 return _mmc_select_card(host, NULL);
94}
95
96int mmc_go_idle(struct mmc_host *host)
97{
98 int err;
Chris Ball1278dba2011-04-13 23:40:30 -040099 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100100
David Brownellaf517152007-08-08 09:11:32 -0700101 /*
102 * Non-SPI hosts need to prevent chipselect going active during
103 * GO_IDLE; that would put chips into SPI mode. Remind them of
104 * that in case of hardware that won't pull up DAT3/nCS otherwise.
105 *
106 * SPI hosts ignore ios.chip_select; it's managed according to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300107 * rules that must accommodate non-MMC slaves which this layer
David Brownellaf517152007-08-08 09:11:32 -0700108 * won't even know about.
109 */
110 if (!mmc_host_is_spi(host)) {
111 mmc_set_chip_select(host, MMC_CS_HIGH);
112 mmc_delay(1);
113 }
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100114
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100115 cmd.opcode = MMC_GO_IDLE_STATE;
116 cmd.arg = 0;
David Brownellaf517152007-08-08 09:11:32 -0700117 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100118
119 err = mmc_wait_for_cmd(host, &cmd, 0);
120
121 mmc_delay(1);
122
David Brownellaf517152007-08-08 09:11:32 -0700123 if (!mmc_host_is_spi(host)) {
124 mmc_set_chip_select(host, MMC_CS_DONTCARE);
125 mmc_delay(1);
126 }
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100127
David Brownellaf517152007-08-08 09:11:32 -0700128 host->use_spi_crc = 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100129
130 return err;
131}
132
133int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
134{
Chris Ball1278dba2011-04-13 23:40:30 -0400135 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100136 int i, err = 0;
137
138 BUG_ON(!host);
139
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100140 cmd.opcode = MMC_SEND_OP_COND;
David Brownellaf517152007-08-08 09:11:32 -0700141 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
142 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100143
144 for (i = 100; i; i--) {
145 err = mmc_wait_for_cmd(host, &cmd, 0);
Pierre Ossman17b04292007-07-22 22:18:46 +0200146 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100147 break;
148
David Brownellaf517152007-08-08 09:11:32 -0700149 /* if we're just probing, do a single pass */
150 if (ocr == 0)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100151 break;
152
David Brownellaf517152007-08-08 09:11:32 -0700153 /* otherwise wait until reset completes */
154 if (mmc_host_is_spi(host)) {
155 if (!(cmd.resp[0] & R1_SPI_IDLE))
156 break;
157 } else {
158 if (cmd.resp[0] & MMC_CARD_BUSY)
159 break;
160 }
161
Pierre Ossman17b04292007-07-22 22:18:46 +0200162 err = -ETIMEDOUT;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100163
164 mmc_delay(10);
165 }
166
David Brownellaf517152007-08-08 09:11:32 -0700167 if (rocr && !mmc_host_is_spi(host))
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100168 *rocr = cmd.resp[0];
169
170 return err;
171}
172
173int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
174{
175 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400176 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100177
178 BUG_ON(!host);
179 BUG_ON(!cid);
180
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100181 cmd.opcode = MMC_ALL_SEND_CID;
182 cmd.arg = 0;
183 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
184
185 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200186 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100187 return err;
188
189 memcpy(cid, cmd.resp, sizeof(u32) * 4);
190
Pierre Ossman17b04292007-07-22 22:18:46 +0200191 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100192}
193
194int mmc_set_relative_addr(struct mmc_card *card)
195{
196 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400197 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100198
199 BUG_ON(!card);
200 BUG_ON(!card->host);
201
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100202 cmd.opcode = MMC_SET_RELATIVE_ADDR;
203 cmd.arg = card->rca << 16;
204 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
205
206 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200207 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100208 return err;
209
Pierre Ossman17b04292007-07-22 22:18:46 +0200210 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100211}
212
David Brownellaf517152007-08-08 09:11:32 -0700213static int
214mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100215{
216 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400217 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100218
David Brownellaf517152007-08-08 09:11:32 -0700219 BUG_ON(!host);
220 BUG_ON(!cxd);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100221
David Brownellaf517152007-08-08 09:11:32 -0700222 cmd.opcode = opcode;
223 cmd.arg = arg;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100224 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
225
David Brownellaf517152007-08-08 09:11:32 -0700226 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200227 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100228 return err;
229
David Brownellaf517152007-08-08 09:11:32 -0700230 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100231
Pierre Ossman17b04292007-07-22 22:18:46 +0200232 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100233}
234
Kyungsik Lee1a413132012-08-03 00:58:03 +0000235/*
236 * NOTE: void *buf, caller for the buf is required to use DMA-capable
237 * buffer or on-stack buffer (with some overhead in callee).
238 */
David Brownellaf517152007-08-08 09:11:32 -0700239static int
240mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
241 u32 opcode, void *buf, unsigned len)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100242{
Venkatraman Sad5fd972011-08-25 00:30:50 +0530243 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400244 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400245 struct mmc_data data = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100246 struct scatterlist sg;
David Brownellaf517152007-08-08 09:11:32 -0700247 void *data_buf;
Kyungsik Lee1a413132012-08-03 00:58:03 +0000248 int is_on_stack;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100249
Kyungsik Lee1a413132012-08-03 00:58:03 +0000250 is_on_stack = object_is_on_stack(buf);
251 if (is_on_stack) {
252 /*
253 * dma onto stack is unsafe/nonportable, but callers to this
254 * routine normally provide temporary on-stack buffers ...
255 */
256 data_buf = kmalloc(len, GFP_KERNEL);
257 if (!data_buf)
258 return -ENOMEM;
259 } else
260 data_buf = buf;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100261
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100262 mrq.cmd = &cmd;
263 mrq.data = &data;
264
David Brownellaf517152007-08-08 09:11:32 -0700265 cmd.opcode = opcode;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100266 cmd.arg = 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100267
David Brownellaf517152007-08-08 09:11:32 -0700268 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
269 * rely on callers to never use this with "native" calls for reading
270 * CSD or CID. Native versions of those commands use the R2 type,
271 * not R1 plus a data block.
272 */
273 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
274
275 data.blksz = len;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100276 data.blocks = 1;
277 data.flags = MMC_DATA_READ;
278 data.sg = &sg;
279 data.sg_len = 1;
280
David Brownellaf517152007-08-08 09:11:32 -0700281 sg_init_one(&sg, data_buf, len);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100282
Adrian Huntercda56ac22009-02-10 16:32:33 +0200283 if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
284 /*
285 * The spec states that CSR and CID accesses have a timeout
286 * of 64 clock cycles.
287 */
288 data.timeout_ns = 0;
289 data.timeout_clks = 64;
290 } else
291 mmc_set_data_timeout(&data, card);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100292
David Brownellaf517152007-08-08 09:11:32 -0700293 mmc_wait_for_req(host, &mrq);
294
Kyungsik Lee1a413132012-08-03 00:58:03 +0000295 if (is_on_stack) {
296 memcpy(buf, data_buf, len);
297 kfree(data_buf);
298 }
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100299
Pierre Ossman17b04292007-07-22 22:18:46 +0200300 if (cmd.error)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100301 return cmd.error;
Pierre Ossman17b04292007-07-22 22:18:46 +0200302 if (data.error)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100303 return data.error;
304
Pierre Ossman17b04292007-07-22 22:18:46 +0200305 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100306}
307
David Brownellaf517152007-08-08 09:11:32 -0700308int mmc_send_csd(struct mmc_card *card, u32 *csd)
309{
Pierre Ossman78e48072007-10-27 14:14:23 +0200310 int ret, i;
Kyungsik Lee1a413132012-08-03 00:58:03 +0000311 u32 *csd_tmp;
Pierre Ossman78e48072007-10-27 14:14:23 +0200312
David Brownellaf517152007-08-08 09:11:32 -0700313 if (!mmc_host_is_spi(card->host))
314 return mmc_send_cxd_native(card->host, card->rca << 16,
315 csd, MMC_SEND_CSD);
316
Kyungsik Lee1a413132012-08-03 00:58:03 +0000317 csd_tmp = kmalloc(16, GFP_KERNEL);
318 if (!csd_tmp)
319 return -ENOMEM;
320
321 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
Pierre Ossman78e48072007-10-27 14:14:23 +0200322 if (ret)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000323 goto err;
Pierre Ossman78e48072007-10-27 14:14:23 +0200324
325 for (i = 0;i < 4;i++)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000326 csd[i] = be32_to_cpu(csd_tmp[i]);
Pierre Ossman78e48072007-10-27 14:14:23 +0200327
Kyungsik Lee1a413132012-08-03 00:58:03 +0000328err:
329 kfree(csd_tmp);
330 return ret;
David Brownellaf517152007-08-08 09:11:32 -0700331}
332
333int mmc_send_cid(struct mmc_host *host, u32 *cid)
334{
Pierre Ossman78e48072007-10-27 14:14:23 +0200335 int ret, i;
Kyungsik Lee1a413132012-08-03 00:58:03 +0000336 u32 *cid_tmp;
Pierre Ossman78e48072007-10-27 14:14:23 +0200337
David Brownellaf517152007-08-08 09:11:32 -0700338 if (!mmc_host_is_spi(host)) {
339 if (!host->card)
340 return -EINVAL;
341 return mmc_send_cxd_native(host, host->card->rca << 16,
342 cid, MMC_SEND_CID);
343 }
344
Kyungsik Lee1a413132012-08-03 00:58:03 +0000345 cid_tmp = kmalloc(16, GFP_KERNEL);
346 if (!cid_tmp)
347 return -ENOMEM;
348
349 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
Pierre Ossman78e48072007-10-27 14:14:23 +0200350 if (ret)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000351 goto err;
Pierre Ossman78e48072007-10-27 14:14:23 +0200352
353 for (i = 0;i < 4;i++)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000354 cid[i] = be32_to_cpu(cid_tmp[i]);
Pierre Ossman78e48072007-10-27 14:14:23 +0200355
Kyungsik Lee1a413132012-08-03 00:58:03 +0000356err:
357 kfree(cid_tmp);
358 return ret;
David Brownellaf517152007-08-08 09:11:32 -0700359}
360
361int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
362{
363 return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
364 ext_csd, 512);
365}
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900366EXPORT_SYMBOL_GPL(mmc_send_ext_csd);
David Brownellaf517152007-08-08 09:11:32 -0700367
368int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
369{
Chris Ball1278dba2011-04-13 23:40:30 -0400370 struct mmc_command cmd = {0};
David Brownellaf517152007-08-08 09:11:32 -0700371 int err;
372
David Brownellaf517152007-08-08 09:11:32 -0700373 cmd.opcode = MMC_SPI_READ_OCR;
374 cmd.arg = highcap ? (1 << 30) : 0;
375 cmd.flags = MMC_RSP_SPI_R3;
376
377 err = mmc_wait_for_cmd(host, &cmd, 0);
378
379 *ocrp = cmd.resp[1];
380 return err;
381}
382
383int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
384{
Chris Ball1278dba2011-04-13 23:40:30 -0400385 struct mmc_command cmd = {0};
David Brownellaf517152007-08-08 09:11:32 -0700386 int err;
387
David Brownellaf517152007-08-08 09:11:32 -0700388 cmd.opcode = MMC_SPI_CRC_ON_OFF;
389 cmd.flags = MMC_RSP_SPI_R1;
390 cmd.arg = use_crc;
391
392 err = mmc_wait_for_cmd(host, &cmd, 0);
393 if (!err)
394 host->use_spi_crc = use_crc;
395 return err;
396}
397
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500398/**
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000399 * __mmc_switch - modify EXT_CSD register
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500400 * @card: the MMC card associated with the data transfer
401 * @set: cmd set values
402 * @index: EXT_CSD register index
403 * @value: value to program into EXT_CSD register
404 * @timeout_ms: timeout (ms) for operation performed by register write,
405 * timeout of zero implies maximum possible timeout
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000406 * @use_busy_signal: use the busy signal as response type
Ulf Hansson878e2002013-09-09 11:57:57 +0200407 * @send_status: send status cmd to poll for busy
Ulf Hansson4509f84772014-01-08 16:09:33 +0100408 * @ignore_crc: ignore CRC errors when sending status cmd to poll for busy
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500409 *
410 * Modifies the EXT_CSD register for selected card.
411 */
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000412int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
Ulf Hansson4509f84772014-01-08 16:09:33 +0100413 unsigned int timeout_ms, bool use_busy_signal, bool send_status,
414 bool ignore_crc)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100415{
Ulf Hansson636bd132014-01-28 14:05:39 +0100416 struct mmc_host *host = card->host;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100417 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400418 struct mmc_command cmd = {0};
Trey Ramsay8fee4762012-11-16 09:31:41 -0600419 unsigned long timeout;
Ulf Hanssonecd3a7d2013-10-28 02:51:41 +0100420 u32 status = 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100421
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100422 cmd.opcode = MMC_SWITCH;
423 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
424 (index << 16) |
425 (value << 8) |
426 set;
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000427 cmd.flags = MMC_CMD_AC;
428 if (use_busy_signal)
429 cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
430 else
431 cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
432
433
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100434 cmd.busy_timeout = timeout_ms;
Maya Erez775a9362013-04-18 15:41:55 +0300435 if (index == EXT_CSD_SANITIZE_START)
436 cmd.sanitize_busy = true;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100437
Ulf Hansson636bd132014-01-28 14:05:39 +0100438 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200439 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100440 return err;
441
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000442 /* No need to check card status in case of unblocking command */
443 if (!use_busy_signal)
444 return 0;
445
Seungwon Jeona27fbf22013-09-04 21:21:05 +0900446 /*
Ulf Hansson4509f84772014-01-08 16:09:33 +0100447 * CRC errors shall only be ignored in cases were CMD13 is used to poll
448 * to detect busy completion.
Seungwon Jeona27fbf22013-09-04 21:21:05 +0900449 */
Ulf Hansson636bd132014-01-28 14:05:39 +0100450 if (host->caps & MMC_CAP_WAIT_WHILE_BUSY)
Ulf Hansson4509f84772014-01-08 16:09:33 +0100451 ignore_crc = false;
Seungwon Jeona27fbf22013-09-04 21:21:05 +0900452
Ulf Hansson4509f84772014-01-08 16:09:33 +0100453 /* Must check status to be sure of no errors. */
Trey Ramsay8fee4762012-11-16 09:31:41 -0600454 timeout = jiffies + msecs_to_jiffies(MMC_OPS_TIMEOUT_MS);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700455 do {
Ulf Hansson878e2002013-09-09 11:57:57 +0200456 if (send_status) {
457 err = __mmc_send_status(card, &status, ignore_crc);
458 if (err)
459 return err;
460 }
Ulf Hansson636bd132014-01-28 14:05:39 +0100461 if (host->caps & MMC_CAP_WAIT_WHILE_BUSY)
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700462 break;
Ulf Hansson636bd132014-01-28 14:05:39 +0100463 if (mmc_host_is_spi(host))
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700464 break;
Trey Ramsay8fee4762012-11-16 09:31:41 -0600465
Ulf Hansson878e2002013-09-09 11:57:57 +0200466 /*
467 * We are not allowed to issue a status command and the host
468 * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only
469 * rely on waiting for the stated timeout to be sufficient.
470 */
471 if (!send_status) {
472 mmc_delay(timeout_ms);
473 return 0;
474 }
475
Trey Ramsay8fee4762012-11-16 09:31:41 -0600476 /* Timeout if the device never leaves the program state. */
477 if (time_after(jiffies, timeout)) {
478 pr_err("%s: Card stuck in programming state! %s\n",
Ulf Hansson636bd132014-01-28 14:05:39 +0100479 mmc_hostname(host), __func__);
Trey Ramsay8fee4762012-11-16 09:31:41 -0600480 return -ETIMEDOUT;
481 }
Jaehoon Chung7435bb72011-08-10 18:46:28 +0900482 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700483
Ulf Hansson636bd132014-01-28 14:05:39 +0100484 if (mmc_host_is_spi(host)) {
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700485 if (status & R1_SPI_ILLEGAL_COMMAND)
486 return -EBADMSG;
487 } else {
488 if (status & 0xFDFFA000)
Ulf Hansson636bd132014-01-28 14:05:39 +0100489 pr_warn("%s: unexpected status %#x after switch\n",
490 mmc_hostname(host), status);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700491 if (status & R1_SWITCH_ERROR)
492 return -EBADMSG;
493 }
494
Pierre Ossman17b04292007-07-22 22:18:46 +0200495 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100496}
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000497EXPORT_SYMBOL_GPL(__mmc_switch);
498
499int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
500 unsigned int timeout_ms)
501{
Ulf Hansson4509f84772014-01-08 16:09:33 +0100502 return __mmc_switch(card, set, index, value, timeout_ms, true, true,
503 false);
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000504}
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500505EXPORT_SYMBOL_GPL(mmc_switch);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100506
Aries Lee22113ef2010-12-15 08:14:24 +0100507static int
508mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
509 u8 len)
510{
Venkatraman Sad5fd972011-08-25 00:30:50 +0530511 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400512 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400513 struct mmc_data data = {0};
Aries Lee22113ef2010-12-15 08:14:24 +0100514 struct scatterlist sg;
515 u8 *data_buf;
516 u8 *test_buf;
517 int i, err;
518 static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
519 static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
520
521 /* dma onto stack is unsafe/nonportable, but callers to this
522 * routine normally provide temporary on-stack buffers ...
523 */
524 data_buf = kmalloc(len, GFP_KERNEL);
525 if (!data_buf)
526 return -ENOMEM;
527
528 if (len == 8)
529 test_buf = testdata_8bit;
530 else if (len == 4)
531 test_buf = testdata_4bit;
532 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530533 pr_err("%s: Invalid bus_width %d\n",
Aries Lee22113ef2010-12-15 08:14:24 +0100534 mmc_hostname(host), len);
535 kfree(data_buf);
536 return -EINVAL;
537 }
538
539 if (opcode == MMC_BUS_TEST_W)
540 memcpy(data_buf, test_buf, len);
541
Aries Lee22113ef2010-12-15 08:14:24 +0100542 mrq.cmd = &cmd;
543 mrq.data = &data;
544 cmd.opcode = opcode;
545 cmd.arg = 0;
546
547 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
548 * rely on callers to never use this with "native" calls for reading
549 * CSD or CID. Native versions of those commands use the R2 type,
550 * not R1 plus a data block.
551 */
552 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
553
554 data.blksz = len;
555 data.blocks = 1;
556 if (opcode == MMC_BUS_TEST_R)
557 data.flags = MMC_DATA_READ;
558 else
559 data.flags = MMC_DATA_WRITE;
560
561 data.sg = &sg;
562 data.sg_len = 1;
Minjian Wu84532e32013-08-24 23:25:12 -0400563 mmc_set_data_timeout(&data, card);
Aries Lee22113ef2010-12-15 08:14:24 +0100564 sg_init_one(&sg, data_buf, len);
565 mmc_wait_for_req(host, &mrq);
566 err = 0;
567 if (opcode == MMC_BUS_TEST_R) {
568 for (i = 0; i < len / 4; i++)
569 if ((test_buf[i] ^ data_buf[i]) != 0xff) {
570 err = -EIO;
571 break;
572 }
573 }
574 kfree(data_buf);
575
576 if (cmd.error)
577 return cmd.error;
578 if (data.error)
579 return data.error;
580
581 return err;
582}
583
584int mmc_bus_test(struct mmc_card *card, u8 bus_width)
585{
586 int err, width;
587
588 if (bus_width == MMC_BUS_WIDTH_8)
589 width = 8;
590 else if (bus_width == MMC_BUS_WIDTH_4)
591 width = 4;
592 else if (bus_width == MMC_BUS_WIDTH_1)
593 return 0; /* no need for test */
594 else
595 return -EINVAL;
596
597 /*
598 * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
599 * is a problem. This improves chances that the test will work.
600 */
601 mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
602 err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
603 return err;
604}
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400605
606int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
607{
608 struct mmc_command cmd = {0};
609 unsigned int opcode;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400610 int err;
611
Jaehoon Chung23789752012-03-12 08:57:27 +0900612 if (!card->ext_csd.hpi) {
613 pr_warning("%s: Card didn't support HPI command\n",
614 mmc_hostname(card->host));
615 return -EINVAL;
616 }
617
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400618 opcode = card->ext_csd.hpi_cmd;
619 if (opcode == MMC_STOP_TRANSMISSION)
Jaehoon Chung23789752012-03-12 08:57:27 +0900620 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400621 else if (opcode == MMC_SEND_STATUS)
Jaehoon Chung23789752012-03-12 08:57:27 +0900622 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400623
624 cmd.opcode = opcode;
625 cmd.arg = card->rca << 16 | 1;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400626
627 err = mmc_wait_for_cmd(card->host, &cmd, 0);
628 if (err) {
629 pr_warn("%s: error %d interrupting operation. "
630 "HPI command response %#x\n", mmc_hostname(card->host),
631 err, cmd.resp[0]);
632 return err;
633 }
634 if (status)
635 *status = cmd.resp[0];
636
637 return 0;
638}