Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / mmc / core / mmc.c
blob10f5a199f1417b1a4604216b3bebcac373f0539d
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 printk(KERN_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 printk(KERN_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 printk(KERN_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 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));
225 } else {
226 printk(KERN_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;
245 BUG_ON(!card);
247 if (!ext_csd)
248 return 0;
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);
257 err = -EINVAL;
258 goto out;
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);
266 err = -EINVAL;
267 goto out;
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;
291 break;
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;
296 break;
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;
301 break;
302 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
303 card->ext_csd.hs_max_dtr = 52000000;
304 break;
305 case EXT_CSD_CARD_TYPE_26:
306 card->ext_csd.hs_max_dtr = 26000000;
307 break;
308 default:
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
342 * multiples of 128K.
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)) {
365 u8 hc_erase_grp_sz =
366 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
367 u8 hc_wp_grp_sz =
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) +
384 ext_csd[140];
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;
388 } else {
390 * If the enhanced area is not enabled, disable these
391 * device attributes.
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;
411 else
412 card->erased_byte = 0x0;
414 out:
415 return err;
418 static inline void mmc_free_ext_csd(u8 *ext_csd)
420 kfree(ext_csd);
424 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
426 u8 *bw_ext_csd;
427 int err;
429 if (bus_width == MMC_BUS_WIDTH_1)
430 return 0;
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)
436 err = -EINVAL;
437 goto out;
440 if (bus_width == MMC_BUS_WIDTH_1)
441 goto out;
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]));
478 if (err)
479 err = -EINVAL;
481 out:
482 mmc_free_ext_csd(bw_ext_csd);
483 return err;
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[] = {
504 &dev_attr_cid.attr,
505 &dev_attr_csd.attr,
506 &dev_attr_date.attr,
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,
512 &dev_attr_name.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,
517 NULL,
520 static struct attribute_group mmc_std_attr_group = {
521 .attrs = mmc_std_attrs,
524 static const struct attribute_group *mmc_attr_groups[] = {
525 &mmc_std_attr_group,
526 NULL,
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;
543 int err, ddr = 0;
544 u32 cid[4];
545 unsigned int max_dtr;
546 u32 rocr;
547 u8 *ext_csd = NULL;
549 BUG_ON(!host);
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
556 * respond.
558 mmc_go_idle(host);
560 /* The extra bit indicates that we support high capacity */
561 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
562 if (err)
563 goto err;
566 * For SPI, enable CRC as appropriate.
568 if (mmc_host_is_spi(host)) {
569 err = mmc_spi_set_crc(host, use_spi_crc);
570 if (err)
571 goto err;
575 * Fetch CID from card.
577 if (mmc_host_is_spi(host))
578 err = mmc_send_cid(host, cid);
579 else
580 err = mmc_all_send_cid(host, cid);
581 if (err)
582 goto err;
584 if (oldcard) {
585 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
586 err = -ENOENT;
587 goto err;
590 card = oldcard;
591 } else {
593 * Allocate card structure.
595 card = mmc_alloc_card(host, &mmc_type);
596 if (IS_ERR(card)) {
597 err = PTR_ERR(card);
598 goto err;
601 card->type = MMC_TYPE_MMC;
602 card->rca = 1;
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);
611 if (err)
612 goto free_card;
614 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
617 if (!oldcard) {
619 * Fetch CSD from card.
621 err = mmc_send_csd(card, card->raw_csd);
622 if (err)
623 goto free_card;
625 err = mmc_decode_csd(card);
626 if (err)
627 goto free_card;
628 err = mmc_decode_cid(card);
629 if (err)
630 goto free_card;
634 * Select card, as all following commands rely on that.
636 if (!mmc_host_is_spi(host)) {
637 err = mmc_select_card(card);
638 if (err)
639 goto free_card;
642 if (!oldcard) {
644 * Fetch and process extended CSD.
647 err = mmc_get_ext_csd(card, &ext_csd);
648 if (err)
649 goto free_card;
650 err = mmc_read_ext_csd(card, ext_csd);
651 if (err)
652 goto free_card;
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)
675 goto free_card;
677 if (err) {
678 err = 0;
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;
686 } else {
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)
706 goto free_card;
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)
717 goto free_card;
719 if (err) {
720 printk(KERN_WARNING "%s: switch to highspeed failed\n",
721 mmc_hostname(card->host));
722 err = 0;
723 } else {
724 mmc_card_set_highspeed(card);
725 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
730 * Compute bus speed.
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 |
749 MMC_CAP_UHS_DDR50))
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 |
754 MMC_CAP_UHS_DDR50))
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[] = {
770 MMC_BUS_WIDTH_8,
771 MMC_BUS_WIDTH_4,
772 MMC_BUS_WIDTH_1
774 unsigned idx, bus_width = 0;
776 if (host->caps & MMC_CAP_8_BIT_DATA)
777 idx = 0;
778 else
779 idx = 1;
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,
785 EXT_CSD_BUS_WIDTH,
786 ext_csd_bits[idx][0],
788 if (!err) {
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,
798 bus_width);
799 else
800 err = mmc_bus_test(card, bus_width);
801 if (!err)
802 break;
806 if (!err && ddr) {
807 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
808 EXT_CSD_BUS_WIDTH,
809 ext_csd_bits[idx][1],
812 if (err) {
813 printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
814 "failed\n", mmc_hostname(card->host),
815 1 << bus_width, ddr);
816 goto free_card;
817 } else if (ddr) {
819 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
820 * signaling.
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);
835 if (err)
836 goto err;
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);
844 if (!oldcard)
845 host->card = card;
847 mmc_free_ext_csd(ext_csd);
848 return 0;
850 free_card:
851 if (!oldcard)
852 mmc_remove_card(card);
853 err:
854 mmc_free_ext_csd(ext_csd);
856 return err;
860 * Host is being removed. Free up the current card.
862 static void mmc_remove(struct mmc_host *host)
864 BUG_ON(!host);
865 BUG_ON(!host->card);
867 mmc_remove_card(host->card);
868 host->card = NULL;
872 * Card detection callback from host.
874 static void mmc_detect(struct mmc_host *host)
876 int err;
878 BUG_ON(!host);
879 BUG_ON(!host->card);
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);
890 if (err) {
891 mmc_remove(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)
904 BUG_ON(!host);
905 BUG_ON(!host->card);
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);
913 return 0;
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)
924 int err;
926 BUG_ON(!host);
927 BUG_ON(!host->card);
929 mmc_claim_host(host);
930 err = mmc_init_card(host, host->ocr, host->card);
931 mmc_release_host(host);
933 return err;
936 static int mmc_power_restore(struct mmc_host *host)
938 int ret;
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);
945 return ret;
948 static int mmc_sleep(struct mmc_host *host)
950 struct mmc_card *card = host->card;
951 int err = -ENOSYS;
953 if (card && card->ext_csd.rev >= 3) {
954 err = mmc_card_sleepawake(host, 1);
955 if (err < 0)
956 pr_debug("%s: Error %d while putting card into sleep",
957 mmc_hostname(host), err);
960 return err;
963 static int mmc_awake(struct mmc_host *host)
965 struct mmc_card *card = host->card;
966 int err = -ENOSYS;
968 if (card && card->ext_csd.rev >= 3) {
969 err = mmc_card_sleepawake(host, 0);
970 if (err < 0)
971 pr_debug("%s: Error %d while awaking sleeping card",
972 mmc_hostname(host), err);
975 return err;
978 static const struct mmc_bus_ops mmc_ops = {
979 .awake = mmc_awake,
980 .sleep = mmc_sleep,
981 .remove = mmc_remove,
982 .detect = mmc_detect,
983 .suspend = NULL,
984 .resume = NULL,
985 .power_restore = mmc_power_restore,
988 static const struct mmc_bus_ops mmc_ops_unsafe = {
989 .awake = mmc_awake,
990 .sleep = mmc_sleep,
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;
1004 else
1005 bus_ops = &mmc_ops;
1006 mmc_attach_bus(host, bus_ops);
1010 * Starting point for MMC card init.
1012 int mmc_attach_mmc(struct mmc_host *host)
1014 int err;
1015 u32 ocr;
1017 BUG_ON(!host);
1018 WARN_ON(!host->claimed);
1020 err = mmc_send_op_cond(host, 0, &ocr);
1021 if (err)
1022 return err;
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);
1033 if (err)
1034 goto err;
1038 * Sanity check the voltages that the card claims to
1039 * support.
1041 if (ocr & 0x7F) {
1042 printk(KERN_WARNING "%s: card claims to support voltages "
1043 "below the defined range. These will be ignored.\n",
1044 mmc_hostname(host));
1045 ocr &= ~0x7F;
1048 host->ocr = mmc_select_voltage(host, ocr);
1051 * Can we support the voltage of the card?
1053 if (!host->ocr) {
1054 err = -EINVAL;
1055 goto err;
1059 * Detect and init the card.
1061 err = mmc_init_card(host, host->ocr, NULL);
1062 if (err)
1063 goto err;
1065 mmc_release_host(host);
1066 err = mmc_add_card(host->card);
1067 mmc_claim_host(host);
1068 if (err)
1069 goto remove_card;
1071 return 0;
1073 remove_card:
1074 mmc_release_host(host);
1075 mmc_remove_card(host->card);
1076 mmc_claim_host(host);
1077 host->card = NULL;
1078 err:
1079 mmc_detach_bus(host);
1081 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
1082 mmc_hostname(host), err);
1084 return err;