UPSTREAM: mmc: core: update mmc.c upstream version
[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_ES) &&
239             card->ext_csd.strobe_support &&
240             (avail_type & EXT_CSD_CARD_TYPE_HS400))
241                 avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
242
243         card->ext_csd.hs_max_dtr = hs_max_dtr;
244         card->ext_csd.hs200_max_dtr = hs200_max_dtr;
245         card->mmc_avail_type = avail_type;
246 }
247
248 static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
249 {
250         u8 hc_erase_grp_sz, hc_wp_grp_sz;
251
252         /*
253          * Disable these attributes by default
254          */
255         card->ext_csd.enhanced_area_offset = -EINVAL;
256         card->ext_csd.enhanced_area_size = -EINVAL;
257
258         /*
259          * Enhanced area feature support -- check whether the eMMC
260          * card has the Enhanced area enabled.  If so, export enhanced
261          * area offset and size to user by adding sysfs interface.
262          */
263         if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
264             (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
265                 if (card->ext_csd.partition_setting_completed) {
266                         hc_erase_grp_sz =
267                                 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
268                         hc_wp_grp_sz =
269                                 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
270
271                         /*
272                          * calculate the enhanced data area offset, in bytes
273                          */
274                         card->ext_csd.enhanced_area_offset =
275                                 (((unsigned long long)ext_csd[139]) << 24) +
276                                 (((unsigned long long)ext_csd[138]) << 16) +
277                                 (((unsigned long long)ext_csd[137]) << 8) +
278                                 (((unsigned long long)ext_csd[136]));
279                         if (mmc_card_blockaddr(card))
280                                 card->ext_csd.enhanced_area_offset <<= 9;
281                         /*
282                          * calculate the enhanced data area size, in kilobytes
283                          */
284                         card->ext_csd.enhanced_area_size =
285                                 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
286                                 ext_csd[140];
287                         card->ext_csd.enhanced_area_size *=
288                                 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
289                         card->ext_csd.enhanced_area_size <<= 9;
290                 } else {
291                         pr_warn("%s: defines enhanced area without partition setting complete\n",
292                                 mmc_hostname(card->host));
293                 }
294         }
295 }
296
297 static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
298 {
299         int idx;
300         u8 hc_erase_grp_sz, hc_wp_grp_sz;
301         unsigned int part_size;
302
303         /*
304          * General purpose partition feature support --
305          * If ext_csd has the size of general purpose partitions,
306          * set size, part_cfg, partition name in mmc_part.
307          */
308         if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
309             EXT_CSD_PART_SUPPORT_PART_EN) {
310                 hc_erase_grp_sz =
311                         ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
312                 hc_wp_grp_sz =
313                         ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
314
315                 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
316                         if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
317                             !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
318                             !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
319                                 continue;
320                         if (card->ext_csd.partition_setting_completed == 0) {
321                                 pr_warn("%s: has partition size defined without partition complete\n",
322                                         mmc_hostname(card->host));
323                                 break;
324                         }
325                         part_size =
326                                 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
327                                 << 16) +
328                                 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
329                                 << 8) +
330                                 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
331                         part_size *= (size_t)(hc_erase_grp_sz *
332                                 hc_wp_grp_sz);
333                         mmc_part_add(card, part_size << 19,
334                                 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
335                                 "gp%d", idx, false,
336                                 MMC_BLK_DATA_AREA_GP);
337                 }
338         }
339 }
340
341 /* Minimum partition switch timeout in milliseconds */
342 #define MMC_MIN_PART_SWITCH_TIME        300
343
344 /*
345  * Decode extended CSD.
346  */
347 static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
348 {
349         int err = 0, idx;
350         unsigned int part_size;
351         struct device_node *np;
352         bool broken_hpi = false;
353
354         /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
355         card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
356         if (card->csd.structure == 3) {
357                 if (card->ext_csd.raw_ext_csd_structure > 2) {
358                         pr_err("%s: unrecognised EXT_CSD structure "
359                                 "version %d\n", mmc_hostname(card->host),
360                                         card->ext_csd.raw_ext_csd_structure);
361                         err = -EINVAL;
362                         goto out;
363                 }
364         }
365
366         np = mmc_of_find_child_device(card->host, 0);
367         if (np && of_device_is_compatible(np, "mmc-card"))
368                 broken_hpi = of_property_read_bool(np, "broken-hpi");
369         of_node_put(np);
370
371         /*
372          * The EXT_CSD format is meant to be forward compatible. As long
373          * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
374          * are authorized, see JEDEC JESD84-B50 section B.8.
375          */
376         card->ext_csd.rev = ext_csd[EXT_CSD_REV];
377
378         card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
379         card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
380         card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
381         card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
382         if (card->ext_csd.rev >= 2) {
383                 card->ext_csd.sectors =
384                         ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
385                         ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
386                         ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
387                         ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
388
389                 /* Cards with density > 2GiB are sector addressed */
390                 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
391                         mmc_card_set_blockaddr(card);
392         }
393
394         card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
395         card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
396         mmc_select_card_type(card);
397
398         card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
399         card->ext_csd.raw_erase_timeout_mult =
400                 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
401         card->ext_csd.raw_hc_erase_grp_size =
402                 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
403         if (card->ext_csd.rev >= 3) {
404                 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
405                 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
406
407                 /* EXT_CSD value is in units of 10ms, but we store in ms */
408                 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
409                 /* Some eMMC set the value too low so set a minimum */
410                 if (card->ext_csd.part_time &&
411                     card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
412                         card->ext_csd.part_time = MMC_MIN_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 MMC_DEV_ATTR(ocr, "%08x\n", card->ocr);
737
738 static ssize_t mmc_fwrev_show(struct device *dev,
739                               struct device_attribute *attr,
740                               char *buf)
741 {
742         struct mmc_card *card = mmc_dev_to_card(dev);
743
744         if (card->ext_csd.rev < 7) {
745                 return sprintf(buf, "0x%x\n", card->cid.fwrev);
746         } else {
747                 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
748                                card->ext_csd.fwrev);
749         }
750 }
751
752 static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
753
754 static ssize_t mmc_dsr_show(struct device *dev,
755                             struct device_attribute *attr,
756                             char *buf)
757 {
758         struct mmc_card *card = mmc_dev_to_card(dev);
759         struct mmc_host *host = card->host;
760
761         if (card->csd.dsr_imp && host->dsr_req)
762                 return sprintf(buf, "0x%x\n", host->dsr);
763         else
764                 /* return default DSR value */
765                 return sprintf(buf, "0x%x\n", 0x404);
766 }
767
768 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
769
770 static struct attribute *mmc_std_attrs[] = {
771         &dev_attr_cid.attr,
772         &dev_attr_csd.attr,
773         &dev_attr_date.attr,
774         &dev_attr_erase_size.attr,
775         &dev_attr_preferred_erase_size.attr,
776         &dev_attr_fwrev.attr,
777         &dev_attr_ffu_capable.attr,
778         &dev_attr_hwrev.attr,
779         &dev_attr_manfid.attr,
780         &dev_attr_name.attr,
781         &dev_attr_oemid.attr,
782         &dev_attr_prv.attr,
783         &dev_attr_serial.attr,
784         &dev_attr_enhanced_area_offset.attr,
785         &dev_attr_enhanced_area_size.attr,
786         &dev_attr_raw_rpmb_size_mult.attr,
787         &dev_attr_rel_sectors.attr,
788         &dev_attr_ocr.attr,
789         &dev_attr_dsr.attr,
790         NULL,
791 };
792 ATTRIBUTE_GROUPS(mmc_std);
793
794 static struct device_type mmc_type = {
795         .groups = mmc_std_groups,
796 };
797
798 /*
799  * Select the PowerClass for the current bus width
800  * If power class is defined for 4/8 bit bus in the
801  * extended CSD register, select it by executing the
802  * mmc_switch command.
803  */
804 static int __mmc_select_powerclass(struct mmc_card *card,
805                                    unsigned int bus_width)
806 {
807         struct mmc_host *host = card->host;
808         struct mmc_ext_csd *ext_csd = &card->ext_csd;
809         unsigned int pwrclass_val = 0;
810         int err = 0;
811
812         switch (1 << host->ios.vdd) {
813         case MMC_VDD_165_195:
814                 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
815                         pwrclass_val = ext_csd->raw_pwr_cl_26_195;
816                 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
817                         pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
818                                 ext_csd->raw_pwr_cl_52_195 :
819                                 ext_csd->raw_pwr_cl_ddr_52_195;
820                 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
821                         pwrclass_val = ext_csd->raw_pwr_cl_200_195;
822                 break;
823         case MMC_VDD_27_28:
824         case MMC_VDD_28_29:
825         case MMC_VDD_29_30:
826         case MMC_VDD_30_31:
827         case MMC_VDD_31_32:
828         case MMC_VDD_32_33:
829         case MMC_VDD_33_34:
830         case MMC_VDD_34_35:
831         case MMC_VDD_35_36:
832                 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
833                         pwrclass_val = ext_csd->raw_pwr_cl_26_360;
834                 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
835                         pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
836                                 ext_csd->raw_pwr_cl_52_360 :
837                                 ext_csd->raw_pwr_cl_ddr_52_360;
838                 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
839                         pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
840                                 ext_csd->raw_pwr_cl_ddr_200_360 :
841                                 ext_csd->raw_pwr_cl_200_360;
842                 break;
843         default:
844                 pr_warn("%s: Voltage range not supported for power class\n",
845                         mmc_hostname(host));
846                 return -EINVAL;
847         }
848
849         if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
850                 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
851                                 EXT_CSD_PWR_CL_8BIT_SHIFT;
852         else
853                 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
854                                 EXT_CSD_PWR_CL_4BIT_SHIFT;
855
856         /* If the power class is different from the default value */
857         if (pwrclass_val > 0) {
858                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
859                                  EXT_CSD_POWER_CLASS,
860                                  pwrclass_val,
861                                  card->ext_csd.generic_cmd6_time);
862         }
863
864         return err;
865 }
866
867 static int mmc_select_powerclass(struct mmc_card *card)
868 {
869         struct mmc_host *host = card->host;
870         u32 bus_width, ext_csd_bits;
871         int err, ddr;
872
873         /* Power class selection is supported for versions >= 4.0 */
874         if (!mmc_can_ext_csd(card))
875                 return 0;
876
877         bus_width = host->ios.bus_width;
878         /* Power class values are defined only for 4/8 bit bus */
879         if (bus_width == MMC_BUS_WIDTH_1)
880                 return 0;
881
882         ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
883         if (ddr)
884                 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
885                         EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
886         else
887                 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
888                         EXT_CSD_BUS_WIDTH_8 :  EXT_CSD_BUS_WIDTH_4;
889
890         err = __mmc_select_powerclass(card, ext_csd_bits);
891         if (err)
892                 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
893                         mmc_hostname(host), 1 << bus_width, ddr);
894
895         return err;
896 }
897
898 /*
899  * Set the bus speed for the selected speed mode.
900  */
901 static void mmc_set_bus_speed(struct mmc_card *card)
902 {
903         unsigned int max_dtr = (unsigned int)-1;
904
905         if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
906              max_dtr > card->ext_csd.hs200_max_dtr)
907                 max_dtr = card->ext_csd.hs200_max_dtr;
908         else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
909                 max_dtr = card->ext_csd.hs_max_dtr;
910         else if (max_dtr > card->csd.max_dtr)
911                 max_dtr = card->csd.max_dtr;
912
913         mmc_set_clock(card->host, max_dtr);
914 }
915
916 /*
917  * Select the bus width amoung 4-bit and 8-bit(SDR).
918  * If the bus width is changed successfully, return the selected width value.
919  * Zero is returned instead of error value if the wide width is not supported.
920  */
921 static int mmc_select_bus_width(struct mmc_card *card)
922 {
923         static unsigned ext_csd_bits[] = {
924                 EXT_CSD_BUS_WIDTH_8,
925                 EXT_CSD_BUS_WIDTH_4,
926         };
927         static unsigned bus_widths[] = {
928                 MMC_BUS_WIDTH_8,
929                 MMC_BUS_WIDTH_4,
930         };
931         struct mmc_host *host = card->host;
932         unsigned idx, bus_width = 0;
933         int err = 0;
934
935         if (!mmc_can_ext_csd(card) ||
936             !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
937                 return 0;
938
939         idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
940
941         /*
942          * Unlike SD, MMC cards dont have a configuration register to notify
943          * supported bus width. So bus test command should be run to identify
944          * the supported bus width or compare the ext csd values of current
945          * bus width and ext csd values of 1 bit mode read earlier.
946          */
947         for (; idx < ARRAY_SIZE(bus_widths); idx++) {
948                 /*
949                  * Host is capable of 8bit transfer, then switch
950                  * the device to work in 8bit transfer mode. If the
951                  * mmc switch command returns error then switch to
952                  * 4bit transfer mode. On success set the corresponding
953                  * bus width on the host.
954                  */
955                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
956                                  EXT_CSD_BUS_WIDTH,
957                                  ext_csd_bits[idx],
958                                  card->ext_csd.generic_cmd6_time);
959                 if (err)
960                         continue;
961
962                 bus_width = bus_widths[idx];
963                 mmc_set_bus_width(host, bus_width);
964
965                 /*
966                  * If controller can't handle bus width test,
967                  * compare ext_csd previously read in 1 bit mode
968                  * against ext_csd at new bus width
969                  */
970                 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
971                         err = mmc_compare_ext_csds(card, bus_width);
972                 else
973                         err = mmc_bus_test(card, bus_width);
974
975                 if (!err) {
976                         err = bus_width;
977                         break;
978                 } else {
979                         pr_warn("%s: switch to bus width %d failed\n",
980                                 mmc_hostname(host), 1 << bus_width);
981                 }
982         }
983
984         return err;
985 }
986
987 /* Caller must hold re-tuning */
988 static int mmc_switch_status(struct mmc_card *card)
989 {
990         u32 status;
991         int err;
992
993         err = mmc_send_status(card, &status);
994         if (err)
995                 return err;
996
997         return mmc_switch_status_error(card->host, status);
998 }
999
1000 /*
1001  * Switch to the high-speed mode
1002  */
1003 static int mmc_select_hs(struct mmc_card *card)
1004 {
1005         int err;
1006
1007         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1008                            EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1009                            card->ext_csd.generic_cmd6_time,
1010                            true, false, true);
1011         if (!err) {
1012                 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1013                 err = mmc_switch_status(card);
1014         }
1015
1016         if (err)
1017                 pr_warn("%s: switch to high-speed failed, err:%d\n",
1018                         mmc_hostname(card->host), err);
1019
1020         return err;
1021 }
1022
1023 /*
1024  * Activate wide bus and DDR if supported.
1025  */
1026 static int mmc_select_hs_ddr(struct mmc_card *card)
1027 {
1028         struct mmc_host *host = card->host;
1029         u32 bus_width, ext_csd_bits;
1030         int err = 0;
1031
1032         if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1033                 return 0;
1034
1035         bus_width = host->ios.bus_width;
1036         if (bus_width == MMC_BUS_WIDTH_1)
1037                 return 0;
1038
1039         ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1040                 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1041
1042         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1043                         EXT_CSD_BUS_WIDTH,
1044                         ext_csd_bits,
1045                         card->ext_csd.generic_cmd6_time);
1046         if (err) {
1047                 pr_err("%s: switch to bus width %d ddr failed\n",
1048                         mmc_hostname(host), 1 << bus_width);
1049                 return err;
1050         }
1051
1052         /*
1053          * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1054          * signaling.
1055          *
1056          * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1057          *
1058          * 1.8V vccq at 3.3V core voltage (vcc) is not required
1059          * in the JEDEC spec for DDR.
1060          *
1061          * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1062          * host controller can support this, like some of the SDHCI
1063          * controller which connect to an eMMC device. Some of these
1064          * host controller still needs to use 1.8v vccq for supporting
1065          * DDR mode.
1066          *
1067          * So the sequence will be:
1068          * if (host and device can both support 1.2v IO)
1069          *      use 1.2v IO;
1070          * else if (host and device can both support 1.8v IO)
1071          *      use 1.8v IO;
1072          * so if host and device can only support 3.3v IO, this is the
1073          * last choice.
1074          *
1075          * WARNING: eMMC rules are NOT the same as SD DDR
1076          */
1077         err = -EINVAL;
1078         if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1079                 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1080
1081         if (err && (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V))
1082                 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1083
1084         /* make sure vccq is 3.3v after switching disaster */
1085         if (err)
1086                 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1087
1088         if (!err)
1089                 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1090
1091         return err;
1092 }
1093
1094 static int mmc_select_hs400(struct mmc_card *card)
1095 {
1096         struct mmc_host *host = card->host;
1097         unsigned int max_dtr;
1098         int err = 0;
1099         u8 val;
1100
1101         /*
1102          * HS400 mode requires 8-bit bus width
1103          */
1104         if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1105               host->ios.bus_width == MMC_BUS_WIDTH_8))
1106                 return 0;
1107
1108         /* Switch card to HS mode */
1109         val = EXT_CSD_TIMING_HS;
1110         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1111                            EXT_CSD_HS_TIMING, val,
1112                            card->ext_csd.generic_cmd6_time,
1113                            true, false, true);
1114         if (err) {
1115                 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
1116                         mmc_hostname(host), err);
1117                 return err;
1118         }
1119
1120         /* Set host controller to HS timing */
1121         mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1122
1123         /* Reduce frequency to HS frequency */
1124         max_dtr = card->ext_csd.hs_max_dtr;
1125         mmc_set_clock(host, max_dtr);
1126
1127         err = mmc_switch_status(card);
1128         if (err)
1129                 goto out_err;
1130
1131         /* Switch card to DDR */
1132         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1133                          EXT_CSD_BUS_WIDTH,
1134                          EXT_CSD_DDR_BUS_WIDTH_8,
1135                          card->ext_csd.generic_cmd6_time);
1136         if (err) {
1137                 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
1138                         mmc_hostname(host), err);
1139                 return err;
1140         }
1141
1142         /* Switch card to HS400 */
1143         val = EXT_CSD_TIMING_HS400 |
1144               card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1145         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1146                            EXT_CSD_HS_TIMING, val,
1147                            card->ext_csd.generic_cmd6_time,
1148                            true, false, true);
1149         if (err) {
1150                 pr_err("%s: switch to hs400 failed, err:%d\n",
1151                          mmc_hostname(host), err);
1152                 return err;
1153         }
1154
1155         /* Set host controller to HS400 timing and frequency */
1156         mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1157         mmc_set_bus_speed(card);
1158
1159         err = mmc_switch_status(card);
1160         if (err)
1161                 goto out_err;
1162
1163         return 0;
1164
1165 out_err:
1166         pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1167                __func__, err);
1168         return err;
1169 }
1170
1171 int mmc_hs200_to_hs400(struct mmc_card *card)
1172 {
1173         return mmc_select_hs400(card);
1174 }
1175
1176 int mmc_hs400_to_hs200(struct mmc_card *card)
1177 {
1178         struct mmc_host *host = card->host;
1179         unsigned int max_dtr;
1180         int err;
1181         u8 val;
1182
1183         /* Reduce frequency to HS */
1184         max_dtr = card->ext_csd.hs_max_dtr;
1185         mmc_set_clock(host, max_dtr);
1186
1187         /* Switch HS400 to HS DDR */
1188         val = EXT_CSD_TIMING_HS;
1189         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1190                            val, card->ext_csd.generic_cmd6_time,
1191                            true, false, true);
1192         if (err)
1193                 goto out_err;
1194
1195         mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1196
1197         err = mmc_switch_status(card);
1198         if (err)
1199                 goto out_err;
1200
1201         /* Switch HS DDR to HS */
1202         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1203                            EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
1204                            true, false, true);
1205         if (err)
1206                 goto out_err;
1207
1208         mmc_set_timing(host, MMC_TIMING_MMC_HS);
1209
1210         err = mmc_switch_status(card);
1211         if (err)
1212                 goto out_err;
1213
1214         /* Switch HS to HS200 */
1215         val = EXT_CSD_TIMING_HS200 |
1216               card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1217         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1218                            val, card->ext_csd.generic_cmd6_time,
1219                            true, false, true);
1220         if (err)
1221                 goto out_err;
1222
1223         mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1224
1225         err = mmc_switch_status(card);
1226         if (err)
1227                 goto out_err;
1228
1229         mmc_set_bus_speed(card);
1230
1231         return 0;
1232
1233 out_err:
1234         pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1235                __func__, err);
1236         return err;
1237 }
1238
1239 static int mmc_select_hs400es(struct mmc_card *card)
1240 {
1241         struct mmc_host *host = card->host;
1242         int err = 0;
1243         u8 val;
1244
1245         if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1246                 err = -ENOTSUPP;
1247                 goto out_err;
1248         }
1249
1250         err = mmc_select_bus_width(card);
1251         if (err < 0)
1252                 goto out_err;
1253
1254         /* Switch card to HS mode */
1255         err = mmc_select_hs(card);
1256         if (err)
1257                 goto out_err;
1258
1259         err = mmc_switch_status(card);
1260         if (err)
1261                 goto out_err;
1262
1263         /* Switch card to DDR with strobe bit */
1264         val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1265         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1266                          EXT_CSD_BUS_WIDTH,
1267                          val,
1268                          card->ext_csd.generic_cmd6_time);
1269         if (err) {
1270                 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1271                         mmc_hostname(host), err);
1272                 goto out_err;
1273         }
1274
1275         /* Switch card to HS400 */
1276         val = EXT_CSD_TIMING_HS400 |
1277               card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1278         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1279                            EXT_CSD_HS_TIMING, val,
1280                            card->ext_csd.generic_cmd6_time,
1281                            true, false, true);
1282         if (err) {
1283                 pr_err("%s: switch to hs400es failed, err:%d\n",
1284                         mmc_hostname(host), err);
1285                 goto out_err;
1286         }
1287
1288         /* Set host controller to HS400 timing and frequency */
1289         mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1290
1291         /* Controller enable enhanced strobe function */
1292         host->ios.enhanced_strobe = true;
1293         if (host->ops->hs400_enhanced_strobe)
1294                 host->ops->hs400_enhanced_strobe(host, &host->ios);
1295
1296         err = mmc_switch_status(card);
1297         if (err)
1298                 goto out_err;
1299
1300         return 0;
1301
1302 out_err:
1303         pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1304                __func__, err);
1305         return err;
1306 }
1307
1308 static void mmc_select_driver_type(struct mmc_card *card)
1309 {
1310         int card_drv_type, drive_strength, drv_type;
1311
1312         card_drv_type = card->ext_csd.raw_driver_strength |
1313                         mmc_driver_type_mask(0);
1314
1315         drive_strength = mmc_select_drive_strength(card,
1316                                                    card->ext_csd.hs200_max_dtr,
1317                                                    card_drv_type, &drv_type);
1318
1319         card->drive_strength = drive_strength;
1320
1321         if (drv_type)
1322                 mmc_set_driver_type(card->host, drv_type);
1323 }
1324
1325 /*
1326  * For device supporting HS200 mode, the following sequence
1327  * should be done before executing the tuning process.
1328  * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1329  * 2. switch to HS200 mode
1330  * 3. set the clock to > 52Mhz and <=200MHz
1331  */
1332 static int mmc_select_hs200(struct mmc_card *card)
1333 {
1334         struct mmc_host *host = card->host;
1335         unsigned int old_timing, old_signal_voltage;
1336         int err = -EINVAL;
1337         u8 val;
1338
1339         old_signal_voltage = host->ios.signal_voltage;
1340         if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1341                 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1342
1343         if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1344                 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1345
1346         /* If fails try again during next card power cycle */
1347         if (err)
1348                 return err;
1349
1350         mmc_select_driver_type(card);
1351
1352         /*
1353          * Set the bus width(4 or 8) with host's support and
1354          * switch to HS200 mode if bus width is set successfully.
1355          */
1356         err = mmc_select_bus_width(card);
1357         if (err > 0) {
1358                 val = EXT_CSD_TIMING_HS200 |
1359                       card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1360                 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1361                                    EXT_CSD_HS_TIMING, val,
1362                                    card->ext_csd.generic_cmd6_time,
1363                                    true, false, true);
1364                 if (err)
1365                         goto err;
1366                 old_timing = host->ios.timing;
1367                 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1368
1369                 err = mmc_switch_status(card);
1370                 /*
1371                  * mmc_select_timing() assumes timing has not changed if
1372                  * it is a switch error.
1373                  */
1374                 if (err == -EBADMSG)
1375                         mmc_set_timing(host, old_timing);
1376         }
1377 err:
1378         if (err) {
1379                 /* fall back to the old signal voltage, if fails report error */
1380                 if (__mmc_set_signal_voltage(host, old_signal_voltage))
1381                         err = -EIO;
1382
1383                 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1384                        __func__, err);
1385         }
1386         return err;
1387 }
1388
1389 /*
1390  * Activate High Speed, HS200 or HS400ES mode if supported.
1391  */
1392 static int mmc_select_timing(struct mmc_card *card)
1393 {
1394         int err = 0;
1395
1396         if (!mmc_can_ext_csd(card))
1397                 goto bus_speed;
1398
1399         if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1400                 err = mmc_select_hs400es(card);
1401         else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1402                 err = mmc_select_hs200(card);
1403         else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1404                 err = mmc_select_hs(card);
1405
1406         if (err && err != -EBADMSG)
1407                 return err;
1408
1409 bus_speed:
1410         /*
1411          * Set the bus speed to the selected bus timing.
1412          * If timing is not selected, backward compatible is the default.
1413          */
1414         mmc_set_bus_speed(card);
1415         return 0;
1416 }
1417
1418 /*
1419  * Execute tuning sequence to seek the proper bus operating
1420  * conditions for HS200 and HS400, which sends CMD21 to the device.
1421  */
1422 static int mmc_hs200_tuning(struct mmc_card *card)
1423 {
1424         struct mmc_host *host = card->host;
1425
1426         /*
1427          * Timing should be adjusted to the HS400 target
1428          * operation frequency for tuning process
1429          */
1430         if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1431             host->ios.bus_width == MMC_BUS_WIDTH_8)
1432                 if (host->ops->prepare_hs400_tuning)
1433                         host->ops->prepare_hs400_tuning(host, &host->ios);
1434
1435         return mmc_execute_tuning(card);
1436 }
1437
1438 /*
1439  * Handle the detection and initialisation of a card.
1440  *
1441  * In the case of a resume, "oldcard" will contain the card
1442  * we're trying to reinitialise.
1443  */
1444 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1445         struct mmc_card *oldcard)
1446 {
1447         struct mmc_card *card;
1448         int err;
1449         u32 cid[4];
1450         u32 rocr;
1451
1452         BUG_ON(!host);
1453         WARN_ON(!host->claimed);
1454
1455         /* Set correct bus mode for MMC before attempting init */
1456         if (!mmc_host_is_spi(host))
1457                 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1458
1459         /*
1460          * Since we're changing the OCR value, we seem to
1461          * need to tell some cards to go back to the idle
1462          * state.  We wait 1ms to give cards time to
1463          * respond.
1464          * mmc_go_idle is needed for eMMC that are asleep
1465          */
1466         mmc_go_idle(host);
1467
1468         /* The extra bit indicates that we support high capacity */
1469         err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1470         if (err)
1471                 goto err;
1472
1473         /*
1474          * For SPI, enable CRC as appropriate.
1475          */
1476         if (mmc_host_is_spi(host)) {
1477                 err = mmc_spi_set_crc(host, use_spi_crc);
1478                 if (err)
1479                         goto err;
1480         }
1481
1482         /*
1483          * Fetch CID from card.
1484          */
1485         if (mmc_host_is_spi(host))
1486                 err = mmc_send_cid(host, cid);
1487         else
1488                 err = mmc_all_send_cid(host, cid);
1489         if (err)
1490                 goto err;
1491
1492         if (oldcard) {
1493                 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1494                         err = -ENOENT;
1495                         goto err;
1496                 }
1497
1498                 card = oldcard;
1499         } else {
1500                 /*
1501                  * Allocate card structure.
1502                  */
1503                 card = mmc_alloc_card(host, &mmc_type);
1504                 if (IS_ERR(card)) {
1505                         err = PTR_ERR(card);
1506                         goto err;
1507                 }
1508
1509                 card->ocr = ocr;
1510                 card->type = MMC_TYPE_MMC;
1511                 card->rca = 1;
1512                 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1513         }
1514
1515         /*
1516          * Call the optional HC's init_card function to handle quirks.
1517          */
1518         if (host->ops->init_card)
1519                 host->ops->init_card(host, card);
1520
1521         /*
1522          * For native busses:  set card RCA and quit open drain mode.
1523          */
1524         if (!mmc_host_is_spi(host)) {
1525                 err = mmc_set_relative_addr(card);
1526                 if (err)
1527                         goto free_card;
1528
1529                 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1530         }
1531
1532         if (!oldcard) {
1533                 /*
1534                  * Fetch CSD from card.
1535                  */
1536                 err = mmc_send_csd(card, card->raw_csd);
1537                 if (err)
1538                         goto free_card;
1539
1540                 err = mmc_decode_csd(card);
1541                 if (err)
1542                         goto free_card;
1543                 err = mmc_decode_cid(card);
1544                 if (err)
1545                         goto free_card;
1546         }
1547
1548         /*
1549          * handling only for cards supporting DSR and hosts requesting
1550          * DSR configuration
1551          */
1552         if (card->csd.dsr_imp && host->dsr_req)
1553                 mmc_set_dsr(host);
1554
1555         /*
1556          * Select card, as all following commands rely on that.
1557          */
1558         if (!mmc_host_is_spi(host)) {
1559                 err = mmc_select_card(card);
1560                 if (err)
1561                         goto free_card;
1562         }
1563
1564         if (!oldcard) {
1565                 /* Read extended CSD. */
1566                 err = mmc_read_ext_csd(card);
1567                 if (err)
1568                         goto free_card;
1569
1570                 /*
1571                  * If doing byte addressing, check if required to do sector
1572                  * addressing.  Handle the case of <2GB cards needing sector
1573                  * addressing.  See section 8.1 JEDEC Standard JED84-A441;
1574                  * ocr register has bit 30 set for sector addressing.
1575                  */
1576                 if (rocr & BIT(30))
1577                         mmc_card_set_blockaddr(card);
1578
1579                 /* Erase size depends on CSD and Extended CSD */
1580                 mmc_set_erase_size(card);
1581         }
1582
1583         /*
1584          * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
1585          * bit.  This bit will be lost every time after a reset or power off.
1586          */
1587         if (card->ext_csd.partition_setting_completed ||
1588             (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
1589                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1590                                  EXT_CSD_ERASE_GROUP_DEF, 1,
1591                                  card->ext_csd.generic_cmd6_time);
1592
1593                 if (err && err != -EBADMSG)
1594                         goto free_card;
1595
1596                 if (err) {
1597                         err = 0;
1598                         /*
1599                          * Just disable enhanced area off & sz
1600                          * will try to enable ERASE_GROUP_DEF
1601                          * during next time reinit
1602                          */
1603                         card->ext_csd.enhanced_area_offset = -EINVAL;
1604                         card->ext_csd.enhanced_area_size = -EINVAL;
1605                 } else {
1606                         card->ext_csd.erase_group_def = 1;
1607                         /*
1608                          * enable ERASE_GRP_DEF successfully.
1609                          * This will affect the erase size, so
1610                          * here need to reset erase size
1611                          */
1612                         mmc_set_erase_size(card);
1613                 }
1614         }
1615
1616         /*
1617          * Ensure eMMC user default partition is enabled
1618          */
1619         if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1620                 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1621                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1622                                  card->ext_csd.part_config,
1623                                  card->ext_csd.part_time);
1624                 if (err && err != -EBADMSG)
1625                         goto free_card;
1626         }
1627
1628         /*
1629          * Enable power_off_notification byte in the ext_csd register
1630          */
1631         if (card->ext_csd.rev >= 6) {
1632                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1633                                  EXT_CSD_POWER_OFF_NOTIFICATION,
1634                                  EXT_CSD_POWER_ON,
1635                                  card->ext_csd.generic_cmd6_time);
1636                 if (err && err != -EBADMSG)
1637                         goto free_card;
1638
1639                 /*
1640                  * The err can be -EBADMSG or 0,
1641                  * so check for success and update the flag
1642                  */
1643                 if (!err)
1644                         card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1645         }
1646
1647         /*
1648          * Select timing interface
1649          */
1650         err = mmc_select_timing(card);
1651         if (err)
1652                 goto free_card;
1653
1654         if (mmc_card_hs200(card)) {
1655                 err = mmc_hs200_tuning(card);
1656                 if (err)
1657                         goto free_card;
1658
1659                 err = mmc_select_hs400(card);
1660                 if (err)
1661                         goto free_card;
1662         } else if (mmc_card_hs(card)) {
1663                 /* Select the desired bus width optionally */
1664                 err = mmc_select_bus_width(card);
1665                 if (err > 0) {
1666                         err = mmc_select_hs_ddr(card);
1667                         if (err)
1668                                 goto free_card;
1669                 }
1670         }
1671
1672         /*
1673          * Choose the power class with selected bus interface
1674          */
1675         mmc_select_powerclass(card);
1676
1677         /*
1678          * Enable HPI feature (if supported)
1679          */
1680         if (card->ext_csd.hpi) {
1681                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1682                                 EXT_CSD_HPI_MGMT, 1,
1683                                 card->ext_csd.generic_cmd6_time);
1684                 if (err && err != -EBADMSG)
1685                         goto free_card;
1686                 if (err) {
1687                         pr_warn("%s: Enabling HPI failed\n",
1688                                 mmc_hostname(card->host));
1689                         err = 0;
1690                 } else
1691                         card->ext_csd.hpi_en = 1;
1692         }
1693
1694         /*
1695          * If cache size is higher than 0, this indicates
1696          * the existence of cache and it can be turned on.
1697          */
1698         if (card->ext_csd.cache_size > 0) {
1699                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1700                                 EXT_CSD_CACHE_CTRL, 1,
1701                                 card->ext_csd.generic_cmd6_time);
1702                 if (err && err != -EBADMSG)
1703                         goto free_card;
1704
1705                 /*
1706                  * Only if no error, cache is turned on successfully.
1707                  */
1708                 if (err) {
1709                         pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1710                                 mmc_hostname(card->host), err);
1711                         card->ext_csd.cache_ctrl = 0;
1712                         err = 0;
1713                 } else {
1714                         card->ext_csd.cache_ctrl = 1;
1715                 }
1716         }
1717
1718         /*
1719          * The mandatory minimum values are defined for packed command.
1720          * read: 5, write: 3
1721          */
1722         if (card->ext_csd.max_packed_writes >= 3 &&
1723             card->ext_csd.max_packed_reads >= 5 &&
1724             host->caps2 & MMC_CAP2_PACKED_CMD) {
1725                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1726                                 EXT_CSD_EXP_EVENTS_CTRL,
1727                                 EXT_CSD_PACKED_EVENT_EN,
1728                                 card->ext_csd.generic_cmd6_time);
1729                 if (err && err != -EBADMSG)
1730                         goto free_card;
1731                 if (err) {
1732                         pr_warn("%s: Enabling packed event failed\n",
1733                                 mmc_hostname(card->host));
1734                         card->ext_csd.packed_event_en = 0;
1735                         err = 0;
1736                 } else {
1737                         card->ext_csd.packed_event_en = 1;
1738                 }
1739         }
1740
1741         if (!oldcard)
1742                 host->card = card;
1743
1744         return 0;
1745
1746 free_card:
1747         if (!oldcard)
1748                 mmc_remove_card(card);
1749 err:
1750         return err;
1751 }
1752
1753 static int mmc_can_sleep(struct mmc_card *card)
1754 {
1755         return (card && card->ext_csd.rev >= 3);
1756 }
1757
1758 static int mmc_sleep(struct mmc_host *host)
1759 {
1760         struct mmc_command cmd = {0};
1761         struct mmc_card *card = host->card;
1762         unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1763         int err;
1764
1765         /* Re-tuning can't be done once the card is deselected */
1766         mmc_retune_hold(host);
1767
1768         err = mmc_deselect_cards(host);
1769         if (err)
1770                 goto out_release;
1771
1772         cmd.opcode = MMC_SLEEP_AWAKE;
1773         cmd.arg = card->rca << 16;
1774         cmd.arg |= 1 << 15;
1775
1776         /*
1777          * If the max_busy_timeout of the host is specified, validate it against
1778          * the sleep cmd timeout. A failure means we need to prevent the host
1779          * from doing hw busy detection, which is done by converting to a R1
1780          * response instead of a R1B.
1781          */
1782         if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1783                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1784         } else {
1785                 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1786                 cmd.busy_timeout = timeout_ms;
1787         }
1788
1789         err = mmc_wait_for_cmd(host, &cmd, 0);
1790         if (err)
1791                 goto out_release;
1792
1793         /*
1794          * If the host does not wait while the card signals busy, then we will
1795          * will have to wait the sleep/awake timeout.  Note, we cannot use the
1796          * SEND_STATUS command to poll the status because that command (and most
1797          * others) is invalid while the card sleeps.
1798          */
1799         if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1800                 mmc_delay(timeout_ms);
1801
1802 out_release:
1803         mmc_retune_release(host);
1804         return err;
1805 }
1806
1807 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1808 {
1809         return card &&
1810                 mmc_card_mmc(card) &&
1811                 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1812 }
1813
1814 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1815 {
1816         unsigned int timeout = card->ext_csd.generic_cmd6_time;
1817         int err;
1818
1819         /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1820         if (notify_type == EXT_CSD_POWER_OFF_LONG)
1821                 timeout = card->ext_csd.power_off_longtime;
1822
1823         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1824                         EXT_CSD_POWER_OFF_NOTIFICATION,
1825                         notify_type, timeout, true, false, false);
1826         if (err)
1827                 pr_err("%s: Power Off Notification timed out, %u\n",
1828                        mmc_hostname(card->host), timeout);
1829
1830         /* Disable the power off notification after the switch operation. */
1831         card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1832
1833         return err;
1834 }
1835
1836 /*
1837  * Host is being removed. Free up the current card.
1838  */
1839 static void mmc_remove(struct mmc_host *host)
1840 {
1841         BUG_ON(!host);
1842         BUG_ON(!host->card);
1843
1844         mmc_remove_card(host->card);
1845         host->card = NULL;
1846 }
1847
1848 /*
1849  * Card detection - card is alive.
1850  */
1851 static int mmc_alive(struct mmc_host *host)
1852 {
1853         return mmc_send_status(host->card, NULL);
1854 }
1855
1856 /*
1857  * Card detection callback from host.
1858  */
1859 static void mmc_detect(struct mmc_host *host)
1860 {
1861         int err;
1862
1863         BUG_ON(!host);
1864         BUG_ON(!host->card);
1865
1866         mmc_get_card(host->card);
1867
1868         /*
1869          * Just check if our card has been removed.
1870          */
1871         err = _mmc_detect_card_removed(host);
1872
1873         mmc_put_card(host->card);
1874
1875         if (err) {
1876                 mmc_remove(host);
1877
1878                 mmc_claim_host(host);
1879                 mmc_detach_bus(host);
1880                 mmc_power_off(host);
1881                 mmc_release_host(host);
1882         }
1883 }
1884
1885 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1886 {
1887         int err = 0;
1888         unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1889                                         EXT_CSD_POWER_OFF_LONG;
1890
1891         BUG_ON(!host);
1892         BUG_ON(!host->card);
1893
1894         mmc_claim_host(host);
1895
1896         if (mmc_card_suspended(host->card))
1897                 goto out;
1898
1899         if (mmc_card_doing_bkops(host->card)) {
1900                 err = mmc_stop_bkops(host->card);
1901                 if (err)
1902                         goto out;
1903         }
1904
1905         err = mmc_flush_cache(host->card);
1906         if (err)
1907                 goto out;
1908
1909         if (mmc_can_poweroff_notify(host->card) &&
1910                 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1911                 err = mmc_poweroff_notify(host->card, notify_type);
1912         else if (mmc_can_sleep(host->card))
1913                 err = mmc_sleep(host);
1914         else if (!mmc_host_is_spi(host))
1915                 err = mmc_deselect_cards(host);
1916
1917         if (!err) {
1918                 mmc_power_off(host);
1919                 mmc_card_set_suspended(host->card);
1920         }
1921 out:
1922         mmc_release_host(host);
1923         return err;
1924 }
1925
1926 /*
1927  * Suspend callback
1928  */
1929 static int mmc_suspend(struct mmc_host *host)
1930 {
1931         int err;
1932
1933         err = _mmc_suspend(host, true);
1934         if (!err) {
1935                 pm_runtime_disable(&host->card->dev);
1936                 pm_runtime_set_suspended(&host->card->dev);
1937         }
1938
1939         return err;
1940 }
1941
1942 /*
1943  * This function tries to determine if the same card is still present
1944  * and, if so, restore all state to it.
1945  */
1946 static int _mmc_resume(struct mmc_host *host)
1947 {
1948         int err = 0;
1949         int i;
1950
1951         BUG_ON(!host);
1952         BUG_ON(!host->card);
1953
1954         mmc_claim_host(host);
1955
1956         if (!mmc_card_suspended(host->card))
1957                 goto out;
1958
1959         /*
1960          * Let's try to fallback the host->f_init
1961          * if failing to init mmc card after resume.
1962          */
1963         for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1964                 if (host->f_init < max(freqs[i], host->f_min))
1965                         continue;
1966                 else
1967                         host->f_init = max(freqs[i], host->f_min);
1968
1969                 mmc_power_up(host, host->card->ocr);
1970                 err = mmc_init_card(host, host->card->ocr, host->card);
1971                 if (!err)
1972                         break;
1973         }
1974
1975         mmc_card_clr_suspended(host->card);
1976
1977 out:
1978         mmc_release_host(host);
1979         return err;
1980 }
1981
1982 /*
1983  * Shutdown callback
1984  */
1985 static int mmc_shutdown(struct mmc_host *host)
1986 {
1987         int err = 0;
1988
1989         /*
1990          * In a specific case for poweroff notify, we need to resume the card
1991          * before we can shutdown it properly.
1992          */
1993         if (mmc_can_poweroff_notify(host->card) &&
1994                 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
1995                 err = _mmc_resume(host);
1996
1997         if (!err)
1998                 err = _mmc_suspend(host, false);
1999
2000         return err;
2001 }
2002
2003 /*
2004  * Callback for resume.
2005  */
2006 static int mmc_resume(struct mmc_host *host)
2007 {
2008         pm_runtime_enable(&host->card->dev);
2009         return 0;
2010 }
2011
2012 /*
2013  * Callback for runtime_suspend.
2014  */
2015 static int mmc_runtime_suspend(struct mmc_host *host)
2016 {
2017         int err;
2018
2019         if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2020                 return 0;
2021
2022         err = _mmc_suspend(host, true);
2023         if (err)
2024                 pr_err("%s: error %d doing aggressive suspend\n",
2025                         mmc_hostname(host), err);
2026
2027         return err;
2028 }
2029
2030 /*
2031  * Callback for runtime_resume.
2032  */
2033 static int mmc_runtime_resume(struct mmc_host *host)
2034 {
2035         int err;
2036
2037         err = _mmc_resume(host);
2038         if (err && err != -ENOMEDIUM)
2039                 pr_err("%s: error %d doing runtime resume\n",
2040                         mmc_hostname(host), err);
2041
2042         return 0;
2043 }
2044
2045 int mmc_can_reset(struct mmc_card *card)
2046 {
2047         u8 rst_n_function;
2048
2049         rst_n_function = card->ext_csd.rst_n_function;
2050         if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2051                 return 0;
2052         return 1;
2053 }
2054 EXPORT_SYMBOL(mmc_can_reset);
2055
2056 static int mmc_reset(struct mmc_host *host)
2057 {
2058         struct mmc_card *card = host->card;
2059
2060         if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2061                 return -EOPNOTSUPP;
2062
2063         if (!mmc_can_reset(card))
2064                 return -EOPNOTSUPP;
2065
2066         mmc_set_clock(host, host->f_init);
2067
2068         host->ops->hw_reset(host);
2069
2070         /* Set initial state and call mmc_set_ios */
2071         mmc_set_initial_state(host);
2072
2073         return mmc_init_card(host, card->ocr, card);
2074 }
2075
2076 static const struct mmc_bus_ops mmc_ops = {
2077         .remove = mmc_remove,
2078         .detect = mmc_detect,
2079         .suspend = mmc_suspend,
2080         .resume = mmc_resume,
2081         .runtime_suspend = mmc_runtime_suspend,
2082         .runtime_resume = mmc_runtime_resume,
2083         .alive = mmc_alive,
2084         .shutdown = mmc_shutdown,
2085         .reset = mmc_reset,
2086 };
2087
2088 /*
2089  * Starting point for MMC card init.
2090  */
2091 int mmc_attach_mmc(struct mmc_host *host)
2092 {
2093         int err;
2094         u32 ocr, rocr;
2095
2096         BUG_ON(!host);
2097         WARN_ON(!host->claimed);
2098
2099         /* Set correct bus mode for MMC before attempting attach */
2100         if (!mmc_host_is_spi(host))
2101                 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2102
2103         err = mmc_send_op_cond(host, 0, &ocr);
2104         if (err)
2105                 return err;
2106
2107         mmc_attach_bus(host, &mmc_ops);
2108         if (host->ocr_avail_mmc)
2109                 host->ocr_avail = host->ocr_avail_mmc;
2110
2111         /*
2112          * We need to get OCR a different way for SPI.
2113          */
2114         if (mmc_host_is_spi(host)) {
2115                 err = mmc_spi_read_ocr(host, 1, &ocr);
2116                 if (err)
2117                         goto err;
2118         }
2119
2120         rocr = mmc_select_voltage(host, ocr);
2121
2122         /*
2123          * Can we support the voltage of the card?
2124          */
2125         if (!rocr) {
2126                 err = -EINVAL;
2127                 goto err;
2128         }
2129
2130         /*
2131          * Detect and init the card.
2132          */
2133         err = mmc_init_card(host, rocr, NULL);
2134         if (err)
2135                 goto err;
2136
2137         mmc_release_host(host);
2138         err = mmc_add_card(host->card);
2139         if (err)
2140                 goto remove_card;
2141
2142         mmc_claim_host(host);
2143         return 0;
2144
2145 remove_card:
2146         mmc_remove_card(host->card);
2147         mmc_claim_host(host);
2148         host->card = NULL;
2149 err:
2150         mmc_detach_bus(host);
2151
2152         pr_err("%s: error %d whilst initialising MMC card\n",
2153                 mmc_hostname(host), err);
2154
2155         return err;
2156 }