mmc: core: Support aggressive power management for (e)MMC/SD
[linux-2.6-block.git] / drivers / mmc / core / mmc.c
CommitLineData
7ea239d9 1/*
70f10482 2 * linux/drivers/mmc/core/mmc.c
7ea239d9
PO
3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
6 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/err.h>
5a0e3ad6 14#include <linux/slab.h>
0205a904 15#include <linux/stat.h>
7ea239d9
PO
16
17#include <linux/mmc/host.h>
18#include <linux/mmc/card.h>
19#include <linux/mmc/mmc.h>
20
21#include "core.h"
4101c16a 22#include "bus.h"
7ea239d9 23#include "mmc_ops.h"
4c4cb171 24#include "sd_ops.h"
7ea239d9
PO
25
26static const unsigned int tran_exp[] = {
27 10000, 100000, 1000000, 10000000,
28 0, 0, 0, 0
29};
30
31static const unsigned char tran_mant[] = {
32 0, 10, 12, 13, 15, 20, 25, 30,
33 35, 40, 45, 50, 55, 60, 70, 80,
34};
35
36static const unsigned int tacc_exp[] = {
37 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
38};
39
40static const unsigned int tacc_mant[] = {
41 0, 10, 12, 13, 15, 20, 25, 30,
42 35, 40, 45, 50, 55, 60, 70, 80,
43};
44
45#define UNSTUFF_BITS(resp,start,size) \
46 ({ \
47 const int __size = size; \
48 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
49 const int __off = 3 - ((start) / 32); \
50 const int __shft = (start) & 31; \
51 u32 __res; \
52 \
53 __res = resp[__off] >> __shft; \
54 if (__size + __shft > 32) \
55 __res |= resp[__off-1] << ((32 - __shft) % 32); \
56 __res & __mask; \
57 })
58
59/*
60 * Given the decoded CSD structure, decode the raw CID to our CID structure.
61 */
bd766312 62static int mmc_decode_cid(struct mmc_card *card)
7ea239d9
PO
63{
64 u32 *resp = card->raw_cid;
65
66 /*
67 * The selection of the format here is based upon published
68 * specs from sandisk and from what people have reported.
69 */
70 switch (card->csd.mmca_vsn) {
71 case 0: /* MMC v1.0 - v1.2 */
72 case 1: /* MMC v1.4 */
73 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
74 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
75 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
76 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
77 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
78 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
79 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
80 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
81 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
82 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
83 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
84 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
85 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
86 break;
87
88 case 2: /* MMC v2.0 - v2.2 */
89 case 3: /* MMC v3.1 - v3.3 */
90 case 4: /* MMC v4 */
91 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
92 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
93 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
94 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
95 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
96 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
97 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
98 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
51e7e8b6 99 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
7ea239d9
PO
100 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
101 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
102 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
103 break;
104
105 default:
a3c76eb9 106 pr_err("%s: card has unknown MMCA version %d\n",
7ea239d9 107 mmc_hostname(card->host), card->csd.mmca_vsn);
bd766312 108 return -EINVAL;
7ea239d9 109 }
bd766312
PO
110
111 return 0;
7ea239d9
PO
112}
113
dfe86cba
AH
114static void mmc_set_erase_size(struct mmc_card *card)
115{
116 if (card->ext_csd.erase_group_def & 1)
117 card->erase_size = card->ext_csd.hc_erase_size;
118 else
119 card->erase_size = card->csd.erase_size;
120
121 mmc_init_erase(card);
122}
123
7ea239d9
PO
124/*
125 * Given a 128-bit response, decode to our card CSD structure.
126 */
bd766312 127static int mmc_decode_csd(struct mmc_card *card)
7ea239d9
PO
128{
129 struct mmc_csd *csd = &card->csd;
dfe86cba 130 unsigned int e, m, a, b;
7ea239d9
PO
131 u32 *resp = card->raw_csd;
132
133 /*
134 * We only understand CSD structure v1.1 and v1.2.
135 * v1.2 has extra information in bits 15, 11 and 10.
6da24b78 136 * We also support eMMC v4.4 & v4.41.
7ea239d9 137 */
6da24b78
KP
138 csd->structure = UNSTUFF_BITS(resp, 126, 2);
139 if (csd->structure == 0) {
a3c76eb9 140 pr_err("%s: unrecognised CSD structure version %d\n",
6da24b78 141 mmc_hostname(card->host), csd->structure);
bd766312 142 return -EINVAL;
7ea239d9
PO
143 }
144
145 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
146 m = UNSTUFF_BITS(resp, 115, 4);
147 e = UNSTUFF_BITS(resp, 112, 3);
148 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
149 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
150
151 m = UNSTUFF_BITS(resp, 99, 4);
152 e = UNSTUFF_BITS(resp, 96, 3);
153 csd->max_dtr = tran_exp[e] * tran_mant[m];
154 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
155
156 e = UNSTUFF_BITS(resp, 47, 3);
157 m = UNSTUFF_BITS(resp, 62, 12);
158 csd->capacity = (1 + m) << (e + 2);
159
160 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
161 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
162 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
163 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
164 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
165 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
166 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
bd766312 167
dfe86cba
AH
168 if (csd->write_blkbits >= 9) {
169 a = UNSTUFF_BITS(resp, 42, 5);
170 b = UNSTUFF_BITS(resp, 37, 5);
171 csd->erase_size = (a + 1) * (b + 1);
172 csd->erase_size <<= csd->write_blkbits - 9;
173 }
174
bd766312 175 return 0;
7ea239d9
PO
176}
177
178/*
08ee80cc 179 * Read extended CSD.
7ea239d9 180 */
08ee80cc 181static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
7ea239d9
PO
182{
183 int err;
184 u8 *ext_csd;
185
186 BUG_ON(!card);
08ee80cc
PR
187 BUG_ON(!new_ext_csd);
188
189 *new_ext_csd = NULL;
7ea239d9 190
7ea239d9 191 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
17b0429d 192 return 0;
7ea239d9
PO
193
194 /*
195 * As the ext_csd is so large and mostly unused, we don't store the
196 * raw block in mmc_card.
197 */
198 ext_csd = kmalloc(512, GFP_KERNEL);
199 if (!ext_csd) {
a3c76eb9 200 pr_err("%s: could not allocate a buffer to "
adf66a0d 201 "receive the ext_csd.\n", mmc_hostname(card->host));
17b0429d 202 return -ENOMEM;
7ea239d9
PO
203 }
204
205 err = mmc_send_ext_csd(card, ext_csd);
17b0429d 206 if (err) {
08ee80cc
PR
207 kfree(ext_csd);
208 *new_ext_csd = NULL;
209
d08ebedd
WM
210 /* If the host or the card can't do the switch,
211 * fail more gracefully. */
212 if ((err != -EINVAL)
213 && (err != -ENOSYS)
214 && (err != -EFAULT))
08ee80cc 215 return err;
adf66a0d 216
7ea239d9
PO
217 /*
218 * High capacity cards should have this "magic" size
219 * stored in their CSD.
220 */
221 if (card->csd.capacity == (4096 * 512)) {
a3c76eb9 222 pr_err("%s: unable to read EXT_CSD "
7ea239d9
PO
223 "on a possible high capacity card. "
224 "Card will be ignored.\n",
225 mmc_hostname(card->host));
226 } else {
a3c76eb9 227 pr_warning("%s: unable to read "
7ea239d9
PO
228 "EXT_CSD, performance might "
229 "suffer.\n",
230 mmc_hostname(card->host));
17b0429d 231 err = 0;
7ea239d9 232 }
08ee80cc
PR
233 } else
234 *new_ext_csd = ext_csd;
adf66a0d 235
08ee80cc
PR
236 return err;
237}
238
96cf5f02
SJ
239static void mmc_select_card_type(struct mmc_card *card)
240{
241 struct mmc_host *host = card->host;
242 u8 card_type = card->ext_csd.raw_card_type & EXT_CSD_CARD_TYPE_MASK;
5f1a4dd0 243 u32 caps = host->caps, caps2 = host->caps2;
96cf5f02
SJ
244 unsigned int hs_max_dtr = 0;
245
246 if (card_type & EXT_CSD_CARD_TYPE_26)
247 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
248
249 if (caps & MMC_CAP_MMC_HIGHSPEED &&
250 card_type & EXT_CSD_CARD_TYPE_52)
251 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
252
253 if ((caps & MMC_CAP_1_8V_DDR &&
254 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) ||
255 (caps & MMC_CAP_1_2V_DDR &&
256 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V))
257 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
258
259 if ((caps2 & MMC_CAP2_HS200_1_8V_SDR &&
260 card_type & EXT_CSD_CARD_TYPE_SDR_1_8V) ||
261 (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
262 card_type & EXT_CSD_CARD_TYPE_SDR_1_2V))
263 hs_max_dtr = MMC_HS200_MAX_DTR;
264
265 card->ext_csd.hs_max_dtr = hs_max_dtr;
266 card->ext_csd.card_type = card_type;
267}
268
08ee80cc
PR
269/*
270 * Decode extended CSD.
271 */
272static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
273{
e0c368d5
NJ
274 int err = 0, idx;
275 unsigned int part_size;
276 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
08ee80cc
PR
277
278 BUG_ON(!card);
279
280 if (!ext_csd)
281 return 0;
7ea239d9 282
6da24b78 283 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
f39b2dd9 284 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
6da24b78 285 if (card->csd.structure == 3) {
f39b2dd9 286 if (card->ext_csd.raw_ext_csd_structure > 2) {
a3c76eb9 287 pr_err("%s: unrecognised EXT_CSD structure "
6da24b78 288 "version %d\n", mmc_hostname(card->host),
f39b2dd9 289 card->ext_csd.raw_ext_csd_structure);
6da24b78
KP
290 err = -EINVAL;
291 goto out;
292 }
293 }
294
b1ebe384 295 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
38ca2850 296 if (card->ext_csd.rev > 6) {
a3c76eb9 297 pr_err("%s: unrecognised EXT_CSD revision %d\n",
6da24b78 298 mmc_hostname(card->host), card->ext_csd.rev);
00cedfa6
FM
299 err = -EINVAL;
300 goto out;
d7604d76
PO
301 }
302
f39b2dd9
PR
303 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
304 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
305 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
306 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
b1ebe384 307 if (card->ext_csd.rev >= 2) {
d7604d76
PO
308 card->ext_csd.sectors =
309 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
310 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
311 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
312 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
fc8a0985
HP
313
314 /* Cards with density > 2GiB are sector addressed */
315 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
d7604d76
PO
316 mmc_card_set_blockaddr(card);
317 }
96cf5f02 318
f39b2dd9 319 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
96cf5f02 320 mmc_select_card_type(card);
7ea239d9 321
f39b2dd9
PR
322 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
323 card->ext_csd.raw_erase_timeout_mult =
324 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
325 card->ext_csd.raw_hc_erase_grp_size =
326 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
b1ebe384
JL
327 if (card->ext_csd.rev >= 3) {
328 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
371a689f
AW
329 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
330
331 /* EXT_CSD value is in units of 10ms, but we store in ms */
332 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
b1ebe384
JL
333
334 /* Sleep / awake timeout in 100ns units */
335 if (sa_shift > 0 && sa_shift <= 0x17)
336 card->ext_csd.sa_timeout =
337 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
dfe86cba
AH
338 card->ext_csd.erase_group_def =
339 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
340 card->ext_csd.hc_erase_timeout = 300 *
341 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
342 card->ext_csd.hc_erase_size =
343 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
f4c5522b
AW
344
345 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
371a689f
AW
346
347 /*
348 * There are two boot regions of equal size, defined in
349 * multiples of 128K.
350 */
e0c368d5
NJ
351 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
352 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
353 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
354 mmc_part_add(card, part_size,
355 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
add710ea
JR
356 "boot%d", idx, true,
357 MMC_BLK_DATA_AREA_BOOT);
e0c368d5
NJ
358 }
359 }
dfe86cba
AH
360 }
361
f39b2dd9 362 card->ext_csd.raw_hc_erase_gap_size =
dd13b4ed 363 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
f39b2dd9
PR
364 card->ext_csd.raw_sec_trim_mult =
365 ext_csd[EXT_CSD_SEC_TRIM_MULT];
366 card->ext_csd.raw_sec_erase_mult =
367 ext_csd[EXT_CSD_SEC_ERASE_MULT];
368 card->ext_csd.raw_sec_feature_support =
369 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
370 card->ext_csd.raw_trim_mult =
371 ext_csd[EXT_CSD_TRIM_MULT];
836dc2fe 372 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
dfe86cba 373 if (card->ext_csd.rev >= 4) {
709de99d
CD
374 /*
375 * Enhanced area feature support -- check whether the eMMC
376 * card has the Enhanced area enabled. If so, export enhanced
377 * area offset and size to user by adding sysfs interface.
378 */
379 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
f39b2dd9 380 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
e0c368d5 381 hc_erase_grp_sz =
709de99d 382 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
e0c368d5 383 hc_wp_grp_sz =
709de99d
CD
384 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
385
386 card->ext_csd.enhanced_area_en = 1;
387 /*
388 * calculate the enhanced data area offset, in bytes
389 */
390 card->ext_csd.enhanced_area_offset =
391 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
392 (ext_csd[137] << 8) + ext_csd[136];
393 if (mmc_card_blockaddr(card))
394 card->ext_csd.enhanced_area_offset <<= 9;
395 /*
396 * calculate the enhanced data area size, in kilobytes
397 */
398 card->ext_csd.enhanced_area_size =
399 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
400 ext_csd[140];
401 card->ext_csd.enhanced_area_size *=
402 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
403 card->ext_csd.enhanced_area_size <<= 9;
404 } else {
405 /*
406 * If the enhanced area is not enabled, disable these
407 * device attributes.
408 */
409 card->ext_csd.enhanced_area_offset = -EINVAL;
410 card->ext_csd.enhanced_area_size = -EINVAL;
411 }
e0c368d5
NJ
412
413 /*
414 * General purpose partition feature support --
415 * If ext_csd has the size of general purpose partitions,
416 * set size, part_cfg, partition name in mmc_part.
417 */
418 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
419 EXT_CSD_PART_SUPPORT_PART_EN) {
420 if (card->ext_csd.enhanced_area_en != 1) {
421 hc_erase_grp_sz =
422 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
423 hc_wp_grp_sz =
424 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
425
426 card->ext_csd.enhanced_area_en = 1;
427 }
428
429 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
430 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
431 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
432 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
433 continue;
434 part_size =
435 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
436 << 16) +
437 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
438 << 8) +
439 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
440 part_size *= (size_t)(hc_erase_grp_sz *
441 hc_wp_grp_sz);
442 mmc_part_add(card, part_size << 19,
443 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
add710ea
JR
444 "gp%d", idx, false,
445 MMC_BLK_DATA_AREA_GP);
e0c368d5
NJ
446 }
447 }
dfe86cba
AH
448 card->ext_csd.sec_trim_mult =
449 ext_csd[EXT_CSD_SEC_TRIM_MULT];
450 card->ext_csd.sec_erase_mult =
451 ext_csd[EXT_CSD_SEC_ERASE_MULT];
452 card->ext_csd.sec_feature_support =
453 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
454 card->ext_csd.trim_timeout = 300 *
455 ext_csd[EXT_CSD_TRIM_MULT];
add710ea
JR
456
457 /*
458 * Note that the call to mmc_part_add above defaults to read
459 * only. If this default assumption is changed, the call must
460 * take into account the value of boot_locked below.
461 */
462 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
463 card->ext_csd.boot_ro_lockable = true;
b1ebe384
JL
464 }
465
b2499518 466 if (card->ext_csd.rev >= 5) {
950d56ac
JC
467 /* check whether the eMMC card supports BKOPS */
468 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
469 card->ext_csd.bkops = 1;
470 card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
471 card->ext_csd.raw_bkops_status =
472 ext_csd[EXT_CSD_BKOPS_STATUS];
473 if (!card->ext_csd.bkops_en)
474 pr_info("%s: BKOPS_EN bit is not set\n",
475 mmc_hostname(card->host));
476 }
477
eb0d8f13
JC
478 /* check whether the eMMC card supports HPI */
479 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
480 card->ext_csd.hpi = 1;
481 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
482 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
483 else
484 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
485 /*
486 * Indicate the maximum timeout to close
487 * a command interrupted by HPI
488 */
489 card->ext_csd.out_of_int_time =
490 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
491 }
492
f4c5522b 493 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
b2499518 494 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
090d25fe
LP
495
496 /*
497 * RPMB regions are defined in multiples of 128K.
498 */
499 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
d0123cca 500 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
090d25fe
LP
501 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
502 EXT_CSD_PART_CONFIG_ACC_RPMB,
503 "rpmb", 0, false,
504 MMC_BLK_DATA_AREA_RPMB);
505 }
b2499518 506 }
f4c5522b 507
5238acbe 508 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
dfe86cba
AH
509 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
510 card->erased_byte = 0xFF;
511 else
512 card->erased_byte = 0x0;
513
336c716a 514 /* eMMC v4.5 or later */
bec8726a 515 if (card->ext_csd.rev >= 6) {
336c716a
SJ
516 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
517
b23cf0bd
SJ
518 card->ext_csd.generic_cmd6_time = 10 *
519 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
bec8726a
G
520 card->ext_csd.power_off_longtime = 10 *
521 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
b23cf0bd 522
336c716a
SJ
523 card->ext_csd.cache_size =
524 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
525 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
526 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
527 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
4265900e
SD
528
529 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
530 card->ext_csd.data_sector_size = 4096;
531 else
532 card->ext_csd.data_sector_size = 512;
533
534 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
535 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
536 card->ext_csd.data_tag_unit_size =
537 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
538 (card->ext_csd.data_sector_size);
539 } else {
540 card->ext_csd.data_tag_unit_size = 0;
541 }
abd9ac14
SJ
542
543 card->ext_csd.max_packed_writes =
544 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
545 card->ext_csd.max_packed_reads =
546 ext_csd[EXT_CSD_MAX_PACKED_READS];
a5075eb9
SD
547 } else {
548 card->ext_csd.data_sector_size = 512;
336c716a 549 }
881d1c25 550
7ea239d9 551out:
08ee80cc
PR
552 return err;
553}
554
555static inline void mmc_free_ext_csd(u8 *ext_csd)
556{
7ea239d9 557 kfree(ext_csd);
08ee80cc
PR
558}
559
7ea239d9 560
f39b2dd9 561static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
08ee80cc
PR
562{
563 u8 *bw_ext_csd;
564 int err;
565
f39b2dd9
PR
566 if (bus_width == MMC_BUS_WIDTH_1)
567 return 0;
568
08ee80cc 569 err = mmc_get_ext_csd(card, &bw_ext_csd);
08ee80cc 570
f39b2dd9 571 if (err || bw_ext_csd == NULL) {
f6f44590 572 err = -EINVAL;
08ee80cc
PR
573 goto out;
574 }
575
08ee80cc 576 /* only compare read only fields */
dd13b4ed 577 err = !((card->ext_csd.raw_partition_support ==
08ee80cc 578 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
f39b2dd9 579 (card->ext_csd.raw_erased_mem_count ==
08ee80cc 580 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
f39b2dd9 581 (card->ext_csd.rev ==
08ee80cc 582 bw_ext_csd[EXT_CSD_REV]) &&
f39b2dd9 583 (card->ext_csd.raw_ext_csd_structure ==
08ee80cc 584 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
f39b2dd9 585 (card->ext_csd.raw_card_type ==
08ee80cc 586 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
f39b2dd9 587 (card->ext_csd.raw_s_a_timeout ==
08ee80cc 588 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
f39b2dd9 589 (card->ext_csd.raw_hc_erase_gap_size ==
08ee80cc 590 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
f39b2dd9 591 (card->ext_csd.raw_erase_timeout_mult ==
08ee80cc 592 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
f39b2dd9 593 (card->ext_csd.raw_hc_erase_grp_size ==
08ee80cc 594 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
f39b2dd9 595 (card->ext_csd.raw_sec_trim_mult ==
08ee80cc 596 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
f39b2dd9 597 (card->ext_csd.raw_sec_erase_mult ==
08ee80cc 598 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
f39b2dd9 599 (card->ext_csd.raw_sec_feature_support ==
08ee80cc 600 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
f39b2dd9 601 (card->ext_csd.raw_trim_mult ==
08ee80cc 602 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
f39b2dd9
PR
603 (card->ext_csd.raw_sectors[0] ==
604 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
605 (card->ext_csd.raw_sectors[1] ==
606 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
607 (card->ext_csd.raw_sectors[2] ==
608 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
609 (card->ext_csd.raw_sectors[3] ==
610 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
08ee80cc
PR
611 if (err)
612 err = -EINVAL;
613
614out:
615 mmc_free_ext_csd(bw_ext_csd);
7ea239d9
PO
616 return err;
617}
618
51ec92e2
PO
619MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
620 card->raw_cid[2], card->raw_cid[3]);
621MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
622 card->raw_csd[2], card->raw_csd[3]);
623MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
dfe86cba
AH
624MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
625MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
51ec92e2
PO
626MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
627MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
628MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
629MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
630MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
51e7e8b6 631MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
51ec92e2 632MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
709de99d
CD
633MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
634 card->ext_csd.enhanced_area_offset);
635MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
188cc042
LP
636MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
637MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
51ec92e2
PO
638
639static struct attribute *mmc_std_attrs[] = {
640 &dev_attr_cid.attr,
641 &dev_attr_csd.attr,
642 &dev_attr_date.attr,
dfe86cba
AH
643 &dev_attr_erase_size.attr,
644 &dev_attr_preferred_erase_size.attr,
51ec92e2
PO
645 &dev_attr_fwrev.attr,
646 &dev_attr_hwrev.attr,
647 &dev_attr_manfid.attr,
648 &dev_attr_name.attr,
649 &dev_attr_oemid.attr,
51e7e8b6 650 &dev_attr_prv.attr,
51ec92e2 651 &dev_attr_serial.attr,
709de99d
CD
652 &dev_attr_enhanced_area_offset.attr,
653 &dev_attr_enhanced_area_size.attr,
188cc042
LP
654 &dev_attr_raw_rpmb_size_mult.attr,
655 &dev_attr_rel_sectors.attr,
51ec92e2
PO
656 NULL,
657};
658
659static struct attribute_group mmc_std_attr_group = {
660 .attrs = mmc_std_attrs,
661};
662
a4dbd674 663static const struct attribute_group *mmc_attr_groups[] = {
51ec92e2
PO
664 &mmc_std_attr_group,
665 NULL,
666};
667
668static struct device_type mmc_type = {
669 .groups = mmc_attr_groups,
670};
671
b87d8dbf
G
672/*
673 * Select the PowerClass for the current bus width
674 * If power class is defined for 4/8 bit bus in the
675 * extended CSD register, select it by executing the
676 * mmc_switch command.
677 */
678static int mmc_select_powerclass(struct mmc_card *card,
679 unsigned int bus_width, u8 *ext_csd)
680{
681 int err = 0;
682 unsigned int pwrclass_val;
683 unsigned int index = 0;
684 struct mmc_host *host;
685
686 BUG_ON(!card);
687
688 host = card->host;
689 BUG_ON(!host);
690
691 if (ext_csd == NULL)
692 return 0;
693
694 /* Power class selection is supported for versions >= 4.0 */
695 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
696 return 0;
697
698 /* Power class values are defined only for 4/8 bit bus */
699 if (bus_width == EXT_CSD_BUS_WIDTH_1)
700 return 0;
701
702 switch (1 << host->ios.vdd) {
703 case MMC_VDD_165_195:
704 if (host->ios.clock <= 26000000)
705 index = EXT_CSD_PWR_CL_26_195;
706 else if (host->ios.clock <= 52000000)
707 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
708 EXT_CSD_PWR_CL_52_195 :
709 EXT_CSD_PWR_CL_DDR_52_195;
710 else if (host->ios.clock <= 200000000)
711 index = EXT_CSD_PWR_CL_200_195;
712 break;
93fc5a47
SJ
713 case MMC_VDD_27_28:
714 case MMC_VDD_28_29:
715 case MMC_VDD_29_30:
716 case MMC_VDD_30_31:
717 case MMC_VDD_31_32:
b87d8dbf
G
718 case MMC_VDD_32_33:
719 case MMC_VDD_33_34:
720 case MMC_VDD_34_35:
721 case MMC_VDD_35_36:
722 if (host->ios.clock <= 26000000)
723 index = EXT_CSD_PWR_CL_26_360;
724 else if (host->ios.clock <= 52000000)
725 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
726 EXT_CSD_PWR_CL_52_360 :
727 EXT_CSD_PWR_CL_DDR_52_360;
728 else if (host->ios.clock <= 200000000)
729 index = EXT_CSD_PWR_CL_200_360;
730 break;
731 default:
732 pr_warning("%s: Voltage range not supported "
733 "for power class.\n", mmc_hostname(host));
734 return -EINVAL;
735 }
736
737 pwrclass_val = ext_csd[index];
738
739 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
740 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
741 EXT_CSD_PWR_CL_8BIT_SHIFT;
742 else
743 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
744 EXT_CSD_PWR_CL_4BIT_SHIFT;
745
746 /* If the power class is different from the default value */
747 if (pwrclass_val > 0) {
748 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
749 EXT_CSD_POWER_CLASS,
750 pwrclass_val,
71fe3eb0 751 card->ext_csd.generic_cmd6_time);
b87d8dbf
G
752 }
753
754 return err;
755}
756
a4924c71
G
757/*
758 * Selects the desired buswidth and switch to the HS200 mode
759 * if bus width set without error
760 */
761static int mmc_select_hs200(struct mmc_card *card)
762{
10942aa4 763 int idx, err = -EINVAL;
a4924c71
G
764 struct mmc_host *host;
765 static unsigned ext_csd_bits[] = {
766 EXT_CSD_BUS_WIDTH_4,
767 EXT_CSD_BUS_WIDTH_8,
768 };
769 static unsigned bus_widths[] = {
770 MMC_BUS_WIDTH_4,
771 MMC_BUS_WIDTH_8,
772 };
773
774 BUG_ON(!card);
775
776 host = card->host;
777
778 if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V &&
10942aa4 779 host->caps2 & MMC_CAP2_HS200_1_2V_SDR)
567c8903 780 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
10942aa4
SJ
781
782 if (err && card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_8V &&
783 host->caps2 & MMC_CAP2_HS200_1_8V_SDR)
567c8903 784 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
a4924c71
G
785
786 /* If fails try again during next card power cycle */
787 if (err)
788 goto err;
789
790 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0;
791
792 /*
793 * Unlike SD, MMC cards dont have a configuration register to notify
794 * supported bus width. So bus test command should be run to identify
795 * the supported bus width or compare the ext csd values of current
796 * bus width and ext csd values of 1 bit mode read earlier.
797 */
798 for (; idx >= 0; idx--) {
799
800 /*
801 * Host is capable of 8bit transfer, then switch
802 * the device to work in 8bit transfer mode. If the
803 * mmc switch command returns error then switch to
804 * 4bit transfer mode. On success set the corresponding
805 * bus width on the host.
806 */
807 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
808 EXT_CSD_BUS_WIDTH,
809 ext_csd_bits[idx],
810 card->ext_csd.generic_cmd6_time);
811 if (err)
812 continue;
813
814 mmc_set_bus_width(card->host, bus_widths[idx]);
815
816 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
817 err = mmc_compare_ext_csds(card, bus_widths[idx]);
818 else
819 err = mmc_bus_test(card, bus_widths[idx]);
820 if (!err)
821 break;
822 }
823
824 /* switch to HS200 mode if bus width set successfully */
825 if (!err)
826 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
827 EXT_CSD_HS_TIMING, 2, 0);
828err:
829 return err;
830}
831
7ea239d9 832/*
6abaa0c9
PO
833 * Handle the detection and initialisation of a card.
834 *
8769392b 835 * In the case of a resume, "oldcard" will contain the card
6abaa0c9 836 * we're trying to reinitialise.
7ea239d9 837 */
8c75deae 838static int mmc_init_card(struct mmc_host *host, u32 ocr,
6abaa0c9 839 struct mmc_card *oldcard)
7ea239d9
PO
840{
841 struct mmc_card *card;
25d5c699 842 int err, ddr = 0;
7ea239d9
PO
843 u32 cid[4];
844 unsigned int max_dtr;
b676f039 845 u32 rocr;
08ee80cc 846 u8 *ext_csd = NULL;
7ea239d9
PO
847
848 BUG_ON(!host);
d84075c8 849 WARN_ON(!host->claimed);
7ea239d9 850
44669034
SNX
851 /* Set correct bus mode for MMC before attempting init */
852 if (!mmc_host_is_spi(host))
853 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
854
7ea239d9
PO
855 /*
856 * Since we're changing the OCR value, we seem to
857 * need to tell some cards to go back to the idle
858 * state. We wait 1ms to give cards time to
859 * respond.
c3805467 860 * mmc_go_idle is needed for eMMC that are asleep
7ea239d9
PO
861 */
862 mmc_go_idle(host);
863
864 /* The extra bit indicates that we support high capacity */
b676f039 865 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
17b0429d 866 if (err)
6abaa0c9 867 goto err;
7ea239d9 868
af517150
DB
869 /*
870 * For SPI, enable CRC as appropriate.
871 */
872 if (mmc_host_is_spi(host)) {
873 err = mmc_spi_set_crc(host, use_spi_crc);
874 if (err)
875 goto err;
876 }
877
7ea239d9
PO
878 /*
879 * Fetch CID from card.
880 */
af517150
DB
881 if (mmc_host_is_spi(host))
882 err = mmc_send_cid(host, cid);
883 else
884 err = mmc_all_send_cid(host, cid);
17b0429d 885 if (err)
7ea239d9
PO
886 goto err;
887
6abaa0c9 888 if (oldcard) {
adf66a0d
PO
889 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
890 err = -ENOENT;
6abaa0c9 891 goto err;
adf66a0d 892 }
6abaa0c9
PO
893
894 card = oldcard;
895 } else {
896 /*
897 * Allocate card structure.
898 */
51ec92e2 899 card = mmc_alloc_card(host, &mmc_type);
adf66a0d
PO
900 if (IS_ERR(card)) {
901 err = PTR_ERR(card);
6abaa0c9 902 goto err;
adf66a0d 903 }
7ea239d9 904
6abaa0c9
PO
905 card->type = MMC_TYPE_MMC;
906 card->rca = 1;
907 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
908 }
7ea239d9
PO
909
910 /*
af517150 911 * For native busses: set card RCA and quit open drain mode.
7ea239d9 912 */
af517150
DB
913 if (!mmc_host_is_spi(host)) {
914 err = mmc_set_relative_addr(card);
915 if (err)
916 goto free_card;
7ea239d9 917
af517150
DB
918 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
919 }
7ea239d9 920
6abaa0c9
PO
921 if (!oldcard) {
922 /*
923 * Fetch CSD from card.
924 */
925 err = mmc_send_csd(card, card->raw_csd);
17b0429d 926 if (err)
6abaa0c9 927 goto free_card;
7ea239d9 928
bd766312 929 err = mmc_decode_csd(card);
adf66a0d 930 if (err)
bd766312
PO
931 goto free_card;
932 err = mmc_decode_cid(card);
adf66a0d 933 if (err)
bd766312 934 goto free_card;
6abaa0c9 935 }
7ea239d9
PO
936
937 /*
89a73cf5 938 * Select card, as all following commands rely on that.
7ea239d9 939 */
af517150
DB
940 if (!mmc_host_is_spi(host)) {
941 err = mmc_select_card(card);
942 if (err)
943 goto free_card;
944 }
7ea239d9 945
6abaa0c9
PO
946 if (!oldcard) {
947 /*
af517150 948 * Fetch and process extended CSD.
6abaa0c9 949 */
08ee80cc
PR
950
951 err = mmc_get_ext_csd(card, &ext_csd);
952 if (err)
953 goto free_card;
954 err = mmc_read_ext_csd(card, ext_csd);
17b0429d 955 if (err)
6abaa0c9 956 goto free_card;
b676f039
PR
957
958 /* If doing byte addressing, check if required to do sector
959 * addressing. Handle the case of <2GB cards needing sector
960 * addressing. See section 8.1 JEDEC Standard JED84-A441;
961 * ocr register has bit 30 set for sector addressing.
962 */
963 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
964 mmc_card_set_blockaddr(card);
965
dfe86cba
AH
966 /* Erase size depends on CSD and Extended CSD */
967 mmc_set_erase_size(card);
6abaa0c9 968 }
7ea239d9 969
709de99d
CD
970 /*
971 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
25985edc 972 * bit. This bit will be lost every time after a reset or power off.
709de99d 973 */
83bb24aa
AH
974 if (card->ext_csd.enhanced_area_en ||
975 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
709de99d 976 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
b23cf0bd
SJ
977 EXT_CSD_ERASE_GROUP_DEF, 1,
978 card->ext_csd.generic_cmd6_time);
709de99d
CD
979
980 if (err && err != -EBADMSG)
981 goto free_card;
982
983 if (err) {
984 err = 0;
985 /*
986 * Just disable enhanced area off & sz
987 * will try to enable ERASE_GROUP_DEF
988 * during next time reinit
989 */
990 card->ext_csd.enhanced_area_offset = -EINVAL;
991 card->ext_csd.enhanced_area_size = -EINVAL;
992 } else {
993 card->ext_csd.erase_group_def = 1;
994 /*
995 * enable ERASE_GRP_DEF successfully.
996 * This will affect the erase size, so
997 * here need to reset erase size
998 */
999 mmc_set_erase_size(card);
1000 }
1001 }
1002
41e2a489
PR
1003 /*
1004 * Ensure eMMC user default partition is enabled
1005 */
371a689f
AW
1006 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1007 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1008 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1009 card->ext_csd.part_config,
1010 card->ext_csd.part_time);
1011 if (err && err != -EBADMSG)
1012 goto free_card;
41e2a489
PR
1013 }
1014
bec8726a
G
1015 /*
1016 * If the host supports the power_off_notify capability then
1017 * set the notification byte in the ext_csd register of device
1018 */
1019 if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) &&
a80f1627 1020 (card->ext_csd.rev >= 6)) {
bec8726a
G
1021 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1022 EXT_CSD_POWER_OFF_NOTIFICATION,
1023 EXT_CSD_POWER_ON,
1024 card->ext_csd.generic_cmd6_time);
1025 if (err && err != -EBADMSG)
1026 goto free_card;
bec8726a 1027
96a85d54
G
1028 /*
1029 * The err can be -EBADMSG or 0,
1030 * so check for success and update the flag
1031 */
1032 if (!err)
e6c08586 1033 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
96a85d54 1034 }
bec8726a 1035
89a73cf5
PO
1036 /*
1037 * Activate high speed (if supported)
1038 */
a4924c71
G
1039 if (card->ext_csd.hs_max_dtr != 0) {
1040 err = 0;
1041 if (card->ext_csd.hs_max_dtr > 52000000 &&
1042 host->caps2 & MMC_CAP2_HS200)
1043 err = mmc_select_hs200(card);
1044 else if (host->caps & MMC_CAP_MMC_HIGHSPEED)
1045 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
7488e924
G
1046 EXT_CSD_HS_TIMING, 1,
1047 card->ext_csd.generic_cmd6_time);
a4924c71 1048
ef0b27d4 1049 if (err && err != -EBADMSG)
89a73cf5
PO
1050 goto free_card;
1051
ef0b27d4 1052 if (err) {
a3c76eb9 1053 pr_warning("%s: switch to highspeed failed\n",
ef0b27d4
AH
1054 mmc_hostname(card->host));
1055 err = 0;
1056 } else {
a4924c71
G
1057 if (card->ext_csd.hs_max_dtr > 52000000 &&
1058 host->caps2 & MMC_CAP2_HS200) {
1059 mmc_card_set_hs200(card);
1060 mmc_set_timing(card->host,
1061 MMC_TIMING_MMC_HS200);
1062 } else {
1063 mmc_card_set_highspeed(card);
1064 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1065 }
ef0b27d4 1066 }
89a73cf5
PO
1067 }
1068
7ea239d9
PO
1069 /*
1070 * Compute bus speed.
1071 */
1072 max_dtr = (unsigned int)-1;
1073
a4924c71 1074 if (mmc_card_highspeed(card) || mmc_card_hs200(card)) {
7ea239d9
PO
1075 if (max_dtr > card->ext_csd.hs_max_dtr)
1076 max_dtr = card->ext_csd.hs_max_dtr;
ccb52a00
AC
1077 if (mmc_card_highspeed(card) && (max_dtr > 52000000))
1078 max_dtr = 52000000;
7ea239d9
PO
1079 } else if (max_dtr > card->csd.max_dtr) {
1080 max_dtr = card->csd.max_dtr;
1081 }
1082
1083 mmc_set_clock(host, max_dtr);
1084
dfc13e84 1085 /*
0f8d8ea6 1086 * Indicate DDR mode (if supported).
dfc13e84
HP
1087 */
1088 if (mmc_card_highspeed(card)) {
1089 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
4c4cb171
PR
1090 && ((host->caps & (MMC_CAP_1_8V_DDR |
1091 MMC_CAP_UHS_DDR50))
1092 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
49e3b5a4 1093 ddr = MMC_1_8V_DDR_MODE;
dfc13e84 1094 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
4c4cb171
PR
1095 && ((host->caps & (MMC_CAP_1_2V_DDR |
1096 MMC_CAP_UHS_DDR50))
1097 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
49e3b5a4 1098 ddr = MMC_1_2V_DDR_MODE;
dfc13e84
HP
1099 }
1100
a4924c71
G
1101 /*
1102 * Indicate HS200 SDR mode (if supported).
1103 */
1104 if (mmc_card_hs200(card)) {
1105 u32 ext_csd_bits;
1106 u32 bus_width = card->host->ios.bus_width;
1107
1108 /*
1109 * For devices supporting HS200 mode, the bus width has
1110 * to be set before executing the tuning function. If
1111 * set before tuning, then device will respond with CRC
1112 * errors for responses on CMD line. So for HS200 the
1113 * sequence will be
1114 * 1. set bus width 4bit / 8 bit (1 bit not supported)
1115 * 2. switch to HS200 mode
1116 * 3. set the clock to > 52Mhz <=200MHz and
1117 * 4. execute tuning for HS200
1118 */
1119 if ((host->caps2 & MMC_CAP2_HS200) &&
52d0974e
SJ
1120 card->host->ops->execute_tuning) {
1121 mmc_host_clk_hold(card->host);
a4924c71
G
1122 err = card->host->ops->execute_tuning(card->host,
1123 MMC_SEND_TUNING_BLOCK_HS200);
52d0974e
SJ
1124 mmc_host_clk_release(card->host);
1125 }
a4924c71
G
1126 if (err) {
1127 pr_warning("%s: tuning execution failed\n",
1128 mmc_hostname(card->host));
1129 goto err;
1130 }
1131
1132 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1133 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
1134 err = mmc_select_powerclass(card, ext_csd_bits, ext_csd);
93fc5a47 1135 if (err)
87f761b6
V
1136 pr_warning("%s: power class selection to bus width %d"
1137 " failed\n", mmc_hostname(card->host),
1138 1 << bus_width);
a4924c71
G
1139 }
1140
89a73cf5 1141 /*
0f8d8ea6 1142 * Activate wide bus and DDR (if supported).
89a73cf5 1143 */
a4924c71 1144 if (!mmc_card_hs200(card) &&
7488e924 1145 (card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
b30f8af3 1146 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
22113efd
AL
1147 static unsigned ext_csd_bits[][2] = {
1148 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
1149 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
1150 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
1151 };
1152 static unsigned bus_widths[] = {
1153 MMC_BUS_WIDTH_8,
1154 MMC_BUS_WIDTH_4,
1155 MMC_BUS_WIDTH_1
1156 };
1157 unsigned idx, bus_width = 0;
1158
1159 if (host->caps & MMC_CAP_8_BIT_DATA)
1160 idx = 0;
1161 else
1162 idx = 1;
1163 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1164 bus_width = bus_widths[idx];
1165 if (bus_width == MMC_BUS_WIDTH_1)
1166 ddr = 0; /* no DDR for 1-bit width */
b87d8dbf
G
1167 err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
1168 ext_csd);
1169 if (err)
87f761b6
V
1170 pr_warning("%s: power class selection to "
1171 "bus width %d failed\n",
1172 mmc_hostname(card->host),
1173 1 << bus_width);
b87d8dbf 1174
22113efd
AL
1175 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1176 EXT_CSD_BUS_WIDTH,
d3a8d95d 1177 ext_csd_bits[idx][0],
b23cf0bd 1178 card->ext_csd.generic_cmd6_time);
22113efd 1179 if (!err) {
4c4cb171 1180 mmc_set_bus_width(card->host, bus_width);
08ee80cc 1181
22113efd
AL
1182 /*
1183 * If controller can't handle bus width test,
08ee80cc
PR
1184 * compare ext_csd previously read in 1 bit mode
1185 * against ext_csd at new bus width
22113efd
AL
1186 */
1187 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
08ee80cc 1188 err = mmc_compare_ext_csds(card,
08ee80cc
PR
1189 bus_width);
1190 else
1191 err = mmc_bus_test(card, bus_width);
22113efd
AL
1192 if (!err)
1193 break;
1194 }
b30f8af3
JL
1195 }
1196
22113efd 1197 if (!err && ddr) {
b87d8dbf
G
1198 err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
1199 ext_csd);
1200 if (err)
87f761b6
V
1201 pr_warning("%s: power class selection to "
1202 "bus width %d ddr %d failed\n",
1203 mmc_hostname(card->host),
1204 1 << bus_width, ddr);
b87d8dbf 1205
22113efd 1206 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
d3a8d95d
AW
1207 EXT_CSD_BUS_WIDTH,
1208 ext_csd_bits[idx][1],
b23cf0bd 1209 card->ext_csd.generic_cmd6_time);
22113efd 1210 }
ef0b27d4 1211 if (err) {
a3c76eb9 1212 pr_warning("%s: switch to bus width %d ddr %d "
22113efd
AL
1213 "failed\n", mmc_hostname(card->host),
1214 1 << bus_width, ddr);
1215 goto free_card;
1216 } else if (ddr) {
4c4cb171
PR
1217 /*
1218 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1219 * signaling.
1220 *
1221 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1222 *
1223 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1224 * in the JEDEC spec for DDR.
1225 *
1226 * Do not force change in vccq since we are obviously
1227 * working and no change to vccq is needed.
1228 *
1229 * WARNING: eMMC rules are NOT the same as SD DDR
1230 */
913047e9 1231 if (ddr == MMC_1_2V_DDR_MODE) {
567c8903
JR
1232 err = __mmc_set_signal_voltage(host,
1233 MMC_SIGNAL_VOLTAGE_120);
4c4cb171
PR
1234 if (err)
1235 goto err;
1236 }
22113efd 1237 mmc_card_set_ddr_mode(card);
4c4cb171
PR
1238 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1239 mmc_set_bus_width(card->host, bus_width);
ef0b27d4 1240 }
89a73cf5
PO
1241 }
1242
52d0974e
SJ
1243 /*
1244 * Enable HPI feature (if supported)
1245 */
1246 if (card->ext_csd.hpi) {
1247 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1248 EXT_CSD_HPI_MGMT, 1,
1249 card->ext_csd.generic_cmd6_time);
1250 if (err && err != -EBADMSG)
1251 goto free_card;
1252 if (err) {
1253 pr_warning("%s: Enabling HPI failed\n",
1254 mmc_hostname(card->host));
1255 err = 0;
1256 } else
1257 card->ext_csd.hpi_en = 1;
1258 }
1259
881d1c25
SJ
1260 /*
1261 * If cache size is higher than 0, this indicates
1262 * the existence of cache and it can be turned on.
1263 */
1264 if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1265 card->ext_csd.cache_size > 0) {
1266 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
8bc0678b
SJ
1267 EXT_CSD_CACHE_CTRL, 1,
1268 card->ext_csd.generic_cmd6_time);
881d1c25
SJ
1269 if (err && err != -EBADMSG)
1270 goto free_card;
1271
1272 /*
1273 * Only if no error, cache is turned on successfully.
1274 */
8bc0678b
SJ
1275 if (err) {
1276 pr_warning("%s: Cache is supported, "
1277 "but failed to turn on (%d)\n",
1278 mmc_hostname(card->host), err);
1279 card->ext_csd.cache_ctrl = 0;
1280 err = 0;
1281 } else {
1282 card->ext_csd.cache_ctrl = 1;
1283 }
881d1c25
SJ
1284 }
1285
abd9ac14
SJ
1286 /*
1287 * The mandatory minimum values are defined for packed command.
1288 * read: 5, write: 3
1289 */
1290 if (card->ext_csd.max_packed_writes >= 3 &&
1291 card->ext_csd.max_packed_reads >= 5 &&
1292 host->caps2 & MMC_CAP2_PACKED_CMD) {
1293 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1294 EXT_CSD_EXP_EVENTS_CTRL,
1295 EXT_CSD_PACKED_EVENT_EN,
1296 card->ext_csd.generic_cmd6_time);
1297 if (err && err != -EBADMSG)
1298 goto free_card;
1299 if (err) {
1300 pr_warn("%s: Enabling packed event failed\n",
1301 mmc_hostname(card->host));
1302 card->ext_csd.packed_event_en = 0;
1303 err = 0;
1304 } else {
1305 card->ext_csd.packed_event_en = 1;
1306 }
1307 }
1308
6abaa0c9
PO
1309 if (!oldcard)
1310 host->card = card;
1311
08ee80cc 1312 mmc_free_ext_csd(ext_csd);
17b0429d 1313 return 0;
6abaa0c9
PO
1314
1315free_card:
1316 if (!oldcard)
1317 mmc_remove_card(card);
1318err:
08ee80cc 1319 mmc_free_ext_csd(ext_csd);
6abaa0c9 1320
adf66a0d 1321 return err;
6abaa0c9
PO
1322}
1323
e6c08586
UH
1324static int mmc_can_poweroff_notify(const struct mmc_card *card)
1325{
1326 return card &&
1327 mmc_card_mmc(card) &&
1328 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1329}
1330
1331static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1332{
1333 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1334 int err;
1335
1336 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1337 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1338 timeout = card->ext_csd.power_off_longtime;
1339
1340 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1341 EXT_CSD_POWER_OFF_NOTIFICATION,
1342 notify_type, timeout);
1343 if (err)
1344 pr_err("%s: Power Off Notification timed out, %u\n",
1345 mmc_hostname(card->host), timeout);
1346
1347 /* Disable the power off notification after the switch operation. */
1348 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1349
1350 return err;
1351}
1352
6abaa0c9
PO
1353/*
1354 * Host is being removed. Free up the current card.
1355 */
1356static void mmc_remove(struct mmc_host *host)
1357{
1358 BUG_ON(!host);
1359 BUG_ON(!host->card);
1360
1361 mmc_remove_card(host->card);
1362 host->card = NULL;
1363}
1364
d3049504
AH
1365/*
1366 * Card detection - card is alive.
1367 */
1368static int mmc_alive(struct mmc_host *host)
1369{
1370 return mmc_send_status(host->card, NULL);
1371}
1372
6abaa0c9
PO
1373/*
1374 * Card detection callback from host.
1375 */
1376static void mmc_detect(struct mmc_host *host)
1377{
1378 int err;
1379
1380 BUG_ON(!host);
1381 BUG_ON(!host->card);
1382
e94cfef6 1383 mmc_get_card(host->card);
6abaa0c9
PO
1384
1385 /*
1386 * Just check if our card has been removed.
1387 */
d3049504 1388 err = _mmc_detect_card_removed(host);
6abaa0c9 1389
e94cfef6 1390 mmc_put_card(host->card);
6abaa0c9 1391
17b0429d 1392 if (err) {
4101c16a 1393 mmc_remove(host);
6abaa0c9
PO
1394
1395 mmc_claim_host(host);
1396 mmc_detach_bus(host);
7f7e4129 1397 mmc_power_off(host);
6abaa0c9
PO
1398 mmc_release_host(host);
1399 }
1400}
1401
6abaa0c9
PO
1402/*
1403 * Suspend callback from host.
1404 */
95cdfb72 1405static int mmc_suspend(struct mmc_host *host)
6abaa0c9 1406{
c3805467
B
1407 int err = 0;
1408
6abaa0c9
PO
1409 BUG_ON(!host);
1410 BUG_ON(!host->card);
7ea239d9 1411
6abaa0c9 1412 mmc_claim_host(host);
881d926d 1413
39b9431b
UH
1414 if (mmc_card_doing_bkops(host->card)) {
1415 err = mmc_stop_bkops(host->card);
1416 if (err)
1417 goto out;
1418 }
1419
881d926d
ME
1420 err = mmc_cache_ctrl(host, 0);
1421 if (err)
1422 goto out;
1423
e6c08586
UH
1424 if (mmc_can_poweroff_notify(host->card))
1425 err = mmc_poweroff_notify(host->card, EXT_CSD_POWER_OFF_SHORT);
1426 else if (mmc_card_can_sleep(host))
c3805467 1427 err = mmc_card_sleep(host);
e6c08586 1428 else if (!mmc_host_is_spi(host))
85e727ed 1429 err = mmc_deselect_cards(host);
3e73c36b 1430 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
95cdfb72 1431
881d926d
ME
1432out:
1433 mmc_release_host(host);
c3805467 1434 return err;
6abaa0c9 1435}
7ea239d9 1436
6abaa0c9
PO
1437/*
1438 * Resume callback from host.
1439 *
1440 * This function tries to determine if the same card is still present
1441 * and, if so, restore all state to it.
1442 */
95cdfb72 1443static int mmc_resume(struct mmc_host *host)
6abaa0c9
PO
1444{
1445 int err;
1446
1447 BUG_ON(!host);
1448 BUG_ON(!host->card);
1449
1450 mmc_claim_host(host);
e6c08586 1451 err = mmc_init_card(host, host->ocr, host->card);
2986d0bf
PO
1452 mmc_release_host(host);
1453
95cdfb72 1454 return err;
6abaa0c9
PO
1455}
1456
c4d770d7
UH
1457
1458/*
1459 * Callback for runtime_suspend.
1460 */
1461static int mmc_runtime_suspend(struct mmc_host *host)
1462{
1463 int err;
1464
1465 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1466 return 0;
1467
1468 mmc_claim_host(host);
1469
1470 err = mmc_suspend(host);
1471 if (err) {
1472 pr_err("%s: error %d doing aggessive suspend\n",
1473 mmc_hostname(host), err);
1474 goto out;
1475 }
1476 mmc_power_off(host);
1477
1478out:
1479 mmc_release_host(host);
1480 return err;
1481}
1482
1483/*
1484 * Callback for runtime_resume.
1485 */
1486static int mmc_runtime_resume(struct mmc_host *host)
1487{
1488 int err;
1489
1490 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1491 return 0;
1492
1493 mmc_claim_host(host);
1494
1495 mmc_power_up(host);
1496 err = mmc_resume(host);
1497 if (err)
1498 pr_err("%s: error %d doing aggessive resume\n",
1499 mmc_hostname(host), err);
1500
1501 mmc_release_host(host);
1502 return 0;
1503}
1504
12ae637f 1505static int mmc_power_restore(struct mmc_host *host)
eae1aeee 1506{
12ae637f
OBC
1507 int ret;
1508
3e73c36b 1509 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
eae1aeee 1510 mmc_claim_host(host);
12ae637f 1511 ret = mmc_init_card(host, host->ocr, host->card);
eae1aeee 1512 mmc_release_host(host);
12ae637f
OBC
1513
1514 return ret;
eae1aeee
AH
1515}
1516
b1ebe384
JL
1517static int mmc_sleep(struct mmc_host *host)
1518{
1519 struct mmc_card *card = host->card;
1520 int err = -ENOSYS;
1521
1522 if (card && card->ext_csd.rev >= 3) {
1523 err = mmc_card_sleepawake(host, 1);
1524 if (err < 0)
1525 pr_debug("%s: Error %d while putting card into sleep",
1526 mmc_hostname(host), err);
1527 }
1528
1529 return err;
1530}
1531
1532static int mmc_awake(struct mmc_host *host)
1533{
1534 struct mmc_card *card = host->card;
1535 int err = -ENOSYS;
1536
1537 if (card && card->ext_csd.rev >= 3) {
1538 err = mmc_card_sleepawake(host, 0);
1539 if (err < 0)
1540 pr_debug("%s: Error %d while awaking sleeping card",
1541 mmc_hostname(host), err);
1542 }
1543
1544 return err;
1545}
1546
6abaa0c9 1547static const struct mmc_bus_ops mmc_ops = {
b1ebe384
JL
1548 .awake = mmc_awake,
1549 .sleep = mmc_sleep,
9feae246
AH
1550 .remove = mmc_remove,
1551 .detect = mmc_detect,
1552 .suspend = NULL,
1553 .resume = NULL,
eae1aeee 1554 .power_restore = mmc_power_restore,
d3049504 1555 .alive = mmc_alive,
9feae246
AH
1556};
1557
1558static const struct mmc_bus_ops mmc_ops_unsafe = {
b1ebe384
JL
1559 .awake = mmc_awake,
1560 .sleep = mmc_sleep,
6abaa0c9
PO
1561 .remove = mmc_remove,
1562 .detect = mmc_detect,
1563 .suspend = mmc_suspend,
1564 .resume = mmc_resume,
c4d770d7
UH
1565 .runtime_suspend = mmc_runtime_suspend,
1566 .runtime_resume = mmc_runtime_resume,
eae1aeee 1567 .power_restore = mmc_power_restore,
d3049504 1568 .alive = mmc_alive,
6abaa0c9
PO
1569};
1570
9feae246
AH
1571static void mmc_attach_bus_ops(struct mmc_host *host)
1572{
1573 const struct mmc_bus_ops *bus_ops;
1574
71d7d3d1 1575 if (!mmc_card_is_removable(host))
9feae246
AH
1576 bus_ops = &mmc_ops_unsafe;
1577 else
1578 bus_ops = &mmc_ops;
1579 mmc_attach_bus(host, bus_ops);
1580}
1581
6abaa0c9
PO
1582/*
1583 * Starting point for MMC card init.
1584 */
807e8e40 1585int mmc_attach_mmc(struct mmc_host *host)
6abaa0c9
PO
1586{
1587 int err;
807e8e40 1588 u32 ocr;
6abaa0c9
PO
1589
1590 BUG_ON(!host);
d84075c8 1591 WARN_ON(!host->claimed);
6abaa0c9 1592
44669034
SNX
1593 /* Set correct bus mode for MMC before attempting attach */
1594 if (!mmc_host_is_spi(host))
1595 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1596
807e8e40
AR
1597 err = mmc_send_op_cond(host, 0, &ocr);
1598 if (err)
1599 return err;
1600
9feae246 1601 mmc_attach_bus_ops(host);
8f230f45
TI
1602 if (host->ocr_avail_mmc)
1603 host->ocr_avail = host->ocr_avail_mmc;
6abaa0c9 1604
af517150
DB
1605 /*
1606 * We need to get OCR a different way for SPI.
1607 */
1608 if (mmc_host_is_spi(host)) {
1609 err = mmc_spi_read_ocr(host, 1, &ocr);
1610 if (err)
1611 goto err;
1612 }
1613
6abaa0c9
PO
1614 /*
1615 * Sanity check the voltages that the card claims to
1616 * support.
1617 */
1618 if (ocr & 0x7F) {
a3c76eb9 1619 pr_warning("%s: card claims to support voltages "
6abaa0c9
PO
1620 "below the defined range. These will be ignored.\n",
1621 mmc_hostname(host));
1622 ocr &= ~0x7F;
1623 }
1624
1625 host->ocr = mmc_select_voltage(host, ocr);
1626
1627 /*
1628 * Can we support the voltage of the card?
1629 */
109b5bed
PO
1630 if (!host->ocr) {
1631 err = -EINVAL;
6abaa0c9 1632 goto err;
109b5bed 1633 }
6abaa0c9
PO
1634
1635 /*
1636 * Detect and init the card.
1637 */
8c75deae 1638 err = mmc_init_card(host, host->ocr, NULL);
17b0429d 1639 if (err)
6abaa0c9
PO
1640 goto err;
1641
1642 mmc_release_host(host);
4101c16a 1643 err = mmc_add_card(host->card);
807e8e40 1644 mmc_claim_host(host);
7ea239d9 1645 if (err)
2986d0bf 1646 goto remove_card;
7ea239d9
PO
1647
1648 return 0;
1649
2986d0bf 1650remove_card:
807e8e40 1651 mmc_release_host(host);
6abaa0c9 1652 mmc_remove_card(host->card);
2986d0bf 1653 mmc_claim_host(host);
807e8e40 1654 host->card = NULL;
7ea239d9
PO
1655err:
1656 mmc_detach_bus(host);
7ea239d9 1657
a3c76eb9 1658 pr_err("%s: error %d whilst initialising MMC card\n",
109b5bed
PO
1659 mmc_hostname(host), err);
1660
adf66a0d 1661 return err;
7ea239d9 1662}