blob: 55997cf84b39f5dd9c3ced5ea9db6702cb02c65a [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"
Ulf Hansson52c82122017-05-08 23:52:04 +020030#include "pwrseq.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010031
Ulf Hanssonfe1b5702016-11-04 18:32:33 +010032#define DEFAULT_CMD6_TIMEOUT_MS 500
Ulf Hanssone3ae3402018-12-10 17:52:38 +010033#define MIN_CACHE_EN_TIMEOUT_MS 1600
Ulf Hanssonfe1b5702016-11-04 18:32:33 +010034
Pierre Ossman7ea239d2006-12-31 00:11:32 +010035static const unsigned int tran_exp[] = {
36 10000, 100000, 1000000, 10000000,
37 0, 0, 0, 0
38};
39
40static const unsigned char tran_mant[] = {
41 0, 10, 12, 13, 15, 20, 25, 30,
42 35, 40, 45, 50, 55, 60, 70, 80,
43};
44
Shawn Lin4406ae22017-08-02 11:12:42 +080045static const unsigned int taac_exp[] = {
Pierre Ossman7ea239d2006-12-31 00:11:32 +010046 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
47};
48
Shawn Lin4406ae22017-08-02 11:12:42 +080049static const unsigned int taac_mant[] = {
Pierre Ossman7ea239d2006-12-31 00:11:32 +010050 0, 10, 12, 13, 15, 20, 25, 30,
51 35, 40, 45, 50, 55, 60, 70, 80,
52};
53
54#define UNSTUFF_BITS(resp,start,size) \
55 ({ \
56 const int __size = size; \
57 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
58 const int __off = 3 - ((start) / 32); \
59 const int __shft = (start) & 31; \
60 u32 __res; \
61 \
62 __res = resp[__off] >> __shft; \
63 if (__size + __shft > 32) \
64 __res |= resp[__off-1] << ((32 - __shft) % 32); \
65 __res & __mask; \
66 })
67
68/*
69 * Given the decoded CSD structure, decode the raw CID to our CID structure.
70 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020071static int mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010072{
73 u32 *resp = card->raw_cid;
74
75 /*
76 * The selection of the format here is based upon published
77 * specs from sandisk and from what people have reported.
78 */
79 switch (card->csd.mmca_vsn) {
80 case 0: /* MMC v1.0 - v1.2 */
81 case 1: /* MMC v1.4 */
82 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
83 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
84 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
85 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
86 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
87 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
88 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
89 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
90 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
91 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
92 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
93 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
94 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
95 break;
96
97 case 2: /* MMC v2.0 - v2.2 */
98 case 3: /* MMC v3.1 - v3.3 */
99 case 4: /* MMC v4 */
100 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
101 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
102 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
103 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
104 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
105 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
106 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
107 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800108 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100109 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
110 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
111 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
112 break;
113
114 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530115 pr_err("%s: card has unknown MMCA version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100116 mmc_hostname(card->host), card->csd.mmca_vsn);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200117 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100118 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200119
120 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100121}
122
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700123static void mmc_set_erase_size(struct mmc_card *card)
124{
125 if (card->ext_csd.erase_group_def & 1)
126 card->erase_size = card->ext_csd.hc_erase_size;
127 else
128 card->erase_size = card->csd.erase_size;
129
130 mmc_init_erase(card);
131}
132
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100133/*
134 * Given a 128-bit response, decode to our card CSD structure.
135 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200136static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100137{
138 struct mmc_csd *csd = &card->csd;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700139 unsigned int e, m, a, b;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100140 u32 *resp = card->raw_csd;
141
142 /*
143 * We only understand CSD structure v1.1 and v1.2.
144 * v1.2 has extra information in bits 15, 11 and 10.
Kyungmin Park6da24b72010-08-10 18:01:36 -0700145 * We also support eMMC v4.4 & v4.41.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100146 */
Kyungmin Park6da24b72010-08-10 18:01:36 -0700147 csd->structure = UNSTUFF_BITS(resp, 126, 2);
148 if (csd->structure == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530149 pr_err("%s: unrecognised CSD structure version %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700150 mmc_hostname(card->host), csd->structure);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200151 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100152 }
153
154 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
155 m = UNSTUFF_BITS(resp, 115, 4);
156 e = UNSTUFF_BITS(resp, 112, 3);
Shawn Lin4406ae22017-08-02 11:12:42 +0800157 csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
158 csd->taac_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100159
160 m = UNSTUFF_BITS(resp, 99, 4);
161 e = UNSTUFF_BITS(resp, 96, 3);
162 csd->max_dtr = tran_exp[e] * tran_mant[m];
163 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
164
165 e = UNSTUFF_BITS(resp, 47, 3);
166 m = UNSTUFF_BITS(resp, 62, 12);
167 csd->capacity = (1 + m) << (e + 2);
168
169 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
170 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
171 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
172 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
Sascha Hauer3d705d12014-08-19 10:45:51 +0200173 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100174 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
175 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
176 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200177
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700178 if (csd->write_blkbits >= 9) {
179 a = UNSTUFF_BITS(resp, 42, 5);
180 b = UNSTUFF_BITS(resp, 37, 5);
181 csd->erase_size = (a + 1) * (b + 1);
182 csd->erase_size <<= csd->write_blkbits - 9;
183 }
184
Pierre Ossmanbd766312007-05-01 16:11:57 +0200185 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100186}
187
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900188static void mmc_select_card_type(struct mmc_card *card)
189{
190 struct mmc_host *host = card->host;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900191 u8 card_type = card->ext_csd.raw_card_type;
Lee Jones5f1a4dd2012-11-14 12:35:51 +0000192 u32 caps = host->caps, caps2 = host->caps2;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900193 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900194 unsigned int avail_type = 0;
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900195
196 if (caps & MMC_CAP_MMC_HIGHSPEED &&
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900197 card_type & EXT_CSD_CARD_TYPE_HS_26) {
198 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
199 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
200 }
201
202 if (caps & MMC_CAP_MMC_HIGHSPEED &&
203 card_type & EXT_CSD_CARD_TYPE_HS_52) {
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900204 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900205 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
206 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900207
Ulf Hansson20f921b2017-01-25 12:04:17 +0100208 if (caps & (MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR) &&
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900209 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900210 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900211 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
212 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900213
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900214 if (caps & MMC_CAP_1_2V_DDR &&
215 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
216 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
217 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
218 }
219
220 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
221 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
Seungwon Jeon577fb132014-04-23 17:08:44 +0900222 hs200_max_dtr = MMC_HS200_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900223 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
224 }
225
226 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
227 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
Seungwon Jeon577fb132014-04-23 17:08:44 +0900228 hs200_max_dtr = MMC_HS200_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900229 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
230 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900231
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900232 if (caps2 & MMC_CAP2_HS400_1_8V &&
233 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
234 hs200_max_dtr = MMC_HS200_MAX_DTR;
235 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
236 }
237
238 if (caps2 & MMC_CAP2_HS400_1_2V &&
239 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
240 hs200_max_dtr = MMC_HS200_MAX_DTR;
241 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
242 }
243
Shawn Lin81ac2af2016-05-26 09:56:22 +0800244 if ((caps2 & MMC_CAP2_HS400_ES) &&
245 card->ext_csd.strobe_support &&
246 (avail_type & EXT_CSD_CARD_TYPE_HS400))
247 avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
248
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900249 card->ext_csd.hs_max_dtr = hs_max_dtr;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900250 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900251 card->mmc_avail_type = avail_type;
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900252}
253
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200254static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
255{
Grégory Soutadé994324b2014-09-15 17:47:11 +0200256 u8 hc_erase_grp_sz, hc_wp_grp_sz;
257
258 /*
259 * Disable these attributes by default
260 */
261 card->ext_csd.enhanced_area_offset = -EINVAL;
262 card->ext_csd.enhanced_area_size = -EINVAL;
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200263
264 /*
265 * Enhanced area feature support -- check whether the eMMC
266 * card has the Enhanced area enabled. If so, export enhanced
267 * area offset and size to user by adding sysfs interface.
268 */
269 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
270 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
Grégory Soutadé994324b2014-09-15 17:47:11 +0200271 if (card->ext_csd.partition_setting_completed) {
272 hc_erase_grp_sz =
273 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
274 hc_wp_grp_sz =
275 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200276
Grégory Soutadé994324b2014-09-15 17:47:11 +0200277 /*
278 * calculate the enhanced data area offset, in bytes
279 */
280 card->ext_csd.enhanced_area_offset =
Kuninori Morimotoded8a5f2015-05-11 07:34:53 +0000281 (((unsigned long long)ext_csd[139]) << 24) +
282 (((unsigned long long)ext_csd[138]) << 16) +
283 (((unsigned long long)ext_csd[137]) << 8) +
284 (((unsigned long long)ext_csd[136]));
Grégory Soutadé994324b2014-09-15 17:47:11 +0200285 if (mmc_card_blockaddr(card))
286 card->ext_csd.enhanced_area_offset <<= 9;
287 /*
288 * calculate the enhanced data area size, in kilobytes
289 */
290 card->ext_csd.enhanced_area_size =
291 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
292 ext_csd[140];
293 card->ext_csd.enhanced_area_size *=
294 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
295 card->ext_csd.enhanced_area_size <<= 9;
296 } else {
297 pr_warn("%s: defines enhanced area without partition setting complete\n",
298 mmc_hostname(card->host));
299 }
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200300 }
301}
302
Ulf Hansson4facdde2017-01-13 14:14:14 +0100303static void mmc_part_add(struct mmc_card *card, unsigned int size,
304 unsigned int part_cfg, char *name, int idx, bool ro,
305 int area_type)
306{
307 card->part[card->nr_parts].size = size;
308 card->part[card->nr_parts].part_cfg = part_cfg;
309 sprintf(card->part[card->nr_parts].name, name, idx);
310 card->part[card->nr_parts].force_ro = ro;
311 card->part[card->nr_parts].area_type = area_type;
312 card->nr_parts++;
313}
314
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200315static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
316{
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200317 int idx;
Grégory Soutadé994324b2014-09-15 17:47:11 +0200318 u8 hc_erase_grp_sz, hc_wp_grp_sz;
319 unsigned int part_size;
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200320
321 /*
322 * General purpose partition feature support --
323 * If ext_csd has the size of general purpose partitions,
324 * set size, part_cfg, partition name in mmc_part.
325 */
326 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
327 EXT_CSD_PART_SUPPORT_PART_EN) {
Grégory Soutadé994324b2014-09-15 17:47:11 +0200328 hc_erase_grp_sz =
329 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
330 hc_wp_grp_sz =
331 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200332
333 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
334 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
335 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
336 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
337 continue;
Grégory Soutadé994324b2014-09-15 17:47:11 +0200338 if (card->ext_csd.partition_setting_completed == 0) {
339 pr_warn("%s: has partition size defined without partition complete\n",
340 mmc_hostname(card->host));
341 break;
342 }
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200343 part_size =
344 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
345 << 16) +
346 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
347 << 8) +
348 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
349 part_size *= (size_t)(hc_erase_grp_sz *
350 hc_wp_grp_sz);
351 mmc_part_add(card, part_size << 19,
352 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
353 "gp%d", idx, false,
354 MMC_BLK_DATA_AREA_GP);
355 }
356 }
357}
358
Adrian Hunter1c447112016-05-05 08:12:28 +0300359/* Minimum partition switch timeout in milliseconds */
360#define MMC_MIN_PART_SWITCH_TIME 300
361
Philip Rakity08ee80c2011-05-24 18:14:58 -0700362/*
363 * Decode extended CSD.
364 */
Ulf Hansson076ec382014-10-20 13:37:24 +0200365static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700366{
Namjae Jeone0c368d2011-10-06 23:41:38 +0900367 int err = 0, idx;
368 unsigned int part_size;
Hans de Goede81f8a7b2015-04-01 17:26:23 +0200369 struct device_node *np;
370 bool broken_hpi = false;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700371
Kyungmin Park6da24b72010-08-10 18:01:36 -0700372 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700373 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
Kyungmin Park6da24b72010-08-10 18:01:36 -0700374 if (card->csd.structure == 3) {
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700375 if (card->ext_csd.raw_ext_csd_structure > 2) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530376 pr_err("%s: unrecognised EXT_CSD structure "
Kyungmin Park6da24b72010-08-10 18:01:36 -0700377 "version %d\n", mmc_hostname(card->host),
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700378 card->ext_csd.raw_ext_csd_structure);
Kyungmin Park6da24b72010-08-10 18:01:36 -0700379 err = -EINVAL;
380 goto out;
381 }
382 }
383
Hans de Goede81f8a7b2015-04-01 17:26:23 +0200384 np = mmc_of_find_child_device(card->host, 0);
385 if (np && of_device_is_compatible(np, "mmc-card"))
386 broken_hpi = of_property_read_bool(np, "broken-hpi");
387 of_node_put(np);
388
Romain Izard03a59432014-06-27 10:51:07 +0200389 /*
390 * The EXT_CSD format is meant to be forward compatible. As long
391 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
392 * are authorized, see JEDEC JESD84-B50 section B.8.
393 */
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700394 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Pierre Ossmand7604d72007-07-23 00:34:07 +0200395
Pratibhasagar V53202262016-06-09 18:09:31 -0400396 /* fixup device after ext_csd revision field is updated */
397 mmc_fixup_device(card, mmc_ext_csd_fixups);
398
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700399 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
400 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
401 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
402 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700403 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200404 card->ext_csd.sectors =
405 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
406 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
407 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
408 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700409
410 /* Cards with density > 2GiB are sector addressed */
411 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200412 mmc_card_set_blockaddr(card);
413 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900414
Shawn Lin81ac2af2016-05-26 09:56:22 +0800415 card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700416 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900417 mmc_select_card_type(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100418
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700419 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
420 card->ext_csd.raw_erase_timeout_mult =
421 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
422 card->ext_csd.raw_hc_erase_grp_size =
423 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700424 if (card->ext_csd.rev >= 3) {
425 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500426 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
427
428 /* EXT_CSD value is in units of 10ms, but we store in ms */
429 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
Adrian Hunter1c447112016-05-05 08:12:28 +0300430 /* Some eMMC set the value too low so set a minimum */
431 if (card->ext_csd.part_time &&
432 card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
433 card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700434
435 /* Sleep / awake timeout in 100ns units */
436 if (sa_shift > 0 && sa_shift <= 0x17)
437 card->ext_csd.sa_timeout =
438 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700439 card->ext_csd.erase_group_def =
440 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
441 card->ext_csd.hc_erase_timeout = 300 *
442 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
443 card->ext_csd.hc_erase_size =
444 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500445
446 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500447
448 /*
449 * There are two boot regions of equal size, defined in
450 * multiples of 128K.
451 */
Namjae Jeone0c368d2011-10-06 23:41:38 +0900452 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
453 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
454 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
455 mmc_part_add(card, part_size,
456 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100457 "boot%d", idx, true,
458 MMC_BLK_DATA_AREA_BOOT);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900459 }
460 }
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700461 }
462
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700463 card->ext_csd.raw_hc_erase_gap_size =
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100464 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700465 card->ext_csd.raw_sec_trim_mult =
466 ext_csd[EXT_CSD_SEC_TRIM_MULT];
467 card->ext_csd.raw_sec_erase_mult =
468 ext_csd[EXT_CSD_SEC_ERASE_MULT];
469 card->ext_csd.raw_sec_feature_support =
470 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
471 card->ext_csd.raw_trim_mult =
472 ext_csd[EXT_CSD_TRIM_MULT];
Philip Rakity836dc2f2013-04-04 20:18:11 +0100473 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
Adrian Hunterb097e072015-02-06 14:12:57 +0200474 card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700475 if (card->ext_csd.rev >= 4) {
Grégory Soutadé69803d42014-09-15 17:47:09 +0200476 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
477 EXT_CSD_PART_SETTING_COMPLETED)
478 card->ext_csd.partition_setting_completed = 1;
479 else
480 card->ext_csd.partition_setting_completed = 0;
481
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200482 mmc_manage_enhanced_area(card, ext_csd);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800483
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200484 mmc_manage_gp_partitions(card, ext_csd);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900485
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700486 card->ext_csd.sec_trim_mult =
487 ext_csd[EXT_CSD_SEC_TRIM_MULT];
488 card->ext_csd.sec_erase_mult =
489 ext_csd[EXT_CSD_SEC_ERASE_MULT];
490 card->ext_csd.sec_feature_support =
491 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
492 card->ext_csd.trim_timeout = 300 *
493 ext_csd[EXT_CSD_TRIM_MULT];
Johan Rudholmadd710e2011-12-02 08:51:06 +0100494
495 /*
496 * Note that the call to mmc_part_add above defaults to read
497 * only. If this default assumption is changed, the call must
498 * take into account the value of boot_locked below.
499 */
500 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
501 card->ext_csd.boot_ro_lockable = true;
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200502
503 /* Save power class values */
504 card->ext_csd.raw_pwr_cl_52_195 =
505 ext_csd[EXT_CSD_PWR_CL_52_195];
506 card->ext_csd.raw_pwr_cl_26_195 =
507 ext_csd[EXT_CSD_PWR_CL_26_195];
508 card->ext_csd.raw_pwr_cl_52_360 =
509 ext_csd[EXT_CSD_PWR_CL_52_360];
510 card->ext_csd.raw_pwr_cl_26_360 =
511 ext_csd[EXT_CSD_PWR_CL_26_360];
512 card->ext_csd.raw_pwr_cl_200_195 =
513 ext_csd[EXT_CSD_PWR_CL_200_195];
514 card->ext_csd.raw_pwr_cl_200_360 =
515 ext_csd[EXT_CSD_PWR_CL_200_360];
516 card->ext_csd.raw_pwr_cl_ddr_52_195 =
517 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
518 card->ext_csd.raw_pwr_cl_ddr_52_360 =
519 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900520 card->ext_csd.raw_pwr_cl_ddr_200_360 =
521 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700522 }
523
Adrian Hunterb2499512011-08-29 16:42:11 +0300524 if (card->ext_csd.rev >= 5) {
Romain Izard7c4f10a2013-06-14 14:25:44 +0200525 /* Adjust production date as per JEDEC JESD84-B451 */
526 if (card->cid.year < 2010)
527 card->cid.year += 16;
528
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000529 /* check whether the eMMC card supports BKOPS */
Ulf Hanssonba9f39a2018-12-10 17:52:37 +0100530 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000531 card->ext_csd.bkops = 1;
Alexey Skidanov0501be62015-01-29 10:49:43 +0200532 card->ext_csd.man_bkops_en =
533 (ext_csd[EXT_CSD_BKOPS_EN] &
534 EXT_CSD_MANUAL_BKOPS_MASK);
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000535 card->ext_csd.raw_bkops_status =
536 ext_csd[EXT_CSD_BKOPS_STATUS];
Uri Yanai6962c2f2017-02-07 18:00:00 +0200537 if (card->ext_csd.man_bkops_en)
538 pr_debug("%s: MAN_BKOPS_EN bit is set\n",
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000539 mmc_hostname(card->host));
Uri Yanaiefff8e72017-02-07 18:00:01 +0200540 card->ext_csd.auto_bkops_en =
541 (ext_csd[EXT_CSD_BKOPS_EN] &
542 EXT_CSD_AUTO_BKOPS_MASK);
543 if (card->ext_csd.auto_bkops_en)
544 pr_debug("%s: AUTO_BKOPS_EN bit is set\n",
545 mmc_hostname(card->host));
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000546 }
547
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400548 /* check whether the eMMC card supports HPI */
Pratibhasagar V53202262016-06-09 18:09:31 -0400549 if (!mmc_card_broken_hpi(card) &&
550 !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400551 card->ext_csd.hpi = 1;
552 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
553 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
554 else
555 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
556 /*
557 * Indicate the maximum timeout to close
558 * a command interrupted by HPI
559 */
560 card->ext_csd.out_of_int_time =
561 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
562 }
563
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500564 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
Adrian Hunterb2499512011-08-29 16:42:11 +0300565 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
Loic Pallardy090d25f2012-11-17 18:08:24 -0500566
567 /*
568 * RPMB regions are defined in multiples of 128K.
569 */
570 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
Balaji T Kd0123cc2013-01-25 17:00:30 +0530571 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
Loic Pallardy090d25f2012-11-17 18:08:24 -0500572 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
573 EXT_CSD_PART_CONFIG_ACC_RPMB,
574 "rpmb", 0, false,
575 MMC_BLK_DATA_AREA_RPMB);
576 }
Adrian Hunterb2499512011-08-29 16:42:11 +0300577 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500578
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400579 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700580 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
581 card->erased_byte = 0xFF;
582 else
583 card->erased_byte = 0x0;
584
Seungwon Jeon336c7162011-10-25 09:43:12 +0900585 /* eMMC v4.5 or later */
Ulf Hanssonfe1b5702016-11-04 18:32:33 +0100586 card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS;
Girish K Sbec87262011-10-13 12:04:16 +0530587 if (card->ext_csd.rev >= 6) {
Seungwon Jeon336c7162011-10-25 09:43:12 +0900588 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
589
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900590 card->ext_csd.generic_cmd6_time = 10 *
591 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
Girish K Sbec87262011-10-13 12:04:16 +0530592 card->ext_csd.power_off_longtime = 10 *
593 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900594
Seungwon Jeon336c7162011-10-25 09:43:12 +0900595 card->ext_csd.cache_size =
596 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
597 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
598 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
599 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
Saugata Das42659002011-12-21 13:09:17 +0530600
601 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
602 card->ext_csd.data_sector_size = 4096;
603 else
604 card->ext_csd.data_sector_size = 512;
605
606 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
607 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
608 card->ext_csd.data_tag_unit_size =
609 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
610 (card->ext_csd.data_sector_size);
611 } else {
612 card->ext_csd.data_tag_unit_size = 0;
613 }
Seungwon Jeonabd9ac12013-02-06 17:01:43 +0900614
615 card->ext_csd.max_packed_writes =
616 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
617 card->ext_csd.max_packed_reads =
618 ext_csd[EXT_CSD_MAX_PACKED_READS];
Saugata Dasa5075eb2012-05-17 16:32:21 +0530619 } else {
620 card->ext_csd.data_sector_size = 512;
Seungwon Jeon336c7162011-10-25 09:43:12 +0900621 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900622
Gwendal Grignou0f762422014-10-16 11:27:16 -0700623 /* eMMC v5 or later */
624 if (card->ext_csd.rev >= 7) {
625 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
626 MMC_FIRMWARE_LEN);
627 card->ext_csd.ffu_capable =
628 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
629 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900630
631 card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO];
632 card->ext_csd.device_life_time_est_typ_a =
633 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
634 card->ext_csd.device_life_time_est_typ_b =
635 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
Gwendal Grignou0f762422014-10-16 11:27:16 -0700636 }
Adrian Hunter925ff3a2016-11-29 12:09:16 +0200637
638 /* eMMC v5.1 or later */
639 if (card->ext_csd.rev >= 8) {
640 card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] &
641 EXT_CSD_CMDQ_SUPPORTED;
642 card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] &
643 EXT_CSD_CMDQ_DEPTH_MASK) + 1;
644 /* Exclude inefficiently small queue depths */
645 if (card->ext_csd.cmdq_depth <= 2) {
646 card->ext_csd.cmdq_support = false;
647 card->ext_csd.cmdq_depth = 0;
648 }
649 if (card->ext_csd.cmdq_support) {
650 pr_debug("%s: Command Queue supported depth %u\n",
651 mmc_hostname(card->host),
652 card->ext_csd.cmdq_depth);
653 }
654 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100655out:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700656 return err;
657}
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100658
Ulf Hansson076ec382014-10-20 13:37:24 +0200659static int mmc_read_ext_csd(struct mmc_card *card)
660{
Ulf Hanssonc1977872014-10-20 14:08:16 +0200661 u8 *ext_csd;
Ulf Hansson076ec382014-10-20 13:37:24 +0200662 int err;
663
Ulf Hanssonc1977872014-10-20 14:08:16 +0200664 if (!mmc_can_ext_csd(card))
665 return 0;
666
Ulf Hansson076ec382014-10-20 13:37:24 +0200667 err = mmc_get_ext_csd(card, &ext_csd);
Ulf Hanssonc1977872014-10-20 14:08:16 +0200668 if (err) {
669 /* If the host or the card can't do the switch,
670 * fail more gracefully. */
671 if ((err != -EINVAL)
672 && (err != -ENOSYS)
673 && (err != -EFAULT))
674 return err;
675
676 /*
677 * High capacity cards should have this "magic" size
678 * stored in their CSD.
679 */
680 if (card->csd.capacity == (4096 * 512)) {
681 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
682 mmc_hostname(card->host));
683 } else {
684 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
685 mmc_hostname(card->host));
686 err = 0;
687 }
688
Ulf Hansson076ec382014-10-20 13:37:24 +0200689 return err;
Ulf Hanssonc1977872014-10-20 14:08:16 +0200690 }
Ulf Hansson076ec382014-10-20 13:37:24 +0200691
692 err = mmc_decode_ext_csd(card, ext_csd);
693 kfree(ext_csd);
694 return err;
695}
696
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700697static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700698{
699 u8 *bw_ext_csd;
700 int err;
701
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700702 if (bus_width == MMC_BUS_WIDTH_1)
703 return 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700704
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700705 err = mmc_get_ext_csd(card, &bw_ext_csd);
Ulf Hanssonc1977872014-10-20 14:08:16 +0200706 if (err)
707 return err;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700708
Philip Rakity08ee80c2011-05-24 18:14:58 -0700709 /* only compare read only fields */
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100710 err = !((card->ext_csd.raw_partition_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700711 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700712 (card->ext_csd.raw_erased_mem_count ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700713 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700714 (card->ext_csd.rev ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700715 bw_ext_csd[EXT_CSD_REV]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700716 (card->ext_csd.raw_ext_csd_structure ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700717 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700718 (card->ext_csd.raw_card_type ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700719 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700720 (card->ext_csd.raw_s_a_timeout ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700721 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700722 (card->ext_csd.raw_hc_erase_gap_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700723 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700724 (card->ext_csd.raw_erase_timeout_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700725 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700726 (card->ext_csd.raw_hc_erase_grp_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700727 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700728 (card->ext_csd.raw_sec_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700729 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700730 (card->ext_csd.raw_sec_erase_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700731 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700732 (card->ext_csd.raw_sec_feature_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700733 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700734 (card->ext_csd.raw_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700735 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700736 (card->ext_csd.raw_sectors[0] ==
737 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
738 (card->ext_csd.raw_sectors[1] ==
739 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
740 (card->ext_csd.raw_sectors[2] ==
741 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
742 (card->ext_csd.raw_sectors[3] ==
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200743 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
744 (card->ext_csd.raw_pwr_cl_52_195 ==
745 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
746 (card->ext_csd.raw_pwr_cl_26_195 ==
747 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
748 (card->ext_csd.raw_pwr_cl_52_360 ==
749 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
750 (card->ext_csd.raw_pwr_cl_26_360 ==
751 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
752 (card->ext_csd.raw_pwr_cl_200_195 ==
753 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
754 (card->ext_csd.raw_pwr_cl_200_360 ==
755 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
756 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
757 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
758 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900759 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
760 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
761 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
762
Philip Rakity08ee80c2011-05-24 18:14:58 -0700763 if (err)
764 err = -EINVAL;
765
Ulf Hansson00b41b52014-10-16 16:18:51 +0200766 kfree(bw_ext_csd);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100767 return err;
768}
769
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100770MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
771 card->raw_cid[2], card->raw_cid[3]);
772MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
773 card->raw_csd[2], card->raw_csd[3]);
774MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700775MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
776MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Gwendal Grignou0f762422014-10-16 11:27:16 -0700777MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100778MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
779MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
780MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
781MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800782MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
Jin Qian04fa0542017-10-12 10:46:59 -0700783MMC_DEV_ATTR(rev, "0x%x\n", card->ext_csd.rev);
Bastian Stender80a780a2017-11-28 09:24:06 +0100784MMC_DEV_ATTR(pre_eol_info, "0x%02x\n", card->ext_csd.pre_eol_info);
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900785MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n",
786 card->ext_csd.device_life_time_est_typ_a,
787 card->ext_csd.device_life_time_est_typ_b);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100788MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800789MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
790 card->ext_csd.enhanced_area_offset);
791MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
Loic Pallardy188cc042012-08-06 17:12:29 +0200792MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
793MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
Bastian Stenderc892b0d2017-11-28 09:24:07 +0100794MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
Harish Jenny K N974e85e2018-03-07 10:31:42 +0530795MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
Adrian Hunterb658af72017-03-13 14:36:37 +0200796MMC_DEV_ATTR(cmdq_en, "%d\n", card->ext_csd.cmdq_en);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100797
Gwendal Grignou0f762422014-10-16 11:27:16 -0700798static ssize_t mmc_fwrev_show(struct device *dev,
799 struct device_attribute *attr,
800 char *buf)
801{
802 struct mmc_card *card = mmc_dev_to_card(dev);
803
804 if (card->ext_csd.rev < 7) {
805 return sprintf(buf, "0x%x\n", card->cid.fwrev);
806 } else {
807 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
808 card->ext_csd.fwrev);
809 }
810}
811
812static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
813
Bojan Prtvar6825a602016-07-19 11:16:38 +0200814static ssize_t mmc_dsr_show(struct device *dev,
815 struct device_attribute *attr,
816 char *buf)
817{
818 struct mmc_card *card = mmc_dev_to_card(dev);
819 struct mmc_host *host = card->host;
820
821 if (card->csd.dsr_imp && host->dsr_req)
822 return sprintf(buf, "0x%x\n", host->dsr);
823 else
824 /* return default DSR value */
825 return sprintf(buf, "0x%x\n", 0x404);
826}
827
828static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
829
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100830static struct attribute *mmc_std_attrs[] = {
831 &dev_attr_cid.attr,
832 &dev_attr_csd.attr,
833 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700834 &dev_attr_erase_size.attr,
835 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100836 &dev_attr_fwrev.attr,
Gwendal Grignou0f762422014-10-16 11:27:16 -0700837 &dev_attr_ffu_capable.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100838 &dev_attr_hwrev.attr,
839 &dev_attr_manfid.attr,
840 &dev_attr_name.attr,
841 &dev_attr_oemid.attr,
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800842 &dev_attr_prv.attr,
Jin Qian04fa0542017-10-12 10:46:59 -0700843 &dev_attr_rev.attr,
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900844 &dev_attr_pre_eol_info.attr,
845 &dev_attr_life_time.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100846 &dev_attr_serial.attr,
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800847 &dev_attr_enhanced_area_offset.attr,
848 &dev_attr_enhanced_area_size.attr,
Loic Pallardy188cc042012-08-06 17:12:29 +0200849 &dev_attr_raw_rpmb_size_mult.attr,
850 &dev_attr_rel_sectors.attr,
Bojan Prtvar5fb06af2016-07-04 13:56:55 +0200851 &dev_attr_ocr.attr,
Harish Jenny K N974e85e2018-03-07 10:31:42 +0530852 &dev_attr_rca.attr,
Bojan Prtvar6825a602016-07-19 11:16:38 +0200853 &dev_attr_dsr.attr,
Adrian Hunterb658af72017-03-13 14:36:37 +0200854 &dev_attr_cmdq_en.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100855 NULL,
856};
Axel Lind1e58212014-03-08 15:05:27 +0800857ATTRIBUTE_GROUPS(mmc_std);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100858
859static struct device_type mmc_type = {
Axel Lind1e58212014-03-08 15:05:27 +0800860 .groups = mmc_std_groups,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100861};
862
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100863/*
Girish K Sb87d8db2011-09-23 20:41:47 +0530864 * Select the PowerClass for the current bus width
865 * If power class is defined for 4/8 bit bus in the
866 * extended CSD register, select it by executing the
867 * mmc_switch command.
868 */
Seungwon Jeon23850492014-04-23 17:08:05 +0900869static int __mmc_select_powerclass(struct mmc_card *card,
870 unsigned int bus_width)
Girish K Sb87d8db2011-09-23 20:41:47 +0530871{
Seungwon Jeon23850492014-04-23 17:08:05 +0900872 struct mmc_host *host = card->host;
873 struct mmc_ext_csd *ext_csd = &card->ext_csd;
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200874 unsigned int pwrclass_val = 0;
Seungwon Jeon23850492014-04-23 17:08:05 +0900875 int err = 0;
Girish K Sb87d8db2011-09-23 20:41:47 +0530876
Girish K Sb87d8db2011-09-23 20:41:47 +0530877 switch (1 << host->ios.vdd) {
878 case MMC_VDD_165_195:
Seungwon Jeon23850492014-04-23 17:08:05 +0900879 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
880 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
881 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200882 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
Seungwon Jeon23850492014-04-23 17:08:05 +0900883 ext_csd->raw_pwr_cl_52_195 :
884 ext_csd->raw_pwr_cl_ddr_52_195;
885 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
886 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
Girish K Sb87d8db2011-09-23 20:41:47 +0530887 break;
Subhash Jadavani93fc5a42012-04-03 12:25:58 +0530888 case MMC_VDD_27_28:
889 case MMC_VDD_28_29:
890 case MMC_VDD_29_30:
891 case MMC_VDD_30_31:
892 case MMC_VDD_31_32:
Girish K Sb87d8db2011-09-23 20:41:47 +0530893 case MMC_VDD_32_33:
894 case MMC_VDD_33_34:
895 case MMC_VDD_34_35:
896 case MMC_VDD_35_36:
Seungwon Jeon23850492014-04-23 17:08:05 +0900897 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
898 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
899 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200900 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
Seungwon Jeon23850492014-04-23 17:08:05 +0900901 ext_csd->raw_pwr_cl_52_360 :
902 ext_csd->raw_pwr_cl_ddr_52_360;
903 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900904 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
905 ext_csd->raw_pwr_cl_ddr_200_360 :
906 ext_csd->raw_pwr_cl_200_360;
Girish K Sb87d8db2011-09-23 20:41:47 +0530907 break;
908 default:
Joe Perches66061102014-09-12 14:56:56 -0700909 pr_warn("%s: Voltage range not supported for power class\n",
910 mmc_hostname(host));
Girish K Sb87d8db2011-09-23 20:41:47 +0530911 return -EINVAL;
912 }
913
Girish K Sb87d8db2011-09-23 20:41:47 +0530914 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
915 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
916 EXT_CSD_PWR_CL_8BIT_SHIFT;
917 else
918 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
919 EXT_CSD_PWR_CL_4BIT_SHIFT;
920
921 /* If the power class is different from the default value */
922 if (pwrclass_val > 0) {
923 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
924 EXT_CSD_POWER_CLASS,
925 pwrclass_val,
Seungwon Jeon71fe3eb2011-10-18 13:20:57 +0900926 card->ext_csd.generic_cmd6_time);
Girish K Sb87d8db2011-09-23 20:41:47 +0530927 }
928
929 return err;
930}
931
Seungwon Jeon23850492014-04-23 17:08:05 +0900932static int mmc_select_powerclass(struct mmc_card *card)
933{
934 struct mmc_host *host = card->host;
935 u32 bus_width, ext_csd_bits;
936 int err, ddr;
937
938 /* Power class selection is supported for versions >= 4.0 */
Ulf Hansson148bcab2014-10-20 11:33:53 +0200939 if (!mmc_can_ext_csd(card))
Seungwon Jeon23850492014-04-23 17:08:05 +0900940 return 0;
941
942 bus_width = host->ios.bus_width;
943 /* Power class values are defined only for 4/8 bit bus */
944 if (bus_width == MMC_BUS_WIDTH_1)
945 return 0;
946
947 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
948 if (ddr)
949 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
950 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
951 else
952 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
953 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
954
955 err = __mmc_select_powerclass(card, ext_csd_bits);
956 if (err)
957 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
958 mmc_hostname(host), 1 << bus_width, ddr);
959
960 return err;
961}
962
Girish K Sb87d8db2011-09-23 20:41:47 +0530963/*
Seungwon Jeon577fb132014-04-23 17:08:44 +0900964 * Set the bus speed for the selected speed mode.
Girish K Sa4924c72012-01-11 14:04:52 -0500965 */
Seungwon Jeon577fb132014-04-23 17:08:44 +0900966static void mmc_set_bus_speed(struct mmc_card *card)
Girish K Sa4924c72012-01-11 14:04:52 -0500967{
Seungwon Jeon577fb132014-04-23 17:08:44 +0900968 unsigned int max_dtr = (unsigned int)-1;
969
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900970 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
971 max_dtr > card->ext_csd.hs200_max_dtr)
Seungwon Jeon577fb132014-04-23 17:08:44 +0900972 max_dtr = card->ext_csd.hs200_max_dtr;
973 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
974 max_dtr = card->ext_csd.hs_max_dtr;
975 else if (max_dtr > card->csd.max_dtr)
976 max_dtr = card->csd.max_dtr;
977
978 mmc_set_clock(card->host, max_dtr);
979}
980
981/*
982 * Select the bus width amoung 4-bit and 8-bit(SDR).
983 * If the bus width is changed successfully, return the selected width value.
984 * Zero is returned instead of error value if the wide width is not supported.
985 */
986static int mmc_select_bus_width(struct mmc_card *card)
987{
Girish K Sa4924c72012-01-11 14:04:52 -0500988 static unsigned ext_csd_bits[] = {
Girish K Sa4924c72012-01-11 14:04:52 -0500989 EXT_CSD_BUS_WIDTH_8,
Seungwon Jeon577fb132014-04-23 17:08:44 +0900990 EXT_CSD_BUS_WIDTH_4,
Girish K Sa4924c72012-01-11 14:04:52 -0500991 };
992 static unsigned bus_widths[] = {
Girish K Sa4924c72012-01-11 14:04:52 -0500993 MMC_BUS_WIDTH_8,
Seungwon Jeon577fb132014-04-23 17:08:44 +0900994 MMC_BUS_WIDTH_4,
Girish K Sa4924c72012-01-11 14:04:52 -0500995 };
Seungwon Jeon577fb132014-04-23 17:08:44 +0900996 struct mmc_host *host = card->host;
997 unsigned idx, bus_width = 0;
998 int err = 0;
Girish K Sa4924c72012-01-11 14:04:52 -0500999
Alexandre Belloni1c2d26e2014-12-17 19:32:06 +01001000 if (!mmc_can_ext_csd(card) ||
Seungwon Jeon577fb132014-04-23 17:08:44 +09001001 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
1002 return 0;
Girish K Sa4924c72012-01-11 14:04:52 -05001003
Seungwon Jeon577fb132014-04-23 17:08:44 +09001004 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
Girish K Sa4924c72012-01-11 14:04:52 -05001005
1006 /*
1007 * Unlike SD, MMC cards dont have a configuration register to notify
1008 * supported bus width. So bus test command should be run to identify
1009 * the supported bus width or compare the ext csd values of current
1010 * bus width and ext csd values of 1 bit mode read earlier.
1011 */
Seungwon Jeon577fb132014-04-23 17:08:44 +09001012 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
Girish K Sa4924c72012-01-11 14:04:52 -05001013 /*
1014 * Host is capable of 8bit transfer, then switch
1015 * the device to work in 8bit transfer mode. If the
1016 * mmc switch command returns error then switch to
1017 * 4bit transfer mode. On success set the corresponding
1018 * bus width on the host.
1019 */
1020 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1021 EXT_CSD_BUS_WIDTH,
1022 ext_csd_bits[idx],
1023 card->ext_csd.generic_cmd6_time);
1024 if (err)
1025 continue;
1026
Seungwon Jeon577fb132014-04-23 17:08:44 +09001027 bus_width = bus_widths[idx];
1028 mmc_set_bus_width(host, bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -05001029
Seungwon Jeon577fb132014-04-23 17:08:44 +09001030 /*
1031 * If controller can't handle bus width test,
1032 * compare ext_csd previously read in 1 bit mode
1033 * against ext_csd at new bus width
1034 */
Girish K Sa4924c72012-01-11 14:04:52 -05001035 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
Seungwon Jeon577fb132014-04-23 17:08:44 +09001036 err = mmc_compare_ext_csds(card, bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -05001037 else
Seungwon Jeon577fb132014-04-23 17:08:44 +09001038 err = mmc_bus_test(card, bus_width);
1039
1040 if (!err) {
1041 err = bus_width;
Girish K Sa4924c72012-01-11 14:04:52 -05001042 break;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001043 } else {
1044 pr_warn("%s: switch to bus width %d failed\n",
Wolfram Sanged9feec2016-01-29 09:27:50 +01001045 mmc_hostname(host), 1 << bus_width);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001046 }
Girish K Sa4924c72012-01-11 14:04:52 -05001047 }
1048
Seungwon Jeon577fb132014-04-23 17:08:44 +09001049 return err;
1050}
1051
1052/*
1053 * Switch to the high-speed mode
1054 */
1055static int mmc_select_hs(struct mmc_card *card)
1056{
1057 int err;
1058
1059 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1060 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
Ulf Hansson53e60652016-11-09 19:40:29 +01001061 card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
1062 true, true, true);
Jungseung Lee67d35962016-08-24 19:34:09 +09001063 if (err)
1064 pr_warn("%s: switch to high-speed failed, err:%d\n",
1065 mmc_hostname(card->host), err);
1066
Seungwon Jeon577fb132014-04-23 17:08:44 +09001067 return err;
1068}
1069
1070/*
1071 * Activate wide bus and DDR if supported.
1072 */
1073static int mmc_select_hs_ddr(struct mmc_card *card)
1074{
1075 struct mmc_host *host = card->host;
1076 u32 bus_width, ext_csd_bits;
1077 int err = 0;
1078
1079 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1080 return 0;
1081
1082 bus_width = host->ios.bus_width;
1083 if (bus_width == MMC_BUS_WIDTH_1)
1084 return 0;
1085
1086 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1087 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1088
Ulf Hanssone173f892016-11-09 21:00:26 +01001089 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1090 EXT_CSD_BUS_WIDTH,
1091 ext_csd_bits,
1092 card->ext_csd.generic_cmd6_time,
1093 MMC_TIMING_MMC_DDR52,
1094 true, true, true);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001095 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001096 pr_err("%s: switch to bus width %d ddr failed\n",
Seungwon Jeon577fb132014-04-23 17:08:44 +09001097 mmc_hostname(host), 1 << bus_width);
1098 return err;
1099 }
1100
1101 /*
1102 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1103 * signaling.
1104 *
1105 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1106 *
1107 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1108 * in the JEDEC spec for DDR.
1109 *
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001110 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1111 * host controller can support this, like some of the SDHCI
1112 * controller which connect to an eMMC device. Some of these
1113 * host controller still needs to use 1.8v vccq for supporting
1114 * DDR mode.
1115 *
1116 * So the sequence will be:
1117 * if (host and device can both support 1.2v IO)
1118 * use 1.2v IO;
1119 * else if (host and device can both support 1.8v IO)
1120 * use 1.8v IO;
1121 * so if host and device can only support 3.3v IO, this is the
1122 * last choice.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001123 *
1124 * WARNING: eMMC rules are NOT the same as SD DDR
1125 */
Ulf Hansson20f921b2017-01-25 12:04:17 +01001126 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001127 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
Ulf Hansson20f921b2017-01-25 12:04:17 +01001128 if (!err)
1129 return 0;
1130 }
Seungwon Jeon577fb132014-04-23 17:08:44 +09001131
Ulf Hansson20f921b2017-01-25 12:04:17 +01001132 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V &&
1133 host->caps & MMC_CAP_1_8V_DDR)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001134 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001135
1136 /* make sure vccq is 3.3v after switching disaster */
1137 if (err)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001138 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001139
Seungwon Jeon577fb132014-04-23 17:08:44 +09001140 return err;
1141}
1142
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001143static int mmc_select_hs400(struct mmc_card *card)
1144{
1145 struct mmc_host *host = card->host;
Adrian Hunter51b12f72015-10-28 14:25:41 +02001146 unsigned int max_dtr;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001147 int err = 0;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001148 u8 val;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001149
1150 /*
1151 * HS400 mode requires 8-bit bus width
1152 */
1153 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1154 host->ios.bus_width == MMC_BUS_WIDTH_8))
1155 return 0;
1156
Adrian Hunter51b12f72015-10-28 14:25:41 +02001157 /* Switch card to HS mode */
Wenkai Duadb24d42015-11-26 14:00:44 +02001158 val = EXT_CSD_TIMING_HS;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001159 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001160 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001161 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001162 true, false, true);
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001163 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001164 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001165 mmc_hostname(host), err);
1166 return err;
1167 }
1168
Adrian Hunter51b12f72015-10-28 14:25:41 +02001169 /* Set host controller to HS timing */
1170 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1171
Simon Hormanba6c7ac2018-06-18 14:57:49 +02001172 /* Prepare host to downgrade to HS timing */
1173 if (host->ops->hs400_downgrade)
1174 host->ops->hs400_downgrade(host);
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
Simon Hormanba6c7ac2018-06-18 14:57:49 +02001216 if (host->ops->hs400_complete)
1217 host->ops->hs400_complete(host);
1218
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001219 return 0;
Adrian Hunterd2302932015-10-28 14:25:43 +02001220
1221out_err:
1222 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1223 __func__, err);
1224 return err;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001225}
1226
Adrian Hunter6376f692015-05-07 13:10:20 +03001227int mmc_hs200_to_hs400(struct mmc_card *card)
1228{
1229 return mmc_select_hs400(card);
1230}
1231
Adrian Hunter6376f692015-05-07 13:10:20 +03001232int mmc_hs400_to_hs200(struct mmc_card *card)
1233{
1234 struct mmc_host *host = card->host;
Adrian Hunter6376f692015-05-07 13:10:20 +03001235 unsigned int max_dtr;
1236 int err;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001237 u8 val;
Adrian Hunter6376f692015-05-07 13:10:20 +03001238
Adrian Hunter6376f692015-05-07 13:10:20 +03001239 /* Reduce frequency to HS */
1240 max_dtr = card->ext_csd.hs_max_dtr;
1241 mmc_set_clock(host, max_dtr);
1242
1243 /* Switch HS400 to HS DDR */
Wenkai Duadb24d42015-11-26 14:00:44 +02001244 val = EXT_CSD_TIMING_HS;
Adrian Hunter6376f692015-05-07 13:10:20 +03001245 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001246 val, card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001247 true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001248 if (err)
1249 goto out_err;
1250
1251 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1252
Chaotian Jing08573ea2016-05-19 16:47:41 +08001253 err = mmc_switch_status(card);
1254 if (err)
1255 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001256
1257 /* Switch HS DDR to HS */
1258 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1259 EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001260 0, true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001261 if (err)
1262 goto out_err;
1263
1264 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1265
Simon Hormanba6c7ac2018-06-18 14:57:49 +02001266 if (host->ops->hs400_downgrade)
1267 host->ops->hs400_downgrade(host);
1268
Chaotian Jing08573ea2016-05-19 16:47:41 +08001269 err = mmc_switch_status(card);
1270 if (err)
1271 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001272
1273 /* Switch HS to HS200 */
Adrian Huntercc4f4142015-02-06 14:12:58 +02001274 val = EXT_CSD_TIMING_HS200 |
1275 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Adrian Hunter6376f692015-05-07 13:10:20 +03001276 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001277 val, card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001278 true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001279 if (err)
1280 goto out_err;
1281
1282 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1283
Adrian Hunteref3d2322016-12-02 13:16:35 +02001284 /*
1285 * For HS200, CRC errors are not a reliable way to know the switch
1286 * failed. If there really is a problem, we would expect tuning will
1287 * fail and the result ends up the same.
1288 */
1289 err = __mmc_switch_status(card, false);
Chaotian Jing08573ea2016-05-19 16:47:41 +08001290 if (err)
1291 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001292
1293 mmc_set_bus_speed(card);
1294
Ulf Hanssonc8a4e302018-05-22 16:26:26 +02001295 /* Prepare tuning for HS400 mode. */
1296 if (host->ops->prepare_hs400_tuning)
1297 host->ops->prepare_hs400_tuning(host, &host->ios);
1298
Adrian Hunter6376f692015-05-07 13:10:20 +03001299 return 0;
1300
1301out_err:
1302 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1303 __func__, err);
1304 return err;
1305}
1306
Chanho Minfb458862017-09-26 09:03:40 +09001307static void mmc_select_driver_type(struct mmc_card *card)
1308{
Wolfram Sangbe17f1c2017-11-30 15:49:10 +01001309 int card_drv_type, drive_strength, drv_type = 0;
Wolfram Sang6186d062017-10-15 14:46:14 +02001310 int fixed_drv_type = card->host->fixed_drv_type;
Chanho Minfb458862017-09-26 09:03:40 +09001311
1312 card_drv_type = card->ext_csd.raw_driver_strength |
1313 mmc_driver_type_mask(0);
1314
Wolfram Sang6186d062017-10-15 14:46:14 +02001315 if (fixed_drv_type >= 0)
1316 drive_strength = card_drv_type & mmc_driver_type_mask(fixed_drv_type)
1317 ? fixed_drv_type : 0;
1318 else
1319 drive_strength = mmc_select_drive_strength(card,
1320 card->ext_csd.hs200_max_dtr,
1321 card_drv_type, &drv_type);
Chanho Minfb458862017-09-26 09:03:40 +09001322
1323 card->drive_strength = drive_strength;
1324
1325 if (drv_type)
1326 mmc_set_driver_type(card->host, drv_type);
1327}
1328
Shawn Lin81ac2af2016-05-26 09:56:22 +08001329static int mmc_select_hs400es(struct mmc_card *card)
1330{
1331 struct mmc_host *host = card->host;
Haibo Chen92ddd952017-08-08 18:54:01 +08001332 int err = -EINVAL;
Shawn Lin81ac2af2016-05-26 09:56:22 +08001333 u8 val;
1334
1335 if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1336 err = -ENOTSUPP;
1337 goto out_err;
1338 }
1339
Shawn Lin1720d352016-09-30 14:18:58 +08001340 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001341 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
Shawn Lin1720d352016-09-30 14:18:58 +08001342
1343 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001344 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
Shawn Lin1720d352016-09-30 14:18:58 +08001345
1346 /* If fails try again during next card power cycle */
1347 if (err)
1348 goto out_err;
1349
Shawn Lin81ac2af2016-05-26 09:56:22 +08001350 err = mmc_select_bus_width(card);
Hongjie Fang7f38abf2018-07-31 10:55:09 +08001351 if (err != MMC_BUS_WIDTH_8) {
1352 pr_err("%s: switch to 8bit bus width failed, err:%d\n",
1353 mmc_hostname(host), err);
1354 err = err < 0 ? err : -ENOTSUPP;
Shawn Lin81ac2af2016-05-26 09:56:22 +08001355 goto out_err;
Hongjie Fang7f38abf2018-07-31 10:55:09 +08001356 }
Shawn Lin81ac2af2016-05-26 09:56:22 +08001357
1358 /* Switch card to HS mode */
Ulf Hansson53e60652016-11-09 19:40:29 +01001359 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1360 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1361 card->ext_csd.generic_cmd6_time, 0,
1362 true, false, true);
1363 if (err) {
1364 pr_err("%s: switch to hs for hs400es failed, err:%d\n",
1365 mmc_hostname(host), err);
1366 goto out_err;
1367 }
1368
1369 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1370 err = mmc_switch_status(card);
Jungseung Lee67d35962016-08-24 19:34:09 +09001371 if (err)
Shawn Lin81ac2af2016-05-26 09:56:22 +08001372 goto out_err;
Shawn Lin81ac2af2016-05-26 09:56:22 +08001373
Shawn Lin4f255802016-09-30 14:18:59 +08001374 mmc_set_clock(host, card->ext_csd.hs_max_dtr);
1375
Shawn Lin81ac2af2016-05-26 09:56:22 +08001376 /* Switch card to DDR with strobe bit */
1377 val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1378 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1379 EXT_CSD_BUS_WIDTH,
1380 val,
1381 card->ext_csd.generic_cmd6_time);
1382 if (err) {
1383 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1384 mmc_hostname(host), err);
1385 goto out_err;
1386 }
1387
Chanho Minfb458862017-09-26 09:03:40 +09001388 mmc_select_driver_type(card);
1389
Shawn Lin81ac2af2016-05-26 09:56:22 +08001390 /* Switch card to HS400 */
1391 val = EXT_CSD_TIMING_HS400 |
1392 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1393 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1394 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001395 card->ext_csd.generic_cmd6_time, 0,
Shawn Lin81ac2af2016-05-26 09:56:22 +08001396 true, false, true);
1397 if (err) {
1398 pr_err("%s: switch to hs400es failed, err:%d\n",
1399 mmc_hostname(host), err);
1400 goto out_err;
1401 }
1402
1403 /* Set host controller to HS400 timing and frequency */
1404 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1405
1406 /* Controller enable enhanced strobe function */
1407 host->ios.enhanced_strobe = true;
1408 if (host->ops->hs400_enhanced_strobe)
1409 host->ops->hs400_enhanced_strobe(host, &host->ios);
1410
1411 err = mmc_switch_status(card);
1412 if (err)
1413 goto out_err;
1414
1415 return 0;
1416
1417out_err:
1418 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1419 __func__, err);
1420 return err;
1421}
1422
Seungwon Jeon577fb132014-04-23 17:08:44 +09001423/*
1424 * For device supporting HS200 mode, the following sequence
1425 * should be done before executing the tuning process.
1426 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1427 * 2. switch to HS200 mode
1428 * 3. set the clock to > 52Mhz and <=200MHz
1429 */
1430static int mmc_select_hs200(struct mmc_card *card)
1431{
1432 struct mmc_host *host = card->host;
Dong Aishenge51534c2016-04-21 00:51:30 +08001433 unsigned int old_timing, old_signal_voltage;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001434 int err = -EINVAL;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001435 u8 val;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001436
Dong Aishenge51534c2016-04-21 00:51:30 +08001437 old_signal_voltage = host->ios.signal_voltage;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001438 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001439 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001440
1441 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001442 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001443
1444 /* If fails try again during next card power cycle */
1445 if (err)
Dong Aishenge51534c2016-04-21 00:51:30 +08001446 return err;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001447
Adrian Huntercc4f4142015-02-06 14:12:58 +02001448 mmc_select_driver_type(card);
1449
Seungwon Jeon577fb132014-04-23 17:08:44 +09001450 /*
1451 * Set the bus width(4 or 8) with host's support and
1452 * switch to HS200 mode if bus width is set successfully.
1453 */
1454 err = mmc_select_bus_width(card);
Peter Griffin8b7be8f2016-06-08 11:27:52 +01001455 if (err > 0) {
Adrian Huntercc4f4142015-02-06 14:12:58 +02001456 val = EXT_CSD_TIMING_HS200 |
1457 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Ulf Hansson4509f84772014-01-08 16:09:33 +01001458 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001459 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001460 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001461 true, false, true);
Adrian Hunter1815e612015-10-28 14:25:40 +02001462 if (err)
1463 goto err;
1464 old_timing = host->ios.timing;
1465 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
Chaotian Jing08573ea2016-05-19 16:47:41 +08001466
Adrian Hunteref3d2322016-12-02 13:16:35 +02001467 /*
1468 * For HS200, CRC errors are not a reliable way to know the
1469 * switch failed. If there really is a problem, we would expect
1470 * tuning will fail and the result ends up the same.
1471 */
1472 err = __mmc_switch_status(card, false);
1473
Chaotian Jing08573ea2016-05-19 16:47:41 +08001474 /*
1475 * mmc_select_timing() assumes timing has not changed if
1476 * it is a switch error.
1477 */
1478 if (err == -EBADMSG)
1479 mmc_set_timing(host, old_timing);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001480 }
Girish K Sa4924c72012-01-11 14:04:52 -05001481err:
Dong Aishenge51534c2016-04-21 00:51:30 +08001482 if (err) {
1483 /* fall back to the old signal voltage, if fails report error */
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001484 if (mmc_set_signal_voltage(host, old_signal_voltage))
Dong Aishenge51534c2016-04-21 00:51:30 +08001485 err = -EIO;
1486
Adrian Hunter1815e612015-10-28 14:25:40 +02001487 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1488 __func__, err);
Dong Aishenge51534c2016-04-21 00:51:30 +08001489 }
Girish K Sa4924c72012-01-11 14:04:52 -05001490 return err;
1491}
1492
1493/*
Shawn Lin81ac2af2016-05-26 09:56:22 +08001494 * Activate High Speed, HS200 or HS400ES mode if supported.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001495 */
1496static int mmc_select_timing(struct mmc_card *card)
1497{
1498 int err = 0;
1499
Ulf Hansson148bcab2014-10-20 11:33:53 +02001500 if (!mmc_can_ext_csd(card))
Seungwon Jeon577fb132014-04-23 17:08:44 +09001501 goto bus_speed;
1502
Shawn Lin81ac2af2016-05-26 09:56:22 +08001503 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1504 err = mmc_select_hs400es(card);
1505 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
Seungwon Jeon577fb132014-04-23 17:08:44 +09001506 err = mmc_select_hs200(card);
1507 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1508 err = mmc_select_hs(card);
1509
1510 if (err && err != -EBADMSG)
1511 return err;
1512
Seungwon Jeon577fb132014-04-23 17:08:44 +09001513bus_speed:
1514 /*
1515 * Set the bus speed to the selected bus timing.
1516 * If timing is not selected, backward compatible is the default.
1517 */
1518 mmc_set_bus_speed(card);
Dong Aisheng0400ed02016-04-21 00:51:31 +08001519 return 0;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001520}
1521
1522/*
1523 * Execute tuning sequence to seek the proper bus operating
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001524 * conditions for HS200 and HS400, which sends CMD21 to the device.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001525 */
1526static int mmc_hs200_tuning(struct mmc_card *card)
1527{
1528 struct mmc_host *host = card->host;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001529
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001530 /*
1531 * Timing should be adjusted to the HS400 target
1532 * operation frequency for tuning process
1533 */
1534 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1535 host->ios.bus_width == MMC_BUS_WIDTH_8)
1536 if (host->ops->prepare_hs400_tuning)
1537 host->ops->prepare_hs400_tuning(host, &host->ios);
1538
Adrian Hunter63e415c2014-12-05 19:40:59 +02001539 return mmc_execute_tuning(card);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001540}
1541
1542/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001543 * Handle the detection and initialisation of a card.
1544 *
Deepak Saxena87693922008-06-16 19:20:57 -07001545 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001546 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001547 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001548static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001549 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001550{
1551 struct mmc_card *card;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001552 int err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001553 u32 cid[4];
Philip Rakityb676f032011-02-13 23:13:09 -08001554 u32 rocr;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001555
Pierre Ossmand84075c82007-08-09 13:23:56 +02001556 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001557
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001558 /* Set correct bus mode for MMC before attempting init */
1559 if (!mmc_host_is_spi(host))
1560 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1561
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001562 /*
1563 * Since we're changing the OCR value, we seem to
1564 * need to tell some cards to go back to the idle
1565 * state. We wait 1ms to give cards time to
1566 * respond.
Balaji T Kc3805462011-09-08 22:08:39 +05301567 * mmc_go_idle is needed for eMMC that are asleep
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001568 */
1569 mmc_go_idle(host);
1570
1571 /* The extra bit indicates that we support high capacity */
Philip Rakityb676f032011-02-13 23:13:09 -08001572 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +02001573 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001574 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001575
1576 /*
David Brownellaf517152007-08-08 09:11:32 -07001577 * For SPI, enable CRC as appropriate.
1578 */
1579 if (mmc_host_is_spi(host)) {
1580 err = mmc_spi_set_crc(host, use_spi_crc);
1581 if (err)
1582 goto err;
1583 }
1584
1585 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001586 * Fetch CID from card.
1587 */
Ulf Hanssona1473732017-06-08 15:27:43 +02001588 err = mmc_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +02001589 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001590 goto err;
1591
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001592 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001593 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1594 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001595 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001596 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001597
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001598 card = oldcard;
1599 } else {
1600 /*
1601 * Allocate card structure.
1602 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +01001603 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001604 if (IS_ERR(card)) {
1605 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001606 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001607 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001608
Ulf Hansson69041152013-09-13 11:31:33 +02001609 card->ocr = ocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001610 card->type = MMC_TYPE_MMC;
1611 card->rca = 1;
1612 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1613 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001614
1615 /*
Doug Andersoneac86322014-12-02 15:42:45 -08001616 * Call the optional HC's init_card function to handle quirks.
1617 */
1618 if (host->ops->init_card)
1619 host->ops->init_card(host, card);
1620
1621 /*
David Brownellaf517152007-08-08 09:11:32 -07001622 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001623 */
David Brownellaf517152007-08-08 09:11:32 -07001624 if (!mmc_host_is_spi(host)) {
1625 err = mmc_set_relative_addr(card);
1626 if (err)
1627 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001628
David Brownellaf517152007-08-08 09:11:32 -07001629 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1630 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001631
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001632 if (!oldcard) {
1633 /*
1634 * Fetch CSD from card.
1635 */
1636 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001637 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001638 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001639
Pierre Ossmanbd766312007-05-01 16:11:57 +02001640 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001641 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001642 goto free_card;
1643 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001644 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001645 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001646 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001647
1648 /*
Sascha Hauer3d705d12014-08-19 10:45:51 +02001649 * handling only for cards supporting DSR and hosts requesting
1650 * DSR configuration
1651 */
1652 if (card->csd.dsr_imp && host->dsr_req)
1653 mmc_set_dsr(host);
1654
1655 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001656 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001657 */
David Brownellaf517152007-08-08 09:11:32 -07001658 if (!mmc_host_is_spi(host)) {
1659 err = mmc_select_card(card);
1660 if (err)
1661 goto free_card;
1662 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001663
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001664 if (!oldcard) {
Ulf Hansson076ec382014-10-20 13:37:24 +02001665 /* Read extended CSD. */
1666 err = mmc_read_ext_csd(card);
Pierre Ossman17b04292007-07-22 22:18:46 +02001667 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001668 goto free_card;
Philip Rakityb676f032011-02-13 23:13:09 -08001669
Masahiro Yamada87e88652016-04-15 20:16:12 +09001670 /*
1671 * If doing byte addressing, check if required to do sector
Philip Rakityb676f032011-02-13 23:13:09 -08001672 * addressing. Handle the case of <2GB cards needing sector
1673 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1674 * ocr register has bit 30 set for sector addressing.
1675 */
Masahiro Yamada87e88652016-04-15 20:16:12 +09001676 if (rocr & BIT(30))
Philip Rakityb676f032011-02-13 23:13:09 -08001677 mmc_card_set_blockaddr(card);
1678
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001679 /* Erase size depends on CSD and Extended CSD */
1680 mmc_set_erase_size(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001681 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001682
Ulf Hanssond2a47172017-06-08 15:23:08 +02001683 /* Enable ERASE_GRP_DEF. This bit is lost after a reset or power off. */
1684 if (card->ext_csd.rev >= 3) {
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001685 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001686 EXT_CSD_ERASE_GROUP_DEF, 1,
1687 card->ext_csd.generic_cmd6_time);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001688
1689 if (err && err != -EBADMSG)
1690 goto free_card;
1691
1692 if (err) {
1693 err = 0;
1694 /*
1695 * Just disable enhanced area off & sz
1696 * will try to enable ERASE_GROUP_DEF
1697 * during next time reinit
1698 */
1699 card->ext_csd.enhanced_area_offset = -EINVAL;
1700 card->ext_csd.enhanced_area_size = -EINVAL;
1701 } else {
1702 card->ext_csd.erase_group_def = 1;
1703 /*
1704 * enable ERASE_GRP_DEF successfully.
1705 * This will affect the erase size, so
1706 * here need to reset erase size
1707 */
1708 mmc_set_erase_size(card);
1709 }
1710 }
1711
1712 /*
Philip Rakity41e2a482011-03-19 14:10:33 -04001713 * Ensure eMMC user default partition is enabled
1714 */
Andrei Warkentin371a6892011-04-11 18:10:25 -05001715 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1716 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1717 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1718 card->ext_csd.part_config,
1719 card->ext_csd.part_time);
1720 if (err && err != -EBADMSG)
1721 goto free_card;
Philip Rakity41e2a482011-03-19 14:10:33 -04001722 }
1723
1724 /*
Ulf Hansson432356792013-06-10 17:03:45 +02001725 * Enable power_off_notification byte in the ext_csd register
Girish K Sbec87262011-10-13 12:04:16 +05301726 */
Ulf Hansson432356792013-06-10 17:03:45 +02001727 if (card->ext_csd.rev >= 6) {
Girish K Sbec87262011-10-13 12:04:16 +05301728 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1729 EXT_CSD_POWER_OFF_NOTIFICATION,
1730 EXT_CSD_POWER_ON,
1731 card->ext_csd.generic_cmd6_time);
1732 if (err && err != -EBADMSG)
1733 goto free_card;
Girish K Sbec87262011-10-13 12:04:16 +05301734
Girish K S96a85d52011-11-04 16:22:47 +05301735 /*
1736 * The err can be -EBADMSG or 0,
1737 * so check for success and update the flag
1738 */
1739 if (!err)
Ulf Hanssone6c08582012-10-05 12:45:39 -04001740 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
Girish K S96a85d52011-11-04 16:22:47 +05301741 }
Girish K Sbec87262011-10-13 12:04:16 +05301742
1743 /*
Seungwon Jeon577fb132014-04-23 17:08:44 +09001744 * Select timing interface
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001745 */
Seungwon Jeon577fb132014-04-23 17:08:44 +09001746 err = mmc_select_timing(card);
1747 if (err)
1748 goto free_card;
Girish K Sa4924c72012-01-11 14:04:52 -05001749
Girish K Sa4924c72012-01-11 14:04:52 -05001750 if (mmc_card_hs200(card)) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001751 err = mmc_hs200_tuning(card);
1752 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001753 goto free_card;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001754
1755 err = mmc_select_hs400(card);
1756 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001757 goto free_card;
Guenter Roeck773dc112017-03-01 14:11:47 -08001758 } else if (!mmc_card_hs400es(card)) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001759 /* Select the desired bus width optionally */
1760 err = mmc_select_bus_width(card);
Anssi Hannula3d4ef322017-02-13 13:46:41 +02001761 if (err > 0 && mmc_card_hs(card)) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001762 err = mmc_select_hs_ddr(card);
1763 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001764 goto free_card;
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001765 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001766 }
1767
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001768 /*
Seungwon Jeon23850492014-04-23 17:08:05 +09001769 * Choose the power class with selected bus interface
1770 */
1771 mmc_select_powerclass(card);
1772
1773 /*
Subhash Jadavani52d09742012-03-06 17:59:12 +05301774 * Enable HPI feature (if supported)
1775 */
1776 if (card->ext_csd.hpi) {
1777 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1778 EXT_CSD_HPI_MGMT, 1,
1779 card->ext_csd.generic_cmd6_time);
1780 if (err && err != -EBADMSG)
1781 goto free_card;
1782 if (err) {
Joe Perches66061102014-09-12 14:56:56 -07001783 pr_warn("%s: Enabling HPI failed\n",
1784 mmc_hostname(card->host));
Ulf Hanssona0741ba2018-12-10 17:52:36 +01001785 card->ext_csd.hpi_en = 0;
Subhash Jadavani52d09742012-03-06 17:59:12 +05301786 err = 0;
Ulf Hanssona0741ba2018-12-10 17:52:36 +01001787 } else {
Subhash Jadavani52d09742012-03-06 17:59:12 +05301788 card->ext_csd.hpi_en = 1;
Ulf Hanssona0741ba2018-12-10 17:52:36 +01001789 }
Subhash Jadavani52d09742012-03-06 17:59:12 +05301790 }
1791
1792 /*
Ulf Hanssone3ae3402018-12-10 17:52:38 +01001793 * If cache size is higher than 0, this indicates the existence of cache
1794 * and it can be turned on. Note that some eMMCs from Micron has been
1795 * reported to need ~800 ms timeout, while enabling the cache after
1796 * sudden power failure tests. Let's extend the timeout to a minimum of
1797 * DEFAULT_CACHE_EN_TIMEOUT_MS and do it for all cards.
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001798 */
Ulf Hanssonba9f39a2018-12-10 17:52:37 +01001799 if (card->ext_csd.cache_size > 0) {
Ulf Hanssone3ae3402018-12-10 17:52:38 +01001800 unsigned int timeout_ms = MIN_CACHE_EN_TIMEOUT_MS;
1801
1802 timeout_ms = max(card->ext_csd.generic_cmd6_time, timeout_ms);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001803 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Ulf Hanssone3ae3402018-12-10 17:52:38 +01001804 EXT_CSD_CACHE_CTRL, 1, timeout_ms);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001805 if (err && err != -EBADMSG)
1806 goto free_card;
1807
1808 /*
1809 * Only if no error, cache is turned on successfully.
1810 */
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001811 if (err) {
Joe Perches66061102014-09-12 14:56:56 -07001812 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1813 mmc_hostname(card->host), err);
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001814 card->ext_csd.cache_ctrl = 0;
1815 err = 0;
1816 } else {
1817 card->ext_csd.cache_ctrl = 1;
1818 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001819 }
1820
Seungwon Jeonabd9ac12013-02-06 17:01:43 +09001821 /*
Adrian Hunter98d4f782017-09-22 15:36:53 +03001822 * Enable Command Queue if supported. Note that Packed Commands cannot
1823 * be used with Command Queue.
1824 */
1825 card->ext_csd.cmdq_en = false;
1826 if (card->ext_csd.cmdq_support && host->caps2 & MMC_CAP2_CQE) {
1827 err = mmc_cmdq_enable(card);
1828 if (err && err != -EBADMSG)
1829 goto free_card;
1830 if (err) {
1831 pr_warn("%s: Enabling CMDQ failed\n",
1832 mmc_hostname(card->host));
1833 card->ext_csd.cmdq_support = false;
1834 card->ext_csd.cmdq_depth = 0;
1835 err = 0;
1836 }
1837 }
1838 /*
Adrian Hunter9d4579a2017-03-13 14:36:38 +02001839 * In some cases (e.g. RPMB or mmc_test), the Command Queue must be
1840 * disabled for a time, so a flag is needed to indicate to re-enable the
1841 * Command Queue.
1842 */
1843 card->reenable_cmdq = card->ext_csd.cmdq_en;
1844
Adrian Hunterf690f442017-09-22 15:36:54 +03001845 if (card->ext_csd.cmdq_en && !host->cqe_enabled) {
1846 err = host->cqe_ops->cqe_enable(host, card);
1847 if (err) {
1848 pr_err("%s: Failed to enable CQE, error %d\n",
1849 mmc_hostname(host), err);
1850 } else {
1851 host->cqe_enabled = true;
1852 pr_info("%s: Command Queue Engine enabled\n",
1853 mmc_hostname(host));
1854 }
1855 }
1856
Kyle Roeschley247cfe52018-04-13 16:54:57 -05001857 if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
1858 host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1859 pr_err("%s: Host failed to negotiate down from 3.3V\n",
1860 mmc_hostname(host));
1861 err = -EINVAL;
1862 goto free_card;
1863 }
1864
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001865 if (!oldcard)
1866 host->card = card;
1867
Pierre Ossman17b04292007-07-22 22:18:46 +02001868 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001869
1870free_card:
1871 if (!oldcard)
1872 mmc_remove_card(card);
1873err:
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001874 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001875}
1876
Ulf Hansson07a68212013-04-19 15:12:11 +02001877static int mmc_can_sleep(struct mmc_card *card)
1878{
1879 return (card && card->ext_csd.rev >= 3);
1880}
1881
1882static int mmc_sleep(struct mmc_host *host)
1883{
Masahiro Yamadac7836d12016-12-19 20:51:18 +09001884 struct mmc_command cmd = {};
Ulf Hansson07a68212013-04-19 15:12:11 +02001885 struct mmc_card *card = host->card;
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001886 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
Ulf Hansson07a68212013-04-19 15:12:11 +02001887 int err;
1888
Adrian Hunter436f8da2015-05-07 13:10:18 +03001889 /* Re-tuning can't be done once the card is deselected */
1890 mmc_retune_hold(host);
1891
Ulf Hansson07a68212013-04-19 15:12:11 +02001892 err = mmc_deselect_cards(host);
1893 if (err)
Adrian Hunter436f8da2015-05-07 13:10:18 +03001894 goto out_release;
Ulf Hansson07a68212013-04-19 15:12:11 +02001895
1896 cmd.opcode = MMC_SLEEP_AWAKE;
1897 cmd.arg = card->rca << 16;
1898 cmd.arg |= 1 << 15;
1899
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001900 /*
1901 * If the max_busy_timeout of the host is specified, validate it against
1902 * the sleep cmd timeout. A failure means we need to prevent the host
1903 * from doing hw busy detection, which is done by converting to a R1
1904 * response instead of a R1B.
1905 */
1906 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1907 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1908 } else {
1909 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1910 cmd.busy_timeout = timeout_ms;
1911 }
1912
Ulf Hansson07a68212013-04-19 15:12:11 +02001913 err = mmc_wait_for_cmd(host, &cmd, 0);
1914 if (err)
Adrian Hunter436f8da2015-05-07 13:10:18 +03001915 goto out_release;
Ulf Hansson07a68212013-04-19 15:12:11 +02001916
1917 /*
1918 * If the host does not wait while the card signals busy, then we will
1919 * will have to wait the sleep/awake timeout. Note, we cannot use the
1920 * SEND_STATUS command to poll the status because that command (and most
1921 * others) is invalid while the card sleeps.
1922 */
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001923 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1924 mmc_delay(timeout_ms);
Ulf Hansson07a68212013-04-19 15:12:11 +02001925
Adrian Hunter436f8da2015-05-07 13:10:18 +03001926out_release:
1927 mmc_retune_release(host);
Ulf Hansson07a68212013-04-19 15:12:11 +02001928 return err;
1929}
1930
Ulf Hanssone6c08582012-10-05 12:45:39 -04001931static int mmc_can_poweroff_notify(const struct mmc_card *card)
1932{
1933 return card &&
1934 mmc_card_mmc(card) &&
1935 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1936}
1937
1938static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1939{
1940 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1941 int err;
1942
1943 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1944 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1945 timeout = card->ext_csd.power_off_longtime;
1946
Ulf Hansson878e2002013-09-09 11:57:57 +02001947 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1948 EXT_CSD_POWER_OFF_NOTIFICATION,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001949 notify_type, timeout, 0, true, false, false);
Ulf Hanssone6c08582012-10-05 12:45:39 -04001950 if (err)
1951 pr_err("%s: Power Off Notification timed out, %u\n",
1952 mmc_hostname(card->host), timeout);
1953
1954 /* Disable the power off notification after the switch operation. */
1955 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1956
1957 return err;
1958}
1959
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001960/*
1961 * Host is being removed. Free up the current card.
1962 */
1963static void mmc_remove(struct mmc_host *host)
1964{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001965 mmc_remove_card(host->card);
1966 host->card = NULL;
1967}
1968
1969/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001970 * Card detection - card is alive.
1971 */
1972static int mmc_alive(struct mmc_host *host)
1973{
1974 return mmc_send_status(host->card, NULL);
1975}
1976
1977/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001978 * Card detection callback from host.
1979 */
1980static void mmc_detect(struct mmc_host *host)
1981{
1982 int err;
1983
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03001984 mmc_get_card(host->card, NULL);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001985
1986 /*
1987 * Just check if our card has been removed.
1988 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001989 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001990
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03001991 mmc_put_card(host->card, NULL);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001992
Pierre Ossman17b04292007-07-22 22:18:46 +02001993 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001994 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001995
1996 mmc_claim_host(host);
1997 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001998 mmc_power_off(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001999 mmc_release_host(host);
2000 }
2001}
2002
Ulf Hansson03d071f2013-06-10 17:03:43 +02002003static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002004{
Balaji T Kc3805462011-09-08 22:08:39 +05302005 int err = 0;
Ulf Hansson03d071f2013-06-10 17:03:43 +02002006 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
2007 EXT_CSD_POWER_OFF_LONG;
Balaji T Kc3805462011-09-08 22:08:39 +05302008
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002009 mmc_claim_host(host);
Maya Erez881d9262013-01-28 16:44:22 -05002010
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002011 if (mmc_card_suspended(host->card))
2012 goto out;
2013
Ulf Hansson39b94312013-05-02 14:02:36 +02002014 if (mmc_card_doing_bkops(host->card)) {
2015 err = mmc_stop_bkops(host->card);
2016 if (err)
2017 goto out;
2018 }
2019
Ulf Hansson10e5d962013-12-16 16:23:22 +01002020 err = mmc_flush_cache(host->card);
Maya Erez881d9262013-01-28 16:44:22 -05002021 if (err)
2022 goto out;
2023
Ulf Hansson432356792013-06-10 17:03:45 +02002024 if (mmc_can_poweroff_notify(host->card) &&
Ulf Hansson53275c22013-06-10 17:03:46 +02002025 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
Ulf Hansson03d071f2013-06-10 17:03:43 +02002026 err = mmc_poweroff_notify(host->card, notify_type);
Ulf Hansson07a68212013-04-19 15:12:11 +02002027 else if (mmc_can_sleep(host->card))
2028 err = mmc_sleep(host);
Ulf Hanssone6c08582012-10-05 12:45:39 -04002029 else if (!mmc_host_is_spi(host))
Jaehoon Chung85e727e2012-05-31 20:31:47 +09002030 err = mmc_deselect_cards(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002031
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002032 if (!err) {
Ulf Hansson74590262013-06-10 17:03:38 +02002033 mmc_power_off(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002034 mmc_card_set_suspended(host->card);
2035 }
Maya Erez881d9262013-01-28 16:44:22 -05002036out:
2037 mmc_release_host(host);
Balaji T Kc3805462011-09-08 22:08:39 +05302038 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002039}
2040
2041/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002042 * Suspend callback
Ulf Hansson03d071f2013-06-10 17:03:43 +02002043 */
2044static int mmc_suspend(struct mmc_host *host)
2045{
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002046 int err;
2047
2048 err = _mmc_suspend(host, true);
2049 if (!err) {
2050 pm_runtime_disable(&host->card->dev);
2051 pm_runtime_set_suspended(&host->card->dev);
2052 }
2053
2054 return err;
Ulf Hansson03d071f2013-06-10 17:03:43 +02002055}
2056
2057/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002058 * This function tries to determine if the same card is still present
2059 * and, if so, restore all state to it.
2060 */
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002061static int _mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002062{
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002063 int err = 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002064
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002065 mmc_claim_host(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002066
2067 if (!mmc_card_suspended(host->card))
2068 goto out;
2069
Ulf Hansson69041152013-09-13 11:31:33 +02002070 mmc_power_up(host, host->card->ocr);
Ulf Hansson69041152013-09-13 11:31:33 +02002071 err = mmc_init_card(host, host->card->ocr, host->card);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002072 mmc_card_clr_suspended(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002073
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002074out:
2075 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002076 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002077}
2078
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002079/*
2080 * Shutdown callback
2081 */
2082static int mmc_shutdown(struct mmc_host *host)
2083{
2084 int err = 0;
2085
2086 /*
2087 * In a specific case for poweroff notify, we need to resume the card
2088 * before we can shutdown it properly.
2089 */
2090 if (mmc_can_poweroff_notify(host->card) &&
2091 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002092 err = _mmc_resume(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002093
2094 if (!err)
2095 err = _mmc_suspend(host, false);
2096
2097 return err;
2098}
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002099
2100/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002101 * Callback for resume.
2102 */
2103static int mmc_resume(struct mmc_host *host)
2104{
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002105 pm_runtime_enable(&host->card->dev);
Ulf Hanssonc29536e2015-11-05 16:01:32 +01002106 return 0;
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002107}
2108
2109/*
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002110 * Callback for runtime_suspend.
2111 */
2112static int mmc_runtime_suspend(struct mmc_host *host)
2113{
2114 int err;
2115
2116 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2117 return 0;
2118
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002119 err = _mmc_suspend(host, true);
Ulf Hansson0cc81a82013-10-03 11:24:44 +02002120 if (err)
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002121 pr_err("%s: error %d doing aggressive suspend\n",
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002122 mmc_hostname(host), err);
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002123
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002124 return err;
2125}
2126
2127/*
2128 * Callback for runtime_resume.
2129 */
2130static int mmc_runtime_resume(struct mmc_host *host)
2131{
2132 int err;
2133
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002134 err = _mmc_resume(host);
Adrian Hunter520322d2015-12-14 15:51:27 +02002135 if (err && err != -ENOMEDIUM)
Ulf Hanssonc29536e2015-11-05 16:01:32 +01002136 pr_err("%s: error %d doing runtime resume\n",
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002137 mmc_hostname(host), err);
2138
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002139 return 0;
2140}
2141
Ulf Hansson88bed3a2017-06-08 15:27:41 +02002142static int mmc_can_reset(struct mmc_card *card)
Johan Rudholmf855a372015-01-12 15:38:05 +01002143{
2144 u8 rst_n_function;
2145
2146 rst_n_function = card->ext_csd.rst_n_function;
2147 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2148 return 0;
2149 return 1;
2150}
Johan Rudholmf855a372015-01-12 15:38:05 +01002151
Ulf Hansson3a3db602018-04-05 13:24:43 +02002152static int _mmc_hw_reset(struct mmc_host *host)
Johan Rudholmf855a372015-01-12 15:38:05 +01002153{
2154 struct mmc_card *card = host->card;
Johan Rudholmf855a372015-01-12 15:38:05 +01002155
Adrian Hunter437db4c2016-05-04 10:38:21 +03002156 /*
2157 * In the case of recovery, we can't expect flushing the cache to work
2158 * always, but we have a go and ignore errors.
2159 */
2160 mmc_flush_cache(host->card);
2161
Gwendal Grignou4e6c7172016-04-01 16:04:22 -07002162 if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2163 mmc_can_reset(card)) {
2164 /* If the card accept RST_n signal, send it. */
2165 mmc_set_clock(host, host->f_init);
2166 host->ops->hw_reset(host);
2167 /* Set initial state and call mmc_set_ios */
2168 mmc_set_initial_state(host);
2169 } else {
2170 /* Do a brute force power cycle */
2171 mmc_power_cycle(host, card->ocr);
Ulf Hansson52c82122017-05-08 23:52:04 +02002172 mmc_pwrseq_reset(host);
Gwendal Grignou4e6c7172016-04-01 16:04:22 -07002173 }
Ulf Hansson364549d2015-06-01 11:14:57 +02002174 return mmc_init_card(host, card->ocr, card);
Johan Rudholmf855a372015-01-12 15:38:05 +01002175}
2176
Adrian Hunter9feae242009-09-22 16:44:32 -07002177static const struct mmc_bus_ops mmc_ops = {
2178 .remove = mmc_remove,
2179 .detect = mmc_detect,
Adrian Hunter9feae242009-09-22 16:44:32 -07002180 .suspend = mmc_suspend,
2181 .resume = mmc_resume,
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002182 .runtime_suspend = mmc_runtime_suspend,
2183 .runtime_resume = mmc_runtime_resume,
Adrian Hunterd3049502011-11-28 16:22:00 +02002184 .alive = mmc_alive,
Ulf Hansson486fdbb2013-06-10 17:03:44 +02002185 .shutdown = mmc_shutdown,
Ulf Hansson3a3db602018-04-05 13:24:43 +02002186 .hw_reset = _mmc_hw_reset,
Adrian Hunter9feae242009-09-22 16:44:32 -07002187};
2188
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002189/*
2190 * Starting point for MMC card init.
2191 */
Andy Ross807e8e42011-01-03 10:36:56 -08002192int mmc_attach_mmc(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002193{
2194 int err;
Ulf Hansson69041152013-09-13 11:31:33 +02002195 u32 ocr, rocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002196
Pierre Ossmand84075c82007-08-09 13:23:56 +02002197 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002198
Stefan Nilsson XK44669032011-09-15 17:50:38 +02002199 /* Set correct bus mode for MMC before attempting attach */
2200 if (!mmc_host_is_spi(host))
2201 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2202
Andy Ross807e8e42011-01-03 10:36:56 -08002203 err = mmc_send_op_cond(host, 0, &ocr);
2204 if (err)
2205 return err;
2206
Ulf Hansson2501c912013-10-30 01:00:18 +01002207 mmc_attach_bus(host, &mmc_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01002208 if (host->ocr_avail_mmc)
2209 host->ocr_avail = host->ocr_avail_mmc;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002210
2211 /*
David Brownellaf517152007-08-08 09:11:32 -07002212 * We need to get OCR a different way for SPI.
2213 */
2214 if (mmc_host_is_spi(host)) {
2215 err = mmc_spi_read_ocr(host, 1, &ocr);
2216 if (err)
2217 goto err;
2218 }
2219
Ulf Hansson69041152013-09-13 11:31:33 +02002220 rocr = mmc_select_voltage(host, ocr);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002221
2222 /*
2223 * Can we support the voltage of the card?
2224 */
Ulf Hansson69041152013-09-13 11:31:33 +02002225 if (!rocr) {
Pierre Ossman109b5be2007-07-23 00:12:10 +02002226 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002227 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02002228 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002229
2230 /*
2231 * Detect and init the card.
2232 */
Ulf Hansson69041152013-09-13 11:31:33 +02002233 err = mmc_init_card(host, rocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02002234 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002235 goto err;
2236
2237 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02002238 err = mmc_add_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002239 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002240 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002241
Sergei Shtylyov2860d062015-10-14 23:53:03 +03002242 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002243 return 0;
2244
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002245remove_card:
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002246 mmc_remove_card(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002247 mmc_claim_host(host);
Andy Ross807e8e42011-01-03 10:36:56 -08002248 host->card = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002249err:
2250 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002251
Girish K Sa3c76eb2011-10-11 11:44:09 +05302252 pr_err("%s: error %d whilst initialising MMC card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02002253 mmc_hostname(host), err);
2254
Pierre Ossmanadf66a02007-07-22 23:08:30 +02002255 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002256}