Merge remote-tracking branch 'xen-tip/linux-next'
[deliverable/linux.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
5f3c1e4f
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
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);
5f3c1e4f 1272 if (err)
81ac2af6 1273 goto out_err;
81ac2af6
SL
1274
1275 err = mmc_switch_status(card);
1276 if (err)
1277 goto out_err;
1278
1279 /* Switch card to DDR with strobe bit */
1280 val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1281 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1282 EXT_CSD_BUS_WIDTH,
1283 val,
1284 card->ext_csd.generic_cmd6_time);
1285 if (err) {
1286 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1287 mmc_hostname(host), err);
1288 goto out_err;
1289 }
1290
1291 /* Switch card to HS400 */
1292 val = EXT_CSD_TIMING_HS400 |
1293 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1294 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1295 EXT_CSD_HS_TIMING, val,
1296 card->ext_csd.generic_cmd6_time,
1297 true, false, true);
1298 if (err) {
1299 pr_err("%s: switch to hs400es failed, err:%d\n",
1300 mmc_hostname(host), err);
1301 goto out_err;
1302 }
1303
1304 /* Set host controller to HS400 timing and frequency */
1305 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1306
1307 /* Controller enable enhanced strobe function */
1308 host->ios.enhanced_strobe = true;
1309 if (host->ops->hs400_enhanced_strobe)
1310 host->ops->hs400_enhanced_strobe(host, &host->ios);
1311
1312 err = mmc_switch_status(card);
1313 if (err)
1314 goto out_err;
1315
1316 return 0;
1317
1318out_err:
1319 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1320 __func__, err);
1321 return err;
1322}
1323
cc4f414c
AH
1324static void mmc_select_driver_type(struct mmc_card *card)
1325{
1326 int card_drv_type, drive_strength, drv_type;
1327
1328 card_drv_type = card->ext_csd.raw_driver_strength |
1329 mmc_driver_type_mask(0);
1330
1331 drive_strength = mmc_select_drive_strength(card,
1332 card->ext_csd.hs200_max_dtr,
1333 card_drv_type, &drv_type);
1334
1335 card->drive_strength = drive_strength;
1336
1337 if (drv_type)
1338 mmc_set_driver_type(card->host, drv_type);
1339}
1340
577fb131
SJ
1341/*
1342 * For device supporting HS200 mode, the following sequence
1343 * should be done before executing the tuning process.
1344 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1345 * 2. switch to HS200 mode
1346 * 3. set the clock to > 52Mhz and <=200MHz
1347 */
1348static int mmc_select_hs200(struct mmc_card *card)
1349{
1350 struct mmc_host *host = card->host;
e51534c8 1351 unsigned int old_timing, old_signal_voltage;
577fb131 1352 int err = -EINVAL;
cc4f414c 1353 u8 val;
577fb131 1354
e51534c8 1355 old_signal_voltage = host->ios.signal_voltage;
577fb131
SJ
1356 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1357 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1358
1359 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1360 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1361
1362 /* If fails try again during next card power cycle */
1363 if (err)
e51534c8 1364 return err;
577fb131 1365
cc4f414c
AH
1366 mmc_select_driver_type(card);
1367
577fb131
SJ
1368 /*
1369 * Set the bus width(4 or 8) with host's support and
1370 * switch to HS200 mode if bus width is set successfully.
1371 */
1372 err = mmc_select_bus_width(card);
8b7be8f2 1373 if (err > 0) {
cc4f414c
AH
1374 val = EXT_CSD_TIMING_HS200 |
1375 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
4509f847 1376 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
cc4f414c 1377 EXT_CSD_HS_TIMING, val,
577fb131 1378 card->ext_csd.generic_cmd6_time,
08573eaf 1379 true, false, true);
1815e61b
AH
1380 if (err)
1381 goto err;
1382 old_timing = host->ios.timing;
1383 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
08573eaf
CJ
1384
1385 err = mmc_switch_status(card);
1386 /*
1387 * mmc_select_timing() assumes timing has not changed if
1388 * it is a switch error.
1389 */
1390 if (err == -EBADMSG)
1391 mmc_set_timing(host, old_timing);
577fb131 1392 }
a4924c71 1393err:
e51534c8
DA
1394 if (err) {
1395 /* fall back to the old signal voltage, if fails report error */
1396 if (__mmc_set_signal_voltage(host, old_signal_voltage))
1397 err = -EIO;
1398
1815e61b
AH
1399 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1400 __func__, err);
e51534c8 1401 }
a4924c71
G
1402 return err;
1403}
1404
577fb131 1405/*
81ac2af6 1406 * Activate High Speed, HS200 or HS400ES mode if supported.
577fb131
SJ
1407 */
1408static int mmc_select_timing(struct mmc_card *card)
1409{
1410 int err = 0;
1411
148bcab2 1412 if (!mmc_can_ext_csd(card))
577fb131
SJ
1413 goto bus_speed;
1414
81ac2af6
SL
1415 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1416 err = mmc_select_hs400es(card);
1417 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
577fb131
SJ
1418 err = mmc_select_hs200(card);
1419 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1420 err = mmc_select_hs(card);
1421
1422 if (err && err != -EBADMSG)
1423 return err;
1424
577fb131
SJ
1425bus_speed:
1426 /*
1427 * Set the bus speed to the selected bus timing.
1428 * If timing is not selected, backward compatible is the default.
1429 */
1430 mmc_set_bus_speed(card);
0400ed0a 1431 return 0;
577fb131
SJ
1432}
1433
1434/*
1435 * Execute tuning sequence to seek the proper bus operating
0a5b6438 1436 * conditions for HS200 and HS400, which sends CMD21 to the device.
577fb131
SJ
1437 */
1438static int mmc_hs200_tuning(struct mmc_card *card)
1439{
1440 struct mmc_host *host = card->host;
577fb131 1441
0a5b6438
SJ
1442 /*
1443 * Timing should be adjusted to the HS400 target
1444 * operation frequency for tuning process
1445 */
1446 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1447 host->ios.bus_width == MMC_BUS_WIDTH_8)
1448 if (host->ops->prepare_hs400_tuning)
1449 host->ops->prepare_hs400_tuning(host, &host->ios);
1450
63e415c6 1451 return mmc_execute_tuning(card);
577fb131
SJ
1452}
1453
7ea239d9 1454/*
6abaa0c9
PO
1455 * Handle the detection and initialisation of a card.
1456 *
8769392b 1457 * In the case of a resume, "oldcard" will contain the card
6abaa0c9 1458 * we're trying to reinitialise.
7ea239d9 1459 */
8c75deae 1460static int mmc_init_card(struct mmc_host *host, u32 ocr,
6abaa0c9 1461 struct mmc_card *oldcard)
7ea239d9
PO
1462{
1463 struct mmc_card *card;
577fb131 1464 int err;
7ea239d9 1465 u32 cid[4];
b676f039 1466 u32 rocr;
7ea239d9
PO
1467
1468 BUG_ON(!host);
d84075c8 1469 WARN_ON(!host->claimed);
7ea239d9 1470
44669034
SNX
1471 /* Set correct bus mode for MMC before attempting init */
1472 if (!mmc_host_is_spi(host))
1473 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1474
7ea239d9
PO
1475 /*
1476 * Since we're changing the OCR value, we seem to
1477 * need to tell some cards to go back to the idle
1478 * state. We wait 1ms to give cards time to
1479 * respond.
c3805467 1480 * mmc_go_idle is needed for eMMC that are asleep
7ea239d9
PO
1481 */
1482 mmc_go_idle(host);
1483
1484 /* The extra bit indicates that we support high capacity */
b676f039 1485 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
17b0429d 1486 if (err)
6abaa0c9 1487 goto err;
7ea239d9 1488
af517150
DB
1489 /*
1490 * For SPI, enable CRC as appropriate.
1491 */
1492 if (mmc_host_is_spi(host)) {
1493 err = mmc_spi_set_crc(host, use_spi_crc);
1494 if (err)
1495 goto err;
1496 }
1497
7ea239d9
PO
1498 /*
1499 * Fetch CID from card.
1500 */
af517150
DB
1501 if (mmc_host_is_spi(host))
1502 err = mmc_send_cid(host, cid);
1503 else
1504 err = mmc_all_send_cid(host, cid);
17b0429d 1505 if (err)
7ea239d9
PO
1506 goto err;
1507
6abaa0c9 1508 if (oldcard) {
adf66a0d
PO
1509 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1510 err = -ENOENT;
6abaa0c9 1511 goto err;
adf66a0d 1512 }
6abaa0c9
PO
1513
1514 card = oldcard;
1515 } else {
1516 /*
1517 * Allocate card structure.
1518 */
51ec92e2 1519 card = mmc_alloc_card(host, &mmc_type);
adf66a0d
PO
1520 if (IS_ERR(card)) {
1521 err = PTR_ERR(card);
6abaa0c9 1522 goto err;
adf66a0d 1523 }
7ea239d9 1524
69041150 1525 card->ocr = ocr;
6abaa0c9
PO
1526 card->type = MMC_TYPE_MMC;
1527 card->rca = 1;
1528 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1529 }
7ea239d9 1530
eac86321
DA
1531 /*
1532 * Call the optional HC's init_card function to handle quirks.
1533 */
1534 if (host->ops->init_card)
1535 host->ops->init_card(host, card);
1536
7ea239d9 1537 /*
af517150 1538 * For native busses: set card RCA and quit open drain mode.
7ea239d9 1539 */
af517150
DB
1540 if (!mmc_host_is_spi(host)) {
1541 err = mmc_set_relative_addr(card);
1542 if (err)
1543 goto free_card;
7ea239d9 1544
af517150
DB
1545 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1546 }
7ea239d9 1547
6abaa0c9
PO
1548 if (!oldcard) {
1549 /*
1550 * Fetch CSD from card.
1551 */
1552 err = mmc_send_csd(card, card->raw_csd);
17b0429d 1553 if (err)
6abaa0c9 1554 goto free_card;
7ea239d9 1555
bd766312 1556 err = mmc_decode_csd(card);
adf66a0d 1557 if (err)
bd766312
PO
1558 goto free_card;
1559 err = mmc_decode_cid(card);
adf66a0d 1560 if (err)
bd766312 1561 goto free_card;
6abaa0c9 1562 }
7ea239d9 1563
3d705d14
SH
1564 /*
1565 * handling only for cards supporting DSR and hosts requesting
1566 * DSR configuration
1567 */
1568 if (card->csd.dsr_imp && host->dsr_req)
1569 mmc_set_dsr(host);
1570
7ea239d9 1571 /*
89a73cf5 1572 * Select card, as all following commands rely on that.
7ea239d9 1573 */
af517150
DB
1574 if (!mmc_host_is_spi(host)) {
1575 err = mmc_select_card(card);
1576 if (err)
1577 goto free_card;
1578 }
7ea239d9 1579
6abaa0c9 1580 if (!oldcard) {
076ec38a
UH
1581 /* Read extended CSD. */
1582 err = mmc_read_ext_csd(card);
17b0429d 1583 if (err)
6abaa0c9 1584 goto free_card;
b676f039 1585
87e88659
MY
1586 /*
1587 * If doing byte addressing, check if required to do sector
b676f039
PR
1588 * addressing. Handle the case of <2GB cards needing sector
1589 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1590 * ocr register has bit 30 set for sector addressing.
1591 */
87e88659 1592 if (rocr & BIT(30))
b676f039
PR
1593 mmc_card_set_blockaddr(card);
1594
dfe86cba
AH
1595 /* Erase size depends on CSD and Extended CSD */
1596 mmc_set_erase_size(card);
6abaa0c9 1597 }
7ea239d9 1598
709de99d
CD
1599 /*
1600 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
25985edc 1601 * bit. This bit will be lost every time after a reset or power off.
709de99d 1602 */
69803d4f 1603 if (card->ext_csd.partition_setting_completed ||
83bb24aa 1604 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
709de99d 1605 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
b23cf0bd
SJ
1606 EXT_CSD_ERASE_GROUP_DEF, 1,
1607 card->ext_csd.generic_cmd6_time);
709de99d
CD
1608
1609 if (err && err != -EBADMSG)
1610 goto free_card;
1611
1612 if (err) {
1613 err = 0;
1614 /*
1615 * Just disable enhanced area off & sz
1616 * will try to enable ERASE_GROUP_DEF
1617 * during next time reinit
1618 */
1619 card->ext_csd.enhanced_area_offset = -EINVAL;
1620 card->ext_csd.enhanced_area_size = -EINVAL;
1621 } else {
1622 card->ext_csd.erase_group_def = 1;
1623 /*
1624 * enable ERASE_GRP_DEF successfully.
1625 * This will affect the erase size, so
1626 * here need to reset erase size
1627 */
1628 mmc_set_erase_size(card);
1629 }
1630 }
1631
41e2a489
PR
1632 /*
1633 * Ensure eMMC user default partition is enabled
1634 */
371a689f
AW
1635 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1636 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1637 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1638 card->ext_csd.part_config,
1639 card->ext_csd.part_time);
1640 if (err && err != -EBADMSG)
1641 goto free_card;
41e2a489
PR
1642 }
1643
bec8726a 1644 /*
43235679 1645 * Enable power_off_notification byte in the ext_csd register
bec8726a 1646 */
43235679 1647 if (card->ext_csd.rev >= 6) {
bec8726a
G
1648 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1649 EXT_CSD_POWER_OFF_NOTIFICATION,
1650 EXT_CSD_POWER_ON,
1651 card->ext_csd.generic_cmd6_time);
1652 if (err && err != -EBADMSG)
1653 goto free_card;
bec8726a 1654
96a85d54
G
1655 /*
1656 * The err can be -EBADMSG or 0,
1657 * so check for success and update the flag
1658 */
1659 if (!err)
e6c08586 1660 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
96a85d54 1661 }
bec8726a 1662
89a73cf5 1663 /*
577fb131 1664 * Select timing interface
dfc13e84 1665 */
577fb131
SJ
1666 err = mmc_select_timing(card);
1667 if (err)
1668 goto free_card;
dfc13e84 1669
a4924c71 1670 if (mmc_card_hs200(card)) {
577fb131
SJ
1671 err = mmc_hs200_tuning(card);
1672 if (err)
4b75bffc 1673 goto free_card;
0a5b6438
SJ
1674
1675 err = mmc_select_hs400(card);
1676 if (err)
4b75bffc 1677 goto free_card;
577fb131
SJ
1678 } else if (mmc_card_hs(card)) {
1679 /* Select the desired bus width optionally */
1680 err = mmc_select_bus_width(card);
8b7be8f2 1681 if (err > 0) {
577fb131
SJ
1682 err = mmc_select_hs_ddr(card);
1683 if (err)
4b75bffc 1684 goto free_card;
ef0b27d4 1685 }
89a73cf5
PO
1686 }
1687
2385049d
SJ
1688 /*
1689 * Choose the power class with selected bus interface
1690 */
1691 mmc_select_powerclass(card);
1692
52d0974e
SJ
1693 /*
1694 * Enable HPI feature (if supported)
1695 */
1696 if (card->ext_csd.hpi) {
1697 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1698 EXT_CSD_HPI_MGMT, 1,
1699 card->ext_csd.generic_cmd6_time);
1700 if (err && err != -EBADMSG)
1701 goto free_card;
1702 if (err) {
6606110d
JP
1703 pr_warn("%s: Enabling HPI failed\n",
1704 mmc_hostname(card->host));
52d0974e
SJ
1705 err = 0;
1706 } else
1707 card->ext_csd.hpi_en = 1;
1708 }
1709
881d1c25
SJ
1710 /*
1711 * If cache size is higher than 0, this indicates
1712 * the existence of cache and it can be turned on.
1713 */
5320226a
P
1714 if (!mmc_card_broken_hpi(card) &&
1715 card->ext_csd.cache_size > 0) {
881d1c25 1716 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
8bc0678b
SJ
1717 EXT_CSD_CACHE_CTRL, 1,
1718 card->ext_csd.generic_cmd6_time);
881d1c25
SJ
1719 if (err && err != -EBADMSG)
1720 goto free_card;
1721
1722 /*
1723 * Only if no error, cache is turned on successfully.
1724 */
8bc0678b 1725 if (err) {
6606110d
JP
1726 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1727 mmc_hostname(card->host), err);
8bc0678b
SJ
1728 card->ext_csd.cache_ctrl = 0;
1729 err = 0;
1730 } else {
1731 card->ext_csd.cache_ctrl = 1;
1732 }
881d1c25
SJ
1733 }
1734
abd9ac14
SJ
1735 /*
1736 * The mandatory minimum values are defined for packed command.
1737 * read: 5, write: 3
1738 */
1739 if (card->ext_csd.max_packed_writes >= 3 &&
1740 card->ext_csd.max_packed_reads >= 5 &&
1741 host->caps2 & MMC_CAP2_PACKED_CMD) {
1742 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1743 EXT_CSD_EXP_EVENTS_CTRL,
1744 EXT_CSD_PACKED_EVENT_EN,
1745 card->ext_csd.generic_cmd6_time);
1746 if (err && err != -EBADMSG)
1747 goto free_card;
1748 if (err) {
1749 pr_warn("%s: Enabling packed event failed\n",
1750 mmc_hostname(card->host));
1751 card->ext_csd.packed_event_en = 0;
1752 err = 0;
1753 } else {
1754 card->ext_csd.packed_event_en = 1;
1755 }
1756 }
1757
6abaa0c9
PO
1758 if (!oldcard)
1759 host->card = card;
1760
17b0429d 1761 return 0;
6abaa0c9
PO
1762
1763free_card:
1764 if (!oldcard)
1765 mmc_remove_card(card);
1766err:
adf66a0d 1767 return err;
6abaa0c9
PO
1768}
1769
07a68216
UH
1770static int mmc_can_sleep(struct mmc_card *card)
1771{
1772 return (card && card->ext_csd.rev >= 3);
1773}
1774
1775static int mmc_sleep(struct mmc_host *host)
1776{
1777 struct mmc_command cmd = {0};
1778 struct mmc_card *card = host->card;
cb962e04 1779 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
07a68216
UH
1780 int err;
1781
436f8daa
AH
1782 /* Re-tuning can't be done once the card is deselected */
1783 mmc_retune_hold(host);
1784
07a68216
UH
1785 err = mmc_deselect_cards(host);
1786 if (err)
436f8daa 1787 goto out_release;
07a68216
UH
1788
1789 cmd.opcode = MMC_SLEEP_AWAKE;
1790 cmd.arg = card->rca << 16;
1791 cmd.arg |= 1 << 15;
1792
cb962e04
UH
1793 /*
1794 * If the max_busy_timeout of the host is specified, validate it against
1795 * the sleep cmd timeout. A failure means we need to prevent the host
1796 * from doing hw busy detection, which is done by converting to a R1
1797 * response instead of a R1B.
1798 */
1799 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1800 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1801 } else {
1802 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1803 cmd.busy_timeout = timeout_ms;
1804 }
1805
07a68216
UH
1806 err = mmc_wait_for_cmd(host, &cmd, 0);
1807 if (err)
436f8daa 1808 goto out_release;
07a68216
UH
1809
1810 /*
1811 * If the host does not wait while the card signals busy, then we will
1812 * will have to wait the sleep/awake timeout. Note, we cannot use the
1813 * SEND_STATUS command to poll the status because that command (and most
1814 * others) is invalid while the card sleeps.
1815 */
cb962e04
UH
1816 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1817 mmc_delay(timeout_ms);
07a68216 1818
436f8daa
AH
1819out_release:
1820 mmc_retune_release(host);
07a68216
UH
1821 return err;
1822}
1823
e6c08586
UH
1824static int mmc_can_poweroff_notify(const struct mmc_card *card)
1825{
1826 return card &&
1827 mmc_card_mmc(card) &&
1828 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1829}
1830
1831static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1832{
1833 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1834 int err;
1835
1836 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1837 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1838 timeout = card->ext_csd.power_off_longtime;
1839
878e200b
UH
1840 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1841 EXT_CSD_POWER_OFF_NOTIFICATION,
4509f847 1842 notify_type, timeout, true, false, false);
e6c08586
UH
1843 if (err)
1844 pr_err("%s: Power Off Notification timed out, %u\n",
1845 mmc_hostname(card->host), timeout);
1846
1847 /* Disable the power off notification after the switch operation. */
1848 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1849
1850 return err;
1851}
1852
6abaa0c9
PO
1853/*
1854 * Host is being removed. Free up the current card.
1855 */
1856static void mmc_remove(struct mmc_host *host)
1857{
1858 BUG_ON(!host);
1859 BUG_ON(!host->card);
1860
1861 mmc_remove_card(host->card);
1862 host->card = NULL;
1863}
1864
d3049504
AH
1865/*
1866 * Card detection - card is alive.
1867 */
1868static int mmc_alive(struct mmc_host *host)
1869{
1870 return mmc_send_status(host->card, NULL);
1871}
1872
6abaa0c9
PO
1873/*
1874 * Card detection callback from host.
1875 */
1876static void mmc_detect(struct mmc_host *host)
1877{
1878 int err;
1879
1880 BUG_ON(!host);
1881 BUG_ON(!host->card);
1882
e94cfef6 1883 mmc_get_card(host->card);
6abaa0c9
PO
1884
1885 /*
1886 * Just check if our card has been removed.
1887 */
d3049504 1888 err = _mmc_detect_card_removed(host);
6abaa0c9 1889
e94cfef6 1890 mmc_put_card(host->card);
6abaa0c9 1891
17b0429d 1892 if (err) {
4101c16a 1893 mmc_remove(host);
6abaa0c9
PO
1894
1895 mmc_claim_host(host);
1896 mmc_detach_bus(host);
7f7e4129 1897 mmc_power_off(host);
6abaa0c9
PO
1898 mmc_release_host(host);
1899 }
1900}
1901
03d071fc 1902static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
6abaa0c9 1903{
c3805467 1904 int err = 0;
03d071fc
UH
1905 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1906 EXT_CSD_POWER_OFF_LONG;
c3805467 1907
6abaa0c9
PO
1908 BUG_ON(!host);
1909 BUG_ON(!host->card);
7ea239d9 1910
6abaa0c9 1911 mmc_claim_host(host);
881d926d 1912
9ec775f7
UH
1913 if (mmc_card_suspended(host->card))
1914 goto out;
1915
39b9431b
UH
1916 if (mmc_card_doing_bkops(host->card)) {
1917 err = mmc_stop_bkops(host->card);
1918 if (err)
1919 goto out;
1920 }
1921
10e5d965 1922 err = mmc_flush_cache(host->card);
881d926d
ME
1923 if (err)
1924 goto out;
1925
43235679 1926 if (mmc_can_poweroff_notify(host->card) &&
53275c21 1927 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
03d071fc 1928 err = mmc_poweroff_notify(host->card, notify_type);
07a68216
UH
1929 else if (mmc_can_sleep(host->card))
1930 err = mmc_sleep(host);
e6c08586 1931 else if (!mmc_host_is_spi(host))
85e727ed 1932 err = mmc_deselect_cards(host);
95cdfb72 1933
9ec775f7 1934 if (!err) {
74590263 1935 mmc_power_off(host);
9ec775f7
UH
1936 mmc_card_set_suspended(host->card);
1937 }
881d926d
ME
1938out:
1939 mmc_release_host(host);
c3805467 1940 return err;
6abaa0c9 1941}
7ea239d9 1942
03d071fc 1943/*
0cb403a2 1944 * Suspend callback
03d071fc
UH
1945 */
1946static int mmc_suspend(struct mmc_host *host)
1947{
0cb403a2
UH
1948 int err;
1949
1950 err = _mmc_suspend(host, true);
1951 if (!err) {
1952 pm_runtime_disable(&host->card->dev);
1953 pm_runtime_set_suspended(&host->card->dev);
1954 }
1955
1956 return err;
03d071fc
UH
1957}
1958
6abaa0c9 1959/*
6abaa0c9
PO
1960 * This function tries to determine if the same card is still present
1961 * and, if so, restore all state to it.
1962 */
0cb403a2 1963static int _mmc_resume(struct mmc_host *host)
6abaa0c9 1964{
9ec775f7 1965 int err = 0;
6abaa0c9
PO
1966
1967 BUG_ON(!host);
1968 BUG_ON(!host->card);
1969
1970 mmc_claim_host(host);
9ec775f7
UH
1971
1972 if (!mmc_card_suspended(host->card))
1973 goto out;
1974
69041150 1975 mmc_power_up(host, host->card->ocr);
69041150 1976 err = mmc_init_card(host, host->card->ocr, host->card);
9ec775f7 1977 mmc_card_clr_suspended(host->card);
2986d0bf 1978
9ec775f7
UH
1979out:
1980 mmc_release_host(host);
95cdfb72 1981 return err;
6abaa0c9
PO
1982}
1983
9ec775f7
UH
1984/*
1985 * Shutdown callback
1986 */
1987static int mmc_shutdown(struct mmc_host *host)
1988{
1989 int err = 0;
1990
1991 /*
1992 * In a specific case for poweroff notify, we need to resume the card
1993 * before we can shutdown it properly.
1994 */
1995 if (mmc_can_poweroff_notify(host->card) &&
1996 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
0cb403a2 1997 err = _mmc_resume(host);
9ec775f7
UH
1998
1999 if (!err)
2000 err = _mmc_suspend(host, false);
2001
2002 return err;
2003}
c4d770d7 2004
0cb403a2
UH
2005/*
2006 * Callback for resume.
2007 */
2008static int mmc_resume(struct mmc_host *host)
2009{
0cb403a2 2010 pm_runtime_enable(&host->card->dev);
c29536e8 2011 return 0;
0cb403a2
UH
2012}
2013
c4d770d7
UH
2014/*
2015 * Callback for runtime_suspend.
2016 */
2017static int mmc_runtime_suspend(struct mmc_host *host)
2018{
2019 int err;
2020
2021 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2022 return 0;
2023
0cb403a2 2024 err = _mmc_suspend(host, true);
0cc81a8c 2025 if (err)
f42cf8d6 2026 pr_err("%s: error %d doing aggressive suspend\n",
c4d770d7 2027 mmc_hostname(host), err);
c4d770d7 2028
c4d770d7
UH
2029 return err;
2030}
2031
2032/*
2033 * Callback for runtime_resume.
2034 */
2035static int mmc_runtime_resume(struct mmc_host *host)
2036{
2037 int err;
2038
0cb403a2 2039 err = _mmc_resume(host);
520322d9 2040 if (err && err != -ENOMEDIUM)
c29536e8 2041 pr_err("%s: error %d doing runtime resume\n",
c4d770d7
UH
2042 mmc_hostname(host), err);
2043
c4d770d7
UH
2044 return 0;
2045}
2046
f855a371
JR
2047int mmc_can_reset(struct mmc_card *card)
2048{
2049 u8 rst_n_function;
2050
2051 rst_n_function = card->ext_csd.rst_n_function;
2052 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2053 return 0;
2054 return 1;
2055}
2056EXPORT_SYMBOL(mmc_can_reset);
2057
2058static int mmc_reset(struct mmc_host *host)
2059{
2060 struct mmc_card *card = host->card;
f855a371 2061
437db4c6
AH
2062 /*
2063 * In the case of recovery, we can't expect flushing the cache to work
2064 * always, but we have a go and ignore errors.
2065 */
2066 mmc_flush_cache(host->card);
2067
4e6c7178
GG
2068 if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2069 mmc_can_reset(card)) {
2070 /* If the card accept RST_n signal, send it. */
2071 mmc_set_clock(host, host->f_init);
2072 host->ops->hw_reset(host);
2073 /* Set initial state and call mmc_set_ios */
2074 mmc_set_initial_state(host);
2075 } else {
2076 /* Do a brute force power cycle */
2077 mmc_power_cycle(host, card->ocr);
2078 }
364549dd 2079 return mmc_init_card(host, card->ocr, card);
f855a371
JR
2080}
2081
6abaa0c9
PO
2082static const struct mmc_bus_ops mmc_ops = {
2083 .remove = mmc_remove,
2084 .detect = mmc_detect,
2085 .suspend = mmc_suspend,
2086 .resume = mmc_resume,
c4d770d7
UH
2087 .runtime_suspend = mmc_runtime_suspend,
2088 .runtime_resume = mmc_runtime_resume,
d3049504 2089 .alive = mmc_alive,
486fdbbc 2090 .shutdown = mmc_shutdown,
f855a371 2091 .reset = mmc_reset,
6abaa0c9
PO
2092};
2093
2094/*
2095 * Starting point for MMC card init.
2096 */
807e8e40 2097int mmc_attach_mmc(struct mmc_host *host)
6abaa0c9
PO
2098{
2099 int err;
69041150 2100 u32 ocr, rocr;
6abaa0c9
PO
2101
2102 BUG_ON(!host);
d84075c8 2103 WARN_ON(!host->claimed);
6abaa0c9 2104
44669034
SNX
2105 /* Set correct bus mode for MMC before attempting attach */
2106 if (!mmc_host_is_spi(host))
2107 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2108
807e8e40
AR
2109 err = mmc_send_op_cond(host, 0, &ocr);
2110 if (err)
2111 return err;
2112
2501c917 2113 mmc_attach_bus(host, &mmc_ops);
8f230f45
TI
2114 if (host->ocr_avail_mmc)
2115 host->ocr_avail = host->ocr_avail_mmc;
6abaa0c9 2116
af517150
DB
2117 /*
2118 * We need to get OCR a different way for SPI.
2119 */
2120 if (mmc_host_is_spi(host)) {
2121 err = mmc_spi_read_ocr(host, 1, &ocr);
2122 if (err)
2123 goto err;
2124 }
2125
69041150 2126 rocr = mmc_select_voltage(host, ocr);
6abaa0c9
PO
2127
2128 /*
2129 * Can we support the voltage of the card?
2130 */
69041150 2131 if (!rocr) {
109b5bed 2132 err = -EINVAL;
6abaa0c9 2133 goto err;
109b5bed 2134 }
6abaa0c9
PO
2135
2136 /*
2137 * Detect and init the card.
2138 */
69041150 2139 err = mmc_init_card(host, rocr, NULL);
17b0429d 2140 if (err)
6abaa0c9
PO
2141 goto err;
2142
2143 mmc_release_host(host);
4101c16a 2144 err = mmc_add_card(host->card);
7ea239d9 2145 if (err)
2986d0bf 2146 goto remove_card;
7ea239d9 2147
2860d060 2148 mmc_claim_host(host);
7ea239d9
PO
2149 return 0;
2150
2986d0bf 2151remove_card:
6abaa0c9 2152 mmc_remove_card(host->card);
2986d0bf 2153 mmc_claim_host(host);
807e8e40 2154 host->card = NULL;
7ea239d9
PO
2155err:
2156 mmc_detach_bus(host);
7ea239d9 2157
a3c76eb9 2158 pr_err("%s: error %d whilst initialising MMC card\n",
109b5bed
PO
2159 mmc_hostname(host), err);
2160
adf66a0d 2161 return err;
7ea239d9 2162}
This page took 0.814306 seconds and 5 git commands to generate.