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