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