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