Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-block.git] / drivers / mmc / core / sd.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
7ea239d9 2/*
70f10482 3 * linux/drivers/mmc/core/sd.c
7ea239d9
PO
4 *
5 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
6 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
7 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
7ea239d9
PO
8 */
9
10#include <linux/err.h>
9288cac0 11#include <linux/sizes.h>
5a0e3ad6 12#include <linux/slab.h>
0205a904 13#include <linux/stat.h>
0cb403a2 14#include <linux/pm_runtime.h>
ac9d2555 15#include <linux/random.h>
24b83deb 16#include <linux/scatterlist.h>
f5d8a5fe 17#include <linux/sysfs.h>
7ea239d9
PO
18
19#include <linux/mmc/host.h>
20#include <linux/mmc/card.h>
21#include <linux/mmc/mmc.h>
3373c0ae 22#include <linux/mmc/sd.h>
7ea239d9
PO
23
24#include "core.h"
4facdde1 25#include "card.h"
5857b29b 26#include "host.h"
4101c16a 27#include "bus.h"
7ea239d9 28#include "mmc_ops.h"
469e5e47 29#include "quirks.h"
ce101496 30#include "sd.h"
7ea239d9
PO
31#include "sd_ops.h"
32
7ea239d9
PO
33static const unsigned int tran_exp[] = {
34 10000, 100000, 1000000, 10000000,
35 0, 0, 0, 0
36};
37
38static const unsigned char tran_mant[] = {
39 0, 10, 12, 13, 15, 20, 25, 30,
40 35, 40, 45, 50, 55, 60, 70, 80,
41};
42
4406ae21 43static const unsigned int taac_exp[] = {
7ea239d9
PO
44 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
45};
46
4406ae21 47static const unsigned int taac_mant[] = {
7ea239d9
PO
48 0, 10, 12, 13, 15, 20, 25, 30,
49 35, 40, 45, 50, 55, 60, 70, 80,
50};
51
9288cac0
WS
52static const unsigned int sd_au_size[] = {
53 0, SZ_16K / 512, SZ_32K / 512, SZ_64K / 512,
54 SZ_128K / 512, SZ_256K / 512, SZ_512K / 512, SZ_1M / 512,
55 SZ_2M / 512, SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
56 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512, SZ_64M / 512,
57};
58
379f56c2 59#define SD_POWEROFF_NOTIFY_TIMEOUT_MS 1000
130206a6 60#define SD_WRITE_EXTR_SINGLE_TIMEOUT_MS 1000
2c5d4276
UH
61
62struct sd_busy_data {
63 struct mmc_card *card;
64 u8 *reg_buf;
65};
66
7ea239d9
PO
67/*
68 * Given the decoded CSD structure, decode the raw CID to our CID structure.
69 */
71578a1e 70void mmc_decode_cid(struct mmc_card *card)
7ea239d9
PO
71{
72 u32 *resp = card->raw_cid;
73
ac9d2555
LW
74 /*
75 * Add the raw card ID (cid) data to the entropy pool. It doesn't
76 * matter that not all of it is unique, it's just bonus entropy.
77 */
78 add_device_randomness(&card->raw_cid, sizeof(card->raw_cid));
79
7ea239d9
PO
80 /*
81 * SD doesn't currently have a version field so we will
82 * have to assume we can parse this.
83 */
38fb6997
AA
84 card->cid.manfid = unstuff_bits(resp, 120, 8);
85 card->cid.oemid = unstuff_bits(resp, 104, 16);
86 card->cid.prod_name[0] = unstuff_bits(resp, 96, 8);
87 card->cid.prod_name[1] = unstuff_bits(resp, 88, 8);
88 card->cid.prod_name[2] = unstuff_bits(resp, 80, 8);
89 card->cid.prod_name[3] = unstuff_bits(resp, 72, 8);
90 card->cid.prod_name[4] = unstuff_bits(resp, 64, 8);
91 card->cid.hwrev = unstuff_bits(resp, 60, 4);
92 card->cid.fwrev = unstuff_bits(resp, 56, 4);
93 card->cid.serial = unstuff_bits(resp, 24, 32);
94 card->cid.year = unstuff_bits(resp, 12, 8);
95 card->cid.month = unstuff_bits(resp, 8, 4);
7ea239d9
PO
96
97 card->cid.year += 2000; /* SD cards year offset */
98}
99
100/*
101 * Given a 128-bit response, decode to our card CSD structure.
102 */
bd766312 103static int mmc_decode_csd(struct mmc_card *card)
7ea239d9
PO
104{
105 struct mmc_csd *csd = &card->csd;
106 unsigned int e, m, csd_struct;
107 u32 *resp = card->raw_csd;
108
38fb6997 109 csd_struct = unstuff_bits(resp, 126, 2);
7ea239d9
PO
110
111 switch (csd_struct) {
112 case 0:
38fb6997
AA
113 m = unstuff_bits(resp, 115, 4);
114 e = unstuff_bits(resp, 112, 3);
4406ae21 115 csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
38fb6997 116 csd->taac_clks = unstuff_bits(resp, 104, 8) * 100;
7ea239d9 117
38fb6997
AA
118 m = unstuff_bits(resp, 99, 4);
119 e = unstuff_bits(resp, 96, 3);
7ea239d9 120 csd->max_dtr = tran_exp[e] * tran_mant[m];
38fb6997 121 csd->cmdclass = unstuff_bits(resp, 84, 12);
7ea239d9 122
38fb6997
AA
123 e = unstuff_bits(resp, 47, 3);
124 m = unstuff_bits(resp, 62, 12);
7ea239d9
PO
125 csd->capacity = (1 + m) << (e + 2);
126
38fb6997
AA
127 csd->read_blkbits = unstuff_bits(resp, 80, 4);
128 csd->read_partial = unstuff_bits(resp, 79, 1);
129 csd->write_misalign = unstuff_bits(resp, 78, 1);
130 csd->read_misalign = unstuff_bits(resp, 77, 1);
131 csd->dsr_imp = unstuff_bits(resp, 76, 1);
132 csd->r2w_factor = unstuff_bits(resp, 26, 3);
133 csd->write_blkbits = unstuff_bits(resp, 22, 4);
134 csd->write_partial = unstuff_bits(resp, 21, 1);
dfe86cba 135
38fb6997 136 if (unstuff_bits(resp, 46, 1)) {
dfe86cba
AH
137 csd->erase_size = 1;
138 } else if (csd->write_blkbits >= 9) {
38fb6997 139 csd->erase_size = unstuff_bits(resp, 39, 7) + 1;
dfe86cba
AH
140 csd->erase_size <<= csd->write_blkbits - 9;
141 }
917a5336 142
38fb6997 143 if (unstuff_bits(resp, 13, 1))
917a5336 144 mmc_card_set_readonly(card);
7ea239d9
PO
145 break;
146 case 1:
147 /*
3a303511 148 * This is a block-addressed SDHC or SDXC card. Most
7ea239d9
PO
149 * interesting fields are unused and have fixed
150 * values. To avoid getting tripped by buggy cards,
151 * we assume those fixed values ourselves.
152 */
153 mmc_card_set_blockaddr(card);
154
4406ae21
SL
155 csd->taac_ns = 0; /* Unused */
156 csd->taac_clks = 0; /* Unused */
7ea239d9 157
38fb6997
AA
158 m = unstuff_bits(resp, 99, 4);
159 e = unstuff_bits(resp, 96, 3);
7ea239d9 160 csd->max_dtr = tran_exp[e] * tran_mant[m];
38fb6997
AA
161 csd->cmdclass = unstuff_bits(resp, 84, 12);
162 csd->c_size = unstuff_bits(resp, 48, 22);
3a303511
AN
163
164 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
165 if (csd->c_size >= 0xFFFF)
166 mmc_card_set_ext_capacity(card);
7ea239d9 167
38fb6997 168 m = unstuff_bits(resp, 48, 22);
7ea239d9
PO
169 csd->capacity = (1 + m) << 10;
170
171 csd->read_blkbits = 9;
172 csd->read_partial = 0;
173 csd->write_misalign = 0;
174 csd->read_misalign = 0;
175 csd->r2w_factor = 4; /* Unused */
176 csd->write_blkbits = 9;
177 csd->write_partial = 0;
dfe86cba 178 csd->erase_size = 1;
917a5336 179
38fb6997 180 if (unstuff_bits(resp, 13, 1))
917a5336 181 mmc_card_set_readonly(card);
7ea239d9
PO
182 break;
183 default:
a3c76eb9 184 pr_err("%s: unrecognised CSD structure version %d\n",
7ea239d9 185 mmc_hostname(card->host), csd_struct);
bd766312 186 return -EINVAL;
7ea239d9 187 }
bd766312 188
dfe86cba
AH
189 card->erase_size = csd->erase_size;
190
bd766312 191 return 0;
7ea239d9
PO
192}
193
194/*
195 * Given a 64-bit response, decode to our card SCR structure.
196 */
bd766312 197static int mmc_decode_scr(struct mmc_card *card)
7ea239d9
PO
198{
199 struct sd_scr *scr = &card->scr;
200 unsigned int scr_struct;
201 u32 resp[4];
202
7ea239d9
PO
203 resp[3] = card->raw_scr[1];
204 resp[2] = card->raw_scr[0];
205
38fb6997 206 scr_struct = unstuff_bits(resp, 60, 4);
7ea239d9 207 if (scr_struct != 0) {
a3c76eb9 208 pr_err("%s: unrecognised SCR structure version %d\n",
7ea239d9 209 mmc_hostname(card->host), scr_struct);
bd766312 210 return -EINVAL;
7ea239d9
PO
211 }
212
38fb6997
AA
213 scr->sda_vsn = unstuff_bits(resp, 56, 4);
214 scr->bus_widths = unstuff_bits(resp, 48, 4);
013909c4
AN
215 if (scr->sda_vsn == SCR_SPEC_VER_2)
216 /* Check if Physical Layer Spec v3.0 is supported */
38fb6997 217 scr->sda_spec3 = unstuff_bits(resp, 47, 1);
bd766312 218
68539e2b 219 if (scr->sda_spec3) {
38fb6997
AA
220 scr->sda_spec4 = unstuff_bits(resp, 42, 1);
221 scr->sda_specx = unstuff_bits(resp, 38, 4);
68539e2b
AA
222 }
223
38fb6997 224 if (unstuff_bits(resp, 55, 1))
dfe86cba
AH
225 card->erased_byte = 0xFF;
226 else
227 card->erased_byte = 0x0;
228
dbea8ae9 229 if (scr->sda_spec4)
38fb6997 230 scr->cmds = unstuff_bits(resp, 32, 4);
dbea8ae9 231 else if (scr->sda_spec3)
38fb6997 232 scr->cmds = unstuff_bits(resp, 32, 2);
9e4be8d0
RR
233
234 /* SD Spec says: any SD Card shall set at least bits 0 and 2 */
235 if (!(scr->bus_widths & SD_SCR_BUS_WIDTH_1) ||
236 !(scr->bus_widths & SD_SCR_BUS_WIDTH_4)) {
237 pr_err("%s: invalid bus width\n", mmc_hostname(card->host));
238 return -EINVAL;
239 }
240
bd766312 241 return 0;
7ea239d9
PO
242}
243
dfe86cba
AH
244/*
245 * Fetch and process SD Status register.
246 */
247static int mmc_read_ssr(struct mmc_card *card)
248{
249 unsigned int au, es, et, eo;
06c9ccb7 250 __be32 *raw_ssr;
bc47e2f6
AA
251 u32 resp[4] = {};
252 u8 discard_support;
5275a652 253 int i;
dfe86cba
AH
254
255 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
6606110d
JP
256 pr_warn("%s: card lacks mandatory SD Status function\n",
257 mmc_hostname(card->host));
dfe86cba
AH
258 return 0;
259 }
260
e85baa88
PB
261 raw_ssr = kmalloc(sizeof(card->raw_ssr), GFP_KERNEL);
262 if (!raw_ssr)
263 return -ENOMEM;
264
265 if (mmc_app_sd_status(card, raw_ssr)) {
6606110d
JP
266 pr_warn("%s: problem reading SD Status register\n",
267 mmc_hostname(card->host));
e85baa88 268 kfree(raw_ssr);
5275a652 269 return 0;
dfe86cba
AH
270 }
271
272 for (i = 0; i < 16; i++)
e85baa88
PB
273 card->raw_ssr[i] = be32_to_cpu(raw_ssr[i]);
274
275 kfree(raw_ssr);
dfe86cba
AH
276
277 /*
38fb6997 278 * unstuff_bits only works with four u32s so we have to offset the
dfe86cba
AH
279 * bitfield positions accordingly.
280 */
38fb6997 281 au = unstuff_bits(card->raw_ssr, 428 - 384, 4);
9288cac0
WS
282 if (au) {
283 if (au <= 9 || card->scr.sda_spec3) {
284 card->ssr.au = sd_au_size[au];
38fb6997
AA
285 es = unstuff_bits(card->raw_ssr, 408 - 384, 16);
286 et = unstuff_bits(card->raw_ssr, 402 - 384, 6);
9288cac0 287 if (es && et) {
38fb6997 288 eo = unstuff_bits(card->raw_ssr, 400 - 384, 2);
9288cac0
WS
289 card->ssr.erase_timeout = (et * 1000) / es;
290 card->ssr.erase_offset = eo * 1000;
291 }
292 } else {
6606110d
JP
293 pr_warn("%s: SD Status: Invalid Allocation Unit size\n",
294 mmc_hostname(card->host));
dfe86cba 295 }
dfe86cba 296 }
5275a652 297
bc47e2f6
AA
298 /*
299 * starting SD5.1 discard is supported if DISCARD_SUPPORT (b313) is set
300 */
301 resp[3] = card->raw_ssr[6];
38fb6997 302 discard_support = unstuff_bits(resp, 313 - 288, 1);
bc47e2f6
AA
303 card->erase_arg = (card->scr.sda_specx && discard_support) ?
304 SD_DISCARD_ARG : SD_ERASE_ARG;
01904ff7 305
5275a652 306 return 0;
dfe86cba
AH
307}
308
7ea239d9 309/*
1addfcdb 310 * Fetches and decodes switch information
7ea239d9 311 */
1addfcdb 312static int mmc_read_switch(struct mmc_card *card)
7ea239d9
PO
313{
314 int err;
315 u8 *status;
316
3373c0ae 317 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
17b0429d 318 return 0;
3373c0ae
PO
319
320 if (!(card->csd.cmdclass & CCC_SWITCH)) {
6606110d 321 pr_warn("%s: card lacks mandatory switch function, performance might suffer\n",
3373c0ae 322 mmc_hostname(card->host));
17b0429d 323 return 0;
3373c0ae
PO
324 }
325
7ea239d9 326 status = kmalloc(64, GFP_KERNEL);
e1df7ae3 327 if (!status)
17b0429d 328 return -ENOMEM;
7ea239d9 329
a39ca6ae
AL
330 /*
331 * Find out the card's support bits with a mode 0 operation.
332 * The argument does not matter, as the support bits do not
333 * change with the arguments.
334 */
4c0a6a0a 335 err = mmc_sd_switch(card, SD_SWITCH_CHECK, 0, 0, status);
17b0429d 336 if (err) {
013909c4
AN
337 /*
338 * If the host or the card can't do the switch,
339 * fail more gracefully.
340 */
341 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
adf66a0d
PO
342 goto out;
343
6606110d 344 pr_warn("%s: problem reading Bus Speed modes\n",
3373c0ae 345 mmc_hostname(card->host));
17b0429d 346 err = 0;
adf66a0d 347
7ea239d9
PO
348 goto out;
349 }
350
fffe5d5a
QL
351 if (status[13] & SD_MODE_HIGH_SPEED)
352 card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
f2815f68 353
013909c4
AN
354 if (card->scr.sda_spec3) {
355 card->sw_caps.sd3_bus_mode = status[13];
a39ca6ae 356 /* Driver Strengths supported by the card */
013909c4 357 card->sw_caps.sd3_drv_type = status[9];
d9812780 358 card->sw_caps.sd3_curr_limit = status[7] | status[6] << 8;
013909c4 359 }
7ea239d9 360
1addfcdb
PO
361out:
362 kfree(status);
363
364 return err;
365}
366
367/*
368 * Test if the card supports high-speed mode and, if so, switch to it.
369 */
71578a1e 370int mmc_sd_switch_hs(struct mmc_card *card)
1addfcdb
PO
371{
372 int err;
373 u8 *status;
374
3373c0ae 375 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
17b0429d 376 return 0;
3373c0ae
PO
377
378 if (!(card->csd.cmdclass & CCC_SWITCH))
17b0429d 379 return 0;
3373c0ae 380
1addfcdb 381 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
17b0429d 382 return 0;
1addfcdb
PO
383
384 if (card->sw_caps.hs_max_dtr == 0)
17b0429d 385 return 0;
1addfcdb 386
1addfcdb 387 status = kmalloc(64, GFP_KERNEL);
e1df7ae3 388 if (!status)
17b0429d 389 return -ENOMEM;
1addfcdb 390
4c0a6a0a
CL
391 err = mmc_sd_switch(card, SD_SWITCH_SET, 0,
392 HIGH_SPEED_BUS_SPEED, status);
17b0429d 393 if (err)
7ea239d9
PO
394 goto out;
395
9d624f4f 396 if ((status[16] & 0xF) != HIGH_SPEED_BUS_SPEED) {
6606110d 397 pr_warn("%s: Problem switching card into high-speed mode!\n",
7ea239d9 398 mmc_hostname(card->host));
71578a1e 399 err = 0;
7ea239d9 400 } else {
71578a1e 401 err = 1;
7ea239d9
PO
402 }
403
404out:
405 kfree(status);
406
407 return err;
408}
409
d6d50a15
AN
410static int sd_select_driver_type(struct mmc_card *card, u8 *status)
411{
fa021cef 412 int card_drv_type, drive_strength, drv_type;
d6d50a15
AN
413 int err;
414
3853a042
AH
415 card->drive_strength = 0;
416
fa021cef 417 card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
ca8e99b3 418
e23350b3
AH
419 drive_strength = mmc_select_drive_strength(card,
420 card->sw_caps.uhs_max_dtr,
421 card_drv_type, &drv_type);
d6d50a15 422
b4f30a17 423 if (drive_strength) {
4c0a6a0a
CL
424 err = mmc_sd_switch(card, SD_SWITCH_SET, 2,
425 drive_strength, status);
b4f30a17
AH
426 if (err)
427 return err;
428 if ((status[15] & 0xF) != drive_strength) {
429 pr_warn("%s: Problem setting drive strength!\n",
430 mmc_hostname(card->host));
431 return 0;
432 }
3853a042 433 card->drive_strength = drive_strength;
d6d50a15
AN
434 }
435
b4f30a17
AH
436 if (drv_type)
437 mmc_set_driver_type(card->host, drv_type);
d6d50a15
AN
438
439 return 0;
440}
441
93c712f9 442static void sd_update_bus_speed_mode(struct mmc_card *card)
49c468fc 443{
49c468fc
AN
444 /*
445 * If the host doesn't support any of the UHS-I modes, fallback on
446 * default speed.
447 */
3f8a7fab 448 if (!mmc_host_uhs(card->host)) {
93c712f9
SJ
449 card->sd_bus_speed = 0;
450 return;
451 }
49c468fc
AN
452
453 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
454 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
93c712f9 455 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
49c468fc
AN
456 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
457 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
93c712f9 458 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
49c468fc
AN
459 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
460 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
461 SD_MODE_UHS_SDR50)) {
93c712f9 462 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
49c468fc
AN
463 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
464 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
465 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
93c712f9 466 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
49c468fc
AN
467 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
468 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
469 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
470 SD_MODE_UHS_SDR12)) {
93c712f9
SJ
471 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
472 }
473}
474
475static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
476{
477 int err;
478 unsigned int timing = 0;
479
480 switch (card->sd_bus_speed) {
481 case UHS_SDR104_BUS_SPEED:
482 timing = MMC_TIMING_UHS_SDR104;
483 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
484 break;
485 case UHS_DDR50_BUS_SPEED:
486 timing = MMC_TIMING_UHS_DDR50;
487 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
488 break;
489 case UHS_SDR50_BUS_SPEED:
490 timing = MMC_TIMING_UHS_SDR50;
491 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
492 break;
493 case UHS_SDR25_BUS_SPEED:
494 timing = MMC_TIMING_UHS_SDR25;
495 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
496 break;
497 case UHS_SDR12_BUS_SPEED:
498 timing = MMC_TIMING_UHS_SDR12;
499 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
500 break;
501 default:
502 return 0;
49c468fc
AN
503 }
504
4c0a6a0a 505 err = mmc_sd_switch(card, SD_SWITCH_SET, 0, card->sd_bus_speed, status);
49c468fc
AN
506 if (err)
507 return err;
508
93c712f9 509 if ((status[16] & 0xF) != card->sd_bus_speed)
6606110d 510 pr_warn("%s: Problem setting bus speed mode!\n",
49c468fc
AN
511 mmc_hostname(card->host));
512 else {
513 mmc_set_timing(card->host, timing);
514 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
515 }
516
517 return 0;
518}
519
55c4665e
AL
520/* Get host's max current setting at its current voltage */
521static u32 sd_get_host_max_current(struct mmc_host *host)
522{
523 u32 voltage, max_current;
524
525 voltage = 1 << host->ios.vdd;
526 switch (voltage) {
527 case MMC_VDD_165_195:
528 max_current = host->max_current_180;
529 break;
530 case MMC_VDD_29_30:
531 case MMC_VDD_30_31:
532 max_current = host->max_current_300;
533 break;
534 case MMC_VDD_32_33:
535 case MMC_VDD_33_34:
536 max_current = host->max_current_330;
537 break;
538 default:
539 max_current = 0;
540 }
541
542 return max_current;
543}
544
5371c927
AN
545static int sd_set_current_limit(struct mmc_card *card, u8 *status)
546{
0aa67700 547 int current_limit = SD_SET_CURRENT_NO_CHANGE;
5371c927 548 int err;
55c4665e 549 u32 max_current;
5371c927
AN
550
551 /*
552 * Current limit switch is only defined for SDR50, SDR104, and DDR50
0aa67700
PR
553 * bus speed modes. For other bus speed modes, we do not change the
554 * current limit.
55c4665e
AL
555 */
556 if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) &&
557 (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) &&
558 (card->sd_bus_speed != UHS_DDR50_BUS_SPEED))
559 return 0;
560
561 /*
562 * Host has different current capabilities when operating at
563 * different voltages, so find out its max current first.
564 */
565 max_current = sd_get_host_max_current(card->host);
566
567 /*
a39ca6ae
AL
568 * We only check host's capability here, if we set a limit that is
569 * higher than the card's maximum current, the card will be using its
570 * maximum current, e.g. if the card's maximum current is 300ma, and
571 * when we set current limit to 200ma, the card will draw 200ma, and
572 * when we set current limit to 400/600/800ma, the card will draw its
573 * maximum 300ma from the host.
d9812780
RK
574 *
575 * The above is incorrect: if we try to set a current limit that is
576 * not supported by the card, the card can rightfully error out the
577 * attempt, and remain at the default current limit. This results
578 * in a 300mA card being limited to 200mA even though the host
579 * supports 800mA. Failures seen with SanDisk 8GB UHS cards with
580 * an iMX6 host. --rmk
5371c927 581 */
d9812780
RK
582 if (max_current >= 800 &&
583 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
55c4665e 584 current_limit = SD_SET_CURRENT_LIMIT_800;
d9812780
RK
585 else if (max_current >= 600 &&
586 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
55c4665e 587 current_limit = SD_SET_CURRENT_LIMIT_600;
d9812780
RK
588 else if (max_current >= 400 &&
589 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
55c4665e 590 current_limit = SD_SET_CURRENT_LIMIT_400;
d9812780
RK
591 else if (max_current >= 200 &&
592 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
55c4665e 593 current_limit = SD_SET_CURRENT_LIMIT_200;
5371c927 594
0aa67700 595 if (current_limit != SD_SET_CURRENT_NO_CHANGE) {
4c0a6a0a
CL
596 err = mmc_sd_switch(card, SD_SWITCH_SET, 3,
597 current_limit, status);
0aa67700
PR
598 if (err)
599 return err;
5371c927 600
0aa67700 601 if (((status[15] >> 4) & 0x0F) != current_limit)
6606110d 602 pr_warn("%s: Problem setting current limit!\n",
0aa67700 603 mmc_hostname(card->host));
5371c927 604
0aa67700 605 }
a39ca6ae 606
5371c927
AN
607 return 0;
608}
609
d6d50a15
AN
610/*
611 * UHS-I specific initialization procedure
612 */
613static int mmc_sd_init_uhs_card(struct mmc_card *card)
614{
615 int err;
616 u8 *status;
617
d6d50a15
AN
618 if (!(card->csd.cmdclass & CCC_SWITCH))
619 return 0;
620
621 status = kmalloc(64, GFP_KERNEL);
e1df7ae3 622 if (!status)
d6d50a15 623 return -ENOMEM;
d6d50a15
AN
624
625 /* Set 4-bit bus width */
d6743a8a
SL
626 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
627 if (err)
628 goto out;
d6d50a15 629
d6743a8a 630 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
d6d50a15 631
93c712f9
SJ
632 /*
633 * Select the bus speed mode depending on host
634 * and card capability.
635 */
636 sd_update_bus_speed_mode(card);
637
d6d50a15
AN
638 /* Set the driver strength for the card */
639 err = sd_select_driver_type(card, status);
49c468fc
AN
640 if (err)
641 goto out;
642
93c712f9
SJ
643 /* Set current limit for the card */
644 err = sd_set_current_limit(card, status);
5371c927
AN
645 if (err)
646 goto out;
647
93c712f9
SJ
648 /* Set bus speed mode of the card */
649 err = sd_set_bus_speed_mode(card, status);
b513ea25
AN
650 if (err)
651 goto out;
652
810e08ee
FS
653 /*
654 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
655 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
656 */
63e415c6 657 if (!mmc_host_is_spi(card->host) &&
e10c3219
CC
658 (card->host->ios.timing == MMC_TIMING_UHS_SDR50 ||
659 card->host->ios.timing == MMC_TIMING_UHS_DDR50 ||
660 card->host->ios.timing == MMC_TIMING_UHS_SDR104)) {
63e415c6 661 err = mmc_execute_tuning(card);
4324f6de
WY
662
663 /*
664 * As SD Specifications Part1 Physical Layer Specification
665 * Version 3.01 says, CMD19 tuning is available for unlocked
666 * cards in transfer state of 1.8V signaling mode. The small
667 * difference between v3.00 and 3.01 spec means that CMD19
668 * tuning is also available for DDR50 mode.
669 */
e10c3219 670 if (err && card->host->ios.timing == MMC_TIMING_UHS_DDR50) {
4324f6de
WY
671 pr_warn("%s: ddr50 tuning failed\n",
672 mmc_hostname(card->host));
673 err = 0;
674 }
675 }
676
d6d50a15
AN
677out:
678 kfree(status);
679
680 return err;
681}
682
51ec92e2
PO
683MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
684 card->raw_cid[2], card->raw_cid[3]);
685MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
686 card->raw_csd[2], card->raw_csd[3]);
687MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
5275a652
UY
688MMC_DEV_ATTR(ssr,
689 "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x\n",
690 card->raw_ssr[0], card->raw_ssr[1], card->raw_ssr[2],
691 card->raw_ssr[3], card->raw_ssr[4], card->raw_ssr[5],
692 card->raw_ssr[6], card->raw_ssr[7], card->raw_ssr[8],
693 card->raw_ssr[9], card->raw_ssr[10], card->raw_ssr[11],
694 card->raw_ssr[12], card->raw_ssr[13], card->raw_ssr[14],
695 card->raw_ssr[15]);
51ec92e2 696MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
dfe86cba
AH
697MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
698MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
51ec92e2
PO
699MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
700MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
701MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
702MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
703MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
704MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
c892b0d8 705MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
974e85e9 706MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
51ec92e2
PO
707
708
f5d8a5fe
SS
709static ssize_t mmc_dsr_show(struct device *dev, struct device_attribute *attr,
710 char *buf)
6825a606 711{
f5d8a5fe
SS
712 struct mmc_card *card = mmc_dev_to_card(dev);
713 struct mmc_host *host = card->host;
714
715 if (card->csd.dsr_imp && host->dsr_req)
716 return sysfs_emit(buf, "0x%x\n", host->dsr);
717 /* return default DSR value */
718 return sysfs_emit(buf, "0x%x\n", 0x404);
6825a606
BP
719}
720
721static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
722
254e1754
PR
723MMC_DEV_ATTR(vendor, "0x%04x\n", card->cis.vendor);
724MMC_DEV_ATTR(device, "0x%04x\n", card->cis.device);
b698f6ab
PR
725MMC_DEV_ATTR(revision, "%u.%u\n", card->major_rev, card->minor_rev);
726
727#define sdio_info_attr(num) \
728static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf) \
729{ \
730 struct mmc_card *card = mmc_dev_to_card(dev); \
731 \
732 if (num > card->num_info) \
733 return -ENODATA; \
f5d8a5fe 734 if (!card->info[num - 1][0]) \
b698f6ab 735 return 0; \
f5d8a5fe 736 return sysfs_emit(buf, "%s\n", card->info[num - 1]); \
b698f6ab
PR
737} \
738static DEVICE_ATTR_RO(info##num)
739
740sdio_info_attr(1);
741sdio_info_attr(2);
742sdio_info_attr(3);
743sdio_info_attr(4);
254e1754 744
51ec92e2 745static struct attribute *sd_std_attrs[] = {
254e1754
PR
746 &dev_attr_vendor.attr,
747 &dev_attr_device.attr,
b698f6ab
PR
748 &dev_attr_revision.attr,
749 &dev_attr_info1.attr,
750 &dev_attr_info2.attr,
751 &dev_attr_info3.attr,
752 &dev_attr_info4.attr,
51ec92e2
PO
753 &dev_attr_cid.attr,
754 &dev_attr_csd.attr,
755 &dev_attr_scr.attr,
5275a652 756 &dev_attr_ssr.attr,
51ec92e2 757 &dev_attr_date.attr,
dfe86cba
AH
758 &dev_attr_erase_size.attr,
759 &dev_attr_preferred_erase_size.attr,
51ec92e2
PO
760 &dev_attr_fwrev.attr,
761 &dev_attr_hwrev.attr,
762 &dev_attr_manfid.attr,
763 &dev_attr_name.attr,
764 &dev_attr_oemid.attr,
765 &dev_attr_serial.attr,
5fb06af7 766 &dev_attr_ocr.attr,
974e85e9 767 &dev_attr_rca.attr,
6825a606 768 &dev_attr_dsr.attr,
51ec92e2
PO
769 NULL,
770};
254e1754
PR
771
772static umode_t sd_std_is_visible(struct kobject *kobj, struct attribute *attr,
773 int index)
774{
e449d983 775 struct device *dev = kobj_to_dev(kobj);
254e1754
PR
776 struct mmc_card *card = mmc_dev_to_card(dev);
777
b698f6ab
PR
778 /* CIS vendor and device ids, revision and info string are available only for Combo cards */
779 if ((attr == &dev_attr_vendor.attr ||
780 attr == &dev_attr_device.attr ||
781 attr == &dev_attr_revision.attr ||
782 attr == &dev_attr_info1.attr ||
783 attr == &dev_attr_info2.attr ||
784 attr == &dev_attr_info3.attr ||
785 attr == &dev_attr_info4.attr
3beb0ab5 786 ) &&!mmc_card_sd_combo(card))
254e1754
PR
787 return 0;
788
789 return attr->mode;
790}
791
792static const struct attribute_group sd_std_group = {
793 .attrs = sd_std_attrs,
794 .is_visible = sd_std_is_visible,
795};
796__ATTRIBUTE_GROUPS(sd_std);
51ec92e2 797
68f5630a 798const struct device_type sd_type = {
d1e58212 799 .groups = sd_std_groups,
51ec92e2
PO
800};
801
7ea239d9 802/*
71578a1e 803 * Fetch CID from card.
7ea239d9 804 */
d6d50a15 805int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
7ea239d9 806{
7ea239d9 807 int err;
55c4665e 808 u32 max_current;
0797e5f1 809 int retries = 10;
69041150 810 u32 pocr = ocr;
0797e5f1
JR
811
812try_again:
813 if (!retries) {
814 ocr &= ~SD_OCR_S18R;
6606110d 815 pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
0797e5f1 816 }
7ea239d9 817
7ea239d9
PO
818 /*
819 * Since we're changing the OCR value, we seem to
820 * need to tell some cards to go back to the idle
821 * state. We wait 1ms to give cards time to
822 * respond.
823 */
824 mmc_go_idle(host);
825
826 /*
827 * If SD_SEND_IF_COND indicates an SD 2.0
828 * compliant card and we should set bit 30
829 * of the ocr to indicate that we can handle
830 * block-addressed SDHC cards.
831 */
6abaa0c9 832 err = mmc_send_if_cond(host, ocr);
17b0429d 833 if (!err)
f2119df6 834 ocr |= SD_OCR_CCS;
7ea239d9 835
f2119df6
AN
836 /*
837 * If the host supports one of UHS-I modes, request the card
0797e5f1
JR
838 * to switch to 1.8V signaling level. If the card has failed
839 * repeatedly to switch however, skip this.
f2119df6 840 */
0797e5f1 841 if (retries && mmc_host_uhs(host))
f2119df6
AN
842 ocr |= SD_OCR_S18R;
843
55c4665e
AL
844 /*
845 * If the host can supply more than 150mA at current voltage,
846 * XPC should be set to 1.
847 */
848 max_current = sd_get_host_max_current(host);
849 if (max_current > 150)
f2119df6
AN
850 ocr |= SD_OCR_XPC;
851
d6d50a15 852 err = mmc_send_app_op_cond(host, ocr, rocr);
17b0429d 853 if (err)
71578a1e 854 return err;
7ea239d9 855
f2119df6 856 /*
09247e11
CL
857 * In case the S18A bit is set in the response, let's start the signal
858 * voltage switch procedure. SPI mode doesn't support CMD11.
859 * Note that, according to the spec, the S18A bit is not valid unless
860 * the CCS bit is set as well. We deliberately deviate from the spec in
861 * regards to this, which allows UHS-I to be supported for SDSC cards.
f2119df6 862 */
e9233917
BN
863 if (!mmc_host_is_spi(host) && (ocr & SD_OCR_S18R) &&
864 rocr && (*rocr & SD_ROCR_S18A)) {
2ed573b6 865 err = mmc_set_uhs_voltage(host, pocr);
0797e5f1
JR
866 if (err == -EAGAIN) {
867 retries--;
868 goto try_again;
869 } else if (err) {
870 retries = 0;
f2119df6
AN
871 goto try_again;
872 }
873 }
874
a1473732 875 err = mmc_send_cid(host, cid);
71578a1e
MM
876 return err;
877}
878
bd7342fe 879int mmc_sd_get_csd(struct mmc_card *card)
71578a1e
MM
880{
881 int err;
882
883 /*
884 * Fetch CSD from card.
885 */
886 err = mmc_send_csd(card, card->raw_csd);
17b0429d 887 if (err)
71578a1e 888 return err;
7ea239d9 889
71578a1e
MM
890 err = mmc_decode_csd(card);
891 if (err)
892 return err;
893
894 return 0;
895}
896
9f6e0bff
LPC
897static int mmc_sd_get_ro(struct mmc_host *host)
898{
899 int ro;
900
901 /*
902 * Some systems don't feature a write-protect pin and don't need one.
903 * E.g. because they only have micro-SD card slot. For those systems
904 * assume that the SD card is always read-write.
905 */
906 if (host->caps2 & MMC_CAP2_NO_WRITE_PROTECT)
907 return 0;
908
909 if (!host->ops->get_ro)
910 return -1;
911
9f6e0bff 912 ro = host->ops->get_ro(host);
9f6e0bff
LPC
913
914 return ro;
915}
916
71578a1e
MM
917int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
918 bool reinit)
919{
920 int err;
921
922 if (!reinit) {
923 /*
924 * Fetch SCR from card.
925 */
06c9ccb7 926 err = mmc_app_send_scr(card);
71578a1e
MM
927 if (err)
928 return err;
929
930 err = mmc_decode_scr(card);
931 if (err)
932 return err;
933
dfe86cba
AH
934 /*
935 * Fetch and process SD Status register.
936 */
937 err = mmc_read_ssr(card);
938 if (err)
939 return err;
940
941 /* Erase init depends on CSD and SSR */
942 mmc_init_erase(card);
71578a1e
MM
943 }
944
63f15609
AH
945 /*
946 * Fetch switch information from card. Note, sd3_bus_mode can change if
947 * voltage switch outcome changes, so do this always.
948 */
949 err = mmc_read_switch(card);
950 if (err)
951 return err;
952
71578a1e
MM
953 /*
954 * For SPI, enable CRC as appropriate.
955 * This CRC enable is located AFTER the reading of the
956 * card registers because some SDHC cards are not able
957 * to provide valid CRCs for non-512-byte blocks.
958 */
959 if (mmc_host_is_spi(host)) {
960 err = mmc_spi_set_crc(host, use_spi_crc);
961 if (err)
962 return err;
963 }
964
965 /*
966 * Check if read-only switch is active.
967 */
968 if (!reinit) {
9f6e0bff 969 int ro = mmc_sd_get_ro(host);
71578a1e
MM
970
971 if (ro < 0) {
6606110d 972 pr_warn("%s: host does not support reading read-only switch, assuming write-enable\n",
71578a1e
MM
973 mmc_hostname(host));
974 } else if (ro > 0) {
975 mmc_card_set_readonly(card);
adf66a0d 976 }
71578a1e
MM
977 }
978
979 return 0;
980}
981
982unsigned mmc_sd_get_max_clock(struct mmc_card *card)
983{
984 unsigned max_dtr = (unsigned int)-1;
985
cdc99179 986 if (mmc_card_hs(card)) {
71578a1e
MM
987 if (max_dtr > card->sw_caps.hs_max_dtr)
988 max_dtr = card->sw_caps.hs_max_dtr;
989 } else if (max_dtr > card->csd.max_dtr) {
990 max_dtr = card->csd.max_dtr;
991 }
992
993 return max_dtr;
994}
995
6a11fc47
AH
996static bool mmc_sd_card_using_v18(struct mmc_card *card)
997{
998 /*
999 * According to the SD spec., the Bus Speed Mode (function group 1) bits
1000 * 2 to 4 are zero if the card is initialized at 3.3V signal level. Thus
1001 * they can be used to determine if the card has already switched to
1002 * 1.8V signaling.
1003 */
1004 return card->sw_caps.sd3_bus_mode &
1005 (SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR104 | SD_MODE_UHS_DDR50);
1006}
1007
2c5d4276
UH
1008static int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
1009 u8 reg_data)
1010{
1011 struct mmc_host *host = card->host;
1012 struct mmc_request mrq = {};
1013 struct mmc_command cmd = {};
1014 struct mmc_data data = {};
1015 struct scatterlist sg;
1016 u8 *reg_buf;
1017
1018 reg_buf = kzalloc(512, GFP_KERNEL);
1019 if (!reg_buf)
1020 return -ENOMEM;
1021
1022 mrq.cmd = &cmd;
1023 mrq.data = &data;
1024
1025 /*
1026 * Arguments of CMD49:
1027 * [31:31] MIO (0 = memory).
1028 * [30:27] FNO (function number).
1029 * [26:26] MW - mask write mode (0 = disable).
1030 * [25:18] page number.
1031 * [17:9] offset address.
1032 * [8:0] length (0 = 1 byte).
1033 */
1034 cmd.arg = fno << 27 | page << 18 | offset << 9;
1035
1036 /* The first byte in the buffer is the data to be written. */
1037 reg_buf[0] = reg_data;
1038
1039 data.flags = MMC_DATA_WRITE;
1040 data.blksz = 512;
1041 data.blocks = 1;
1042 data.sg = &sg;
1043 data.sg_len = 1;
1044 sg_init_one(&sg, reg_buf, 512);
1045
1046 cmd.opcode = SD_WRITE_EXTR_SINGLE;
1047 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1048
1049 mmc_set_data_timeout(&data, card);
1050 mmc_wait_for_req(host, &mrq);
1051
1052 kfree(reg_buf);
1053
1054 /*
1055 * Note that, the SD card is allowed to signal busy on DAT0 up to 1s
1056 * after the CMD49. Although, let's leave this to be managed by the
1057 * caller.
1058 */
1059
1060 if (cmd.error)
1061 return cmd.error;
1062 if (data.error)
1063 return data.error;
1064
1065 return 0;
1066}
1067
c784f927
UH
1068static int sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page,
1069 u16 offset, u16 len, u8 *reg_buf)
1070{
1071 u32 cmd_args;
1072
1073 /*
1074 * Command arguments of CMD48:
1075 * [31:31] MIO (0 = memory).
1076 * [30:27] FNO (function number).
1077 * [26:26] reserved (0).
1078 * [25:18] page number.
1079 * [17:9] offset address.
1080 * [8:0] length (0 = 1 byte, 1ff = 512 bytes).
1081 */
1082 cmd_args = fno << 27 | page << 18 | offset << 9 | (len -1);
1083
1084 return mmc_send_adtc_data(card, card->host, SD_READ_EXTR_SINGLE,
1085 cmd_args, reg_buf, 512);
1086}
1087
1088static int sd_parse_ext_reg_power(struct mmc_card *card, u8 fno, u8 page,
1089 u16 offset)
1090{
1091 int err;
1092 u8 *reg_buf;
1093
1094 reg_buf = kzalloc(512, GFP_KERNEL);
1095 if (!reg_buf)
1096 return -ENOMEM;
1097
1098 /* Read the extension register for power management function. */
1099 err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
1100 if (err) {
1101 pr_warn("%s: error %d reading PM func of ext reg\n",
1102 mmc_hostname(card->host), err);
1103 goto out;
1104 }
1105
1106 /* PM revision consists of 4 bits. */
1107 card->ext_power.rev = reg_buf[0] & 0xf;
1108
1109 /* Power Off Notification support at bit 4. */
1110 if (reg_buf[1] & BIT(4))
1111 card->ext_power.feature_support |= SD_EXT_POWER_OFF_NOTIFY;
1112
1113 /* Power Sustenance support at bit 5. */
1114 if (reg_buf[1] & BIT(5))
1115 card->ext_power.feature_support |= SD_EXT_POWER_SUSTENANCE;
1116
1117 /* Power Down Mode support at bit 6. */
1118 if (reg_buf[1] & BIT(6))
1119 card->ext_power.feature_support |= SD_EXT_POWER_DOWN_MODE;
1120
1121 card->ext_power.fno = fno;
1122 card->ext_power.page = page;
1123 card->ext_power.offset = offset;
1124
1125out:
1126 kfree(reg_buf);
1127 return err;
1128}
1129
4e6306e0
UH
1130static int sd_parse_ext_reg_perf(struct mmc_card *card, u8 fno, u8 page,
1131 u16 offset)
1132{
1133 int err;
1134 u8 *reg_buf;
1135
1136 reg_buf = kzalloc(512, GFP_KERNEL);
1137 if (!reg_buf)
1138 return -ENOMEM;
1139
1140 err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
1141 if (err) {
1142 pr_warn("%s: error %d reading PERF func of ext reg\n",
1143 mmc_hostname(card->host), err);
1144 goto out;
1145 }
1146
1147 /* PERF revision. */
1148 card->ext_perf.rev = reg_buf[0];
1149
1150 /* FX_EVENT support at bit 0. */
1151 if (reg_buf[1] & BIT(0))
1152 card->ext_perf.feature_support |= SD_EXT_PERF_FX_EVENT;
1153
1154 /* Card initiated self-maintenance support at bit 0. */
1155 if (reg_buf[2] & BIT(0))
1156 card->ext_perf.feature_support |= SD_EXT_PERF_CARD_MAINT;
1157
1158 /* Host initiated self-maintenance support at bit 1. */
1159 if (reg_buf[2] & BIT(1))
1160 card->ext_perf.feature_support |= SD_EXT_PERF_HOST_MAINT;
1161
1162 /* Cache support at bit 0. */
c467c8f0 1163 if ((reg_buf[4] & BIT(0)) && !mmc_card_broken_sd_cache(card))
4e6306e0
UH
1164 card->ext_perf.feature_support |= SD_EXT_PERF_CACHE;
1165
1166 /* Command queue support indicated via queue depth bits (0 to 4). */
1167 if (reg_buf[6] & 0x1f)
1168 card->ext_perf.feature_support |= SD_EXT_PERF_CMD_QUEUE;
1169
1170 card->ext_perf.fno = fno;
1171 card->ext_perf.page = page;
1172 card->ext_perf.offset = offset;
1173
1174out:
1175 kfree(reg_buf);
1176 return err;
1177}
1178
c784f927
UH
1179static int sd_parse_ext_reg(struct mmc_card *card, u8 *gen_info_buf,
1180 u16 *next_ext_addr)
1181{
1182 u8 num_regs, fno, page;
1183 u16 sfc, offset, ext = *next_ext_addr;
1184 u32 reg_addr;
1185
1186 /*
1187 * Parse only one register set per extension, as that is sufficient to
1188 * support the standard functions. This means another 48 bytes in the
1189 * buffer must be available.
1190 */
1191 if (ext + 48 > 512)
1192 return -EFAULT;
1193
1194 /* Standard Function Code */
1195 memcpy(&sfc, &gen_info_buf[ext], 2);
1196
1197 /* Address to the next extension. */
1198 memcpy(next_ext_addr, &gen_info_buf[ext + 40], 2);
1199
1200 /* Number of registers for this extension. */
1201 num_regs = gen_info_buf[ext + 42];
1202
1203 /* We support only one register per extension. */
1204 if (num_regs != 1)
1205 return 0;
1206
1207 /* Extension register address. */
1208 memcpy(&reg_addr, &gen_info_buf[ext + 44], 4);
1209
1210 /* 9 bits (0 to 8) contains the offset address. */
1211 offset = reg_addr & 0x1ff;
1212
1213 /* 8 bits (9 to 16) contains the page number. */
1214 page = reg_addr >> 9 & 0xff ;
1215
1216 /* 4 bits (18 to 21) contains the function number. */
1217 fno = reg_addr >> 18 & 0xf;
1218
1219 /* Standard Function Code for power management. */
1220 if (sfc == 0x1)
1221 return sd_parse_ext_reg_power(card, fno, page, offset);
1222
4e6306e0
UH
1223 /* Standard Function Code for performance enhancement. */
1224 if (sfc == 0x2)
1225 return sd_parse_ext_reg_perf(card, fno, page, offset);
1226
c784f927
UH
1227 return 0;
1228}
1229
1230static int sd_read_ext_regs(struct mmc_card *card)
1231{
1232 int err, i;
1233 u8 num_ext, *gen_info_buf;
1234 u16 rev, len, next_ext_addr;
1235
1236 if (mmc_host_is_spi(card->host))
1237 return 0;
1238
1239 if (!(card->scr.cmds & SD_SCR_CMD48_SUPPORT))
1240 return 0;
1241
1242 gen_info_buf = kzalloc(512, GFP_KERNEL);
1243 if (!gen_info_buf)
1244 return -ENOMEM;
1245
1246 /*
1247 * Read 512 bytes of general info, which is found at function number 0,
1248 * at page 0 and with no offset.
1249 */
1250 err = sd_read_ext_reg(card, 0, 0, 0, 512, gen_info_buf);
1251 if (err) {
fc02e2b5 1252 pr_err("%s: error %d reading general info of SD ext reg\n",
c784f927
UH
1253 mmc_hostname(card->host), err);
1254 goto out;
1255 }
1256
1257 /* General info structure revision. */
1258 memcpy(&rev, &gen_info_buf[0], 2);
1259
1260 /* Length of general info in bytes. */
1261 memcpy(&len, &gen_info_buf[2], 2);
1262
1263 /* Number of extensions to be find. */
1264 num_ext = gen_info_buf[4];
1265
fc02e2b5
ZL
1266 /*
1267 * We only support revision 0 and limit it to 512 bytes for simplicity.
1268 * No matter what, let's return zero to allow us to continue using the
1269 * card, even if we can't support the features from the SD function
1270 * extensions registers.
1271 */
c784f927
UH
1272 if (rev != 0 || len > 512) {
1273 pr_warn("%s: non-supported SD ext reg layout\n",
1274 mmc_hostname(card->host));
1275 goto out;
1276 }
1277
1278 /*
1279 * Parse the extension registers. The first extension should start
1280 * immediately after the general info header (16 bytes).
1281 */
1282 next_ext_addr = 16;
1283 for (i = 0; i < num_ext; i++) {
1284 err = sd_parse_ext_reg(card, gen_info_buf, &next_ext_addr);
1285 if (err) {
fc02e2b5 1286 pr_err("%s: error %d parsing SD ext reg\n",
c784f927
UH
1287 mmc_hostname(card->host), err);
1288 goto out;
1289 }
1290 }
1291
1292out:
1293 kfree(gen_info_buf);
1294 return err;
1295}
1296
130206a6
UH
1297static bool sd_cache_enabled(struct mmc_host *host)
1298{
1299 return host->card->ext_perf.feature_enabled & SD_EXT_PERF_CACHE;
1300}
1301
1302static int sd_flush_cache(struct mmc_host *host)
1303{
1304 struct mmc_card *card = host->card;
1305 u8 *reg_buf, fno, page;
1306 u16 offset;
1307 int err;
1308
1309 if (!sd_cache_enabled(host))
1310 return 0;
1311
1312 reg_buf = kzalloc(512, GFP_KERNEL);
1313 if (!reg_buf)
1314 return -ENOMEM;
1315
1316 /*
1317 * Set Flush Cache at bit 0 in the performance enhancement register at
1318 * 261 bytes offset.
1319 */
1320 fno = card->ext_perf.fno;
1321 page = card->ext_perf.page;
1322 offset = card->ext_perf.offset + 261;
1323
1324 err = sd_write_ext_reg(card, fno, page, offset, BIT(0));
1325 if (err) {
1326 pr_warn("%s: error %d writing Cache Flush bit\n",
1327 mmc_hostname(host), err);
1328 goto out;
1329 }
1330
1331 err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1332 MMC_BUSY_EXTR_SINGLE);
1333 if (err)
1334 goto out;
1335
1336 /*
1337 * Read the Flush Cache bit. The card shall reset it, to confirm that
1338 * it's has completed the flushing of the cache.
1339 */
1340 err = sd_read_ext_reg(card, fno, page, offset, 1, reg_buf);
1341 if (err) {
1342 pr_warn("%s: error %d reading Cache Flush bit\n",
1343 mmc_hostname(host), err);
1344 goto out;
1345 }
1346
1347 if (reg_buf[0] & BIT(0))
1348 err = -ETIMEDOUT;
1349out:
1350 kfree(reg_buf);
1351 return err;
1352}
1353
1354static int sd_enable_cache(struct mmc_card *card)
1355{
1356 u8 *reg_buf;
1357 int err;
1358
1359 card->ext_perf.feature_enabled &= ~SD_EXT_PERF_CACHE;
1360
1361 reg_buf = kzalloc(512, GFP_KERNEL);
1362 if (!reg_buf)
1363 return -ENOMEM;
1364
1365 /*
1366 * Set Cache Enable at bit 0 in the performance enhancement register at
1367 * 260 bytes offset.
1368 */
1369 err = sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
1370 card->ext_perf.offset + 260, BIT(0));
1371 if (err) {
1372 pr_warn("%s: error %d writing Cache Enable bit\n",
1373 mmc_hostname(card->host), err);
1374 goto out;
1375 }
1376
1377 err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1378 MMC_BUSY_EXTR_SINGLE);
1379 if (!err)
1380 card->ext_perf.feature_enabled |= SD_EXT_PERF_CACHE;
1381
1382out:
1383 kfree(reg_buf);
1384 return err;
1385}
1386
71578a1e
MM
1387/*
1388 * Handle the detection and initialisation of a card.
1389 *
1390 * In the case of a resume, "oldcard" will contain the card
1391 * we're trying to reinitialise.
1392 */
1393static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
1394 struct mmc_card *oldcard)
1395{
1396 struct mmc_card *card;
1397 int err;
1398 u32 cid[4];
d6d50a15 1399 u32 rocr = 0;
6a11fc47 1400 bool v18_fixup_failed = false;
71578a1e 1401
71578a1e 1402 WARN_ON(!host->claimed);
6a11fc47 1403retry:
d6d50a15 1404 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
71578a1e
MM
1405 if (err)
1406 return err;
1407
1408 if (oldcard) {
099b6481 1409 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1410 pr_debug("%s: Perhaps the card was replaced\n",
1411 mmc_hostname(host));
71578a1e 1412 return -ENOENT;
099b6481 1413 }
7ea239d9 1414
6abaa0c9
PO
1415 card = oldcard;
1416 } else {
1417 /*
1418 * Allocate card structure.
1419 */
51ec92e2 1420 card = mmc_alloc_card(host, &sd_type);
71578a1e
MM
1421 if (IS_ERR(card))
1422 return PTR_ERR(card);
6abaa0c9 1423
69041150 1424 card->ocr = ocr;
6abaa0c9
PO
1425 card->type = MMC_TYPE_SD;
1426 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1427 }
7ea239d9 1428
eac86321
DA
1429 /*
1430 * Call the optional HC's init_card function to handle quirks.
1431 */
1432 if (host->ops->init_card)
1433 host->ops->init_card(host, card);
1434
7ea239d9 1435 /*
af517150 1436 * For native busses: get card RCA and quit open drain mode.
7ea239d9 1437 */
af517150
DB
1438 if (!mmc_host_is_spi(host)) {
1439 err = mmc_send_relative_addr(host, &card->rca);
1440 if (err)
7fca9675 1441 goto free_card;
af517150 1442 }
7ea239d9 1443
6abaa0c9 1444 if (!oldcard) {
bd7342fe 1445 err = mmc_sd_get_csd(card);
17b0429d 1446 if (err)
7fca9675 1447 goto free_card;
bd766312 1448
6abaa0c9
PO
1449 mmc_decode_cid(card);
1450 }
7ea239d9 1451
3d705d14
SH
1452 /*
1453 * handling only for cards supporting DSR and hosts requesting
1454 * DSR configuration
1455 */
1456 if (card->csd.dsr_imp && host->dsr_req)
1457 mmc_set_dsr(host);
1458
7ea239d9 1459 /*
6abaa0c9 1460 * Select card, as all following commands rely on that.
7ea239d9 1461 */
af517150
DB
1462 if (!mmc_host_is_spi(host)) {
1463 err = mmc_select_card(card);
1464 if (err)
7fca9675 1465 goto free_card;
6abaa0c9 1466 }
1addfcdb 1467
469e5e47
JB
1468 /* Apply quirks prior to card setup */
1469 mmc_fixup_device(card, mmc_sd_fixups);
1470
71578a1e
MM
1471 err = mmc_sd_setup_card(host, card, oldcard != NULL);
1472 if (err)
1473 goto free_card;
9d9f25c0 1474
6a11fc47
AH
1475 /*
1476 * If the card has not been power cycled, it may still be using 1.8V
1477 * signaling. Detect that situation and try to initialize a UHS-I (1.8V)
1478 * transfer mode.
1479 */
1480 if (!v18_fixup_failed && !mmc_host_is_spi(host) && mmc_host_uhs(host) &&
1481 mmc_sd_card_using_v18(card) &&
1482 host->ios.signal_voltage != MMC_SIGNAL_VOLTAGE_180) {
63f15609
AH
1483 if (mmc_host_set_uhs_voltage(host) ||
1484 mmc_sd_init_uhs_card(card)) {
1485 v18_fixup_failed = true;
1486 mmc_power_cycle(host, ocr);
1487 if (!oldcard)
1488 mmc_remove_card(card);
1489 goto retry;
6a11fc47 1490 }
63f15609 1491 goto cont;
6a11fc47
AH
1492 }
1493
d6d50a15 1494 /* Initialization sequence for UHS-I cards */
d6743a8a 1495 if (rocr & SD_ROCR_S18A && mmc_host_uhs(host)) {
d6d50a15 1496 err = mmc_sd_init_uhs_card(card);
17b0429d 1497 if (err)
7ea239d9 1498 goto free_card;
d6d50a15
AN
1499 } else {
1500 /*
1501 * Attempt to change to high-speed (if supported)
1502 */
1503 err = mmc_sd_switch_hs(card);
1504 if (err > 0)
cdc99179 1505 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
d6d50a15
AN
1506 else if (err)
1507 goto free_card;
1508
1509 /*
1510 * Set bus speed.
1511 */
1512 mmc_set_clock(host, mmc_sd_get_max_clock(card));
7ea239d9 1513
bac80683
WC
1514 if (host->ios.timing == MMC_TIMING_SD_HS &&
1515 host->ops->prepare_sd_hs_tuning) {
1516 err = host->ops->prepare_sd_hs_tuning(host, card);
1517 if (err)
1518 goto free_card;
1519 }
1520
d6d50a15
AN
1521 /*
1522 * Switch to wider bus (if supported).
1523 */
1524 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1525 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1526 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1527 if (err)
1528 goto free_card;
1529
1530 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1531 }
bac80683
WC
1532
1533 if (host->ios.timing == MMC_TIMING_SD_HS &&
1534 host->ops->execute_sd_hs_tuning) {
1535 err = host->ops->execute_sd_hs_tuning(host, card);
1536 if (err)
1537 goto free_card;
1538 }
7ea239d9 1539 }
15c56208 1540cont:
c784f927
UH
1541 if (!oldcard) {
1542 /* Read/parse the extension registers. */
1543 err = sd_read_ext_regs(card);
1544 if (err)
1545 goto free_card;
1546 }
1547
130206a6
UH
1548 /* Enable internal SD cache if supported. */
1549 if (card->ext_perf.feature_support & SD_EXT_PERF_CACHE) {
1550 err = sd_enable_cache(card);
1551 if (err)
1552 goto free_card;
1553 }
1554
045d705d
BW
1555 if (host->cqe_ops && !host->cqe_enabled) {
1556 err = host->cqe_ops->cqe_enable(host, card);
1557 if (!err) {
1558 host->cqe_enabled = true;
1559 host->hsq_enabled = true;
1560 pr_info("%s: Host Software Queue enabled\n",
1561 mmc_hostname(host));
1562 }
1563 }
1564
247cfe53
KR
1565 if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
1566 host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1567 pr_err("%s: Host failed to negotiate down from 3.3V\n",
1568 mmc_hostname(host));
1569 err = -EINVAL;
1570 goto free_card;
1571 }
15c56208 1572
71578a1e 1573 host->card = card;
17b0429d 1574 return 0;
6abaa0c9
PO
1575
1576free_card:
1577 if (!oldcard)
1578 mmc_remove_card(card);
6abaa0c9 1579
adf66a0d 1580 return err;
6abaa0c9
PO
1581}
1582
1583/*
1584 * Host is being removed. Free up the current card.
1585 */
1586static void mmc_sd_remove(struct mmc_host *host)
1587{
6abaa0c9
PO
1588 mmc_remove_card(host->card);
1589 host->card = NULL;
1590}
1591
d3049504
AH
1592/*
1593 * Card detection - card is alive.
1594 */
1595static int mmc_sd_alive(struct mmc_host *host)
1596{
1597 return mmc_send_status(host->card, NULL);
1598}
1599
6abaa0c9
PO
1600/*
1601 * Card detection callback from host.
1602 */
1603static void mmc_sd_detect(struct mmc_host *host)
1604{
1605 int err;
1606
6c0cedd1 1607 mmc_get_card(host->card, NULL);
6abaa0c9
PO
1608
1609 /*
1610 * Just check if our card has been removed.
1611 */
d3049504 1612 err = _mmc_detect_card_removed(host);
7ea239d9 1613
6c0cedd1 1614 mmc_put_card(host->card, NULL);
7ea239d9 1615
17b0429d 1616 if (err) {
4101c16a 1617 mmc_sd_remove(host);
6abaa0c9
PO
1618
1619 mmc_claim_host(host);
1620 mmc_detach_bus(host);
7f7e4129 1621 mmc_power_off(host);
6abaa0c9
PO
1622 mmc_release_host(host);
1623 }
1624}
1625
2c5d4276
UH
1626static int sd_can_poweroff_notify(struct mmc_card *card)
1627{
1628 return card->ext_power.feature_support & SD_EXT_POWER_OFF_NOTIFY;
1629}
1630
1631static int sd_busy_poweroff_notify_cb(void *cb_data, bool *busy)
1632{
1633 struct sd_busy_data *data = cb_data;
1634 struct mmc_card *card = data->card;
1635 int err;
1636
1637 /*
1638 * Read the status register for the power management function. It's at
1639 * one byte offset and is one byte long. The Power Off Notification
1640 * Ready is bit 0.
1641 */
1642 err = sd_read_ext_reg(card, card->ext_power.fno, card->ext_power.page,
1643 card->ext_power.offset + 1, 1, data->reg_buf);
1644 if (err) {
1645 pr_warn("%s: error %d reading status reg of PM func\n",
1646 mmc_hostname(card->host), err);
1647 return err;
1648 }
1649
1650 *busy = !(data->reg_buf[0] & BIT(0));
1651 return 0;
1652}
1653
1654static int sd_poweroff_notify(struct mmc_card *card)
1655{
1656 struct sd_busy_data cb_data;
1657 u8 *reg_buf;
1658 int err;
1659
1660 reg_buf = kzalloc(512, GFP_KERNEL);
1661 if (!reg_buf)
1662 return -ENOMEM;
1663
1664 /*
1665 * Set the Power Off Notification bit in the power management settings
1666 * register at 2 bytes offset.
1667 */
1668 err = sd_write_ext_reg(card, card->ext_power.fno, card->ext_power.page,
1669 card->ext_power.offset + 2, BIT(0));
1670 if (err) {
1671 pr_warn("%s: error %d writing Power Off Notify bit\n",
1672 mmc_hostname(card->host), err);
1673 goto out;
1674 }
1675
379f56c2
AS
1676 /* Find out when the command is completed. */
1677 err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1678 MMC_BUSY_EXTR_SINGLE);
1679 if (err)
1680 goto out;
1681
2c5d4276
UH
1682 cb_data.card = card;
1683 cb_data.reg_buf = reg_buf;
1760fdb6 1684 err = __mmc_poll_for_busy(card->host, 0, SD_POWEROFF_NOTIFY_TIMEOUT_MS,
2c5d4276
UH
1685 &sd_busy_poweroff_notify_cb, &cb_data);
1686
1687out:
1688 kfree(reg_buf);
1689 return err;
1690}
1691
0cb403a2 1692static int _mmc_sd_suspend(struct mmc_host *host)
6abaa0c9 1693{
2c5d4276 1694 struct mmc_card *card = host->card;
85e727ed
JC
1695 int err = 0;
1696
6abaa0c9 1697 mmc_claim_host(host);
9ec775f7 1698
2c5d4276 1699 if (mmc_card_suspended(card))
9ec775f7
UH
1700 goto out;
1701
2c5d4276
UH
1702 if (sd_can_poweroff_notify(card))
1703 err = sd_poweroff_notify(card);
1704 else if (!mmc_host_is_spi(host))
85e727ed 1705 err = mmc_deselect_cards(host);
cdc99179 1706
9ec775f7 1707 if (!err) {
74590263 1708 mmc_power_off(host);
2c5d4276 1709 mmc_card_set_suspended(card);
9ec775f7 1710 }
95cdfb72 1711
9ec775f7
UH
1712out:
1713 mmc_release_host(host);
85e727ed 1714 return err;
6abaa0c9
PO
1715}
1716
1717/*
0cb403a2
UH
1718 * Callback for suspend
1719 */
1720static int mmc_sd_suspend(struct mmc_host *host)
1721{
1722 int err;
1723
1724 err = _mmc_sd_suspend(host);
1725 if (!err) {
1726 pm_runtime_disable(&host->card->dev);
1727 pm_runtime_set_suspended(&host->card->dev);
1728 }
1729
1730 return err;
1731}
1732
1733/*
6abaa0c9
PO
1734 * This function tries to determine if the same card is still present
1735 * and, if so, restore all state to it.
1736 */
0cb403a2 1737static int _mmc_sd_resume(struct mmc_host *host)
6abaa0c9 1738{
9ec775f7 1739 int err = 0;
6abaa0c9 1740
6abaa0c9 1741 mmc_claim_host(host);
9ec775f7
UH
1742
1743 if (!mmc_card_suspended(host->card))
1744 goto out;
1745
69041150 1746 mmc_power_up(host, host->card->ocr);
69041150 1747 err = mmc_sd_init_card(host, host->card->ocr, host->card);
9ec775f7 1748 mmc_card_clr_suspended(host->card);
2986d0bf 1749
9ec775f7
UH
1750out:
1751 mmc_release_host(host);
95cdfb72 1752 return err;
6abaa0c9
PO
1753}
1754
0cb403a2
UH
1755/*
1756 * Callback for resume
1757 */
1758static int mmc_sd_resume(struct mmc_host *host)
1759{
0cb403a2 1760 pm_runtime_enable(&host->card->dev);
c29536e8 1761 return 0;
0cb403a2
UH
1762}
1763
c4d770d7
UH
1764/*
1765 * Callback for runtime_suspend.
1766 */
1767static int mmc_sd_runtime_suspend(struct mmc_host *host)
1768{
1769 int err;
1770
1771 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1772 return 0;
1773
0cb403a2 1774 err = _mmc_sd_suspend(host);
0cc81a8c 1775 if (err)
f42cf8d6 1776 pr_err("%s: error %d doing aggressive suspend\n",
c4d770d7 1777 mmc_hostname(host), err);
c4d770d7 1778
c4d770d7
UH
1779 return err;
1780}
1781
1782/*
1783 * Callback for runtime_resume.
1784 */
1785static int mmc_sd_runtime_resume(struct mmc_host *host)
1786{
1787 int err;
1788
0cb403a2 1789 err = _mmc_sd_resume(host);
520322d9 1790 if (err && err != -ENOMEDIUM)
c29536e8 1791 pr_err("%s: error %d doing runtime resume\n",
c4d770d7
UH
1792 mmc_hostname(host), err);
1793
c4d770d7
UH
1794 return 0;
1795}
1796
3a3db603 1797static int mmc_sd_hw_reset(struct mmc_host *host)
dc0ecfef
JR
1798{
1799 mmc_power_cycle(host, host->card->ocr);
3056c49c 1800 return mmc_sd_init_card(host, host->card->ocr, host->card);
dc0ecfef
JR
1801}
1802
6abaa0c9
PO
1803static const struct mmc_bus_ops mmc_sd_ops = {
1804 .remove = mmc_sd_remove,
1805 .detect = mmc_sd_detect,
c4d770d7
UH
1806 .runtime_suspend = mmc_sd_runtime_suspend,
1807 .runtime_resume = mmc_sd_runtime_resume,
6abaa0c9
PO
1808 .suspend = mmc_sd_suspend,
1809 .resume = mmc_sd_resume,
d3049504 1810 .alive = mmc_sd_alive,
5992e786 1811 .shutdown = mmc_sd_suspend,
3a3db603 1812 .hw_reset = mmc_sd_hw_reset,
130206a6
UH
1813 .cache_enabled = sd_cache_enabled,
1814 .flush_cache = sd_flush_cache,
6abaa0c9
PO
1815};
1816
1817/*
1818 * Starting point for SD card init.
1819 */
807e8e40 1820int mmc_attach_sd(struct mmc_host *host)
6abaa0c9
PO
1821{
1822 int err;
69041150 1823 u32 ocr, rocr;
6abaa0c9 1824
d84075c8 1825 WARN_ON(!host->claimed);
6abaa0c9 1826
807e8e40
AR
1827 err = mmc_send_app_op_cond(host, 0, &ocr);
1828 if (err)
1829 return err;
1830
2501c917 1831 mmc_attach_bus(host, &mmc_sd_ops);
8f230f45
TI
1832 if (host->ocr_avail_sd)
1833 host->ocr_avail = host->ocr_avail_sd;
6abaa0c9 1834
af517150
DB
1835 /*
1836 * We need to get OCR a different way for SPI.
1837 */
1838 if (mmc_host_is_spi(host)) {
1839 mmc_go_idle(host);
1840
1841 err = mmc_spi_read_ocr(host, 0, &ocr);
1842 if (err)
1843 goto err;
1844 }
1845
72741084
UH
1846 /*
1847 * Some SD cards claims an out of spec VDD voltage range. Let's treat
1848 * these bits as being in-valid and especially also bit7.
1849 */
1850 ocr &= ~0x7FFF;
1851
69041150 1852 rocr = mmc_select_voltage(host, ocr);
6abaa0c9
PO
1853
1854 /*
1855 * Can we support the voltage(s) of the card(s)?
1856 */
69041150 1857 if (!rocr) {
109b5bed 1858 err = -EINVAL;
6abaa0c9 1859 goto err;
109b5bed 1860 }
6abaa0c9
PO
1861
1862 /*
1863 * Detect and init the card.
1864 */
69041150 1865 err = mmc_sd_init_card(host, rocr, NULL);
17b0429d 1866 if (err)
6abaa0c9
PO
1867 goto err;
1868
1869 mmc_release_host(host);
4101c16a 1870 err = mmc_add_card(host->card);
7ea239d9 1871 if (err)
2986d0bf 1872 goto remove_card;
7ea239d9 1873
2860d060 1874 mmc_claim_host(host);
7ea239d9
PO
1875 return 0;
1876
2986d0bf 1877remove_card:
6abaa0c9 1878 mmc_remove_card(host->card);
7ea239d9 1879 host->card = NULL;
2986d0bf 1880 mmc_claim_host(host);
7ea239d9
PO
1881err:
1882 mmc_detach_bus(host);
7ea239d9 1883
a3c76eb9 1884 pr_err("%s: error %d whilst initialising SD card\n",
109b5bed
PO
1885 mmc_hostname(host), err);
1886
adf66a0d 1887 return err;
7ea239d9 1888}