mmc: Fix implicit use of stat.h header in associated files
[linux-2.6/btrfs-unstable.git] / drivers / mmc / core / mmc.c
blobdbf421a6279c702d91baeede5bb5f1e55dcb6fcd
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_DDR_52 | EXT_CSD_CARD_TYPE_52 |
290 EXT_CSD_CARD_TYPE_26:
291 card->ext_csd.hs_max_dtr = 52000000;
292 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52;
293 break;
294 case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 |
295 EXT_CSD_CARD_TYPE_26:
296 card->ext_csd.hs_max_dtr = 52000000;
297 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V;
298 break;
299 case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 |
300 EXT_CSD_CARD_TYPE_26:
301 card->ext_csd.hs_max_dtr = 52000000;
302 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V;
303 break;
304 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
305 card->ext_csd.hs_max_dtr = 52000000;
306 break;
307 case EXT_CSD_CARD_TYPE_26:
308 card->ext_csd.hs_max_dtr = 26000000;
309 break;
310 default:
311 /* MMC v4 spec says this cannot happen */
312 pr_warning("%s: card is mmc v4 but doesn't "
313 "support any high-speed modes.\n",
314 mmc_hostname(card->host));
317 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
318 card->ext_csd.raw_erase_timeout_mult =
319 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
320 card->ext_csd.raw_hc_erase_grp_size =
321 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
322 if (card->ext_csd.rev >= 3) {
323 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
324 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
326 /* EXT_CSD value is in units of 10ms, but we store in ms */
327 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
329 /* Sleep / awake timeout in 100ns units */
330 if (sa_shift > 0 && sa_shift <= 0x17)
331 card->ext_csd.sa_timeout =
332 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
333 card->ext_csd.erase_group_def =
334 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
335 card->ext_csd.hc_erase_timeout = 300 *
336 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
337 card->ext_csd.hc_erase_size =
338 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
340 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
343 * There are two boot regions of equal size, defined in
344 * multiples of 128K.
346 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
347 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
348 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
349 mmc_part_add(card, part_size,
350 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
351 "boot%d", idx, true);
356 card->ext_csd.raw_hc_erase_gap_size =
357 ext_csd[EXT_CSD_PARTITION_ATTRIBUTE];
358 card->ext_csd.raw_sec_trim_mult =
359 ext_csd[EXT_CSD_SEC_TRIM_MULT];
360 card->ext_csd.raw_sec_erase_mult =
361 ext_csd[EXT_CSD_SEC_ERASE_MULT];
362 card->ext_csd.raw_sec_feature_support =
363 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
364 card->ext_csd.raw_trim_mult =
365 ext_csd[EXT_CSD_TRIM_MULT];
366 if (card->ext_csd.rev >= 4) {
368 * Enhanced area feature support -- check whether the eMMC
369 * card has the Enhanced area enabled. If so, export enhanced
370 * area offset and size to user by adding sysfs interface.
372 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
373 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
374 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
375 hc_erase_grp_sz =
376 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
377 hc_wp_grp_sz =
378 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
380 card->ext_csd.enhanced_area_en = 1;
382 * calculate the enhanced data area offset, in bytes
384 card->ext_csd.enhanced_area_offset =
385 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
386 (ext_csd[137] << 8) + ext_csd[136];
387 if (mmc_card_blockaddr(card))
388 card->ext_csd.enhanced_area_offset <<= 9;
390 * calculate the enhanced data area size, in kilobytes
392 card->ext_csd.enhanced_area_size =
393 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
394 ext_csd[140];
395 card->ext_csd.enhanced_area_size *=
396 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
397 card->ext_csd.enhanced_area_size <<= 9;
398 } else {
400 * If the enhanced area is not enabled, disable these
401 * device attributes.
403 card->ext_csd.enhanced_area_offset = -EINVAL;
404 card->ext_csd.enhanced_area_size = -EINVAL;
408 * General purpose partition feature support --
409 * If ext_csd has the size of general purpose partitions,
410 * set size, part_cfg, partition name in mmc_part.
412 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
413 EXT_CSD_PART_SUPPORT_PART_EN) {
414 if (card->ext_csd.enhanced_area_en != 1) {
415 hc_erase_grp_sz =
416 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
417 hc_wp_grp_sz =
418 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
420 card->ext_csd.enhanced_area_en = 1;
423 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
424 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
425 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
426 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
427 continue;
428 part_size =
429 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
430 << 16) +
431 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
432 << 8) +
433 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
434 part_size *= (size_t)(hc_erase_grp_sz *
435 hc_wp_grp_sz);
436 mmc_part_add(card, part_size << 19,
437 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
438 "gp%d", idx, false);
441 card->ext_csd.sec_trim_mult =
442 ext_csd[EXT_CSD_SEC_TRIM_MULT];
443 card->ext_csd.sec_erase_mult =
444 ext_csd[EXT_CSD_SEC_ERASE_MULT];
445 card->ext_csd.sec_feature_support =
446 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
447 card->ext_csd.trim_timeout = 300 *
448 ext_csd[EXT_CSD_TRIM_MULT];
451 if (card->ext_csd.rev >= 5) {
452 /* check whether the eMMC card supports HPI */
453 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
454 card->ext_csd.hpi = 1;
455 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
456 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
457 else
458 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
460 * Indicate the maximum timeout to close
461 * a command interrupted by HPI
463 card->ext_csd.out_of_int_time =
464 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
467 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
468 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
471 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
472 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
473 card->erased_byte = 0xFF;
474 else
475 card->erased_byte = 0x0;
477 /* eMMC v4.5 or later */
478 if (card->ext_csd.rev >= 6) {
479 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
481 card->ext_csd.generic_cmd6_time = 10 *
482 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
483 card->ext_csd.power_off_longtime = 10 *
484 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
486 card->ext_csd.cache_size =
487 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
488 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
489 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
490 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
493 out:
494 return err;
497 static inline void mmc_free_ext_csd(u8 *ext_csd)
499 kfree(ext_csd);
503 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
505 u8 *bw_ext_csd;
506 int err;
508 if (bus_width == MMC_BUS_WIDTH_1)
509 return 0;
511 err = mmc_get_ext_csd(card, &bw_ext_csd);
513 if (err || bw_ext_csd == NULL) {
514 if (bus_width != MMC_BUS_WIDTH_1)
515 err = -EINVAL;
516 goto out;
519 if (bus_width == MMC_BUS_WIDTH_1)
520 goto out;
522 /* only compare read only fields */
523 err = (!(card->ext_csd.raw_partition_support ==
524 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
525 (card->ext_csd.raw_erased_mem_count ==
526 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
527 (card->ext_csd.rev ==
528 bw_ext_csd[EXT_CSD_REV]) &&
529 (card->ext_csd.raw_ext_csd_structure ==
530 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
531 (card->ext_csd.raw_card_type ==
532 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
533 (card->ext_csd.raw_s_a_timeout ==
534 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
535 (card->ext_csd.raw_hc_erase_gap_size ==
536 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
537 (card->ext_csd.raw_erase_timeout_mult ==
538 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
539 (card->ext_csd.raw_hc_erase_grp_size ==
540 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
541 (card->ext_csd.raw_sec_trim_mult ==
542 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
543 (card->ext_csd.raw_sec_erase_mult ==
544 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
545 (card->ext_csd.raw_sec_feature_support ==
546 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
547 (card->ext_csd.raw_trim_mult ==
548 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
549 (card->ext_csd.raw_sectors[0] ==
550 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
551 (card->ext_csd.raw_sectors[1] ==
552 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
553 (card->ext_csd.raw_sectors[2] ==
554 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
555 (card->ext_csd.raw_sectors[3] ==
556 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
557 if (err)
558 err = -EINVAL;
560 out:
561 mmc_free_ext_csd(bw_ext_csd);
562 return err;
565 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
566 card->raw_cid[2], card->raw_cid[3]);
567 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
568 card->raw_csd[2], card->raw_csd[3]);
569 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
570 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
571 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
572 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
573 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
574 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
575 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
576 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
577 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
578 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
579 card->ext_csd.enhanced_area_offset);
580 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
582 static struct attribute *mmc_std_attrs[] = {
583 &dev_attr_cid.attr,
584 &dev_attr_csd.attr,
585 &dev_attr_date.attr,
586 &dev_attr_erase_size.attr,
587 &dev_attr_preferred_erase_size.attr,
588 &dev_attr_fwrev.attr,
589 &dev_attr_hwrev.attr,
590 &dev_attr_manfid.attr,
591 &dev_attr_name.attr,
592 &dev_attr_oemid.attr,
593 &dev_attr_serial.attr,
594 &dev_attr_enhanced_area_offset.attr,
595 &dev_attr_enhanced_area_size.attr,
596 NULL,
599 static struct attribute_group mmc_std_attr_group = {
600 .attrs = mmc_std_attrs,
603 static const struct attribute_group *mmc_attr_groups[] = {
604 &mmc_std_attr_group,
605 NULL,
608 static struct device_type mmc_type = {
609 .groups = mmc_attr_groups,
613 * Select the PowerClass for the current bus width
614 * If power class is defined for 4/8 bit bus in the
615 * extended CSD register, select it by executing the
616 * mmc_switch command.
618 static int mmc_select_powerclass(struct mmc_card *card,
619 unsigned int bus_width, u8 *ext_csd)
621 int err = 0;
622 unsigned int pwrclass_val;
623 unsigned int index = 0;
624 struct mmc_host *host;
626 BUG_ON(!card);
628 host = card->host;
629 BUG_ON(!host);
631 if (ext_csd == NULL)
632 return 0;
634 /* Power class selection is supported for versions >= 4.0 */
635 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
636 return 0;
638 /* Power class values are defined only for 4/8 bit bus */
639 if (bus_width == EXT_CSD_BUS_WIDTH_1)
640 return 0;
642 switch (1 << host->ios.vdd) {
643 case MMC_VDD_165_195:
644 if (host->ios.clock <= 26000000)
645 index = EXT_CSD_PWR_CL_26_195;
646 else if (host->ios.clock <= 52000000)
647 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
648 EXT_CSD_PWR_CL_52_195 :
649 EXT_CSD_PWR_CL_DDR_52_195;
650 else if (host->ios.clock <= 200000000)
651 index = EXT_CSD_PWR_CL_200_195;
652 break;
653 case MMC_VDD_32_33:
654 case MMC_VDD_33_34:
655 case MMC_VDD_34_35:
656 case MMC_VDD_35_36:
657 if (host->ios.clock <= 26000000)
658 index = EXT_CSD_PWR_CL_26_360;
659 else if (host->ios.clock <= 52000000)
660 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
661 EXT_CSD_PWR_CL_52_360 :
662 EXT_CSD_PWR_CL_DDR_52_360;
663 else if (host->ios.clock <= 200000000)
664 index = EXT_CSD_PWR_CL_200_360;
665 break;
666 default:
667 pr_warning("%s: Voltage range not supported "
668 "for power class.\n", mmc_hostname(host));
669 return -EINVAL;
672 pwrclass_val = ext_csd[index];
674 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
675 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
676 EXT_CSD_PWR_CL_8BIT_SHIFT;
677 else
678 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
679 EXT_CSD_PWR_CL_4BIT_SHIFT;
681 /* If the power class is different from the default value */
682 if (pwrclass_val > 0) {
683 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
684 EXT_CSD_POWER_CLASS,
685 pwrclass_val,
686 card->ext_csd.generic_cmd6_time);
689 return err;
693 * Handle the detection and initialisation of a card.
695 * In the case of a resume, "oldcard" will contain the card
696 * we're trying to reinitialise.
698 static int mmc_init_card(struct mmc_host *host, u32 ocr,
699 struct mmc_card *oldcard)
701 struct mmc_card *card;
702 int err, ddr = 0;
703 u32 cid[4];
704 unsigned int max_dtr;
705 u32 rocr;
706 u8 *ext_csd = NULL;
708 BUG_ON(!host);
709 WARN_ON(!host->claimed);
711 /* Set correct bus mode for MMC before attempting init */
712 if (!mmc_host_is_spi(host))
713 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
716 * Since we're changing the OCR value, we seem to
717 * need to tell some cards to go back to the idle
718 * state. We wait 1ms to give cards time to
719 * respond.
720 * mmc_go_idle is needed for eMMC that are asleep
722 mmc_go_idle(host);
724 /* The extra bit indicates that we support high capacity */
725 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
726 if (err)
727 goto err;
730 * For SPI, enable CRC as appropriate.
732 if (mmc_host_is_spi(host)) {
733 err = mmc_spi_set_crc(host, use_spi_crc);
734 if (err)
735 goto err;
739 * Fetch CID from card.
741 if (mmc_host_is_spi(host))
742 err = mmc_send_cid(host, cid);
743 else
744 err = mmc_all_send_cid(host, cid);
745 if (err)
746 goto err;
748 if (oldcard) {
749 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
750 err = -ENOENT;
751 goto err;
754 card = oldcard;
755 } else {
757 * Allocate card structure.
759 card = mmc_alloc_card(host, &mmc_type);
760 if (IS_ERR(card)) {
761 err = PTR_ERR(card);
762 goto err;
765 card->type = MMC_TYPE_MMC;
766 card->rca = 1;
767 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
771 * For native busses: set card RCA and quit open drain mode.
773 if (!mmc_host_is_spi(host)) {
774 err = mmc_set_relative_addr(card);
775 if (err)
776 goto free_card;
778 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
781 if (!oldcard) {
783 * Fetch CSD from card.
785 err = mmc_send_csd(card, card->raw_csd);
786 if (err)
787 goto free_card;
789 err = mmc_decode_csd(card);
790 if (err)
791 goto free_card;
792 err = mmc_decode_cid(card);
793 if (err)
794 goto free_card;
798 * Select card, as all following commands rely on that.
800 if (!mmc_host_is_spi(host)) {
801 err = mmc_select_card(card);
802 if (err)
803 goto free_card;
806 if (!oldcard) {
808 * Fetch and process extended CSD.
811 err = mmc_get_ext_csd(card, &ext_csd);
812 if (err)
813 goto free_card;
814 err = mmc_read_ext_csd(card, ext_csd);
815 if (err)
816 goto free_card;
818 /* If doing byte addressing, check if required to do sector
819 * addressing. Handle the case of <2GB cards needing sector
820 * addressing. See section 8.1 JEDEC Standard JED84-A441;
821 * ocr register has bit 30 set for sector addressing.
823 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
824 mmc_card_set_blockaddr(card);
826 /* Erase size depends on CSD and Extended CSD */
827 mmc_set_erase_size(card);
831 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
832 * bit. This bit will be lost every time after a reset or power off.
834 if (card->ext_csd.enhanced_area_en) {
835 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
836 EXT_CSD_ERASE_GROUP_DEF, 1,
837 card->ext_csd.generic_cmd6_time);
839 if (err && err != -EBADMSG)
840 goto free_card;
842 if (err) {
843 err = 0;
845 * Just disable enhanced area off & sz
846 * will try to enable ERASE_GROUP_DEF
847 * during next time reinit
849 card->ext_csd.enhanced_area_offset = -EINVAL;
850 card->ext_csd.enhanced_area_size = -EINVAL;
851 } else {
852 card->ext_csd.erase_group_def = 1;
854 * enable ERASE_GRP_DEF successfully.
855 * This will affect the erase size, so
856 * here need to reset erase size
858 mmc_set_erase_size(card);
863 * Ensure eMMC user default partition is enabled
865 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
866 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
867 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
868 card->ext_csd.part_config,
869 card->ext_csd.part_time);
870 if (err && err != -EBADMSG)
871 goto free_card;
875 * If the host supports the power_off_notify capability then
876 * set the notification byte in the ext_csd register of device
878 if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) &&
879 (card->poweroff_notify_state == MMC_NO_POWER_NOTIFICATION)) {
880 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
881 EXT_CSD_POWER_OFF_NOTIFICATION,
882 EXT_CSD_POWER_ON,
883 card->ext_csd.generic_cmd6_time);
884 if (err && err != -EBADMSG)
885 goto free_card;
888 if (!err)
889 card->poweroff_notify_state = MMC_POWERED_ON;
892 * Activate high speed (if supported)
894 if ((card->ext_csd.hs_max_dtr != 0) &&
895 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
896 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
897 EXT_CSD_HS_TIMING, 1,
898 card->ext_csd.generic_cmd6_time);
899 if (err && err != -EBADMSG)
900 goto free_card;
902 if (err) {
903 pr_warning("%s: switch to highspeed failed\n",
904 mmc_hostname(card->host));
905 err = 0;
906 } else {
907 mmc_card_set_highspeed(card);
908 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
913 * Enable HPI feature (if supported)
915 if (card->ext_csd.hpi) {
916 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
917 EXT_CSD_HPI_MGMT, 1, 0);
918 if (err && err != -EBADMSG)
919 goto free_card;
920 if (err) {
921 pr_warning("%s: Enabling HPI failed\n",
922 mmc_hostname(card->host));
923 err = 0;
924 } else
925 card->ext_csd.hpi_en = 1;
929 * Compute bus speed.
931 max_dtr = (unsigned int)-1;
933 if (mmc_card_highspeed(card)) {
934 if (max_dtr > card->ext_csd.hs_max_dtr)
935 max_dtr = card->ext_csd.hs_max_dtr;
936 } else if (max_dtr > card->csd.max_dtr) {
937 max_dtr = card->csd.max_dtr;
940 mmc_set_clock(host, max_dtr);
943 * Indicate DDR mode (if supported).
945 if (mmc_card_highspeed(card)) {
946 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
947 && ((host->caps & (MMC_CAP_1_8V_DDR |
948 MMC_CAP_UHS_DDR50))
949 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
950 ddr = MMC_1_8V_DDR_MODE;
951 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
952 && ((host->caps & (MMC_CAP_1_2V_DDR |
953 MMC_CAP_UHS_DDR50))
954 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
955 ddr = MMC_1_2V_DDR_MODE;
959 * Activate wide bus and DDR (if supported).
961 if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
962 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
963 static unsigned ext_csd_bits[][2] = {
964 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
965 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
966 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
968 static unsigned bus_widths[] = {
969 MMC_BUS_WIDTH_8,
970 MMC_BUS_WIDTH_4,
971 MMC_BUS_WIDTH_1
973 unsigned idx, bus_width = 0;
975 if (host->caps & MMC_CAP_8_BIT_DATA)
976 idx = 0;
977 else
978 idx = 1;
979 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
980 bus_width = bus_widths[idx];
981 if (bus_width == MMC_BUS_WIDTH_1)
982 ddr = 0; /* no DDR for 1-bit width */
983 err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
984 ext_csd);
985 if (err)
986 pr_err("%s: power class selection to "
987 "bus width %d failed\n",
988 mmc_hostname(card->host),
989 1 << bus_width);
991 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
992 EXT_CSD_BUS_WIDTH,
993 ext_csd_bits[idx][0],
994 card->ext_csd.generic_cmd6_time);
995 if (!err) {
996 mmc_set_bus_width(card->host, bus_width);
999 * If controller can't handle bus width test,
1000 * compare ext_csd previously read in 1 bit mode
1001 * against ext_csd at new bus width
1003 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
1004 err = mmc_compare_ext_csds(card,
1005 bus_width);
1006 else
1007 err = mmc_bus_test(card, bus_width);
1008 if (!err)
1009 break;
1013 if (!err && ddr) {
1014 err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
1015 ext_csd);
1016 if (err)
1017 pr_err("%s: power class selection to "
1018 "bus width %d ddr %d failed\n",
1019 mmc_hostname(card->host),
1020 1 << bus_width, ddr);
1022 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1023 EXT_CSD_BUS_WIDTH,
1024 ext_csd_bits[idx][1],
1025 card->ext_csd.generic_cmd6_time);
1027 if (err) {
1028 pr_warning("%s: switch to bus width %d ddr %d "
1029 "failed\n", mmc_hostname(card->host),
1030 1 << bus_width, ddr);
1031 goto free_card;
1032 } else if (ddr) {
1034 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1035 * signaling.
1037 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1039 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1040 * in the JEDEC spec for DDR.
1042 * Do not force change in vccq since we are obviously
1043 * working and no change to vccq is needed.
1045 * WARNING: eMMC rules are NOT the same as SD DDR
1047 if (ddr == EXT_CSD_CARD_TYPE_DDR_1_2V) {
1048 err = mmc_set_signal_voltage(host,
1049 MMC_SIGNAL_VOLTAGE_120, 0);
1050 if (err)
1051 goto err;
1053 mmc_card_set_ddr_mode(card);
1054 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1055 mmc_set_bus_width(card->host, bus_width);
1060 * If cache size is higher than 0, this indicates
1061 * the existence of cache and it can be turned on.
1063 if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1064 card->ext_csd.cache_size > 0) {
1065 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1066 EXT_CSD_CACHE_CTRL, 1, 0);
1067 if (err && err != -EBADMSG)
1068 goto free_card;
1071 * Only if no error, cache is turned on successfully.
1073 card->ext_csd.cache_ctrl = err ? 0 : 1;
1076 if (!oldcard)
1077 host->card = card;
1079 mmc_free_ext_csd(ext_csd);
1080 return 0;
1082 free_card:
1083 if (!oldcard)
1084 mmc_remove_card(card);
1085 err:
1086 mmc_free_ext_csd(ext_csd);
1088 return err;
1092 * Host is being removed. Free up the current card.
1094 static void mmc_remove(struct mmc_host *host)
1096 BUG_ON(!host);
1097 BUG_ON(!host->card);
1099 mmc_remove_card(host->card);
1100 host->card = NULL;
1104 * Card detection callback from host.
1106 static void mmc_detect(struct mmc_host *host)
1108 int err;
1110 BUG_ON(!host);
1111 BUG_ON(!host->card);
1113 mmc_claim_host(host);
1116 * Just check if our card has been removed.
1118 err = mmc_send_status(host->card, NULL);
1120 mmc_release_host(host);
1122 if (err) {
1123 mmc_remove(host);
1125 mmc_claim_host(host);
1126 mmc_detach_bus(host);
1127 mmc_power_off(host);
1128 mmc_release_host(host);
1133 * Suspend callback from host.
1135 static int mmc_suspend(struct mmc_host *host)
1137 int err = 0;
1139 BUG_ON(!host);
1140 BUG_ON(!host->card);
1142 mmc_claim_host(host);
1143 if (mmc_card_can_sleep(host))
1144 err = mmc_card_sleep(host);
1145 else if (!mmc_host_is_spi(host))
1146 mmc_deselect_cards(host);
1147 host->card->state &= ~MMC_STATE_HIGHSPEED;
1148 mmc_release_host(host);
1150 return err;
1154 * Resume callback from host.
1156 * This function tries to determine if the same card is still present
1157 * and, if so, restore all state to it.
1159 static int mmc_resume(struct mmc_host *host)
1161 int err;
1163 BUG_ON(!host);
1164 BUG_ON(!host->card);
1166 mmc_claim_host(host);
1167 err = mmc_init_card(host, host->ocr, host->card);
1168 mmc_release_host(host);
1170 return err;
1173 static int mmc_power_restore(struct mmc_host *host)
1175 int ret;
1177 host->card->state &= ~MMC_STATE_HIGHSPEED;
1178 mmc_claim_host(host);
1179 ret = mmc_init_card(host, host->ocr, host->card);
1180 mmc_release_host(host);
1182 return ret;
1185 static int mmc_sleep(struct mmc_host *host)
1187 struct mmc_card *card = host->card;
1188 int err = -ENOSYS;
1190 if (card && card->ext_csd.rev >= 3) {
1191 err = mmc_card_sleepawake(host, 1);
1192 if (err < 0)
1193 pr_debug("%s: Error %d while putting card into sleep",
1194 mmc_hostname(host), err);
1197 return err;
1200 static int mmc_awake(struct mmc_host *host)
1202 struct mmc_card *card = host->card;
1203 int err = -ENOSYS;
1205 if (card && card->ext_csd.rev >= 3) {
1206 err = mmc_card_sleepawake(host, 0);
1207 if (err < 0)
1208 pr_debug("%s: Error %d while awaking sleeping card",
1209 mmc_hostname(host), err);
1212 return err;
1215 static const struct mmc_bus_ops mmc_ops = {
1216 .awake = mmc_awake,
1217 .sleep = mmc_sleep,
1218 .remove = mmc_remove,
1219 .detect = mmc_detect,
1220 .suspend = NULL,
1221 .resume = NULL,
1222 .power_restore = mmc_power_restore,
1225 static const struct mmc_bus_ops mmc_ops_unsafe = {
1226 .awake = mmc_awake,
1227 .sleep = mmc_sleep,
1228 .remove = mmc_remove,
1229 .detect = mmc_detect,
1230 .suspend = mmc_suspend,
1231 .resume = mmc_resume,
1232 .power_restore = mmc_power_restore,
1235 static void mmc_attach_bus_ops(struct mmc_host *host)
1237 const struct mmc_bus_ops *bus_ops;
1239 if (!mmc_card_is_removable(host))
1240 bus_ops = &mmc_ops_unsafe;
1241 else
1242 bus_ops = &mmc_ops;
1243 mmc_attach_bus(host, bus_ops);
1247 * Starting point for MMC card init.
1249 int mmc_attach_mmc(struct mmc_host *host)
1251 int err;
1252 u32 ocr;
1254 BUG_ON(!host);
1255 WARN_ON(!host->claimed);
1257 /* Set correct bus mode for MMC before attempting attach */
1258 if (!mmc_host_is_spi(host))
1259 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1261 err = mmc_send_op_cond(host, 0, &ocr);
1262 if (err)
1263 return err;
1265 mmc_attach_bus_ops(host);
1266 if (host->ocr_avail_mmc)
1267 host->ocr_avail = host->ocr_avail_mmc;
1270 * We need to get OCR a different way for SPI.
1272 if (mmc_host_is_spi(host)) {
1273 err = mmc_spi_read_ocr(host, 1, &ocr);
1274 if (err)
1275 goto err;
1279 * Sanity check the voltages that the card claims to
1280 * support.
1282 if (ocr & 0x7F) {
1283 pr_warning("%s: card claims to support voltages "
1284 "below the defined range. These will be ignored.\n",
1285 mmc_hostname(host));
1286 ocr &= ~0x7F;
1289 host->ocr = mmc_select_voltage(host, ocr);
1292 * Can we support the voltage of the card?
1294 if (!host->ocr) {
1295 err = -EINVAL;
1296 goto err;
1300 * Detect and init the card.
1302 err = mmc_init_card(host, host->ocr, NULL);
1303 if (err)
1304 goto err;
1306 mmc_release_host(host);
1307 err = mmc_add_card(host->card);
1308 mmc_claim_host(host);
1309 if (err)
1310 goto remove_card;
1312 return 0;
1314 remove_card:
1315 mmc_release_host(host);
1316 mmc_remove_card(host->card);
1317 mmc_claim_host(host);
1318 host->card = NULL;
1319 err:
1320 mmc_detach_bus(host);
1322 pr_err("%s: error %d whilst initialising MMC card\n",
1323 mmc_hostname(host), err);
1325 return err;