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