mmc: core: Add shutdown callback for (e)MMC bus_ops
[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];
6636bad8 296 if (card->ext_csd.rev > 7) {
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;
60443712
FS
464
465 /* Save power class values */
466 card->ext_csd.raw_pwr_cl_52_195 =
467 ext_csd[EXT_CSD_PWR_CL_52_195];
468 card->ext_csd.raw_pwr_cl_26_195 =
469 ext_csd[EXT_CSD_PWR_CL_26_195];
470 card->ext_csd.raw_pwr_cl_52_360 =
471 ext_csd[EXT_CSD_PWR_CL_52_360];
472 card->ext_csd.raw_pwr_cl_26_360 =
473 ext_csd[EXT_CSD_PWR_CL_26_360];
474 card->ext_csd.raw_pwr_cl_200_195 =
475 ext_csd[EXT_CSD_PWR_CL_200_195];
476 card->ext_csd.raw_pwr_cl_200_360 =
477 ext_csd[EXT_CSD_PWR_CL_200_360];
478 card->ext_csd.raw_pwr_cl_ddr_52_195 =
479 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
480 card->ext_csd.raw_pwr_cl_ddr_52_360 =
481 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
b1ebe384
JL
482 }
483
b2499518 484 if (card->ext_csd.rev >= 5) {
950d56ac
JC
485 /* check whether the eMMC card supports BKOPS */
486 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
487 card->ext_csd.bkops = 1;
488 card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
489 card->ext_csd.raw_bkops_status =
490 ext_csd[EXT_CSD_BKOPS_STATUS];
491 if (!card->ext_csd.bkops_en)
492 pr_info("%s: BKOPS_EN bit is not set\n",
493 mmc_hostname(card->host));
494 }
495
eb0d8f13
JC
496 /* check whether the eMMC card supports HPI */
497 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
498 card->ext_csd.hpi = 1;
499 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
500 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
501 else
502 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
503 /*
504 * Indicate the maximum timeout to close
505 * a command interrupted by HPI
506 */
507 card->ext_csd.out_of_int_time =
508 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
509 }
510
f4c5522b 511 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
b2499518 512 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
090d25fe
LP
513
514 /*
515 * RPMB regions are defined in multiples of 128K.
516 */
517 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
d0123cca 518 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
090d25fe
LP
519 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
520 EXT_CSD_PART_CONFIG_ACC_RPMB,
521 "rpmb", 0, false,
522 MMC_BLK_DATA_AREA_RPMB);
523 }
b2499518 524 }
f4c5522b 525
5238acbe 526 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
dfe86cba
AH
527 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
528 card->erased_byte = 0xFF;
529 else
530 card->erased_byte = 0x0;
531
336c716a 532 /* eMMC v4.5 or later */
bec8726a 533 if (card->ext_csd.rev >= 6) {
336c716a
SJ
534 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
535
b23cf0bd
SJ
536 card->ext_csd.generic_cmd6_time = 10 *
537 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
bec8726a
G
538 card->ext_csd.power_off_longtime = 10 *
539 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
b23cf0bd 540
336c716a
SJ
541 card->ext_csd.cache_size =
542 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
543 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
544 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
545 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
4265900e
SD
546
547 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
548 card->ext_csd.data_sector_size = 4096;
549 else
550 card->ext_csd.data_sector_size = 512;
551
552 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
553 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
554 card->ext_csd.data_tag_unit_size =
555 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
556 (card->ext_csd.data_sector_size);
557 } else {
558 card->ext_csd.data_tag_unit_size = 0;
559 }
abd9ac14
SJ
560
561 card->ext_csd.max_packed_writes =
562 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
563 card->ext_csd.max_packed_reads =
564 ext_csd[EXT_CSD_MAX_PACKED_READS];
a5075eb9
SD
565 } else {
566 card->ext_csd.data_sector_size = 512;
336c716a 567 }
881d1c25 568
7ea239d9 569out:
08ee80cc
PR
570 return err;
571}
572
573static inline void mmc_free_ext_csd(u8 *ext_csd)
574{
7ea239d9 575 kfree(ext_csd);
08ee80cc
PR
576}
577
7ea239d9 578
f39b2dd9 579static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
08ee80cc
PR
580{
581 u8 *bw_ext_csd;
582 int err;
583
f39b2dd9
PR
584 if (bus_width == MMC_BUS_WIDTH_1)
585 return 0;
586
08ee80cc 587 err = mmc_get_ext_csd(card, &bw_ext_csd);
08ee80cc 588
f39b2dd9 589 if (err || bw_ext_csd == NULL) {
f6f44590 590 err = -EINVAL;
08ee80cc
PR
591 goto out;
592 }
593
08ee80cc 594 /* only compare read only fields */
dd13b4ed 595 err = !((card->ext_csd.raw_partition_support ==
08ee80cc 596 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
f39b2dd9 597 (card->ext_csd.raw_erased_mem_count ==
08ee80cc 598 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
f39b2dd9 599 (card->ext_csd.rev ==
08ee80cc 600 bw_ext_csd[EXT_CSD_REV]) &&
f39b2dd9 601 (card->ext_csd.raw_ext_csd_structure ==
08ee80cc 602 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
f39b2dd9 603 (card->ext_csd.raw_card_type ==
08ee80cc 604 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
f39b2dd9 605 (card->ext_csd.raw_s_a_timeout ==
08ee80cc 606 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
f39b2dd9 607 (card->ext_csd.raw_hc_erase_gap_size ==
08ee80cc 608 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
f39b2dd9 609 (card->ext_csd.raw_erase_timeout_mult ==
08ee80cc 610 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
f39b2dd9 611 (card->ext_csd.raw_hc_erase_grp_size ==
08ee80cc 612 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
f39b2dd9 613 (card->ext_csd.raw_sec_trim_mult ==
08ee80cc 614 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
f39b2dd9 615 (card->ext_csd.raw_sec_erase_mult ==
08ee80cc 616 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
f39b2dd9 617 (card->ext_csd.raw_sec_feature_support ==
08ee80cc 618 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
f39b2dd9 619 (card->ext_csd.raw_trim_mult ==
08ee80cc 620 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
f39b2dd9
PR
621 (card->ext_csd.raw_sectors[0] ==
622 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
623 (card->ext_csd.raw_sectors[1] ==
624 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
625 (card->ext_csd.raw_sectors[2] ==
626 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
627 (card->ext_csd.raw_sectors[3] ==
60443712
FS
628 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
629 (card->ext_csd.raw_pwr_cl_52_195 ==
630 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
631 (card->ext_csd.raw_pwr_cl_26_195 ==
632 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
633 (card->ext_csd.raw_pwr_cl_52_360 ==
634 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
635 (card->ext_csd.raw_pwr_cl_26_360 ==
636 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
637 (card->ext_csd.raw_pwr_cl_200_195 ==
638 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
639 (card->ext_csd.raw_pwr_cl_200_360 ==
640 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
641 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
642 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
643 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
644 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]));
08ee80cc
PR
645 if (err)
646 err = -EINVAL;
647
648out:
649 mmc_free_ext_csd(bw_ext_csd);
7ea239d9
PO
650 return err;
651}
652
51ec92e2
PO
653MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
654 card->raw_cid[2], card->raw_cid[3]);
655MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
656 card->raw_csd[2], card->raw_csd[3]);
657MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
dfe86cba
AH
658MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
659MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
51ec92e2
PO
660MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
661MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
662MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
663MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
664MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
51e7e8b6 665MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
51ec92e2 666MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
709de99d
CD
667MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
668 card->ext_csd.enhanced_area_offset);
669MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
188cc042
LP
670MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
671MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
51ec92e2
PO
672
673static struct attribute *mmc_std_attrs[] = {
674 &dev_attr_cid.attr,
675 &dev_attr_csd.attr,
676 &dev_attr_date.attr,
dfe86cba
AH
677 &dev_attr_erase_size.attr,
678 &dev_attr_preferred_erase_size.attr,
51ec92e2
PO
679 &dev_attr_fwrev.attr,
680 &dev_attr_hwrev.attr,
681 &dev_attr_manfid.attr,
682 &dev_attr_name.attr,
683 &dev_attr_oemid.attr,
51e7e8b6 684 &dev_attr_prv.attr,
51ec92e2 685 &dev_attr_serial.attr,
709de99d
CD
686 &dev_attr_enhanced_area_offset.attr,
687 &dev_attr_enhanced_area_size.attr,
188cc042
LP
688 &dev_attr_raw_rpmb_size_mult.attr,
689 &dev_attr_rel_sectors.attr,
51ec92e2
PO
690 NULL,
691};
692
693static struct attribute_group mmc_std_attr_group = {
694 .attrs = mmc_std_attrs,
695};
696
a4dbd674 697static const struct attribute_group *mmc_attr_groups[] = {
51ec92e2
PO
698 &mmc_std_attr_group,
699 NULL,
700};
701
702static struct device_type mmc_type = {
703 .groups = mmc_attr_groups,
704};
705
b87d8dbf
G
706/*
707 * Select the PowerClass for the current bus width
708 * If power class is defined for 4/8 bit bus in the
709 * extended CSD register, select it by executing the
710 * mmc_switch command.
711 */
712static int mmc_select_powerclass(struct mmc_card *card,
60443712 713 unsigned int bus_width)
b87d8dbf
G
714{
715 int err = 0;
60443712 716 unsigned int pwrclass_val = 0;
b87d8dbf
G
717 struct mmc_host *host;
718
719 BUG_ON(!card);
720
721 host = card->host;
722 BUG_ON(!host);
723
b87d8dbf
G
724 /* Power class selection is supported for versions >= 4.0 */
725 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
726 return 0;
727
728 /* Power class values are defined only for 4/8 bit bus */
729 if (bus_width == EXT_CSD_BUS_WIDTH_1)
730 return 0;
731
732 switch (1 << host->ios.vdd) {
733 case MMC_VDD_165_195:
734 if (host->ios.clock <= 26000000)
60443712 735 pwrclass_val = card->ext_csd.raw_pwr_cl_26_195;
b87d8dbf 736 else if (host->ios.clock <= 52000000)
60443712
FS
737 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
738 card->ext_csd.raw_pwr_cl_52_195 :
739 card->ext_csd.raw_pwr_cl_ddr_52_195;
b87d8dbf 740 else if (host->ios.clock <= 200000000)
60443712 741 pwrclass_val = card->ext_csd.raw_pwr_cl_200_195;
b87d8dbf 742 break;
93fc5a47
SJ
743 case MMC_VDD_27_28:
744 case MMC_VDD_28_29:
745 case MMC_VDD_29_30:
746 case MMC_VDD_30_31:
747 case MMC_VDD_31_32:
b87d8dbf
G
748 case MMC_VDD_32_33:
749 case MMC_VDD_33_34:
750 case MMC_VDD_34_35:
751 case MMC_VDD_35_36:
752 if (host->ios.clock <= 26000000)
60443712 753 pwrclass_val = card->ext_csd.raw_pwr_cl_26_360;
b87d8dbf 754 else if (host->ios.clock <= 52000000)
60443712
FS
755 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
756 card->ext_csd.raw_pwr_cl_52_360 :
757 card->ext_csd.raw_pwr_cl_ddr_52_360;
b87d8dbf 758 else if (host->ios.clock <= 200000000)
60443712 759 pwrclass_val = card->ext_csd.raw_pwr_cl_200_360;
b87d8dbf
G
760 break;
761 default:
762 pr_warning("%s: Voltage range not supported "
763 "for power class.\n", mmc_hostname(host));
764 return -EINVAL;
765 }
766
b87d8dbf
G
767 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
768 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
769 EXT_CSD_PWR_CL_8BIT_SHIFT;
770 else
771 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
772 EXT_CSD_PWR_CL_4BIT_SHIFT;
773
774 /* If the power class is different from the default value */
775 if (pwrclass_val > 0) {
776 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
777 EXT_CSD_POWER_CLASS,
778 pwrclass_val,
71fe3eb0 779 card->ext_csd.generic_cmd6_time);
b87d8dbf
G
780 }
781
782 return err;
783}
784
a4924c71
G
785/*
786 * Selects the desired buswidth and switch to the HS200 mode
787 * if bus width set without error
788 */
789static int mmc_select_hs200(struct mmc_card *card)
790{
10942aa4 791 int idx, err = -EINVAL;
a4924c71
G
792 struct mmc_host *host;
793 static unsigned ext_csd_bits[] = {
794 EXT_CSD_BUS_WIDTH_4,
795 EXT_CSD_BUS_WIDTH_8,
796 };
797 static unsigned bus_widths[] = {
798 MMC_BUS_WIDTH_4,
799 MMC_BUS_WIDTH_8,
800 };
801
802 BUG_ON(!card);
803
804 host = card->host;
805
806 if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V &&
10942aa4 807 host->caps2 & MMC_CAP2_HS200_1_2V_SDR)
567c8903 808 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
10942aa4
SJ
809
810 if (err && card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_8V &&
811 host->caps2 & MMC_CAP2_HS200_1_8V_SDR)
567c8903 812 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
a4924c71
G
813
814 /* If fails try again during next card power cycle */
815 if (err)
816 goto err;
817
818 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0;
819
820 /*
821 * Unlike SD, MMC cards dont have a configuration register to notify
822 * supported bus width. So bus test command should be run to identify
823 * the supported bus width or compare the ext csd values of current
824 * bus width and ext csd values of 1 bit mode read earlier.
825 */
826 for (; idx >= 0; idx--) {
827
828 /*
829 * Host is capable of 8bit transfer, then switch
830 * the device to work in 8bit transfer mode. If the
831 * mmc switch command returns error then switch to
832 * 4bit transfer mode. On success set the corresponding
833 * bus width on the host.
834 */
835 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
836 EXT_CSD_BUS_WIDTH,
837 ext_csd_bits[idx],
838 card->ext_csd.generic_cmd6_time);
839 if (err)
840 continue;
841
842 mmc_set_bus_width(card->host, bus_widths[idx]);
843
844 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
845 err = mmc_compare_ext_csds(card, bus_widths[idx]);
846 else
847 err = mmc_bus_test(card, bus_widths[idx]);
848 if (!err)
849 break;
850 }
851
852 /* switch to HS200 mode if bus width set successfully */
853 if (!err)
854 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
855 EXT_CSD_HS_TIMING, 2, 0);
856err:
857 return err;
858}
859
7ea239d9 860/*
6abaa0c9
PO
861 * Handle the detection and initialisation of a card.
862 *
8769392b 863 * In the case of a resume, "oldcard" will contain the card
6abaa0c9 864 * we're trying to reinitialise.
7ea239d9 865 */
8c75deae 866static int mmc_init_card(struct mmc_host *host, u32 ocr,
6abaa0c9 867 struct mmc_card *oldcard)
7ea239d9
PO
868{
869 struct mmc_card *card;
25d5c699 870 int err, ddr = 0;
7ea239d9
PO
871 u32 cid[4];
872 unsigned int max_dtr;
b676f039 873 u32 rocr;
08ee80cc 874 u8 *ext_csd = NULL;
7ea239d9
PO
875
876 BUG_ON(!host);
d84075c8 877 WARN_ON(!host->claimed);
7ea239d9 878
44669034
SNX
879 /* Set correct bus mode for MMC before attempting init */
880 if (!mmc_host_is_spi(host))
881 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
882
7ea239d9
PO
883 /*
884 * Since we're changing the OCR value, we seem to
885 * need to tell some cards to go back to the idle
886 * state. We wait 1ms to give cards time to
887 * respond.
c3805467 888 * mmc_go_idle is needed for eMMC that are asleep
7ea239d9
PO
889 */
890 mmc_go_idle(host);
891
892 /* The extra bit indicates that we support high capacity */
b676f039 893 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
17b0429d 894 if (err)
6abaa0c9 895 goto err;
7ea239d9 896
af517150
DB
897 /*
898 * For SPI, enable CRC as appropriate.
899 */
900 if (mmc_host_is_spi(host)) {
901 err = mmc_spi_set_crc(host, use_spi_crc);
902 if (err)
903 goto err;
904 }
905
7ea239d9
PO
906 /*
907 * Fetch CID from card.
908 */
af517150
DB
909 if (mmc_host_is_spi(host))
910 err = mmc_send_cid(host, cid);
911 else
912 err = mmc_all_send_cid(host, cid);
17b0429d 913 if (err)
7ea239d9
PO
914 goto err;
915
6abaa0c9 916 if (oldcard) {
adf66a0d
PO
917 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
918 err = -ENOENT;
6abaa0c9 919 goto err;
adf66a0d 920 }
6abaa0c9
PO
921
922 card = oldcard;
923 } else {
924 /*
925 * Allocate card structure.
926 */
51ec92e2 927 card = mmc_alloc_card(host, &mmc_type);
adf66a0d
PO
928 if (IS_ERR(card)) {
929 err = PTR_ERR(card);
6abaa0c9 930 goto err;
adf66a0d 931 }
7ea239d9 932
6abaa0c9
PO
933 card->type = MMC_TYPE_MMC;
934 card->rca = 1;
935 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
936 }
7ea239d9
PO
937
938 /*
af517150 939 * For native busses: set card RCA and quit open drain mode.
7ea239d9 940 */
af517150
DB
941 if (!mmc_host_is_spi(host)) {
942 err = mmc_set_relative_addr(card);
943 if (err)
944 goto free_card;
7ea239d9 945
af517150
DB
946 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
947 }
7ea239d9 948
6abaa0c9
PO
949 if (!oldcard) {
950 /*
951 * Fetch CSD from card.
952 */
953 err = mmc_send_csd(card, card->raw_csd);
17b0429d 954 if (err)
6abaa0c9 955 goto free_card;
7ea239d9 956
bd766312 957 err = mmc_decode_csd(card);
adf66a0d 958 if (err)
bd766312
PO
959 goto free_card;
960 err = mmc_decode_cid(card);
adf66a0d 961 if (err)
bd766312 962 goto free_card;
6abaa0c9 963 }
7ea239d9
PO
964
965 /*
89a73cf5 966 * Select card, as all following commands rely on that.
7ea239d9 967 */
af517150
DB
968 if (!mmc_host_is_spi(host)) {
969 err = mmc_select_card(card);
970 if (err)
971 goto free_card;
972 }
7ea239d9 973
6abaa0c9
PO
974 if (!oldcard) {
975 /*
af517150 976 * Fetch and process extended CSD.
6abaa0c9 977 */
08ee80cc
PR
978
979 err = mmc_get_ext_csd(card, &ext_csd);
980 if (err)
981 goto free_card;
982 err = mmc_read_ext_csd(card, ext_csd);
17b0429d 983 if (err)
6abaa0c9 984 goto free_card;
b676f039
PR
985
986 /* If doing byte addressing, check if required to do sector
987 * addressing. Handle the case of <2GB cards needing sector
988 * addressing. See section 8.1 JEDEC Standard JED84-A441;
989 * ocr register has bit 30 set for sector addressing.
990 */
991 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
992 mmc_card_set_blockaddr(card);
993
dfe86cba
AH
994 /* Erase size depends on CSD and Extended CSD */
995 mmc_set_erase_size(card);
6abaa0c9 996 }
7ea239d9 997
709de99d
CD
998 /*
999 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
25985edc 1000 * bit. This bit will be lost every time after a reset or power off.
709de99d 1001 */
83bb24aa
AH
1002 if (card->ext_csd.enhanced_area_en ||
1003 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
709de99d 1004 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
b23cf0bd
SJ
1005 EXT_CSD_ERASE_GROUP_DEF, 1,
1006 card->ext_csd.generic_cmd6_time);
709de99d
CD
1007
1008 if (err && err != -EBADMSG)
1009 goto free_card;
1010
1011 if (err) {
1012 err = 0;
1013 /*
1014 * Just disable enhanced area off & sz
1015 * will try to enable ERASE_GROUP_DEF
1016 * during next time reinit
1017 */
1018 card->ext_csd.enhanced_area_offset = -EINVAL;
1019 card->ext_csd.enhanced_area_size = -EINVAL;
1020 } else {
1021 card->ext_csd.erase_group_def = 1;
1022 /*
1023 * enable ERASE_GRP_DEF successfully.
1024 * This will affect the erase size, so
1025 * here need to reset erase size
1026 */
1027 mmc_set_erase_size(card);
1028 }
1029 }
1030
41e2a489
PR
1031 /*
1032 * Ensure eMMC user default partition is enabled
1033 */
371a689f
AW
1034 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1035 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1036 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1037 card->ext_csd.part_config,
1038 card->ext_csd.part_time);
1039 if (err && err != -EBADMSG)
1040 goto free_card;
41e2a489
PR
1041 }
1042
bec8726a
G
1043 /*
1044 * If the host supports the power_off_notify capability then
1045 * set the notification byte in the ext_csd register of device
1046 */
1047 if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) &&
a80f1627 1048 (card->ext_csd.rev >= 6)) {
bec8726a
G
1049 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1050 EXT_CSD_POWER_OFF_NOTIFICATION,
1051 EXT_CSD_POWER_ON,
1052 card->ext_csd.generic_cmd6_time);
1053 if (err && err != -EBADMSG)
1054 goto free_card;
bec8726a 1055
96a85d54
G
1056 /*
1057 * The err can be -EBADMSG or 0,
1058 * so check for success and update the flag
1059 */
1060 if (!err)
e6c08586 1061 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
96a85d54 1062 }
bec8726a 1063
89a73cf5
PO
1064 /*
1065 * Activate high speed (if supported)
1066 */
a4924c71
G
1067 if (card->ext_csd.hs_max_dtr != 0) {
1068 err = 0;
1069 if (card->ext_csd.hs_max_dtr > 52000000 &&
1070 host->caps2 & MMC_CAP2_HS200)
1071 err = mmc_select_hs200(card);
1072 else if (host->caps & MMC_CAP_MMC_HIGHSPEED)
1073 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
7488e924
G
1074 EXT_CSD_HS_TIMING, 1,
1075 card->ext_csd.generic_cmd6_time);
a4924c71 1076
ef0b27d4 1077 if (err && err != -EBADMSG)
89a73cf5
PO
1078 goto free_card;
1079
ef0b27d4 1080 if (err) {
a3c76eb9 1081 pr_warning("%s: switch to highspeed failed\n",
ef0b27d4
AH
1082 mmc_hostname(card->host));
1083 err = 0;
1084 } else {
a4924c71
G
1085 if (card->ext_csd.hs_max_dtr > 52000000 &&
1086 host->caps2 & MMC_CAP2_HS200) {
1087 mmc_card_set_hs200(card);
1088 mmc_set_timing(card->host,
1089 MMC_TIMING_MMC_HS200);
1090 } else {
1091 mmc_card_set_highspeed(card);
1092 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1093 }
ef0b27d4 1094 }
89a73cf5
PO
1095 }
1096
7ea239d9
PO
1097 /*
1098 * Compute bus speed.
1099 */
1100 max_dtr = (unsigned int)-1;
1101
a4924c71 1102 if (mmc_card_highspeed(card) || mmc_card_hs200(card)) {
7ea239d9
PO
1103 if (max_dtr > card->ext_csd.hs_max_dtr)
1104 max_dtr = card->ext_csd.hs_max_dtr;
ccb52a00
AC
1105 if (mmc_card_highspeed(card) && (max_dtr > 52000000))
1106 max_dtr = 52000000;
7ea239d9
PO
1107 } else if (max_dtr > card->csd.max_dtr) {
1108 max_dtr = card->csd.max_dtr;
1109 }
1110
1111 mmc_set_clock(host, max_dtr);
1112
dfc13e84 1113 /*
0f8d8ea6 1114 * Indicate DDR mode (if supported).
dfc13e84
HP
1115 */
1116 if (mmc_card_highspeed(card)) {
1117 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
4c4cb171
PR
1118 && ((host->caps & (MMC_CAP_1_8V_DDR |
1119 MMC_CAP_UHS_DDR50))
1120 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
49e3b5a4 1121 ddr = MMC_1_8V_DDR_MODE;
dfc13e84 1122 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
4c4cb171
PR
1123 && ((host->caps & (MMC_CAP_1_2V_DDR |
1124 MMC_CAP_UHS_DDR50))
1125 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
49e3b5a4 1126 ddr = MMC_1_2V_DDR_MODE;
dfc13e84
HP
1127 }
1128
a4924c71
G
1129 /*
1130 * Indicate HS200 SDR mode (if supported).
1131 */
1132 if (mmc_card_hs200(card)) {
1133 u32 ext_csd_bits;
1134 u32 bus_width = card->host->ios.bus_width;
1135
1136 /*
1137 * For devices supporting HS200 mode, the bus width has
1138 * to be set before executing the tuning function. If
1139 * set before tuning, then device will respond with CRC
1140 * errors for responses on CMD line. So for HS200 the
1141 * sequence will be
1142 * 1. set bus width 4bit / 8 bit (1 bit not supported)
1143 * 2. switch to HS200 mode
1144 * 3. set the clock to > 52Mhz <=200MHz and
1145 * 4. execute tuning for HS200
1146 */
1147 if ((host->caps2 & MMC_CAP2_HS200) &&
52d0974e
SJ
1148 card->host->ops->execute_tuning) {
1149 mmc_host_clk_hold(card->host);
a4924c71
G
1150 err = card->host->ops->execute_tuning(card->host,
1151 MMC_SEND_TUNING_BLOCK_HS200);
52d0974e
SJ
1152 mmc_host_clk_release(card->host);
1153 }
a4924c71
G
1154 if (err) {
1155 pr_warning("%s: tuning execution failed\n",
1156 mmc_hostname(card->host));
1157 goto err;
1158 }
1159
1160 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1161 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
60443712 1162 err = mmc_select_powerclass(card, ext_csd_bits);
93fc5a47 1163 if (err)
87f761b6
V
1164 pr_warning("%s: power class selection to bus width %d"
1165 " failed\n", mmc_hostname(card->host),
1166 1 << bus_width);
a4924c71
G
1167 }
1168
89a73cf5 1169 /*
0f8d8ea6 1170 * Activate wide bus and DDR (if supported).
89a73cf5 1171 */
a4924c71 1172 if (!mmc_card_hs200(card) &&
7488e924 1173 (card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
b30f8af3 1174 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
22113efd
AL
1175 static unsigned ext_csd_bits[][2] = {
1176 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
1177 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
1178 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
1179 };
1180 static unsigned bus_widths[] = {
1181 MMC_BUS_WIDTH_8,
1182 MMC_BUS_WIDTH_4,
1183 MMC_BUS_WIDTH_1
1184 };
1185 unsigned idx, bus_width = 0;
1186
1187 if (host->caps & MMC_CAP_8_BIT_DATA)
1188 idx = 0;
1189 else
1190 idx = 1;
1191 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1192 bus_width = bus_widths[idx];
1193 if (bus_width == MMC_BUS_WIDTH_1)
1194 ddr = 0; /* no DDR for 1-bit width */
60443712 1195 err = mmc_select_powerclass(card, ext_csd_bits[idx][0]);
b87d8dbf 1196 if (err)
87f761b6
V
1197 pr_warning("%s: power class selection to "
1198 "bus width %d failed\n",
1199 mmc_hostname(card->host),
1200 1 << bus_width);
b87d8dbf 1201
22113efd
AL
1202 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1203 EXT_CSD_BUS_WIDTH,
d3a8d95d 1204 ext_csd_bits[idx][0],
b23cf0bd 1205 card->ext_csd.generic_cmd6_time);
22113efd 1206 if (!err) {
4c4cb171 1207 mmc_set_bus_width(card->host, bus_width);
08ee80cc 1208
22113efd
AL
1209 /*
1210 * If controller can't handle bus width test,
08ee80cc
PR
1211 * compare ext_csd previously read in 1 bit mode
1212 * against ext_csd at new bus width
22113efd
AL
1213 */
1214 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
08ee80cc 1215 err = mmc_compare_ext_csds(card,
08ee80cc
PR
1216 bus_width);
1217 else
1218 err = mmc_bus_test(card, bus_width);
22113efd
AL
1219 if (!err)
1220 break;
1221 }
b30f8af3
JL
1222 }
1223
22113efd 1224 if (!err && ddr) {
60443712 1225 err = mmc_select_powerclass(card, ext_csd_bits[idx][1]);
b87d8dbf 1226 if (err)
87f761b6
V
1227 pr_warning("%s: power class selection to "
1228 "bus width %d ddr %d failed\n",
1229 mmc_hostname(card->host),
1230 1 << bus_width, ddr);
b87d8dbf 1231
22113efd 1232 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
d3a8d95d
AW
1233 EXT_CSD_BUS_WIDTH,
1234 ext_csd_bits[idx][1],
b23cf0bd 1235 card->ext_csd.generic_cmd6_time);
22113efd 1236 }
ef0b27d4 1237 if (err) {
a3c76eb9 1238 pr_warning("%s: switch to bus width %d ddr %d "
22113efd
AL
1239 "failed\n", mmc_hostname(card->host),
1240 1 << bus_width, ddr);
1241 goto free_card;
1242 } else if (ddr) {
4c4cb171
PR
1243 /*
1244 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1245 * signaling.
1246 *
1247 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1248 *
1249 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1250 * in the JEDEC spec for DDR.
1251 *
1252 * Do not force change in vccq since we are obviously
1253 * working and no change to vccq is needed.
1254 *
1255 * WARNING: eMMC rules are NOT the same as SD DDR
1256 */
913047e9 1257 if (ddr == MMC_1_2V_DDR_MODE) {
567c8903
JR
1258 err = __mmc_set_signal_voltage(host,
1259 MMC_SIGNAL_VOLTAGE_120);
4c4cb171
PR
1260 if (err)
1261 goto err;
1262 }
22113efd 1263 mmc_card_set_ddr_mode(card);
4c4cb171
PR
1264 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1265 mmc_set_bus_width(card->host, bus_width);
ef0b27d4 1266 }
89a73cf5
PO
1267 }
1268
52d0974e
SJ
1269 /*
1270 * Enable HPI feature (if supported)
1271 */
1272 if (card->ext_csd.hpi) {
1273 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1274 EXT_CSD_HPI_MGMT, 1,
1275 card->ext_csd.generic_cmd6_time);
1276 if (err && err != -EBADMSG)
1277 goto free_card;
1278 if (err) {
1279 pr_warning("%s: Enabling HPI failed\n",
1280 mmc_hostname(card->host));
1281 err = 0;
1282 } else
1283 card->ext_csd.hpi_en = 1;
1284 }
1285
881d1c25
SJ
1286 /*
1287 * If cache size is higher than 0, this indicates
1288 * the existence of cache and it can be turned on.
1289 */
1290 if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1291 card->ext_csd.cache_size > 0) {
1292 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
8bc0678b
SJ
1293 EXT_CSD_CACHE_CTRL, 1,
1294 card->ext_csd.generic_cmd6_time);
881d1c25
SJ
1295 if (err && err != -EBADMSG)
1296 goto free_card;
1297
1298 /*
1299 * Only if no error, cache is turned on successfully.
1300 */
8bc0678b
SJ
1301 if (err) {
1302 pr_warning("%s: Cache is supported, "
1303 "but failed to turn on (%d)\n",
1304 mmc_hostname(card->host), err);
1305 card->ext_csd.cache_ctrl = 0;
1306 err = 0;
1307 } else {
1308 card->ext_csd.cache_ctrl = 1;
1309 }
881d1c25
SJ
1310 }
1311
abd9ac14
SJ
1312 /*
1313 * The mandatory minimum values are defined for packed command.
1314 * read: 5, write: 3
1315 */
1316 if (card->ext_csd.max_packed_writes >= 3 &&
1317 card->ext_csd.max_packed_reads >= 5 &&
1318 host->caps2 & MMC_CAP2_PACKED_CMD) {
1319 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1320 EXT_CSD_EXP_EVENTS_CTRL,
1321 EXT_CSD_PACKED_EVENT_EN,
1322 card->ext_csd.generic_cmd6_time);
1323 if (err && err != -EBADMSG)
1324 goto free_card;
1325 if (err) {
1326 pr_warn("%s: Enabling packed event failed\n",
1327 mmc_hostname(card->host));
1328 card->ext_csd.packed_event_en = 0;
1329 err = 0;
1330 } else {
1331 card->ext_csd.packed_event_en = 1;
1332 }
1333 }
1334
6abaa0c9
PO
1335 if (!oldcard)
1336 host->card = card;
1337
08ee80cc 1338 mmc_free_ext_csd(ext_csd);
17b0429d 1339 return 0;
6abaa0c9
PO
1340
1341free_card:
1342 if (!oldcard)
1343 mmc_remove_card(card);
1344err:
08ee80cc 1345 mmc_free_ext_csd(ext_csd);
6abaa0c9 1346
adf66a0d 1347 return err;
6abaa0c9
PO
1348}
1349
07a68216
UH
1350static int mmc_can_sleep(struct mmc_card *card)
1351{
1352 return (card && card->ext_csd.rev >= 3);
1353}
1354
1355static int mmc_sleep(struct mmc_host *host)
1356{
1357 struct mmc_command cmd = {0};
1358 struct mmc_card *card = host->card;
1359 int err;
1360
1361 if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
1362 return 0;
1363
1364 err = mmc_deselect_cards(host);
1365 if (err)
1366 return err;
1367
1368 cmd.opcode = MMC_SLEEP_AWAKE;
1369 cmd.arg = card->rca << 16;
1370 cmd.arg |= 1 << 15;
1371
1372 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1373 err = mmc_wait_for_cmd(host, &cmd, 0);
1374 if (err)
1375 return err;
1376
1377 /*
1378 * If the host does not wait while the card signals busy, then we will
1379 * will have to wait the sleep/awake timeout. Note, we cannot use the
1380 * SEND_STATUS command to poll the status because that command (and most
1381 * others) is invalid while the card sleeps.
1382 */
1383 if (!(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1384 mmc_delay(DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000));
1385
1386 return err;
1387}
1388
e6c08586
UH
1389static int mmc_can_poweroff_notify(const struct mmc_card *card)
1390{
1391 return card &&
1392 mmc_card_mmc(card) &&
1393 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1394}
1395
1396static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1397{
1398 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1399 int err;
1400
1401 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1402 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1403 timeout = card->ext_csd.power_off_longtime;
1404
1405 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1406 EXT_CSD_POWER_OFF_NOTIFICATION,
1407 notify_type, timeout);
1408 if (err)
1409 pr_err("%s: Power Off Notification timed out, %u\n",
1410 mmc_hostname(card->host), timeout);
1411
1412 /* Disable the power off notification after the switch operation. */
1413 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1414
1415 return err;
1416}
1417
6abaa0c9
PO
1418/*
1419 * Host is being removed. Free up the current card.
1420 */
1421static void mmc_remove(struct mmc_host *host)
1422{
1423 BUG_ON(!host);
1424 BUG_ON(!host->card);
1425
1426 mmc_remove_card(host->card);
1427 host->card = NULL;
1428}
1429
d3049504
AH
1430/*
1431 * Card detection - card is alive.
1432 */
1433static int mmc_alive(struct mmc_host *host)
1434{
1435 return mmc_send_status(host->card, NULL);
1436}
1437
6abaa0c9
PO
1438/*
1439 * Card detection callback from host.
1440 */
1441static void mmc_detect(struct mmc_host *host)
1442{
1443 int err;
1444
1445 BUG_ON(!host);
1446 BUG_ON(!host->card);
1447
e94cfef6 1448 mmc_get_card(host->card);
6abaa0c9
PO
1449
1450 /*
1451 * Just check if our card has been removed.
1452 */
d3049504 1453 err = _mmc_detect_card_removed(host);
6abaa0c9 1454
e94cfef6 1455 mmc_put_card(host->card);
6abaa0c9 1456
17b0429d 1457 if (err) {
4101c16a 1458 mmc_remove(host);
6abaa0c9
PO
1459
1460 mmc_claim_host(host);
1461 mmc_detach_bus(host);
7f7e4129 1462 mmc_power_off(host);
6abaa0c9
PO
1463 mmc_release_host(host);
1464 }
1465}
1466
03d071fc 1467static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
6abaa0c9 1468{
c3805467 1469 int err = 0;
03d071fc
UH
1470 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1471 EXT_CSD_POWER_OFF_LONG;
c3805467 1472
6abaa0c9
PO
1473 BUG_ON(!host);
1474 BUG_ON(!host->card);
7ea239d9 1475
6abaa0c9 1476 mmc_claim_host(host);
881d926d 1477
39b9431b
UH
1478 if (mmc_card_doing_bkops(host->card)) {
1479 err = mmc_stop_bkops(host->card);
1480 if (err)
1481 goto out;
1482 }
1483
881d926d
ME
1484 err = mmc_cache_ctrl(host, 0);
1485 if (err)
1486 goto out;
1487
e6c08586 1488 if (mmc_can_poweroff_notify(host->card))
03d071fc 1489 err = mmc_poweroff_notify(host->card, notify_type);
07a68216
UH
1490 else if (mmc_can_sleep(host->card))
1491 err = mmc_sleep(host);
e6c08586 1492 else if (!mmc_host_is_spi(host))
85e727ed 1493 err = mmc_deselect_cards(host);
3e73c36b 1494 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
95cdfb72 1495
74590263
UH
1496 if (!err)
1497 mmc_power_off(host);
881d926d
ME
1498out:
1499 mmc_release_host(host);
c3805467 1500 return err;
6abaa0c9 1501}
7ea239d9 1502
03d071fc
UH
1503/*
1504 * Suspend callback from host.
1505 */
1506static int mmc_suspend(struct mmc_host *host)
1507{
1508 return _mmc_suspend(host, true);
1509}
1510
486fdbbc
UH
1511/*
1512 * Shutdown callback
1513 */
1514static int mmc_shutdown(struct mmc_host *host)
1515{
1516 return _mmc_suspend(host, false);
1517}
1518
6abaa0c9
PO
1519/*
1520 * Resume callback from host.
1521 *
1522 * This function tries to determine if the same card is still present
1523 * and, if so, restore all state to it.
1524 */
95cdfb72 1525static int mmc_resume(struct mmc_host *host)
6abaa0c9
PO
1526{
1527 int err;
1528
1529 BUG_ON(!host);
1530 BUG_ON(!host->card);
1531
1532 mmc_claim_host(host);
74590263
UH
1533 mmc_power_up(host);
1534 mmc_select_voltage(host, host->ocr);
e6c08586 1535 err = mmc_init_card(host, host->ocr, host->card);
2986d0bf
PO
1536 mmc_release_host(host);
1537
95cdfb72 1538 return err;
6abaa0c9
PO
1539}
1540
c4d770d7
UH
1541
1542/*
1543 * Callback for runtime_suspend.
1544 */
1545static int mmc_runtime_suspend(struct mmc_host *host)
1546{
1547 int err;
1548
1549 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1550 return 0;
1551
1552 mmc_claim_host(host);
1553
1554 err = mmc_suspend(host);
1555 if (err) {
1556 pr_err("%s: error %d doing aggessive suspend\n",
1557 mmc_hostname(host), err);
1558 goto out;
1559 }
1560 mmc_power_off(host);
1561
1562out:
1563 mmc_release_host(host);
1564 return err;
1565}
1566
1567/*
1568 * Callback for runtime_resume.
1569 */
1570static int mmc_runtime_resume(struct mmc_host *host)
1571{
1572 int err;
1573
1574 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1575 return 0;
1576
1577 mmc_claim_host(host);
1578
1579 mmc_power_up(host);
1580 err = mmc_resume(host);
1581 if (err)
1582 pr_err("%s: error %d doing aggessive resume\n",
1583 mmc_hostname(host), err);
1584
1585 mmc_release_host(host);
1586 return 0;
1587}
1588
12ae637f 1589static int mmc_power_restore(struct mmc_host *host)
eae1aeee 1590{
12ae637f
OBC
1591 int ret;
1592
3e73c36b 1593 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
eae1aeee 1594 mmc_claim_host(host);
12ae637f 1595 ret = mmc_init_card(host, host->ocr, host->card);
eae1aeee 1596 mmc_release_host(host);
12ae637f
OBC
1597
1598 return ret;
eae1aeee
AH
1599}
1600
6abaa0c9 1601static const struct mmc_bus_ops mmc_ops = {
9feae246
AH
1602 .remove = mmc_remove,
1603 .detect = mmc_detect,
1604 .suspend = NULL,
1605 .resume = NULL,
eae1aeee 1606 .power_restore = mmc_power_restore,
d3049504 1607 .alive = mmc_alive,
486fdbbc 1608 .shutdown = mmc_shutdown,
9feae246
AH
1609};
1610
1611static const struct mmc_bus_ops mmc_ops_unsafe = {
6abaa0c9
PO
1612 .remove = mmc_remove,
1613 .detect = mmc_detect,
1614 .suspend = mmc_suspend,
1615 .resume = mmc_resume,
c4d770d7
UH
1616 .runtime_suspend = mmc_runtime_suspend,
1617 .runtime_resume = mmc_runtime_resume,
eae1aeee 1618 .power_restore = mmc_power_restore,
d3049504 1619 .alive = mmc_alive,
486fdbbc 1620 .shutdown = mmc_shutdown,
6abaa0c9
PO
1621};
1622
9feae246
AH
1623static void mmc_attach_bus_ops(struct mmc_host *host)
1624{
1625 const struct mmc_bus_ops *bus_ops;
1626
71d7d3d1 1627 if (!mmc_card_is_removable(host))
9feae246
AH
1628 bus_ops = &mmc_ops_unsafe;
1629 else
1630 bus_ops = &mmc_ops;
1631 mmc_attach_bus(host, bus_ops);
1632}
1633
6abaa0c9
PO
1634/*
1635 * Starting point for MMC card init.
1636 */
807e8e40 1637int mmc_attach_mmc(struct mmc_host *host)
6abaa0c9
PO
1638{
1639 int err;
807e8e40 1640 u32 ocr;
6abaa0c9
PO
1641
1642 BUG_ON(!host);
d84075c8 1643 WARN_ON(!host->claimed);
6abaa0c9 1644
44669034
SNX
1645 /* Set correct bus mode for MMC before attempting attach */
1646 if (!mmc_host_is_spi(host))
1647 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1648
807e8e40
AR
1649 err = mmc_send_op_cond(host, 0, &ocr);
1650 if (err)
1651 return err;
1652
9feae246 1653 mmc_attach_bus_ops(host);
8f230f45
TI
1654 if (host->ocr_avail_mmc)
1655 host->ocr_avail = host->ocr_avail_mmc;
6abaa0c9 1656
af517150
DB
1657 /*
1658 * We need to get OCR a different way for SPI.
1659 */
1660 if (mmc_host_is_spi(host)) {
1661 err = mmc_spi_read_ocr(host, 1, &ocr);
1662 if (err)
1663 goto err;
1664 }
1665
6abaa0c9
PO
1666 /*
1667 * Sanity check the voltages that the card claims to
1668 * support.
1669 */
1670 if (ocr & 0x7F) {
a3c76eb9 1671 pr_warning("%s: card claims to support voltages "
6abaa0c9
PO
1672 "below the defined range. These will be ignored.\n",
1673 mmc_hostname(host));
1674 ocr &= ~0x7F;
1675 }
1676
1677 host->ocr = mmc_select_voltage(host, ocr);
1678
1679 /*
1680 * Can we support the voltage of the card?
1681 */
109b5bed
PO
1682 if (!host->ocr) {
1683 err = -EINVAL;
6abaa0c9 1684 goto err;
109b5bed 1685 }
6abaa0c9
PO
1686
1687 /*
1688 * Detect and init the card.
1689 */
8c75deae 1690 err = mmc_init_card(host, host->ocr, NULL);
17b0429d 1691 if (err)
6abaa0c9
PO
1692 goto err;
1693
1694 mmc_release_host(host);
4101c16a 1695 err = mmc_add_card(host->card);
807e8e40 1696 mmc_claim_host(host);
7ea239d9 1697 if (err)
2986d0bf 1698 goto remove_card;
7ea239d9
PO
1699
1700 return 0;
1701
2986d0bf 1702remove_card:
807e8e40 1703 mmc_release_host(host);
6abaa0c9 1704 mmc_remove_card(host->card);
2986d0bf 1705 mmc_claim_host(host);
807e8e40 1706 host->card = NULL;
7ea239d9
PO
1707err:
1708 mmc_detach_bus(host);
7ea239d9 1709
a3c76eb9 1710 pr_err("%s: error %d whilst initialising MMC card\n",
109b5bed
PO
1711 mmc_hostname(host), err);
1712
adf66a0d 1713 return err;
7ea239d9 1714}