rk: restore file mode
[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 & EXT_CSD_CARD_TYPE_MASK;
244         u32 caps = host->caps, caps2 = host->caps2;
245         unsigned int hs_max_dtr = 0;
246
247         if (card_type & EXT_CSD_CARD_TYPE_26)
248                 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
249
250         if (caps & MMC_CAP_MMC_HIGHSPEED &&
251                         card_type & EXT_CSD_CARD_TYPE_52)
252                 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
253
254         if ((caps & MMC_CAP_1_8V_DDR &&
255                         card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) ||
256             (caps & MMC_CAP_1_2V_DDR &&
257                         card_type & EXT_CSD_CARD_TYPE_DDR_1_2V))
258                 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
259
260         if ((caps2 & MMC_CAP2_HS200_1_8V_SDR &&
261                         card_type & EXT_CSD_CARD_TYPE_SDR_1_8V) ||
262             (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
263                         card_type & EXT_CSD_CARD_TYPE_SDR_1_2V))
264                 hs_max_dtr = MMC_HS200_MAX_DTR;
265
266         card->ext_csd.hs_max_dtr = hs_max_dtr;
267         card->ext_csd.card_type = card_type;
268 }
269
270 /*
271  * Decode extended CSD.
272  */
273 static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
274 {
275         int err = 0, idx;
276         unsigned int part_size;
277         u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
278
279         BUG_ON(!card);
280
281         if (!ext_csd)
282                 return 0;
283
284         /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
285         card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
286         if (card->csd.structure == 3) {
287                 if (card->ext_csd.raw_ext_csd_structure > 2) {
288                         pr_err("%s: unrecognised EXT_CSD structure "
289                                 "version %d\n", mmc_hostname(card->host),
290                                         card->ext_csd.raw_ext_csd_structure);
291                         err = -EINVAL;
292                         goto out;
293                 }
294         }
295
296         /*
297         * The EXT_CSD format is meant to be forward compatible. As long
298         * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
299         * are authorized, see JEDEC JESD84-B50 section B.8.
300         */
301         card->ext_csd.rev = ext_csd[EXT_CSD_REV];
302
303         card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
304         card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
305         card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
306         card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
307         if (card->ext_csd.rev >= 2) {
308                 card->ext_csd.sectors =
309                         ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
310                         ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
311                         ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
312                         ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
313
314                 /* Cards with density > 2GiB are sector addressed */
315                 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
316                         mmc_card_set_blockaddr(card);
317         }
318
319         card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
320         mmc_select_card_type(card);
321
322         card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
323         card->ext_csd.raw_erase_timeout_mult =
324                 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
325         card->ext_csd.raw_hc_erase_grp_size =
326                 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
327         if (card->ext_csd.rev >= 3) {
328                 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
329                 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
330
331                 /* EXT_CSD value is in units of 10ms, but we store in ms */
332                 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
333
334                 /* Sleep / awake timeout in 100ns units */
335                 if (sa_shift > 0 && sa_shift <= 0x17)
336                         card->ext_csd.sa_timeout =
337                                         1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
338                 card->ext_csd.erase_group_def =
339                         ext_csd[EXT_CSD_ERASE_GROUP_DEF];
340                 card->ext_csd.hc_erase_timeout = 300 *
341                         ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
342                 card->ext_csd.hc_erase_size =
343                         ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
344
345                 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
346
347                 /*
348                  * There are two boot regions of equal size, defined in
349                  * multiples of 128K.
350                  */
351                 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
352                         for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
353                                 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
354                                 mmc_part_add(card, part_size,
355                                         EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
356                                         "boot%d", idx, true,
357                                         MMC_BLK_DATA_AREA_BOOT);
358                         }
359                 }
360         }
361
362         card->ext_csd.raw_hc_erase_gap_size =
363                 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
364         card->ext_csd.raw_sec_trim_mult =
365                 ext_csd[EXT_CSD_SEC_TRIM_MULT];
366         card->ext_csd.raw_sec_erase_mult =
367                 ext_csd[EXT_CSD_SEC_ERASE_MULT];
368         card->ext_csd.raw_sec_feature_support =
369                 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
370         card->ext_csd.raw_trim_mult =
371                 ext_csd[EXT_CSD_TRIM_MULT];
372         card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
373         if (card->ext_csd.rev >= 4) {
374                 /*
375                  * Enhanced area feature support -- check whether the eMMC
376                  * card has the Enhanced area enabled.  If so, export enhanced
377                  * area offset and size to user by adding sysfs interface.
378                  */
379                 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
380                     (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
381                         hc_erase_grp_sz =
382                                 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
383                         hc_wp_grp_sz =
384                                 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
385
386                         card->ext_csd.enhanced_area_en = 1;
387                         /*
388                          * calculate the enhanced data area offset, in bytes
389                          */
390                         card->ext_csd.enhanced_area_offset =
391                                 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
392                                 (ext_csd[137] << 8) + ext_csd[136];
393                         if (mmc_card_blockaddr(card))
394                                 card->ext_csd.enhanced_area_offset <<= 9;
395                         /*
396                          * calculate the enhanced data area size, in kilobytes
397                          */
398                         card->ext_csd.enhanced_area_size =
399                                 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
400                                 ext_csd[140];
401                         card->ext_csd.enhanced_area_size *=
402                                 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
403                         card->ext_csd.enhanced_area_size <<= 9;
404                 } else {
405                         /*
406                          * If the enhanced area is not enabled, disable these
407                          * device attributes.
408                          */
409                         card->ext_csd.enhanced_area_offset = -EINVAL;
410                         card->ext_csd.enhanced_area_size = -EINVAL;
411                 }
412
413                 /*
414                  * General purpose partition feature support --
415                  * If ext_csd has the size of general purpose partitions,
416                  * set size, part_cfg, partition name in mmc_part.
417                  */
418                 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
419                         EXT_CSD_PART_SUPPORT_PART_EN) {
420                         if (card->ext_csd.enhanced_area_en != 1) {
421                                 hc_erase_grp_sz =
422                                         ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
423                                 hc_wp_grp_sz =
424                                         ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
425
426                                 card->ext_csd.enhanced_area_en = 1;
427                         }
428
429                         for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
430                                 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
431                                 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
432                                 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
433                                         continue;
434                                 part_size =
435                                 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
436                                         << 16) +
437                                 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
438                                         << 8) +
439                                 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
440                                 part_size *= (size_t)(hc_erase_grp_sz *
441                                         hc_wp_grp_sz);
442                                 mmc_part_add(card, part_size << 19,
443                                         EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
444                                         "gp%d", idx, false,
445                                         MMC_BLK_DATA_AREA_GP);
446                         }
447                 }
448                 card->ext_csd.sec_trim_mult =
449                         ext_csd[EXT_CSD_SEC_TRIM_MULT];
450                 card->ext_csd.sec_erase_mult =
451                         ext_csd[EXT_CSD_SEC_ERASE_MULT];
452                 card->ext_csd.sec_feature_support =
453                         ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
454                 card->ext_csd.trim_timeout = 300 *
455                         ext_csd[EXT_CSD_TRIM_MULT];
456
457                 /*
458                  * Note that the call to mmc_part_add above defaults to read
459                  * only. If this default assumption is changed, the call must
460                  * take into account the value of boot_locked below.
461                  */
462                 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
463                 card->ext_csd.boot_ro_lockable = true;
464         }
465
466         if (card->ext_csd.rev >= 5) {
467                 /* Adjust production date as per JEDEC JESD84-B451 */
468                 if (card->cid.year < 2010)
469                         card->cid.year += 16;
470
471                 /* check whether the eMMC card supports BKOPS */
472                 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
473                         card->ext_csd.bkops = 1;
474                         card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
475                         card->ext_csd.raw_bkops_status =
476                                 ext_csd[EXT_CSD_BKOPS_STATUS];
477                         if (!card->ext_csd.bkops_en)
478                                 pr_info("%s: BKOPS_EN bit is not set\n",
479                                         mmc_hostname(card->host));
480                 }
481
482                 /* check whether the eMMC card supports HPI */
483                 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
484                         card->ext_csd.hpi = 1;
485                         if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
486                                 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
487                         else
488                                 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
489                         /*
490                          * Indicate the maximum timeout to close
491                          * a command interrupted by HPI
492                          */
493                         card->ext_csd.out_of_int_time =
494                                 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
495                 }
496
497                 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
498                 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
499
500                 /*
501                  * RPMB regions are defined in multiples of 128K.
502                  */
503                 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
504                 #if 0 //noted by xbw,2014-03-11
505                 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
506                         mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
507                                 EXT_CSD_PART_CONFIG_ACC_RPMB,
508                                 "rpmb", 0, false,
509                                 MMC_BLK_DATA_AREA_RPMB);
510                 }
511                 #endif
512         }
513
514         card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
515         if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
516                 card->erased_byte = 0xFF;
517         else
518                 card->erased_byte = 0x0;
519
520         /* eMMC v4.5 or later */
521         if (card->ext_csd.rev >= 6) {
522                 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
523
524                 card->ext_csd.generic_cmd6_time = 10 *
525                         ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
526                 card->ext_csd.power_off_longtime = 10 *
527                         ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
528
529                 card->ext_csd.cache_size =
530                         ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
531                         ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
532                         ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
533                         ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
534
535                 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
536                         card->ext_csd.data_sector_size = 4096;
537                 else
538                         card->ext_csd.data_sector_size = 512;
539
540                 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
541                     (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
542                         card->ext_csd.data_tag_unit_size =
543                         ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
544                         (card->ext_csd.data_sector_size);
545                 } else {
546                         card->ext_csd.data_tag_unit_size = 0;
547                 }
548
549                 card->ext_csd.max_packed_writes =
550                         ext_csd[EXT_CSD_MAX_PACKED_WRITES];
551                 card->ext_csd.max_packed_reads =
552                         ext_csd[EXT_CSD_MAX_PACKED_READS];
553         } else {
554                 card->ext_csd.data_sector_size = 512;
555         }
556
557 out:
558         return err;
559 }
560
561 static inline void mmc_free_ext_csd(u8 *ext_csd)
562 {
563         kfree(ext_csd);
564 }
565
566
567 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
568 {
569         u8 *bw_ext_csd;
570         int err;
571
572         if (bus_width == MMC_BUS_WIDTH_1)
573                 return 0;
574
575         err = mmc_get_ext_csd(card, &bw_ext_csd);
576
577         if (err || bw_ext_csd == NULL) {
578                 err = -EINVAL;
579                 goto out;
580         }
581
582         /* only compare read only fields */
583         err = !((card->ext_csd.raw_partition_support ==
584                         bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
585                 (card->ext_csd.raw_erased_mem_count ==
586                         bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
587                 (card->ext_csd.rev ==
588                         bw_ext_csd[EXT_CSD_REV]) &&
589                 (card->ext_csd.raw_ext_csd_structure ==
590                         bw_ext_csd[EXT_CSD_STRUCTURE]) &&
591                 (card->ext_csd.raw_card_type ==
592                         bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
593                 (card->ext_csd.raw_s_a_timeout ==
594                         bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
595                 (card->ext_csd.raw_hc_erase_gap_size ==
596                         bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
597                 (card->ext_csd.raw_erase_timeout_mult ==
598                         bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
599                 (card->ext_csd.raw_hc_erase_grp_size ==
600                         bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
601                 (card->ext_csd.raw_sec_trim_mult ==
602                         bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
603                 (card->ext_csd.raw_sec_erase_mult ==
604                         bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
605                 (card->ext_csd.raw_sec_feature_support ==
606                         bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
607                 (card->ext_csd.raw_trim_mult ==
608                         bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
609                 (card->ext_csd.raw_sectors[0] ==
610                         bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
611                 (card->ext_csd.raw_sectors[1] ==
612                         bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
613                 (card->ext_csd.raw_sectors[2] ==
614                         bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
615                 (card->ext_csd.raw_sectors[3] ==
616                         bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
617         if (err)
618                 err = -EINVAL;
619
620 out:
621         mmc_free_ext_csd(bw_ext_csd);
622         return err;
623 }
624
625 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
626         card->raw_cid[2], card->raw_cid[3]);
627 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
628         card->raw_csd[2], card->raw_csd[3]);
629 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
630 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
631 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
632 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
633 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
634 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
635 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
636 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
637 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
638 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
639 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
640                 card->ext_csd.enhanced_area_offset);
641 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
642 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
643 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
644
645 static struct attribute *mmc_std_attrs[] = {
646         &dev_attr_cid.attr,
647         &dev_attr_csd.attr,
648         &dev_attr_date.attr,
649         &dev_attr_erase_size.attr,
650         &dev_attr_preferred_erase_size.attr,
651         &dev_attr_fwrev.attr,
652         &dev_attr_hwrev.attr,
653         &dev_attr_manfid.attr,
654         &dev_attr_name.attr,
655         &dev_attr_oemid.attr,
656         &dev_attr_prv.attr,
657         &dev_attr_serial.attr,
658         &dev_attr_enhanced_area_offset.attr,
659         &dev_attr_enhanced_area_size.attr,
660         &dev_attr_raw_rpmb_size_mult.attr,
661         &dev_attr_rel_sectors.attr,
662         NULL,
663 };
664
665 static struct attribute_group mmc_std_attr_group = {
666         .attrs = mmc_std_attrs,
667 };
668
669 static const struct attribute_group *mmc_attr_groups[] = {
670         &mmc_std_attr_group,
671         NULL,
672 };
673
674 static struct device_type mmc_type = {
675         .groups = mmc_attr_groups,
676 };
677
678 /*
679  * Select the PowerClass for the current bus width
680  * If power class is defined for 4/8 bit bus in the
681  * extended CSD register, select it by executing the
682  * mmc_switch command.
683  */
684 static int mmc_select_powerclass(struct mmc_card *card,
685                 unsigned int bus_width)
686 {
687         int err = 0;
688         unsigned int pwrclass_val = 0;
689         struct mmc_host *host;
690
691         BUG_ON(!card);
692
693         host = card->host;
694         BUG_ON(!host);
695
696         /* Power class selection is supported for versions >= 4.0 */
697         if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
698                 return 0;
699
700         /* Power class values are defined only for 4/8 bit bus */
701         if (bus_width == EXT_CSD_BUS_WIDTH_1)
702                 return 0;
703
704         switch (1 << host->ios.vdd) {
705         case MMC_VDD_165_195:
706                 if (host->ios.clock <= 26000000)
707                         pwrclass_val = card->ext_csd.raw_pwr_cl_26_195;
708                 else if (host->ios.clock <= 52000000)
709                         pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
710                                 card->ext_csd.raw_pwr_cl_52_195 :
711                                 card->ext_csd.raw_pwr_cl_ddr_52_195;
712                 else if (host->ios.clock <= 200000000)
713                         pwrclass_val = card->ext_csd.raw_pwr_cl_200_195;
714                 break;
715         case MMC_VDD_27_28:
716         case MMC_VDD_28_29:
717         case MMC_VDD_29_30:
718         case MMC_VDD_30_31:
719         case MMC_VDD_31_32:
720         case MMC_VDD_32_33:
721         case MMC_VDD_33_34:
722         case MMC_VDD_34_35:
723         case MMC_VDD_35_36:
724                 if (host->ios.clock <= 26000000)
725                         pwrclass_val = card->ext_csd.raw_pwr_cl_26_360;
726                 else if (host->ios.clock <= 52000000)
727                         pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
728                                 card->ext_csd.raw_pwr_cl_52_360 :
729                                 card->ext_csd.raw_pwr_cl_ddr_52_360;
730                 else if (host->ios.clock <= 200000000)
731                         pwrclass_val = card->ext_csd.raw_pwr_cl_200_360;
732                 break;
733         default:
734                 pr_warning("%s: Voltage range not supported "
735                            "for power class.\n", mmc_hostname(host));
736                 return -EINVAL;
737         }
738
739         if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
740                 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
741                                 EXT_CSD_PWR_CL_8BIT_SHIFT;
742         else
743                 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
744                                 EXT_CSD_PWR_CL_4BIT_SHIFT;
745
746         /* If the power class is different from the default value */
747         if (pwrclass_val > 0) {
748                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
749                                  EXT_CSD_POWER_CLASS,
750                                  pwrclass_val,
751                                  card->ext_csd.generic_cmd6_time);
752         }
753
754         return err;
755 }
756
757 /*
758  * Selects the desired buswidth and switch to the HS200 mode
759  * if bus width set without error
760  */
761 static int mmc_select_hs200(struct mmc_card *card)
762 {
763         int idx, err = -EINVAL;
764         struct mmc_host *host;
765         static unsigned ext_csd_bits[] = {
766                 EXT_CSD_BUS_WIDTH_4,
767                 EXT_CSD_BUS_WIDTH_8,
768         };
769         static unsigned bus_widths[] = {
770                 MMC_BUS_WIDTH_4,
771                 MMC_BUS_WIDTH_8,
772         };
773
774         BUG_ON(!card);
775
776         host = card->host;
777
778         if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V &&
779                         host->caps2 & MMC_CAP2_HS200_1_2V_SDR)
780                 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
781
782         if (err && card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_8V &&
783                         host->caps2 & MMC_CAP2_HS200_1_8V_SDR)
784                 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
785
786         /* If fails try again during next card power cycle */
787         if (err)
788                 goto err;
789
790         idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0;
791
792         /*
793          * Unlike SD, MMC cards dont have a configuration register to notify
794          * supported bus width. So bus test command should be run to identify
795          * the supported bus width or compare the ext csd values of current
796          * bus width and ext csd values of 1 bit mode read earlier.
797          */
798         for (; idx >= 0; idx--) {
799
800                 /*
801                  * Host is capable of 8bit transfer, then switch
802                  * the device to work in 8bit transfer mode. If the
803                  * mmc switch command returns error then switch to
804                  * 4bit transfer mode. On success set the corresponding
805                  * bus width on the host.
806                  */
807                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
808                                  EXT_CSD_BUS_WIDTH,
809                                  ext_csd_bits[idx],
810                                  card->ext_csd.generic_cmd6_time);
811                 if (err)
812                         continue;
813
814                 mmc_set_bus_width(card->host, bus_widths[idx]);
815
816                 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
817                         err = mmc_compare_ext_csds(card, bus_widths[idx]);
818                 else
819                         err = mmc_bus_test(card, bus_widths[idx]);
820                 if (!err)
821                         break;
822         }
823
824         /* switch to HS200 mode if bus width set successfully */
825         if (!err)
826                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
827                                  EXT_CSD_HS_TIMING, 2, 0);
828 err:
829         return err;
830 }
831
832 /*
833  * Handle the detection and initialisation of a card.
834  *
835  * In the case of a resume, "oldcard" will contain the card
836  * we're trying to reinitialise.
837  */
838 static int mmc_init_card(struct mmc_host *host, u32 ocr,
839         struct mmc_card *oldcard)
840 {
841         struct mmc_card *card;
842         int err, ddr = 0;
843         u32 cid[4];
844         unsigned int max_dtr;
845         u32 rocr;
846         u8 *ext_csd = NULL;
847
848         BUG_ON(!host);
849         WARN_ON(!host->claimed);
850
851         /* Set correct bus mode for MMC before attempting init */
852         if (!mmc_host_is_spi(host))
853                 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
854
855         /*
856          * Since we're changing the OCR value, we seem to
857          * need to tell some cards to go back to the idle
858          * state.  We wait 1ms to give cards time to
859          * respond.
860          * mmc_go_idle is needed for eMMC that are asleep
861          */
862         mmc_go_idle(host);
863
864         /* The extra bit indicates that we support high capacity */
865         err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
866         if (err)
867                 goto err;
868
869         /*
870          * For SPI, enable CRC as appropriate.
871          */
872         if (mmc_host_is_spi(host)) {
873                 err = mmc_spi_set_crc(host, use_spi_crc);
874                 if (err)
875                         goto err;
876         }
877
878         /*
879          * Fetch CID from card.
880          */
881         if (mmc_host_is_spi(host))
882                 err = mmc_send_cid(host, cid);
883         else
884                 err = mmc_all_send_cid(host, cid);
885         if (err)
886                 goto err;
887
888         if (oldcard) {
889                 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
890                         err = -ENOENT;
891                         goto err;
892                 }
893
894                 card = oldcard;
895         } else {
896                 /*
897                  * Allocate card structure.
898                  */
899                 card = mmc_alloc_card(host, &mmc_type);
900                 if (IS_ERR(card)) {
901                         err = PTR_ERR(card);
902                         goto err;
903                 }
904
905                 card->ocr = ocr;
906                 card->type = MMC_TYPE_MMC;
907                 card->rca = 1;
908                 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
909         }
910
911         /*
912          * For native busses:  set card RCA and quit open drain mode.
913          */
914         if (!mmc_host_is_spi(host)) {
915                 err = mmc_set_relative_addr(card);
916                 if (err)
917                         goto free_card;
918
919                 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
920         }
921
922         if (!oldcard) {
923                 /*
924                  * Fetch CSD from card.
925                  */
926                 err = mmc_send_csd(card, card->raw_csd);
927                 if (err)
928                         goto free_card;
929
930                 err = mmc_decode_csd(card);
931                 if (err)
932                         goto free_card;
933                 err = mmc_decode_cid(card);
934                 if (err)
935                         goto free_card;
936         }
937
938         /*
939          * Select card, as all following commands rely on that.
940          */
941         if (!mmc_host_is_spi(host)) {
942                 err = mmc_select_card(card);
943                 if (err)
944                         goto free_card;
945         }
946
947         if (!oldcard) {
948                 /*
949                  * Fetch and process extended CSD.
950                  */
951
952                 err = mmc_get_ext_csd(card, &ext_csd);
953                 if (err)
954                         goto free_card;
955                 err = mmc_read_ext_csd(card, ext_csd);
956                 if (err)
957                         goto free_card;
958
959                 /* If doing byte addressing, check if required to do sector
960                  * addressing.  Handle the case of <2GB cards needing sector
961                  * addressing.  See section 8.1 JEDEC Standard JED84-A441;
962                  * ocr register has bit 30 set for sector addressing.
963                  */
964                 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
965                         mmc_card_set_blockaddr(card);
966
967                 /* Erase size depends on CSD and Extended CSD */
968                 mmc_set_erase_size(card);
969         }
970
971         /*
972          * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
973          * bit.  This bit will be lost every time after a reset or power off.
974          */
975         if (card->ext_csd.enhanced_area_en ||
976             (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
977                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
978                                  EXT_CSD_ERASE_GROUP_DEF, 1,
979                                  card->ext_csd.generic_cmd6_time);
980
981                 if (err && err != -EBADMSG)
982                         goto free_card;
983
984                 if (err) {
985                         err = 0;
986                         /*
987                          * Just disable enhanced area off & sz
988                          * will try to enable ERASE_GROUP_DEF
989                          * during next time reinit
990                          */
991                         card->ext_csd.enhanced_area_offset = -EINVAL;
992                         card->ext_csd.enhanced_area_size = -EINVAL;
993                 } else {
994                         card->ext_csd.erase_group_def = 1;
995                         /*
996                          * enable ERASE_GRP_DEF successfully.
997                          * This will affect the erase size, so
998                          * here need to reset erase size
999                          */
1000                         mmc_set_erase_size(card);
1001                 }
1002         }
1003
1004         /*
1005          * Ensure eMMC user default partition is enabled
1006          */
1007         if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1008                 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1009                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1010                                  card->ext_csd.part_config,
1011                                  card->ext_csd.part_time);
1012                 if (err && err != -EBADMSG)
1013                         goto free_card;
1014         }
1015
1016         /*
1017          * Enable power_off_notification byte in the ext_csd register
1018          */
1019         if (card->ext_csd.rev >= 6) {
1020                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1021                                  EXT_CSD_POWER_OFF_NOTIFICATION,
1022                                  EXT_CSD_POWER_ON,
1023                                  card->ext_csd.generic_cmd6_time);
1024                 if (err && err != -EBADMSG)
1025                         goto free_card;
1026
1027                 /*
1028                  * The err can be -EBADMSG or 0,
1029                  * so check for success and update the flag
1030                  */
1031                 if (!err)
1032                         card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1033         }
1034
1035         /*
1036          * Activate high speed (if supported)
1037          */
1038         if (card->ext_csd.hs_max_dtr != 0) {
1039                 err = 0;
1040                 if (card->ext_csd.hs_max_dtr > 52000000 &&
1041                     host->caps2 & MMC_CAP2_HS200)
1042                         err = mmc_select_hs200(card);
1043                 else if (host->caps & MMC_CAP_MMC_HIGHSPEED)
1044                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1045                                          EXT_CSD_HS_TIMING, 1,
1046                                          card->ext_csd.generic_cmd6_time);
1047
1048                 if (err && err != -EBADMSG)
1049                         goto free_card;
1050
1051                 if (err) {
1052                         pr_warning("%s: switch to highspeed failed\n",
1053                                mmc_hostname(card->host));
1054                         err = 0;
1055                 } else {
1056                         if (card->ext_csd.hs_max_dtr > 52000000 &&
1057                             host->caps2 & MMC_CAP2_HS200) {
1058                                 mmc_card_set_hs200(card);
1059                                 mmc_set_timing(card->host,
1060                                                MMC_TIMING_MMC_HS200);
1061                         } else {
1062                                 mmc_card_set_highspeed(card);
1063                                 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1064                         }
1065                 }
1066         }
1067
1068         /*
1069          * Compute bus speed.
1070          */
1071         max_dtr = (unsigned int)-1;
1072
1073         if (mmc_card_highspeed(card) || mmc_card_hs200(card)) {
1074                 if (max_dtr > card->ext_csd.hs_max_dtr)
1075                         max_dtr = card->ext_csd.hs_max_dtr;
1076                 if (mmc_card_highspeed(card) && (max_dtr > 52000000))
1077                         max_dtr = 52000000;
1078         } else if (max_dtr > card->csd.max_dtr) {
1079                 max_dtr = card->csd.max_dtr;
1080         }
1081
1082         mmc_set_clock(host, max_dtr);
1083
1084         /*
1085          * Indicate DDR mode (if supported).
1086          */
1087         if (mmc_card_highspeed(card)) {
1088                 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
1089                         && (host->caps & MMC_CAP_1_8V_DDR))
1090                                 ddr = MMC_1_8V_DDR_MODE;
1091                 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1092                         && (host->caps & MMC_CAP_1_2V_DDR))
1093                                 ddr = MMC_1_2V_DDR_MODE;
1094         }
1095
1096         /*
1097          * Indicate HS200 SDR mode (if supported).
1098          */
1099         if (mmc_card_hs200(card)) {
1100                 u32 ext_csd_bits;
1101                 u32 bus_width = card->host->ios.bus_width;
1102
1103                 /*
1104                  * For devices supporting HS200 mode, the bus width has
1105                  * to be set before executing the tuning function. If
1106                  * set before tuning, then device will respond with CRC
1107                  * errors for responses on CMD line. So for HS200 the
1108                  * sequence will be
1109                  * 1. set bus width 4bit / 8 bit (1 bit not supported)
1110                  * 2. switch to HS200 mode
1111                  * 3. set the clock to > 52Mhz <=200MHz and
1112                  * 4. execute tuning for HS200
1113                  */
1114                 /*
1115                 if ((host->caps2 & MMC_CAP2_HS200) &&
1116                     card->host->ops->execute_tuning) {
1117                         mmc_host_clk_hold(card->host);
1118                         err = card->host->ops->execute_tuning(card->host,
1119                                 MMC_SEND_TUNING_BLOCK_HS200);
1120                         mmc_host_clk_release(card->host);
1121                 }
1122                 if (err) {
1123                         pr_warning("%s: tuning execution failed\n",
1124                                    mmc_hostname(card->host));
1125                         goto err;
1126                 }
1127                 */
1128                 if (host->caps2 & MMC_CAP2_HS200)
1129                         mmc_execute_tuning(card);
1130
1131                 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1132                                 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
1133                 err = mmc_select_powerclass(card, ext_csd_bits);
1134                 if (err)
1135                         pr_warning("%s: power class selection to bus width %d"
1136                                    " failed\n", mmc_hostname(card->host),
1137                                    1 << bus_width);
1138         }
1139
1140         /*
1141          * Activate wide bus and DDR (if supported).
1142          */
1143         if (!mmc_card_hs200(card) &&
1144             (card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
1145             (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
1146                 static unsigned ext_csd_bits[][2] = {
1147                         { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
1148                         { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
1149                         { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
1150                 };
1151                 static unsigned bus_widths[] = {
1152                         MMC_BUS_WIDTH_8,
1153                         MMC_BUS_WIDTH_4,
1154                         MMC_BUS_WIDTH_1
1155                 };
1156                 unsigned idx, bus_width = 0;
1157
1158                 if (host->caps & MMC_CAP_8_BIT_DATA)
1159                         idx = 0;
1160                 else
1161                         idx = 1;
1162                 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1163                         bus_width = bus_widths[idx];
1164                         if (bus_width == MMC_BUS_WIDTH_1)
1165                                 ddr = 0; /* no DDR for 1-bit width */
1166                         err = mmc_select_powerclass(card, ext_csd_bits[idx][0]);
1167                         if (err)
1168                                 pr_warning("%s: power class selection to "
1169                                            "bus width %d failed\n",
1170                                            mmc_hostname(card->host),
1171                                            1 << bus_width);
1172
1173                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1174                                          EXT_CSD_BUS_WIDTH,
1175                                          ext_csd_bits[idx][0],
1176                                          card->ext_csd.generic_cmd6_time);
1177                         if (!err) {
1178                                 mmc_set_bus_width(card->host, bus_width);
1179
1180                                 /*
1181                                  * If controller can't handle bus width test,
1182                                  * compare ext_csd previously read in 1 bit mode
1183                                  * against ext_csd at new bus width
1184                                  */
1185                                 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
1186                                         err = mmc_compare_ext_csds(card,
1187                                                 bus_width);
1188                                 else
1189                                         err = mmc_bus_test(card, bus_width);
1190                                 if (!err)
1191                                         break;
1192                         }
1193                 }
1194
1195                 if (!err && ddr) {
1196                         err = mmc_select_powerclass(card, ext_csd_bits[idx][1]);
1197                         if (err)
1198                                 pr_warning("%s: power class selection to "
1199                                            "bus width %d ddr %d failed\n",
1200                                            mmc_hostname(card->host),
1201                                            1 << bus_width, ddr);
1202
1203                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1204                                          EXT_CSD_BUS_WIDTH,
1205                                          ext_csd_bits[idx][1],
1206                                          card->ext_csd.generic_cmd6_time);
1207                 }
1208                 if (err) {
1209                         pr_warning("%s: switch to bus width %d ddr %d "
1210                                 "failed\n", mmc_hostname(card->host),
1211                                 1 << bus_width, ddr);
1212                         goto free_card;
1213                 } else if (ddr) {
1214                         /*
1215                          * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1216                          * signaling.
1217                          *
1218                          * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1219                          *
1220                          * 1.8V vccq at 3.3V core voltage (vcc) is not required
1221                          * in the JEDEC spec for DDR.
1222                          *
1223                          * Do not force change in vccq since we are obviously
1224                          * working and no change to vccq is needed.
1225                          *
1226                          * WARNING: eMMC rules are NOT the same as SD DDR
1227                          */
1228                         if (ddr == MMC_1_2V_DDR_MODE) {
1229                                 err = __mmc_set_signal_voltage(host,
1230                                         MMC_SIGNAL_VOLTAGE_120);
1231                                 if (err)
1232                                         goto err;
1233                         }
1234                         mmc_card_set_ddr_mode(card);
1235                         mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1236                         mmc_set_bus_width(card->host, bus_width);
1237                 }
1238         }
1239
1240         /*
1241          * Enable HPI feature (if supported)
1242          */
1243         if (card->ext_csd.hpi) {
1244                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1245                                 EXT_CSD_HPI_MGMT, 1,
1246                                 card->ext_csd.generic_cmd6_time);
1247                 if (err && err != -EBADMSG)
1248                         goto free_card;
1249                 if (err) {
1250                         pr_warning("%s: Enabling HPI failed\n",
1251                                    mmc_hostname(card->host));
1252                         err = 0;
1253                 } else
1254                         card->ext_csd.hpi_en = 1;
1255         }
1256
1257         /*
1258          * If cache size is higher than 0, this indicates
1259          * the existence of cache and it can be turned on.
1260          */
1261         if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1262                         card->ext_csd.cache_size > 0) {
1263                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1264                                 EXT_CSD_CACHE_CTRL, 1,
1265                                 card->ext_csd.generic_cmd6_time);
1266                 if (err && err != -EBADMSG)
1267                         goto free_card;
1268
1269                 /*
1270                  * Only if no error, cache is turned on successfully.
1271                  */
1272                 if (err) {
1273                         pr_warning("%s: Cache is supported, "
1274                                         "but failed to turn on (%d)\n",
1275                                         mmc_hostname(card->host), err);
1276                         card->ext_csd.cache_ctrl = 0;
1277                         err = 0;
1278                 } else {
1279                         card->ext_csd.cache_ctrl = 1;
1280                 }
1281         }
1282
1283         /*
1284          * The mandatory minimum values are defined for packed command.
1285          * read: 5, write: 3
1286          */
1287         if (card->ext_csd.max_packed_writes >= 3 &&
1288             card->ext_csd.max_packed_reads >= 5 &&
1289             host->caps2 & MMC_CAP2_PACKED_CMD) {
1290                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1291                                 EXT_CSD_EXP_EVENTS_CTRL,
1292                                 EXT_CSD_PACKED_EVENT_EN,
1293                                 card->ext_csd.generic_cmd6_time);
1294                 if (err && err != -EBADMSG)
1295                         goto free_card;
1296                 if (err) {
1297                         pr_warn("%s: Enabling packed event failed\n",
1298                                 mmc_hostname(card->host));
1299                         card->ext_csd.packed_event_en = 0;
1300                         err = 0;
1301                 } else {
1302                         card->ext_csd.packed_event_en = 1;
1303                 }
1304         }
1305
1306         if (!oldcard)
1307                 host->card = card;
1308
1309         mmc_free_ext_csd(ext_csd);
1310         return 0;
1311
1312 free_card:
1313         if (!oldcard)
1314                 mmc_remove_card(card);
1315 err:
1316         mmc_free_ext_csd(ext_csd);
1317
1318         return err;
1319 }
1320
1321 static int mmc_can_sleep(struct mmc_card *card)
1322 {
1323         return (card && card->ext_csd.rev >= 3);
1324 }
1325
1326 static int mmc_sleep(struct mmc_host *host)
1327 {
1328         struct mmc_command cmd = {0};
1329         struct mmc_card *card = host->card;
1330         int err;
1331
1332         if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
1333                 return 0;
1334
1335         err = mmc_deselect_cards(host);
1336         if (err)
1337                 return err;
1338
1339         cmd.opcode = MMC_SLEEP_AWAKE;
1340         cmd.arg = card->rca << 16;
1341         cmd.arg |= 1 << 15;
1342
1343         cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1344         err = mmc_wait_for_cmd(host, &cmd, 0);
1345         if (err)
1346                 return err;
1347
1348         /*
1349          * If the host does not wait while the card signals busy, then we will
1350          * will have to wait the sleep/awake timeout.  Note, we cannot use the
1351          * SEND_STATUS command to poll the status because that command (and most
1352          * others) is invalid while the card sleeps.
1353          */
1354         if (!(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1355                 mmc_delay(DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000));
1356
1357         return err;
1358 }
1359
1360 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1361 {
1362         return card &&
1363                 mmc_card_mmc(card) &&
1364                 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1365 }
1366
1367 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1368 {
1369         unsigned int timeout = card->ext_csd.generic_cmd6_time;
1370         int err;
1371
1372         /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1373         if (notify_type == EXT_CSD_POWER_OFF_LONG)
1374                 timeout = card->ext_csd.power_off_longtime;
1375
1376         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1377                         EXT_CSD_POWER_OFF_NOTIFICATION,
1378                         notify_type, timeout, true, false);
1379         if (err)
1380                 pr_err("%s: Power Off Notification timed out, %u\n",
1381                        mmc_hostname(card->host), timeout);
1382
1383         /* Disable the power off notification after the switch operation. */
1384         card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1385
1386         return err;
1387 }
1388
1389 /*
1390  * Host is being removed. Free up the current card.
1391  */
1392 static void mmc_remove(struct mmc_host *host)
1393 {
1394         BUG_ON(!host);
1395         BUG_ON(!host->card);
1396
1397         mmc_remove_card(host->card);
1398         host->card = NULL;
1399 }
1400
1401 /*
1402  * Card detection - card is alive.
1403  */
1404 static int mmc_alive(struct mmc_host *host)
1405 {
1406         return mmc_send_status(host->card, NULL);
1407 }
1408
1409 /*
1410  * Card detection callback from host.
1411  */
1412 static void mmc_detect(struct mmc_host *host)
1413 {
1414         int err;
1415
1416         BUG_ON(!host);
1417         BUG_ON(!host->card);
1418
1419         mmc_get_card(host->card);
1420
1421         /*
1422          * Just check if our card has been removed.
1423          */
1424         err = _mmc_detect_card_removed(host);
1425
1426         mmc_put_card(host->card);
1427
1428         if (err) {
1429                 mmc_remove(host);
1430
1431                 mmc_claim_host(host);
1432                 mmc_detach_bus(host);
1433                 mmc_power_off(host);
1434                 mmc_release_host(host);
1435         }
1436 }
1437
1438 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1439 {
1440         int err = 0;
1441         unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1442                                         EXT_CSD_POWER_OFF_LONG;
1443
1444         BUG_ON(!host);
1445         BUG_ON(!host->card);
1446
1447         mmc_claim_host(host);
1448
1449         if (mmc_card_suspended(host->card))
1450                 goto out;
1451
1452         if (mmc_card_doing_bkops(host->card)) {
1453                 err = mmc_stop_bkops(host->card);
1454                 if (err)
1455                         goto out;
1456         }
1457
1458         err = mmc_cache_ctrl(host, 0);
1459         if (err)
1460                 goto out;
1461
1462         if (mmc_can_poweroff_notify(host->card) &&
1463                 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1464                 err = mmc_poweroff_notify(host->card, notify_type);
1465         else if (mmc_can_sleep(host->card))
1466                 err = mmc_sleep(host);
1467         else if (!mmc_host_is_spi(host))
1468                 err = mmc_deselect_cards(host);
1469         host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
1470
1471         if (!err) {
1472                 mmc_power_off(host);
1473                 mmc_card_set_suspended(host->card);
1474         }
1475 out:
1476         mmc_release_host(host);
1477         return err;
1478 }
1479
1480 /*
1481  * Suspend callback
1482  */
1483 static int mmc_suspend(struct mmc_host *host)
1484 {
1485     int err;
1486
1487     err = _mmc_suspend(host, true);
1488         if (!err) {
1489                 pm_runtime_disable(&host->card->dev);
1490                 pm_runtime_set_suspended(&host->card->dev);
1491         }
1492         
1493         return err;
1494 }
1495
1496 /*
1497  * Resume callback from host.
1498  *
1499  * This function tries to determine if the same card is still present
1500  * and, if so, restore all state to it.
1501  */
1502 static int _mmc_resume(struct mmc_host *host)
1503 {
1504         int err = 0;
1505
1506         BUG_ON(!host);
1507         BUG_ON(!host->card);
1508
1509         mmc_claim_host(host);
1510
1511         if (!mmc_card_suspended(host->card))
1512                 goto out;
1513
1514         mmc_power_up(host, host->card->ocr);
1515         err = mmc_init_card(host, host->card->ocr, host->card);
1516         mmc_card_clr_suspended(host->card);
1517
1518 out:
1519         mmc_release_host(host);
1520         return err;
1521 }
1522
1523
1524 /*
1525  * Callback for resume.
1526  */
1527 static int mmc_resume(struct mmc_host *host)
1528 {
1529         int err = 0;
1530
1531         if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1532                 err = _mmc_resume(host);
1533                 pm_runtime_set_active(&host->card->dev);
1534                 pm_runtime_mark_last_busy(&host->card->dev);
1535         }
1536         pm_runtime_enable(&host->card->dev);
1537         return err;
1538 }
1539 /*
1540  * Shutdown callback
1541  */
1542 static int mmc_shutdown(struct mmc_host *host)
1543 {
1544         int err = 0;
1545
1546         /*
1547          * In a specific case for poweroff notify, we need to resume the card
1548          * before we can shutdown it properly.
1549          */
1550         if (mmc_can_poweroff_notify(host->card) &&
1551                 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
1552                 err = mmc_resume(host);
1553
1554         if (!err)
1555                 err = _mmc_suspend(host, false);
1556
1557         return err;
1558 }
1559
1560 /*
1561  * Callback for runtime_suspend.
1562  */
1563 static int mmc_runtime_suspend(struct mmc_host *host)
1564 {
1565         int err;
1566
1567         if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1568                 return 0;
1569
1570         err = _mmc_suspend(host, true);
1571         if (err)
1572                 pr_err("%s: error %d doing aggessive suspend\n",
1573                         mmc_hostname(host), err);
1574
1575         return err;
1576 }
1577
1578 /*
1579  * Callback for runtime_resume.
1580  */
1581 static int mmc_runtime_resume(struct mmc_host *host)
1582 {
1583         int err;
1584
1585         if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
1586                 return 0;
1587
1588         err = _mmc_resume(host);
1589         if (err)
1590                 pr_err("%s: error %d doing aggessive resume\n",
1591                         mmc_hostname(host), err);
1592
1593         return 0;
1594 }
1595
1596 static int mmc_power_restore(struct mmc_host *host)
1597 {
1598         int ret;
1599
1600         host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
1601         mmc_claim_host(host);
1602         ret = mmc_init_card(host, host->card->ocr, host->card);
1603         mmc_release_host(host);
1604
1605         return ret;
1606 }
1607
1608 static const struct mmc_bus_ops mmc_ops = {
1609         .remove = mmc_remove,
1610         .detect = mmc_detect,
1611         .suspend = NULL,
1612         .resume = NULL,
1613         .power_restore = mmc_power_restore,
1614         .alive = mmc_alive,
1615         .shutdown = mmc_shutdown,
1616 };
1617
1618 static const struct mmc_bus_ops mmc_ops_unsafe = {
1619         .remove = mmc_remove,
1620         .detect = mmc_detect,
1621         .suspend = mmc_suspend,
1622         .resume = mmc_resume,
1623         .runtime_suspend = mmc_runtime_suspend,
1624         .runtime_resume = mmc_runtime_resume,
1625         .power_restore = mmc_power_restore,
1626         .alive = mmc_alive,
1627         .shutdown = mmc_shutdown,
1628 };
1629
1630 static void mmc_attach_bus_ops(struct mmc_host *host)
1631 {
1632         const struct mmc_bus_ops *bus_ops;
1633
1634         if (!mmc_card_is_removable(host))
1635                 bus_ops = &mmc_ops_unsafe;
1636         else
1637                 bus_ops = &mmc_ops;
1638         mmc_attach_bus(host, bus_ops);
1639 }
1640
1641 /*
1642  * Starting point for MMC card init.
1643  */
1644 int mmc_attach_mmc(struct mmc_host *host)
1645 {
1646         int err;
1647         u32 ocr, rocr;
1648
1649         BUG_ON(!host);
1650         WARN_ON(!host->claimed);
1651
1652         /* Set correct bus mode for MMC before attempting attach */
1653         if (!mmc_host_is_spi(host))
1654                 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1655
1656         err = mmc_send_op_cond(host, 0, &ocr);
1657         if (err)
1658                 return err;
1659
1660         mmc_attach_bus_ops(host);
1661         if (host->ocr_avail_mmc)
1662                 host->ocr_avail = host->ocr_avail_mmc;
1663
1664         /*
1665          * We need to get OCR a different way for SPI.
1666          */
1667         if (mmc_host_is_spi(host)) {
1668                 err = mmc_spi_read_ocr(host, 1, &ocr);
1669                 if (err)
1670                         goto err;
1671         }
1672
1673         rocr = mmc_select_voltage(host, ocr);
1674
1675         /*
1676          * Can we support the voltage of the card?
1677          */
1678         if (!rocr) {
1679                 err = -EINVAL;
1680                 goto err;
1681         }
1682
1683         /*
1684          * Detect and init the card.
1685          */
1686         err = mmc_init_card(host, rocr, NULL);
1687         if (err)
1688                 goto err;
1689
1690         mmc_release_host(host);
1691         err = mmc_add_card(host->card);
1692         mmc_claim_host(host);
1693         if (err)
1694                 goto remove_card;
1695
1696         return 0;
1697
1698 remove_card:
1699         mmc_release_host(host);
1700         mmc_remove_card(host->card);
1701         mmc_claim_host(host);
1702         host->card = NULL;
1703 err:
1704         mmc_detach_bus(host);
1705
1706         pr_err("%s: error %d whilst initialising MMC card\n",
1707                 mmc_hostname(host), err);
1708
1709         return err;
1710 }