Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / mmc / core / mmc.c
blob2b9ed1401dc439bf1dd29e4aa751f74e0175bc93
1 /*
2 * linux/drivers/mmc/core/mmc.c
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.
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.
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/stat.h>
17 #include <linux/mmc/host.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/mmc.h>
21 #include "core.h"
22 #include "bus.h"
23 #include "mmc_ops.h"
24 #include "sd_ops.h"
26 static const unsigned int tran_exp[] = {
27 10000, 100000, 1000000, 10000000,
28 0, 0, 0, 0
31 static const unsigned char tran_mant[] = {
32 0, 10, 12, 13, 15, 20, 25, 30,
33 35, 40, 45, 50, 55, 60, 70, 80,
36 static const unsigned int tacc_exp[] = {
37 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
40 static const unsigned int tacc_mant[] = {
41 0, 10, 12, 13, 15, 20, 25, 30,
42 35, 40, 45, 50, 55, 60, 70, 80,
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; \
53 __res = resp[__off] >> __shft; \
54 if (__size + __shft > 32) \
55 __res |= resp[__off-1] << ((32 - __shft) % 32); \
56 __res & __mask; \
60 * Given the decoded CSD structure, decode the raw CID to our CID structure.
62 static int mmc_decode_cid(struct mmc_card *card)
64 u32 *resp = card->raw_cid;
67 * The selection of the format here is based upon published
68 * specs from sandisk and from what people have reported.
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;
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;
104 default:
105 pr_err("%s: card has unknown MMCA version %d\n",
106 mmc_hostname(card->host), card->csd.mmca_vsn);
107 return -EINVAL;
110 return 0;
113 static void mmc_set_erase_size(struct mmc_card *card)
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;
120 mmc_init_erase(card);
124 * Given a 128-bit response, decode to our card CSD structure.
126 static int mmc_decode_csd(struct mmc_card *card)
128 struct mmc_csd *csd = &card->csd;
129 unsigned int e, m, a, b;
130 u32 *resp = card->raw_csd;
133 * We only understand CSD structure v1.1 and v1.2.
134 * v1.2 has extra information in bits 15, 11 and 10.
135 * We also support eMMC v4.4 & v4.41.
137 csd->structure = UNSTUFF_BITS(resp, 126, 2);
138 if (csd->structure == 0) {
139 pr_err("%s: unrecognised CSD structure version %d\n",
140 mmc_hostname(card->host), csd->structure);
141 return -EINVAL;
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;
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);
155 e = UNSTUFF_BITS(resp, 47, 3);
156 m = UNSTUFF_BITS(resp, 62, 12);
157 csd->capacity = (1 + m) << (e + 2);
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);
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;
174 return 0;
178 * Read extended CSD.
180 static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
182 int err;
183 u8 *ext_csd;
185 BUG_ON(!card);
186 BUG_ON(!new_ext_csd);
188 *new_ext_csd = NULL;
190 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
191 return 0;
194 * As the ext_csd is so large and mostly unused, we don't store the
195 * raw block in mmc_card.
197 ext_csd = kmalloc(512, GFP_KERNEL);
198 if (!ext_csd) {
199 pr_err("%s: could not allocate a buffer to "
200 "receive the ext_csd.\n", mmc_hostname(card->host));
201 return -ENOMEM;
204 err = mmc_send_ext_csd(card, ext_csd);
205 if (err) {
206 kfree(ext_csd);
207 *new_ext_csd = NULL;
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))
214 return err;
217 * High capacity cards should have this "magic" size
218 * stored in their CSD.
220 if (card->csd.capacity == (4096 * 512)) {
221 pr_err("%s: unable to read EXT_CSD "
222 "on a possible high capacity card. "
223 "Card will be ignored.\n",
224 mmc_hostname(card->host));
225 } else {
226 pr_warning("%s: unable to read "
227 "EXT_CSD, performance might "
228 "suffer.\n",
229 mmc_hostname(card->host));
230 err = 0;
232 } else
233 *new_ext_csd = ext_csd;
235 return err;
239 * Decode extended CSD.
241 static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
243 int err = 0, idx;
244 unsigned int part_size;
245 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
247 BUG_ON(!card);
249 if (!ext_csd)
250 return 0;
252 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
253 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
254 if (card->csd.structure == 3) {
255 if (card->ext_csd.raw_ext_csd_structure > 2) {
256 pr_err("%s: unrecognised EXT_CSD structure "
257 "version %d\n", mmc_hostname(card->host),
258 card->ext_csd.raw_ext_csd_structure);
259 err = -EINVAL;
260 goto out;
264 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
265 if (card->ext_csd.rev > 6) {
266 pr_err("%s: unrecognised EXT_CSD revision %d\n",
267 mmc_hostname(card->host), card->ext_csd.rev);
268 err = -EINVAL;
269 goto out;
272 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
273 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
274 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
275 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
276 if (card->ext_csd.rev >= 2) {
277 card->ext_csd.sectors =
278 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
279 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
280 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
281 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
283 /* Cards with density > 2GiB are sector addressed */
284 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
285 mmc_card_set_blockaddr(card);
287 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
288 switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
289 case EXT_CSD_CARD_TYPE_SDR_ALL:
290 case EXT_CSD_CARD_TYPE_SDR_ALL_DDR_1_8V:
291 case EXT_CSD_CARD_TYPE_SDR_ALL_DDR_1_2V:
292 case EXT_CSD_CARD_TYPE_SDR_ALL_DDR_52:
293 card->ext_csd.hs_max_dtr = 200000000;
294 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_SDR_200;
295 break;
296 case EXT_CSD_CARD_TYPE_SDR_1_2V_ALL:
297 case EXT_CSD_CARD_TYPE_SDR_1_2V_DDR_1_8V:
298 case EXT_CSD_CARD_TYPE_SDR_1_2V_DDR_1_2V:
299 case EXT_CSD_CARD_TYPE_SDR_1_2V_DDR_52:
300 card->ext_csd.hs_max_dtr = 200000000;
301 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_SDR_1_2V;
302 break;
303 case EXT_CSD_CARD_TYPE_SDR_1_8V_ALL:
304 case EXT_CSD_CARD_TYPE_SDR_1_8V_DDR_1_8V:
305 case EXT_CSD_CARD_TYPE_SDR_1_8V_DDR_1_2V:
306 case EXT_CSD_CARD_TYPE_SDR_1_8V_DDR_52:
307 card->ext_csd.hs_max_dtr = 200000000;
308 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_SDR_1_8V;
309 break;
310 case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 |
311 EXT_CSD_CARD_TYPE_26:
312 card->ext_csd.hs_max_dtr = 52000000;
313 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52;
314 break;
315 case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 |
316 EXT_CSD_CARD_TYPE_26:
317 card->ext_csd.hs_max_dtr = 52000000;
318 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V;
319 break;
320 case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 |
321 EXT_CSD_CARD_TYPE_26:
322 card->ext_csd.hs_max_dtr = 52000000;
323 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V;
324 break;
325 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
326 card->ext_csd.hs_max_dtr = 52000000;
327 break;
328 case EXT_CSD_CARD_TYPE_26:
329 card->ext_csd.hs_max_dtr = 26000000;
330 break;
331 default:
332 /* MMC v4 spec says this cannot happen */
333 pr_warning("%s: card is mmc v4 but doesn't "
334 "support any high-speed modes.\n",
335 mmc_hostname(card->host));
338 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
339 card->ext_csd.raw_erase_timeout_mult =
340 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
341 card->ext_csd.raw_hc_erase_grp_size =
342 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
343 if (card->ext_csd.rev >= 3) {
344 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
345 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
347 /* EXT_CSD value is in units of 10ms, but we store in ms */
348 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
350 /* Sleep / awake timeout in 100ns units */
351 if (sa_shift > 0 && sa_shift <= 0x17)
352 card->ext_csd.sa_timeout =
353 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
354 card->ext_csd.erase_group_def =
355 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
356 card->ext_csd.hc_erase_timeout = 300 *
357 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
358 card->ext_csd.hc_erase_size =
359 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
361 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
364 * There are two boot regions of equal size, defined in
365 * multiples of 128K.
367 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
368 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
369 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
370 mmc_part_add(card, part_size,
371 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
372 "boot%d", idx, true,
373 MMC_BLK_DATA_AREA_BOOT);
378 card->ext_csd.raw_hc_erase_gap_size =
379 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
380 card->ext_csd.raw_sec_trim_mult =
381 ext_csd[EXT_CSD_SEC_TRIM_MULT];
382 card->ext_csd.raw_sec_erase_mult =
383 ext_csd[EXT_CSD_SEC_ERASE_MULT];
384 card->ext_csd.raw_sec_feature_support =
385 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
386 card->ext_csd.raw_trim_mult =
387 ext_csd[EXT_CSD_TRIM_MULT];
388 if (card->ext_csd.rev >= 4) {
390 * Enhanced area feature support -- check whether the eMMC
391 * card has the Enhanced area enabled. If so, export enhanced
392 * area offset and size to user by adding sysfs interface.
394 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
395 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
396 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
397 hc_erase_grp_sz =
398 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
399 hc_wp_grp_sz =
400 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
402 card->ext_csd.enhanced_area_en = 1;
404 * calculate the enhanced data area offset, in bytes
406 card->ext_csd.enhanced_area_offset =
407 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
408 (ext_csd[137] << 8) + ext_csd[136];
409 if (mmc_card_blockaddr(card))
410 card->ext_csd.enhanced_area_offset <<= 9;
412 * calculate the enhanced data area size, in kilobytes
414 card->ext_csd.enhanced_area_size =
415 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
416 ext_csd[140];
417 card->ext_csd.enhanced_area_size *=
418 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
419 card->ext_csd.enhanced_area_size <<= 9;
420 } else {
422 * If the enhanced area is not enabled, disable these
423 * device attributes.
425 card->ext_csd.enhanced_area_offset = -EINVAL;
426 card->ext_csd.enhanced_area_size = -EINVAL;
430 * General purpose partition feature support --
431 * If ext_csd has the size of general purpose partitions,
432 * set size, part_cfg, partition name in mmc_part.
434 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
435 EXT_CSD_PART_SUPPORT_PART_EN) {
436 if (card->ext_csd.enhanced_area_en != 1) {
437 hc_erase_grp_sz =
438 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
439 hc_wp_grp_sz =
440 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
442 card->ext_csd.enhanced_area_en = 1;
445 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
446 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
447 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
448 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
449 continue;
450 part_size =
451 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
452 << 16) +
453 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
454 << 8) +
455 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
456 part_size *= (size_t)(hc_erase_grp_sz *
457 hc_wp_grp_sz);
458 mmc_part_add(card, part_size << 19,
459 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
460 "gp%d", idx, false,
461 MMC_BLK_DATA_AREA_GP);
464 card->ext_csd.sec_trim_mult =
465 ext_csd[EXT_CSD_SEC_TRIM_MULT];
466 card->ext_csd.sec_erase_mult =
467 ext_csd[EXT_CSD_SEC_ERASE_MULT];
468 card->ext_csd.sec_feature_support =
469 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
470 card->ext_csd.trim_timeout = 300 *
471 ext_csd[EXT_CSD_TRIM_MULT];
474 * Note that the call to mmc_part_add above defaults to read
475 * only. If this default assumption is changed, the call must
476 * take into account the value of boot_locked below.
478 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
479 card->ext_csd.boot_ro_lockable = true;
482 if (card->ext_csd.rev >= 5) {
483 /* check whether the eMMC card supports HPI */
484 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
485 card->ext_csd.hpi = 1;
486 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
487 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
488 else
489 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
491 * Indicate the maximum timeout to close
492 * a command interrupted by HPI
494 card->ext_csd.out_of_int_time =
495 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
498 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
499 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
502 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
503 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
504 card->erased_byte = 0xFF;
505 else
506 card->erased_byte = 0x0;
508 /* eMMC v4.5 or later */
509 if (card->ext_csd.rev >= 6) {
510 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
512 card->ext_csd.generic_cmd6_time = 10 *
513 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
514 card->ext_csd.power_off_longtime = 10 *
515 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
517 card->ext_csd.cache_size =
518 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
519 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
520 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
521 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
524 out:
525 return err;
528 static inline void mmc_free_ext_csd(u8 *ext_csd)
530 kfree(ext_csd);
534 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
536 u8 *bw_ext_csd;
537 int err;
539 if (bus_width == MMC_BUS_WIDTH_1)
540 return 0;
542 err = mmc_get_ext_csd(card, &bw_ext_csd);
544 if (err || bw_ext_csd == NULL) {
545 if (bus_width != MMC_BUS_WIDTH_1)
546 err = -EINVAL;
547 goto out;
550 if (bus_width == MMC_BUS_WIDTH_1)
551 goto out;
553 /* only compare read only fields */
554 err = !((card->ext_csd.raw_partition_support ==
555 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
556 (card->ext_csd.raw_erased_mem_count ==
557 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
558 (card->ext_csd.rev ==
559 bw_ext_csd[EXT_CSD_REV]) &&
560 (card->ext_csd.raw_ext_csd_structure ==
561 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
562 (card->ext_csd.raw_card_type ==
563 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
564 (card->ext_csd.raw_s_a_timeout ==
565 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
566 (card->ext_csd.raw_hc_erase_gap_size ==
567 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
568 (card->ext_csd.raw_erase_timeout_mult ==
569 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
570 (card->ext_csd.raw_hc_erase_grp_size ==
571 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
572 (card->ext_csd.raw_sec_trim_mult ==
573 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
574 (card->ext_csd.raw_sec_erase_mult ==
575 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
576 (card->ext_csd.raw_sec_feature_support ==
577 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
578 (card->ext_csd.raw_trim_mult ==
579 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
580 (card->ext_csd.raw_sectors[0] ==
581 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
582 (card->ext_csd.raw_sectors[1] ==
583 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
584 (card->ext_csd.raw_sectors[2] ==
585 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
586 (card->ext_csd.raw_sectors[3] ==
587 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
588 if (err)
589 err = -EINVAL;
591 out:
592 mmc_free_ext_csd(bw_ext_csd);
593 return err;
596 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
597 card->raw_cid[2], card->raw_cid[3]);
598 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
599 card->raw_csd[2], card->raw_csd[3]);
600 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
601 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
602 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
603 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
604 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
605 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
606 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
607 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
608 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
609 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
610 card->ext_csd.enhanced_area_offset);
611 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
613 static struct attribute *mmc_std_attrs[] = {
614 &dev_attr_cid.attr,
615 &dev_attr_csd.attr,
616 &dev_attr_date.attr,
617 &dev_attr_erase_size.attr,
618 &dev_attr_preferred_erase_size.attr,
619 &dev_attr_fwrev.attr,
620 &dev_attr_hwrev.attr,
621 &dev_attr_manfid.attr,
622 &dev_attr_name.attr,
623 &dev_attr_oemid.attr,
624 &dev_attr_serial.attr,
625 &dev_attr_enhanced_area_offset.attr,
626 &dev_attr_enhanced_area_size.attr,
627 NULL,
630 static struct attribute_group mmc_std_attr_group = {
631 .attrs = mmc_std_attrs,
634 static const struct attribute_group *mmc_attr_groups[] = {
635 &mmc_std_attr_group,
636 NULL,
639 static struct device_type mmc_type = {
640 .groups = mmc_attr_groups,
644 * Select the PowerClass for the current bus width
645 * If power class is defined for 4/8 bit bus in the
646 * extended CSD register, select it by executing the
647 * mmc_switch command.
649 static int mmc_select_powerclass(struct mmc_card *card,
650 unsigned int bus_width, u8 *ext_csd)
652 int err = 0;
653 unsigned int pwrclass_val;
654 unsigned int index = 0;
655 struct mmc_host *host;
657 BUG_ON(!card);
659 host = card->host;
660 BUG_ON(!host);
662 if (ext_csd == NULL)
663 return 0;
665 /* Power class selection is supported for versions >= 4.0 */
666 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
667 return 0;
669 /* Power class values are defined only for 4/8 bit bus */
670 if (bus_width == EXT_CSD_BUS_WIDTH_1)
671 return 0;
673 switch (1 << host->ios.vdd) {
674 case MMC_VDD_165_195:
675 if (host->ios.clock <= 26000000)
676 index = EXT_CSD_PWR_CL_26_195;
677 else if (host->ios.clock <= 52000000)
678 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
679 EXT_CSD_PWR_CL_52_195 :
680 EXT_CSD_PWR_CL_DDR_52_195;
681 else if (host->ios.clock <= 200000000)
682 index = EXT_CSD_PWR_CL_200_195;
683 break;
684 case MMC_VDD_32_33:
685 case MMC_VDD_33_34:
686 case MMC_VDD_34_35:
687 case MMC_VDD_35_36:
688 if (host->ios.clock <= 26000000)
689 index = EXT_CSD_PWR_CL_26_360;
690 else if (host->ios.clock <= 52000000)
691 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
692 EXT_CSD_PWR_CL_52_360 :
693 EXT_CSD_PWR_CL_DDR_52_360;
694 else if (host->ios.clock <= 200000000)
695 index = EXT_CSD_PWR_CL_200_360;
696 break;
697 default:
698 pr_warning("%s: Voltage range not supported "
699 "for power class.\n", mmc_hostname(host));
700 return -EINVAL;
703 pwrclass_val = ext_csd[index];
705 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
706 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
707 EXT_CSD_PWR_CL_8BIT_SHIFT;
708 else
709 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
710 EXT_CSD_PWR_CL_4BIT_SHIFT;
712 /* If the power class is different from the default value */
713 if (pwrclass_val > 0) {
714 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
715 EXT_CSD_POWER_CLASS,
716 pwrclass_val,
717 card->ext_csd.generic_cmd6_time);
720 return err;
724 * Selects the desired buswidth and switch to the HS200 mode
725 * if bus width set without error
727 static int mmc_select_hs200(struct mmc_card *card)
729 int idx, err = 0;
730 struct mmc_host *host;
731 static unsigned ext_csd_bits[] = {
732 EXT_CSD_BUS_WIDTH_4,
733 EXT_CSD_BUS_WIDTH_8,
735 static unsigned bus_widths[] = {
736 MMC_BUS_WIDTH_4,
737 MMC_BUS_WIDTH_8,
740 BUG_ON(!card);
742 host = card->host;
744 if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V &&
745 host->caps2 & MMC_CAP2_HS200_1_2V_SDR)
746 if (mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120, 0))
747 err = mmc_set_signal_voltage(host,
748 MMC_SIGNAL_VOLTAGE_180, 0);
750 /* If fails try again during next card power cycle */
751 if (err)
752 goto err;
754 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0;
757 * Unlike SD, MMC cards dont have a configuration register to notify
758 * supported bus width. So bus test command should be run to identify
759 * the supported bus width or compare the ext csd values of current
760 * bus width and ext csd values of 1 bit mode read earlier.
762 for (; idx >= 0; idx--) {
765 * Host is capable of 8bit transfer, then switch
766 * the device to work in 8bit transfer mode. If the
767 * mmc switch command returns error then switch to
768 * 4bit transfer mode. On success set the corresponding
769 * bus width on the host.
771 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
772 EXT_CSD_BUS_WIDTH,
773 ext_csd_bits[idx],
774 card->ext_csd.generic_cmd6_time);
775 if (err)
776 continue;
778 mmc_set_bus_width(card->host, bus_widths[idx]);
780 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
781 err = mmc_compare_ext_csds(card, bus_widths[idx]);
782 else
783 err = mmc_bus_test(card, bus_widths[idx]);
784 if (!err)
785 break;
788 /* switch to HS200 mode if bus width set successfully */
789 if (!err)
790 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
791 EXT_CSD_HS_TIMING, 2, 0);
792 err:
793 return err;
797 * Handle the detection and initialisation of a card.
799 * In the case of a resume, "oldcard" will contain the card
800 * we're trying to reinitialise.
802 static int mmc_init_card(struct mmc_host *host, u32 ocr,
803 struct mmc_card *oldcard)
805 struct mmc_card *card;
806 int err, ddr = 0;
807 u32 cid[4];
808 unsigned int max_dtr;
809 u32 rocr;
810 u8 *ext_csd = NULL;
812 BUG_ON(!host);
813 WARN_ON(!host->claimed);
815 /* Set correct bus mode for MMC before attempting init */
816 if (!mmc_host_is_spi(host))
817 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
819 /* Initialization should be done at 3.3 V I/O voltage. */
820 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
823 * Since we're changing the OCR value, we seem to
824 * need to tell some cards to go back to the idle
825 * state. We wait 1ms to give cards time to
826 * respond.
827 * mmc_go_idle is needed for eMMC that are asleep
829 mmc_go_idle(host);
831 /* The extra bit indicates that we support high capacity */
832 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
833 if (err)
834 goto err;
837 * For SPI, enable CRC as appropriate.
839 if (mmc_host_is_spi(host)) {
840 err = mmc_spi_set_crc(host, use_spi_crc);
841 if (err)
842 goto err;
846 * Fetch CID from card.
848 if (mmc_host_is_spi(host))
849 err = mmc_send_cid(host, cid);
850 else
851 err = mmc_all_send_cid(host, cid);
852 if (err)
853 goto err;
855 if (oldcard) {
856 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
857 err = -ENOENT;
858 goto err;
861 card = oldcard;
862 } else {
864 * Allocate card structure.
866 card = mmc_alloc_card(host, &mmc_type);
867 if (IS_ERR(card)) {
868 err = PTR_ERR(card);
869 goto err;
872 card->type = MMC_TYPE_MMC;
873 card->rca = 1;
874 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
878 * For native busses: set card RCA and quit open drain mode.
880 if (!mmc_host_is_spi(host)) {
881 err = mmc_set_relative_addr(card);
882 if (err)
883 goto free_card;
885 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
888 if (!oldcard) {
890 * Fetch CSD from card.
892 err = mmc_send_csd(card, card->raw_csd);
893 if (err)
894 goto free_card;
896 err = mmc_decode_csd(card);
897 if (err)
898 goto free_card;
899 err = mmc_decode_cid(card);
900 if (err)
901 goto free_card;
905 * Select card, as all following commands rely on that.
907 if (!mmc_host_is_spi(host)) {
908 err = mmc_select_card(card);
909 if (err)
910 goto free_card;
913 if (!oldcard) {
915 * Fetch and process extended CSD.
918 err = mmc_get_ext_csd(card, &ext_csd);
919 if (err)
920 goto free_card;
921 err = mmc_read_ext_csd(card, ext_csd);
922 if (err)
923 goto free_card;
925 /* If doing byte addressing, check if required to do sector
926 * addressing. Handle the case of <2GB cards needing sector
927 * addressing. See section 8.1 JEDEC Standard JED84-A441;
928 * ocr register has bit 30 set for sector addressing.
930 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
931 mmc_card_set_blockaddr(card);
933 /* Erase size depends on CSD and Extended CSD */
934 mmc_set_erase_size(card);
938 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
939 * bit. This bit will be lost every time after a reset or power off.
941 if (card->ext_csd.enhanced_area_en) {
942 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
943 EXT_CSD_ERASE_GROUP_DEF, 1,
944 card->ext_csd.generic_cmd6_time);
946 if (err && err != -EBADMSG)
947 goto free_card;
949 if (err) {
950 err = 0;
952 * Just disable enhanced area off & sz
953 * will try to enable ERASE_GROUP_DEF
954 * during next time reinit
956 card->ext_csd.enhanced_area_offset = -EINVAL;
957 card->ext_csd.enhanced_area_size = -EINVAL;
958 } else {
959 card->ext_csd.erase_group_def = 1;
961 * enable ERASE_GRP_DEF successfully.
962 * This will affect the erase size, so
963 * here need to reset erase size
965 mmc_set_erase_size(card);
970 * Ensure eMMC user default partition is enabled
972 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
973 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
974 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
975 card->ext_csd.part_config,
976 card->ext_csd.part_time);
977 if (err && err != -EBADMSG)
978 goto free_card;
982 * If the host supports the power_off_notify capability then
983 * set the notification byte in the ext_csd register of device
985 if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) &&
986 (card->ext_csd.rev >= 6)) {
987 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
988 EXT_CSD_POWER_OFF_NOTIFICATION,
989 EXT_CSD_POWER_ON,
990 card->ext_csd.generic_cmd6_time);
991 if (err && err != -EBADMSG)
992 goto free_card;
995 * The err can be -EBADMSG or 0,
996 * so check for success and update the flag
998 if (!err)
999 card->poweroff_notify_state = MMC_POWERED_ON;
1003 * Activate high speed (if supported)
1005 if (card->ext_csd.hs_max_dtr != 0) {
1006 err = 0;
1007 if (card->ext_csd.hs_max_dtr > 52000000 &&
1008 host->caps2 & MMC_CAP2_HS200)
1009 err = mmc_select_hs200(card);
1010 else if (host->caps & MMC_CAP_MMC_HIGHSPEED)
1011 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1012 EXT_CSD_HS_TIMING, 1,
1013 card->ext_csd.generic_cmd6_time);
1015 if (err && err != -EBADMSG)
1016 goto free_card;
1018 if (err) {
1019 pr_warning("%s: switch to highspeed failed\n",
1020 mmc_hostname(card->host));
1021 err = 0;
1022 } else {
1023 if (card->ext_csd.hs_max_dtr > 52000000 &&
1024 host->caps2 & MMC_CAP2_HS200) {
1025 mmc_card_set_hs200(card);
1026 mmc_set_timing(card->host,
1027 MMC_TIMING_MMC_HS200);
1028 } else {
1029 mmc_card_set_highspeed(card);
1030 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1036 * Enable HPI feature (if supported)
1038 if (card->ext_csd.hpi) {
1039 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1040 EXT_CSD_HPI_MGMT, 1, 0);
1041 if (err && err != -EBADMSG)
1042 goto free_card;
1043 if (err) {
1044 pr_warning("%s: Enabling HPI failed\n",
1045 mmc_hostname(card->host));
1046 err = 0;
1047 } else
1048 card->ext_csd.hpi_en = 1;
1052 * Compute bus speed.
1054 max_dtr = (unsigned int)-1;
1056 if (mmc_card_highspeed(card) || mmc_card_hs200(card)) {
1057 if (max_dtr > card->ext_csd.hs_max_dtr)
1058 max_dtr = card->ext_csd.hs_max_dtr;
1059 } else if (max_dtr > card->csd.max_dtr) {
1060 max_dtr = card->csd.max_dtr;
1063 mmc_set_clock(host, max_dtr);
1066 * Indicate DDR mode (if supported).
1068 if (mmc_card_highspeed(card)) {
1069 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
1070 && ((host->caps & (MMC_CAP_1_8V_DDR |
1071 MMC_CAP_UHS_DDR50))
1072 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
1073 ddr = MMC_1_8V_DDR_MODE;
1074 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1075 && ((host->caps & (MMC_CAP_1_2V_DDR |
1076 MMC_CAP_UHS_DDR50))
1077 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
1078 ddr = MMC_1_2V_DDR_MODE;
1082 * Indicate HS200 SDR mode (if supported).
1084 if (mmc_card_hs200(card)) {
1085 u32 ext_csd_bits;
1086 u32 bus_width = card->host->ios.bus_width;
1089 * For devices supporting HS200 mode, the bus width has
1090 * to be set before executing the tuning function. If
1091 * set before tuning, then device will respond with CRC
1092 * errors for responses on CMD line. So for HS200 the
1093 * sequence will be
1094 * 1. set bus width 4bit / 8 bit (1 bit not supported)
1095 * 2. switch to HS200 mode
1096 * 3. set the clock to > 52Mhz <=200MHz and
1097 * 4. execute tuning for HS200
1099 if ((host->caps2 & MMC_CAP2_HS200) &&
1100 card->host->ops->execute_tuning)
1101 err = card->host->ops->execute_tuning(card->host,
1102 MMC_SEND_TUNING_BLOCK_HS200);
1103 if (err) {
1104 pr_warning("%s: tuning execution failed\n",
1105 mmc_hostname(card->host));
1106 goto err;
1109 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1110 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
1111 err = mmc_select_powerclass(card, ext_csd_bits, ext_csd);
1112 if (err) {
1113 pr_err("%s: power class selection to bus width %d failed\n",
1114 mmc_hostname(card->host), 1 << bus_width);
1115 goto err;
1120 * Activate wide bus and DDR (if supported).
1122 if (!mmc_card_hs200(card) &&
1123 (card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
1124 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
1125 static unsigned ext_csd_bits[][2] = {
1126 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
1127 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
1128 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
1130 static unsigned bus_widths[] = {
1131 MMC_BUS_WIDTH_8,
1132 MMC_BUS_WIDTH_4,
1133 MMC_BUS_WIDTH_1
1135 unsigned idx, bus_width = 0;
1137 if (host->caps & MMC_CAP_8_BIT_DATA)
1138 idx = 0;
1139 else
1140 idx = 1;
1141 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1142 bus_width = bus_widths[idx];
1143 if (bus_width == MMC_BUS_WIDTH_1)
1144 ddr = 0; /* no DDR for 1-bit width */
1145 err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
1146 ext_csd);
1147 if (err)
1148 pr_err("%s: power class selection to "
1149 "bus width %d failed\n",
1150 mmc_hostname(card->host),
1151 1 << bus_width);
1153 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1154 EXT_CSD_BUS_WIDTH,
1155 ext_csd_bits[idx][0],
1156 card->ext_csd.generic_cmd6_time);
1157 if (!err) {
1158 mmc_set_bus_width(card->host, bus_width);
1161 * If controller can't handle bus width test,
1162 * compare ext_csd previously read in 1 bit mode
1163 * against ext_csd at new bus width
1165 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
1166 err = mmc_compare_ext_csds(card,
1167 bus_width);
1168 else
1169 err = mmc_bus_test(card, bus_width);
1170 if (!err)
1171 break;
1175 if (!err && ddr) {
1176 err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
1177 ext_csd);
1178 if (err)
1179 pr_err("%s: power class selection to "
1180 "bus width %d ddr %d failed\n",
1181 mmc_hostname(card->host),
1182 1 << bus_width, ddr);
1184 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1185 EXT_CSD_BUS_WIDTH,
1186 ext_csd_bits[idx][1],
1187 card->ext_csd.generic_cmd6_time);
1189 if (err) {
1190 pr_warning("%s: switch to bus width %d ddr %d "
1191 "failed\n", mmc_hostname(card->host),
1192 1 << bus_width, ddr);
1193 goto free_card;
1194 } else if (ddr) {
1196 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1197 * signaling.
1199 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1201 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1202 * in the JEDEC spec for DDR.
1204 * Do not force change in vccq since we are obviously
1205 * working and no change to vccq is needed.
1207 * WARNING: eMMC rules are NOT the same as SD DDR
1209 if (ddr == MMC_1_2V_DDR_MODE) {
1210 err = mmc_set_signal_voltage(host,
1211 MMC_SIGNAL_VOLTAGE_120, 0);
1212 if (err)
1213 goto err;
1215 mmc_card_set_ddr_mode(card);
1216 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1217 mmc_set_bus_width(card->host, bus_width);
1222 * If cache size is higher than 0, this indicates
1223 * the existence of cache and it can be turned on.
1225 if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1226 card->ext_csd.cache_size > 0) {
1227 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1228 EXT_CSD_CACHE_CTRL, 1,
1229 card->ext_csd.generic_cmd6_time);
1230 if (err && err != -EBADMSG)
1231 goto free_card;
1234 * Only if no error, cache is turned on successfully.
1236 if (err) {
1237 pr_warning("%s: Cache is supported, "
1238 "but failed to turn on (%d)\n",
1239 mmc_hostname(card->host), err);
1240 card->ext_csd.cache_ctrl = 0;
1241 err = 0;
1242 } else {
1243 card->ext_csd.cache_ctrl = 1;
1247 if (!oldcard)
1248 host->card = card;
1250 mmc_free_ext_csd(ext_csd);
1251 return 0;
1253 free_card:
1254 if (!oldcard)
1255 mmc_remove_card(card);
1256 err:
1257 mmc_free_ext_csd(ext_csd);
1259 return err;
1263 * Host is being removed. Free up the current card.
1265 static void mmc_remove(struct mmc_host *host)
1267 BUG_ON(!host);
1268 BUG_ON(!host->card);
1270 mmc_remove_card(host->card);
1271 host->card = NULL;
1275 * Card detection - card is alive.
1277 static int mmc_alive(struct mmc_host *host)
1279 return mmc_send_status(host->card, NULL);
1283 * Card detection callback from host.
1285 static void mmc_detect(struct mmc_host *host)
1287 int err;
1289 BUG_ON(!host);
1290 BUG_ON(!host->card);
1292 mmc_claim_host(host);
1295 * Just check if our card has been removed.
1297 err = _mmc_detect_card_removed(host);
1299 mmc_release_host(host);
1301 if (err) {
1302 mmc_remove(host);
1304 mmc_claim_host(host);
1305 mmc_detach_bus(host);
1306 mmc_power_off(host);
1307 mmc_release_host(host);
1312 * Suspend callback from host.
1314 static int mmc_suspend(struct mmc_host *host)
1316 int err = 0;
1318 BUG_ON(!host);
1319 BUG_ON(!host->card);
1321 mmc_claim_host(host);
1322 if (mmc_card_can_sleep(host)) {
1323 err = mmc_card_sleep(host);
1324 if (!err)
1325 mmc_card_set_sleep(host->card);
1326 } else if (!mmc_host_is_spi(host))
1327 mmc_deselect_cards(host);
1328 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
1329 mmc_release_host(host);
1331 return err;
1335 * Resume callback from host.
1337 * This function tries to determine if the same card is still present
1338 * and, if so, restore all state to it.
1340 static int mmc_resume(struct mmc_host *host)
1342 int err;
1344 BUG_ON(!host);
1345 BUG_ON(!host->card);
1347 mmc_claim_host(host);
1348 if (mmc_card_is_sleep(host->card)) {
1349 err = mmc_card_awake(host);
1350 mmc_card_clr_sleep(host->card);
1351 } else
1352 err = mmc_init_card(host, host->ocr, host->card);
1353 mmc_release_host(host);
1355 return err;
1358 static int mmc_power_restore(struct mmc_host *host)
1360 int ret;
1362 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
1363 mmc_card_clr_sleep(host->card);
1364 mmc_claim_host(host);
1365 ret = mmc_init_card(host, host->ocr, host->card);
1366 mmc_release_host(host);
1368 return ret;
1371 static int mmc_sleep(struct mmc_host *host)
1373 struct mmc_card *card = host->card;
1374 int err = -ENOSYS;
1376 if (card && card->ext_csd.rev >= 3) {
1377 err = mmc_card_sleepawake(host, 1);
1378 if (err < 0)
1379 pr_debug("%s: Error %d while putting card into sleep",
1380 mmc_hostname(host), err);
1383 return err;
1386 static int mmc_awake(struct mmc_host *host)
1388 struct mmc_card *card = host->card;
1389 int err = -ENOSYS;
1391 if (card && card->ext_csd.rev >= 3) {
1392 err = mmc_card_sleepawake(host, 0);
1393 if (err < 0)
1394 pr_debug("%s: Error %d while awaking sleeping card",
1395 mmc_hostname(host), err);
1398 return err;
1401 static const struct mmc_bus_ops mmc_ops = {
1402 .awake = mmc_awake,
1403 .sleep = mmc_sleep,
1404 .remove = mmc_remove,
1405 .detect = mmc_detect,
1406 .suspend = NULL,
1407 .resume = NULL,
1408 .power_restore = mmc_power_restore,
1409 .alive = mmc_alive,
1412 static const struct mmc_bus_ops mmc_ops_unsafe = {
1413 .awake = mmc_awake,
1414 .sleep = mmc_sleep,
1415 .remove = mmc_remove,
1416 .detect = mmc_detect,
1417 .suspend = mmc_suspend,
1418 .resume = mmc_resume,
1419 .power_restore = mmc_power_restore,
1420 .alive = mmc_alive,
1423 static void mmc_attach_bus_ops(struct mmc_host *host)
1425 const struct mmc_bus_ops *bus_ops;
1427 if (!mmc_card_is_removable(host))
1428 bus_ops = &mmc_ops_unsafe;
1429 else
1430 bus_ops = &mmc_ops;
1431 mmc_attach_bus(host, bus_ops);
1435 * Starting point for MMC card init.
1437 int mmc_attach_mmc(struct mmc_host *host)
1439 int err;
1440 u32 ocr;
1442 BUG_ON(!host);
1443 WARN_ON(!host->claimed);
1445 /* Set correct bus mode for MMC before attempting attach */
1446 if (!mmc_host_is_spi(host))
1447 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1449 err = mmc_send_op_cond(host, 0, &ocr);
1450 if (err)
1451 return err;
1453 mmc_attach_bus_ops(host);
1454 if (host->ocr_avail_mmc)
1455 host->ocr_avail = host->ocr_avail_mmc;
1458 * We need to get OCR a different way for SPI.
1460 if (mmc_host_is_spi(host)) {
1461 err = mmc_spi_read_ocr(host, 1, &ocr);
1462 if (err)
1463 goto err;
1467 * Sanity check the voltages that the card claims to
1468 * support.
1470 if (ocr & 0x7F) {
1471 pr_warning("%s: card claims to support voltages "
1472 "below the defined range. These will be ignored.\n",
1473 mmc_hostname(host));
1474 ocr &= ~0x7F;
1477 host->ocr = mmc_select_voltage(host, ocr);
1480 * Can we support the voltage of the card?
1482 if (!host->ocr) {
1483 err = -EINVAL;
1484 goto err;
1488 * Detect and init the card.
1490 err = mmc_init_card(host, host->ocr, NULL);
1491 if (err)
1492 goto err;
1494 mmc_release_host(host);
1495 err = mmc_add_card(host->card);
1496 mmc_claim_host(host);
1497 if (err)
1498 goto remove_card;
1500 return 0;
1502 remove_card:
1503 mmc_release_host(host);
1504 mmc_remove_card(host->card);
1505 mmc_claim_host(host);
1506 host->card = NULL;
1507 err:
1508 mmc_detach_bus(host);
1510 pr_err("%s: error %d whilst initialising MMC card\n",
1511 mmc_hostname(host), err);
1513 return err;