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