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>
26 static const unsigned int tran_exp
[] = {
27 10000, 100000, 1000000, 10000000,
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) \
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; \
53 __res = resp[__off] >> __shft; \
54 if (__size + __shft > 32) \
55 __res |= resp[__off-1] << ((32 - __shft) % 32); \
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;
88 case 2: /* MMC v2.0 - v2.2 */
89 case 3: /* MMC v3.1 - v3.3 */
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;
105 printk(KERN_ERR
"%s: card has unknown MMCA version %d\n",
106 mmc_hostname(card
->host
), card
->csd
.mmca_vsn
);
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
;
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 printk(KERN_ERR
"%s: unrecognised CSD structure version %d\n",
140 mmc_hostname(card
->host
), csd
->structure
);
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;
180 static int mmc_get_ext_csd(struct mmc_card
*card
, u8
**new_ext_csd
)
186 BUG_ON(!new_ext_csd
);
190 if (card
->csd
.mmca_vsn
< CSD_SPEC_VER_4
)
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
);
199 printk(KERN_ERR
"%s: could not allocate a buffer to "
200 "receive the ext_csd.\n", mmc_hostname(card
->host
));
204 err
= mmc_send_ext_csd(card
, ext_csd
);
209 /* If the host or the card can't do the switch,
210 * fail more gracefully. */
217 * High capacity cards should have this "magic" size
218 * stored in their CSD.
220 if (card
->csd
.capacity
== (4096 * 512)) {
221 printk(KERN_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
));
226 printk(KERN_WARNING
"%s: unable to read "
227 "EXT_CSD, performance might "
229 mmc_hostname(card
->host
));
233 *new_ext_csd
= ext_csd
;
239 * Decode extended CSD.
241 static int mmc_read_ext_csd(struct mmc_card
*card
, u8
*ext_csd
)
250 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
251 card
->ext_csd
.raw_ext_csd_structure
= ext_csd
[EXT_CSD_STRUCTURE
];
252 if (card
->csd
.structure
== 3) {
253 if (card
->ext_csd
.raw_ext_csd_structure
> 2) {
254 printk(KERN_ERR
"%s: unrecognised EXT_CSD structure "
255 "version %d\n", mmc_hostname(card
->host
),
256 card
->ext_csd
.raw_ext_csd_structure
);
262 card
->ext_csd
.rev
= ext_csd
[EXT_CSD_REV
];
263 if (card
->ext_csd
.rev
> 6) {
264 printk(KERN_ERR
"%s: unrecognised EXT_CSD revision %d\n",
265 mmc_hostname(card
->host
), card
->ext_csd
.rev
);
270 card
->ext_csd
.raw_sectors
[0] = ext_csd
[EXT_CSD_SEC_CNT
+ 0];
271 card
->ext_csd
.raw_sectors
[1] = ext_csd
[EXT_CSD_SEC_CNT
+ 1];
272 card
->ext_csd
.raw_sectors
[2] = ext_csd
[EXT_CSD_SEC_CNT
+ 2];
273 card
->ext_csd
.raw_sectors
[3] = ext_csd
[EXT_CSD_SEC_CNT
+ 3];
274 if (card
->ext_csd
.rev
>= 2) {
275 card
->ext_csd
.sectors
=
276 ext_csd
[EXT_CSD_SEC_CNT
+ 0] << 0 |
277 ext_csd
[EXT_CSD_SEC_CNT
+ 1] << 8 |
278 ext_csd
[EXT_CSD_SEC_CNT
+ 2] << 16 |
279 ext_csd
[EXT_CSD_SEC_CNT
+ 3] << 24;
281 /* Cards with density > 2GiB are sector addressed */
282 if (card
->ext_csd
.sectors
> (2u * 1024 * 1024 * 1024) / 512)
283 mmc_card_set_blockaddr(card
);
285 card
->ext_csd
.raw_card_type
= ext_csd
[EXT_CSD_CARD_TYPE
];
286 switch (ext_csd
[EXT_CSD_CARD_TYPE
] & EXT_CSD_CARD_TYPE_MASK
) {
287 case EXT_CSD_CARD_TYPE_DDR_52
| EXT_CSD_CARD_TYPE_52
|
288 EXT_CSD_CARD_TYPE_26
:
289 card
->ext_csd
.hs_max_dtr
= 52000000;
290 card
->ext_csd
.card_type
= EXT_CSD_CARD_TYPE_DDR_52
;
292 case EXT_CSD_CARD_TYPE_DDR_1_2V
| EXT_CSD_CARD_TYPE_52
|
293 EXT_CSD_CARD_TYPE_26
:
294 card
->ext_csd
.hs_max_dtr
= 52000000;
295 card
->ext_csd
.card_type
= EXT_CSD_CARD_TYPE_DDR_1_2V
;
297 case EXT_CSD_CARD_TYPE_DDR_1_8V
| EXT_CSD_CARD_TYPE_52
|
298 EXT_CSD_CARD_TYPE_26
:
299 card
->ext_csd
.hs_max_dtr
= 52000000;
300 card
->ext_csd
.card_type
= EXT_CSD_CARD_TYPE_DDR_1_8V
;
302 case EXT_CSD_CARD_TYPE_52
| EXT_CSD_CARD_TYPE_26
:
303 card
->ext_csd
.hs_max_dtr
= 52000000;
305 case EXT_CSD_CARD_TYPE_26
:
306 card
->ext_csd
.hs_max_dtr
= 26000000;
309 /* MMC v4 spec says this cannot happen */
310 printk(KERN_WARNING
"%s: card is mmc v4 but doesn't "
311 "support any high-speed modes.\n",
312 mmc_hostname(card
->host
));
315 card
->ext_csd
.raw_s_a_timeout
= ext_csd
[EXT_CSD_S_A_TIMEOUT
];
316 card
->ext_csd
.raw_erase_timeout_mult
=
317 ext_csd
[EXT_CSD_ERASE_TIMEOUT_MULT
];
318 card
->ext_csd
.raw_hc_erase_grp_size
=
319 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
];
320 if (card
->ext_csd
.rev
>= 3) {
321 u8 sa_shift
= ext_csd
[EXT_CSD_S_A_TIMEOUT
];
322 card
->ext_csd
.part_config
= ext_csd
[EXT_CSD_PART_CONFIG
];
324 /* EXT_CSD value is in units of 10ms, but we store in ms */
325 card
->ext_csd
.part_time
= 10 * ext_csd
[EXT_CSD_PART_SWITCH_TIME
];
327 /* Sleep / awake timeout in 100ns units */
328 if (sa_shift
> 0 && sa_shift
<= 0x17)
329 card
->ext_csd
.sa_timeout
=
330 1 << ext_csd
[EXT_CSD_S_A_TIMEOUT
];
331 card
->ext_csd
.erase_group_def
=
332 ext_csd
[EXT_CSD_ERASE_GROUP_DEF
];
333 card
->ext_csd
.hc_erase_timeout
= 300 *
334 ext_csd
[EXT_CSD_ERASE_TIMEOUT_MULT
];
335 card
->ext_csd
.hc_erase_size
=
336 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
] << 10;
338 card
->ext_csd
.rel_sectors
= ext_csd
[EXT_CSD_REL_WR_SEC_C
];
341 * There are two boot regions of equal size, defined in
344 card
->ext_csd
.boot_size
= ext_csd
[EXT_CSD_BOOT_MULT
] << 17;
347 card
->ext_csd
.raw_hc_erase_gap_size
=
348 ext_csd
[EXT_CSD_PARTITION_ATTRIBUTE
];
349 card
->ext_csd
.raw_sec_trim_mult
=
350 ext_csd
[EXT_CSD_SEC_TRIM_MULT
];
351 card
->ext_csd
.raw_sec_erase_mult
=
352 ext_csd
[EXT_CSD_SEC_ERASE_MULT
];
353 card
->ext_csd
.raw_sec_feature_support
=
354 ext_csd
[EXT_CSD_SEC_FEATURE_SUPPORT
];
355 card
->ext_csd
.raw_trim_mult
=
356 ext_csd
[EXT_CSD_TRIM_MULT
];
357 if (card
->ext_csd
.rev
>= 4) {
359 * Enhanced area feature support -- check whether the eMMC
360 * card has the Enhanced area enabled. If so, export enhanced
361 * area offset and size to user by adding sysfs interface.
363 if ((ext_csd
[EXT_CSD_PARTITION_SUPPORT
] & 0x2) &&
364 (ext_csd
[EXT_CSD_PARTITION_ATTRIBUTE
] & 0x1)) {
366 ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
];
368 ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
];
370 card
->ext_csd
.enhanced_area_en
= 1;
372 * calculate the enhanced data area offset, in bytes
374 card
->ext_csd
.enhanced_area_offset
=
375 (ext_csd
[139] << 24) + (ext_csd
[138] << 16) +
376 (ext_csd
[137] << 8) + ext_csd
[136];
377 if (mmc_card_blockaddr(card
))
378 card
->ext_csd
.enhanced_area_offset
<<= 9;
380 * calculate the enhanced data area size, in kilobytes
382 card
->ext_csd
.enhanced_area_size
=
383 (ext_csd
[142] << 16) + (ext_csd
[141] << 8) +
385 card
->ext_csd
.enhanced_area_size
*=
386 (size_t)(hc_erase_grp_sz
* hc_wp_grp_sz
);
387 card
->ext_csd
.enhanced_area_size
<<= 9;
390 * If the enhanced area is not enabled, disable these
393 card
->ext_csd
.enhanced_area_offset
= -EINVAL
;
394 card
->ext_csd
.enhanced_area_size
= -EINVAL
;
396 card
->ext_csd
.sec_trim_mult
=
397 ext_csd
[EXT_CSD_SEC_TRIM_MULT
];
398 card
->ext_csd
.sec_erase_mult
=
399 ext_csd
[EXT_CSD_SEC_ERASE_MULT
];
400 card
->ext_csd
.sec_feature_support
=
401 ext_csd
[EXT_CSD_SEC_FEATURE_SUPPORT
];
402 card
->ext_csd
.trim_timeout
= 300 *
403 ext_csd
[EXT_CSD_TRIM_MULT
];
406 if (card
->ext_csd
.rev
>= 5)
407 card
->ext_csd
.rel_param
= ext_csd
[EXT_CSD_WR_REL_PARAM
];
409 if (ext_csd
[EXT_CSD_ERASED_MEM_CONT
])
410 card
->erased_byte
= 0xFF;
412 card
->erased_byte
= 0x0;
418 static inline void mmc_free_ext_csd(u8
*ext_csd
)
424 static int mmc_compare_ext_csds(struct mmc_card
*card
, unsigned bus_width
)
429 if (bus_width
== MMC_BUS_WIDTH_1
)
432 err
= mmc_get_ext_csd(card
, &bw_ext_csd
);
434 if (err
|| bw_ext_csd
== NULL
) {
435 if (bus_width
!= MMC_BUS_WIDTH_1
)
440 if (bus_width
== MMC_BUS_WIDTH_1
)
443 /* only compare read only fields */
444 err
= (!(card
->ext_csd
.raw_partition_support
==
445 bw_ext_csd
[EXT_CSD_PARTITION_SUPPORT
]) &&
446 (card
->ext_csd
.raw_erased_mem_count
==
447 bw_ext_csd
[EXT_CSD_ERASED_MEM_CONT
]) &&
448 (card
->ext_csd
.rev
==
449 bw_ext_csd
[EXT_CSD_REV
]) &&
450 (card
->ext_csd
.raw_ext_csd_structure
==
451 bw_ext_csd
[EXT_CSD_STRUCTURE
]) &&
452 (card
->ext_csd
.raw_card_type
==
453 bw_ext_csd
[EXT_CSD_CARD_TYPE
]) &&
454 (card
->ext_csd
.raw_s_a_timeout
==
455 bw_ext_csd
[EXT_CSD_S_A_TIMEOUT
]) &&
456 (card
->ext_csd
.raw_hc_erase_gap_size
==
457 bw_ext_csd
[EXT_CSD_HC_WP_GRP_SIZE
]) &&
458 (card
->ext_csd
.raw_erase_timeout_mult
==
459 bw_ext_csd
[EXT_CSD_ERASE_TIMEOUT_MULT
]) &&
460 (card
->ext_csd
.raw_hc_erase_grp_size
==
461 bw_ext_csd
[EXT_CSD_HC_ERASE_GRP_SIZE
]) &&
462 (card
->ext_csd
.raw_sec_trim_mult
==
463 bw_ext_csd
[EXT_CSD_SEC_TRIM_MULT
]) &&
464 (card
->ext_csd
.raw_sec_erase_mult
==
465 bw_ext_csd
[EXT_CSD_SEC_ERASE_MULT
]) &&
466 (card
->ext_csd
.raw_sec_feature_support
==
467 bw_ext_csd
[EXT_CSD_SEC_FEATURE_SUPPORT
]) &&
468 (card
->ext_csd
.raw_trim_mult
==
469 bw_ext_csd
[EXT_CSD_TRIM_MULT
]) &&
470 (card
->ext_csd
.raw_sectors
[0] ==
471 bw_ext_csd
[EXT_CSD_SEC_CNT
+ 0]) &&
472 (card
->ext_csd
.raw_sectors
[1] ==
473 bw_ext_csd
[EXT_CSD_SEC_CNT
+ 1]) &&
474 (card
->ext_csd
.raw_sectors
[2] ==
475 bw_ext_csd
[EXT_CSD_SEC_CNT
+ 2]) &&
476 (card
->ext_csd
.raw_sectors
[3] ==
477 bw_ext_csd
[EXT_CSD_SEC_CNT
+ 3]));
482 mmc_free_ext_csd(bw_ext_csd
);
486 MMC_DEV_ATTR(cid
, "%08x%08x%08x%08x\n", card
->raw_cid
[0], card
->raw_cid
[1],
487 card
->raw_cid
[2], card
->raw_cid
[3]);
488 MMC_DEV_ATTR(csd
, "%08x%08x%08x%08x\n", card
->raw_csd
[0], card
->raw_csd
[1],
489 card
->raw_csd
[2], card
->raw_csd
[3]);
490 MMC_DEV_ATTR(date
, "%02d/%04d\n", card
->cid
.month
, card
->cid
.year
);
491 MMC_DEV_ATTR(erase_size
, "%u\n", card
->erase_size
<< 9);
492 MMC_DEV_ATTR(preferred_erase_size
, "%u\n", card
->pref_erase
<< 9);
493 MMC_DEV_ATTR(fwrev
, "0x%x\n", card
->cid
.fwrev
);
494 MMC_DEV_ATTR(hwrev
, "0x%x\n", card
->cid
.hwrev
);
495 MMC_DEV_ATTR(manfid
, "0x%06x\n", card
->cid
.manfid
);
496 MMC_DEV_ATTR(name
, "%s\n", card
->cid
.prod_name
);
497 MMC_DEV_ATTR(oemid
, "0x%04x\n", card
->cid
.oemid
);
498 MMC_DEV_ATTR(serial
, "0x%08x\n", card
->cid
.serial
);
499 MMC_DEV_ATTR(enhanced_area_offset
, "%llu\n",
500 card
->ext_csd
.enhanced_area_offset
);
501 MMC_DEV_ATTR(enhanced_area_size
, "%u\n", card
->ext_csd
.enhanced_area_size
);
503 static struct attribute
*mmc_std_attrs
[] = {
507 &dev_attr_erase_size
.attr
,
508 &dev_attr_preferred_erase_size
.attr
,
509 &dev_attr_fwrev
.attr
,
510 &dev_attr_hwrev
.attr
,
511 &dev_attr_manfid
.attr
,
513 &dev_attr_oemid
.attr
,
514 &dev_attr_serial
.attr
,
515 &dev_attr_enhanced_area_offset
.attr
,
516 &dev_attr_enhanced_area_size
.attr
,
520 static struct attribute_group mmc_std_attr_group
= {
521 .attrs
= mmc_std_attrs
,
524 static const struct attribute_group
*mmc_attr_groups
[] = {
529 static struct device_type mmc_type
= {
530 .groups
= mmc_attr_groups
,
534 * Handle the detection and initialisation of a card.
536 * In the case of a resume, "oldcard" will contain the card
537 * we're trying to reinitialise.
539 static int mmc_init_card(struct mmc_host
*host
, u32 ocr
,
540 struct mmc_card
*oldcard
)
542 struct mmc_card
*card
;
545 unsigned int max_dtr
;
550 WARN_ON(!host
->claimed
);
553 * Since we're changing the OCR value, we seem to
554 * need to tell some cards to go back to the idle
555 * state. We wait 1ms to give cards time to
560 /* The extra bit indicates that we support high capacity */
561 err
= mmc_send_op_cond(host
, ocr
| (1 << 30), &rocr
);
566 * For SPI, enable CRC as appropriate.
568 if (mmc_host_is_spi(host
)) {
569 err
= mmc_spi_set_crc(host
, use_spi_crc
);
575 * Fetch CID from card.
577 if (mmc_host_is_spi(host
))
578 err
= mmc_send_cid(host
, cid
);
580 err
= mmc_all_send_cid(host
, cid
);
585 if (memcmp(cid
, oldcard
->raw_cid
, sizeof(cid
)) != 0) {
593 * Allocate card structure.
595 card
= mmc_alloc_card(host
, &mmc_type
);
601 card
->type
= MMC_TYPE_MMC
;
603 memcpy(card
->raw_cid
, cid
, sizeof(card
->raw_cid
));
607 * For native busses: set card RCA and quit open drain mode.
609 if (!mmc_host_is_spi(host
)) {
610 err
= mmc_set_relative_addr(card
);
614 mmc_set_bus_mode(host
, MMC_BUSMODE_PUSHPULL
);
619 * Fetch CSD from card.
621 err
= mmc_send_csd(card
, card
->raw_csd
);
625 err
= mmc_decode_csd(card
);
628 err
= mmc_decode_cid(card
);
634 * Select card, as all following commands rely on that.
636 if (!mmc_host_is_spi(host
)) {
637 err
= mmc_select_card(card
);
644 * Fetch and process extended CSD.
647 err
= mmc_get_ext_csd(card
, &ext_csd
);
650 err
= mmc_read_ext_csd(card
, ext_csd
);
654 /* If doing byte addressing, check if required to do sector
655 * addressing. Handle the case of <2GB cards needing sector
656 * addressing. See section 8.1 JEDEC Standard JED84-A441;
657 * ocr register has bit 30 set for sector addressing.
659 if (!(mmc_card_blockaddr(card
)) && (rocr
& (1<<30)))
660 mmc_card_set_blockaddr(card
);
662 /* Erase size depends on CSD and Extended CSD */
663 mmc_set_erase_size(card
);
667 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
668 * bit. This bit will be lost every time after a reset or power off.
670 if (card
->ext_csd
.enhanced_area_en
) {
671 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
672 EXT_CSD_ERASE_GROUP_DEF
, 1, 0);
674 if (err
&& err
!= -EBADMSG
)
680 * Just disable enhanced area off & sz
681 * will try to enable ERASE_GROUP_DEF
682 * during next time reinit
684 card
->ext_csd
.enhanced_area_offset
= -EINVAL
;
685 card
->ext_csd
.enhanced_area_size
= -EINVAL
;
687 card
->ext_csd
.erase_group_def
= 1;
689 * enable ERASE_GRP_DEF successfully.
690 * This will affect the erase size, so
691 * here need to reset erase size
693 mmc_set_erase_size(card
);
698 * Ensure eMMC user default partition is enabled
700 if (card
->ext_csd
.part_config
& EXT_CSD_PART_CONFIG_ACC_MASK
) {
701 card
->ext_csd
.part_config
&= ~EXT_CSD_PART_CONFIG_ACC_MASK
;
702 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_PART_CONFIG
,
703 card
->ext_csd
.part_config
,
704 card
->ext_csd
.part_time
);
705 if (err
&& err
!= -EBADMSG
)
710 * Activate high speed (if supported)
712 if ((card
->ext_csd
.hs_max_dtr
!= 0) &&
713 (host
->caps
& MMC_CAP_MMC_HIGHSPEED
)) {
714 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
715 EXT_CSD_HS_TIMING
, 1, 0);
716 if (err
&& err
!= -EBADMSG
)
720 printk(KERN_WARNING
"%s: switch to highspeed failed\n",
721 mmc_hostname(card
->host
));
724 mmc_card_set_highspeed(card
);
725 mmc_set_timing(card
->host
, MMC_TIMING_MMC_HS
);
732 max_dtr
= (unsigned int)-1;
734 if (mmc_card_highspeed(card
)) {
735 if (max_dtr
> card
->ext_csd
.hs_max_dtr
)
736 max_dtr
= card
->ext_csd
.hs_max_dtr
;
737 } else if (max_dtr
> card
->csd
.max_dtr
) {
738 max_dtr
= card
->csd
.max_dtr
;
741 mmc_set_clock(host
, max_dtr
);
744 * Indicate DDR mode (if supported).
746 if (mmc_card_highspeed(card
)) {
747 if ((card
->ext_csd
.card_type
& EXT_CSD_CARD_TYPE_DDR_1_8V
)
748 && ((host
->caps
& (MMC_CAP_1_8V_DDR
|
750 == (MMC_CAP_1_8V_DDR
| MMC_CAP_UHS_DDR50
)))
751 ddr
= MMC_1_8V_DDR_MODE
;
752 else if ((card
->ext_csd
.card_type
& EXT_CSD_CARD_TYPE_DDR_1_2V
)
753 && ((host
->caps
& (MMC_CAP_1_2V_DDR
|
755 == (MMC_CAP_1_2V_DDR
| MMC_CAP_UHS_DDR50
)))
756 ddr
= MMC_1_2V_DDR_MODE
;
760 * Activate wide bus and DDR (if supported).
762 if ((card
->csd
.mmca_vsn
>= CSD_SPEC_VER_4
) &&
763 (host
->caps
& (MMC_CAP_4_BIT_DATA
| MMC_CAP_8_BIT_DATA
))) {
764 static unsigned ext_csd_bits
[][2] = {
765 { EXT_CSD_BUS_WIDTH_8
, EXT_CSD_DDR_BUS_WIDTH_8
},
766 { EXT_CSD_BUS_WIDTH_4
, EXT_CSD_DDR_BUS_WIDTH_4
},
767 { EXT_CSD_BUS_WIDTH_1
, EXT_CSD_BUS_WIDTH_1
},
769 static unsigned bus_widths
[] = {
774 unsigned idx
, bus_width
= 0;
776 if (host
->caps
& MMC_CAP_8_BIT_DATA
)
780 for (; idx
< ARRAY_SIZE(bus_widths
); idx
++) {
781 bus_width
= bus_widths
[idx
];
782 if (bus_width
== MMC_BUS_WIDTH_1
)
783 ddr
= 0; /* no DDR for 1-bit width */
784 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
786 ext_csd_bits
[idx
][0],
789 mmc_set_bus_width(card
->host
, bus_width
);
792 * If controller can't handle bus width test,
793 * compare ext_csd previously read in 1 bit mode
794 * against ext_csd at new bus width
796 if (!(host
->caps
& MMC_CAP_BUS_WIDTH_TEST
))
797 err
= mmc_compare_ext_csds(card
,
800 err
= mmc_bus_test(card
, bus_width
);
807 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
809 ext_csd_bits
[idx
][1],
813 printk(KERN_WARNING
"%s: switch to bus width %d ddr %d "
814 "failed\n", mmc_hostname(card
->host
),
815 1 << bus_width
, ddr
);
819 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
822 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
824 * 1.8V vccq at 3.3V core voltage (vcc) is not required
825 * in the JEDEC spec for DDR.
827 * Do not force change in vccq since we are obviously
828 * working and no change to vccq is needed.
830 * WARNING: eMMC rules are NOT the same as SD DDR
832 if (ddr
== EXT_CSD_CARD_TYPE_DDR_1_2V
) {
833 err
= mmc_set_signal_voltage(host
,
834 MMC_SIGNAL_VOLTAGE_120
, 0);
838 mmc_card_set_ddr_mode(card
);
839 mmc_set_timing(card
->host
, MMC_TIMING_UHS_DDR50
);
840 mmc_set_bus_width(card
->host
, bus_width
);
847 mmc_free_ext_csd(ext_csd
);
852 mmc_remove_card(card
);
854 mmc_free_ext_csd(ext_csd
);
860 * Host is being removed. Free up the current card.
862 static void mmc_remove(struct mmc_host
*host
)
867 mmc_remove_card(host
->card
);
872 * Card detection callback from host.
874 static void mmc_detect(struct mmc_host
*host
)
881 mmc_claim_host(host
);
884 * Just check if our card has been removed.
886 err
= mmc_send_status(host
->card
, NULL
);
888 mmc_release_host(host
);
893 mmc_claim_host(host
);
894 mmc_detach_bus(host
);
895 mmc_release_host(host
);
900 * Suspend callback from host.
902 static int mmc_suspend(struct mmc_host
*host
)
907 mmc_claim_host(host
);
908 if (!mmc_host_is_spi(host
))
909 mmc_deselect_cards(host
);
910 host
->card
->state
&= ~MMC_STATE_HIGHSPEED
;
911 mmc_release_host(host
);
917 * Resume callback from host.
919 * This function tries to determine if the same card is still present
920 * and, if so, restore all state to it.
922 static int mmc_resume(struct mmc_host
*host
)
929 mmc_claim_host(host
);
930 err
= mmc_init_card(host
, host
->ocr
, host
->card
);
931 mmc_release_host(host
);
936 static int mmc_power_restore(struct mmc_host
*host
)
940 host
->card
->state
&= ~MMC_STATE_HIGHSPEED
;
941 mmc_claim_host(host
);
942 ret
= mmc_init_card(host
, host
->ocr
, host
->card
);
943 mmc_release_host(host
);
948 static int mmc_sleep(struct mmc_host
*host
)
950 struct mmc_card
*card
= host
->card
;
953 if (card
&& card
->ext_csd
.rev
>= 3) {
954 err
= mmc_card_sleepawake(host
, 1);
956 pr_debug("%s: Error %d while putting card into sleep",
957 mmc_hostname(host
), err
);
963 static int mmc_awake(struct mmc_host
*host
)
965 struct mmc_card
*card
= host
->card
;
968 if (card
&& card
->ext_csd
.rev
>= 3) {
969 err
= mmc_card_sleepawake(host
, 0);
971 pr_debug("%s: Error %d while awaking sleeping card",
972 mmc_hostname(host
), err
);
978 static const struct mmc_bus_ops mmc_ops
= {
981 .remove
= mmc_remove
,
982 .detect
= mmc_detect
,
985 .power_restore
= mmc_power_restore
,
988 static const struct mmc_bus_ops mmc_ops_unsafe
= {
991 .remove
= mmc_remove
,
992 .detect
= mmc_detect
,
993 .suspend
= mmc_suspend
,
994 .resume
= mmc_resume
,
995 .power_restore
= mmc_power_restore
,
998 static void mmc_attach_bus_ops(struct mmc_host
*host
)
1000 const struct mmc_bus_ops
*bus_ops
;
1002 if (!mmc_card_is_removable(host
))
1003 bus_ops
= &mmc_ops_unsafe
;
1006 mmc_attach_bus(host
, bus_ops
);
1010 * Starting point for MMC card init.
1012 int mmc_attach_mmc(struct mmc_host
*host
)
1018 WARN_ON(!host
->claimed
);
1020 err
= mmc_send_op_cond(host
, 0, &ocr
);
1024 mmc_attach_bus_ops(host
);
1025 if (host
->ocr_avail_mmc
)
1026 host
->ocr_avail
= host
->ocr_avail_mmc
;
1029 * We need to get OCR a different way for SPI.
1031 if (mmc_host_is_spi(host
)) {
1032 err
= mmc_spi_read_ocr(host
, 1, &ocr
);
1038 * Sanity check the voltages that the card claims to
1042 printk(KERN_WARNING
"%s: card claims to support voltages "
1043 "below the defined range. These will be ignored.\n",
1044 mmc_hostname(host
));
1048 host
->ocr
= mmc_select_voltage(host
, ocr
);
1051 * Can we support the voltage of the card?
1059 * Detect and init the card.
1061 err
= mmc_init_card(host
, host
->ocr
, NULL
);
1065 mmc_release_host(host
);
1066 err
= mmc_add_card(host
->card
);
1067 mmc_claim_host(host
);
1074 mmc_release_host(host
);
1075 mmc_remove_card(host
->card
);
1076 mmc_claim_host(host
);
1079 mmc_detach_bus(host
);
1081 printk(KERN_ERR
"%s: error %d whilst initialising MMC card\n",
1082 mmc_hostname(host
), err
);