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