blob: 837fc7386e237e620f408fd740ec702ad43c2c8b [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
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010026static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
27{
28 int err;
Chris Ball1278dba2011-04-13 23:40:30 -040029 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010030
31 BUG_ON(!host);
32
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010033 cmd.opcode = MMC_SELECT_CARD;
34
35 if (card) {
36 cmd.arg = card->rca << 16;
37 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
38 } else {
39 cmd.arg = 0;
40 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
41 }
42
43 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +020044 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010045 return err;
46
Pierre Ossman17b04292007-07-22 22:18:46 +020047 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010048}
49
50int mmc_select_card(struct mmc_card *card)
51{
52 BUG_ON(!card);
53
54 return _mmc_select_card(card->host, card);
55}
56
57int mmc_deselect_cards(struct mmc_host *host)
58{
59 return _mmc_select_card(host, NULL);
60}
61
62int mmc_go_idle(struct mmc_host *host)
63{
64 int err;
Chris Ball1278dba2011-04-13 23:40:30 -040065 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010066
David Brownellaf517152007-08-08 09:11:32 -070067 /*
68 * Non-SPI hosts need to prevent chipselect going active during
69 * GO_IDLE; that would put chips into SPI mode. Remind them of
70 * that in case of hardware that won't pull up DAT3/nCS otherwise.
71 *
72 * SPI hosts ignore ios.chip_select; it's managed according to
Lucas De Marchi25985ed2011-03-30 22:57:33 -030073 * rules that must accommodate non-MMC slaves which this layer
David Brownellaf517152007-08-08 09:11:32 -070074 * won't even know about.
75 */
76 if (!mmc_host_is_spi(host)) {
77 mmc_set_chip_select(host, MMC_CS_HIGH);
78 mmc_delay(1);
79 }
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010080
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010081 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{
Chris Ball1278dba2011-04-13 23:40:30 -0400101 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100102 int i, err = 0;
103
104 BUG_ON(!host);
105
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100106 cmd.opcode = MMC_SEND_OP_COND;
David Brownellaf517152007-08-08 09:11:32 -0700107 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
108 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100109
110 for (i = 100; i; i--) {
111 err = mmc_wait_for_cmd(host, &cmd, 0);
Pierre Ossman17b04292007-07-22 22:18:46 +0200112 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100113 break;
114
David Brownellaf517152007-08-08 09:11:32 -0700115 /* if we're just probing, do a single pass */
116 if (ocr == 0)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100117 break;
118
David Brownellaf517152007-08-08 09:11:32 -0700119 /* otherwise wait until reset completes */
120 if (mmc_host_is_spi(host)) {
121 if (!(cmd.resp[0] & R1_SPI_IDLE))
122 break;
123 } else {
124 if (cmd.resp[0] & MMC_CARD_BUSY)
125 break;
126 }
127
Pierre Ossman17b04292007-07-22 22:18:46 +0200128 err = -ETIMEDOUT;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100129
130 mmc_delay(10);
131 }
132
David Brownellaf517152007-08-08 09:11:32 -0700133 if (rocr && !mmc_host_is_spi(host))
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100134 *rocr = cmd.resp[0];
135
136 return err;
137}
138
139int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
140{
141 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400142 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100143
144 BUG_ON(!host);
145 BUG_ON(!cid);
146
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100147 cmd.opcode = MMC_ALL_SEND_CID;
148 cmd.arg = 0;
149 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
150
151 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200152 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100153 return err;
154
155 memcpy(cid, cmd.resp, sizeof(u32) * 4);
156
Pierre Ossman17b04292007-07-22 22:18:46 +0200157 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100158}
159
160int mmc_set_relative_addr(struct mmc_card *card)
161{
162 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400163 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100164
165 BUG_ON(!card);
166 BUG_ON(!card->host);
167
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100168 cmd.opcode = MMC_SET_RELATIVE_ADDR;
169 cmd.arg = card->rca << 16;
170 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
171
172 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200173 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100174 return err;
175
Pierre Ossman17b04292007-07-22 22:18:46 +0200176 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100177}
178
David Brownellaf517152007-08-08 09:11:32 -0700179static int
180mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100181{
182 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400183 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100184
David Brownellaf517152007-08-08 09:11:32 -0700185 BUG_ON(!host);
186 BUG_ON(!cxd);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100187
David Brownellaf517152007-08-08 09:11:32 -0700188 cmd.opcode = opcode;
189 cmd.arg = arg;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100190 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
191
David Brownellaf517152007-08-08 09:11:32 -0700192 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200193 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100194 return err;
195
David Brownellaf517152007-08-08 09:11:32 -0700196 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100197
Pierre Ossman17b04292007-07-22 22:18:46 +0200198 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100199}
200
Kyungsik Lee1a413132012-08-03 00:58:03 +0000201/*
202 * NOTE: void *buf, caller for the buf is required to use DMA-capable
203 * buffer or on-stack buffer (with some overhead in callee).
204 */
David Brownellaf517152007-08-08 09:11:32 -0700205static int
206mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
207 u32 opcode, void *buf, unsigned len)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100208{
Venkatraman Sad5fd972011-08-25 00:30:50 +0530209 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400210 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400211 struct mmc_data data = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100212 struct scatterlist sg;
David Brownellaf517152007-08-08 09:11:32 -0700213 void *data_buf;
Kyungsik Lee1a413132012-08-03 00:58:03 +0000214 int is_on_stack;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100215
Kyungsik Lee1a413132012-08-03 00:58:03 +0000216 is_on_stack = object_is_on_stack(buf);
217 if (is_on_stack) {
218 /*
219 * 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)
224 return -ENOMEM;
225 } else
226 data_buf = buf;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100227
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100228 mrq.cmd = &cmd;
229 mrq.data = &data;
230
David Brownellaf517152007-08-08 09:11:32 -0700231 cmd.opcode = opcode;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100232 cmd.arg = 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100233
David Brownellaf517152007-08-08 09:11:32 -0700234 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
235 * rely on callers to never use this with "native" calls for reading
236 * CSD or CID. Native versions of those commands use the R2 type,
237 * not R1 plus a data block.
238 */
239 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
240
241 data.blksz = len;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100242 data.blocks = 1;
243 data.flags = MMC_DATA_READ;
244 data.sg = &sg;
245 data.sg_len = 1;
246
David Brownellaf517152007-08-08 09:11:32 -0700247 sg_init_one(&sg, data_buf, len);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100248
Adrian Huntercda56ac22009-02-10 16:32:33 +0200249 if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
250 /*
251 * The spec states that CSR and CID accesses have a timeout
252 * of 64 clock cycles.
253 */
254 data.timeout_ns = 0;
255 data.timeout_clks = 64;
256 } else
257 mmc_set_data_timeout(&data, card);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100258
David Brownellaf517152007-08-08 09:11:32 -0700259 mmc_wait_for_req(host, &mrq);
260
Kyungsik Lee1a413132012-08-03 00:58:03 +0000261 if (is_on_stack) {
262 memcpy(buf, data_buf, len);
263 kfree(data_buf);
264 }
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100265
Pierre Ossman17b04292007-07-22 22:18:46 +0200266 if (cmd.error)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100267 return cmd.error;
Pierre Ossman17b04292007-07-22 22:18:46 +0200268 if (data.error)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100269 return data.error;
270
Pierre Ossman17b04292007-07-22 22:18:46 +0200271 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100272}
273
David Brownellaf517152007-08-08 09:11:32 -0700274int mmc_send_csd(struct mmc_card *card, u32 *csd)
275{
Pierre Ossman78e48072007-10-27 14:14:23 +0200276 int ret, i;
Kyungsik Lee1a413132012-08-03 00:58:03 +0000277 u32 *csd_tmp;
Pierre Ossman78e48072007-10-27 14:14:23 +0200278
David Brownellaf517152007-08-08 09:11:32 -0700279 if (!mmc_host_is_spi(card->host))
280 return mmc_send_cxd_native(card->host, card->rca << 16,
281 csd, MMC_SEND_CSD);
282
Kyungsik Lee1a413132012-08-03 00:58:03 +0000283 csd_tmp = kmalloc(16, GFP_KERNEL);
284 if (!csd_tmp)
285 return -ENOMEM;
286
287 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
Pierre Ossman78e48072007-10-27 14:14:23 +0200288 if (ret)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000289 goto err;
Pierre Ossman78e48072007-10-27 14:14:23 +0200290
291 for (i = 0;i < 4;i++)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000292 csd[i] = be32_to_cpu(csd_tmp[i]);
Pierre Ossman78e48072007-10-27 14:14:23 +0200293
Kyungsik Lee1a413132012-08-03 00:58:03 +0000294err:
295 kfree(csd_tmp);
296 return ret;
David Brownellaf517152007-08-08 09:11:32 -0700297}
298
299int mmc_send_cid(struct mmc_host *host, u32 *cid)
300{
Pierre Ossman78e48072007-10-27 14:14:23 +0200301 int ret, i;
Kyungsik Lee1a413132012-08-03 00:58:03 +0000302 u32 *cid_tmp;
Pierre Ossman78e48072007-10-27 14:14:23 +0200303
David Brownellaf517152007-08-08 09:11:32 -0700304 if (!mmc_host_is_spi(host)) {
305 if (!host->card)
306 return -EINVAL;
307 return mmc_send_cxd_native(host, host->card->rca << 16,
308 cid, MMC_SEND_CID);
309 }
310
Kyungsik Lee1a413132012-08-03 00:58:03 +0000311 cid_tmp = kmalloc(16, GFP_KERNEL);
312 if (!cid_tmp)
313 return -ENOMEM;
314
315 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
Pierre Ossman78e48072007-10-27 14:14:23 +0200316 if (ret)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000317 goto err;
Pierre Ossman78e48072007-10-27 14:14:23 +0200318
319 for (i = 0;i < 4;i++)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000320 cid[i] = be32_to_cpu(cid_tmp[i]);
Pierre Ossman78e48072007-10-27 14:14:23 +0200321
Kyungsik Lee1a413132012-08-03 00:58:03 +0000322err:
323 kfree(cid_tmp);
324 return ret;
David Brownellaf517152007-08-08 09:11:32 -0700325}
326
327int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
328{
329 return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
330 ext_csd, 512);
331}
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900332EXPORT_SYMBOL_GPL(mmc_send_ext_csd);
David Brownellaf517152007-08-08 09:11:32 -0700333
334int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
335{
Chris Ball1278dba2011-04-13 23:40:30 -0400336 struct mmc_command cmd = {0};
David Brownellaf517152007-08-08 09:11:32 -0700337 int err;
338
David Brownellaf517152007-08-08 09:11:32 -0700339 cmd.opcode = MMC_SPI_READ_OCR;
340 cmd.arg = highcap ? (1 << 30) : 0;
341 cmd.flags = MMC_RSP_SPI_R3;
342
343 err = mmc_wait_for_cmd(host, &cmd, 0);
344
345 *ocrp = cmd.resp[1];
346 return err;
347}
348
349int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
350{
Chris Ball1278dba2011-04-13 23:40:30 -0400351 struct mmc_command cmd = {0};
David Brownellaf517152007-08-08 09:11:32 -0700352 int err;
353
David Brownellaf517152007-08-08 09:11:32 -0700354 cmd.opcode = MMC_SPI_CRC_ON_OFF;
355 cmd.flags = MMC_RSP_SPI_R1;
356 cmd.arg = use_crc;
357
358 err = mmc_wait_for_cmd(host, &cmd, 0);
359 if (!err)
360 host->use_spi_crc = use_crc;
361 return err;
362}
363
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500364/**
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000365 * __mmc_switch - modify EXT_CSD register
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500366 * @card: the MMC card associated with the data transfer
367 * @set: cmd set values
368 * @index: EXT_CSD register index
369 * @value: value to program into EXT_CSD register
370 * @timeout_ms: timeout (ms) for operation performed by register write,
371 * timeout of zero implies maximum possible timeout
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000372 * @use_busy_signal: use the busy signal as response type
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500373 *
374 * Modifies the EXT_CSD register for selected card.
375 */
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000376int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
377 unsigned int timeout_ms, bool use_busy_signal)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100378{
379 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400380 struct mmc_command cmd = {0};
Trey Ramsay8fee4762012-11-16 09:31:41 -0600381 unsigned long timeout;
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700382 u32 status;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100383
384 BUG_ON(!card);
385 BUG_ON(!card->host);
386
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100387 cmd.opcode = MMC_SWITCH;
388 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
389 (index << 16) |
390 (value << 8) |
391 set;
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000392 cmd.flags = MMC_CMD_AC;
393 if (use_busy_signal)
394 cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
395 else
396 cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
397
398
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500399 cmd.cmd_timeout_ms = timeout_ms;
Maya Erez775a9362013-04-18 15:41:55 +0300400 if (index == EXT_CSD_SANITIZE_START)
401 cmd.sanitize_busy = true;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100402
403 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200404 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100405 return err;
406
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000407 /* No need to check card status in case of unblocking command */
408 if (!use_busy_signal)
409 return 0;
410
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700411 /* Must check status to be sure of no errors */
Trey Ramsay8fee4762012-11-16 09:31:41 -0600412 timeout = jiffies + msecs_to_jiffies(MMC_OPS_TIMEOUT_MS);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700413 do {
414 err = mmc_send_status(card, &status);
415 if (err)
416 return err;
417 if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
418 break;
419 if (mmc_host_is_spi(card->host))
420 break;
Trey Ramsay8fee4762012-11-16 09:31:41 -0600421
422 /* Timeout if the device never leaves the program state. */
423 if (time_after(jiffies, timeout)) {
424 pr_err("%s: Card stuck in programming state! %s\n",
425 mmc_hostname(card->host), __func__);
426 return -ETIMEDOUT;
427 }
Jaehoon Chung7435bb72011-08-10 18:46:28 +0900428 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700429
430 if (mmc_host_is_spi(card->host)) {
431 if (status & R1_SPI_ILLEGAL_COMMAND)
432 return -EBADMSG;
433 } else {
434 if (status & 0xFDFFA000)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530435 pr_warning("%s: unexpected status %#x after "
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700436 "switch", mmc_hostname(card->host), status);
437 if (status & R1_SWITCH_ERROR)
438 return -EBADMSG;
439 }
440
Pierre Ossman17b04292007-07-22 22:18:46 +0200441 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100442}
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000443EXPORT_SYMBOL_GPL(__mmc_switch);
444
445int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
446 unsigned int timeout_ms)
447{
448 return __mmc_switch(card, set, index, value, timeout_ms, true);
449}
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500450EXPORT_SYMBOL_GPL(mmc_switch);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100451
452int mmc_send_status(struct mmc_card *card, u32 *status)
453{
454 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400455 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100456
457 BUG_ON(!card);
458 BUG_ON(!card->host);
459
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100460 cmd.opcode = MMC_SEND_STATUS;
David Brownellaf517152007-08-08 09:11:32 -0700461 if (!mmc_host_is_spi(card->host))
462 cmd.arg = card->rca << 16;
463 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100464
465 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200466 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100467 return err;
468
David Brownellaf517152007-08-08 09:11:32 -0700469 /* NOTE: callers are required to understand the difference
470 * between "native" and SPI format status words!
471 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100472 if (status)
473 *status = cmd.resp[0];
474
Pierre Ossman17b04292007-07-22 22:18:46 +0200475 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100476}
477
Aries Lee22113ef2010-12-15 08:14:24 +0100478static int
479mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
480 u8 len)
481{
Venkatraman Sad5fd972011-08-25 00:30:50 +0530482 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400483 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400484 struct mmc_data data = {0};
Aries Lee22113ef2010-12-15 08:14:24 +0100485 struct scatterlist sg;
486 u8 *data_buf;
487 u8 *test_buf;
488 int i, err;
489 static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
490 static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
491
492 /* dma onto stack is unsafe/nonportable, but callers to this
493 * routine normally provide temporary on-stack buffers ...
494 */
495 data_buf = kmalloc(len, GFP_KERNEL);
496 if (!data_buf)
497 return -ENOMEM;
498
499 if (len == 8)
500 test_buf = testdata_8bit;
501 else if (len == 4)
502 test_buf = testdata_4bit;
503 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530504 pr_err("%s: Invalid bus_width %d\n",
Aries Lee22113ef2010-12-15 08:14:24 +0100505 mmc_hostname(host), len);
506 kfree(data_buf);
507 return -EINVAL;
508 }
509
510 if (opcode == MMC_BUS_TEST_W)
511 memcpy(data_buf, test_buf, len);
512
Aries Lee22113ef2010-12-15 08:14:24 +0100513 mrq.cmd = &cmd;
514 mrq.data = &data;
515 cmd.opcode = opcode;
516 cmd.arg = 0;
517
518 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
519 * rely on callers to never use this with "native" calls for reading
520 * CSD or CID. Native versions of those commands use the R2 type,
521 * not R1 plus a data block.
522 */
523 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
524
525 data.blksz = len;
526 data.blocks = 1;
527 if (opcode == MMC_BUS_TEST_R)
528 data.flags = MMC_DATA_READ;
529 else
530 data.flags = MMC_DATA_WRITE;
531
532 data.sg = &sg;
533 data.sg_len = 1;
534 sg_init_one(&sg, data_buf, len);
535 mmc_wait_for_req(host, &mrq);
536 err = 0;
537 if (opcode == MMC_BUS_TEST_R) {
538 for (i = 0; i < len / 4; i++)
539 if ((test_buf[i] ^ data_buf[i]) != 0xff) {
540 err = -EIO;
541 break;
542 }
543 }
544 kfree(data_buf);
545
546 if (cmd.error)
547 return cmd.error;
548 if (data.error)
549 return data.error;
550
551 return err;
552}
553
554int mmc_bus_test(struct mmc_card *card, u8 bus_width)
555{
556 int err, width;
557
558 if (bus_width == MMC_BUS_WIDTH_8)
559 width = 8;
560 else if (bus_width == MMC_BUS_WIDTH_4)
561 width = 4;
562 else if (bus_width == MMC_BUS_WIDTH_1)
563 return 0; /* no need for test */
564 else
565 return -EINVAL;
566
567 /*
568 * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
569 * is a problem. This improves chances that the test will work.
570 */
571 mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
572 err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
573 return err;
574}
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400575
576int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
577{
578 struct mmc_command cmd = {0};
579 unsigned int opcode;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400580 int err;
581
Jaehoon Chung23789752012-03-12 08:57:27 +0900582 if (!card->ext_csd.hpi) {
583 pr_warning("%s: Card didn't support HPI command\n",
584 mmc_hostname(card->host));
585 return -EINVAL;
586 }
587
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400588 opcode = card->ext_csd.hpi_cmd;
589 if (opcode == MMC_STOP_TRANSMISSION)
Jaehoon Chung23789752012-03-12 08:57:27 +0900590 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400591 else if (opcode == MMC_SEND_STATUS)
Jaehoon Chung23789752012-03-12 08:57:27 +0900592 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400593
594 cmd.opcode = opcode;
595 cmd.arg = card->rca << 16 | 1;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400596
597 err = mmc_wait_for_cmd(card->host, &cmd, 0);
598 if (err) {
599 pr_warn("%s: error %d interrupting operation. "
600 "HPI command response %#x\n", mmc_hostname(card->host),
601 err, cmd.resp[0]);
602 return err;
603 }
604 if (status)
605 *status = cmd.resp[0];
606
607 return 0;
608}