mmc: core: switch to 1V8 or 1V2 for hs400es mode
[linux-2.6-block.git] / drivers / mmc / core / mmc.c
CommitLineData
7ea239d9 1/*
70f10482 2 * linux/drivers/mmc/core/mmc.c
7ea239d9
PO
3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
6 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/err.h>
81f8a7be 14#include <linux/of.h>
5a0e3ad6 15#include <linux/slab.h>
0205a904 16#include <linux/stat.h>
0cb403a2 17#include <linux/pm_runtime.h>
7ea239d9
PO
18
19#include <linux/mmc/host.h>
20#include <linux/mmc/card.h>
21#include <linux/mmc/mmc.h>
22
23#include "core.h"
436f8daa 24#include "host.h"
4101c16a 25#include "bus.h"
7ea239d9 26#include "mmc_ops.h"
4c4cb171 27#include "sd_ops.h"
7ea239d9
PO
28
29static const unsigned int tran_exp[] = {
30 10000, 100000, 1000000, 10000000,
31 0, 0, 0, 0
32};
33
34static const unsigned char tran_mant[] = {
35 0, 10, 12, 13, 15, 20, 25, 30,
36 35, 40, 45, 50, 55, 60, 70, 80,
37};
38
39static const unsigned int tacc_exp[] = {
40 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
41};
42
43static const unsigned int tacc_mant[] = {
44 0, 10, 12, 13, 15, 20, 25, 30,
45 35, 40, 45, 50, 55, 60, 70, 80,
46};
47
5320226a
P
48static const struct mmc_fixup mmc_ext_csd_fixups[] = {
49 /*
50 * Certain Hynix eMMC 4.41 cards might get broken when HPI feature
51 * is used so disable the HPI feature for such buggy cards.
52 */
53 MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_HYNIX,
54 0x014a, add_quirk, MMC_QUIRK_BROKEN_HPI, 5),
55
56 END_FIXUP
57};
58
7ea239d9
PO
59#define UNSTUFF_BITS(resp,start,size) \
60 ({ \
61 const int __size = size; \
62 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
63 const int __off = 3 - ((start) / 32); \
64 const int __shft = (start) & 31; \
65 u32 __res; \
66 \
67 __res = resp[__off] >> __shft; \
68 if (__size + __shft > 32) \
69 __res |= resp[__off-1] << ((32 - __shft) % 32); \
70 __res & __mask; \
71 })
72
73/*
74 * Given the decoded CSD structure, decode the raw CID to our CID structure.
75 */
bd766312 76static int mmc_decode_cid(struct mmc_card *card)
7ea239d9
PO
77{
78 u32 *resp = card->raw_cid;
79
80 /*
81 * The selection of the format here is based upon published
82 * specs from sandisk and from what people have reported.
83 */
84 switch (card->csd.mmca_vsn) {
85 case 0: /* MMC v1.0 - v1.2 */
86 case 1: /* MMC v1.4 */
87 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
88 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
89 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
90 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
91 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
92 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
93 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
94 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
95 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
96 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
97 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
98 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
99 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
100 break;
101
102 case 2: /* MMC v2.0 - v2.2 */
103 case 3: /* MMC v3.1 - v3.3 */
104 case 4: /* MMC v4 */
105 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
106 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
107 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
108 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
109 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
110 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
111 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
112 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
51e7e8b6 113 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
7ea239d9
PO
114 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
115 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
116 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
117 break;
118
119 default:
a3c76eb9 120 pr_err("%s: card has unknown MMCA version %d\n",
7ea239d9 121 mmc_hostname(card->host), card->csd.mmca_vsn);
bd766312 122 return -EINVAL;
7ea239d9 123 }
bd766312
PO
124
125 return 0;
7ea239d9
PO
126}
127
dfe86cba
AH
128static void mmc_set_erase_size(struct mmc_card *card)
129{
130 if (card->ext_csd.erase_group_def & 1)
131 card->erase_size = card->ext_csd.hc_erase_size;
132 else
133 card->erase_size = card->csd.erase_size;
134
135 mmc_init_erase(card);
136}
137
7ea239d9
PO
138/*
139 * Given a 128-bit response, decode to our card CSD structure.
140 */
bd766312 141static int mmc_decode_csd(struct mmc_card *card)
7ea239d9
PO
142{
143 struct mmc_csd *csd = &card->csd;
dfe86cba 144 unsigned int e, m, a, b;
7ea239d9
PO
145 u32 *resp = card->raw_csd;
146
147 /*
148 * We only understand CSD structure v1.1 and v1.2.
149 * v1.2 has extra information in bits 15, 11 and 10.
6da24b78 150 * We also support eMMC v4.4 & v4.41.
7ea239d9 151 */
6da24b78
KP
152 csd->structure = UNSTUFF_BITS(resp, 126, 2);
153 if (csd->structure == 0) {
a3c76eb9 154 pr_err("%s: unrecognised CSD structure version %d\n",
6da24b78 155 mmc_hostname(card->host), csd->structure);
bd766312 156 return -EINVAL;
7ea239d9
PO
157 }
158
159 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
160 m = UNSTUFF_BITS(resp, 115, 4);
161 e = UNSTUFF_BITS(resp, 112, 3);
162 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
163 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
164
165 m = UNSTUFF_BITS(resp, 99, 4);
166 e = UNSTUFF_BITS(resp, 96, 3);
167 csd->max_dtr = tran_exp[e] * tran_mant[m];
168 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
169
170 e = UNSTUFF_BITS(resp, 47, 3);
171 m = UNSTUFF_BITS(resp, 62, 12);
172 csd->capacity = (1 + m) << (e + 2);
173
174 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
175 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
176 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
177 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
3d705d14 178 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
7ea239d9
PO
179 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
180 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
181 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
bd766312 182
dfe86cba
AH
183 if (csd->write_blkbits >= 9) {
184 a = UNSTUFF_BITS(resp, 42, 5);
185 b = UNSTUFF_BITS(resp, 37, 5);
186 csd->erase_size = (a + 1) * (b + 1);
187 csd->erase_size <<= csd->write_blkbits - 9;
188 }
189
bd766312 190 return 0;
7ea239d9
PO
191}
192
96cf5f02
SJ
193static void mmc_select_card_type(struct mmc_card *card)
194{
195 struct mmc_host *host = card->host;
0a5b6438 196 u8 card_type = card->ext_csd.raw_card_type;
5f1a4dd0 197 u32 caps = host->caps, caps2 = host->caps2;
577fb131 198 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
2415c0ef 199 unsigned int avail_type = 0;
96cf5f02 200
2415c0ef
SJ
201 if (caps & MMC_CAP_MMC_HIGHSPEED &&
202 card_type & EXT_CSD_CARD_TYPE_HS_26) {
96cf5f02 203 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
2415c0ef
SJ
204 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
205 }
96cf5f02
SJ
206
207 if (caps & MMC_CAP_MMC_HIGHSPEED &&
2415c0ef 208 card_type & EXT_CSD_CARD_TYPE_HS_52) {
96cf5f02 209 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
2415c0ef
SJ
210 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
211 }
96cf5f02 212
2415c0ef
SJ
213 if (caps & MMC_CAP_1_8V_DDR &&
214 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
96cf5f02 215 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
2415c0ef
SJ
216 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
217 }
218
219 if (caps & MMC_CAP_1_2V_DDR &&
220 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
221 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
222 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
223 }
96cf5f02 224
2415c0ef
SJ
225 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
226 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
577fb131 227 hs200_max_dtr = MMC_HS200_MAX_DTR;
2415c0ef
SJ
228 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
229 }
230
231 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
232 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
577fb131 233 hs200_max_dtr = MMC_HS200_MAX_DTR;
2415c0ef
SJ
234 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
235 }
96cf5f02 236
0a5b6438
SJ
237 if (caps2 & MMC_CAP2_HS400_1_8V &&
238 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
239 hs200_max_dtr = MMC_HS200_MAX_DTR;
240 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
241 }
242
243 if (caps2 & MMC_CAP2_HS400_1_2V &&
244 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
245 hs200_max_dtr = MMC_HS200_MAX_DTR;
246 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
247 }
248
81ac2af6
SL
249 if ((caps2 & MMC_CAP2_HS400_ES) &&
250 card->ext_csd.strobe_support &&
251 (avail_type & EXT_CSD_CARD_TYPE_HS400))
252 avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
253
96cf5f02 254 card->ext_csd.hs_max_dtr = hs_max_dtr;
577fb131 255 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
2415c0ef 256 card->mmc_avail_type = avail_type;
96cf5f02
SJ
257}
258
b4493eea
GS
259static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
260{
994324bb
GS
261 u8 hc_erase_grp_sz, hc_wp_grp_sz;
262
263 /*
264 * Disable these attributes by default
265 */
266 card->ext_csd.enhanced_area_offset = -EINVAL;
267 card->ext_csd.enhanced_area_size = -EINVAL;
b4493eea
GS
268
269 /*
270 * Enhanced area feature support -- check whether the eMMC
271 * card has the Enhanced area enabled. If so, export enhanced
272 * area offset and size to user by adding sysfs interface.
273 */
274 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
275 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
994324bb
GS
276 if (card->ext_csd.partition_setting_completed) {
277 hc_erase_grp_sz =
278 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
279 hc_wp_grp_sz =
280 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
b4493eea 281
994324bb
GS
282 /*
283 * calculate the enhanced data area offset, in bytes
284 */
285 card->ext_csd.enhanced_area_offset =
ded8a5f9
KM
286 (((unsigned long long)ext_csd[139]) << 24) +
287 (((unsigned long long)ext_csd[138]) << 16) +
288 (((unsigned long long)ext_csd[137]) << 8) +
289 (((unsigned long long)ext_csd[136]));
994324bb
GS
290 if (mmc_card_blockaddr(card))
291 card->ext_csd.enhanced_area_offset <<= 9;
292 /*
293 * calculate the enhanced data area size, in kilobytes
294 */
295 card->ext_csd.enhanced_area_size =
296 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
297 ext_csd[140];
298 card->ext_csd.enhanced_area_size *=
299 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
300 card->ext_csd.enhanced_area_size <<= 9;
301 } else {
302 pr_warn("%s: defines enhanced area without partition setting complete\n",
303 mmc_hostname(card->host));
304 }
b4493eea
GS
305 }
306}
307
308static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
309{
b4493eea 310 int idx;
994324bb
GS
311 u8 hc_erase_grp_sz, hc_wp_grp_sz;
312 unsigned int part_size;
b4493eea
GS
313
314 /*
315 * General purpose partition feature support --
316 * If ext_csd has the size of general purpose partitions,
317 * set size, part_cfg, partition name in mmc_part.
318 */
319 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
320 EXT_CSD_PART_SUPPORT_PART_EN) {
994324bb
GS
321 hc_erase_grp_sz =
322 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
323 hc_wp_grp_sz =
324 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
b4493eea
GS
325
326 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
327 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
328 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
329 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
330 continue;
994324bb
GS
331 if (card->ext_csd.partition_setting_completed == 0) {
332 pr_warn("%s: has partition size defined without partition complete\n",
333 mmc_hostname(card->host));
334 break;
335 }
b4493eea
GS
336 part_size =
337 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
338 << 16) +
339 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
340 << 8) +
341 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
342 part_size *= (size_t)(hc_erase_grp_sz *
343 hc_wp_grp_sz);
344 mmc_part_add(card, part_size << 19,
345 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
346 "gp%d", idx, false,
347 MMC_BLK_DATA_AREA_GP);
348 }
349 }
350}
351
1c447116
AH
352/* Minimum partition switch timeout in milliseconds */
353#define MMC_MIN_PART_SWITCH_TIME 300
354
08ee80cc
PR
355/*
356 * Decode extended CSD.
357 */
076ec38a 358static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
08ee80cc 359{
e0c368d5
NJ
360 int err = 0, idx;
361 unsigned int part_size;
81f8a7be
HG
362 struct device_node *np;
363 bool broken_hpi = false;
08ee80cc 364
6da24b78 365 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
f39b2dd9 366 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
6da24b78 367 if (card->csd.structure == 3) {
f39b2dd9 368 if (card->ext_csd.raw_ext_csd_structure > 2) {
a3c76eb9 369 pr_err("%s: unrecognised EXT_CSD structure "
6da24b78 370 "version %d\n", mmc_hostname(card->host),
f39b2dd9 371 card->ext_csd.raw_ext_csd_structure);
6da24b78
KP
372 err = -EINVAL;
373 goto out;
374 }
375 }
376
81f8a7be
HG
377 np = mmc_of_find_child_device(card->host, 0);
378 if (np && of_device_is_compatible(np, "mmc-card"))
379 broken_hpi = of_property_read_bool(np, "broken-hpi");
380 of_node_put(np);
381
03a59437
RI
382 /*
383 * The EXT_CSD format is meant to be forward compatible. As long
384 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
385 * are authorized, see JEDEC JESD84-B50 section B.8.
386 */
b1ebe384 387 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
d7604d76 388
5320226a
P
389 /* fixup device after ext_csd revision field is updated */
390 mmc_fixup_device(card, mmc_ext_csd_fixups);
391
f39b2dd9
PR
392 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
393 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
394 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
395 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
b1ebe384 396 if (card->ext_csd.rev >= 2) {
d7604d76
PO
397 card->ext_csd.sectors =
398 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
399 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
400 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
401 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
fc8a0985
HP
402
403 /* Cards with density > 2GiB are sector addressed */
404 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
d7604d76
PO
405 mmc_card_set_blockaddr(card);
406 }
96cf5f02 407
81ac2af6 408 card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
f39b2dd9 409 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
96cf5f02 410 mmc_select_card_type(card);
7ea239d9 411
f39b2dd9
PR
412 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
413 card->ext_csd.raw_erase_timeout_mult =
414 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
415 card->ext_csd.raw_hc_erase_grp_size =
416 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
b1ebe384
JL
417 if (card->ext_csd.rev >= 3) {
418 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
371a689f
AW
419 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
420
421 /* EXT_CSD value is in units of 10ms, but we store in ms */
422 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
1c447116
AH
423 /* Some eMMC set the value too low so set a minimum */
424 if (card->ext_csd.part_time &&
425 card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
426 card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
b1ebe384
JL
427
428 /* Sleep / awake timeout in 100ns units */
429 if (sa_shift > 0 && sa_shift <= 0x17)
430 card->ext_csd.sa_timeout =
431 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
dfe86cba
AH
432 card->ext_csd.erase_group_def =
433 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
434 card->ext_csd.hc_erase_timeout = 300 *
435 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
436 card->ext_csd.hc_erase_size =
437 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
f4c5522b
AW
438
439 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
371a689f
AW
440
441 /*
442 * There are two boot regions of equal size, defined in
443 * multiples of 128K.
444 */
e0c368d5
NJ
445 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
446 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
447 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
448 mmc_part_add(card, part_size,
449 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
add710ea
JR
450 "boot%d", idx, true,
451 MMC_BLK_DATA_AREA_BOOT);
e0c368d5
NJ
452 }
453 }
dfe86cba
AH
454 }
455
f39b2dd9 456 card->ext_csd.raw_hc_erase_gap_size =
dd13b4ed 457 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
f39b2dd9
PR
458 card->ext_csd.raw_sec_trim_mult =
459 ext_csd[EXT_CSD_SEC_TRIM_MULT];
460 card->ext_csd.raw_sec_erase_mult =
461 ext_csd[EXT_CSD_SEC_ERASE_MULT];
462 card->ext_csd.raw_sec_feature_support =
463 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
464 card->ext_csd.raw_trim_mult =
465 ext_csd[EXT_CSD_TRIM_MULT];
836dc2fe 466 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
b097e07f 467 card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
dfe86cba 468 if (card->ext_csd.rev >= 4) {
69803d4f
GS
469 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
470 EXT_CSD_PART_SETTING_COMPLETED)
471 card->ext_csd.partition_setting_completed = 1;
472 else
473 card->ext_csd.partition_setting_completed = 0;
474
b4493eea 475 mmc_manage_enhanced_area(card, ext_csd);
709de99d 476
b4493eea 477 mmc_manage_gp_partitions(card, ext_csd);
e0c368d5 478
dfe86cba
AH
479 card->ext_csd.sec_trim_mult =
480 ext_csd[EXT_CSD_SEC_TRIM_MULT];
481 card->ext_csd.sec_erase_mult =
482 ext_csd[EXT_CSD_SEC_ERASE_MULT];
483 card->ext_csd.sec_feature_support =
484 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
485 card->ext_csd.trim_timeout = 300 *
486 ext_csd[EXT_CSD_TRIM_MULT];
add710ea
JR
487
488 /*
489 * Note that the call to mmc_part_add above defaults to read
490 * only. If this default assumption is changed, the call must
491 * take into account the value of boot_locked below.
492 */
493 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
494 card->ext_csd.boot_ro_lockable = true;
60443712
FS
495
496 /* Save power class values */
497 card->ext_csd.raw_pwr_cl_52_195 =
498 ext_csd[EXT_CSD_PWR_CL_52_195];
499 card->ext_csd.raw_pwr_cl_26_195 =
500 ext_csd[EXT_CSD_PWR_CL_26_195];
501 card->ext_csd.raw_pwr_cl_52_360 =
502 ext_csd[EXT_CSD_PWR_CL_52_360];
503 card->ext_csd.raw_pwr_cl_26_360 =
504 ext_csd[EXT_CSD_PWR_CL_26_360];
505 card->ext_csd.raw_pwr_cl_200_195 =
506 ext_csd[EXT_CSD_PWR_CL_200_195];
507 card->ext_csd.raw_pwr_cl_200_360 =
508 ext_csd[EXT_CSD_PWR_CL_200_360];
509 card->ext_csd.raw_pwr_cl_ddr_52_195 =
510 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
511 card->ext_csd.raw_pwr_cl_ddr_52_360 =
512 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
0a5b6438
SJ
513 card->ext_csd.raw_pwr_cl_ddr_200_360 =
514 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
b1ebe384
JL
515 }
516
b2499518 517 if (card->ext_csd.rev >= 5) {
7c4f10ac
RI
518 /* Adjust production date as per JEDEC JESD84-B451 */
519 if (card->cid.year < 2010)
520 card->cid.year += 16;
521
950d56ac 522 /* check whether the eMMC card supports BKOPS */
5320226a
P
523 if (!mmc_card_broken_hpi(card) &&
524 ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
950d56ac 525 card->ext_csd.bkops = 1;
0501be64
AS
526 card->ext_csd.man_bkops_en =
527 (ext_csd[EXT_CSD_BKOPS_EN] &
528 EXT_CSD_MANUAL_BKOPS_MASK);
950d56ac
JC
529 card->ext_csd.raw_bkops_status =
530 ext_csd[EXT_CSD_BKOPS_STATUS];
0501be64 531 if (!card->ext_csd.man_bkops_en)
4ec96b4c 532 pr_debug("%s: MAN_BKOPS_EN bit is not set\n",
950d56ac
JC
533 mmc_hostname(card->host));
534 }
535
eb0d8f13 536 /* check whether the eMMC card supports HPI */
5320226a
P
537 if (!mmc_card_broken_hpi(card) &&
538 !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
eb0d8f13
JC
539 card->ext_csd.hpi = 1;
540 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
541 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
542 else
543 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
544 /*
545 * Indicate the maximum timeout to close
546 * a command interrupted by HPI
547 */
548 card->ext_csd.out_of_int_time =
549 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
550 }
551
f4c5522b 552 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
b2499518 553 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
090d25fe
LP
554
555 /*
556 * RPMB regions are defined in multiples of 128K.
557 */
558 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
d0123cca 559 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
090d25fe
LP
560 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
561 EXT_CSD_PART_CONFIG_ACC_RPMB,
562 "rpmb", 0, false,
563 MMC_BLK_DATA_AREA_RPMB);
564 }
b2499518 565 }
f4c5522b 566
5238acbe 567 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
dfe86cba
AH
568 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
569 card->erased_byte = 0xFF;
570 else
571 card->erased_byte = 0x0;
572
336c716a 573 /* eMMC v4.5 or later */
bec8726a 574 if (card->ext_csd.rev >= 6) {
336c716a
SJ
575 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
576
b23cf0bd
SJ
577 card->ext_csd.generic_cmd6_time = 10 *
578 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
bec8726a
G
579 card->ext_csd.power_off_longtime = 10 *
580 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
b23cf0bd 581
336c716a
SJ
582 card->ext_csd.cache_size =
583 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
584 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
585 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
586 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
4265900e
SD
587
588 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
589 card->ext_csd.data_sector_size = 4096;
590 else
591 card->ext_csd.data_sector_size = 512;
592
593 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
594 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
595 card->ext_csd.data_tag_unit_size =
596 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
597 (card->ext_csd.data_sector_size);
598 } else {
599 card->ext_csd.data_tag_unit_size = 0;
600 }
abd9ac14
SJ
601
602 card->ext_csd.max_packed_writes =
603 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
604 card->ext_csd.max_packed_reads =
605 ext_csd[EXT_CSD_MAX_PACKED_READS];
a5075eb9
SD
606 } else {
607 card->ext_csd.data_sector_size = 512;
336c716a 608 }
881d1c25 609
0f762426
GG
610 /* eMMC v5 or later */
611 if (card->ext_csd.rev >= 7) {
612 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
613 MMC_FIRMWARE_LEN);
614 card->ext_csd.ffu_capable =
615 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
616 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
617 }
7ea239d9 618out:
08ee80cc
PR
619 return err;
620}
621
076ec38a
UH
622static int mmc_read_ext_csd(struct mmc_card *card)
623{
c197787c 624 u8 *ext_csd;
076ec38a
UH
625 int err;
626
c197787c
UH
627 if (!mmc_can_ext_csd(card))
628 return 0;
629
076ec38a 630 err = mmc_get_ext_csd(card, &ext_csd);
c197787c
UH
631 if (err) {
632 /* If the host or the card can't do the switch,
633 * fail more gracefully. */
634 if ((err != -EINVAL)
635 && (err != -ENOSYS)
636 && (err != -EFAULT))
637 return err;
638
639 /*
640 * High capacity cards should have this "magic" size
641 * stored in their CSD.
642 */
643 if (card->csd.capacity == (4096 * 512)) {
644 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
645 mmc_hostname(card->host));
646 } else {
647 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
648 mmc_hostname(card->host));
649 err = 0;
650 }
651
076ec38a 652 return err;
c197787c 653 }
076ec38a
UH
654
655 err = mmc_decode_ext_csd(card, ext_csd);
656 kfree(ext_csd);
657 return err;
658}
659
f39b2dd9 660static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
08ee80cc
PR
661{
662 u8 *bw_ext_csd;
663 int err;
664
f39b2dd9
PR
665 if (bus_width == MMC_BUS_WIDTH_1)
666 return 0;
667
08ee80cc 668 err = mmc_get_ext_csd(card, &bw_ext_csd);
c197787c
UH
669 if (err)
670 return err;
08ee80cc 671
08ee80cc 672 /* only compare read only fields */
dd13b4ed 673 err = !((card->ext_csd.raw_partition_support ==
08ee80cc 674 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
f39b2dd9 675 (card->ext_csd.raw_erased_mem_count ==
08ee80cc 676 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
f39b2dd9 677 (card->ext_csd.rev ==
08ee80cc 678 bw_ext_csd[EXT_CSD_REV]) &&
f39b2dd9 679 (card->ext_csd.raw_ext_csd_structure ==
08ee80cc 680 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
f39b2dd9 681 (card->ext_csd.raw_card_type ==
08ee80cc 682 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
f39b2dd9 683 (card->ext_csd.raw_s_a_timeout ==
08ee80cc 684 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
f39b2dd9 685 (card->ext_csd.raw_hc_erase_gap_size ==
08ee80cc 686 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
f39b2dd9 687 (card->ext_csd.raw_erase_timeout_mult ==
08ee80cc 688 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
f39b2dd9 689 (card->ext_csd.raw_hc_erase_grp_size ==
08ee80cc 690 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
f39b2dd9 691 (card->ext_csd.raw_sec_trim_mult ==
08ee80cc 692 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
f39b2dd9 693 (card->ext_csd.raw_sec_erase_mult ==
08ee80cc 694 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
f39b2dd9 695 (card->ext_csd.raw_sec_feature_support ==
08ee80cc 696 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
f39b2dd9 697 (card->ext_csd.raw_trim_mult ==
08ee80cc 698 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
f39b2dd9
PR
699 (card->ext_csd.raw_sectors[0] ==
700 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
701 (card->ext_csd.raw_sectors[1] ==
702 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
703 (card->ext_csd.raw_sectors[2] ==
704 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
705 (card->ext_csd.raw_sectors[3] ==
60443712
FS
706 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
707 (card->ext_csd.raw_pwr_cl_52_195 ==
708 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
709 (card->ext_csd.raw_pwr_cl_26_195 ==
710 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
711 (card->ext_csd.raw_pwr_cl_52_360 ==
712 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
713 (card->ext_csd.raw_pwr_cl_26_360 ==
714 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
715 (card->ext_csd.raw_pwr_cl_200_195 ==
716 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
717 (card->ext_csd.raw_pwr_cl_200_360 ==
718 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
719 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
720 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
721 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
0a5b6438
SJ
722 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
723 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
724 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
725
08ee80cc
PR
726 if (err)
727 err = -EINVAL;
728
00b41b58 729 kfree(bw_ext_csd);
7ea239d9
PO
730 return err;
731}
732
51ec92e2
PO
733MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
734 card->raw_cid[2], card->raw_cid[3]);
735MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
736 card->raw_csd[2], card->raw_csd[3]);
737MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
dfe86cba
AH
738MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
739MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
0f762426 740MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
51ec92e2
PO
741MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
742MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
743MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
744MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
51e7e8b6 745MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
51ec92e2 746MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
709de99d
CD
747MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
748 card->ext_csd.enhanced_area_offset);
749MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
188cc042
LP
750MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
751MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
5fb06af7 752MMC_DEV_ATTR(ocr, "%08x\n", card->ocr);
51ec92e2 753
0f762426
GG
754static ssize_t mmc_fwrev_show(struct device *dev,
755 struct device_attribute *attr,
756 char *buf)
757{
758 struct mmc_card *card = mmc_dev_to_card(dev);
759
760 if (card->ext_csd.rev < 7) {
761 return sprintf(buf, "0x%x\n", card->cid.fwrev);
762 } else {
763 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
764 card->ext_csd.fwrev);
765 }
766}
767
768static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
769
6825a606
BP
770static ssize_t mmc_dsr_show(struct device *dev,
771 struct device_attribute *attr,
772 char *buf)
773{
774 struct mmc_card *card = mmc_dev_to_card(dev);
775 struct mmc_host *host = card->host;
776
777 if (card->csd.dsr_imp && host->dsr_req)
778 return sprintf(buf, "0x%x\n", host->dsr);
779 else
780 /* return default DSR value */
781 return sprintf(buf, "0x%x\n", 0x404);
782}
783
784static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
785
51ec92e2
PO
786static struct attribute *mmc_std_attrs[] = {
787 &dev_attr_cid.attr,
788 &dev_attr_csd.attr,
789 &dev_attr_date.attr,
dfe86cba
AH
790 &dev_attr_erase_size.attr,
791 &dev_attr_preferred_erase_size.attr,
51ec92e2 792 &dev_attr_fwrev.attr,
0f762426 793 &dev_attr_ffu_capable.attr,
51ec92e2
PO
794 &dev_attr_hwrev.attr,
795 &dev_attr_manfid.attr,
796 &dev_attr_name.attr,
797 &dev_attr_oemid.attr,
51e7e8b6 798 &dev_attr_prv.attr,
51ec92e2 799 &dev_attr_serial.attr,
709de99d
CD
800 &dev_attr_enhanced_area_offset.attr,
801 &dev_attr_enhanced_area_size.attr,
188cc042
LP
802 &dev_attr_raw_rpmb_size_mult.attr,
803 &dev_attr_rel_sectors.attr,
5fb06af7 804 &dev_attr_ocr.attr,
6825a606 805 &dev_attr_dsr.attr,
51ec92e2
PO
806 NULL,
807};
d1e58212 808ATTRIBUTE_GROUPS(mmc_std);
51ec92e2
PO
809
810static struct device_type mmc_type = {
d1e58212 811 .groups = mmc_std_groups,
51ec92e2
PO
812};
813
b87d8dbf
G
814/*
815 * Select the PowerClass for the current bus width
816 * If power class is defined for 4/8 bit bus in the
817 * extended CSD register, select it by executing the
818 * mmc_switch command.
819 */
2385049d
SJ
820static int __mmc_select_powerclass(struct mmc_card *card,
821 unsigned int bus_width)
b87d8dbf 822{
2385049d
SJ
823 struct mmc_host *host = card->host;
824 struct mmc_ext_csd *ext_csd = &card->ext_csd;
60443712 825 unsigned int pwrclass_val = 0;
2385049d 826 int err = 0;
b87d8dbf 827
b87d8dbf
G
828 switch (1 << host->ios.vdd) {
829 case MMC_VDD_165_195:
2385049d
SJ
830 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
831 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
832 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
60443712 833 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
2385049d
SJ
834 ext_csd->raw_pwr_cl_52_195 :
835 ext_csd->raw_pwr_cl_ddr_52_195;
836 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
837 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
b87d8dbf 838 break;
93fc5a47
SJ
839 case MMC_VDD_27_28:
840 case MMC_VDD_28_29:
841 case MMC_VDD_29_30:
842 case MMC_VDD_30_31:
843 case MMC_VDD_31_32:
b87d8dbf
G
844 case MMC_VDD_32_33:
845 case MMC_VDD_33_34:
846 case MMC_VDD_34_35:
847 case MMC_VDD_35_36:
2385049d
SJ
848 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
849 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
850 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
60443712 851 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
2385049d
SJ
852 ext_csd->raw_pwr_cl_52_360 :
853 ext_csd->raw_pwr_cl_ddr_52_360;
854 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
0a5b6438
SJ
855 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
856 ext_csd->raw_pwr_cl_ddr_200_360 :
857 ext_csd->raw_pwr_cl_200_360;
b87d8dbf
G
858 break;
859 default:
6606110d
JP
860 pr_warn("%s: Voltage range not supported for power class\n",
861 mmc_hostname(host));
b87d8dbf
G
862 return -EINVAL;
863 }
864
b87d8dbf
G
865 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
866 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
867 EXT_CSD_PWR_CL_8BIT_SHIFT;
868 else
869 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
870 EXT_CSD_PWR_CL_4BIT_SHIFT;
871
872 /* If the power class is different from the default value */
873 if (pwrclass_val > 0) {
874 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
875 EXT_CSD_POWER_CLASS,
876 pwrclass_val,
71fe3eb0 877 card->ext_csd.generic_cmd6_time);
b87d8dbf
G
878 }
879
880 return err;
881}
882
2385049d
SJ
883static int mmc_select_powerclass(struct mmc_card *card)
884{
885 struct mmc_host *host = card->host;
886 u32 bus_width, ext_csd_bits;
887 int err, ddr;
888
889 /* Power class selection is supported for versions >= 4.0 */
148bcab2 890 if (!mmc_can_ext_csd(card))
2385049d
SJ
891 return 0;
892
893 bus_width = host->ios.bus_width;
894 /* Power class values are defined only for 4/8 bit bus */
895 if (bus_width == MMC_BUS_WIDTH_1)
896 return 0;
897
898 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
899 if (ddr)
900 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
901 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
902 else
903 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
904 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
905
906 err = __mmc_select_powerclass(card, ext_csd_bits);
907 if (err)
908 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
909 mmc_hostname(host), 1 << bus_width, ddr);
910
911 return err;
912}
913
a4924c71 914/*
577fb131 915 * Set the bus speed for the selected speed mode.
a4924c71 916 */
577fb131
SJ
917static void mmc_set_bus_speed(struct mmc_card *card)
918{
919 unsigned int max_dtr = (unsigned int)-1;
920
0a5b6438
SJ
921 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
922 max_dtr > card->ext_csd.hs200_max_dtr)
577fb131
SJ
923 max_dtr = card->ext_csd.hs200_max_dtr;
924 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
925 max_dtr = card->ext_csd.hs_max_dtr;
926 else if (max_dtr > card->csd.max_dtr)
927 max_dtr = card->csd.max_dtr;
928
929 mmc_set_clock(card->host, max_dtr);
930}
931
932/*
933 * Select the bus width amoung 4-bit and 8-bit(SDR).
934 * If the bus width is changed successfully, return the selected width value.
935 * Zero is returned instead of error value if the wide width is not supported.
936 */
937static int mmc_select_bus_width(struct mmc_card *card)
a4924c71 938{
a4924c71 939 static unsigned ext_csd_bits[] = {
a4924c71 940 EXT_CSD_BUS_WIDTH_8,
577fb131 941 EXT_CSD_BUS_WIDTH_4,
a4924c71
G
942 };
943 static unsigned bus_widths[] = {
a4924c71 944 MMC_BUS_WIDTH_8,
577fb131 945 MMC_BUS_WIDTH_4,
a4924c71 946 };
577fb131
SJ
947 struct mmc_host *host = card->host;
948 unsigned idx, bus_width = 0;
949 int err = 0;
a4924c71 950
1c2d26e3 951 if (!mmc_can_ext_csd(card) ||
577fb131
SJ
952 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
953 return 0;
a4924c71 954
577fb131 955 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
a4924c71
G
956
957 /*
958 * Unlike SD, MMC cards dont have a configuration register to notify
959 * supported bus width. So bus test command should be run to identify
960 * the supported bus width or compare the ext csd values of current
961 * bus width and ext csd values of 1 bit mode read earlier.
962 */
577fb131 963 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
a4924c71
G
964 /*
965 * Host is capable of 8bit transfer, then switch
966 * the device to work in 8bit transfer mode. If the
967 * mmc switch command returns error then switch to
968 * 4bit transfer mode. On success set the corresponding
969 * bus width on the host.
970 */
971 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
972 EXT_CSD_BUS_WIDTH,
973 ext_csd_bits[idx],
974 card->ext_csd.generic_cmd6_time);
975 if (err)
976 continue;
977
577fb131
SJ
978 bus_width = bus_widths[idx];
979 mmc_set_bus_width(host, bus_width);
a4924c71 980
577fb131
SJ
981 /*
982 * If controller can't handle bus width test,
983 * compare ext_csd previously read in 1 bit mode
984 * against ext_csd at new bus width
985 */
a4924c71 986 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
577fb131 987 err = mmc_compare_ext_csds(card, bus_width);
a4924c71 988 else
577fb131
SJ
989 err = mmc_bus_test(card, bus_width);
990
991 if (!err) {
992 err = bus_width;
a4924c71 993 break;
577fb131
SJ
994 } else {
995 pr_warn("%s: switch to bus width %d failed\n",
ed9feec7 996 mmc_hostname(host), 1 << bus_width);
577fb131 997 }
a4924c71
G
998 }
999
577fb131
SJ
1000 return err;
1001}
1002
08573eaf
CJ
1003/* Caller must hold re-tuning */
1004static int mmc_switch_status(struct mmc_card *card)
1005{
1006 u32 status;
1007 int err;
1008
1009 err = mmc_send_status(card, &status);
1010 if (err)
1011 return err;
1012
1013 return mmc_switch_status_error(card->host, status);
1014}
1015
577fb131
SJ
1016/*
1017 * Switch to the high-speed mode
1018 */
1019static int mmc_select_hs(struct mmc_card *card)
1020{
1021 int err;
1022
1023 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1024 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1025 card->ext_csd.generic_cmd6_time,
08573eaf
CJ
1026 true, false, true);
1027 if (!err) {
577fb131 1028 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
08573eaf
CJ
1029 err = mmc_switch_status(card);
1030 }
577fb131 1031
67d35960
JL
1032 if (err)
1033 pr_warn("%s: switch to high-speed failed, err:%d\n",
1034 mmc_hostname(card->host), err);
1035
577fb131
SJ
1036 return err;
1037}
1038
1039/*
1040 * Activate wide bus and DDR if supported.
1041 */
1042static int mmc_select_hs_ddr(struct mmc_card *card)
1043{
1044 struct mmc_host *host = card->host;
1045 u32 bus_width, ext_csd_bits;
1046 int err = 0;
1047
1048 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1049 return 0;
1050
1051 bus_width = host->ios.bus_width;
1052 if (bus_width == MMC_BUS_WIDTH_1)
1053 return 0;
1054
1055 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1056 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1057
1058 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1059 EXT_CSD_BUS_WIDTH,
1060 ext_csd_bits,
1061 card->ext_csd.generic_cmd6_time);
1062 if (err) {
4b75bffc 1063 pr_err("%s: switch to bus width %d ddr failed\n",
577fb131
SJ
1064 mmc_hostname(host), 1 << bus_width);
1065 return err;
1066 }
1067
1068 /*
1069 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1070 * signaling.
1071 *
1072 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1073 *
1074 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1075 * in the JEDEC spec for DDR.
1076 *
312449ef
CD
1077 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1078 * host controller can support this, like some of the SDHCI
1079 * controller which connect to an eMMC device. Some of these
1080 * host controller still needs to use 1.8v vccq for supporting
1081 * DDR mode.
1082 *
1083 * So the sequence will be:
1084 * if (host and device can both support 1.2v IO)
1085 * use 1.2v IO;
1086 * else if (host and device can both support 1.8v IO)
1087 * use 1.8v IO;
1088 * so if host and device can only support 3.3v IO, this is the
1089 * last choice.
577fb131
SJ
1090 *
1091 * WARNING: eMMC rules are NOT the same as SD DDR
1092 */
312449ef
CD
1093 err = -EINVAL;
1094 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1095 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
577fb131 1096
312449ef
CD
1097 if (err && (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V))
1098 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1099
1100 /* make sure vccq is 3.3v after switching disaster */
1101 if (err)
1102 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1103
1104 if (!err)
1105 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
577fb131
SJ
1106
1107 return err;
1108}
1109
0a5b6438
SJ
1110static int mmc_select_hs400(struct mmc_card *card)
1111{
1112 struct mmc_host *host = card->host;
51b12f77 1113 unsigned int max_dtr;
0a5b6438 1114 int err = 0;
cc4f414c 1115 u8 val;
0a5b6438
SJ
1116
1117 /*
1118 * HS400 mode requires 8-bit bus width
1119 */
1120 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1121 host->ios.bus_width == MMC_BUS_WIDTH_8))
1122 return 0;
1123
51b12f77 1124 /* Switch card to HS mode */
adb24d42 1125 val = EXT_CSD_TIMING_HS;
0a5b6438 1126 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
cc4f414c 1127 EXT_CSD_HS_TIMING, val,
0a5b6438 1128 card->ext_csd.generic_cmd6_time,
08573eaf 1129 true, false, true);
0a5b6438 1130 if (err) {
4b75bffc 1131 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
0a5b6438
SJ
1132 mmc_hostname(host), err);
1133 return err;
1134 }
1135
51b12f77
AH
1136 /* Set host controller to HS timing */
1137 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1138
649c6059
ZX
1139 /* Reduce frequency to HS frequency */
1140 max_dtr = card->ext_csd.hs_max_dtr;
1141 mmc_set_clock(host, max_dtr);
1142
08573eaf
CJ
1143 err = mmc_switch_status(card);
1144 if (err)
1145 goto out_err;
d2302933
AH
1146
1147 /* Switch card to DDR */
0a5b6438
SJ
1148 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1149 EXT_CSD_BUS_WIDTH,
1150 EXT_CSD_DDR_BUS_WIDTH_8,
1151 card->ext_csd.generic_cmd6_time);
1152 if (err) {
4b75bffc 1153 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
0a5b6438
SJ
1154 mmc_hostname(host), err);
1155 return err;
1156 }
1157
d2302933 1158 /* Switch card to HS400 */
cc4f414c
AH
1159 val = EXT_CSD_TIMING_HS400 |
1160 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
0a5b6438 1161 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
cc4f414c 1162 EXT_CSD_HS_TIMING, val,
0a5b6438 1163 card->ext_csd.generic_cmd6_time,
08573eaf 1164 true, false, true);
0a5b6438 1165 if (err) {
4b75bffc 1166 pr_err("%s: switch to hs400 failed, err:%d\n",
0a5b6438
SJ
1167 mmc_hostname(host), err);
1168 return err;
1169 }
1170
d2302933 1171 /* Set host controller to HS400 timing and frequency */
0a5b6438
SJ
1172 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1173 mmc_set_bus_speed(card);
1174
08573eaf
CJ
1175 err = mmc_switch_status(card);
1176 if (err)
1177 goto out_err;
d2302933 1178
0a5b6438 1179 return 0;
d2302933
AH
1180
1181out_err:
1182 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1183 __func__, err);
1184 return err;
0a5b6438
SJ
1185}
1186
6376f69d
AH
1187int mmc_hs200_to_hs400(struct mmc_card *card)
1188{
1189 return mmc_select_hs400(card);
1190}
1191
6376f69d
AH
1192int mmc_hs400_to_hs200(struct mmc_card *card)
1193{
1194 struct mmc_host *host = card->host;
6376f69d
AH
1195 unsigned int max_dtr;
1196 int err;
cc4f414c 1197 u8 val;
6376f69d 1198
6376f69d
AH
1199 /* Reduce frequency to HS */
1200 max_dtr = card->ext_csd.hs_max_dtr;
1201 mmc_set_clock(host, max_dtr);
1202
1203 /* Switch HS400 to HS DDR */
adb24d42 1204 val = EXT_CSD_TIMING_HS;
6376f69d 1205 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
cc4f414c 1206 val, card->ext_csd.generic_cmd6_time,
08573eaf 1207 true, false, true);
6376f69d
AH
1208 if (err)
1209 goto out_err;
1210
1211 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1212
08573eaf
CJ
1213 err = mmc_switch_status(card);
1214 if (err)
1215 goto out_err;
6376f69d
AH
1216
1217 /* Switch HS DDR to HS */
1218 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1219 EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
08573eaf 1220 true, false, true);
6376f69d
AH
1221 if (err)
1222 goto out_err;
1223
1224 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1225
08573eaf
CJ
1226 err = mmc_switch_status(card);
1227 if (err)
1228 goto out_err;
6376f69d
AH
1229
1230 /* Switch HS to HS200 */
cc4f414c
AH
1231 val = EXT_CSD_TIMING_HS200 |
1232 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
6376f69d 1233 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
08573eaf
CJ
1234 val, card->ext_csd.generic_cmd6_time,
1235 true, false, true);
6376f69d
AH
1236 if (err)
1237 goto out_err;
1238
1239 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1240
08573eaf
CJ
1241 err = mmc_switch_status(card);
1242 if (err)
1243 goto out_err;
6376f69d
AH
1244
1245 mmc_set_bus_speed(card);
1246
1247 return 0;
1248
1249out_err:
1250 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1251 __func__, err);
1252 return err;
1253}
1254
81ac2af6
SL
1255static int mmc_select_hs400es(struct mmc_card *card)
1256{
1257 struct mmc_host *host = card->host;
1258 int err = 0;
1259 u8 val;
1260
1261 if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1262 err = -ENOTSUPP;
1263 goto out_err;
1264 }
1265
1720d354
SL
1266 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
1267 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1268
1269 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
1270 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1271
1272 /* If fails try again during next card power cycle */
1273 if (err)
1274 goto out_err;
1275
81ac2af6
SL
1276 err = mmc_select_bus_width(card);
1277 if (err < 0)
1278 goto out_err;
1279
1280 /* Switch card to HS mode */
1281 err = mmc_select_hs(card);
67d35960 1282 if (err)
81ac2af6 1283 goto out_err;
81ac2af6
SL
1284
1285 err = mmc_switch_status(card);
1286 if (err)
1287 goto out_err;
1288
1289 /* Switch card to DDR with strobe bit */
1290 val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1291 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1292 EXT_CSD_BUS_WIDTH,
1293 val,
1294 card->ext_csd.generic_cmd6_time);
1295 if (err) {
1296 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1297 mmc_hostname(host), err);
1298 goto out_err;
1299 }
1300
1301 /* Switch card to HS400 */
1302 val = EXT_CSD_TIMING_HS400 |
1303 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1304 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1305 EXT_CSD_HS_TIMING, val,
1306 card->ext_csd.generic_cmd6_time,
1307 true, false, true);
1308 if (err) {
1309 pr_err("%s: switch to hs400es failed, err:%d\n",
1310 mmc_hostname(host), err);
1311 goto out_err;
1312 }
1313
1314 /* Set host controller to HS400 timing and frequency */
1315 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1316
1317 /* Controller enable enhanced strobe function */
1318 host->ios.enhanced_strobe = true;
1319 if (host->ops->hs400_enhanced_strobe)
1320 host->ops->hs400_enhanced_strobe(host, &host->ios);
1321
1322 err = mmc_switch_status(card);
1323 if (err)
1324 goto out_err;
1325
1326 return 0;
1327
1328out_err:
1329 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1330 __func__, err);
1331 return err;
1332}
1333
cc4f414c
AH
1334static void mmc_select_driver_type(struct mmc_card *card)
1335{
1336 int card_drv_type, drive_strength, drv_type;
1337
1338 card_drv_type = card->ext_csd.raw_driver_strength |
1339 mmc_driver_type_mask(0);
1340
1341 drive_strength = mmc_select_drive_strength(card,
1342 card->ext_csd.hs200_max_dtr,
1343 card_drv_type, &drv_type);
1344
1345 card->drive_strength = drive_strength;
1346
1347 if (drv_type)
1348 mmc_set_driver_type(card->host, drv_type);
1349}
1350
577fb131
SJ
1351/*
1352 * For device supporting HS200 mode, the following sequence
1353 * should be done before executing the tuning process.
1354 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1355 * 2. switch to HS200 mode
1356 * 3. set the clock to > 52Mhz and <=200MHz
1357 */
1358static int mmc_select_hs200(struct mmc_card *card)
1359{
1360 struct mmc_host *host = card->host;
e51534c8 1361 unsigned int old_timing, old_signal_voltage;
577fb131 1362 int err = -EINVAL;
cc4f414c 1363 u8 val;
577fb131 1364
e51534c8 1365 old_signal_voltage = host->ios.signal_voltage;
577fb131
SJ
1366 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1367 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1368
1369 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1370 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1371
1372 /* If fails try again during next card power cycle */
1373 if (err)
e51534c8 1374 return err;
577fb131 1375
cc4f414c
AH
1376 mmc_select_driver_type(card);
1377
577fb131
SJ
1378 /*
1379 * Set the bus width(4 or 8) with host's support and
1380 * switch to HS200 mode if bus width is set successfully.
1381 */
1382 err = mmc_select_bus_width(card);
8b7be8f2 1383 if (err > 0) {
cc4f414c
AH
1384 val = EXT_CSD_TIMING_HS200 |
1385 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
4509f847 1386 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
cc4f414c 1387 EXT_CSD_HS_TIMING, val,
577fb131 1388 card->ext_csd.generic_cmd6_time,
08573eaf 1389 true, false, true);
1815e61b
AH
1390 if (err)
1391 goto err;
1392 old_timing = host->ios.timing;
1393 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
08573eaf
CJ
1394
1395 err = mmc_switch_status(card);
1396 /*
1397 * mmc_select_timing() assumes timing has not changed if
1398 * it is a switch error.
1399 */
1400 if (err == -EBADMSG)
1401 mmc_set_timing(host, old_timing);
577fb131 1402 }
a4924c71 1403err:
e51534c8
DA
1404 if (err) {
1405 /* fall back to the old signal voltage, if fails report error */
1406 if (__mmc_set_signal_voltage(host, old_signal_voltage))
1407 err = -EIO;
1408
1815e61b
AH
1409 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1410 __func__, err);
e51534c8 1411 }
a4924c71
G
1412 return err;
1413}
1414
577fb131 1415/*
81ac2af6 1416 * Activate High Speed, HS200 or HS400ES mode if supported.
577fb131
SJ
1417 */
1418static int mmc_select_timing(struct mmc_card *card)
1419{
1420 int err = 0;
1421
148bcab2 1422 if (!mmc_can_ext_csd(card))
577fb131
SJ
1423 goto bus_speed;
1424
81ac2af6
SL
1425 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1426 err = mmc_select_hs400es(card);
1427 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
577fb131
SJ
1428 err = mmc_select_hs200(card);
1429 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1430 err = mmc_select_hs(card);
1431
1432 if (err && err != -EBADMSG)
1433 return err;
1434
577fb131
SJ
1435bus_speed:
1436 /*
1437 * Set the bus speed to the selected bus timing.
1438 * If timing is not selected, backward compatible is the default.
1439 */
1440 mmc_set_bus_speed(card);
0400ed0a 1441 return 0;
577fb131
SJ
1442}
1443
1444/*
1445 * Execute tuning sequence to seek the proper bus operating
0a5b6438 1446 * conditions for HS200 and HS400, which sends CMD21 to the device.
577fb131
SJ
1447 */
1448static int mmc_hs200_tuning(struct mmc_card *card)
1449{
1450 struct mmc_host *host = card->host;
577fb131 1451
0a5b6438
SJ
1452 /*
1453 * Timing should be adjusted to the HS400 target
1454 * operation frequency for tuning process
1455 */
1456 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1457 host->ios.bus_width == MMC_BUS_WIDTH_8)
1458 if (host->ops->prepare_hs400_tuning)
1459 host->ops->prepare_hs400_tuning(host, &host->ios);
1460
63e415c6 1461 return mmc_execute_tuning(card);
577fb131
SJ
1462}
1463
7ea239d9 1464/*
6abaa0c9
PO
1465 * Handle the detection and initialisation of a card.
1466 *
8769392b 1467 * In the case of a resume, "oldcard" will contain the card
6abaa0c9 1468 * we're trying to reinitialise.
7ea239d9 1469 */
8c75deae 1470static int mmc_init_card(struct mmc_host *host, u32 ocr,
6abaa0c9 1471 struct mmc_card *oldcard)
7ea239d9
PO
1472{
1473 struct mmc_card *card;
577fb131 1474 int err;
7ea239d9 1475 u32 cid[4];
b676f039 1476 u32 rocr;
7ea239d9
PO
1477
1478 BUG_ON(!host);
d84075c8 1479 WARN_ON(!host->claimed);
7ea239d9 1480
44669034
SNX
1481 /* Set correct bus mode for MMC before attempting init */
1482 if (!mmc_host_is_spi(host))
1483 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1484
7ea239d9
PO
1485 /*
1486 * Since we're changing the OCR value, we seem to
1487 * need to tell some cards to go back to the idle
1488 * state. We wait 1ms to give cards time to
1489 * respond.
c3805467 1490 * mmc_go_idle is needed for eMMC that are asleep
7ea239d9
PO
1491 */
1492 mmc_go_idle(host);
1493
1494 /* The extra bit indicates that we support high capacity */
b676f039 1495 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
17b0429d 1496 if (err)
6abaa0c9 1497 goto err;
7ea239d9 1498
af517150
DB
1499 /*
1500 * For SPI, enable CRC as appropriate.
1501 */
1502 if (mmc_host_is_spi(host)) {
1503 err = mmc_spi_set_crc(host, use_spi_crc);
1504 if (err)
1505 goto err;
1506 }
1507
7ea239d9
PO
1508 /*
1509 * Fetch CID from card.
1510 */
af517150
DB
1511 if (mmc_host_is_spi(host))
1512 err = mmc_send_cid(host, cid);
1513 else
1514 err = mmc_all_send_cid(host, cid);
17b0429d 1515 if (err)
7ea239d9
PO
1516 goto err;
1517
6abaa0c9 1518 if (oldcard) {
adf66a0d
PO
1519 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1520 err = -ENOENT;
6abaa0c9 1521 goto err;
adf66a0d 1522 }
6abaa0c9
PO
1523
1524 card = oldcard;
1525 } else {
1526 /*
1527 * Allocate card structure.
1528 */
51ec92e2 1529 card = mmc_alloc_card(host, &mmc_type);
adf66a0d
PO
1530 if (IS_ERR(card)) {
1531 err = PTR_ERR(card);
6abaa0c9 1532 goto err;
adf66a0d 1533 }
7ea239d9 1534
69041150 1535 card->ocr = ocr;
6abaa0c9
PO
1536 card->type = MMC_TYPE_MMC;
1537 card->rca = 1;
1538 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1539 }
7ea239d9 1540
eac86321
DA
1541 /*
1542 * Call the optional HC's init_card function to handle quirks.
1543 */
1544 if (host->ops->init_card)
1545 host->ops->init_card(host, card);
1546
7ea239d9 1547 /*
af517150 1548 * For native busses: set card RCA and quit open drain mode.
7ea239d9 1549 */
af517150
DB
1550 if (!mmc_host_is_spi(host)) {
1551 err = mmc_set_relative_addr(card);
1552 if (err)
1553 goto free_card;
7ea239d9 1554
af517150
DB
1555 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1556 }
7ea239d9 1557
6abaa0c9
PO
1558 if (!oldcard) {
1559 /*
1560 * Fetch CSD from card.
1561 */
1562 err = mmc_send_csd(card, card->raw_csd);
17b0429d 1563 if (err)
6abaa0c9 1564 goto free_card;
7ea239d9 1565
bd766312 1566 err = mmc_decode_csd(card);
adf66a0d 1567 if (err)
bd766312
PO
1568 goto free_card;
1569 err = mmc_decode_cid(card);
adf66a0d 1570 if (err)
bd766312 1571 goto free_card;
6abaa0c9 1572 }
7ea239d9 1573
3d705d14
SH
1574 /*
1575 * handling only for cards supporting DSR and hosts requesting
1576 * DSR configuration
1577 */
1578 if (card->csd.dsr_imp && host->dsr_req)
1579 mmc_set_dsr(host);
1580
7ea239d9 1581 /*
89a73cf5 1582 * Select card, as all following commands rely on that.
7ea239d9 1583 */
af517150
DB
1584 if (!mmc_host_is_spi(host)) {
1585 err = mmc_select_card(card);
1586 if (err)
1587 goto free_card;
1588 }
7ea239d9 1589
6abaa0c9 1590 if (!oldcard) {
076ec38a
UH
1591 /* Read extended CSD. */
1592 err = mmc_read_ext_csd(card);
17b0429d 1593 if (err)
6abaa0c9 1594 goto free_card;
b676f039 1595
87e88659
MY
1596 /*
1597 * If doing byte addressing, check if required to do sector
b676f039
PR
1598 * addressing. Handle the case of <2GB cards needing sector
1599 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1600 * ocr register has bit 30 set for sector addressing.
1601 */
87e88659 1602 if (rocr & BIT(30))
b676f039
PR
1603 mmc_card_set_blockaddr(card);
1604
dfe86cba
AH
1605 /* Erase size depends on CSD and Extended CSD */
1606 mmc_set_erase_size(card);
6abaa0c9 1607 }
7ea239d9 1608
709de99d
CD
1609 /*
1610 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
25985edc 1611 * bit. This bit will be lost every time after a reset or power off.
709de99d 1612 */
69803d4f 1613 if (card->ext_csd.partition_setting_completed ||
83bb24aa 1614 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
709de99d 1615 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
b23cf0bd
SJ
1616 EXT_CSD_ERASE_GROUP_DEF, 1,
1617 card->ext_csd.generic_cmd6_time);
709de99d
CD
1618
1619 if (err && err != -EBADMSG)
1620 goto free_card;
1621
1622 if (err) {
1623 err = 0;
1624 /*
1625 * Just disable enhanced area off & sz
1626 * will try to enable ERASE_GROUP_DEF
1627 * during next time reinit
1628 */
1629 card->ext_csd.enhanced_area_offset = -EINVAL;
1630 card->ext_csd.enhanced_area_size = -EINVAL;
1631 } else {
1632 card->ext_csd.erase_group_def = 1;
1633 /*
1634 * enable ERASE_GRP_DEF successfully.
1635 * This will affect the erase size, so
1636 * here need to reset erase size
1637 */
1638 mmc_set_erase_size(card);
1639 }
1640 }
1641
41e2a489
PR
1642 /*
1643 * Ensure eMMC user default partition is enabled
1644 */
371a689f
AW
1645 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1646 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1647 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1648 card->ext_csd.part_config,
1649 card->ext_csd.part_time);
1650 if (err && err != -EBADMSG)
1651 goto free_card;
41e2a489
PR
1652 }
1653
bec8726a 1654 /*
43235679 1655 * Enable power_off_notification byte in the ext_csd register
bec8726a 1656 */
43235679 1657 if (card->ext_csd.rev >= 6) {
bec8726a
G
1658 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1659 EXT_CSD_POWER_OFF_NOTIFICATION,
1660 EXT_CSD_POWER_ON,
1661 card->ext_csd.generic_cmd6_time);
1662 if (err && err != -EBADMSG)
1663 goto free_card;
bec8726a 1664
96a85d54
G
1665 /*
1666 * The err can be -EBADMSG or 0,
1667 * so check for success and update the flag
1668 */
1669 if (!err)
e6c08586 1670 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
96a85d54 1671 }
bec8726a 1672
89a73cf5 1673 /*
577fb131 1674 * Select timing interface
dfc13e84 1675 */
577fb131
SJ
1676 err = mmc_select_timing(card);
1677 if (err)
1678 goto free_card;
dfc13e84 1679
a4924c71 1680 if (mmc_card_hs200(card)) {
577fb131
SJ
1681 err = mmc_hs200_tuning(card);
1682 if (err)
4b75bffc 1683 goto free_card;
0a5b6438
SJ
1684
1685 err = mmc_select_hs400(card);
1686 if (err)
4b75bffc 1687 goto free_card;
577fb131
SJ
1688 } else if (mmc_card_hs(card)) {
1689 /* Select the desired bus width optionally */
1690 err = mmc_select_bus_width(card);
8b7be8f2 1691 if (err > 0) {
577fb131
SJ
1692 err = mmc_select_hs_ddr(card);
1693 if (err)
4b75bffc 1694 goto free_card;
ef0b27d4 1695 }
89a73cf5
PO
1696 }
1697
2385049d
SJ
1698 /*
1699 * Choose the power class with selected bus interface
1700 */
1701 mmc_select_powerclass(card);
1702
52d0974e
SJ
1703 /*
1704 * Enable HPI feature (if supported)
1705 */
1706 if (card->ext_csd.hpi) {
1707 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1708 EXT_CSD_HPI_MGMT, 1,
1709 card->ext_csd.generic_cmd6_time);
1710 if (err && err != -EBADMSG)
1711 goto free_card;
1712 if (err) {
6606110d
JP
1713 pr_warn("%s: Enabling HPI failed\n",
1714 mmc_hostname(card->host));
52d0974e
SJ
1715 err = 0;
1716 } else
1717 card->ext_csd.hpi_en = 1;
1718 }
1719
881d1c25
SJ
1720 /*
1721 * If cache size is higher than 0, this indicates
1722 * the existence of cache and it can be turned on.
1723 */
5320226a
P
1724 if (!mmc_card_broken_hpi(card) &&
1725 card->ext_csd.cache_size > 0) {
881d1c25 1726 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
8bc0678b
SJ
1727 EXT_CSD_CACHE_CTRL, 1,
1728 card->ext_csd.generic_cmd6_time);
881d1c25
SJ
1729 if (err && err != -EBADMSG)
1730 goto free_card;
1731
1732 /*
1733 * Only if no error, cache is turned on successfully.
1734 */
8bc0678b 1735 if (err) {
6606110d
JP
1736 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1737 mmc_hostname(card->host), err);
8bc0678b
SJ
1738 card->ext_csd.cache_ctrl = 0;
1739 err = 0;
1740 } else {
1741 card->ext_csd.cache_ctrl = 1;
1742 }
881d1c25
SJ
1743 }
1744
abd9ac14
SJ
1745 /*
1746 * The mandatory minimum values are defined for packed command.
1747 * read: 5, write: 3
1748 */
1749 if (card->ext_csd.max_packed_writes >= 3 &&
1750 card->ext_csd.max_packed_reads >= 5 &&
1751 host->caps2 & MMC_CAP2_PACKED_CMD) {
1752 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1753 EXT_CSD_EXP_EVENTS_CTRL,
1754 EXT_CSD_PACKED_EVENT_EN,
1755 card->ext_csd.generic_cmd6_time);
1756 if (err && err != -EBADMSG)
1757 goto free_card;
1758 if (err) {
1759 pr_warn("%s: Enabling packed event failed\n",
1760 mmc_hostname(card->host));
1761 card->ext_csd.packed_event_en = 0;
1762 err = 0;
1763 } else {
1764 card->ext_csd.packed_event_en = 1;
1765 }
1766 }
1767
6abaa0c9
PO
1768 if (!oldcard)
1769 host->card = card;
1770
17b0429d 1771 return 0;
6abaa0c9
PO
1772
1773free_card:
1774 if (!oldcard)
1775 mmc_remove_card(card);
1776err:
adf66a0d 1777 return err;
6abaa0c9
PO
1778}
1779
07a68216
UH
1780static int mmc_can_sleep(struct mmc_card *card)
1781{
1782 return (card && card->ext_csd.rev >= 3);
1783}
1784
1785static int mmc_sleep(struct mmc_host *host)
1786{
1787 struct mmc_command cmd = {0};
1788 struct mmc_card *card = host->card;
cb962e04 1789 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
07a68216
UH
1790 int err;
1791
436f8daa
AH
1792 /* Re-tuning can't be done once the card is deselected */
1793 mmc_retune_hold(host);
1794
07a68216
UH
1795 err = mmc_deselect_cards(host);
1796 if (err)
436f8daa 1797 goto out_release;
07a68216
UH
1798
1799 cmd.opcode = MMC_SLEEP_AWAKE;
1800 cmd.arg = card->rca << 16;
1801 cmd.arg |= 1 << 15;
1802
cb962e04
UH
1803 /*
1804 * If the max_busy_timeout of the host is specified, validate it against
1805 * the sleep cmd timeout. A failure means we need to prevent the host
1806 * from doing hw busy detection, which is done by converting to a R1
1807 * response instead of a R1B.
1808 */
1809 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1810 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1811 } else {
1812 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1813 cmd.busy_timeout = timeout_ms;
1814 }
1815
07a68216
UH
1816 err = mmc_wait_for_cmd(host, &cmd, 0);
1817 if (err)
436f8daa 1818 goto out_release;
07a68216
UH
1819
1820 /*
1821 * If the host does not wait while the card signals busy, then we will
1822 * will have to wait the sleep/awake timeout. Note, we cannot use the
1823 * SEND_STATUS command to poll the status because that command (and most
1824 * others) is invalid while the card sleeps.
1825 */
cb962e04
UH
1826 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1827 mmc_delay(timeout_ms);
07a68216 1828
436f8daa
AH
1829out_release:
1830 mmc_retune_release(host);
07a68216
UH
1831 return err;
1832}
1833
e6c08586
UH
1834static int mmc_can_poweroff_notify(const struct mmc_card *card)
1835{
1836 return card &&
1837 mmc_card_mmc(card) &&
1838 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1839}
1840
1841static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1842{
1843 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1844 int err;
1845
1846 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1847 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1848 timeout = card->ext_csd.power_off_longtime;
1849
878e200b
UH
1850 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1851 EXT_CSD_POWER_OFF_NOTIFICATION,
4509f847 1852 notify_type, timeout, true, false, false);
e6c08586
UH
1853 if (err)
1854 pr_err("%s: Power Off Notification timed out, %u\n",
1855 mmc_hostname(card->host), timeout);
1856
1857 /* Disable the power off notification after the switch operation. */
1858 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1859
1860 return err;
1861}
1862
6abaa0c9
PO
1863/*
1864 * Host is being removed. Free up the current card.
1865 */
1866static void mmc_remove(struct mmc_host *host)
1867{
1868 BUG_ON(!host);
1869 BUG_ON(!host->card);
1870
1871 mmc_remove_card(host->card);
1872 host->card = NULL;
1873}
1874
d3049504
AH
1875/*
1876 * Card detection - card is alive.
1877 */
1878static int mmc_alive(struct mmc_host *host)
1879{
1880 return mmc_send_status(host->card, NULL);
1881}
1882
6abaa0c9
PO
1883/*
1884 * Card detection callback from host.
1885 */
1886static void mmc_detect(struct mmc_host *host)
1887{
1888 int err;
1889
1890 BUG_ON(!host);
1891 BUG_ON(!host->card);
1892
e94cfef6 1893 mmc_get_card(host->card);
6abaa0c9
PO
1894
1895 /*
1896 * Just check if our card has been removed.
1897 */
d3049504 1898 err = _mmc_detect_card_removed(host);
6abaa0c9 1899
e94cfef6 1900 mmc_put_card(host->card);
6abaa0c9 1901
17b0429d 1902 if (err) {
4101c16a 1903 mmc_remove(host);
6abaa0c9
PO
1904
1905 mmc_claim_host(host);
1906 mmc_detach_bus(host);
7f7e4129 1907 mmc_power_off(host);
6abaa0c9
PO
1908 mmc_release_host(host);
1909 }
1910}
1911
03d071fc 1912static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
6abaa0c9 1913{
c3805467 1914 int err = 0;
03d071fc
UH
1915 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1916 EXT_CSD_POWER_OFF_LONG;
c3805467 1917
6abaa0c9
PO
1918 BUG_ON(!host);
1919 BUG_ON(!host->card);
7ea239d9 1920
6abaa0c9 1921 mmc_claim_host(host);
881d926d 1922
9ec775f7
UH
1923 if (mmc_card_suspended(host->card))
1924 goto out;
1925
39b9431b
UH
1926 if (mmc_card_doing_bkops(host->card)) {
1927 err = mmc_stop_bkops(host->card);
1928 if (err)
1929 goto out;
1930 }
1931
10e5d965 1932 err = mmc_flush_cache(host->card);
881d926d
ME
1933 if (err)
1934 goto out;
1935
43235679 1936 if (mmc_can_poweroff_notify(host->card) &&
53275c21 1937 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
03d071fc 1938 err = mmc_poweroff_notify(host->card, notify_type);
07a68216
UH
1939 else if (mmc_can_sleep(host->card))
1940 err = mmc_sleep(host);
e6c08586 1941 else if (!mmc_host_is_spi(host))
85e727ed 1942 err = mmc_deselect_cards(host);
95cdfb72 1943
9ec775f7 1944 if (!err) {
74590263 1945 mmc_power_off(host);
9ec775f7
UH
1946 mmc_card_set_suspended(host->card);
1947 }
881d926d
ME
1948out:
1949 mmc_release_host(host);
c3805467 1950 return err;
6abaa0c9 1951}
7ea239d9 1952
03d071fc 1953/*
0cb403a2 1954 * Suspend callback
03d071fc
UH
1955 */
1956static int mmc_suspend(struct mmc_host *host)
1957{
0cb403a2
UH
1958 int err;
1959
1960 err = _mmc_suspend(host, true);
1961 if (!err) {
1962 pm_runtime_disable(&host->card->dev);
1963 pm_runtime_set_suspended(&host->card->dev);
1964 }
1965
1966 return err;
03d071fc
UH
1967}
1968
6abaa0c9 1969/*
6abaa0c9
PO
1970 * This function tries to determine if the same card is still present
1971 * and, if so, restore all state to it.
1972 */
0cb403a2 1973static int _mmc_resume(struct mmc_host *host)
6abaa0c9 1974{
9ec775f7 1975 int err = 0;
6abaa0c9
PO
1976
1977 BUG_ON(!host);
1978 BUG_ON(!host->card);
1979
1980 mmc_claim_host(host);
9ec775f7
UH
1981
1982 if (!mmc_card_suspended(host->card))
1983 goto out;
1984
69041150 1985 mmc_power_up(host, host->card->ocr);
69041150 1986 err = mmc_init_card(host, host->card->ocr, host->card);
9ec775f7 1987 mmc_card_clr_suspended(host->card);
2986d0bf 1988
9ec775f7
UH
1989out:
1990 mmc_release_host(host);
95cdfb72 1991 return err;
6abaa0c9
PO
1992}
1993
9ec775f7
UH
1994/*
1995 * Shutdown callback
1996 */
1997static int mmc_shutdown(struct mmc_host *host)
1998{
1999 int err = 0;
2000
2001 /*
2002 * In a specific case for poweroff notify, we need to resume the card
2003 * before we can shutdown it properly.
2004 */
2005 if (mmc_can_poweroff_notify(host->card) &&
2006 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
0cb403a2 2007 err = _mmc_resume(host);
9ec775f7
UH
2008
2009 if (!err)
2010 err = _mmc_suspend(host, false);
2011
2012 return err;
2013}
c4d770d7 2014
0cb403a2
UH
2015/*
2016 * Callback for resume.
2017 */
2018static int mmc_resume(struct mmc_host *host)
2019{
0cb403a2 2020 pm_runtime_enable(&host->card->dev);
c29536e8 2021 return 0;
0cb403a2
UH
2022}
2023
c4d770d7
UH
2024/*
2025 * Callback for runtime_suspend.
2026 */
2027static int mmc_runtime_suspend(struct mmc_host *host)
2028{
2029 int err;
2030
2031 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2032 return 0;
2033
0cb403a2 2034 err = _mmc_suspend(host, true);
0cc81a8c 2035 if (err)
f42cf8d6 2036 pr_err("%s: error %d doing aggressive suspend\n",
c4d770d7 2037 mmc_hostname(host), err);
c4d770d7 2038
c4d770d7
UH
2039 return err;
2040}
2041
2042/*
2043 * Callback for runtime_resume.
2044 */
2045static int mmc_runtime_resume(struct mmc_host *host)
2046{
2047 int err;
2048
0cb403a2 2049 err = _mmc_resume(host);
520322d9 2050 if (err && err != -ENOMEDIUM)
c29536e8 2051 pr_err("%s: error %d doing runtime resume\n",
c4d770d7
UH
2052 mmc_hostname(host), err);
2053
c4d770d7
UH
2054 return 0;
2055}
2056
f855a371
JR
2057int mmc_can_reset(struct mmc_card *card)
2058{
2059 u8 rst_n_function;
2060
2061 rst_n_function = card->ext_csd.rst_n_function;
2062 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2063 return 0;
2064 return 1;
2065}
2066EXPORT_SYMBOL(mmc_can_reset);
2067
2068static int mmc_reset(struct mmc_host *host)
2069{
2070 struct mmc_card *card = host->card;
f855a371 2071
437db4c6
AH
2072 /*
2073 * In the case of recovery, we can't expect flushing the cache to work
2074 * always, but we have a go and ignore errors.
2075 */
2076 mmc_flush_cache(host->card);
2077
4e6c7178
GG
2078 if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2079 mmc_can_reset(card)) {
2080 /* If the card accept RST_n signal, send it. */
2081 mmc_set_clock(host, host->f_init);
2082 host->ops->hw_reset(host);
2083 /* Set initial state and call mmc_set_ios */
2084 mmc_set_initial_state(host);
2085 } else {
2086 /* Do a brute force power cycle */
2087 mmc_power_cycle(host, card->ocr);
2088 }
364549dd 2089 return mmc_init_card(host, card->ocr, card);
f855a371
JR
2090}
2091
6abaa0c9
PO
2092static const struct mmc_bus_ops mmc_ops = {
2093 .remove = mmc_remove,
2094 .detect = mmc_detect,
2095 .suspend = mmc_suspend,
2096 .resume = mmc_resume,
c4d770d7
UH
2097 .runtime_suspend = mmc_runtime_suspend,
2098 .runtime_resume = mmc_runtime_resume,
d3049504 2099 .alive = mmc_alive,
486fdbbc 2100 .shutdown = mmc_shutdown,
f855a371 2101 .reset = mmc_reset,
6abaa0c9
PO
2102};
2103
2104/*
2105 * Starting point for MMC card init.
2106 */
807e8e40 2107int mmc_attach_mmc(struct mmc_host *host)
6abaa0c9
PO
2108{
2109 int err;
69041150 2110 u32 ocr, rocr;
6abaa0c9
PO
2111
2112 BUG_ON(!host);
d84075c8 2113 WARN_ON(!host->claimed);
6abaa0c9 2114
44669034
SNX
2115 /* Set correct bus mode for MMC before attempting attach */
2116 if (!mmc_host_is_spi(host))
2117 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2118
807e8e40
AR
2119 err = mmc_send_op_cond(host, 0, &ocr);
2120 if (err)
2121 return err;
2122
2501c917 2123 mmc_attach_bus(host, &mmc_ops);
8f230f45
TI
2124 if (host->ocr_avail_mmc)
2125 host->ocr_avail = host->ocr_avail_mmc;
6abaa0c9 2126
af517150
DB
2127 /*
2128 * We need to get OCR a different way for SPI.
2129 */
2130 if (mmc_host_is_spi(host)) {
2131 err = mmc_spi_read_ocr(host, 1, &ocr);
2132 if (err)
2133 goto err;
2134 }
2135
69041150 2136 rocr = mmc_select_voltage(host, ocr);
6abaa0c9
PO
2137
2138 /*
2139 * Can we support the voltage of the card?
2140 */
69041150 2141 if (!rocr) {
109b5bed 2142 err = -EINVAL;
6abaa0c9 2143 goto err;
109b5bed 2144 }
6abaa0c9
PO
2145
2146 /*
2147 * Detect and init the card.
2148 */
69041150 2149 err = mmc_init_card(host, rocr, NULL);
17b0429d 2150 if (err)
6abaa0c9
PO
2151 goto err;
2152
2153 mmc_release_host(host);
4101c16a 2154 err = mmc_add_card(host->card);
7ea239d9 2155 if (err)
2986d0bf 2156 goto remove_card;
7ea239d9 2157
2860d060 2158 mmc_claim_host(host);
7ea239d9
PO
2159 return 0;
2160
2986d0bf 2161remove_card:
6abaa0c9 2162 mmc_remove_card(host->card);
2986d0bf 2163 mmc_claim_host(host);
807e8e40 2164 host->card = NULL;
7ea239d9
PO
2165err:
2166 mmc_detach_bus(host);
7ea239d9 2167
a3c76eb9 2168 pr_err("%s: error %d whilst initialising MMC card\n",
109b5bed
PO
2169 mmc_hostname(host), err);
2170
adf66a0d 2171 return err;
7ea239d9 2172}