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