blob: f830970958e6796fcd9fb0fa32e16e402f73c360 [file] [log] [blame]
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/core/mmc.c
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003 *
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>
Hans de Goede81f8a7b2015-04-01 17:26:23 +020014#include <linux/of.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Gortmaker0205a902011-07-15 12:01:27 -040016#include <linux/stat.h>
Ulf Hansson0cb403a2013-10-10 14:20:05 +020017#include <linux/pm_runtime.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010018
19#include <linux/mmc/host.h>
20#include <linux/mmc/card.h>
21#include <linux/mmc/mmc.h>
22
23#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010024#include "card.h"
Adrian Hunter436f8da2015-05-07 13:10:18 +030025#include "host.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020026#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010027#include "mmc_ops.h"
Shawn Lin28fc64a2017-02-15 16:35:28 +080028#include "quirks.h"
Philip Rakity4c4cb172011-05-13 11:17:18 +053029#include "sd_ops.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010030
Ulf Hanssonfe1b5702016-11-04 18:32:33 +010031#define DEFAULT_CMD6_TIMEOUT_MS 500
32
Pierre Ossman7ea239d2006-12-31 00:11:32 +010033static const unsigned int tran_exp[] = {
34 10000, 100000, 1000000, 10000000,
35 0, 0, 0, 0
36};
37
38static const unsigned char tran_mant[] = {
39 0, 10, 12, 13, 15, 20, 25, 30,
40 35, 40, 45, 50, 55, 60, 70, 80,
41};
42
43static const unsigned int tacc_exp[] = {
44 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
45};
46
47static const unsigned int tacc_mant[] = {
48 0, 10, 12, 13, 15, 20, 25, 30,
49 35, 40, 45, 50, 55, 60, 70, 80,
50};
51
Pratibhasagar V53202262016-06-09 18:09:31 -040052static const struct mmc_fixup mmc_ext_csd_fixups[] = {
53 /*
54 * Certain Hynix eMMC 4.41 cards might get broken when HPI feature
55 * is used so disable the HPI feature for such buggy cards.
56 */
57 MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_HYNIX,
58 0x014a, add_quirk, MMC_QUIRK_BROKEN_HPI, 5),
59
60 END_FIXUP
61};
62
Pierre Ossman7ea239d2006-12-31 00:11:32 +010063#define UNSTUFF_BITS(resp,start,size) \
64 ({ \
65 const int __size = size; \
66 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
67 const int __off = 3 - ((start) / 32); \
68 const int __shft = (start) & 31; \
69 u32 __res; \
70 \
71 __res = resp[__off] >> __shft; \
72 if (__size + __shft > 32) \
73 __res |= resp[__off-1] << ((32 - __shft) % 32); \
74 __res & __mask; \
75 })
76
77/*
78 * Given the decoded CSD structure, decode the raw CID to our CID structure.
79 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020080static int mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010081{
82 u32 *resp = card->raw_cid;
83
84 /*
85 * The selection of the format here is based upon published
86 * specs from sandisk and from what people have reported.
87 */
88 switch (card->csd.mmca_vsn) {
89 case 0: /* MMC v1.0 - v1.2 */
90 case 1: /* MMC v1.4 */
91 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
92 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
93 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
94 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
95 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
96 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
97 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
98 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
99 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
100 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
101 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
102 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
103 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
104 break;
105
106 case 2: /* MMC v2.0 - v2.2 */
107 case 3: /* MMC v3.1 - v3.3 */
108 case 4: /* MMC v4 */
109 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
110 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
111 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
112 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
113 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
114 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
115 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
116 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800117 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100118 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
119 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
120 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
121 break;
122
123 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530124 pr_err("%s: card has unknown MMCA version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100125 mmc_hostname(card->host), card->csd.mmca_vsn);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200126 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100127 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200128
129 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100130}
131
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700132static void mmc_set_erase_size(struct mmc_card *card)
133{
134 if (card->ext_csd.erase_group_def & 1)
135 card->erase_size = card->ext_csd.hc_erase_size;
136 else
137 card->erase_size = card->csd.erase_size;
138
139 mmc_init_erase(card);
140}
141
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100142/*
143 * Given a 128-bit response, decode to our card CSD structure.
144 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200145static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100146{
147 struct mmc_csd *csd = &card->csd;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700148 unsigned int e, m, a, b;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100149 u32 *resp = card->raw_csd;
150
151 /*
152 * We only understand CSD structure v1.1 and v1.2.
153 * v1.2 has extra information in bits 15, 11 and 10.
Kyungmin Park6da24b72010-08-10 18:01:36 -0700154 * We also support eMMC v4.4 & v4.41.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100155 */
Kyungmin Park6da24b72010-08-10 18:01:36 -0700156 csd->structure = UNSTUFF_BITS(resp, 126, 2);
157 if (csd->structure == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530158 pr_err("%s: unrecognised CSD structure version %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700159 mmc_hostname(card->host), csd->structure);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200160 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100161 }
162
163 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
164 m = UNSTUFF_BITS(resp, 115, 4);
165 e = UNSTUFF_BITS(resp, 112, 3);
166 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
167 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
168
169 m = UNSTUFF_BITS(resp, 99, 4);
170 e = UNSTUFF_BITS(resp, 96, 3);
171 csd->max_dtr = tran_exp[e] * tran_mant[m];
172 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
173
174 e = UNSTUFF_BITS(resp, 47, 3);
175 m = UNSTUFF_BITS(resp, 62, 12);
176 csd->capacity = (1 + m) << (e + 2);
177
178 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
179 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
180 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
181 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
Sascha Hauer3d705d12014-08-19 10:45:51 +0200182 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100183 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
184 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
185 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200186
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700187 if (csd->write_blkbits >= 9) {
188 a = UNSTUFF_BITS(resp, 42, 5);
189 b = UNSTUFF_BITS(resp, 37, 5);
190 csd->erase_size = (a + 1) * (b + 1);
191 csd->erase_size <<= csd->write_blkbits - 9;
192 }
193
Pierre Ossmanbd766312007-05-01 16:11:57 +0200194 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100195}
196
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900197static void mmc_select_card_type(struct mmc_card *card)
198{
199 struct mmc_host *host = card->host;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900200 u8 card_type = card->ext_csd.raw_card_type;
Lee Jones5f1a4dd2012-11-14 12:35:51 +0000201 u32 caps = host->caps, caps2 = host->caps2;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900202 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900203 unsigned int avail_type = 0;
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900204
205 if (caps & MMC_CAP_MMC_HIGHSPEED &&
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900206 card_type & EXT_CSD_CARD_TYPE_HS_26) {
207 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
208 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
209 }
210
211 if (caps & MMC_CAP_MMC_HIGHSPEED &&
212 card_type & EXT_CSD_CARD_TYPE_HS_52) {
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900213 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900214 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
215 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900216
Ulf Hansson20f921b2017-01-25 12:04:17 +0100217 if (caps & (MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR) &&
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900218 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900219 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900220 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
221 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900222
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900223 if (caps & MMC_CAP_1_2V_DDR &&
224 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
225 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
226 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
227 }
228
229 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
230 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
Seungwon Jeon577fb132014-04-23 17:08:44 +0900231 hs200_max_dtr = MMC_HS200_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900232 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
233 }
234
235 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
236 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
Seungwon Jeon577fb132014-04-23 17:08:44 +0900237 hs200_max_dtr = MMC_HS200_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900238 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
239 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900240
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900241 if (caps2 & MMC_CAP2_HS400_1_8V &&
242 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
243 hs200_max_dtr = MMC_HS200_MAX_DTR;
244 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
245 }
246
247 if (caps2 & MMC_CAP2_HS400_1_2V &&
248 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
249 hs200_max_dtr = MMC_HS200_MAX_DTR;
250 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
251 }
252
Shawn Lin81ac2af2016-05-26 09:56:22 +0800253 if ((caps2 & MMC_CAP2_HS400_ES) &&
254 card->ext_csd.strobe_support &&
255 (avail_type & EXT_CSD_CARD_TYPE_HS400))
256 avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
257
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900258 card->ext_csd.hs_max_dtr = hs_max_dtr;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900259 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900260 card->mmc_avail_type = avail_type;
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900261}
262
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200263static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
264{
Grégory Soutadé994324b2014-09-15 17:47:11 +0200265 u8 hc_erase_grp_sz, hc_wp_grp_sz;
266
267 /*
268 * Disable these attributes by default
269 */
270 card->ext_csd.enhanced_area_offset = -EINVAL;
271 card->ext_csd.enhanced_area_size = -EINVAL;
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200272
273 /*
274 * Enhanced area feature support -- check whether the eMMC
275 * card has the Enhanced area enabled. If so, export enhanced
276 * area offset and size to user by adding sysfs interface.
277 */
278 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
279 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
Grégory Soutadé994324b2014-09-15 17:47:11 +0200280 if (card->ext_csd.partition_setting_completed) {
281 hc_erase_grp_sz =
282 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
283 hc_wp_grp_sz =
284 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200285
Grégory Soutadé994324b2014-09-15 17:47:11 +0200286 /*
287 * calculate the enhanced data area offset, in bytes
288 */
289 card->ext_csd.enhanced_area_offset =
Kuninori Morimotoded8a5f2015-05-11 07:34:53 +0000290 (((unsigned long long)ext_csd[139]) << 24) +
291 (((unsigned long long)ext_csd[138]) << 16) +
292 (((unsigned long long)ext_csd[137]) << 8) +
293 (((unsigned long long)ext_csd[136]));
Grégory Soutadé994324b2014-09-15 17:47:11 +0200294 if (mmc_card_blockaddr(card))
295 card->ext_csd.enhanced_area_offset <<= 9;
296 /*
297 * calculate the enhanced data area size, in kilobytes
298 */
299 card->ext_csd.enhanced_area_size =
300 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
301 ext_csd[140];
302 card->ext_csd.enhanced_area_size *=
303 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
304 card->ext_csd.enhanced_area_size <<= 9;
305 } else {
306 pr_warn("%s: defines enhanced area without partition setting complete\n",
307 mmc_hostname(card->host));
308 }
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200309 }
310}
311
Ulf Hansson4facdde2017-01-13 14:14:14 +0100312static void mmc_part_add(struct mmc_card *card, unsigned int size,
313 unsigned int part_cfg, char *name, int idx, bool ro,
314 int area_type)
315{
316 card->part[card->nr_parts].size = size;
317 card->part[card->nr_parts].part_cfg = part_cfg;
318 sprintf(card->part[card->nr_parts].name, name, idx);
319 card->part[card->nr_parts].force_ro = ro;
320 card->part[card->nr_parts].area_type = area_type;
321 card->nr_parts++;
322}
323
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200324static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
325{
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200326 int idx;
Grégory Soutadé994324b2014-09-15 17:47:11 +0200327 u8 hc_erase_grp_sz, hc_wp_grp_sz;
328 unsigned int part_size;
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200329
330 /*
331 * General purpose partition feature support --
332 * If ext_csd has the size of general purpose partitions,
333 * set size, part_cfg, partition name in mmc_part.
334 */
335 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
336 EXT_CSD_PART_SUPPORT_PART_EN) {
Grégory Soutadé994324b2014-09-15 17:47:11 +0200337 hc_erase_grp_sz =
338 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
339 hc_wp_grp_sz =
340 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200341
342 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
343 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
344 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
345 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
346 continue;
Grégory Soutadé994324b2014-09-15 17:47:11 +0200347 if (card->ext_csd.partition_setting_completed == 0) {
348 pr_warn("%s: has partition size defined without partition complete\n",
349 mmc_hostname(card->host));
350 break;
351 }
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200352 part_size =
353 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
354 << 16) +
355 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
356 << 8) +
357 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
358 part_size *= (size_t)(hc_erase_grp_sz *
359 hc_wp_grp_sz);
360 mmc_part_add(card, part_size << 19,
361 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
362 "gp%d", idx, false,
363 MMC_BLK_DATA_AREA_GP);
364 }
365 }
366}
367
Adrian Hunter1c447112016-05-05 08:12:28 +0300368/* Minimum partition switch timeout in milliseconds */
369#define MMC_MIN_PART_SWITCH_TIME 300
370
Philip Rakity08ee80c2011-05-24 18:14:58 -0700371/*
372 * Decode extended CSD.
373 */
Ulf Hansson076ec382014-10-20 13:37:24 +0200374static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700375{
Namjae Jeone0c368d2011-10-06 23:41:38 +0900376 int err = 0, idx;
377 unsigned int part_size;
Hans de Goede81f8a7b2015-04-01 17:26:23 +0200378 struct device_node *np;
379 bool broken_hpi = false;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700380
Kyungmin Park6da24b72010-08-10 18:01:36 -0700381 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700382 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
Kyungmin Park6da24b72010-08-10 18:01:36 -0700383 if (card->csd.structure == 3) {
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700384 if (card->ext_csd.raw_ext_csd_structure > 2) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530385 pr_err("%s: unrecognised EXT_CSD structure "
Kyungmin Park6da24b72010-08-10 18:01:36 -0700386 "version %d\n", mmc_hostname(card->host),
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700387 card->ext_csd.raw_ext_csd_structure);
Kyungmin Park6da24b72010-08-10 18:01:36 -0700388 err = -EINVAL;
389 goto out;
390 }
391 }
392
Hans de Goede81f8a7b2015-04-01 17:26:23 +0200393 np = mmc_of_find_child_device(card->host, 0);
394 if (np && of_device_is_compatible(np, "mmc-card"))
395 broken_hpi = of_property_read_bool(np, "broken-hpi");
396 of_node_put(np);
397
Romain Izard03a59432014-06-27 10:51:07 +0200398 /*
399 * The EXT_CSD format is meant to be forward compatible. As long
400 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
401 * are authorized, see JEDEC JESD84-B50 section B.8.
402 */
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700403 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Pierre Ossmand7604d72007-07-23 00:34:07 +0200404
Pratibhasagar V53202262016-06-09 18:09:31 -0400405 /* fixup device after ext_csd revision field is updated */
406 mmc_fixup_device(card, mmc_ext_csd_fixups);
407
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700408 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
409 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
410 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
411 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700412 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200413 card->ext_csd.sectors =
414 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
415 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
416 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
417 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700418
419 /* Cards with density > 2GiB are sector addressed */
420 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200421 mmc_card_set_blockaddr(card);
422 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900423
Shawn Lin81ac2af2016-05-26 09:56:22 +0800424 card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700425 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900426 mmc_select_card_type(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100427
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700428 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
429 card->ext_csd.raw_erase_timeout_mult =
430 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
431 card->ext_csd.raw_hc_erase_grp_size =
432 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700433 if (card->ext_csd.rev >= 3) {
434 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500435 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
436
437 /* EXT_CSD value is in units of 10ms, but we store in ms */
438 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
Adrian Hunter1c447112016-05-05 08:12:28 +0300439 /* Some eMMC set the value too low so set a minimum */
440 if (card->ext_csd.part_time &&
441 card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
442 card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700443
444 /* Sleep / awake timeout in 100ns units */
445 if (sa_shift > 0 && sa_shift <= 0x17)
446 card->ext_csd.sa_timeout =
447 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700448 card->ext_csd.erase_group_def =
449 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
450 card->ext_csd.hc_erase_timeout = 300 *
451 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
452 card->ext_csd.hc_erase_size =
453 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500454
455 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500456
457 /*
458 * There are two boot regions of equal size, defined in
459 * multiples of 128K.
460 */
Namjae Jeone0c368d2011-10-06 23:41:38 +0900461 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
462 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
463 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
464 mmc_part_add(card, part_size,
465 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100466 "boot%d", idx, true,
467 MMC_BLK_DATA_AREA_BOOT);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900468 }
469 }
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700470 }
471
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700472 card->ext_csd.raw_hc_erase_gap_size =
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100473 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700474 card->ext_csd.raw_sec_trim_mult =
475 ext_csd[EXT_CSD_SEC_TRIM_MULT];
476 card->ext_csd.raw_sec_erase_mult =
477 ext_csd[EXT_CSD_SEC_ERASE_MULT];
478 card->ext_csd.raw_sec_feature_support =
479 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
480 card->ext_csd.raw_trim_mult =
481 ext_csd[EXT_CSD_TRIM_MULT];
Philip Rakity836dc2f2013-04-04 20:18:11 +0100482 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
Adrian Hunterb097e072015-02-06 14:12:57 +0200483 card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700484 if (card->ext_csd.rev >= 4) {
Grégory Soutadé69803d42014-09-15 17:47:09 +0200485 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
486 EXT_CSD_PART_SETTING_COMPLETED)
487 card->ext_csd.partition_setting_completed = 1;
488 else
489 card->ext_csd.partition_setting_completed = 0;
490
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200491 mmc_manage_enhanced_area(card, ext_csd);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800492
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200493 mmc_manage_gp_partitions(card, ext_csd);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900494
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700495 card->ext_csd.sec_trim_mult =
496 ext_csd[EXT_CSD_SEC_TRIM_MULT];
497 card->ext_csd.sec_erase_mult =
498 ext_csd[EXT_CSD_SEC_ERASE_MULT];
499 card->ext_csd.sec_feature_support =
500 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
501 card->ext_csd.trim_timeout = 300 *
502 ext_csd[EXT_CSD_TRIM_MULT];
Johan Rudholmadd710e2011-12-02 08:51:06 +0100503
504 /*
505 * Note that the call to mmc_part_add above defaults to read
506 * only. If this default assumption is changed, the call must
507 * take into account the value of boot_locked below.
508 */
509 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
510 card->ext_csd.boot_ro_lockable = true;
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200511
512 /* Save power class values */
513 card->ext_csd.raw_pwr_cl_52_195 =
514 ext_csd[EXT_CSD_PWR_CL_52_195];
515 card->ext_csd.raw_pwr_cl_26_195 =
516 ext_csd[EXT_CSD_PWR_CL_26_195];
517 card->ext_csd.raw_pwr_cl_52_360 =
518 ext_csd[EXT_CSD_PWR_CL_52_360];
519 card->ext_csd.raw_pwr_cl_26_360 =
520 ext_csd[EXT_CSD_PWR_CL_26_360];
521 card->ext_csd.raw_pwr_cl_200_195 =
522 ext_csd[EXT_CSD_PWR_CL_200_195];
523 card->ext_csd.raw_pwr_cl_200_360 =
524 ext_csd[EXT_CSD_PWR_CL_200_360];
525 card->ext_csd.raw_pwr_cl_ddr_52_195 =
526 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
527 card->ext_csd.raw_pwr_cl_ddr_52_360 =
528 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900529 card->ext_csd.raw_pwr_cl_ddr_200_360 =
530 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700531 }
532
Adrian Hunterb2499512011-08-29 16:42:11 +0300533 if (card->ext_csd.rev >= 5) {
Romain Izard7c4f10a2013-06-14 14:25:44 +0200534 /* Adjust production date as per JEDEC JESD84-B451 */
535 if (card->cid.year < 2010)
536 card->cid.year += 16;
537
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000538 /* check whether the eMMC card supports BKOPS */
Pratibhasagar V53202262016-06-09 18:09:31 -0400539 if (!mmc_card_broken_hpi(card) &&
540 ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000541 card->ext_csd.bkops = 1;
Alexey Skidanov0501be62015-01-29 10:49:43 +0200542 card->ext_csd.man_bkops_en =
543 (ext_csd[EXT_CSD_BKOPS_EN] &
544 EXT_CSD_MANUAL_BKOPS_MASK);
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000545 card->ext_csd.raw_bkops_status =
546 ext_csd[EXT_CSD_BKOPS_STATUS];
Uri Yanai6962c2f2017-02-07 18:00:00 +0200547 if (card->ext_csd.man_bkops_en)
548 pr_debug("%s: MAN_BKOPS_EN bit is set\n",
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000549 mmc_hostname(card->host));
Uri Yanaiefff8e72017-02-07 18:00:01 +0200550 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));
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000556 }
557
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400558 /* check whether the eMMC card supports HPI */
Pratibhasagar V53202262016-06-09 18:09:31 -0400559 if (!mmc_card_broken_hpi(card) &&
560 !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400561 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
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500574 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
Adrian Hunterb2499512011-08-29 16:42:11 +0300575 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
Loic Pallardy090d25f2012-11-17 18:08:24 -0500576
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];
Balaji T Kd0123cc2013-01-25 17:00:30 +0530581 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
Loic Pallardy090d25f2012-11-17 18:08:24 -0500582 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 }
Adrian Hunterb2499512011-08-29 16:42:11 +0300587 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500588
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400589 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700590 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
591 card->erased_byte = 0xFF;
592 else
593 card->erased_byte = 0x0;
594
Seungwon Jeon336c7162011-10-25 09:43:12 +0900595 /* eMMC v4.5 or later */
Ulf Hanssonfe1b5702016-11-04 18:32:33 +0100596 card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS;
Girish K Sbec87262011-10-13 12:04:16 +0530597 if (card->ext_csd.rev >= 6) {
Seungwon Jeon336c7162011-10-25 09:43:12 +0900598 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
599
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900600 card->ext_csd.generic_cmd6_time = 10 *
601 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
Girish K Sbec87262011-10-13 12:04:16 +0530602 card->ext_csd.power_off_longtime = 10 *
603 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900604
Seungwon Jeon336c7162011-10-25 09:43:12 +0900605 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;
Saugata Das42659002011-12-21 13:09:17 +0530610
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 }
Seungwon Jeonabd9ac12013-02-06 17:01:43 +0900624
625 card->ext_csd.max_packed_writes =
626 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
627 card->ext_csd.max_packed_reads =
628 ext_csd[EXT_CSD_MAX_PACKED_READS];
Saugata Dasa5075eb2012-05-17 16:32:21 +0530629 } else {
630 card->ext_csd.data_sector_size = 512;
Seungwon Jeon336c7162011-10-25 09:43:12 +0900631 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900632
Gwendal Grignou0f762422014-10-16 11:27:16 -0700633 /* eMMC v5 or later */
634 if (card->ext_csd.rev >= 7) {
635 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
636 MMC_FIRMWARE_LEN);
637 card->ext_csd.ffu_capable =
638 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
639 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900640
641 card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO];
642 card->ext_csd.device_life_time_est_typ_a =
643 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
644 card->ext_csd.device_life_time_est_typ_b =
645 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
Gwendal Grignou0f762422014-10-16 11:27:16 -0700646 }
Adrian Hunter925ff3a2016-11-29 12:09:16 +0200647
648 /* eMMC v5.1 or later */
649 if (card->ext_csd.rev >= 8) {
650 card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] &
651 EXT_CSD_CMDQ_SUPPORTED;
652 card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] &
653 EXT_CSD_CMDQ_DEPTH_MASK) + 1;
654 /* Exclude inefficiently small queue depths */
655 if (card->ext_csd.cmdq_depth <= 2) {
656 card->ext_csd.cmdq_support = false;
657 card->ext_csd.cmdq_depth = 0;
658 }
659 if (card->ext_csd.cmdq_support) {
660 pr_debug("%s: Command Queue supported depth %u\n",
661 mmc_hostname(card->host),
662 card->ext_csd.cmdq_depth);
663 }
664 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100665out:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700666 return err;
667}
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100668
Ulf Hansson076ec382014-10-20 13:37:24 +0200669static int mmc_read_ext_csd(struct mmc_card *card)
670{
Ulf Hanssonc1977872014-10-20 14:08:16 +0200671 u8 *ext_csd;
Ulf Hansson076ec382014-10-20 13:37:24 +0200672 int err;
673
Ulf Hanssonc1977872014-10-20 14:08:16 +0200674 if (!mmc_can_ext_csd(card))
675 return 0;
676
Ulf Hansson076ec382014-10-20 13:37:24 +0200677 err = mmc_get_ext_csd(card, &ext_csd);
Ulf Hanssonc1977872014-10-20 14:08:16 +0200678 if (err) {
679 /* If the host or the card can't do the switch,
680 * fail more gracefully. */
681 if ((err != -EINVAL)
682 && (err != -ENOSYS)
683 && (err != -EFAULT))
684 return err;
685
686 /*
687 * High capacity cards should have this "magic" size
688 * stored in their CSD.
689 */
690 if (card->csd.capacity == (4096 * 512)) {
691 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
692 mmc_hostname(card->host));
693 } else {
694 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
695 mmc_hostname(card->host));
696 err = 0;
697 }
698
Ulf Hansson076ec382014-10-20 13:37:24 +0200699 return err;
Ulf Hanssonc1977872014-10-20 14:08:16 +0200700 }
Ulf Hansson076ec382014-10-20 13:37:24 +0200701
702 err = mmc_decode_ext_csd(card, ext_csd);
703 kfree(ext_csd);
704 return err;
705}
706
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700707static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700708{
709 u8 *bw_ext_csd;
710 int err;
711
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700712 if (bus_width == MMC_BUS_WIDTH_1)
713 return 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700714
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700715 err = mmc_get_ext_csd(card, &bw_ext_csd);
Ulf Hanssonc1977872014-10-20 14:08:16 +0200716 if (err)
717 return err;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700718
Philip Rakity08ee80c2011-05-24 18:14:58 -0700719 /* only compare read only fields */
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100720 err = !((card->ext_csd.raw_partition_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700721 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700722 (card->ext_csd.raw_erased_mem_count ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700723 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700724 (card->ext_csd.rev ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700725 bw_ext_csd[EXT_CSD_REV]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700726 (card->ext_csd.raw_ext_csd_structure ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700727 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700728 (card->ext_csd.raw_card_type ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700729 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700730 (card->ext_csd.raw_s_a_timeout ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700731 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700732 (card->ext_csd.raw_hc_erase_gap_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700733 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700734 (card->ext_csd.raw_erase_timeout_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700735 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700736 (card->ext_csd.raw_hc_erase_grp_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700737 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700738 (card->ext_csd.raw_sec_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700739 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700740 (card->ext_csd.raw_sec_erase_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700741 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700742 (card->ext_csd.raw_sec_feature_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700743 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700744 (card->ext_csd.raw_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700745 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700746 (card->ext_csd.raw_sectors[0] ==
747 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
748 (card->ext_csd.raw_sectors[1] ==
749 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
750 (card->ext_csd.raw_sectors[2] ==
751 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
752 (card->ext_csd.raw_sectors[3] ==
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200753 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
754 (card->ext_csd.raw_pwr_cl_52_195 ==
755 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
756 (card->ext_csd.raw_pwr_cl_26_195 ==
757 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
758 (card->ext_csd.raw_pwr_cl_52_360 ==
759 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
760 (card->ext_csd.raw_pwr_cl_26_360 ==
761 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
762 (card->ext_csd.raw_pwr_cl_200_195 ==
763 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
764 (card->ext_csd.raw_pwr_cl_200_360 ==
765 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
766 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
767 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
768 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900769 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
770 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
771 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
772
Philip Rakity08ee80c2011-05-24 18:14:58 -0700773 if (err)
774 err = -EINVAL;
775
Ulf Hansson00b41b52014-10-16 16:18:51 +0200776 kfree(bw_ext_csd);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100777 return err;
778}
779
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100780MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
781 card->raw_cid[2], card->raw_cid[3]);
782MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
783 card->raw_csd[2], card->raw_csd[3]);
784MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700785MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
786MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Gwendal Grignou0f762422014-10-16 11:27:16 -0700787MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100788MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
789MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
790MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
791MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800792MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900793MMC_DEV_ATTR(pre_eol_info, "%02x\n", card->ext_csd.pre_eol_info);
794MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n",
795 card->ext_csd.device_life_time_est_typ_a,
796 card->ext_csd.device_life_time_est_typ_b);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100797MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800798MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
799 card->ext_csd.enhanced_area_offset);
800MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
Loic Pallardy188cc042012-08-06 17:12:29 +0200801MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
802MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
Bojan Prtvar5fb06af2016-07-04 13:56:55 +0200803MMC_DEV_ATTR(ocr, "%08x\n", card->ocr);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100804
Gwendal Grignou0f762422014-10-16 11:27:16 -0700805static ssize_t mmc_fwrev_show(struct device *dev,
806 struct device_attribute *attr,
807 char *buf)
808{
809 struct mmc_card *card = mmc_dev_to_card(dev);
810
811 if (card->ext_csd.rev < 7) {
812 return sprintf(buf, "0x%x\n", card->cid.fwrev);
813 } else {
814 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
815 card->ext_csd.fwrev);
816 }
817}
818
819static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
820
Bojan Prtvar6825a602016-07-19 11:16:38 +0200821static ssize_t mmc_dsr_show(struct device *dev,
822 struct device_attribute *attr,
823 char *buf)
824{
825 struct mmc_card *card = mmc_dev_to_card(dev);
826 struct mmc_host *host = card->host;
827
828 if (card->csd.dsr_imp && host->dsr_req)
829 return sprintf(buf, "0x%x\n", host->dsr);
830 else
831 /* return default DSR value */
832 return sprintf(buf, "0x%x\n", 0x404);
833}
834
835static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
836
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100837static struct attribute *mmc_std_attrs[] = {
838 &dev_attr_cid.attr,
839 &dev_attr_csd.attr,
840 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700841 &dev_attr_erase_size.attr,
842 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100843 &dev_attr_fwrev.attr,
Gwendal Grignou0f762422014-10-16 11:27:16 -0700844 &dev_attr_ffu_capable.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100845 &dev_attr_hwrev.attr,
846 &dev_attr_manfid.attr,
847 &dev_attr_name.attr,
848 &dev_attr_oemid.attr,
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800849 &dev_attr_prv.attr,
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900850 &dev_attr_pre_eol_info.attr,
851 &dev_attr_life_time.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100852 &dev_attr_serial.attr,
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800853 &dev_attr_enhanced_area_offset.attr,
854 &dev_attr_enhanced_area_size.attr,
Loic Pallardy188cc042012-08-06 17:12:29 +0200855 &dev_attr_raw_rpmb_size_mult.attr,
856 &dev_attr_rel_sectors.attr,
Bojan Prtvar5fb06af2016-07-04 13:56:55 +0200857 &dev_attr_ocr.attr,
Bojan Prtvar6825a602016-07-19 11:16:38 +0200858 &dev_attr_dsr.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100859 NULL,
860};
Axel Lind1e58212014-03-08 15:05:27 +0800861ATTRIBUTE_GROUPS(mmc_std);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100862
863static struct device_type mmc_type = {
Axel Lind1e58212014-03-08 15:05:27 +0800864 .groups = mmc_std_groups,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100865};
866
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100867/*
Girish K Sb87d8db2011-09-23 20:41:47 +0530868 * Select the PowerClass for the current bus width
869 * If power class is defined for 4/8 bit bus in the
870 * extended CSD register, select it by executing the
871 * mmc_switch command.
872 */
Seungwon Jeon23850492014-04-23 17:08:05 +0900873static int __mmc_select_powerclass(struct mmc_card *card,
874 unsigned int bus_width)
Girish K Sb87d8db2011-09-23 20:41:47 +0530875{
Seungwon Jeon23850492014-04-23 17:08:05 +0900876 struct mmc_host *host = card->host;
877 struct mmc_ext_csd *ext_csd = &card->ext_csd;
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200878 unsigned int pwrclass_val = 0;
Seungwon Jeon23850492014-04-23 17:08:05 +0900879 int err = 0;
Girish K Sb87d8db2011-09-23 20:41:47 +0530880
Girish K Sb87d8db2011-09-23 20:41:47 +0530881 switch (1 << host->ios.vdd) {
882 case MMC_VDD_165_195:
Seungwon Jeon23850492014-04-23 17:08:05 +0900883 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
884 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
885 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200886 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
Seungwon Jeon23850492014-04-23 17:08:05 +0900887 ext_csd->raw_pwr_cl_52_195 :
888 ext_csd->raw_pwr_cl_ddr_52_195;
889 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
890 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
Girish K Sb87d8db2011-09-23 20:41:47 +0530891 break;
Subhash Jadavani93fc5a42012-04-03 12:25:58 +0530892 case MMC_VDD_27_28:
893 case MMC_VDD_28_29:
894 case MMC_VDD_29_30:
895 case MMC_VDD_30_31:
896 case MMC_VDD_31_32:
Girish K Sb87d8db2011-09-23 20:41:47 +0530897 case MMC_VDD_32_33:
898 case MMC_VDD_33_34:
899 case MMC_VDD_34_35:
900 case MMC_VDD_35_36:
Seungwon Jeon23850492014-04-23 17:08:05 +0900901 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
902 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
903 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200904 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
Seungwon Jeon23850492014-04-23 17:08:05 +0900905 ext_csd->raw_pwr_cl_52_360 :
906 ext_csd->raw_pwr_cl_ddr_52_360;
907 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900908 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
909 ext_csd->raw_pwr_cl_ddr_200_360 :
910 ext_csd->raw_pwr_cl_200_360;
Girish K Sb87d8db2011-09-23 20:41:47 +0530911 break;
912 default:
Joe Perches66061102014-09-12 14:56:56 -0700913 pr_warn("%s: Voltage range not supported for power class\n",
914 mmc_hostname(host));
Girish K Sb87d8db2011-09-23 20:41:47 +0530915 return -EINVAL;
916 }
917
Girish K Sb87d8db2011-09-23 20:41:47 +0530918 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
919 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
920 EXT_CSD_PWR_CL_8BIT_SHIFT;
921 else
922 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
923 EXT_CSD_PWR_CL_4BIT_SHIFT;
924
925 /* If the power class is different from the default value */
926 if (pwrclass_val > 0) {
927 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
928 EXT_CSD_POWER_CLASS,
929 pwrclass_val,
Seungwon Jeon71fe3eb2011-10-18 13:20:57 +0900930 card->ext_csd.generic_cmd6_time);
Girish K Sb87d8db2011-09-23 20:41:47 +0530931 }
932
933 return err;
934}
935
Seungwon Jeon23850492014-04-23 17:08:05 +0900936static int mmc_select_powerclass(struct mmc_card *card)
937{
938 struct mmc_host *host = card->host;
939 u32 bus_width, ext_csd_bits;
940 int err, ddr;
941
942 /* Power class selection is supported for versions >= 4.0 */
Ulf Hansson148bcab2014-10-20 11:33:53 +0200943 if (!mmc_can_ext_csd(card))
Seungwon Jeon23850492014-04-23 17:08:05 +0900944 return 0;
945
946 bus_width = host->ios.bus_width;
947 /* Power class values are defined only for 4/8 bit bus */
948 if (bus_width == MMC_BUS_WIDTH_1)
949 return 0;
950
951 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
952 if (ddr)
953 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
954 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
955 else
956 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
957 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
958
959 err = __mmc_select_powerclass(card, ext_csd_bits);
960 if (err)
961 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
962 mmc_hostname(host), 1 << bus_width, ddr);
963
964 return err;
965}
966
Girish K Sb87d8db2011-09-23 20:41:47 +0530967/*
Seungwon Jeon577fb132014-04-23 17:08:44 +0900968 * Set the bus speed for the selected speed mode.
Girish K Sa4924c72012-01-11 14:04:52 -0500969 */
Seungwon Jeon577fb132014-04-23 17:08:44 +0900970static void mmc_set_bus_speed(struct mmc_card *card)
Girish K Sa4924c72012-01-11 14:04:52 -0500971{
Seungwon Jeon577fb132014-04-23 17:08:44 +0900972 unsigned int max_dtr = (unsigned int)-1;
973
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900974 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
975 max_dtr > card->ext_csd.hs200_max_dtr)
Seungwon Jeon577fb132014-04-23 17:08:44 +0900976 max_dtr = card->ext_csd.hs200_max_dtr;
977 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
978 max_dtr = card->ext_csd.hs_max_dtr;
979 else if (max_dtr > card->csd.max_dtr)
980 max_dtr = card->csd.max_dtr;
981
982 mmc_set_clock(card->host, max_dtr);
983}
984
985/*
986 * Select the bus width amoung 4-bit and 8-bit(SDR).
987 * If the bus width is changed successfully, return the selected width value.
988 * Zero is returned instead of error value if the wide width is not supported.
989 */
990static int mmc_select_bus_width(struct mmc_card *card)
991{
Girish K Sa4924c72012-01-11 14:04:52 -0500992 static unsigned ext_csd_bits[] = {
Girish K Sa4924c72012-01-11 14:04:52 -0500993 EXT_CSD_BUS_WIDTH_8,
Seungwon Jeon577fb132014-04-23 17:08:44 +0900994 EXT_CSD_BUS_WIDTH_4,
Girish K Sa4924c72012-01-11 14:04:52 -0500995 };
996 static unsigned bus_widths[] = {
Girish K Sa4924c72012-01-11 14:04:52 -0500997 MMC_BUS_WIDTH_8,
Seungwon Jeon577fb132014-04-23 17:08:44 +0900998 MMC_BUS_WIDTH_4,
Girish K Sa4924c72012-01-11 14:04:52 -0500999 };
Seungwon Jeon577fb132014-04-23 17:08:44 +09001000 struct mmc_host *host = card->host;
1001 unsigned idx, bus_width = 0;
1002 int err = 0;
Girish K Sa4924c72012-01-11 14:04:52 -05001003
Alexandre Belloni1c2d26e2014-12-17 19:32:06 +01001004 if (!mmc_can_ext_csd(card) ||
Seungwon Jeon577fb132014-04-23 17:08:44 +09001005 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
1006 return 0;
Girish K Sa4924c72012-01-11 14:04:52 -05001007
Seungwon Jeon577fb132014-04-23 17:08:44 +09001008 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
Girish K Sa4924c72012-01-11 14:04:52 -05001009
1010 /*
1011 * Unlike SD, MMC cards dont have a configuration register to notify
1012 * supported bus width. So bus test command should be run to identify
1013 * the supported bus width or compare the ext csd values of current
1014 * bus width and ext csd values of 1 bit mode read earlier.
1015 */
Seungwon Jeon577fb132014-04-23 17:08:44 +09001016 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
Girish K Sa4924c72012-01-11 14:04:52 -05001017 /*
1018 * Host is capable of 8bit transfer, then switch
1019 * the device to work in 8bit transfer mode. If the
1020 * mmc switch command returns error then switch to
1021 * 4bit transfer mode. On success set the corresponding
1022 * bus width on the host.
1023 */
1024 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1025 EXT_CSD_BUS_WIDTH,
1026 ext_csd_bits[idx],
1027 card->ext_csd.generic_cmd6_time);
1028 if (err)
1029 continue;
1030
Seungwon Jeon577fb132014-04-23 17:08:44 +09001031 bus_width = bus_widths[idx];
1032 mmc_set_bus_width(host, bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -05001033
Seungwon Jeon577fb132014-04-23 17:08:44 +09001034 /*
1035 * If controller can't handle bus width test,
1036 * compare ext_csd previously read in 1 bit mode
1037 * against ext_csd at new bus width
1038 */
Girish K Sa4924c72012-01-11 14:04:52 -05001039 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
Seungwon Jeon577fb132014-04-23 17:08:44 +09001040 err = mmc_compare_ext_csds(card, bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -05001041 else
Seungwon Jeon577fb132014-04-23 17:08:44 +09001042 err = mmc_bus_test(card, bus_width);
1043
1044 if (!err) {
1045 err = bus_width;
Girish K Sa4924c72012-01-11 14:04:52 -05001046 break;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001047 } else {
1048 pr_warn("%s: switch to bus width %d failed\n",
Wolfram Sanged9feec2016-01-29 09:27:50 +01001049 mmc_hostname(host), 1 << bus_width);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001050 }
Girish K Sa4924c72012-01-11 14:04:52 -05001051 }
1052
Seungwon Jeon577fb132014-04-23 17:08:44 +09001053 return err;
1054}
1055
1056/*
1057 * Switch to the high-speed mode
1058 */
1059static int mmc_select_hs(struct mmc_card *card)
1060{
1061 int err;
1062
1063 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1064 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
Ulf Hansson53e60652016-11-09 19:40:29 +01001065 card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
1066 true, true, true);
Jungseung Lee67d35962016-08-24 19:34:09 +09001067 if (err)
1068 pr_warn("%s: switch to high-speed failed, err:%d\n",
1069 mmc_hostname(card->host), err);
1070
Seungwon Jeon577fb132014-04-23 17:08:44 +09001071 return err;
1072}
1073
1074/*
1075 * Activate wide bus and DDR if supported.
1076 */
1077static int mmc_select_hs_ddr(struct mmc_card *card)
1078{
1079 struct mmc_host *host = card->host;
1080 u32 bus_width, ext_csd_bits;
1081 int err = 0;
1082
1083 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1084 return 0;
1085
1086 bus_width = host->ios.bus_width;
1087 if (bus_width == MMC_BUS_WIDTH_1)
1088 return 0;
1089
1090 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1091 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1092
Ulf Hanssone173f892016-11-09 21:00:26 +01001093 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1094 EXT_CSD_BUS_WIDTH,
1095 ext_csd_bits,
1096 card->ext_csd.generic_cmd6_time,
1097 MMC_TIMING_MMC_DDR52,
1098 true, true, true);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001099 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001100 pr_err("%s: switch to bus width %d ddr failed\n",
Seungwon Jeon577fb132014-04-23 17:08:44 +09001101 mmc_hostname(host), 1 << bus_width);
1102 return err;
1103 }
1104
1105 /*
1106 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1107 * signaling.
1108 *
1109 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1110 *
1111 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1112 * in the JEDEC spec for DDR.
1113 *
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001114 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1115 * host controller can support this, like some of the SDHCI
1116 * controller which connect to an eMMC device. Some of these
1117 * host controller still needs to use 1.8v vccq for supporting
1118 * DDR mode.
1119 *
1120 * So the sequence will be:
1121 * if (host and device can both support 1.2v IO)
1122 * use 1.2v IO;
1123 * else if (host and device can both support 1.8v IO)
1124 * use 1.8v IO;
1125 * so if host and device can only support 3.3v IO, this is the
1126 * last choice.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001127 *
1128 * WARNING: eMMC rules are NOT the same as SD DDR
1129 */
Ulf Hansson20f921b2017-01-25 12:04:17 +01001130 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001131 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
Ulf Hansson20f921b2017-01-25 12:04:17 +01001132 if (!err)
1133 return 0;
1134 }
Seungwon Jeon577fb132014-04-23 17:08:44 +09001135
Ulf Hansson20f921b2017-01-25 12:04:17 +01001136 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V &&
1137 host->caps & MMC_CAP_1_8V_DDR)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001138 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001139
1140 /* make sure vccq is 3.3v after switching disaster */
1141 if (err)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001142 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001143
Seungwon Jeon577fb132014-04-23 17:08:44 +09001144 return err;
1145}
1146
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001147static int mmc_select_hs400(struct mmc_card *card)
1148{
1149 struct mmc_host *host = card->host;
Adrian Hunter51b12f72015-10-28 14:25:41 +02001150 unsigned int max_dtr;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001151 int err = 0;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001152 u8 val;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001153
1154 /*
1155 * HS400 mode requires 8-bit bus width
1156 */
1157 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1158 host->ios.bus_width == MMC_BUS_WIDTH_8))
1159 return 0;
1160
Adrian Hunter51b12f72015-10-28 14:25:41 +02001161 /* Switch card to HS mode */
Wenkai Duadb24d42015-11-26 14:00:44 +02001162 val = EXT_CSD_TIMING_HS;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001163 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001164 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001165 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001166 true, false, true);
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001167 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001168 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001169 mmc_hostname(host), err);
1170 return err;
1171 }
1172
Adrian Hunter51b12f72015-10-28 14:25:41 +02001173 /* Set host controller to HS timing */
1174 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1175
Ziyuan Xu649c6052016-05-26 13:50:32 +08001176 /* Reduce frequency to HS frequency */
1177 max_dtr = card->ext_csd.hs_max_dtr;
1178 mmc_set_clock(host, max_dtr);
1179
Chaotian Jing08573ea2016-05-19 16:47:41 +08001180 err = mmc_switch_status(card);
1181 if (err)
1182 goto out_err;
Adrian Hunterd2302932015-10-28 14:25:43 +02001183
1184 /* Switch card to DDR */
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001185 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1186 EXT_CSD_BUS_WIDTH,
1187 EXT_CSD_DDR_BUS_WIDTH_8,
1188 card->ext_csd.generic_cmd6_time);
1189 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001190 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001191 mmc_hostname(host), err);
1192 return err;
1193 }
1194
Adrian Hunterd2302932015-10-28 14:25:43 +02001195 /* Switch card to HS400 */
Adrian Huntercc4f4142015-02-06 14:12:58 +02001196 val = EXT_CSD_TIMING_HS400 |
1197 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001198 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001199 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001200 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001201 true, false, true);
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001202 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001203 pr_err("%s: switch to hs400 failed, err:%d\n",
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001204 mmc_hostname(host), err);
1205 return err;
1206 }
1207
Adrian Hunterd2302932015-10-28 14:25:43 +02001208 /* Set host controller to HS400 timing and frequency */
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001209 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1210 mmc_set_bus_speed(card);
1211
Chaotian Jing08573ea2016-05-19 16:47:41 +08001212 err = mmc_switch_status(card);
1213 if (err)
1214 goto out_err;
Adrian Hunterd2302932015-10-28 14:25:43 +02001215
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001216 return 0;
Adrian Hunterd2302932015-10-28 14:25:43 +02001217
1218out_err:
1219 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1220 __func__, err);
1221 return err;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001222}
1223
Adrian Hunter6376f692015-05-07 13:10:20 +03001224int mmc_hs200_to_hs400(struct mmc_card *card)
1225{
1226 return mmc_select_hs400(card);
1227}
1228
Adrian Hunter6376f692015-05-07 13:10:20 +03001229int mmc_hs400_to_hs200(struct mmc_card *card)
1230{
1231 struct mmc_host *host = card->host;
Adrian Hunter6376f692015-05-07 13:10:20 +03001232 unsigned int max_dtr;
1233 int err;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001234 u8 val;
Adrian Hunter6376f692015-05-07 13:10:20 +03001235
Adrian Hunter6376f692015-05-07 13:10:20 +03001236 /* Reduce frequency to HS */
1237 max_dtr = card->ext_csd.hs_max_dtr;
1238 mmc_set_clock(host, max_dtr);
1239
1240 /* Switch HS400 to HS DDR */
Wenkai Duadb24d42015-11-26 14:00:44 +02001241 val = EXT_CSD_TIMING_HS;
Adrian Hunter6376f692015-05-07 13:10:20 +03001242 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001243 val, card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001244 true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001245 if (err)
1246 goto out_err;
1247
1248 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1249
Chaotian Jing08573ea2016-05-19 16:47:41 +08001250 err = mmc_switch_status(card);
1251 if (err)
1252 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001253
1254 /* Switch HS DDR to HS */
1255 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1256 EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001257 0, true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001258 if (err)
1259 goto out_err;
1260
1261 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1262
Chaotian Jing08573ea2016-05-19 16:47:41 +08001263 err = mmc_switch_status(card);
1264 if (err)
1265 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001266
1267 /* Switch HS to HS200 */
Adrian Huntercc4f4142015-02-06 14:12:58 +02001268 val = EXT_CSD_TIMING_HS200 |
1269 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Adrian Hunter6376f692015-05-07 13:10:20 +03001270 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001271 val, card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001272 true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001273 if (err)
1274 goto out_err;
1275
1276 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1277
Adrian Hunteref3d2322016-12-02 13:16:35 +02001278 /*
1279 * For HS200, CRC errors are not a reliable way to know the switch
1280 * failed. If there really is a problem, we would expect tuning will
1281 * fail and the result ends up the same.
1282 */
1283 err = __mmc_switch_status(card, false);
Chaotian Jing08573ea2016-05-19 16:47:41 +08001284 if (err)
1285 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001286
1287 mmc_set_bus_speed(card);
1288
1289 return 0;
1290
1291out_err:
1292 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1293 __func__, err);
1294 return err;
1295}
1296
Shawn Lin81ac2af2016-05-26 09:56:22 +08001297static int mmc_select_hs400es(struct mmc_card *card)
1298{
1299 struct mmc_host *host = card->host;
1300 int err = 0;
1301 u8 val;
1302
1303 if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1304 err = -ENOTSUPP;
1305 goto out_err;
1306 }
1307
Shawn Lin1720d352016-09-30 14:18:58 +08001308 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001309 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
Shawn Lin1720d352016-09-30 14:18:58 +08001310
1311 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001312 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
Shawn Lin1720d352016-09-30 14:18:58 +08001313
1314 /* If fails try again during next card power cycle */
1315 if (err)
1316 goto out_err;
1317
Shawn Lin81ac2af2016-05-26 09:56:22 +08001318 err = mmc_select_bus_width(card);
1319 if (err < 0)
1320 goto out_err;
1321
1322 /* Switch card to HS mode */
Ulf Hansson53e60652016-11-09 19:40:29 +01001323 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1324 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1325 card->ext_csd.generic_cmd6_time, 0,
1326 true, false, true);
1327 if (err) {
1328 pr_err("%s: switch to hs for hs400es failed, err:%d\n",
1329 mmc_hostname(host), err);
1330 goto out_err;
1331 }
1332
1333 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1334 err = mmc_switch_status(card);
Jungseung Lee67d35962016-08-24 19:34:09 +09001335 if (err)
Shawn Lin81ac2af2016-05-26 09:56:22 +08001336 goto out_err;
Shawn Lin81ac2af2016-05-26 09:56:22 +08001337
Shawn Lin4f255802016-09-30 14:18:59 +08001338 mmc_set_clock(host, card->ext_csd.hs_max_dtr);
1339
Shawn Lin81ac2af2016-05-26 09:56:22 +08001340 /* Switch card to DDR with strobe bit */
1341 val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1342 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1343 EXT_CSD_BUS_WIDTH,
1344 val,
1345 card->ext_csd.generic_cmd6_time);
1346 if (err) {
1347 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1348 mmc_hostname(host), err);
1349 goto out_err;
1350 }
1351
1352 /* Switch card to HS400 */
1353 val = EXT_CSD_TIMING_HS400 |
1354 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1355 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1356 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001357 card->ext_csd.generic_cmd6_time, 0,
Shawn Lin81ac2af2016-05-26 09:56:22 +08001358 true, false, true);
1359 if (err) {
1360 pr_err("%s: switch to hs400es failed, err:%d\n",
1361 mmc_hostname(host), err);
1362 goto out_err;
1363 }
1364
1365 /* Set host controller to HS400 timing and frequency */
1366 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1367
1368 /* Controller enable enhanced strobe function */
1369 host->ios.enhanced_strobe = true;
1370 if (host->ops->hs400_enhanced_strobe)
1371 host->ops->hs400_enhanced_strobe(host, &host->ios);
1372
1373 err = mmc_switch_status(card);
1374 if (err)
1375 goto out_err;
1376
1377 return 0;
1378
1379out_err:
1380 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1381 __func__, err);
1382 return err;
1383}
1384
Adrian Huntercc4f4142015-02-06 14:12:58 +02001385static void mmc_select_driver_type(struct mmc_card *card)
1386{
1387 int card_drv_type, drive_strength, drv_type;
1388
1389 card_drv_type = card->ext_csd.raw_driver_strength |
1390 mmc_driver_type_mask(0);
1391
1392 drive_strength = mmc_select_drive_strength(card,
1393 card->ext_csd.hs200_max_dtr,
1394 card_drv_type, &drv_type);
1395
1396 card->drive_strength = drive_strength;
1397
1398 if (drv_type)
1399 mmc_set_driver_type(card->host, drv_type);
1400}
1401
Seungwon Jeon577fb132014-04-23 17:08:44 +09001402/*
1403 * For device supporting HS200 mode, the following sequence
1404 * should be done before executing the tuning process.
1405 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1406 * 2. switch to HS200 mode
1407 * 3. set the clock to > 52Mhz and <=200MHz
1408 */
1409static int mmc_select_hs200(struct mmc_card *card)
1410{
1411 struct mmc_host *host = card->host;
Dong Aishenge51534c2016-04-21 00:51:30 +08001412 unsigned int old_timing, old_signal_voltage;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001413 int err = -EINVAL;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001414 u8 val;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001415
Dong Aishenge51534c2016-04-21 00:51:30 +08001416 old_signal_voltage = host->ios.signal_voltage;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001417 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001418 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001419
1420 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001421 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001422
1423 /* If fails try again during next card power cycle */
1424 if (err)
Dong Aishenge51534c2016-04-21 00:51:30 +08001425 return err;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001426
Adrian Huntercc4f4142015-02-06 14:12:58 +02001427 mmc_select_driver_type(card);
1428
Seungwon Jeon577fb132014-04-23 17:08:44 +09001429 /*
1430 * Set the bus width(4 or 8) with host's support and
1431 * switch to HS200 mode if bus width is set successfully.
1432 */
1433 err = mmc_select_bus_width(card);
Peter Griffin8b7be8f2016-06-08 11:27:52 +01001434 if (err > 0) {
Adrian Huntercc4f4142015-02-06 14:12:58 +02001435 val = EXT_CSD_TIMING_HS200 |
1436 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Ulf Hansson4509f84772014-01-08 16:09:33 +01001437 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001438 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001439 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001440 true, false, true);
Adrian Hunter1815e612015-10-28 14:25:40 +02001441 if (err)
1442 goto err;
1443 old_timing = host->ios.timing;
1444 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
Chaotian Jing08573ea2016-05-19 16:47:41 +08001445
Adrian Hunteref3d2322016-12-02 13:16:35 +02001446 /*
1447 * For HS200, CRC errors are not a reliable way to know the
1448 * switch failed. If there really is a problem, we would expect
1449 * tuning will fail and the result ends up the same.
1450 */
1451 err = __mmc_switch_status(card, false);
1452
Chaotian Jing08573ea2016-05-19 16:47:41 +08001453 /*
1454 * mmc_select_timing() assumes timing has not changed if
1455 * it is a switch error.
1456 */
1457 if (err == -EBADMSG)
1458 mmc_set_timing(host, old_timing);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001459 }
Girish K Sa4924c72012-01-11 14:04:52 -05001460err:
Dong Aishenge51534c2016-04-21 00:51:30 +08001461 if (err) {
1462 /* fall back to the old signal voltage, if fails report error */
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001463 if (mmc_set_signal_voltage(host, old_signal_voltage))
Dong Aishenge51534c2016-04-21 00:51:30 +08001464 err = -EIO;
1465
Adrian Hunter1815e612015-10-28 14:25:40 +02001466 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1467 __func__, err);
Dong Aishenge51534c2016-04-21 00:51:30 +08001468 }
Girish K Sa4924c72012-01-11 14:04:52 -05001469 return err;
1470}
1471
1472/*
Shawn Lin81ac2af2016-05-26 09:56:22 +08001473 * Activate High Speed, HS200 or HS400ES mode if supported.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001474 */
1475static int mmc_select_timing(struct mmc_card *card)
1476{
1477 int err = 0;
1478
Ulf Hansson148bcab2014-10-20 11:33:53 +02001479 if (!mmc_can_ext_csd(card))
Seungwon Jeon577fb132014-04-23 17:08:44 +09001480 goto bus_speed;
1481
Shawn Lin81ac2af2016-05-26 09:56:22 +08001482 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1483 err = mmc_select_hs400es(card);
1484 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
Seungwon Jeon577fb132014-04-23 17:08:44 +09001485 err = mmc_select_hs200(card);
1486 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1487 err = mmc_select_hs(card);
1488
1489 if (err && err != -EBADMSG)
1490 return err;
1491
Seungwon Jeon577fb132014-04-23 17:08:44 +09001492bus_speed:
1493 /*
1494 * Set the bus speed to the selected bus timing.
1495 * If timing is not selected, backward compatible is the default.
1496 */
1497 mmc_set_bus_speed(card);
Dong Aisheng0400ed02016-04-21 00:51:31 +08001498 return 0;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001499}
1500
1501/*
1502 * Execute tuning sequence to seek the proper bus operating
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001503 * conditions for HS200 and HS400, which sends CMD21 to the device.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001504 */
1505static int mmc_hs200_tuning(struct mmc_card *card)
1506{
1507 struct mmc_host *host = card->host;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001508
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001509 /*
1510 * Timing should be adjusted to the HS400 target
1511 * operation frequency for tuning process
1512 */
1513 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1514 host->ios.bus_width == MMC_BUS_WIDTH_8)
1515 if (host->ops->prepare_hs400_tuning)
1516 host->ops->prepare_hs400_tuning(host, &host->ios);
1517
Adrian Hunter63e415c2014-12-05 19:40:59 +02001518 return mmc_execute_tuning(card);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001519}
1520
1521/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001522 * Handle the detection and initialisation of a card.
1523 *
Deepak Saxena87693922008-06-16 19:20:57 -07001524 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001525 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001526 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001527static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001528 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001529{
1530 struct mmc_card *card;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001531 int err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001532 u32 cid[4];
Philip Rakityb676f032011-02-13 23:13:09 -08001533 u32 rocr;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001534
Pierre Ossmand84075c82007-08-09 13:23:56 +02001535 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001536
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001537 /* Set correct bus mode for MMC before attempting init */
1538 if (!mmc_host_is_spi(host))
1539 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1540
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001541 /*
1542 * Since we're changing the OCR value, we seem to
1543 * need to tell some cards to go back to the idle
1544 * state. We wait 1ms to give cards time to
1545 * respond.
Balaji T Kc3805462011-09-08 22:08:39 +05301546 * mmc_go_idle is needed for eMMC that are asleep
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001547 */
1548 mmc_go_idle(host);
1549
1550 /* The extra bit indicates that we support high capacity */
Philip Rakityb676f032011-02-13 23:13:09 -08001551 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +02001552 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001553 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001554
1555 /*
David Brownellaf517152007-08-08 09:11:32 -07001556 * For SPI, enable CRC as appropriate.
1557 */
1558 if (mmc_host_is_spi(host)) {
1559 err = mmc_spi_set_crc(host, use_spi_crc);
1560 if (err)
1561 goto err;
1562 }
1563
1564 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001565 * Fetch CID from card.
1566 */
David Brownellaf517152007-08-08 09:11:32 -07001567 if (mmc_host_is_spi(host))
1568 err = mmc_send_cid(host, cid);
1569 else
1570 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +02001571 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001572 goto err;
1573
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001574 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001575 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1576 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001577 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001578 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001579
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001580 card = oldcard;
1581 } else {
1582 /*
1583 * Allocate card structure.
1584 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +01001585 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001586 if (IS_ERR(card)) {
1587 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001588 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001589 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001590
Ulf Hansson69041152013-09-13 11:31:33 +02001591 card->ocr = ocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001592 card->type = MMC_TYPE_MMC;
1593 card->rca = 1;
1594 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1595 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001596
1597 /*
Doug Andersoneac86322014-12-02 15:42:45 -08001598 * Call the optional HC's init_card function to handle quirks.
1599 */
1600 if (host->ops->init_card)
1601 host->ops->init_card(host, card);
1602
1603 /*
David Brownellaf517152007-08-08 09:11:32 -07001604 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001605 */
David Brownellaf517152007-08-08 09:11:32 -07001606 if (!mmc_host_is_spi(host)) {
1607 err = mmc_set_relative_addr(card);
1608 if (err)
1609 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001610
David Brownellaf517152007-08-08 09:11:32 -07001611 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1612 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001613
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001614 if (!oldcard) {
1615 /*
1616 * Fetch CSD from card.
1617 */
1618 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001619 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001620 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001621
Pierre Ossmanbd766312007-05-01 16:11:57 +02001622 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001623 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001624 goto free_card;
1625 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001626 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001627 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001628 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001629
1630 /*
Sascha Hauer3d705d12014-08-19 10:45:51 +02001631 * handling only for cards supporting DSR and hosts requesting
1632 * DSR configuration
1633 */
1634 if (card->csd.dsr_imp && host->dsr_req)
1635 mmc_set_dsr(host);
1636
1637 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001638 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001639 */
David Brownellaf517152007-08-08 09:11:32 -07001640 if (!mmc_host_is_spi(host)) {
1641 err = mmc_select_card(card);
1642 if (err)
1643 goto free_card;
1644 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001645
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001646 if (!oldcard) {
Ulf Hansson076ec382014-10-20 13:37:24 +02001647 /* Read extended CSD. */
1648 err = mmc_read_ext_csd(card);
Pierre Ossman17b04292007-07-22 22:18:46 +02001649 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001650 goto free_card;
Philip Rakityb676f032011-02-13 23:13:09 -08001651
Masahiro Yamada87e88652016-04-15 20:16:12 +09001652 /*
1653 * If doing byte addressing, check if required to do sector
Philip Rakityb676f032011-02-13 23:13:09 -08001654 * addressing. Handle the case of <2GB cards needing sector
1655 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1656 * ocr register has bit 30 set for sector addressing.
1657 */
Masahiro Yamada87e88652016-04-15 20:16:12 +09001658 if (rocr & BIT(30))
Philip Rakityb676f032011-02-13 23:13:09 -08001659 mmc_card_set_blockaddr(card);
1660
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001661 /* Erase size depends on CSD and Extended CSD */
1662 mmc_set_erase_size(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001663 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001664
1665 /*
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001666 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001667 * bit. This bit will be lost every time after a reset or power off.
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001668 */
Grégory Soutadé69803d42014-09-15 17:47:09 +02001669 if (card->ext_csd.partition_setting_completed ||
Adrian Hunter83bb24a2012-03-13 05:19:13 -04001670 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001671 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001672 EXT_CSD_ERASE_GROUP_DEF, 1,
1673 card->ext_csd.generic_cmd6_time);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001674
1675 if (err && err != -EBADMSG)
1676 goto free_card;
1677
1678 if (err) {
1679 err = 0;
1680 /*
1681 * Just disable enhanced area off & sz
1682 * will try to enable ERASE_GROUP_DEF
1683 * during next time reinit
1684 */
1685 card->ext_csd.enhanced_area_offset = -EINVAL;
1686 card->ext_csd.enhanced_area_size = -EINVAL;
1687 } else {
1688 card->ext_csd.erase_group_def = 1;
1689 /*
1690 * enable ERASE_GRP_DEF successfully.
1691 * This will affect the erase size, so
1692 * here need to reset erase size
1693 */
1694 mmc_set_erase_size(card);
1695 }
1696 }
1697
1698 /*
Philip Rakity41e2a482011-03-19 14:10:33 -04001699 * Ensure eMMC user default partition is enabled
1700 */
Andrei Warkentin371a6892011-04-11 18:10:25 -05001701 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1702 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1703 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1704 card->ext_csd.part_config,
1705 card->ext_csd.part_time);
1706 if (err && err != -EBADMSG)
1707 goto free_card;
Philip Rakity41e2a482011-03-19 14:10:33 -04001708 }
1709
1710 /*
Ulf Hansson432356792013-06-10 17:03:45 +02001711 * Enable power_off_notification byte in the ext_csd register
Girish K Sbec87262011-10-13 12:04:16 +05301712 */
Ulf Hansson432356792013-06-10 17:03:45 +02001713 if (card->ext_csd.rev >= 6) {
Girish K Sbec87262011-10-13 12:04:16 +05301714 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1715 EXT_CSD_POWER_OFF_NOTIFICATION,
1716 EXT_CSD_POWER_ON,
1717 card->ext_csd.generic_cmd6_time);
1718 if (err && err != -EBADMSG)
1719 goto free_card;
Girish K Sbec87262011-10-13 12:04:16 +05301720
Girish K S96a85d52011-11-04 16:22:47 +05301721 /*
1722 * The err can be -EBADMSG or 0,
1723 * so check for success and update the flag
1724 */
1725 if (!err)
Ulf Hanssone6c08582012-10-05 12:45:39 -04001726 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
Girish K S96a85d52011-11-04 16:22:47 +05301727 }
Girish K Sbec87262011-10-13 12:04:16 +05301728
1729 /*
Seungwon Jeon577fb132014-04-23 17:08:44 +09001730 * Select timing interface
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001731 */
Seungwon Jeon577fb132014-04-23 17:08:44 +09001732 err = mmc_select_timing(card);
1733 if (err)
1734 goto free_card;
Girish K Sa4924c72012-01-11 14:04:52 -05001735
Girish K Sa4924c72012-01-11 14:04:52 -05001736 if (mmc_card_hs200(card)) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001737 err = mmc_hs200_tuning(card);
1738 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001739 goto free_card;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001740
1741 err = mmc_select_hs400(card);
1742 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001743 goto free_card;
Anssi Hannula3d4ef322017-02-13 13:46:41 +02001744 } else {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001745 /* Select the desired bus width optionally */
1746 err = mmc_select_bus_width(card);
Anssi Hannula3d4ef322017-02-13 13:46:41 +02001747 if (err > 0 && mmc_card_hs(card)) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001748 err = mmc_select_hs_ddr(card);
1749 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001750 goto free_card;
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001751 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001752 }
1753
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001754 /*
Seungwon Jeon23850492014-04-23 17:08:05 +09001755 * Choose the power class with selected bus interface
1756 */
1757 mmc_select_powerclass(card);
1758
1759 /*
Subhash Jadavani52d09742012-03-06 17:59:12 +05301760 * Enable HPI feature (if supported)
1761 */
1762 if (card->ext_csd.hpi) {
1763 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1764 EXT_CSD_HPI_MGMT, 1,
1765 card->ext_csd.generic_cmd6_time);
1766 if (err && err != -EBADMSG)
1767 goto free_card;
1768 if (err) {
Joe Perches66061102014-09-12 14:56:56 -07001769 pr_warn("%s: Enabling HPI failed\n",
1770 mmc_hostname(card->host));
Subhash Jadavani52d09742012-03-06 17:59:12 +05301771 err = 0;
1772 } else
1773 card->ext_csd.hpi_en = 1;
1774 }
1775
1776 /*
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001777 * If cache size is higher than 0, this indicates
1778 * the existence of cache and it can be turned on.
1779 */
Pratibhasagar V53202262016-06-09 18:09:31 -04001780 if (!mmc_card_broken_hpi(card) &&
1781 card->ext_csd.cache_size > 0) {
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001782 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001783 EXT_CSD_CACHE_CTRL, 1,
1784 card->ext_csd.generic_cmd6_time);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001785 if (err && err != -EBADMSG)
1786 goto free_card;
1787
1788 /*
1789 * Only if no error, cache is turned on successfully.
1790 */
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001791 if (err) {
Joe Perches66061102014-09-12 14:56:56 -07001792 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1793 mmc_hostname(card->host), err);
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001794 card->ext_csd.cache_ctrl = 0;
1795 err = 0;
1796 } else {
1797 card->ext_csd.cache_ctrl = 1;
1798 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001799 }
1800
Seungwon Jeonabd9ac12013-02-06 17:01:43 +09001801 /*
1802 * The mandatory minimum values are defined for packed command.
1803 * read: 5, write: 3
1804 */
1805 if (card->ext_csd.max_packed_writes >= 3 &&
1806 card->ext_csd.max_packed_reads >= 5 &&
1807 host->caps2 & MMC_CAP2_PACKED_CMD) {
1808 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1809 EXT_CSD_EXP_EVENTS_CTRL,
1810 EXT_CSD_PACKED_EVENT_EN,
1811 card->ext_csd.generic_cmd6_time);
1812 if (err && err != -EBADMSG)
1813 goto free_card;
1814 if (err) {
1815 pr_warn("%s: Enabling packed event failed\n",
1816 mmc_hostname(card->host));
1817 card->ext_csd.packed_event_en = 0;
1818 err = 0;
1819 } else {
1820 card->ext_csd.packed_event_en = 1;
1821 }
1822 }
1823
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001824 if (!oldcard)
1825 host->card = card;
1826
Pierre Ossman17b04292007-07-22 22:18:46 +02001827 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001828
1829free_card:
1830 if (!oldcard)
1831 mmc_remove_card(card);
1832err:
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001833 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001834}
1835
Ulf Hansson07a68212013-04-19 15:12:11 +02001836static int mmc_can_sleep(struct mmc_card *card)
1837{
1838 return (card && card->ext_csd.rev >= 3);
1839}
1840
1841static int mmc_sleep(struct mmc_host *host)
1842{
Masahiro Yamadac7836d12016-12-19 20:51:18 +09001843 struct mmc_command cmd = {};
Ulf Hansson07a68212013-04-19 15:12:11 +02001844 struct mmc_card *card = host->card;
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001845 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
Ulf Hansson07a68212013-04-19 15:12:11 +02001846 int err;
1847
Adrian Hunter436f8da2015-05-07 13:10:18 +03001848 /* Re-tuning can't be done once the card is deselected */
1849 mmc_retune_hold(host);
1850
Ulf Hansson07a68212013-04-19 15:12:11 +02001851 err = mmc_deselect_cards(host);
1852 if (err)
Adrian Hunter436f8da2015-05-07 13:10:18 +03001853 goto out_release;
Ulf Hansson07a68212013-04-19 15:12:11 +02001854
1855 cmd.opcode = MMC_SLEEP_AWAKE;
1856 cmd.arg = card->rca << 16;
1857 cmd.arg |= 1 << 15;
1858
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001859 /*
1860 * If the max_busy_timeout of the host is specified, validate it against
1861 * the sleep cmd timeout. A failure means we need to prevent the host
1862 * from doing hw busy detection, which is done by converting to a R1
1863 * response instead of a R1B.
1864 */
1865 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1866 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1867 } else {
1868 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1869 cmd.busy_timeout = timeout_ms;
1870 }
1871
Ulf Hansson07a68212013-04-19 15:12:11 +02001872 err = mmc_wait_for_cmd(host, &cmd, 0);
1873 if (err)
Adrian Hunter436f8da2015-05-07 13:10:18 +03001874 goto out_release;
Ulf Hansson07a68212013-04-19 15:12:11 +02001875
1876 /*
1877 * If the host does not wait while the card signals busy, then we will
1878 * will have to wait the sleep/awake timeout. Note, we cannot use the
1879 * SEND_STATUS command to poll the status because that command (and most
1880 * others) is invalid while the card sleeps.
1881 */
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001882 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1883 mmc_delay(timeout_ms);
Ulf Hansson07a68212013-04-19 15:12:11 +02001884
Adrian Hunter436f8da2015-05-07 13:10:18 +03001885out_release:
1886 mmc_retune_release(host);
Ulf Hansson07a68212013-04-19 15:12:11 +02001887 return err;
1888}
1889
Ulf Hanssone6c08582012-10-05 12:45:39 -04001890static int mmc_can_poweroff_notify(const struct mmc_card *card)
1891{
1892 return card &&
1893 mmc_card_mmc(card) &&
1894 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1895}
1896
1897static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1898{
1899 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1900 int err;
1901
1902 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1903 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1904 timeout = card->ext_csd.power_off_longtime;
1905
Ulf Hansson878e2002013-09-09 11:57:57 +02001906 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1907 EXT_CSD_POWER_OFF_NOTIFICATION,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001908 notify_type, timeout, 0, true, false, false);
Ulf Hanssone6c08582012-10-05 12:45:39 -04001909 if (err)
1910 pr_err("%s: Power Off Notification timed out, %u\n",
1911 mmc_hostname(card->host), timeout);
1912
1913 /* Disable the power off notification after the switch operation. */
1914 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1915
1916 return err;
1917}
1918
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001919/*
1920 * Host is being removed. Free up the current card.
1921 */
1922static void mmc_remove(struct mmc_host *host)
1923{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001924 mmc_remove_card(host->card);
1925 host->card = NULL;
1926}
1927
1928/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001929 * Card detection - card is alive.
1930 */
1931static int mmc_alive(struct mmc_host *host)
1932{
1933 return mmc_send_status(host->card, NULL);
1934}
1935
1936/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001937 * Card detection callback from host.
1938 */
1939static void mmc_detect(struct mmc_host *host)
1940{
1941 int err;
1942
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001943 mmc_get_card(host->card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001944
1945 /*
1946 * Just check if our card has been removed.
1947 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001948 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001949
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001950 mmc_put_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001951
Pierre Ossman17b04292007-07-22 22:18:46 +02001952 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001953 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001954
1955 mmc_claim_host(host);
1956 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001957 mmc_power_off(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001958 mmc_release_host(host);
1959 }
1960}
1961
Ulf Hansson03d071f2013-06-10 17:03:43 +02001962static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001963{
Balaji T Kc3805462011-09-08 22:08:39 +05301964 int err = 0;
Ulf Hansson03d071f2013-06-10 17:03:43 +02001965 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1966 EXT_CSD_POWER_OFF_LONG;
Balaji T Kc3805462011-09-08 22:08:39 +05301967
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001968 mmc_claim_host(host);
Maya Erez881d9262013-01-28 16:44:22 -05001969
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001970 if (mmc_card_suspended(host->card))
1971 goto out;
1972
Ulf Hansson39b94312013-05-02 14:02:36 +02001973 if (mmc_card_doing_bkops(host->card)) {
1974 err = mmc_stop_bkops(host->card);
1975 if (err)
1976 goto out;
1977 }
1978
Ulf Hansson10e5d962013-12-16 16:23:22 +01001979 err = mmc_flush_cache(host->card);
Maya Erez881d9262013-01-28 16:44:22 -05001980 if (err)
1981 goto out;
1982
Ulf Hansson432356792013-06-10 17:03:45 +02001983 if (mmc_can_poweroff_notify(host->card) &&
Ulf Hansson53275c22013-06-10 17:03:46 +02001984 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
Ulf Hansson03d071f2013-06-10 17:03:43 +02001985 err = mmc_poweroff_notify(host->card, notify_type);
Ulf Hansson07a68212013-04-19 15:12:11 +02001986 else if (mmc_can_sleep(host->card))
1987 err = mmc_sleep(host);
Ulf Hanssone6c08582012-10-05 12:45:39 -04001988 else if (!mmc_host_is_spi(host))
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001989 err = mmc_deselect_cards(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001990
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001991 if (!err) {
Ulf Hansson74590262013-06-10 17:03:38 +02001992 mmc_power_off(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001993 mmc_card_set_suspended(host->card);
1994 }
Maya Erez881d9262013-01-28 16:44:22 -05001995out:
1996 mmc_release_host(host);
Balaji T Kc3805462011-09-08 22:08:39 +05301997 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001998}
1999
2000/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002001 * Suspend callback
Ulf Hansson03d071f2013-06-10 17:03:43 +02002002 */
2003static int mmc_suspend(struct mmc_host *host)
2004{
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002005 int err;
2006
2007 err = _mmc_suspend(host, true);
2008 if (!err) {
2009 pm_runtime_disable(&host->card->dev);
2010 pm_runtime_set_suspended(&host->card->dev);
2011 }
2012
2013 return err;
Ulf Hansson03d071f2013-06-10 17:03:43 +02002014}
2015
2016/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002017 * This function tries to determine if the same card is still present
2018 * and, if so, restore all state to it.
2019 */
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002020static int _mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002021{
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002022 int err = 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002023
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002024 mmc_claim_host(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002025
2026 if (!mmc_card_suspended(host->card))
2027 goto out;
2028
Ulf Hansson69041152013-09-13 11:31:33 +02002029 mmc_power_up(host, host->card->ocr);
Ulf Hansson69041152013-09-13 11:31:33 +02002030 err = mmc_init_card(host, host->card->ocr, host->card);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002031 mmc_card_clr_suspended(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002032
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002033out:
2034 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002035 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002036}
2037
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002038/*
2039 * Shutdown callback
2040 */
2041static int mmc_shutdown(struct mmc_host *host)
2042{
2043 int err = 0;
2044
2045 /*
2046 * In a specific case for poweroff notify, we need to resume the card
2047 * before we can shutdown it properly.
2048 */
2049 if (mmc_can_poweroff_notify(host->card) &&
2050 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002051 err = _mmc_resume(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002052
2053 if (!err)
2054 err = _mmc_suspend(host, false);
2055
2056 return err;
2057}
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002058
2059/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002060 * Callback for resume.
2061 */
2062static int mmc_resume(struct mmc_host *host)
2063{
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002064 pm_runtime_enable(&host->card->dev);
Ulf Hanssonc29536e2015-11-05 16:01:32 +01002065 return 0;
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002066}
2067
2068/*
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002069 * Callback for runtime_suspend.
2070 */
2071static int mmc_runtime_suspend(struct mmc_host *host)
2072{
2073 int err;
2074
2075 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2076 return 0;
2077
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002078 err = _mmc_suspend(host, true);
Ulf Hansson0cc81a82013-10-03 11:24:44 +02002079 if (err)
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002080 pr_err("%s: error %d doing aggressive suspend\n",
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002081 mmc_hostname(host), err);
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002082
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002083 return err;
2084}
2085
2086/*
2087 * Callback for runtime_resume.
2088 */
2089static int mmc_runtime_resume(struct mmc_host *host)
2090{
2091 int err;
2092
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002093 err = _mmc_resume(host);
Adrian Hunter520322d2015-12-14 15:51:27 +02002094 if (err && err != -ENOMEDIUM)
Ulf Hanssonc29536e2015-11-05 16:01:32 +01002095 pr_err("%s: error %d doing runtime resume\n",
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002096 mmc_hostname(host), err);
2097
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002098 return 0;
2099}
2100
Johan Rudholmf855a372015-01-12 15:38:05 +01002101int mmc_can_reset(struct mmc_card *card)
2102{
2103 u8 rst_n_function;
2104
2105 rst_n_function = card->ext_csd.rst_n_function;
2106 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2107 return 0;
2108 return 1;
2109}
2110EXPORT_SYMBOL(mmc_can_reset);
2111
2112static int mmc_reset(struct mmc_host *host)
2113{
2114 struct mmc_card *card = host->card;
Johan Rudholmf855a372015-01-12 15:38:05 +01002115
Adrian Hunter437db4c2016-05-04 10:38:21 +03002116 /*
2117 * In the case of recovery, we can't expect flushing the cache to work
2118 * always, but we have a go and ignore errors.
2119 */
2120 mmc_flush_cache(host->card);
2121
Gwendal Grignou4e6c7172016-04-01 16:04:22 -07002122 if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2123 mmc_can_reset(card)) {
2124 /* If the card accept RST_n signal, send it. */
2125 mmc_set_clock(host, host->f_init);
2126 host->ops->hw_reset(host);
2127 /* Set initial state and call mmc_set_ios */
2128 mmc_set_initial_state(host);
2129 } else {
2130 /* Do a brute force power cycle */
2131 mmc_power_cycle(host, card->ocr);
2132 }
Ulf Hansson364549d2015-06-01 11:14:57 +02002133 return mmc_init_card(host, card->ocr, card);
Johan Rudholmf855a372015-01-12 15:38:05 +01002134}
2135
Adrian Hunter9feae242009-09-22 16:44:32 -07002136static const struct mmc_bus_ops mmc_ops = {
2137 .remove = mmc_remove,
2138 .detect = mmc_detect,
Adrian Hunter9feae242009-09-22 16:44:32 -07002139 .suspend = mmc_suspend,
2140 .resume = mmc_resume,
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002141 .runtime_suspend = mmc_runtime_suspend,
2142 .runtime_resume = mmc_runtime_resume,
Adrian Hunterd3049502011-11-28 16:22:00 +02002143 .alive = mmc_alive,
Ulf Hansson486fdbb2013-06-10 17:03:44 +02002144 .shutdown = mmc_shutdown,
Johan Rudholmf855a372015-01-12 15:38:05 +01002145 .reset = mmc_reset,
Adrian Hunter9feae242009-09-22 16:44:32 -07002146};
2147
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002148/*
2149 * Starting point for MMC card init.
2150 */
Andy Ross807e8e42011-01-03 10:36:56 -08002151int mmc_attach_mmc(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002152{
2153 int err;
Ulf Hansson69041152013-09-13 11:31:33 +02002154 u32 ocr, rocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002155
Pierre Ossmand84075c82007-08-09 13:23:56 +02002156 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002157
Stefan Nilsson XK44669032011-09-15 17:50:38 +02002158 /* Set correct bus mode for MMC before attempting attach */
2159 if (!mmc_host_is_spi(host))
2160 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2161
Andy Ross807e8e42011-01-03 10:36:56 -08002162 err = mmc_send_op_cond(host, 0, &ocr);
2163 if (err)
2164 return err;
2165
Ulf Hansson2501c912013-10-30 01:00:18 +01002166 mmc_attach_bus(host, &mmc_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01002167 if (host->ocr_avail_mmc)
2168 host->ocr_avail = host->ocr_avail_mmc;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002169
2170 /*
David Brownellaf517152007-08-08 09:11:32 -07002171 * We need to get OCR a different way for SPI.
2172 */
2173 if (mmc_host_is_spi(host)) {
2174 err = mmc_spi_read_ocr(host, 1, &ocr);
2175 if (err)
2176 goto err;
2177 }
2178
Ulf Hansson69041152013-09-13 11:31:33 +02002179 rocr = mmc_select_voltage(host, ocr);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002180
2181 /*
2182 * Can we support the voltage of the card?
2183 */
Ulf Hansson69041152013-09-13 11:31:33 +02002184 if (!rocr) {
Pierre Ossman109b5be2007-07-23 00:12:10 +02002185 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002186 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02002187 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002188
2189 /*
2190 * Detect and init the card.
2191 */
Ulf Hansson69041152013-09-13 11:31:33 +02002192 err = mmc_init_card(host, rocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02002193 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002194 goto err;
2195
2196 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02002197 err = mmc_add_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002198 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002199 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002200
Sergei Shtylyov2860d062015-10-14 23:53:03 +03002201 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002202 return 0;
2203
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002204remove_card:
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002205 mmc_remove_card(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002206 mmc_claim_host(host);
Andy Ross807e8e42011-01-03 10:36:56 -08002207 host->card = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002208err:
2209 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002210
Girish K Sa3c76eb2011-10-11 11:44:09 +05302211 pr_err("%s: error %d whilst initialising MMC card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02002212 mmc_hostname(host), err);
2213
Pierre Ossmanadf66a02007-07-22 23:08:30 +02002214 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002215}