Merge tag 'sound-fix-3.17-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / core / mmc.c
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 #include <linux/pm_runtime.h>
17
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/mmc.h>
21
22 #include "core.h"
23 #include "bus.h"
24 #include "mmc_ops.h"
25 #include "sd_ops.h"
26
27 static const unsigned int tran_exp[] = {
28         10000,          100000,         1000000,        10000000,
29         0,              0,              0,              0
30 };
31
32 static const unsigned char tran_mant[] = {
33         0,      10,     12,     13,     15,     20,     25,     30,
34         35,     40,     45,     50,     55,     60,     70,     80,
35 };
36
37 static const unsigned int tacc_exp[] = {
38         1,      10,     100,    1000,   10000,  100000, 1000000, 10000000,
39 };
40
41 static const unsigned int tacc_mant[] = {
42         0,      10,     12,     13,     15,     20,     25,     30,
43         35,     40,     45,     50,     55,     60,     70,     80,
44 };
45
46 #define UNSTUFF_BITS(resp,start,size)                                   \
47         ({                                                              \
48                 const int __size = size;                                \
49                 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
50                 const int __off = 3 - ((start) / 32);                   \
51                 const int __shft = (start) & 31;                        \
52                 u32 __res;                                              \
53                                                                         \
54                 __res = resp[__off] >> __shft;                          \
55                 if (__size + __shft > 32)                               \
56                         __res |= resp[__off-1] << ((32 - __shft) % 32); \
57                 __res & __mask;                                         \
58         })
59
60 /*
61  * Given the decoded CSD structure, decode the raw CID to our CID structure.
62  */
63 static int mmc_decode_cid(struct mmc_card *card)
64 {
65         u32 *resp = card->raw_cid;
66
67         /*
68          * The selection of the format here is based upon published
69          * specs from sandisk and from what people have reported.
70          */
71         switch (card->csd.mmca_vsn) {
72         case 0: /* MMC v1.0 - v1.2 */
73         case 1: /* MMC v1.4 */
74                 card->cid.manfid        = UNSTUFF_BITS(resp, 104, 24);
75                 card->cid.prod_name[0]  = UNSTUFF_BITS(resp, 96, 8);
76                 card->cid.prod_name[1]  = UNSTUFF_BITS(resp, 88, 8);
77                 card->cid.prod_name[2]  = UNSTUFF_BITS(resp, 80, 8);
78                 card->cid.prod_name[3]  = UNSTUFF_BITS(resp, 72, 8);
79                 card->cid.prod_name[4]  = UNSTUFF_BITS(resp, 64, 8);
80                 card->cid.prod_name[5]  = UNSTUFF_BITS(resp, 56, 8);
81                 card->cid.prod_name[6]  = UNSTUFF_BITS(resp, 48, 8);
82                 card->cid.hwrev         = UNSTUFF_BITS(resp, 44, 4);
83                 card->cid.fwrev         = UNSTUFF_BITS(resp, 40, 4);
84                 card->cid.serial        = UNSTUFF_BITS(resp, 16, 24);
85                 card->cid.month         = UNSTUFF_BITS(resp, 12, 4);
86                 card->cid.year          = UNSTUFF_BITS(resp, 8, 4) + 1997;
87                 break;
88
89         case 2: /* MMC v2.0 - v2.2 */
90         case 3: /* MMC v3.1 - v3.3 */
91         case 4: /* MMC v4 */
92                 card->cid.manfid        = UNSTUFF_BITS(resp, 120, 8);
93                 card->cid.oemid         = UNSTUFF_BITS(resp, 104, 16);
94                 card->cid.prod_name[0]  = UNSTUFF_BITS(resp, 96, 8);
95                 card->cid.prod_name[1]  = UNSTUFF_BITS(resp, 88, 8);
96                 card->cid.prod_name[2]  = UNSTUFF_BITS(resp, 80, 8);
97                 card->cid.prod_name[3]  = UNSTUFF_BITS(resp, 72, 8);
98                 card->cid.prod_name[4]  = UNSTUFF_BITS(resp, 64, 8);
99                 card->cid.prod_name[5]  = UNSTUFF_BITS(resp, 56, 8);
100                 card->cid.prv           = UNSTUFF_BITS(resp, 48, 8);
101                 card->cid.serial        = UNSTUFF_BITS(resp, 16, 32);
102                 card->cid.month         = UNSTUFF_BITS(resp, 12, 4);
103                 card->cid.year          = UNSTUFF_BITS(resp, 8, 4) + 1997;
104                 break;
105
106         default:
107                 pr_err("%s: card has unknown MMCA version %d\n",
108                         mmc_hostname(card->host), card->csd.mmca_vsn);
109                 return -EINVAL;
110         }
111
112         return 0;
113 }
114
115 static void mmc_set_erase_size(struct mmc_card *card)
116 {
117         if (card->ext_csd.erase_group_def & 1)
118                 card->erase_size = card->ext_csd.hc_erase_size;
119         else
120                 card->erase_size = card->csd.erase_size;
121
122         mmc_init_erase(card);
123 }
124
125 /*
126  * Given a 128-bit response, decode to our card CSD structure.
127  */
128 static int mmc_decode_csd(struct mmc_card *card)
129 {
130         struct mmc_csd *csd = &card->csd;
131         unsigned int e, m, a, b;
132         u32 *resp = card->raw_csd;
133
134         /*
135          * We only understand CSD structure v1.1 and v1.2.
136          * v1.2 has extra information in bits 15, 11 and 10.
137          * We also support eMMC v4.4 & v4.41.
138          */
139         csd->structure = UNSTUFF_BITS(resp, 126, 2);
140         if (csd->structure == 0) {
141                 pr_err("%s: unrecognised CSD structure version %d\n",
142                         mmc_hostname(card->host), csd->structure);
143                 return -EINVAL;
144         }
145
146         csd->mmca_vsn    = UNSTUFF_BITS(resp, 122, 4);
147         m = UNSTUFF_BITS(resp, 115, 4);
148         e = UNSTUFF_BITS(resp, 112, 3);
149         csd->tacc_ns     = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
150         csd->tacc_clks   = UNSTUFF_BITS(resp, 104, 8) * 100;
151
152         m = UNSTUFF_BITS(resp, 99, 4);
153         e = UNSTUFF_BITS(resp, 96, 3);
154         csd->max_dtr      = tran_exp[e] * tran_mant[m];
155         csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
156
157         e = UNSTUFF_BITS(resp, 47, 3);
158         m = UNSTUFF_BITS(resp, 62, 12);
159         csd->capacity     = (1 + m) << (e + 2);
160
161         csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
162         csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
163         csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
164         csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
165         csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
166         csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
167         csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
168
169         if (csd->write_blkbits >= 9) {
170                 a = UNSTUFF_BITS(resp, 42, 5);
171                 b = UNSTUFF_BITS(resp, 37, 5);
172                 csd->erase_size = (a + 1) * (b + 1);
173                 csd->erase_size <<= csd->write_blkbits - 9;
174         }
175
176         return 0;
177 }
178
179 /*
180  * Read extended CSD.
181  */
182 static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
183 {
184         int err;
185         u8 *ext_csd;
186
187         BUG_ON(!card);
188         BUG_ON(!new_ext_csd);
189
190         *new_ext_csd = NULL;
191
192         if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
193                 return 0;
194
195         /*
196          * As the ext_csd is so large and mostly unused, we don't store the
197          * raw block in mmc_card.
198          */
199         ext_csd = kmalloc(512, GFP_KERNEL);
200         if (!ext_csd) {
201                 pr_err("%s: could not allocate a buffer to "
202                         "receive the ext_csd.\n", mmc_hostname(card->host));
203                 return -ENOMEM;
204         }
205
206         err = mmc_send_ext_csd(card, ext_csd);
207         if (err) {
208                 kfree(ext_csd);
209                 *new_ext_csd = NULL;
210
211                 /* If the host or the card can't do the switch,
212                  * fail more gracefully. */
213                 if ((err != -EINVAL)
214                  && (err != -ENOSYS)
215                  && (err != -EFAULT))
216                         return err;
217
218                 /*
219                  * High capacity cards should have this "magic" size
220                  * stored in their CSD.
221                  */
222                 if (card->csd.capacity == (4096 * 512)) {
223                         pr_err("%s: unable to read EXT_CSD "
224                                 "on a possible high capacity card. "
225                                 "Card will be ignored.\n",
226                                 mmc_hostname(card->host));
227                 } else {
228                         pr_warning("%s: unable to read "
229                                 "EXT_CSD, performance might "
230                                 "suffer.\n",
231                                 mmc_hostname(card->host));
232                         err = 0;
233                 }
234         } else
235                 *new_ext_csd = ext_csd;
236
237         return err;
238 }
239
240 static void mmc_select_card_type(struct mmc_card *card)
241 {
242         struct mmc_host *host = card->host;
243         u8 card_type = card->ext_csd.raw_card_type;
244         u32 caps = host->caps, caps2 = host->caps2;
245         unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
246         unsigned int avail_type = 0;
247
248         if (caps & MMC_CAP_MMC_HIGHSPEED &&
249             card_type & EXT_CSD_CARD_TYPE_HS_26) {
250                 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
251                 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
252         }
253
254         if (caps & MMC_CAP_MMC_HIGHSPEED &&
255             card_type & EXT_CSD_CARD_TYPE_HS_52) {
256                 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
257                 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
258         }
259
260         if (caps & MMC_CAP_1_8V_DDR &&
261             card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
262                 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
263                 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
264         }
265
266         if (caps & MMC_CAP_1_2V_DDR &&
267             card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
268                 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
269                 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
270         }
271
272         if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
273             card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
274                 hs200_max_dtr = MMC_HS200_MAX_DTR;
275                 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
276         }
277
278         if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
279             card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
280                 hs200_max_dtr = MMC_HS200_MAX_DTR;
281                 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
282         }
283
284         if (caps2 & MMC_CAP2_HS400_1_8V &&
285             card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
286                 hs200_max_dtr = MMC_HS200_MAX_DTR;
287                 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
288         }
289
290         if (caps2 & MMC_CAP2_HS400_1_2V &&
291             card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
292                 hs200_max_dtr = MMC_HS200_MAX_DTR;
293                 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
294         }
295
296         card->ext_csd.hs_max_dtr = hs_max_dtr;
297         card->ext_csd.hs200_max_dtr = hs200_max_dtr;
298         card->mmc_avail_type = avail_type;
299 }
300
301 /*
302  * Decode extended CSD.
303  */
304 static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
305 {
306         int err = 0, idx;
307         unsigned int part_size;
308         u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
309
310         BUG_ON(!card);
311
312         if (!ext_csd)
313                 return 0;
314
315         /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
316         card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
317         if (card->csd.structure == 3) {
318                 if (card->ext_csd.raw_ext_csd_structure > 2) {
319                         pr_err("%s: unrecognised EXT_CSD structure "
320                                 "version %d\n", mmc_hostname(card->host),
321                                         card->ext_csd.raw_ext_csd_structure);
322                         err = -EINVAL;
323                         goto out;
324                 }
325         }
326
327         /*
328          * The EXT_CSD format is meant to be forward compatible. As long
329          * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
330          * are authorized, see JEDEC JESD84-B50 section B.8.
331          */
332         card->ext_csd.rev = ext_csd[EXT_CSD_REV];
333
334         card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
335         card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
336         card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
337         card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
338         if (card->ext_csd.rev >= 2) {
339                 card->ext_csd.sectors =
340                         ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
341                         ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
342                         ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
343                         ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
344
345                 /* Cards with density > 2GiB are sector addressed */
346                 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
347                         mmc_card_set_blockaddr(card);
348         }
349
350         card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
351         mmc_select_card_type(card);
352
353         card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
354         card->ext_csd.raw_erase_timeout_mult =
355                 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
356         card->ext_csd.raw_hc_erase_grp_size =
357                 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
358         if (card->ext_csd.rev >= 3) {
359                 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
360                 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
361
362                 /* EXT_CSD value is in units of 10ms, but we store in ms */
363                 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
364
365                 /* Sleep / awake timeout in 100ns units */
366                 if (sa_shift > 0 && sa_shift <= 0x17)
367                         card->ext_csd.sa_timeout =
368                                         1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
369                 card->ext_csd.erase_group_def =
370                         ext_csd[EXT_CSD_ERASE_GROUP_DEF];
371                 card->ext_csd.hc_erase_timeout = 300 *
372                         ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
373                 card->ext_csd.hc_erase_size =
374                         ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
375
376                 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
377
378                 /*
379                  * There are two boot regions of equal size, defined in
380                  * multiples of 128K.
381                  */
382                 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
383                         for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
384                                 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
385                                 mmc_part_add(card, part_size,
386                                         EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
387                                         "boot%d", idx, true,
388                                         MMC_BLK_DATA_AREA_BOOT);
389                         }
390                 }
391         }
392
393         card->ext_csd.raw_hc_erase_gap_size =
394                 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
395         card->ext_csd.raw_sec_trim_mult =
396                 ext_csd[EXT_CSD_SEC_TRIM_MULT];
397         card->ext_csd.raw_sec_erase_mult =
398                 ext_csd[EXT_CSD_SEC_ERASE_MULT];
399         card->ext_csd.raw_sec_feature_support =
400                 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
401         card->ext_csd.raw_trim_mult =
402                 ext_csd[EXT_CSD_TRIM_MULT];
403         card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
404         if (card->ext_csd.rev >= 4) {
405                 /*
406                  * Enhanced area feature support -- check whether the eMMC
407                  * card has the Enhanced area enabled.  If so, export enhanced
408                  * area offset and size to user by adding sysfs interface.
409                  */
410                 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
411                     (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
412                         hc_erase_grp_sz =
413                                 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
414                         hc_wp_grp_sz =
415                                 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
416
417                         card->ext_csd.enhanced_area_en = 1;
418                         /*
419                          * calculate the enhanced data area offset, in bytes
420                          */
421                         card->ext_csd.enhanced_area_offset =
422                                 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
423                                 (ext_csd[137] << 8) + ext_csd[136];
424                         if (mmc_card_blockaddr(card))
425                                 card->ext_csd.enhanced_area_offset <<= 9;
426                         /*
427                          * calculate the enhanced data area size, in kilobytes
428                          */
429                         card->ext_csd.enhanced_area_size =
430                                 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
431                                 ext_csd[140];
432                         card->ext_csd.enhanced_area_size *=
433                                 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
434                         card->ext_csd.enhanced_area_size <<= 9;
435                 } else {
436                         /*
437                          * If the enhanced area is not enabled, disable these
438                          * device attributes.
439                          */
440                         card->ext_csd.enhanced_area_offset = -EINVAL;
441                         card->ext_csd.enhanced_area_size = -EINVAL;
442                 }
443
444                 /*
445                  * General purpose partition feature support --
446                  * If ext_csd has the size of general purpose partitions,
447                  * set size, part_cfg, partition name in mmc_part.
448                  */
449                 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
450                         EXT_CSD_PART_SUPPORT_PART_EN) {
451                         if (card->ext_csd.enhanced_area_en != 1) {
452                                 hc_erase_grp_sz =
453                                         ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
454                                 hc_wp_grp_sz =
455                                         ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
456
457                                 card->ext_csd.enhanced_area_en = 1;
458                         }
459
460                         for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
461                                 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
462                                 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
463                                 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
464                                         continue;
465                                 part_size =
466                                 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
467                                         << 16) +
468                                 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
469                                         << 8) +
470                                 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
471                                 part_size *= (size_t)(hc_erase_grp_sz *
472                                         hc_wp_grp_sz);
473                                 mmc_part_add(card, part_size << 19,
474                                         EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
475                                         "gp%d", idx, false,
476                                         MMC_BLK_DATA_AREA_GP);
477                         }
478                 }
479                 card->ext_csd.sec_trim_mult =
480                         ext_csd[EXT_CSD_SEC_TRIM_MULT];
481                 card->ext_csd.sec_erase_mult =
482                         ext_csd[EXT_CSD_SEC_ERASE_MULT];
483                 card->ext_csd.sec_feature_support =
484                         ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
485                 card->ext_csd.trim_timeout = 300 *
486                         ext_csd[EXT_CSD_TRIM_MULT];
487
488                 /*
489                  * Note that the call to mmc_part_add above defaults to read
490                  * only. If this default assumption is changed, the call must
491                  * take into account the value of boot_locked below.
492                  */
493                 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
494                 card->ext_csd.boot_ro_lockable = true;
495
496                 /* Save power class values */
497                 card->ext_csd.raw_pwr_cl_52_195 =
498                         ext_csd[EXT_CSD_PWR_CL_52_195];
499                 card->ext_csd.raw_pwr_cl_26_195 =
500                         ext_csd[EXT_CSD_PWR_CL_26_195];
501                 card->ext_csd.raw_pwr_cl_52_360 =
502                         ext_csd[EXT_CSD_PWR_CL_52_360];
503                 card->ext_csd.raw_pwr_cl_26_360 =
504                         ext_csd[EXT_CSD_PWR_CL_26_360];
505                 card->ext_csd.raw_pwr_cl_200_195 =
506                         ext_csd[EXT_CSD_PWR_CL_200_195];
507                 card->ext_csd.raw_pwr_cl_200_360 =
508                         ext_csd[EXT_CSD_PWR_CL_200_360];
509                 card->ext_csd.raw_pwr_cl_ddr_52_195 =
510                         ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
511                 card->ext_csd.raw_pwr_cl_ddr_52_360 =
512                         ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
513                 card->ext_csd.raw_pwr_cl_ddr_200_360 =
514                         ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
515         }
516
517         if (card->ext_csd.rev >= 5) {
518                 /* Adjust production date as per JEDEC JESD84-B451 */
519                 if (card->cid.year < 2010)
520                         card->cid.year += 16;
521
522                 /* check whether the eMMC card supports BKOPS */
523                 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
524                         card->ext_csd.bkops = 1;
525                         card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
526                         card->ext_csd.raw_bkops_status =
527                                 ext_csd[EXT_CSD_BKOPS_STATUS];
528                         if (!card->ext_csd.bkops_en)
529                                 pr_info("%s: BKOPS_EN bit is not set\n",
530                                         mmc_hostname(card->host));
531                 }
532
533                 /* check whether the eMMC card supports HPI */
534                 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
535                         card->ext_csd.hpi = 1;
536                         if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
537                                 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
538                         else
539                                 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
540                         /*
541                          * Indicate the maximum timeout to close
542                          * a command interrupted by HPI
543                          */
544                         card->ext_csd.out_of_int_time =
545                                 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
546                 }
547
548                 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
549                 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
550
551                 /*
552                  * RPMB regions are defined in multiples of 128K.
553                  */
554                 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
555                 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
556                         mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
557                                 EXT_CSD_PART_CONFIG_ACC_RPMB,
558                                 "rpmb", 0, false,
559                                 MMC_BLK_DATA_AREA_RPMB);
560                 }
561         }
562
563         card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
564         if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
565                 card->erased_byte = 0xFF;
566         else
567                 card->erased_byte = 0x0;
568
569         /* eMMC v4.5 or later */
570         if (card->ext_csd.rev >= 6) {
571                 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
572
573                 card->ext_csd.generic_cmd6_time = 10 *
574                         ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
575                 card->ext_csd.power_off_longtime = 10 *
576                         ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
577
578                 card->ext_csd.cache_size =
579                         ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
580                         ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
581                         ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
582                         ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
583
584                 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
585                         card->ext_csd.data_sector_size = 4096;
586                 else
587                         card->ext_csd.data_sector_size = 512;
588
589                 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
590                     (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
591                         card->ext_csd.data_tag_unit_size =
592                         ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
593                         (card->ext_csd.data_sector_size);
594                 } else {
595                         card->ext_csd.data_tag_unit_size = 0;
596                 }
597
598                 card->ext_csd.max_packed_writes =
599                         ext_csd[EXT_CSD_MAX_PACKED_WRITES];
600                 card->ext_csd.max_packed_reads =
601                         ext_csd[EXT_CSD_MAX_PACKED_READS];
602         } else {
603                 card->ext_csd.data_sector_size = 512;
604         }
605
606 out:
607         return err;
608 }
609
610 static inline void mmc_free_ext_csd(u8 *ext_csd)
611 {
612         kfree(ext_csd);
613 }
614
615
616 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
617 {
618         u8 *bw_ext_csd;
619         int err;
620
621         if (bus_width == MMC_BUS_WIDTH_1)
622                 return 0;
623
624         err = mmc_get_ext_csd(card, &bw_ext_csd);
625
626         if (err || bw_ext_csd == NULL) {
627                 err = -EINVAL;
628                 goto out;
629         }
630
631         /* only compare read only fields */
632         err = !((card->ext_csd.raw_partition_support ==
633                         bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
634                 (card->ext_csd.raw_erased_mem_count ==
635                         bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
636                 (card->ext_csd.rev ==
637                         bw_ext_csd[EXT_CSD_REV]) &&
638                 (card->ext_csd.raw_ext_csd_structure ==
639                         bw_ext_csd[EXT_CSD_STRUCTURE]) &&
640                 (card->ext_csd.raw_card_type ==
641                         bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
642                 (card->ext_csd.raw_s_a_timeout ==
643                         bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
644                 (card->ext_csd.raw_hc_erase_gap_size ==
645                         bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
646                 (card->ext_csd.raw_erase_timeout_mult ==
647                         bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
648                 (card->ext_csd.raw_hc_erase_grp_size ==
649                         bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
650                 (card->ext_csd.raw_sec_trim_mult ==
651                         bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
652                 (card->ext_csd.raw_sec_erase_mult ==
653                         bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
654                 (card->ext_csd.raw_sec_feature_support ==
655                         bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
656                 (card->ext_csd.raw_trim_mult ==
657                         bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
658                 (card->ext_csd.raw_sectors[0] ==
659                         bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
660                 (card->ext_csd.raw_sectors[1] ==
661                         bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
662                 (card->ext_csd.raw_sectors[2] ==
663                         bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
664                 (card->ext_csd.raw_sectors[3] ==
665                         bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
666                 (card->ext_csd.raw_pwr_cl_52_195 ==
667                         bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
668                 (card->ext_csd.raw_pwr_cl_26_195 ==
669                         bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
670                 (card->ext_csd.raw_pwr_cl_52_360 ==
671                         bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
672                 (card->ext_csd.raw_pwr_cl_26_360 ==
673                         bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
674                 (card->ext_csd.raw_pwr_cl_200_195 ==
675                         bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
676                 (card->ext_csd.raw_pwr_cl_200_360 ==
677                         bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
678                 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
679                         bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
680                 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
681                         bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
682                 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
683                         bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
684
685         if (err)
686                 err = -EINVAL;
687
688 out:
689         mmc_free_ext_csd(bw_ext_csd);
690         return err;
691 }
692
693 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
694         card->raw_cid[2], card->raw_cid[3]);
695 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
696         card->raw_csd[2], card->raw_csd[3]);
697 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
698 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
699 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
700 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
701 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
702 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
703 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
704 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
705 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
706 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
707 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
708                 card->ext_csd.enhanced_area_offset);
709 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
710 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
711 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
712
713 static struct attribute *mmc_std_attrs[] = {
714         &dev_attr_cid.attr,
715         &dev_attr_csd.attr,
716         &dev_attr_date.attr,
717         &dev_attr_erase_size.attr,
718         &dev_attr_preferred_erase_size.attr,
719         &dev_attr_fwrev.attr,
720         &dev_attr_hwrev.attr,
721         &dev_attr_manfid.attr,
722         &dev_attr_name.attr,
723         &dev_attr_oemid.attr,
724         &dev_attr_prv.attr,
725         &dev_attr_serial.attr,
726         &dev_attr_enhanced_area_offset.attr,
727         &dev_attr_enhanced_area_size.attr,
728         &dev_attr_raw_rpmb_size_mult.attr,
729         &dev_attr_rel_sectors.attr,
730         NULL,
731 };
732 ATTRIBUTE_GROUPS(mmc_std);
733
734 static struct device_type mmc_type = {
735         .groups = mmc_std_groups,
736 };
737
738 /*
739  * Select the PowerClass for the current bus width
740  * If power class is defined for 4/8 bit bus in the
741  * extended CSD register, select it by executing the
742  * mmc_switch command.
743  */
744 static int __mmc_select_powerclass(struct mmc_card *card,
745                                    unsigned int bus_width)
746 {
747         struct mmc_host *host = card->host;
748         struct mmc_ext_csd *ext_csd = &card->ext_csd;
749         unsigned int pwrclass_val = 0;
750         int err = 0;
751
752         /* Power class selection is supported for versions >= 4.0 */
753         if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
754                 return 0;
755
756         /* Power class values are defined only for 4/8 bit bus */
757         if (bus_width == EXT_CSD_BUS_WIDTH_1)
758                 return 0;
759
760         switch (1 << host->ios.vdd) {
761         case MMC_VDD_165_195:
762                 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
763                         pwrclass_val = ext_csd->raw_pwr_cl_26_195;
764                 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
765                         pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
766                                 ext_csd->raw_pwr_cl_52_195 :
767                                 ext_csd->raw_pwr_cl_ddr_52_195;
768                 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
769                         pwrclass_val = ext_csd->raw_pwr_cl_200_195;
770                 break;
771         case MMC_VDD_27_28:
772         case MMC_VDD_28_29:
773         case MMC_VDD_29_30:
774         case MMC_VDD_30_31:
775         case MMC_VDD_31_32:
776         case MMC_VDD_32_33:
777         case MMC_VDD_33_34:
778         case MMC_VDD_34_35:
779         case MMC_VDD_35_36:
780                 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
781                         pwrclass_val = ext_csd->raw_pwr_cl_26_360;
782                 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
783                         pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
784                                 ext_csd->raw_pwr_cl_52_360 :
785                                 ext_csd->raw_pwr_cl_ddr_52_360;
786                 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
787                         pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
788                                 ext_csd->raw_pwr_cl_ddr_200_360 :
789                                 ext_csd->raw_pwr_cl_200_360;
790                 break;
791         default:
792                 pr_warning("%s: Voltage range not supported "
793                            "for power class.\n", mmc_hostname(host));
794                 return -EINVAL;
795         }
796
797         if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
798                 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
799                                 EXT_CSD_PWR_CL_8BIT_SHIFT;
800         else
801                 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
802                                 EXT_CSD_PWR_CL_4BIT_SHIFT;
803
804         /* If the power class is different from the default value */
805         if (pwrclass_val > 0) {
806                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
807                                  EXT_CSD_POWER_CLASS,
808                                  pwrclass_val,
809                                  card->ext_csd.generic_cmd6_time);
810         }
811
812         return err;
813 }
814
815 static int mmc_select_powerclass(struct mmc_card *card)
816 {
817         struct mmc_host *host = card->host;
818         u32 bus_width, ext_csd_bits;
819         int err, ddr;
820
821         /* Power class selection is supported for versions >= 4.0 */
822         if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
823                 return 0;
824
825         bus_width = host->ios.bus_width;
826         /* Power class values are defined only for 4/8 bit bus */
827         if (bus_width == MMC_BUS_WIDTH_1)
828                 return 0;
829
830         ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
831         if (ddr)
832                 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
833                         EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
834         else
835                 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
836                         EXT_CSD_BUS_WIDTH_8 :  EXT_CSD_BUS_WIDTH_4;
837
838         err = __mmc_select_powerclass(card, ext_csd_bits);
839         if (err)
840                 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
841                         mmc_hostname(host), 1 << bus_width, ddr);
842
843         return err;
844 }
845
846 /*
847  * Set the bus speed for the selected speed mode.
848  */
849 static void mmc_set_bus_speed(struct mmc_card *card)
850 {
851         unsigned int max_dtr = (unsigned int)-1;
852
853         if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
854              max_dtr > card->ext_csd.hs200_max_dtr)
855                 max_dtr = card->ext_csd.hs200_max_dtr;
856         else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
857                 max_dtr = card->ext_csd.hs_max_dtr;
858         else if (max_dtr > card->csd.max_dtr)
859                 max_dtr = card->csd.max_dtr;
860
861         mmc_set_clock(card->host, max_dtr);
862 }
863
864 /*
865  * Select the bus width amoung 4-bit and 8-bit(SDR).
866  * If the bus width is changed successfully, return the selected width value.
867  * Zero is returned instead of error value if the wide width is not supported.
868  */
869 static int mmc_select_bus_width(struct mmc_card *card)
870 {
871         static unsigned ext_csd_bits[] = {
872                 EXT_CSD_BUS_WIDTH_8,
873                 EXT_CSD_BUS_WIDTH_4,
874         };
875         static unsigned bus_widths[] = {
876                 MMC_BUS_WIDTH_8,
877                 MMC_BUS_WIDTH_4,
878         };
879         struct mmc_host *host = card->host;
880         unsigned idx, bus_width = 0;
881         int err = 0;
882
883         if ((card->csd.mmca_vsn < CSD_SPEC_VER_4) &&
884             !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
885                 return 0;
886
887         idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
888
889         /*
890          * Unlike SD, MMC cards dont have a configuration register to notify
891          * supported bus width. So bus test command should be run to identify
892          * the supported bus width or compare the ext csd values of current
893          * bus width and ext csd values of 1 bit mode read earlier.
894          */
895         for (; idx < ARRAY_SIZE(bus_widths); idx++) {
896                 /*
897                  * Host is capable of 8bit transfer, then switch
898                  * the device to work in 8bit transfer mode. If the
899                  * mmc switch command returns error then switch to
900                  * 4bit transfer mode. On success set the corresponding
901                  * bus width on the host.
902                  */
903                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
904                                  EXT_CSD_BUS_WIDTH,
905                                  ext_csd_bits[idx],
906                                  card->ext_csd.generic_cmd6_time);
907                 if (err)
908                         continue;
909
910                 bus_width = bus_widths[idx];
911                 mmc_set_bus_width(host, bus_width);
912
913                 /*
914                  * If controller can't handle bus width test,
915                  * compare ext_csd previously read in 1 bit mode
916                  * against ext_csd at new bus width
917                  */
918                 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
919                         err = mmc_compare_ext_csds(card, bus_width);
920                 else
921                         err = mmc_bus_test(card, bus_width);
922
923                 if (!err) {
924                         err = bus_width;
925                         break;
926                 } else {
927                         pr_warn("%s: switch to bus width %d failed\n",
928                                 mmc_hostname(host), ext_csd_bits[idx]);
929                 }
930         }
931
932         return err;
933 }
934
935 /*
936  * Switch to the high-speed mode
937  */
938 static int mmc_select_hs(struct mmc_card *card)
939 {
940         int err;
941
942         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
943                            EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
944                            card->ext_csd.generic_cmd6_time,
945                            true, true, true);
946         if (!err)
947                 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
948
949         return err;
950 }
951
952 /*
953  * Activate wide bus and DDR if supported.
954  */
955 static int mmc_select_hs_ddr(struct mmc_card *card)
956 {
957         struct mmc_host *host = card->host;
958         u32 bus_width, ext_csd_bits;
959         int err = 0;
960
961         if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
962                 return 0;
963
964         bus_width = host->ios.bus_width;
965         if (bus_width == MMC_BUS_WIDTH_1)
966                 return 0;
967
968         ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
969                 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
970
971         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
972                         EXT_CSD_BUS_WIDTH,
973                         ext_csd_bits,
974                         card->ext_csd.generic_cmd6_time);
975         if (err) {
976                 pr_warn("%s: switch to bus width %d ddr failed\n",
977                         mmc_hostname(host), 1 << bus_width);
978                 return err;
979         }
980
981         /*
982          * eMMC cards can support 3.3V to 1.2V i/o (vccq)
983          * signaling.
984          *
985          * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
986          *
987          * 1.8V vccq at 3.3V core voltage (vcc) is not required
988          * in the JEDEC spec for DDR.
989          *
990          * Do not force change in vccq since we are obviously
991          * working and no change to vccq is needed.
992          *
993          * WARNING: eMMC rules are NOT the same as SD DDR
994          */
995         if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
996                 err = __mmc_set_signal_voltage(host,
997                                 MMC_SIGNAL_VOLTAGE_120);
998                 if (err)
999                         return err;
1000         }
1001
1002         mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1003
1004         return err;
1005 }
1006
1007 static int mmc_select_hs400(struct mmc_card *card)
1008 {
1009         struct mmc_host *host = card->host;
1010         int err = 0;
1011
1012         /*
1013          * HS400 mode requires 8-bit bus width
1014          */
1015         if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1016               host->ios.bus_width == MMC_BUS_WIDTH_8))
1017                 return 0;
1018
1019         /*
1020          * Before switching to dual data rate operation for HS400,
1021          * it is required to convert from HS200 mode to HS mode.
1022          */
1023         mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1024         mmc_set_bus_speed(card);
1025
1026         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1027                            EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1028                            card->ext_csd.generic_cmd6_time,
1029                            true, true, true);
1030         if (err) {
1031                 pr_warn("%s: switch to high-speed from hs200 failed, err:%d\n",
1032                         mmc_hostname(host), err);
1033                 return err;
1034         }
1035
1036         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1037                          EXT_CSD_BUS_WIDTH,
1038                          EXT_CSD_DDR_BUS_WIDTH_8,
1039                          card->ext_csd.generic_cmd6_time);
1040         if (err) {
1041                 pr_warn("%s: switch to bus width for hs400 failed, err:%d\n",
1042                         mmc_hostname(host), err);
1043                 return err;
1044         }
1045
1046         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1047                            EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS400,
1048                            card->ext_csd.generic_cmd6_time,
1049                            true, true, true);
1050         if (err) {
1051                 pr_warn("%s: switch to hs400 failed, err:%d\n",
1052                          mmc_hostname(host), err);
1053                 return err;
1054         }
1055
1056         mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1057         mmc_set_bus_speed(card);
1058
1059         return 0;
1060 }
1061
1062 /*
1063  * For device supporting HS200 mode, the following sequence
1064  * should be done before executing the tuning process.
1065  * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1066  * 2. switch to HS200 mode
1067  * 3. set the clock to > 52Mhz and <=200MHz
1068  */
1069 static int mmc_select_hs200(struct mmc_card *card)
1070 {
1071         struct mmc_host *host = card->host;
1072         int err = -EINVAL;
1073
1074         if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1075                 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1076
1077         if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1078                 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1079
1080         /* If fails try again during next card power cycle */
1081         if (err)
1082                 goto err;
1083
1084         /*
1085          * Set the bus width(4 or 8) with host's support and
1086          * switch to HS200 mode if bus width is set successfully.
1087          */
1088         err = mmc_select_bus_width(card);
1089         if (!IS_ERR_VALUE(err)) {
1090                 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1091                                    EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200,
1092                                    card->ext_csd.generic_cmd6_time,
1093                                    true, true, true);
1094                 if (!err)
1095                         mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1096         }
1097 err:
1098         return err;
1099 }
1100
1101 /*
1102  * Activate High Speed or HS200 mode if supported.
1103  */
1104 static int mmc_select_timing(struct mmc_card *card)
1105 {
1106         int err = 0;
1107
1108         if ((card->csd.mmca_vsn < CSD_SPEC_VER_4 &&
1109              card->ext_csd.hs_max_dtr == 0))
1110                 goto bus_speed;
1111
1112         if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1113                 err = mmc_select_hs200(card);
1114         else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1115                 err = mmc_select_hs(card);
1116
1117         if (err && err != -EBADMSG)
1118                 return err;
1119
1120         if (err) {
1121                 pr_warn("%s: switch to %s failed\n",
1122                         mmc_card_hs(card) ? "high-speed" :
1123                         (mmc_card_hs200(card) ? "hs200" : ""),
1124                         mmc_hostname(card->host));
1125                 err = 0;
1126         }
1127
1128 bus_speed:
1129         /*
1130          * Set the bus speed to the selected bus timing.
1131          * If timing is not selected, backward compatible is the default.
1132          */
1133         mmc_set_bus_speed(card);
1134         return err;
1135 }
1136
1137 /*
1138  * Execute tuning sequence to seek the proper bus operating
1139  * conditions for HS200 and HS400, which sends CMD21 to the device.
1140  */
1141 static int mmc_hs200_tuning(struct mmc_card *card)
1142 {
1143         struct mmc_host *host = card->host;
1144         int err = 0;
1145
1146         /*
1147          * Timing should be adjusted to the HS400 target
1148          * operation frequency for tuning process
1149          */
1150         if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1151             host->ios.bus_width == MMC_BUS_WIDTH_8)
1152                 if (host->ops->prepare_hs400_tuning)
1153                         host->ops->prepare_hs400_tuning(host, &host->ios);
1154
1155         if (host->ops->execute_tuning) {
1156                 mmc_host_clk_hold(host);
1157                 err = host->ops->execute_tuning(host,
1158                                 MMC_SEND_TUNING_BLOCK_HS200);
1159                 mmc_host_clk_release(host);
1160
1161                 if (err)
1162                         pr_warn("%s: tuning execution failed\n",
1163                                 mmc_hostname(host));
1164         }
1165
1166         return err;
1167 }
1168
1169 /*
1170  * Handle the detection and initialisation of a card.
1171  *
1172  * In the case of a resume, "oldcard" will contain the card
1173  * we're trying to reinitialise.
1174  */
1175 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1176         struct mmc_card *oldcard)
1177 {
1178         struct mmc_card *card;
1179         int err;
1180         u32 cid[4];
1181         u32 rocr;
1182         u8 *ext_csd = NULL;
1183
1184         BUG_ON(!host);
1185         WARN_ON(!host->claimed);
1186
1187         /* Set correct bus mode for MMC before attempting init */
1188         if (!mmc_host_is_spi(host))
1189                 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1190
1191         /*
1192          * Since we're changing the OCR value, we seem to
1193          * need to tell some cards to go back to the idle
1194          * state.  We wait 1ms to give cards time to
1195          * respond.
1196          * mmc_go_idle is needed for eMMC that are asleep
1197          */
1198         mmc_go_idle(host);
1199
1200         /* The extra bit indicates that we support high capacity */
1201         err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1202         if (err)
1203                 goto err;
1204
1205         /*
1206          * For SPI, enable CRC as appropriate.
1207          */
1208         if (mmc_host_is_spi(host)) {
1209                 err = mmc_spi_set_crc(host, use_spi_crc);
1210                 if (err)
1211                         goto err;
1212         }
1213
1214         /*
1215          * Fetch CID from card.
1216          */
1217         if (mmc_host_is_spi(host))
1218                 err = mmc_send_cid(host, cid);
1219         else
1220                 err = mmc_all_send_cid(host, cid);
1221         if (err)
1222                 goto err;
1223
1224         if (oldcard) {
1225                 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1226                         err = -ENOENT;
1227                         goto err;
1228                 }
1229
1230                 card = oldcard;
1231         } else {
1232                 /*
1233                  * Allocate card structure.
1234                  */
1235                 card = mmc_alloc_card(host, &mmc_type);
1236                 if (IS_ERR(card)) {
1237                         err = PTR_ERR(card);
1238                         goto err;
1239                 }
1240
1241                 card->ocr = ocr;
1242                 card->type = MMC_TYPE_MMC;
1243                 card->rca = 1;
1244                 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1245         }
1246
1247         /*
1248          * For native busses:  set card RCA and quit open drain mode.
1249          */
1250         if (!mmc_host_is_spi(host)) {
1251                 err = mmc_set_relative_addr(card);
1252                 if (err)
1253                         goto free_card;
1254
1255                 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1256         }
1257
1258         if (!oldcard) {
1259                 /*
1260                  * Fetch CSD from card.
1261                  */
1262                 err = mmc_send_csd(card, card->raw_csd);
1263                 if (err)
1264                         goto free_card;
1265
1266                 err = mmc_decode_csd(card);
1267                 if (err)
1268                         goto free_card;
1269                 err = mmc_decode_cid(card);
1270                 if (err)
1271                         goto free_card;
1272         }
1273
1274         /*
1275          * Select card, as all following commands rely on that.
1276          */
1277         if (!mmc_host_is_spi(host)) {
1278                 err = mmc_select_card(card);
1279                 if (err)
1280                         goto free_card;
1281         }
1282
1283         if (!oldcard) {
1284                 /*
1285                  * Fetch and process extended CSD.
1286                  */
1287
1288                 err = mmc_get_ext_csd(card, &ext_csd);
1289                 if (err)
1290                         goto free_card;
1291                 err = mmc_read_ext_csd(card, ext_csd);
1292                 if (err)
1293                         goto free_card;
1294
1295                 /* If doing byte addressing, check if required to do sector
1296                  * addressing.  Handle the case of <2GB cards needing sector
1297                  * addressing.  See section 8.1 JEDEC Standard JED84-A441;
1298                  * ocr register has bit 30 set for sector addressing.
1299                  */
1300                 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
1301                         mmc_card_set_blockaddr(card);
1302
1303                 /* Erase size depends on CSD and Extended CSD */
1304                 mmc_set_erase_size(card);
1305         }
1306
1307         /*
1308          * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
1309          * bit.  This bit will be lost every time after a reset or power off.
1310          */
1311         if (card->ext_csd.enhanced_area_en ||
1312             (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
1313                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1314                                  EXT_CSD_ERASE_GROUP_DEF, 1,
1315                                  card->ext_csd.generic_cmd6_time);
1316
1317                 if (err && err != -EBADMSG)
1318                         goto free_card;
1319
1320                 if (err) {
1321                         err = 0;
1322                         /*
1323                          * Just disable enhanced area off & sz
1324                          * will try to enable ERASE_GROUP_DEF
1325                          * during next time reinit
1326                          */
1327                         card->ext_csd.enhanced_area_offset = -EINVAL;
1328                         card->ext_csd.enhanced_area_size = -EINVAL;
1329                 } else {
1330                         card->ext_csd.erase_group_def = 1;
1331                         /*
1332                          * enable ERASE_GRP_DEF successfully.
1333                          * This will affect the erase size, so
1334                          * here need to reset erase size
1335                          */
1336                         mmc_set_erase_size(card);
1337                 }
1338         }
1339
1340         /*
1341          * Ensure eMMC user default partition is enabled
1342          */
1343         if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1344                 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1345                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1346                                  card->ext_csd.part_config,
1347                                  card->ext_csd.part_time);
1348                 if (err && err != -EBADMSG)
1349                         goto free_card;
1350         }
1351
1352         /*
1353          * Enable power_off_notification byte in the ext_csd register
1354          */
1355         if (card->ext_csd.rev >= 6) {
1356                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1357                                  EXT_CSD_POWER_OFF_NOTIFICATION,
1358                                  EXT_CSD_POWER_ON,
1359                                  card->ext_csd.generic_cmd6_time);
1360                 if (err && err != -EBADMSG)
1361                         goto free_card;
1362
1363                 /*
1364                  * The err can be -EBADMSG or 0,
1365                  * so check for success and update the flag
1366                  */
1367                 if (!err)
1368                         card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1369         }
1370
1371         /*
1372          * Select timing interface
1373          */
1374         err = mmc_select_timing(card);
1375         if (err)
1376                 goto free_card;
1377
1378         if (mmc_card_hs200(card)) {
1379                 err = mmc_hs200_tuning(card);
1380                 if (err)
1381                         goto err;
1382
1383                 err = mmc_select_hs400(card);
1384                 if (err)
1385                         goto err;
1386         } else if (mmc_card_hs(card)) {
1387                 /* Select the desired bus width optionally */
1388                 err = mmc_select_bus_width(card);
1389                 if (!IS_ERR_VALUE(err)) {
1390                         err = mmc_select_hs_ddr(card);
1391                         if (err)
1392                                 goto err;
1393                 }
1394         }
1395
1396         /*
1397          * Choose the power class with selected bus interface
1398          */
1399         mmc_select_powerclass(card);
1400
1401         /*
1402          * Enable HPI feature (if supported)
1403          */
1404         if (card->ext_csd.hpi) {
1405                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1406                                 EXT_CSD_HPI_MGMT, 1,
1407                                 card->ext_csd.generic_cmd6_time);
1408                 if (err && err != -EBADMSG)
1409                         goto free_card;
1410                 if (err) {
1411                         pr_warning("%s: Enabling HPI failed\n",
1412                                    mmc_hostname(card->host));
1413                         err = 0;
1414                 } else
1415                         card->ext_csd.hpi_en = 1;
1416         }
1417
1418         /*
1419          * If cache size is higher than 0, this indicates
1420          * the existence of cache and it can be turned on.
1421          */
1422         if (card->ext_csd.cache_size > 0) {
1423                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1424                                 EXT_CSD_CACHE_CTRL, 1,
1425                                 card->ext_csd.generic_cmd6_time);
1426                 if (err && err != -EBADMSG)
1427                         goto free_card;
1428
1429                 /*
1430                  * Only if no error, cache is turned on successfully.
1431                  */
1432                 if (err) {
1433                         pr_warning("%s: Cache is supported, "
1434                                         "but failed to turn on (%d)\n",
1435                                         mmc_hostname(card->host), err);
1436                         card->ext_csd.cache_ctrl = 0;
1437                         err = 0;
1438                 } else {
1439                         card->ext_csd.cache_ctrl = 1;
1440                 }
1441         }
1442
1443         /*
1444          * The mandatory minimum values are defined for packed command.
1445          * read: 5, write: 3
1446          */
1447         if (card->ext_csd.max_packed_writes >= 3 &&
1448             card->ext_csd.max_packed_reads >= 5 &&
1449             host->caps2 & MMC_CAP2_PACKED_CMD) {
1450                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1451                                 EXT_CSD_EXP_EVENTS_CTRL,
1452                                 EXT_CSD_PACKED_EVENT_EN,
1453                                 card->ext_csd.generic_cmd6_time);
1454                 if (err && err != -EBADMSG)
1455                         goto free_card;
1456                 if (err) {
1457                         pr_warn("%s: Enabling packed event failed\n",
1458                                 mmc_hostname(card->host));
1459                         card->ext_csd.packed_event_en = 0;
1460                         err = 0;
1461                 } else {
1462                         card->ext_csd.packed_event_en = 1;
1463                 }
1464         }
1465
1466         if (!oldcard)
1467                 host->card = card;
1468
1469         mmc_free_ext_csd(ext_csd);
1470         return 0;
1471
1472 free_card:
1473         if (!oldcard)
1474                 mmc_remove_card(card);
1475 err:
1476         mmc_free_ext_csd(ext_csd);
1477
1478         return err;
1479 }
1480
1481 static int mmc_can_sleep(struct mmc_card *card)
1482 {
1483         return (card && card->ext_csd.rev >= 3);
1484 }
1485
1486 static int mmc_sleep(struct mmc_host *host)
1487 {
1488         struct mmc_command cmd = {0};
1489         struct mmc_card *card = host->card;
1490         unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1491         int err;
1492
1493         err = mmc_deselect_cards(host);
1494         if (err)
1495                 return err;
1496
1497         cmd.opcode = MMC_SLEEP_AWAKE;
1498         cmd.arg = card->rca << 16;
1499         cmd.arg |= 1 << 15;
1500
1501         /*
1502          * If the max_busy_timeout of the host is specified, validate it against
1503          * the sleep cmd timeout. A failure means we need to prevent the host
1504          * from doing hw busy detection, which is done by converting to a R1
1505          * response instead of a R1B.
1506          */
1507         if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1508                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1509         } else {
1510                 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1511                 cmd.busy_timeout = timeout_ms;
1512         }
1513
1514         err = mmc_wait_for_cmd(host, &cmd, 0);
1515         if (err)
1516                 return err;
1517
1518         /*
1519          * If the host does not wait while the card signals busy, then we will
1520          * will have to wait the sleep/awake timeout.  Note, we cannot use the
1521          * SEND_STATUS command to poll the status because that command (and most
1522          * others) is invalid while the card sleeps.
1523          */
1524         if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1525                 mmc_delay(timeout_ms);
1526
1527         return err;
1528 }
1529
1530 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1531 {
1532         return card &&
1533                 mmc_card_mmc(card) &&
1534                 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1535 }
1536
1537 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1538 {
1539         unsigned int timeout = card->ext_csd.generic_cmd6_time;
1540         int err;
1541
1542         /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1543         if (notify_type == EXT_CSD_POWER_OFF_LONG)
1544                 timeout = card->ext_csd.power_off_longtime;
1545
1546         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1547                         EXT_CSD_POWER_OFF_NOTIFICATION,
1548                         notify_type, timeout, true, false, false);
1549         if (err)
1550                 pr_err("%s: Power Off Notification timed out, %u\n",
1551                        mmc_hostname(card->host), timeout);
1552
1553         /* Disable the power off notification after the switch operation. */
1554         card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1555
1556         return err;
1557 }
1558
1559 /*
1560  * Host is being removed. Free up the current card.
1561  */
1562 static void mmc_remove(struct mmc_host *host)
1563 {
1564         BUG_ON(!host);
1565         BUG_ON(!host->card);
1566
1567         mmc_remove_card(host->card);
1568         host->card = NULL;
1569 }
1570
1571 /*
1572  * Card detection - card is alive.
1573  */
1574 static int mmc_alive(struct mmc_host *host)
1575 {
1576         return mmc_send_status(host->card, NULL);
1577 }
1578
1579 /*
1580  * Card detection callback from host.
1581  */
1582 static void mmc_detect(struct mmc_host *host)
1583 {
1584         int err;
1585
1586         BUG_ON(!host);
1587         BUG_ON(!host->card);
1588
1589         mmc_get_card(host->card);
1590
1591         /*
1592          * Just check if our card has been removed.
1593          */
1594         err = _mmc_detect_card_removed(host);
1595
1596         mmc_put_card(host->card);
1597
1598         if (err) {
1599                 mmc_remove(host);
1600
1601                 mmc_claim_host(host);
1602                 mmc_detach_bus(host);
1603                 mmc_power_off(host);
1604                 mmc_release_host(host);
1605         }
1606 }
1607
1608 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1609 {
1610         int err = 0;
1611         unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1612                                         EXT_CSD_POWER_OFF_LONG;
1613
1614         BUG_ON(!host);
1615         BUG_ON(!host->card);
1616
1617         mmc_claim_host(host);
1618
1619         if (mmc_card_suspended(host->card))
1620                 goto out;
1621
1622         if (mmc_card_doing_bkops(host->card)) {
1623                 err = mmc_stop_bkops(host->card);
1624                 if (err)
1625                         goto out;
1626         }
1627
1628         err = mmc_flush_cache(host->card);
1629         if (err)
1630                 goto out;
1631
1632         if (mmc_can_poweroff_notify(host->card) &&
1633                 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1634                 err = mmc_poweroff_notify(host->card, notify_type);
1635         else if (mmc_can_sleep(host->card))
1636                 err = mmc_sleep(host);
1637         else if (!mmc_host_is_spi(host))
1638                 err = mmc_deselect_cards(host);
1639
1640         if (!err) {
1641                 mmc_power_off(host);
1642                 mmc_card_set_suspended(host->card);
1643         }
1644 out:
1645         mmc_release_host(host);
1646         return err;
1647 }
1648
1649 /*
1650  * Suspend callback
1651  */
1652 static int mmc_suspend(struct mmc_host *host)
1653 {
1654         int err;
1655
1656         err = _mmc_suspend(host, true);
1657         if (!err) {
1658                 pm_runtime_disable(&host->card->dev);
1659                 pm_runtime_set_suspended(&host->card->dev);
1660         }
1661
1662         return err;
1663 }
1664
1665 /*
1666  * This function tries to determine if the same card is still present
1667  * and, if so, restore all state to it.
1668  */
1669 static int _mmc_resume(struct mmc_host *host)
1670 {
1671         int err = 0;
1672
1673         BUG_ON(!host);
1674         BUG_ON(!host->card);
1675
1676         mmc_claim_host(host);
1677
1678         if (!mmc_card_suspended(host->card))
1679                 goto out;
1680
1681         mmc_power_up(host, host->card->ocr);
1682         err = mmc_init_card(host, host->card->ocr, host->card);
1683         mmc_card_clr_suspended(host->card);
1684
1685 out:
1686         mmc_release_host(host);
1687         return err;
1688 }
1689
1690 /*
1691  * Shutdown callback
1692  */
1693 static int mmc_shutdown(struct mmc_host *host)
1694 {
1695         int err = 0;
1696
1697         /*
1698          * In a specific case for poweroff notify, we need to resume the card
1699          * before we can shutdown it properly.
1700          */
1701         if (mmc_can_poweroff_notify(host->card) &&
1702                 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
1703                 err = _mmc_resume(host);
1704
1705         if (!err)
1706                 err = _mmc_suspend(host, false);
1707
1708         return err;
1709 }
1710
1711 /*
1712  * Callback for resume.
1713  */
1714 static int mmc_resume(struct mmc_host *host)
1715 {
1716         int err = 0;
1717
1718         if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1719                 err = _mmc_resume(host);
1720                 pm_runtime_set_active(&host->card->dev);
1721                 pm_runtime_mark_last_busy(&host->card->dev);
1722         }
1723         pm_runtime_enable(&host->card->dev);
1724
1725         return err;
1726 }
1727
1728 /*
1729  * Callback for runtime_suspend.
1730  */
1731 static int mmc_runtime_suspend(struct mmc_host *host)
1732 {
1733         int err;
1734
1735         if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1736                 return 0;
1737
1738         err = _mmc_suspend(host, true);
1739         if (err)
1740                 pr_err("%s: error %d doing aggessive suspend\n",
1741                         mmc_hostname(host), err);
1742
1743         return err;
1744 }
1745
1746 /*
1747  * Callback for runtime_resume.
1748  */
1749 static int mmc_runtime_resume(struct mmc_host *host)
1750 {
1751         int err;
1752
1753         if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
1754                 return 0;
1755
1756         err = _mmc_resume(host);
1757         if (err)
1758                 pr_err("%s: error %d doing aggessive resume\n",
1759                         mmc_hostname(host), err);
1760
1761         return 0;
1762 }
1763
1764 static int mmc_power_restore(struct mmc_host *host)
1765 {
1766         int ret;
1767
1768         mmc_claim_host(host);
1769         ret = mmc_init_card(host, host->card->ocr, host->card);
1770         mmc_release_host(host);
1771
1772         return ret;
1773 }
1774
1775 static const struct mmc_bus_ops mmc_ops = {
1776         .remove = mmc_remove,
1777         .detect = mmc_detect,
1778         .suspend = mmc_suspend,
1779         .resume = mmc_resume,
1780         .runtime_suspend = mmc_runtime_suspend,
1781         .runtime_resume = mmc_runtime_resume,
1782         .power_restore = mmc_power_restore,
1783         .alive = mmc_alive,
1784         .shutdown = mmc_shutdown,
1785 };
1786
1787 /*
1788  * Starting point for MMC card init.
1789  */
1790 int mmc_attach_mmc(struct mmc_host *host)
1791 {
1792         int err;
1793         u32 ocr, rocr;
1794
1795         BUG_ON(!host);
1796         WARN_ON(!host->claimed);
1797
1798         /* Set correct bus mode for MMC before attempting attach */
1799         if (!mmc_host_is_spi(host))
1800                 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1801
1802         err = mmc_send_op_cond(host, 0, &ocr);
1803         if (err)
1804                 return err;
1805
1806         mmc_attach_bus(host, &mmc_ops);
1807         if (host->ocr_avail_mmc)
1808                 host->ocr_avail = host->ocr_avail_mmc;
1809
1810         /*
1811          * We need to get OCR a different way for SPI.
1812          */
1813         if (mmc_host_is_spi(host)) {
1814                 err = mmc_spi_read_ocr(host, 1, &ocr);
1815                 if (err)
1816                         goto err;
1817         }
1818
1819         rocr = mmc_select_voltage(host, ocr);
1820
1821         /*
1822          * Can we support the voltage of the card?
1823          */
1824         if (!rocr) {
1825                 err = -EINVAL;
1826                 goto err;
1827         }
1828
1829         /*
1830          * Detect and init the card.
1831          */
1832         err = mmc_init_card(host, rocr, NULL);
1833         if (err)
1834                 goto err;
1835
1836         mmc_release_host(host);
1837         err = mmc_add_card(host->card);
1838         mmc_claim_host(host);
1839         if (err)
1840                 goto remove_card;
1841
1842         return 0;
1843
1844 remove_card:
1845         mmc_release_host(host);
1846         mmc_remove_card(host->card);
1847         mmc_claim_host(host);
1848         host->card = NULL;
1849 err:
1850         mmc_detach_bus(host);
1851
1852         pr_err("%s: error %d whilst initialising MMC card\n",
1853                 mmc_hostname(host), err);
1854
1855         return err;
1856 }