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