blob: 0ea042dc74433c3808af4f5680ec46c070d68ba8 [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
Ulf Hansson04cdbbf2014-12-01 16:53:34 +010026static const u8 tuning_blk_pattern_4bit[] = {
27 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
28 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
29 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
30 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
31 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
32 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
33 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
34 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
35};
36
37static const u8 tuning_blk_pattern_8bit[] = {
38 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
39 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
40 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
41 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
42 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
43 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
44 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
45 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
46 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
47 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
48 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
49 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
50 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
51 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
52 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
53 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
54};
55
Seungwon Jeona27fbf22013-09-04 21:21:05 +090056static inline int __mmc_send_status(struct mmc_card *card, u32 *status,
57 bool ignore_crc)
58{
59 int err;
60 struct mmc_command cmd = {0};
61
62 BUG_ON(!card);
63 BUG_ON(!card->host);
64
65 cmd.opcode = MMC_SEND_STATUS;
66 if (!mmc_host_is_spi(card->host))
67 cmd.arg = card->rca << 16;
68 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
69 if (ignore_crc)
70 cmd.flags &= ~MMC_RSP_CRC;
71
72 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
73 if (err)
74 return err;
75
76 /* NOTE: callers are required to understand the difference
77 * between "native" and SPI format status words!
78 */
79 if (status)
80 *status = cmd.resp[0];
81
82 return 0;
83}
84
85int mmc_send_status(struct mmc_card *card, u32 *status)
86{
87 return __mmc_send_status(card, status, false);
88}
89
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010090static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
91{
92 int err;
Chris Ball1278dba2011-04-13 23:40:30 -040093 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010094
95 BUG_ON(!host);
96
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010097 cmd.opcode = MMC_SELECT_CARD;
98
99 if (card) {
100 cmd.arg = card->rca << 16;
101 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
102 } else {
103 cmd.arg = 0;
104 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
105 }
106
107 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200108 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100109 return err;
110
Pierre Ossman17b04292007-07-22 22:18:46 +0200111 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100112}
113
114int mmc_select_card(struct mmc_card *card)
115{
116 BUG_ON(!card);
117
118 return _mmc_select_card(card->host, card);
119}
120
121int mmc_deselect_cards(struct mmc_host *host)
122{
123 return _mmc_select_card(host, NULL);
124}
125
Sascha Hauer3d705d12014-08-19 10:45:51 +0200126/*
127 * Write the value specified in the device tree or board code into the optional
128 * 16 bit Driver Stage Register. This can be used to tune raise/fall times and
129 * drive strength of the DAT and CMD outputs. The actual meaning of a given
130 * value is hardware dependant.
131 * The presence of the DSR register can be determined from the CSD register,
132 * bit 76.
133 */
134int mmc_set_dsr(struct mmc_host *host)
135{
136 struct mmc_command cmd = {0};
137
138 cmd.opcode = MMC_SET_DSR;
139
140 cmd.arg = (host->dsr << 16) | 0xffff;
141 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
142
143 return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
144}
145
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100146int mmc_go_idle(struct mmc_host *host)
147{
148 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400149 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100150
David Brownellaf517152007-08-08 09:11:32 -0700151 /*
152 * Non-SPI hosts need to prevent chipselect going active during
153 * GO_IDLE; that would put chips into SPI mode. Remind them of
154 * that in case of hardware that won't pull up DAT3/nCS otherwise.
155 *
156 * SPI hosts ignore ios.chip_select; it's managed according to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300157 * rules that must accommodate non-MMC slaves which this layer
David Brownellaf517152007-08-08 09:11:32 -0700158 * won't even know about.
159 */
160 if (!mmc_host_is_spi(host)) {
161 mmc_set_chip_select(host, MMC_CS_HIGH);
162 mmc_delay(1);
163 }
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100164
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100165 cmd.opcode = MMC_GO_IDLE_STATE;
166 cmd.arg = 0;
David Brownellaf517152007-08-08 09:11:32 -0700167 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100168
169 err = mmc_wait_for_cmd(host, &cmd, 0);
170
171 mmc_delay(1);
172
David Brownellaf517152007-08-08 09:11:32 -0700173 if (!mmc_host_is_spi(host)) {
174 mmc_set_chip_select(host, MMC_CS_DONTCARE);
175 mmc_delay(1);
176 }
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100177
David Brownellaf517152007-08-08 09:11:32 -0700178 host->use_spi_crc = 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100179
180 return err;
181}
182
183int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
184{
Chris Ball1278dba2011-04-13 23:40:30 -0400185 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100186 int i, err = 0;
187
188 BUG_ON(!host);
189
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100190 cmd.opcode = MMC_SEND_OP_COND;
David Brownellaf517152007-08-08 09:11:32 -0700191 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
192 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100193
194 for (i = 100; i; i--) {
195 err = mmc_wait_for_cmd(host, &cmd, 0);
Pierre Ossman17b04292007-07-22 22:18:46 +0200196 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100197 break;
198
David Brownellaf517152007-08-08 09:11:32 -0700199 /* if we're just probing, do a single pass */
200 if (ocr == 0)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100201 break;
202
David Brownellaf517152007-08-08 09:11:32 -0700203 /* otherwise wait until reset completes */
204 if (mmc_host_is_spi(host)) {
205 if (!(cmd.resp[0] & R1_SPI_IDLE))
206 break;
207 } else {
208 if (cmd.resp[0] & MMC_CARD_BUSY)
209 break;
210 }
211
Pierre Ossman17b04292007-07-22 22:18:46 +0200212 err = -ETIMEDOUT;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100213
214 mmc_delay(10);
215 }
216
David Brownellaf517152007-08-08 09:11:32 -0700217 if (rocr && !mmc_host_is_spi(host))
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100218 *rocr = cmd.resp[0];
219
220 return err;
221}
222
223int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
224{
225 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400226 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100227
228 BUG_ON(!host);
229 BUG_ON(!cid);
230
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100231 cmd.opcode = MMC_ALL_SEND_CID;
232 cmd.arg = 0;
233 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
234
235 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200236 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100237 return err;
238
239 memcpy(cid, cmd.resp, sizeof(u32) * 4);
240
Pierre Ossman17b04292007-07-22 22:18:46 +0200241 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100242}
243
244int mmc_set_relative_addr(struct mmc_card *card)
245{
246 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400247 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100248
249 BUG_ON(!card);
250 BUG_ON(!card->host);
251
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100252 cmd.opcode = MMC_SET_RELATIVE_ADDR;
253 cmd.arg = card->rca << 16;
254 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
255
256 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200257 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100258 return err;
259
Pierre Ossman17b04292007-07-22 22:18:46 +0200260 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100261}
262
David Brownellaf517152007-08-08 09:11:32 -0700263static int
264mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100265{
266 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400267 struct mmc_command cmd = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100268
David Brownellaf517152007-08-08 09:11:32 -0700269 BUG_ON(!host);
270 BUG_ON(!cxd);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100271
David Brownellaf517152007-08-08 09:11:32 -0700272 cmd.opcode = opcode;
273 cmd.arg = arg;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100274 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
275
David Brownellaf517152007-08-08 09:11:32 -0700276 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200277 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100278 return err;
279
David Brownellaf517152007-08-08 09:11:32 -0700280 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100281
Pierre Ossman17b04292007-07-22 22:18:46 +0200282 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100283}
284
Kyungsik Lee1a413132012-08-03 00:58:03 +0000285/*
286 * NOTE: void *buf, caller for the buf is required to use DMA-capable
287 * buffer or on-stack buffer (with some overhead in callee).
288 */
David Brownellaf517152007-08-08 09:11:32 -0700289static int
290mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
291 u32 opcode, void *buf, unsigned len)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100292{
Venkatraman Sad5fd972011-08-25 00:30:50 +0530293 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400294 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400295 struct mmc_data data = {0};
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100296 struct scatterlist sg;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100297
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100298 mrq.cmd = &cmd;
299 mrq.data = &data;
300
David Brownellaf517152007-08-08 09:11:32 -0700301 cmd.opcode = opcode;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100302 cmd.arg = 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100303
David Brownellaf517152007-08-08 09:11:32 -0700304 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
305 * rely on callers to never use this with "native" calls for reading
306 * CSD or CID. Native versions of those commands use the R2 type,
307 * not R1 plus a data block.
308 */
309 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
310
311 data.blksz = len;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100312 data.blocks = 1;
313 data.flags = MMC_DATA_READ;
314 data.sg = &sg;
315 data.sg_len = 1;
316
Ulf Hansson601ed602014-10-17 11:58:24 +0200317 sg_init_one(&sg, buf, len);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100318
Adrian Huntercda56ac22009-02-10 16:32:33 +0200319 if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
320 /*
321 * The spec states that CSR and CID accesses have a timeout
322 * of 64 clock cycles.
323 */
324 data.timeout_ns = 0;
325 data.timeout_clks = 64;
326 } else
327 mmc_set_data_timeout(&data, card);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100328
David Brownellaf517152007-08-08 09:11:32 -0700329 mmc_wait_for_req(host, &mrq);
330
Pierre Ossman17b04292007-07-22 22:18:46 +0200331 if (cmd.error)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100332 return cmd.error;
Pierre Ossman17b04292007-07-22 22:18:46 +0200333 if (data.error)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100334 return data.error;
335
Pierre Ossman17b04292007-07-22 22:18:46 +0200336 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100337}
338
David Brownellaf517152007-08-08 09:11:32 -0700339int mmc_send_csd(struct mmc_card *card, u32 *csd)
340{
Pierre Ossman78e48072007-10-27 14:14:23 +0200341 int ret, i;
Kyungsik Lee1a413132012-08-03 00:58:03 +0000342 u32 *csd_tmp;
Pierre Ossman78e48072007-10-27 14:14:23 +0200343
David Brownellaf517152007-08-08 09:11:32 -0700344 if (!mmc_host_is_spi(card->host))
345 return mmc_send_cxd_native(card->host, card->rca << 16,
346 csd, MMC_SEND_CSD);
347
Ulf Hansson22b78702014-10-17 12:00:56 +0200348 csd_tmp = kzalloc(16, GFP_KERNEL);
Kyungsik Lee1a413132012-08-03 00:58:03 +0000349 if (!csd_tmp)
350 return -ENOMEM;
351
352 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16);
Pierre Ossman78e48072007-10-27 14:14:23 +0200353 if (ret)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000354 goto err;
Pierre Ossman78e48072007-10-27 14:14:23 +0200355
356 for (i = 0;i < 4;i++)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000357 csd[i] = be32_to_cpu(csd_tmp[i]);
Pierre Ossman78e48072007-10-27 14:14:23 +0200358
Kyungsik Lee1a413132012-08-03 00:58:03 +0000359err:
360 kfree(csd_tmp);
361 return ret;
David Brownellaf517152007-08-08 09:11:32 -0700362}
363
364int mmc_send_cid(struct mmc_host *host, u32 *cid)
365{
Pierre Ossman78e48072007-10-27 14:14:23 +0200366 int ret, i;
Kyungsik Lee1a413132012-08-03 00:58:03 +0000367 u32 *cid_tmp;
Pierre Ossman78e48072007-10-27 14:14:23 +0200368
David Brownellaf517152007-08-08 09:11:32 -0700369 if (!mmc_host_is_spi(host)) {
370 if (!host->card)
371 return -EINVAL;
372 return mmc_send_cxd_native(host, host->card->rca << 16,
373 cid, MMC_SEND_CID);
374 }
375
Ulf Hansson22b78702014-10-17 12:00:56 +0200376 cid_tmp = kzalloc(16, GFP_KERNEL);
Kyungsik Lee1a413132012-08-03 00:58:03 +0000377 if (!cid_tmp)
378 return -ENOMEM;
379
380 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16);
Pierre Ossman78e48072007-10-27 14:14:23 +0200381 if (ret)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000382 goto err;
Pierre Ossman78e48072007-10-27 14:14:23 +0200383
384 for (i = 0;i < 4;i++)
Kyungsik Lee1a413132012-08-03 00:58:03 +0000385 cid[i] = be32_to_cpu(cid_tmp[i]);
Pierre Ossman78e48072007-10-27 14:14:23 +0200386
Kyungsik Lee1a413132012-08-03 00:58:03 +0000387err:
388 kfree(cid_tmp);
389 return ret;
David Brownellaf517152007-08-08 09:11:32 -0700390}
391
Ulf Hanssone21aa512014-10-17 11:32:32 +0200392int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
393{
394 int err;
395 u8 *ext_csd;
396
397 if (!card || !new_ext_csd)
398 return -EINVAL;
399
400 if (!mmc_can_ext_csd(card))
401 return -EOPNOTSUPP;
402
403 /*
404 * As the ext_csd is so large and mostly unused, we don't store the
405 * raw block in mmc_card.
406 */
Ulf Hansson22b78702014-10-17 12:00:56 +0200407 ext_csd = kzalloc(512, GFP_KERNEL);
Ulf Hanssone21aa512014-10-17 11:32:32 +0200408 if (!ext_csd)
409 return -ENOMEM;
410
Ulf Hansson2fc91e82014-10-17 11:54:22 +0200411 err = mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, ext_csd,
412 512);
Ulf Hanssone21aa512014-10-17 11:32:32 +0200413 if (err)
414 kfree(ext_csd);
415 else
416 *new_ext_csd = ext_csd;
417
418 return err;
419}
420EXPORT_SYMBOL_GPL(mmc_get_ext_csd);
421
David Brownellaf517152007-08-08 09:11:32 -0700422int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
423{
Chris Ball1278dba2011-04-13 23:40:30 -0400424 struct mmc_command cmd = {0};
David Brownellaf517152007-08-08 09:11:32 -0700425 int err;
426
David Brownellaf517152007-08-08 09:11:32 -0700427 cmd.opcode = MMC_SPI_READ_OCR;
428 cmd.arg = highcap ? (1 << 30) : 0;
429 cmd.flags = MMC_RSP_SPI_R3;
430
431 err = mmc_wait_for_cmd(host, &cmd, 0);
432
433 *ocrp = cmd.resp[1];
434 return err;
435}
436
437int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
438{
Chris Ball1278dba2011-04-13 23:40:30 -0400439 struct mmc_command cmd = {0};
David Brownellaf517152007-08-08 09:11:32 -0700440 int err;
441
David Brownellaf517152007-08-08 09:11:32 -0700442 cmd.opcode = MMC_SPI_CRC_ON_OFF;
443 cmd.flags = MMC_RSP_SPI_R1;
444 cmd.arg = use_crc;
445
446 err = mmc_wait_for_cmd(host, &cmd, 0);
447 if (!err)
448 host->use_spi_crc = use_crc;
449 return err;
450}
451
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500452/**
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000453 * __mmc_switch - modify EXT_CSD register
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500454 * @card: the MMC card associated with the data transfer
455 * @set: cmd set values
456 * @index: EXT_CSD register index
457 * @value: value to program into EXT_CSD register
458 * @timeout_ms: timeout (ms) for operation performed by register write,
459 * timeout of zero implies maximum possible timeout
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000460 * @use_busy_signal: use the busy signal as response type
Ulf Hansson878e2002013-09-09 11:57:57 +0200461 * @send_status: send status cmd to poll for busy
Ulf Hansson4509f84772014-01-08 16:09:33 +0100462 * @ignore_crc: ignore CRC errors when sending status cmd to poll for busy
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500463 *
464 * Modifies the EXT_CSD register for selected card.
465 */
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000466int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
Ulf Hansson4509f84772014-01-08 16:09:33 +0100467 unsigned int timeout_ms, bool use_busy_signal, bool send_status,
468 bool ignore_crc)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100469{
Ulf Hansson636bd132014-01-28 14:05:39 +0100470 struct mmc_host *host = card->host;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100471 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400472 struct mmc_command cmd = {0};
Trey Ramsay8fee4762012-11-16 09:31:41 -0600473 unsigned long timeout;
Ulf Hanssonecd3a7d2013-10-28 02:51:41 +0100474 u32 status = 0;
Ulf Hanssonb9ec2612014-01-28 14:15:34 +0100475 bool use_r1b_resp = use_busy_signal;
476
477 /*
478 * If the cmd timeout and the max_busy_timeout of the host are both
479 * specified, let's validate them. A failure means we need to prevent
480 * the host from doing hw busy detection, which is done by converting
481 * to a R1 response instead of a R1B.
482 */
483 if (timeout_ms && host->max_busy_timeout &&
484 (timeout_ms > host->max_busy_timeout))
485 use_r1b_resp = false;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100486
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100487 cmd.opcode = MMC_SWITCH;
488 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
489 (index << 16) |
490 (value << 8) |
491 set;
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000492 cmd.flags = MMC_CMD_AC;
Ulf Hanssonb9ec2612014-01-28 14:15:34 +0100493 if (use_r1b_resp) {
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000494 cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B;
Ulf Hanssonb9ec2612014-01-28 14:15:34 +0100495 /*
496 * A busy_timeout of zero means the host can decide to use
497 * whatever value it finds suitable.
498 */
499 cmd.busy_timeout = timeout_ms;
500 } else {
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000501 cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1;
Ulf Hanssonb9ec2612014-01-28 14:15:34 +0100502 }
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000503
Maya Erez775a9362013-04-18 15:41:55 +0300504 if (index == EXT_CSD_SANITIZE_START)
505 cmd.sanitize_busy = true;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100506
Ulf Hansson636bd132014-01-28 14:05:39 +0100507 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
Pierre Ossman17b04292007-07-22 22:18:46 +0200508 if (err)
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100509 return err;
510
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000511 /* No need to check card status in case of unblocking command */
512 if (!use_busy_signal)
513 return 0;
514
Seungwon Jeona27fbf22013-09-04 21:21:05 +0900515 /*
Ulf Hansson4509f84772014-01-08 16:09:33 +0100516 * CRC errors shall only be ignored in cases were CMD13 is used to poll
517 * to detect busy completion.
Seungwon Jeona27fbf22013-09-04 21:21:05 +0900518 */
Ulf Hanssonb9ec2612014-01-28 14:15:34 +0100519 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
Ulf Hansson4509f84772014-01-08 16:09:33 +0100520 ignore_crc = false;
Seungwon Jeona27fbf22013-09-04 21:21:05 +0900521
Ulf Hanssonb9ec2612014-01-28 14:15:34 +0100522 /* We have an unspecified cmd timeout, use the fallback value. */
523 if (!timeout_ms)
524 timeout_ms = MMC_OPS_TIMEOUT_MS;
525
Ulf Hansson4509f84772014-01-08 16:09:33 +0100526 /* Must check status to be sure of no errors. */
Ulf Hanssonb9ec2612014-01-28 14:15:34 +0100527 timeout = jiffies + msecs_to_jiffies(timeout_ms);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700528 do {
Ulf Hansson878e2002013-09-09 11:57:57 +0200529 if (send_status) {
530 err = __mmc_send_status(card, &status, ignore_crc);
531 if (err)
532 return err;
533 }
Ulf Hanssonb9ec2612014-01-28 14:15:34 +0100534 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700535 break;
Ulf Hansson636bd132014-01-28 14:05:39 +0100536 if (mmc_host_is_spi(host))
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700537 break;
Trey Ramsay8fee4762012-11-16 09:31:41 -0600538
Ulf Hansson878e2002013-09-09 11:57:57 +0200539 /*
540 * We are not allowed to issue a status command and the host
541 * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only
542 * rely on waiting for the stated timeout to be sufficient.
543 */
544 if (!send_status) {
545 mmc_delay(timeout_ms);
546 return 0;
547 }
548
Trey Ramsay8fee4762012-11-16 09:31:41 -0600549 /* Timeout if the device never leaves the program state. */
550 if (time_after(jiffies, timeout)) {
551 pr_err("%s: Card stuck in programming state! %s\n",
Ulf Hansson636bd132014-01-28 14:05:39 +0100552 mmc_hostname(host), __func__);
Trey Ramsay8fee4762012-11-16 09:31:41 -0600553 return -ETIMEDOUT;
554 }
Jaehoon Chung7435bb72011-08-10 18:46:28 +0900555 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700556
Ulf Hansson636bd132014-01-28 14:05:39 +0100557 if (mmc_host_is_spi(host)) {
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700558 if (status & R1_SPI_ILLEGAL_COMMAND)
559 return -EBADMSG;
560 } else {
561 if (status & 0xFDFFA000)
Ulf Hansson636bd132014-01-28 14:05:39 +0100562 pr_warn("%s: unexpected status %#x after switch\n",
563 mmc_hostname(host), status);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700564 if (status & R1_SWITCH_ERROR)
565 return -EBADMSG;
566 }
567
Pierre Ossman17b04292007-07-22 22:18:46 +0200568 return 0;
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100569}
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000570EXPORT_SYMBOL_GPL(__mmc_switch);
571
572int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value,
573 unsigned int timeout_ms)
574{
Ulf Hansson4509f84772014-01-08 16:09:33 +0100575 return __mmc_switch(card, set, index, value, timeout_ms, true, true,
576 false);
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000577}
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500578EXPORT_SYMBOL_GPL(mmc_switch);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100579
Ulf Hanssonfe5afb13d2014-12-05 11:31:22 +0100580int mmc_send_tuning(struct mmc_host *host)
Minda Chen996903d2014-11-26 13:05:33 +0800581{
582 struct mmc_request mrq = {NULL};
583 struct mmc_command cmd = {0};
584 struct mmc_data data = {0};
585 struct scatterlist sg;
Ulf Hanssonfe5afb13d2014-12-05 11:31:22 +0100586 struct mmc_ios *ios = &host->ios;
Minda Chen996903d2014-11-26 13:05:33 +0800587 const u8 *tuning_block_pattern;
588 int size, err = 0;
589 u8 *data_buf;
590 u32 opcode;
591
592 if (ios->bus_width == MMC_BUS_WIDTH_8) {
593 tuning_block_pattern = tuning_blk_pattern_8bit;
594 size = sizeof(tuning_blk_pattern_8bit);
595 opcode = MMC_SEND_TUNING_BLOCK_HS200;
596 } else if (ios->bus_width == MMC_BUS_WIDTH_4) {
597 tuning_block_pattern = tuning_blk_pattern_4bit;
598 size = sizeof(tuning_blk_pattern_4bit);
599 opcode = MMC_SEND_TUNING_BLOCK;
600 } else
601 return -EINVAL;
602
603 data_buf = kzalloc(size, GFP_KERNEL);
604 if (!data_buf)
605 return -ENOMEM;
606
607 mrq.cmd = &cmd;
608 mrq.data = &data;
609
610 cmd.opcode = opcode;
611 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
612
613 data.blksz = size;
614 data.blocks = 1;
615 data.flags = MMC_DATA_READ;
616
617 /*
618 * According to the tuning specs, Tuning process
619 * is normally shorter 40 executions of CMD19,
620 * and timeout value should be shorter than 150 ms
621 */
622 data.timeout_ns = 150 * NSEC_PER_MSEC;
623
624 data.sg = &sg;
625 data.sg_len = 1;
626 sg_init_one(&sg, data_buf, size);
627
Ulf Hanssonfe5afb13d2014-12-05 11:31:22 +0100628 mmc_wait_for_req(host, &mrq);
Minda Chen996903d2014-11-26 13:05:33 +0800629
630 if (cmd.error) {
631 err = cmd.error;
632 goto out;
633 }
634
635 if (data.error) {
636 err = data.error;
637 goto out;
638 }
639
640 if (memcmp(data_buf, tuning_block_pattern, size))
641 err = -EIO;
642
643out:
644 kfree(data_buf);
645 return err;
646}
647EXPORT_SYMBOL_GPL(mmc_send_tuning);
648
Aries Lee22113ef2010-12-15 08:14:24 +0100649static int
650mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
651 u8 len)
652{
Venkatraman Sad5fd972011-08-25 00:30:50 +0530653 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400654 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400655 struct mmc_data data = {0};
Aries Lee22113ef2010-12-15 08:14:24 +0100656 struct scatterlist sg;
657 u8 *data_buf;
658 u8 *test_buf;
659 int i, err;
660 static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
661 static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
662
663 /* dma onto stack is unsafe/nonportable, but callers to this
664 * routine normally provide temporary on-stack buffers ...
665 */
666 data_buf = kmalloc(len, GFP_KERNEL);
667 if (!data_buf)
668 return -ENOMEM;
669
670 if (len == 8)
671 test_buf = testdata_8bit;
672 else if (len == 4)
673 test_buf = testdata_4bit;
674 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530675 pr_err("%s: Invalid bus_width %d\n",
Aries Lee22113ef2010-12-15 08:14:24 +0100676 mmc_hostname(host), len);
677 kfree(data_buf);
678 return -EINVAL;
679 }
680
681 if (opcode == MMC_BUS_TEST_W)
682 memcpy(data_buf, test_buf, len);
683
Aries Lee22113ef2010-12-15 08:14:24 +0100684 mrq.cmd = &cmd;
685 mrq.data = &data;
686 cmd.opcode = opcode;
687 cmd.arg = 0;
688
689 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
690 * rely on callers to never use this with "native" calls for reading
691 * CSD or CID. Native versions of those commands use the R2 type,
692 * not R1 plus a data block.
693 */
694 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
695
696 data.blksz = len;
697 data.blocks = 1;
698 if (opcode == MMC_BUS_TEST_R)
699 data.flags = MMC_DATA_READ;
700 else
701 data.flags = MMC_DATA_WRITE;
702
703 data.sg = &sg;
704 data.sg_len = 1;
Minjian Wu84532e32013-08-24 23:25:12 -0400705 mmc_set_data_timeout(&data, card);
Aries Lee22113ef2010-12-15 08:14:24 +0100706 sg_init_one(&sg, data_buf, len);
707 mmc_wait_for_req(host, &mrq);
708 err = 0;
709 if (opcode == MMC_BUS_TEST_R) {
710 for (i = 0; i < len / 4; i++)
711 if ((test_buf[i] ^ data_buf[i]) != 0xff) {
712 err = -EIO;
713 break;
714 }
715 }
716 kfree(data_buf);
717
718 if (cmd.error)
719 return cmd.error;
720 if (data.error)
721 return data.error;
722
723 return err;
724}
725
726int mmc_bus_test(struct mmc_card *card, u8 bus_width)
727{
728 int err, width;
729
730 if (bus_width == MMC_BUS_WIDTH_8)
731 width = 8;
732 else if (bus_width == MMC_BUS_WIDTH_4)
733 width = 4;
734 else if (bus_width == MMC_BUS_WIDTH_1)
735 return 0; /* no need for test */
736 else
737 return -EINVAL;
738
739 /*
740 * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
741 * is a problem. This improves chances that the test will work.
742 */
743 mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
744 err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
745 return err;
746}
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400747
748int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status)
749{
750 struct mmc_command cmd = {0};
751 unsigned int opcode;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400752 int err;
753
Jaehoon Chung23789752012-03-12 08:57:27 +0900754 if (!card->ext_csd.hpi) {
Joe Perches66061102014-09-12 14:56:56 -0700755 pr_warn("%s: Card didn't support HPI command\n",
756 mmc_hostname(card->host));
Jaehoon Chung23789752012-03-12 08:57:27 +0900757 return -EINVAL;
758 }
759
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400760 opcode = card->ext_csd.hpi_cmd;
761 if (opcode == MMC_STOP_TRANSMISSION)
Jaehoon Chung23789752012-03-12 08:57:27 +0900762 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400763 else if (opcode == MMC_SEND_STATUS)
Jaehoon Chung23789752012-03-12 08:57:27 +0900764 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400765
766 cmd.opcode = opcode;
767 cmd.arg = card->rca << 16 | 1;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400768
769 err = mmc_wait_for_cmd(card->host, &cmd, 0);
770 if (err) {
771 pr_warn("%s: error %d interrupting operation. "
772 "HPI command response %#x\n", mmc_hostname(card->host),
773 err, cmd.resp[0]);
774 return err;
775 }
776 if (status)
777 *status = cmd.resp[0];
778
779 return 0;
780}
Ulf Hansson148bcab2014-10-20 11:33:53 +0200781
782int mmc_can_ext_csd(struct mmc_card *card)
783{
784 return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3);
785}