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