mmc: core: Hold re-tuning during switch commands
[deliverable/linux.git] / drivers / mmc / core / mmc_ops.c
CommitLineData
da7fbe58 1/*
70f10482 2 * linux/drivers/mmc/core/mmc_ops.h
da7fbe58
PO
3 *
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
5a0e3ad6 12#include <linux/slab.h>
3ef77af1 13#include <linux/export.h>
da7fbe58 14#include <linux/types.h>
da7fbe58
PO
15#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"
c6dbab9c 22#include "host.h"
da7fbe58
PO
23#include "mmc_ops.h"
24
8fee476b
TR
25#define MMC_OPS_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
26
04cdbbfa
UH
27static const u8 tuning_blk_pattern_4bit[] = {
28 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
29 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
30 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
31 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
32 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
33 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
34 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
35 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
36};
37
38static const u8 tuning_blk_pattern_8bit[] = {
39 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
40 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
41 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
42 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
43 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
44 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
45 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
46 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
47 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
48 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
49 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
50 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
51 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
52 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
53 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
54 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
55};
56
a27fbf2f
SJ
57static inline int __mmc_send_status(struct mmc_card *card, u32 *status,
58 bool ignore_crc)
59{
60 int err;
61 struct mmc_command cmd = {0};
62
63 BUG_ON(!card);
64 BUG_ON(!card->host);
65
66 cmd.opcode = MMC_SEND_STATUS;
67 if (!mmc_host_is_spi(card->host))
68 cmd.arg = card->rca << 16;
69 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
70 if (ignore_crc)
71 cmd.flags &= ~MMC_RSP_CRC;
72
73 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
74 if (err)
75 return err;
76
77 /* NOTE: callers are required to understand the difference
78 * between "native" and SPI format status words!
79 */
80 if (status)
81 *status = cmd.resp[0];
82
83 return 0;
84}
85
86int mmc_send_status(struct mmc_card *card, u32 *status)
87{
88 return __mmc_send_status(card, status, false);
89}
90
da7fbe58
PO
91static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
92{
93 int err;
1278dba1 94 struct mmc_command cmd = {0};
da7fbe58
PO
95
96 BUG_ON(!host);
97
da7fbe58
PO
98 cmd.opcode = MMC_SELECT_CARD;
99
100 if (card) {
101 cmd.arg = card->rca << 16;
102 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
103 } else {
104 cmd.arg = 0;
105 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
106 }
107
108 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
17b0429d 109 if (err)
da7fbe58
PO
110 return err;
111
17b0429d 112 return 0;
da7fbe58
PO
113}
114
115int mmc_select_card(struct mmc_card *card)
116{
117 BUG_ON(!card);
118
119 return _mmc_select_card(card->host, card);
120}
121
122int mmc_deselect_cards(struct mmc_host *host)
123{
124 return _mmc_select_card(host, NULL);
125}
126
3d705d14
SH
127/*
128 * Write the value specified in the device tree or board code into the optional
129 * 16 bit Driver Stage Register. This can be used to tune raise/fall times and
130 * drive strength of the DAT and CMD outputs. The actual meaning of a given
131 * value is hardware dependant.
132 * The presence of the DSR register can be determined from the CSD register,
133 * bit 76.
134 */
135int mmc_set_dsr(struct mmc_host *host)
136{
137 struct mmc_command cmd = {0};
138
139 cmd.opcode = MMC_SET_DSR;
140
141 cmd.arg = (host->dsr << 16) | 0xffff;
142 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
143
144 return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
145}
146
da7fbe58
PO
147int mmc_go_idle(struct mmc_host *host)
148{
149 int err;
1278dba1 150 struct mmc_command cmd = {0};
da7fbe58 151
af517150
DB
152 /*
153 * Non-SPI hosts need to prevent chipselect going active during
154 * GO_IDLE; that would put chips into SPI mode. Remind them of
155 * that in case of hardware that won't pull up DAT3/nCS otherwise.
156 *
157 * SPI hosts ignore ios.chip_select; it's managed according to
25985edc 158 * rules that must accommodate non-MMC slaves which this layer
af517150
DB
159 * won't even know about.
160 */
161 if (!mmc_host_is_spi(host)) {
162 mmc_set_chip_select(host, MMC_CS_HIGH);
163 mmc_delay(1);
164 }
da7fbe58 165
da7fbe58
PO
166 cmd.opcode = MMC_GO_IDLE_STATE;
167 cmd.arg = 0;
af517150 168 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
da7fbe58
PO
169
170 err = mmc_wait_for_cmd(host, &cmd, 0);
171
172 mmc_delay(1);
173
af517150
DB
174 if (!mmc_host_is_spi(host)) {
175 mmc_set_chip_select(host, MMC_CS_DONTCARE);
176 mmc_delay(1);
177 }
da7fbe58 178
af517150 179 host->use_spi_crc = 0;
da7fbe58
PO
180
181 return err;
182}
183
184int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
185{
1278dba1 186 struct mmc_command cmd = {0};
da7fbe58
PO
187 int i, err = 0;
188
189 BUG_ON(!host);
190
da7fbe58 191 cmd.opcode = MMC_SEND_OP_COND;
af517150
DB
192 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
193 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
da7fbe58
PO
194
195 for (i = 100; i; i--) {
196 err = mmc_wait_for_cmd(host, &cmd, 0);
17b0429d 197 if (err)
da7fbe58
PO
198 break;
199
af517150
DB
200 /* if we're just probing, do a single pass */
201 if (ocr == 0)
da7fbe58
PO
202 break;
203
af517150
DB
204 /* otherwise wait until reset completes */
205 if (mmc_host_is_spi(host)) {
206 if (!(cmd.resp[0] & R1_SPI_IDLE))
207 break;
208 } else {
209 if (cmd.resp[0] & MMC_CARD_BUSY)
210 break;
211 }
212
17b0429d 213 err = -ETIMEDOUT;
da7fbe58
PO
214
215 mmc_delay(10);
216 }
217
af517150 218 if (rocr && !mmc_host_is_spi(host))
da7fbe58
PO
219 *rocr = cmd.resp[0];
220
221 return err;
222}
223
224int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
225{
226 int err;
1278dba1 227 struct mmc_command cmd = {0};
da7fbe58
PO
228
229 BUG_ON(!host);
230 BUG_ON(!cid);
231
da7fbe58
PO
232 cmd.opcode = MMC_ALL_SEND_CID;
233 cmd.arg = 0;
234 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
235
236 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
17b0429d 237 if (err)
da7fbe58
PO
238 return err;
239
240 memcpy(cid, cmd.resp, sizeof(u32) * 4);
241
17b0429d 242 return 0;
da7fbe58
PO
243}
244
245int mmc_set_relative_addr(struct mmc_card *card)
246{
247 int err;
1278dba1 248 struct mmc_command cmd = {0};
da7fbe58
PO
249
250 BUG_ON(!card);
251 BUG_ON(!card->host);
252
da7fbe58
PO
253 cmd.opcode = MMC_SET_RELATIVE_ADDR;
254 cmd.arg = card->rca << 16;
255 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
256
257 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
17b0429d 258 if (err)
da7fbe58
PO
259 return err;
260
17b0429d 261 return 0;
da7fbe58
PO
262}
263
af517150
DB
264static int
265mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
da7fbe58
PO
266{
267 int err;
1278dba1 268 struct mmc_command cmd = {0};
da7fbe58 269
af517150
DB
270 BUG_ON(!host);
271 BUG_ON(!cxd);
da7fbe58 272
af517150
DB
273 cmd.opcode = opcode;
274 cmd.arg = arg;
da7fbe58
PO
275 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
276
af517150 277 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
17b0429d 278 if (err)
da7fbe58
PO
279 return err;
280
af517150 281 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
da7fbe58 282
17b0429d 283 return 0;
da7fbe58
PO
284}
285
1a41313e
KL
286/*
287 * NOTE: void *buf, caller for the buf is required to use DMA-capable
288 * buffer or on-stack buffer (with some overhead in callee).
289 */
af517150
DB
290static int
291mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
292 u32 opcode, void *buf, unsigned len)
da7fbe58 293{
ad5fd972 294 struct mmc_request mrq = {NULL};
1278dba1 295 struct mmc_command cmd = {0};
a61ad2b4 296 struct mmc_data data = {0};
da7fbe58 297 struct scatterlist sg;
da7fbe58 298
da7fbe58
PO
299 mrq.cmd = &cmd;
300 mrq.data = &data;
301
af517150 302 cmd.opcode = opcode;
da7fbe58 303 cmd.arg = 0;
da7fbe58 304
af517150
DB
305 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
306 * rely on callers to never use this with "native" calls for reading
307 * CSD or CID. Native versions of those commands use the R2 type,
308 * not R1 plus a data block.
309 */
310 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
311
312 data.blksz = len;
da7fbe58
PO
313 data.blocks = 1;
314 data.flags = MMC_DATA_READ;
315 data.sg = &sg;
316 data.sg_len = 1;
317
601ed60c 318 sg_init_one(&sg, buf, len);
da7fbe58 319
cda56ac2
AH
320 if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
321 /*
322 * The spec states that CSR and CID accesses have a timeout
323 * of 64 clock cycles.
324 */
325 data.timeout_ns = 0;
326 data.timeout_clks = 64;
327 } else
328 mmc_set_data_timeout(&data, card);
da7fbe58 329
af517150
DB
330 mmc_wait_for_req(host, &mrq);
331
17b0429d 332 if (cmd.error)
da7fbe58 333 return cmd.error;
17b0429d 334 if (data.error)
da7fbe58
PO
335 return data.error;
336
17b0429d 337 return 0;
da7fbe58
PO
338}
339
af517150
DB
340int mmc_send_csd(struct mmc_card *card, u32 *csd)
341{
78e48073 342 int ret, i;
1a41313e 343 u32 *csd_tmp;
78e48073 344
af517150
DB
345 if (!mmc_host_is_spi(card->host))
346 return mmc_send_cxd_native(card->host, card->rca << 16,
347 csd, MMC_SEND_CSD);
348
22b78700 349 csd_tmp = kzalloc(16, GFP_KERNEL);
1a41313e
KL
350 if (!csd_tmp)
351 return -ENOMEM;
352
353 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
78e48073 354 if (ret)
1a41313e 355 goto err;
78e48073
PO
356
357 for (i = 0;i < 4;i++)
1a41313e 358 csd[i] = be32_to_cpu(csd_tmp[i]);
78e48073 359
1a41313e
KL
360err:
361 kfree(csd_tmp);
362 return ret;
af517150
DB
363}
364
365int mmc_send_cid(struct mmc_host *host, u32 *cid)
366{
78e48073 367 int ret, i;
1a41313e 368 u32 *cid_tmp;
78e48073 369
af517150
DB
370 if (!mmc_host_is_spi(host)) {
371 if (!host->card)
372 return -EINVAL;
373 return mmc_send_cxd_native(host, host->card->rca << 16,
374 cid, MMC_SEND_CID);
375 }
376
22b78700 377 cid_tmp = kzalloc(16, GFP_KERNEL);
1a41313e
KL
378 if (!cid_tmp)
379 return -ENOMEM;
380
381 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
78e48073 382 if (ret)
1a41313e 383 goto err;
78e48073
PO
384
385 for (i = 0;i < 4;i++)
1a41313e 386 cid[i] = be32_to_cpu(cid_tmp[i]);
78e48073 387
1a41313e
KL
388err:
389 kfree(cid_tmp);
390 return ret;
af517150
DB
391}
392
e21aa519
UH
393int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
394{
395 int err;
396 u8 *ext_csd;
397
398 if (!card || !new_ext_csd)
399 return -EINVAL;
400
401 if (!mmc_can_ext_csd(card))
402 return -EOPNOTSUPP;
403
404 /*
405 * As the ext_csd is so large and mostly unused, we don't store the
406 * raw block in mmc_card.
407 */
22b78700 408 ext_csd = kzalloc(512, GFP_KERNEL);
e21aa519
UH
409 if (!ext_csd)
410 return -ENOMEM;
411
2fc91e8b
UH
412 err = mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, ext_csd,
413 512);
e21aa519
UH
414 if (err)
415 kfree(ext_csd);
416 else
417 *new_ext_csd = ext_csd;
418
419 return err;
420}
421EXPORT_SYMBOL_GPL(mmc_get_ext_csd);
422
af517150
DB
423int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
424{
1278dba1 425 struct mmc_command cmd = {0};
af517150
DB
426 int err;
427
af517150
DB
428 cmd.opcode = MMC_SPI_READ_OCR;
429 cmd.arg = highcap ? (1 << 30) : 0;
430 cmd.flags = MMC_RSP_SPI_R3;
431
432 err = mmc_wait_for_cmd(host, &cmd, 0);
433
434 *ocrp = cmd.resp[1];
435 return err;
436}
437
438int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
439{
1278dba1 440 struct mmc_command cmd = {0};
af517150
DB
441 int err;
442
af517150
DB
443 cmd.opcode = MMC_SPI_CRC_ON_OFF;
444 cmd.flags = MMC_RSP_SPI_R1;
445 cmd.arg = use_crc;
446
447 err = mmc_wait_for_cmd(host, &cmd, 0);
448 if (!err)
449 host->use_spi_crc = use_crc;
450 return err;
451}
452
d3a8d95d 453/**
950d56ac 454 * __mmc_switch - modify EXT_CSD register
d3a8d95d
AW
455 * @card: the MMC card associated with the data transfer
456 * @set: cmd set values
457 * @index: EXT_CSD register index
458 * @value: value to program into EXT_CSD register
459 * @timeout_ms: timeout (ms) for operation performed by register write,
460 * timeout of zero implies maximum possible timeout
950d56ac 461 * @use_busy_signal: use the busy signal as response type
878e200b 462 * @send_status: send status cmd to poll for busy
4509f847 463 * @ignore_crc: ignore CRC errors when sending status cmd to poll for busy
d3a8d95d
AW
464 *
465 * Modifies the EXT_CSD register for selected card.
466 */
950d56ac 467int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
4509f847
UH
468 unsigned int timeout_ms, bool use_busy_signal, bool send_status,
469 bool ignore_crc)
da7fbe58 470{
636bd13c 471 struct mmc_host *host = card->host;
da7fbe58 472 int err;
1278dba1 473 struct mmc_command cmd = {0};
8fee476b 474 unsigned long timeout;
ecd3a7da 475 u32 status = 0;
b9ec2616
UH
476 bool use_r1b_resp = use_busy_signal;
477
c6dbab9c
AH
478 mmc_retune_hold(host);
479
b9ec2616
UH
480 /*
481 * If the cmd timeout and the max_busy_timeout of the host are both
482 * specified, let's validate them. A failure means we need to prevent
483 * the host from doing hw busy detection, which is done by converting
484 * to a R1 response instead of a R1B.
485 */
486 if (timeout_ms && host->max_busy_timeout &&
487 (timeout_ms > host->max_busy_timeout))
488 use_r1b_resp = false;
da7fbe58 489
da7fbe58
PO
490 cmd.opcode = MMC_SWITCH;
491 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
492 (index << 16) |
493 (value << 8) |
494 set;
950d56ac 495 cmd.flags = MMC_CMD_AC;
b9ec2616 496 if (use_r1b_resp) {
950d56ac 497 cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
b9ec2616
UH
498 /*
499 * A busy_timeout of zero means the host can decide to use
500 * whatever value it finds suitable.
501 */
502 cmd.busy_timeout = timeout_ms;
503 } else {
950d56ac 504 cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
b9ec2616 505 }
950d56ac 506
775a9362
ME
507 if (index == EXT_CSD_SANITIZE_START)
508 cmd.sanitize_busy = true;
da7fbe58 509
636bd13c 510 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
17b0429d 511 if (err)
c6dbab9c 512 goto out;
da7fbe58 513
950d56ac
JC
514 /* No need to check card status in case of unblocking command */
515 if (!use_busy_signal)
c6dbab9c 516 goto out;
950d56ac 517
a27fbf2f 518 /*
4509f847
UH
519 * CRC errors shall only be ignored in cases were CMD13 is used to poll
520 * to detect busy completion.
a27fbf2f 521 */
b9ec2616 522 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
4509f847 523 ignore_crc = false;
a27fbf2f 524
b9ec2616
UH
525 /* We have an unspecified cmd timeout, use the fallback value. */
526 if (!timeout_ms)
527 timeout_ms = MMC_OPS_TIMEOUT_MS;
528
4509f847 529 /* Must check status to be sure of no errors. */
b9ec2616 530 timeout = jiffies + msecs_to_jiffies(timeout_ms);
ef0b27d4 531 do {
878e200b
UH
532 if (send_status) {
533 err = __mmc_send_status(card, &status, ignore_crc);
534 if (err)
c6dbab9c 535 goto out;
878e200b 536 }
b9ec2616 537 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
ef0b27d4 538 break;
636bd13c 539 if (mmc_host_is_spi(host))
ef0b27d4 540 break;
8fee476b 541
878e200b
UH
542 /*
543 * We are not allowed to issue a status command and the host
544 * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only
545 * rely on waiting for the stated timeout to be sufficient.
546 */
547 if (!send_status) {
548 mmc_delay(timeout_ms);
c6dbab9c 549 goto out;
878e200b
UH
550 }
551
8fee476b
TR
552 /* Timeout if the device never leaves the program state. */
553 if (time_after(jiffies, timeout)) {
554 pr_err("%s: Card stuck in programming state! %s\n",
636bd13c 555 mmc_hostname(host), __func__);
c6dbab9c
AH
556 err = -ETIMEDOUT;
557 goto out;
8fee476b 558 }
7435bb79 559 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
ef0b27d4 560
636bd13c 561 if (mmc_host_is_spi(host)) {
c6dbab9c
AH
562 if (status & R1_SPI_ILLEGAL_COMMAND) {
563 err = -EBADMSG;
564 goto out;
565 }
ef0b27d4
AH
566 } else {
567 if (status & 0xFDFFA000)
636bd13c
UH
568 pr_warn("%s: unexpected status %#x after switch\n",
569 mmc_hostname(host), status);
c6dbab9c
AH
570 if (status & R1_SWITCH_ERROR) {
571 err = -EBADMSG;
572 goto out;
573 }
ef0b27d4 574 }
c6dbab9c
AH
575out:
576 mmc_retune_release(host);
ef0b27d4 577
c6dbab9c 578 return err;
da7fbe58 579}
950d56ac
JC
580EXPORT_SYMBOL_GPL(__mmc_switch);
581
582int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
583 unsigned int timeout_ms)
584{
4509f847
UH
585 return __mmc_switch(card, set, index, value, timeout_ms, true, true,
586 false);
950d56ac 587}
d3a8d95d 588EXPORT_SYMBOL_GPL(mmc_switch);
da7fbe58 589
fe5afb13 590int mmc_send_tuning(struct mmc_host *host)
996903de
MC
591{
592 struct mmc_request mrq = {NULL};
593 struct mmc_command cmd = {0};
594 struct mmc_data data = {0};
595 struct scatterlist sg;
fe5afb13 596 struct mmc_ios *ios = &host->ios;
996903de
MC
597 const u8 *tuning_block_pattern;
598 int size, err = 0;
599 u8 *data_buf;
600 u32 opcode;
601
602 if (ios->bus_width == MMC_BUS_WIDTH_8) {
603 tuning_block_pattern = tuning_blk_pattern_8bit;
604 size = sizeof(tuning_blk_pattern_8bit);
605 opcode = MMC_SEND_TUNING_BLOCK_HS200;
606 } else if (ios->bus_width == MMC_BUS_WIDTH_4) {
607 tuning_block_pattern = tuning_blk_pattern_4bit;
608 size = sizeof(tuning_blk_pattern_4bit);
609 opcode = MMC_SEND_TUNING_BLOCK;
610 } else
611 return -EINVAL;
612
613 data_buf = kzalloc(size, GFP_KERNEL);
614 if (!data_buf)
615 return -ENOMEM;
616
617 mrq.cmd = &cmd;
618 mrq.data = &data;
619
620 cmd.opcode = opcode;
621 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
622
623 data.blksz = size;
624 data.blocks = 1;
625 data.flags = MMC_DATA_READ;
626
627 /*
628 * According to the tuning specs, Tuning process
629 * is normally shorter 40 executions of CMD19,
630 * and timeout value should be shorter than 150 ms
631 */
632 data.timeout_ns = 150 * NSEC_PER_MSEC;
633
634 data.sg = &sg;
635 data.sg_len = 1;
636 sg_init_one(&sg, data_buf, size);
637
fe5afb13 638 mmc_wait_for_req(host, &mrq);
996903de
MC
639
640 if (cmd.error) {
641 err = cmd.error;
642 goto out;
643 }
644
645 if (data.error) {
646 err = data.error;
647 goto out;
648 }
649
650 if (memcmp(data_buf, tuning_block_pattern, size))
651 err = -EIO;
652
653out:
654 kfree(data_buf);
655 return err;
656}
657EXPORT_SYMBOL_GPL(mmc_send_tuning);
658
22113efd
AL
659static int
660mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
661 u8 len)
662{
ad5fd972 663 struct mmc_request mrq = {NULL};
1278dba1 664 struct mmc_command cmd = {0};
a61ad2b4 665 struct mmc_data data = {0};
22113efd
AL
666 struct scatterlist sg;
667 u8 *data_buf;
668 u8 *test_buf;
669 int i, err;
670 static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
671 static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
672
673 /* dma onto stack is unsafe/nonportable, but callers to this
674 * routine normally provide temporary on-stack buffers ...
675 */
676 data_buf = kmalloc(len, GFP_KERNEL);
677 if (!data_buf)
678 return -ENOMEM;
679
680 if (len == 8)
681 test_buf = testdata_8bit;
682 else if (len == 4)
683 test_buf = testdata_4bit;
684 else {
a3c76eb9 685 pr_err("%s: Invalid bus_width %d\n",
22113efd
AL
686 mmc_hostname(host), len);
687 kfree(data_buf);
688 return -EINVAL;
689 }
690
691 if (opcode == MMC_BUS_TEST_W)
692 memcpy(data_buf, test_buf, len);
693
22113efd
AL
694 mrq.cmd = &cmd;
695 mrq.data = &data;
696 cmd.opcode = opcode;
697 cmd.arg = 0;
698
699 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
700 * rely on callers to never use this with "native" calls for reading
701 * CSD or CID. Native versions of those commands use the R2 type,
702 * not R1 plus a data block.
703 */
704 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
705
706 data.blksz = len;
707 data.blocks = 1;
708 if (opcode == MMC_BUS_TEST_R)
709 data.flags = MMC_DATA_READ;
710 else
711 data.flags = MMC_DATA_WRITE;
712
713 data.sg = &sg;
714 data.sg_len = 1;
84532e33 715 mmc_set_data_timeout(&data, card);
22113efd
AL
716 sg_init_one(&sg, data_buf, len);
717 mmc_wait_for_req(host, &mrq);
718 err = 0;
719 if (opcode == MMC_BUS_TEST_R) {
720 for (i = 0; i < len / 4; i++)
721 if ((test_buf[i] ^ data_buf[i]) != 0xff) {
722 err = -EIO;
723 break;
724 }
725 }
726 kfree(data_buf);
727
728 if (cmd.error)
729 return cmd.error;
730 if (data.error)
731 return data.error;
732
733 return err;
734}
735
736int mmc_bus_test(struct mmc_card *card, u8 bus_width)
737{
738 int err, width;
739
740 if (bus_width == MMC_BUS_WIDTH_8)
741 width = 8;
742 else if (bus_width == MMC_BUS_WIDTH_4)
743 width = 4;
744 else if (bus_width == MMC_BUS_WIDTH_1)
745 return 0; /* no need for test */
746 else
747 return -EINVAL;
748
749 /*
750 * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
751 * is a problem. This improves chances that the test will work.
752 */
753 mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
754 err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
755 return err;
756}
eb0d8f13
JC
757
758int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
759{
760 struct mmc_command cmd = {0};
761 unsigned int opcode;
eb0d8f13
JC
762 int err;
763
2378975b 764 if (!card->ext_csd.hpi) {
6606110d
JP
765 pr_warn("%s: Card didn't support HPI command\n",
766 mmc_hostname(card->host));
2378975b
JC
767 return -EINVAL;
768 }
769
eb0d8f13
JC
770 opcode = card->ext_csd.hpi_cmd;
771 if (opcode == MMC_STOP_TRANSMISSION)
2378975b 772 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
eb0d8f13 773 else if (opcode == MMC_SEND_STATUS)
2378975b 774 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
eb0d8f13
JC
775
776 cmd.opcode = opcode;
777 cmd.arg = card->rca << 16 | 1;
eb0d8f13
JC
778
779 err = mmc_wait_for_cmd(card->host, &cmd, 0);
780 if (err) {
781 pr_warn("%s: error %d interrupting operation. "
782 "HPI command response %#x\n", mmc_hostname(card->host),
783 err, cmd.resp[0]);
784 return err;
785 }
786 if (status)
787 *status = cmd.resp[0];
788
789 return 0;
790}
148bcab2
UH
791
792int mmc_can_ext_csd(struct mmc_card *card)
793{
794 return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3);
795}
This page took 0.726808 seconds and 5 git commands to generate.