blob: b91abe01aacc29b18ecbbd03cdcfc9e006d78b60 [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"
Philip Rakity4c4cb172011-05-13 11:17:18 +053028#include "sd_ops.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010029
Ulf Hanssonfe1b5702016-11-04 18:32:33 +010030#define DEFAULT_CMD6_TIMEOUT_MS 500
31
Pierre Ossman7ea239d2006-12-31 00:11:32 +010032static const unsigned int tran_exp[] = {
33 10000, 100000, 1000000, 10000000,
34 0, 0, 0, 0
35};
36
37static const unsigned char tran_mant[] = {
38 0, 10, 12, 13, 15, 20, 25, 30,
39 35, 40, 45, 50, 55, 60, 70, 80,
40};
41
42static const unsigned int tacc_exp[] = {
43 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
44};
45
46static const unsigned int tacc_mant[] = {
47 0, 10, 12, 13, 15, 20, 25, 30,
48 35, 40, 45, 50, 55, 60, 70, 80,
49};
50
Pratibhasagar V53202262016-06-09 18:09:31 -040051static const struct mmc_fixup mmc_ext_csd_fixups[] = {
52 /*
53 * Certain Hynix eMMC 4.41 cards might get broken when HPI feature
54 * is used so disable the HPI feature for such buggy cards.
55 */
56 MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_HYNIX,
57 0x014a, add_quirk, MMC_QUIRK_BROKEN_HPI, 5),
58
59 END_FIXUP
60};
61
Pierre Ossman7ea239d2006-12-31 00:11:32 +010062#define UNSTUFF_BITS(resp,start,size) \
63 ({ \
64 const int __size = size; \
65 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
66 const int __off = 3 - ((start) / 32); \
67 const int __shft = (start) & 31; \
68 u32 __res; \
69 \
70 __res = resp[__off] >> __shft; \
71 if (__size + __shft > 32) \
72 __res |= resp[__off-1] << ((32 - __shft) % 32); \
73 __res & __mask; \
74 })
75
76/*
77 * Given the decoded CSD structure, decode the raw CID to our CID structure.
78 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020079static int mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010080{
81 u32 *resp = card->raw_cid;
82
83 /*
84 * The selection of the format here is based upon published
85 * specs from sandisk and from what people have reported.
86 */
87 switch (card->csd.mmca_vsn) {
88 case 0: /* MMC v1.0 - v1.2 */
89 case 1: /* MMC v1.4 */
90 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
91 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
92 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
93 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
94 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
95 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
96 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
97 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
98 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
99 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
100 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
101 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
102 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
103 break;
104
105 case 2: /* MMC v2.0 - v2.2 */
106 case 3: /* MMC v3.1 - v3.3 */
107 case 4: /* MMC v4 */
108 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
109 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
110 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
111 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
112 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
113 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
114 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
115 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800116 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100117 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
118 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
119 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
120 break;
121
122 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530123 pr_err("%s: card has unknown MMCA version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100124 mmc_hostname(card->host), card->csd.mmca_vsn);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200125 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100126 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200127
128 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100129}
130
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700131static void mmc_set_erase_size(struct mmc_card *card)
132{
133 if (card->ext_csd.erase_group_def & 1)
134 card->erase_size = card->ext_csd.hc_erase_size;
135 else
136 card->erase_size = card->csd.erase_size;
137
138 mmc_init_erase(card);
139}
140
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100141/*
142 * Given a 128-bit response, decode to our card CSD structure.
143 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200144static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100145{
146 struct mmc_csd *csd = &card->csd;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700147 unsigned int e, m, a, b;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100148 u32 *resp = card->raw_csd;
149
150 /*
151 * We only understand CSD structure v1.1 and v1.2.
152 * v1.2 has extra information in bits 15, 11 and 10.
Kyungmin Park6da24b72010-08-10 18:01:36 -0700153 * We also support eMMC v4.4 & v4.41.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100154 */
Kyungmin Park6da24b72010-08-10 18:01:36 -0700155 csd->structure = UNSTUFF_BITS(resp, 126, 2);
156 if (csd->structure == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530157 pr_err("%s: unrecognised CSD structure version %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700158 mmc_hostname(card->host), csd->structure);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200159 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100160 }
161
162 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
163 m = UNSTUFF_BITS(resp, 115, 4);
164 e = UNSTUFF_BITS(resp, 112, 3);
165 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
166 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
167
168 m = UNSTUFF_BITS(resp, 99, 4);
169 e = UNSTUFF_BITS(resp, 96, 3);
170 csd->max_dtr = tran_exp[e] * tran_mant[m];
171 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
172
173 e = UNSTUFF_BITS(resp, 47, 3);
174 m = UNSTUFF_BITS(resp, 62, 12);
175 csd->capacity = (1 + m) << (e + 2);
176
177 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
178 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
179 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
180 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
Sascha Hauer3d705d12014-08-19 10:45:51 +0200181 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100182 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
183 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
184 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200185
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700186 if (csd->write_blkbits >= 9) {
187 a = UNSTUFF_BITS(resp, 42, 5);
188 b = UNSTUFF_BITS(resp, 37, 5);
189 csd->erase_size = (a + 1) * (b + 1);
190 csd->erase_size <<= csd->write_blkbits - 9;
191 }
192
Pierre Ossmanbd766312007-05-01 16:11:57 +0200193 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100194}
195
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900196static void mmc_select_card_type(struct mmc_card *card)
197{
198 struct mmc_host *host = card->host;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900199 u8 card_type = card->ext_csd.raw_card_type;
Lee Jones5f1a4dd2012-11-14 12:35:51 +0000200 u32 caps = host->caps, caps2 = host->caps2;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900201 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900202 unsigned int avail_type = 0;
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900203
204 if (caps & MMC_CAP_MMC_HIGHSPEED &&
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900205 card_type & EXT_CSD_CARD_TYPE_HS_26) {
206 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
207 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
208 }
209
210 if (caps & MMC_CAP_MMC_HIGHSPEED &&
211 card_type & EXT_CSD_CARD_TYPE_HS_52) {
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900212 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900213 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
214 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900215
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900216 if (caps & MMC_CAP_1_8V_DDR &&
217 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900218 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900219 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
220 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900221
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900222 if (caps & MMC_CAP_1_2V_DDR &&
223 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
224 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
225 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
226 }
227
228 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
229 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
Seungwon Jeon577fb132014-04-23 17:08:44 +0900230 hs200_max_dtr = MMC_HS200_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900231 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
232 }
233
234 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
235 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
Seungwon Jeon577fb132014-04-23 17:08:44 +0900236 hs200_max_dtr = MMC_HS200_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900237 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
238 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900239
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900240 if (caps2 & MMC_CAP2_HS400_1_8V &&
241 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
242 hs200_max_dtr = MMC_HS200_MAX_DTR;
243 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
244 }
245
246 if (caps2 & MMC_CAP2_HS400_1_2V &&
247 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
248 hs200_max_dtr = MMC_HS200_MAX_DTR;
249 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
250 }
251
Shawn Lin81ac2af2016-05-26 09:56:22 +0800252 if ((caps2 & MMC_CAP2_HS400_ES) &&
253 card->ext_csd.strobe_support &&
254 (avail_type & EXT_CSD_CARD_TYPE_HS400))
255 avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
256
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900257 card->ext_csd.hs_max_dtr = hs_max_dtr;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900258 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900259 card->mmc_avail_type = avail_type;
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900260}
261
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200262static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
263{
Grégory Soutadé994324b2014-09-15 17:47:11 +0200264 u8 hc_erase_grp_sz, hc_wp_grp_sz;
265
266 /*
267 * Disable these attributes by default
268 */
269 card->ext_csd.enhanced_area_offset = -EINVAL;
270 card->ext_csd.enhanced_area_size = -EINVAL;
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200271
272 /*
273 * Enhanced area feature support -- check whether the eMMC
274 * card has the Enhanced area enabled. If so, export enhanced
275 * area offset and size to user by adding sysfs interface.
276 */
277 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
278 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
Grégory Soutadé994324b2014-09-15 17:47:11 +0200279 if (card->ext_csd.partition_setting_completed) {
280 hc_erase_grp_sz =
281 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
282 hc_wp_grp_sz =
283 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200284
Grégory Soutadé994324b2014-09-15 17:47:11 +0200285 /*
286 * calculate the enhanced data area offset, in bytes
287 */
288 card->ext_csd.enhanced_area_offset =
Kuninori Morimotoded8a5f2015-05-11 07:34:53 +0000289 (((unsigned long long)ext_csd[139]) << 24) +
290 (((unsigned long long)ext_csd[138]) << 16) +
291 (((unsigned long long)ext_csd[137]) << 8) +
292 (((unsigned long long)ext_csd[136]));
Grégory Soutadé994324b2014-09-15 17:47:11 +0200293 if (mmc_card_blockaddr(card))
294 card->ext_csd.enhanced_area_offset <<= 9;
295 /*
296 * calculate the enhanced data area size, in kilobytes
297 */
298 card->ext_csd.enhanced_area_size =
299 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
300 ext_csd[140];
301 card->ext_csd.enhanced_area_size *=
302 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
303 card->ext_csd.enhanced_area_size <<= 9;
304 } else {
305 pr_warn("%s: defines enhanced area without partition setting complete\n",
306 mmc_hostname(card->host));
307 }
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200308 }
309}
310
Ulf Hansson4facdde2017-01-13 14:14:14 +0100311static void mmc_part_add(struct mmc_card *card, unsigned int size,
312 unsigned int part_cfg, char *name, int idx, bool ro,
313 int area_type)
314{
315 card->part[card->nr_parts].size = size;
316 card->part[card->nr_parts].part_cfg = part_cfg;
317 sprintf(card->part[card->nr_parts].name, name, idx);
318 card->part[card->nr_parts].force_ro = ro;
319 card->part[card->nr_parts].area_type = area_type;
320 card->nr_parts++;
321}
322
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200323static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
324{
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200325 int idx;
Grégory Soutadé994324b2014-09-15 17:47:11 +0200326 u8 hc_erase_grp_sz, hc_wp_grp_sz;
327 unsigned int part_size;
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200328
329 /*
330 * General purpose partition feature support --
331 * If ext_csd has the size of general purpose partitions,
332 * set size, part_cfg, partition name in mmc_part.
333 */
334 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
335 EXT_CSD_PART_SUPPORT_PART_EN) {
Grégory Soutadé994324b2014-09-15 17:47:11 +0200336 hc_erase_grp_sz =
337 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
338 hc_wp_grp_sz =
339 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200340
341 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
342 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
343 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
344 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
345 continue;
Grégory Soutadé994324b2014-09-15 17:47:11 +0200346 if (card->ext_csd.partition_setting_completed == 0) {
347 pr_warn("%s: has partition size defined without partition complete\n",
348 mmc_hostname(card->host));
349 break;
350 }
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200351 part_size =
352 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
353 << 16) +
354 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
355 << 8) +
356 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
357 part_size *= (size_t)(hc_erase_grp_sz *
358 hc_wp_grp_sz);
359 mmc_part_add(card, part_size << 19,
360 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
361 "gp%d", idx, false,
362 MMC_BLK_DATA_AREA_GP);
363 }
364 }
365}
366
Adrian Hunter1c447112016-05-05 08:12:28 +0300367/* Minimum partition switch timeout in milliseconds */
368#define MMC_MIN_PART_SWITCH_TIME 300
369
Philip Rakity08ee80c2011-05-24 18:14:58 -0700370/*
371 * Decode extended CSD.
372 */
Ulf Hansson076ec382014-10-20 13:37:24 +0200373static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700374{
Namjae Jeone0c368d2011-10-06 23:41:38 +0900375 int err = 0, idx;
376 unsigned int part_size;
Hans de Goede81f8a7b2015-04-01 17:26:23 +0200377 struct device_node *np;
378 bool broken_hpi = false;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700379
Kyungmin Park6da24b72010-08-10 18:01:36 -0700380 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700381 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
Kyungmin Park6da24b72010-08-10 18:01:36 -0700382 if (card->csd.structure == 3) {
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700383 if (card->ext_csd.raw_ext_csd_structure > 2) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530384 pr_err("%s: unrecognised EXT_CSD structure "
Kyungmin Park6da24b72010-08-10 18:01:36 -0700385 "version %d\n", mmc_hostname(card->host),
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700386 card->ext_csd.raw_ext_csd_structure);
Kyungmin Park6da24b72010-08-10 18:01:36 -0700387 err = -EINVAL;
388 goto out;
389 }
390 }
391
Hans de Goede81f8a7b2015-04-01 17:26:23 +0200392 np = mmc_of_find_child_device(card->host, 0);
393 if (np && of_device_is_compatible(np, "mmc-card"))
394 broken_hpi = of_property_read_bool(np, "broken-hpi");
395 of_node_put(np);
396
Romain Izard03a59432014-06-27 10:51:07 +0200397 /*
398 * The EXT_CSD format is meant to be forward compatible. As long
399 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
400 * are authorized, see JEDEC JESD84-B50 section B.8.
401 */
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700402 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Pierre Ossmand7604d72007-07-23 00:34:07 +0200403
Pratibhasagar V53202262016-06-09 18:09:31 -0400404 /* fixup device after ext_csd revision field is updated */
405 mmc_fixup_device(card, mmc_ext_csd_fixups);
406
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700407 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
408 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
409 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
410 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700411 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200412 card->ext_csd.sectors =
413 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
414 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
415 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
416 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700417
418 /* Cards with density > 2GiB are sector addressed */
419 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200420 mmc_card_set_blockaddr(card);
421 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900422
Shawn Lin81ac2af2016-05-26 09:56:22 +0800423 card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700424 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900425 mmc_select_card_type(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100426
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700427 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
428 card->ext_csd.raw_erase_timeout_mult =
429 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
430 card->ext_csd.raw_hc_erase_grp_size =
431 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700432 if (card->ext_csd.rev >= 3) {
433 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500434 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
435
436 /* EXT_CSD value is in units of 10ms, but we store in ms */
437 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
Adrian Hunter1c447112016-05-05 08:12:28 +0300438 /* Some eMMC set the value too low so set a minimum */
439 if (card->ext_csd.part_time &&
440 card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
441 card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700442
443 /* Sleep / awake timeout in 100ns units */
444 if (sa_shift > 0 && sa_shift <= 0x17)
445 card->ext_csd.sa_timeout =
446 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700447 card->ext_csd.erase_group_def =
448 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
449 card->ext_csd.hc_erase_timeout = 300 *
450 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
451 card->ext_csd.hc_erase_size =
452 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500453
454 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500455
456 /*
457 * There are two boot regions of equal size, defined in
458 * multiples of 128K.
459 */
Namjae Jeone0c368d2011-10-06 23:41:38 +0900460 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
461 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
462 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
463 mmc_part_add(card, part_size,
464 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100465 "boot%d", idx, true,
466 MMC_BLK_DATA_AREA_BOOT);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900467 }
468 }
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700469 }
470
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700471 card->ext_csd.raw_hc_erase_gap_size =
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100472 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700473 card->ext_csd.raw_sec_trim_mult =
474 ext_csd[EXT_CSD_SEC_TRIM_MULT];
475 card->ext_csd.raw_sec_erase_mult =
476 ext_csd[EXT_CSD_SEC_ERASE_MULT];
477 card->ext_csd.raw_sec_feature_support =
478 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
479 card->ext_csd.raw_trim_mult =
480 ext_csd[EXT_CSD_TRIM_MULT];
Philip Rakity836dc2f2013-04-04 20:18:11 +0100481 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
Adrian Hunterb097e072015-02-06 14:12:57 +0200482 card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700483 if (card->ext_csd.rev >= 4) {
Grégory Soutadé69803d42014-09-15 17:47:09 +0200484 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
485 EXT_CSD_PART_SETTING_COMPLETED)
486 card->ext_csd.partition_setting_completed = 1;
487 else
488 card->ext_csd.partition_setting_completed = 0;
489
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200490 mmc_manage_enhanced_area(card, ext_csd);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800491
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200492 mmc_manage_gp_partitions(card, ext_csd);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900493
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700494 card->ext_csd.sec_trim_mult =
495 ext_csd[EXT_CSD_SEC_TRIM_MULT];
496 card->ext_csd.sec_erase_mult =
497 ext_csd[EXT_CSD_SEC_ERASE_MULT];
498 card->ext_csd.sec_feature_support =
499 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
500 card->ext_csd.trim_timeout = 300 *
501 ext_csd[EXT_CSD_TRIM_MULT];
Johan Rudholmadd710e2011-12-02 08:51:06 +0100502
503 /*
504 * Note that the call to mmc_part_add above defaults to read
505 * only. If this default assumption is changed, the call must
506 * take into account the value of boot_locked below.
507 */
508 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
509 card->ext_csd.boot_ro_lockable = true;
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200510
511 /* Save power class values */
512 card->ext_csd.raw_pwr_cl_52_195 =
513 ext_csd[EXT_CSD_PWR_CL_52_195];
514 card->ext_csd.raw_pwr_cl_26_195 =
515 ext_csd[EXT_CSD_PWR_CL_26_195];
516 card->ext_csd.raw_pwr_cl_52_360 =
517 ext_csd[EXT_CSD_PWR_CL_52_360];
518 card->ext_csd.raw_pwr_cl_26_360 =
519 ext_csd[EXT_CSD_PWR_CL_26_360];
520 card->ext_csd.raw_pwr_cl_200_195 =
521 ext_csd[EXT_CSD_PWR_CL_200_195];
522 card->ext_csd.raw_pwr_cl_200_360 =
523 ext_csd[EXT_CSD_PWR_CL_200_360];
524 card->ext_csd.raw_pwr_cl_ddr_52_195 =
525 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
526 card->ext_csd.raw_pwr_cl_ddr_52_360 =
527 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900528 card->ext_csd.raw_pwr_cl_ddr_200_360 =
529 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700530 }
531
Adrian Hunterb2499512011-08-29 16:42:11 +0300532 if (card->ext_csd.rev >= 5) {
Romain Izard7c4f10a2013-06-14 14:25:44 +0200533 /* Adjust production date as per JEDEC JESD84-B451 */
534 if (card->cid.year < 2010)
535 card->cid.year += 16;
536
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000537 /* check whether the eMMC card supports BKOPS */
Pratibhasagar V53202262016-06-09 18:09:31 -0400538 if (!mmc_card_broken_hpi(card) &&
539 ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000540 card->ext_csd.bkops = 1;
Alexey Skidanov0501be62015-01-29 10:49:43 +0200541 card->ext_csd.man_bkops_en =
542 (ext_csd[EXT_CSD_BKOPS_EN] &
543 EXT_CSD_MANUAL_BKOPS_MASK);
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000544 card->ext_csd.raw_bkops_status =
545 ext_csd[EXT_CSD_BKOPS_STATUS];
Alexey Skidanov0501be62015-01-29 10:49:43 +0200546 if (!card->ext_csd.man_bkops_en)
Wolfram Sang4ec96b42016-01-25 20:18:12 +0100547 pr_debug("%s: MAN_BKOPS_EN bit is not set\n",
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000548 mmc_hostname(card->host));
549 }
550
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400551 /* check whether the eMMC card supports HPI */
Pratibhasagar V53202262016-06-09 18:09:31 -0400552 if (!mmc_card_broken_hpi(card) &&
553 !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400554 card->ext_csd.hpi = 1;
555 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
556 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
557 else
558 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
559 /*
560 * Indicate the maximum timeout to close
561 * a command interrupted by HPI
562 */
563 card->ext_csd.out_of_int_time =
564 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
565 }
566
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500567 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
Adrian Hunterb2499512011-08-29 16:42:11 +0300568 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
Loic Pallardy090d25f2012-11-17 18:08:24 -0500569
570 /*
571 * RPMB regions are defined in multiples of 128K.
572 */
573 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
Balaji T Kd0123cc2013-01-25 17:00:30 +0530574 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
Loic Pallardy090d25f2012-11-17 18:08:24 -0500575 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
576 EXT_CSD_PART_CONFIG_ACC_RPMB,
577 "rpmb", 0, false,
578 MMC_BLK_DATA_AREA_RPMB);
579 }
Adrian Hunterb2499512011-08-29 16:42:11 +0300580 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500581
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400582 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700583 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
584 card->erased_byte = 0xFF;
585 else
586 card->erased_byte = 0x0;
587
Seungwon Jeon336c7162011-10-25 09:43:12 +0900588 /* eMMC v4.5 or later */
Ulf Hanssonfe1b5702016-11-04 18:32:33 +0100589 card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS;
Girish K Sbec87262011-10-13 12:04:16 +0530590 if (card->ext_csd.rev >= 6) {
Seungwon Jeon336c7162011-10-25 09:43:12 +0900591 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
592
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900593 card->ext_csd.generic_cmd6_time = 10 *
594 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
Girish K Sbec87262011-10-13 12:04:16 +0530595 card->ext_csd.power_off_longtime = 10 *
596 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900597
Seungwon Jeon336c7162011-10-25 09:43:12 +0900598 card->ext_csd.cache_size =
599 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
600 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
601 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
602 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
Saugata Das42659002011-12-21 13:09:17 +0530603
604 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
605 card->ext_csd.data_sector_size = 4096;
606 else
607 card->ext_csd.data_sector_size = 512;
608
609 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
610 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
611 card->ext_csd.data_tag_unit_size =
612 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
613 (card->ext_csd.data_sector_size);
614 } else {
615 card->ext_csd.data_tag_unit_size = 0;
616 }
Seungwon Jeonabd9ac12013-02-06 17:01:43 +0900617
618 card->ext_csd.max_packed_writes =
619 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
620 card->ext_csd.max_packed_reads =
621 ext_csd[EXT_CSD_MAX_PACKED_READS];
Saugata Dasa5075eb2012-05-17 16:32:21 +0530622 } else {
623 card->ext_csd.data_sector_size = 512;
Seungwon Jeon336c7162011-10-25 09:43:12 +0900624 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900625
Gwendal Grignou0f762422014-10-16 11:27:16 -0700626 /* eMMC v5 or later */
627 if (card->ext_csd.rev >= 7) {
628 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
629 MMC_FIRMWARE_LEN);
630 card->ext_csd.ffu_capable =
631 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
632 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900633
634 card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO];
635 card->ext_csd.device_life_time_est_typ_a =
636 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
637 card->ext_csd.device_life_time_est_typ_b =
638 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
Gwendal Grignou0f762422014-10-16 11:27:16 -0700639 }
Adrian Hunter925ff3a2016-11-29 12:09:16 +0200640
641 /* eMMC v5.1 or later */
642 if (card->ext_csd.rev >= 8) {
643 card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] &
644 EXT_CSD_CMDQ_SUPPORTED;
645 card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] &
646 EXT_CSD_CMDQ_DEPTH_MASK) + 1;
647 /* Exclude inefficiently small queue depths */
648 if (card->ext_csd.cmdq_depth <= 2) {
649 card->ext_csd.cmdq_support = false;
650 card->ext_csd.cmdq_depth = 0;
651 }
652 if (card->ext_csd.cmdq_support) {
653 pr_debug("%s: Command Queue supported depth %u\n",
654 mmc_hostname(card->host),
655 card->ext_csd.cmdq_depth);
656 }
657 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100658out:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700659 return err;
660}
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100661
Ulf Hansson076ec382014-10-20 13:37:24 +0200662static int mmc_read_ext_csd(struct mmc_card *card)
663{
Ulf Hanssonc1977872014-10-20 14:08:16 +0200664 u8 *ext_csd;
Ulf Hansson076ec382014-10-20 13:37:24 +0200665 int err;
666
Ulf Hanssonc1977872014-10-20 14:08:16 +0200667 if (!mmc_can_ext_csd(card))
668 return 0;
669
Ulf Hansson076ec382014-10-20 13:37:24 +0200670 err = mmc_get_ext_csd(card, &ext_csd);
Ulf Hanssonc1977872014-10-20 14:08:16 +0200671 if (err) {
672 /* If the host or the card can't do the switch,
673 * fail more gracefully. */
674 if ((err != -EINVAL)
675 && (err != -ENOSYS)
676 && (err != -EFAULT))
677 return err;
678
679 /*
680 * High capacity cards should have this "magic" size
681 * stored in their CSD.
682 */
683 if (card->csd.capacity == (4096 * 512)) {
684 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
685 mmc_hostname(card->host));
686 } else {
687 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
688 mmc_hostname(card->host));
689 err = 0;
690 }
691
Ulf Hansson076ec382014-10-20 13:37:24 +0200692 return err;
Ulf Hanssonc1977872014-10-20 14:08:16 +0200693 }
Ulf Hansson076ec382014-10-20 13:37:24 +0200694
695 err = mmc_decode_ext_csd(card, ext_csd);
696 kfree(ext_csd);
697 return err;
698}
699
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700700static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700701{
702 u8 *bw_ext_csd;
703 int err;
704
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700705 if (bus_width == MMC_BUS_WIDTH_1)
706 return 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700707
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700708 err = mmc_get_ext_csd(card, &bw_ext_csd);
Ulf Hanssonc1977872014-10-20 14:08:16 +0200709 if (err)
710 return err;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700711
Philip Rakity08ee80c2011-05-24 18:14:58 -0700712 /* only compare read only fields */
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100713 err = !((card->ext_csd.raw_partition_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700714 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700715 (card->ext_csd.raw_erased_mem_count ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700716 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700717 (card->ext_csd.rev ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700718 bw_ext_csd[EXT_CSD_REV]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700719 (card->ext_csd.raw_ext_csd_structure ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700720 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700721 (card->ext_csd.raw_card_type ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700722 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700723 (card->ext_csd.raw_s_a_timeout ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700724 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700725 (card->ext_csd.raw_hc_erase_gap_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700726 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700727 (card->ext_csd.raw_erase_timeout_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700728 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700729 (card->ext_csd.raw_hc_erase_grp_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700730 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700731 (card->ext_csd.raw_sec_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700732 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700733 (card->ext_csd.raw_sec_erase_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700734 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700735 (card->ext_csd.raw_sec_feature_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700736 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700737 (card->ext_csd.raw_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700738 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700739 (card->ext_csd.raw_sectors[0] ==
740 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
741 (card->ext_csd.raw_sectors[1] ==
742 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
743 (card->ext_csd.raw_sectors[2] ==
744 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
745 (card->ext_csd.raw_sectors[3] ==
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200746 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
747 (card->ext_csd.raw_pwr_cl_52_195 ==
748 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
749 (card->ext_csd.raw_pwr_cl_26_195 ==
750 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
751 (card->ext_csd.raw_pwr_cl_52_360 ==
752 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
753 (card->ext_csd.raw_pwr_cl_26_360 ==
754 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
755 (card->ext_csd.raw_pwr_cl_200_195 ==
756 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
757 (card->ext_csd.raw_pwr_cl_200_360 ==
758 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
759 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
760 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
761 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900762 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
763 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
764 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
765
Philip Rakity08ee80c2011-05-24 18:14:58 -0700766 if (err)
767 err = -EINVAL;
768
Ulf Hansson00b41b52014-10-16 16:18:51 +0200769 kfree(bw_ext_csd);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100770 return err;
771}
772
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100773MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
774 card->raw_cid[2], card->raw_cid[3]);
775MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
776 card->raw_csd[2], card->raw_csd[3]);
777MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700778MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
779MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Gwendal Grignou0f762422014-10-16 11:27:16 -0700780MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100781MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
782MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
783MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
784MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800785MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900786MMC_DEV_ATTR(pre_eol_info, "%02x\n", card->ext_csd.pre_eol_info);
787MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n",
788 card->ext_csd.device_life_time_est_typ_a,
789 card->ext_csd.device_life_time_est_typ_b);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100790MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800791MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
792 card->ext_csd.enhanced_area_offset);
793MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
Loic Pallardy188cc042012-08-06 17:12:29 +0200794MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
795MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
Bojan Prtvar5fb06af2016-07-04 13:56:55 +0200796MMC_DEV_ATTR(ocr, "%08x\n", card->ocr);
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,
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900843 &dev_attr_pre_eol_info.attr,
844 &dev_attr_life_time.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100845 &dev_attr_serial.attr,
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800846 &dev_attr_enhanced_area_offset.attr,
847 &dev_attr_enhanced_area_size.attr,
Loic Pallardy188cc042012-08-06 17:12:29 +0200848 &dev_attr_raw_rpmb_size_mult.attr,
849 &dev_attr_rel_sectors.attr,
Bojan Prtvar5fb06af2016-07-04 13:56:55 +0200850 &dev_attr_ocr.attr,
Bojan Prtvar6825a602016-07-19 11:16:38 +0200851 &dev_attr_dsr.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100852 NULL,
853};
Axel Lind1e58212014-03-08 15:05:27 +0800854ATTRIBUTE_GROUPS(mmc_std);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100855
856static struct device_type mmc_type = {
Axel Lind1e58212014-03-08 15:05:27 +0800857 .groups = mmc_std_groups,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100858};
859
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100860/*
Girish K Sb87d8db2011-09-23 20:41:47 +0530861 * Select the PowerClass for the current bus width
862 * If power class is defined for 4/8 bit bus in the
863 * extended CSD register, select it by executing the
864 * mmc_switch command.
865 */
Seungwon Jeon23850492014-04-23 17:08:05 +0900866static int __mmc_select_powerclass(struct mmc_card *card,
867 unsigned int bus_width)
Girish K Sb87d8db2011-09-23 20:41:47 +0530868{
Seungwon Jeon23850492014-04-23 17:08:05 +0900869 struct mmc_host *host = card->host;
870 struct mmc_ext_csd *ext_csd = &card->ext_csd;
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200871 unsigned int pwrclass_val = 0;
Seungwon Jeon23850492014-04-23 17:08:05 +0900872 int err = 0;
Girish K Sb87d8db2011-09-23 20:41:47 +0530873
Girish K Sb87d8db2011-09-23 20:41:47 +0530874 switch (1 << host->ios.vdd) {
875 case MMC_VDD_165_195:
Seungwon Jeon23850492014-04-23 17:08:05 +0900876 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
877 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
878 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200879 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
Seungwon Jeon23850492014-04-23 17:08:05 +0900880 ext_csd->raw_pwr_cl_52_195 :
881 ext_csd->raw_pwr_cl_ddr_52_195;
882 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
883 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
Girish K Sb87d8db2011-09-23 20:41:47 +0530884 break;
Subhash Jadavani93fc5a42012-04-03 12:25:58 +0530885 case MMC_VDD_27_28:
886 case MMC_VDD_28_29:
887 case MMC_VDD_29_30:
888 case MMC_VDD_30_31:
889 case MMC_VDD_31_32:
Girish K Sb87d8db2011-09-23 20:41:47 +0530890 case MMC_VDD_32_33:
891 case MMC_VDD_33_34:
892 case MMC_VDD_34_35:
893 case MMC_VDD_35_36:
Seungwon Jeon23850492014-04-23 17:08:05 +0900894 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
895 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
896 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200897 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
Seungwon Jeon23850492014-04-23 17:08:05 +0900898 ext_csd->raw_pwr_cl_52_360 :
899 ext_csd->raw_pwr_cl_ddr_52_360;
900 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900901 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
902 ext_csd->raw_pwr_cl_ddr_200_360 :
903 ext_csd->raw_pwr_cl_200_360;
Girish K Sb87d8db2011-09-23 20:41:47 +0530904 break;
905 default:
Joe Perches66061102014-09-12 14:56:56 -0700906 pr_warn("%s: Voltage range not supported for power class\n",
907 mmc_hostname(host));
Girish K Sb87d8db2011-09-23 20:41:47 +0530908 return -EINVAL;
909 }
910
Girish K Sb87d8db2011-09-23 20:41:47 +0530911 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
912 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
913 EXT_CSD_PWR_CL_8BIT_SHIFT;
914 else
915 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
916 EXT_CSD_PWR_CL_4BIT_SHIFT;
917
918 /* If the power class is different from the default value */
919 if (pwrclass_val > 0) {
920 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
921 EXT_CSD_POWER_CLASS,
922 pwrclass_val,
Seungwon Jeon71fe3eb2011-10-18 13:20:57 +0900923 card->ext_csd.generic_cmd6_time);
Girish K Sb87d8db2011-09-23 20:41:47 +0530924 }
925
926 return err;
927}
928
Seungwon Jeon23850492014-04-23 17:08:05 +0900929static int mmc_select_powerclass(struct mmc_card *card)
930{
931 struct mmc_host *host = card->host;
932 u32 bus_width, ext_csd_bits;
933 int err, ddr;
934
935 /* Power class selection is supported for versions >= 4.0 */
Ulf Hansson148bcab2014-10-20 11:33:53 +0200936 if (!mmc_can_ext_csd(card))
Seungwon Jeon23850492014-04-23 17:08:05 +0900937 return 0;
938
939 bus_width = host->ios.bus_width;
940 /* Power class values are defined only for 4/8 bit bus */
941 if (bus_width == MMC_BUS_WIDTH_1)
942 return 0;
943
944 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
945 if (ddr)
946 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
947 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
948 else
949 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
950 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
951
952 err = __mmc_select_powerclass(card, ext_csd_bits);
953 if (err)
954 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
955 mmc_hostname(host), 1 << bus_width, ddr);
956
957 return err;
958}
959
Girish K Sb87d8db2011-09-23 20:41:47 +0530960/*
Seungwon Jeon577fb132014-04-23 17:08:44 +0900961 * Set the bus speed for the selected speed mode.
Girish K Sa4924c72012-01-11 14:04:52 -0500962 */
Seungwon Jeon577fb132014-04-23 17:08:44 +0900963static void mmc_set_bus_speed(struct mmc_card *card)
Girish K Sa4924c72012-01-11 14:04:52 -0500964{
Seungwon Jeon577fb132014-04-23 17:08:44 +0900965 unsigned int max_dtr = (unsigned int)-1;
966
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900967 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
968 max_dtr > card->ext_csd.hs200_max_dtr)
Seungwon Jeon577fb132014-04-23 17:08:44 +0900969 max_dtr = card->ext_csd.hs200_max_dtr;
970 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
971 max_dtr = card->ext_csd.hs_max_dtr;
972 else if (max_dtr > card->csd.max_dtr)
973 max_dtr = card->csd.max_dtr;
974
975 mmc_set_clock(card->host, max_dtr);
976}
977
978/*
979 * Select the bus width amoung 4-bit and 8-bit(SDR).
980 * If the bus width is changed successfully, return the selected width value.
981 * Zero is returned instead of error value if the wide width is not supported.
982 */
983static int mmc_select_bus_width(struct mmc_card *card)
984{
Girish K Sa4924c72012-01-11 14:04:52 -0500985 static unsigned ext_csd_bits[] = {
Girish K Sa4924c72012-01-11 14:04:52 -0500986 EXT_CSD_BUS_WIDTH_8,
Seungwon Jeon577fb132014-04-23 17:08:44 +0900987 EXT_CSD_BUS_WIDTH_4,
Girish K Sa4924c72012-01-11 14:04:52 -0500988 };
989 static unsigned bus_widths[] = {
Girish K Sa4924c72012-01-11 14:04:52 -0500990 MMC_BUS_WIDTH_8,
Seungwon Jeon577fb132014-04-23 17:08:44 +0900991 MMC_BUS_WIDTH_4,
Girish K Sa4924c72012-01-11 14:04:52 -0500992 };
Seungwon Jeon577fb132014-04-23 17:08:44 +0900993 struct mmc_host *host = card->host;
994 unsigned idx, bus_width = 0;
995 int err = 0;
Girish K Sa4924c72012-01-11 14:04:52 -0500996
Alexandre Belloni1c2d26e2014-12-17 19:32:06 +0100997 if (!mmc_can_ext_csd(card) ||
Seungwon Jeon577fb132014-04-23 17:08:44 +0900998 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
999 return 0;
Girish K Sa4924c72012-01-11 14:04:52 -05001000
Seungwon Jeon577fb132014-04-23 17:08:44 +09001001 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
Girish K Sa4924c72012-01-11 14:04:52 -05001002
1003 /*
1004 * Unlike SD, MMC cards dont have a configuration register to notify
1005 * supported bus width. So bus test command should be run to identify
1006 * the supported bus width or compare the ext csd values of current
1007 * bus width and ext csd values of 1 bit mode read earlier.
1008 */
Seungwon Jeon577fb132014-04-23 17:08:44 +09001009 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
Girish K Sa4924c72012-01-11 14:04:52 -05001010 /*
1011 * Host is capable of 8bit transfer, then switch
1012 * the device to work in 8bit transfer mode. If the
1013 * mmc switch command returns error then switch to
1014 * 4bit transfer mode. On success set the corresponding
1015 * bus width on the host.
1016 */
1017 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1018 EXT_CSD_BUS_WIDTH,
1019 ext_csd_bits[idx],
1020 card->ext_csd.generic_cmd6_time);
1021 if (err)
1022 continue;
1023
Seungwon Jeon577fb132014-04-23 17:08:44 +09001024 bus_width = bus_widths[idx];
1025 mmc_set_bus_width(host, bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -05001026
Seungwon Jeon577fb132014-04-23 17:08:44 +09001027 /*
1028 * If controller can't handle bus width test,
1029 * compare ext_csd previously read in 1 bit mode
1030 * against ext_csd at new bus width
1031 */
Girish K Sa4924c72012-01-11 14:04:52 -05001032 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
Seungwon Jeon577fb132014-04-23 17:08:44 +09001033 err = mmc_compare_ext_csds(card, bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -05001034 else
Seungwon Jeon577fb132014-04-23 17:08:44 +09001035 err = mmc_bus_test(card, bus_width);
1036
1037 if (!err) {
1038 err = bus_width;
Girish K Sa4924c72012-01-11 14:04:52 -05001039 break;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001040 } else {
1041 pr_warn("%s: switch to bus width %d failed\n",
Wolfram Sanged9feec2016-01-29 09:27:50 +01001042 mmc_hostname(host), 1 << bus_width);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001043 }
Girish K Sa4924c72012-01-11 14:04:52 -05001044 }
1045
Seungwon Jeon577fb132014-04-23 17:08:44 +09001046 return err;
1047}
1048
1049/*
1050 * Switch to the high-speed mode
1051 */
1052static int mmc_select_hs(struct mmc_card *card)
1053{
1054 int err;
1055
1056 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1057 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
Ulf Hansson53e60652016-11-09 19:40:29 +01001058 card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
1059 true, true, true);
Jungseung Lee67d35962016-08-24 19:34:09 +09001060 if (err)
1061 pr_warn("%s: switch to high-speed failed, err:%d\n",
1062 mmc_hostname(card->host), err);
1063
Seungwon Jeon577fb132014-04-23 17:08:44 +09001064 return err;
1065}
1066
1067/*
1068 * Activate wide bus and DDR if supported.
1069 */
1070static int mmc_select_hs_ddr(struct mmc_card *card)
1071{
1072 struct mmc_host *host = card->host;
1073 u32 bus_width, ext_csd_bits;
1074 int err = 0;
1075
1076 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1077 return 0;
1078
1079 bus_width = host->ios.bus_width;
1080 if (bus_width == MMC_BUS_WIDTH_1)
1081 return 0;
1082
1083 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1084 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1085
Ulf Hanssone173f892016-11-09 21:00:26 +01001086 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1087 EXT_CSD_BUS_WIDTH,
1088 ext_csd_bits,
1089 card->ext_csd.generic_cmd6_time,
1090 MMC_TIMING_MMC_DDR52,
1091 true, true, true);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001092 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001093 pr_err("%s: switch to bus width %d ddr failed\n",
Seungwon Jeon577fb132014-04-23 17:08:44 +09001094 mmc_hostname(host), 1 << bus_width);
1095 return err;
1096 }
1097
1098 /*
1099 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1100 * signaling.
1101 *
1102 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1103 *
1104 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1105 * in the JEDEC spec for DDR.
1106 *
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001107 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1108 * host controller can support this, like some of the SDHCI
1109 * controller which connect to an eMMC device. Some of these
1110 * host controller still needs to use 1.8v vccq for supporting
1111 * DDR mode.
1112 *
1113 * So the sequence will be:
1114 * if (host and device can both support 1.2v IO)
1115 * use 1.2v IO;
1116 * else if (host and device can both support 1.8v IO)
1117 * use 1.8v IO;
1118 * so if host and device can only support 3.3v IO, this is the
1119 * last choice.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001120 *
1121 * WARNING: eMMC rules are NOT the same as SD DDR
1122 */
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001123 err = -EINVAL;
1124 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1125 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001126
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001127 if (err && (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V))
1128 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1129
1130 /* make sure vccq is 3.3v after switching disaster */
1131 if (err)
1132 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1133
Seungwon Jeon577fb132014-04-23 17:08:44 +09001134 return err;
1135}
1136
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001137static int mmc_select_hs400(struct mmc_card *card)
1138{
1139 struct mmc_host *host = card->host;
Adrian Hunter51b12f72015-10-28 14:25:41 +02001140 unsigned int max_dtr;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001141 int err = 0;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001142 u8 val;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001143
1144 /*
1145 * HS400 mode requires 8-bit bus width
1146 */
1147 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1148 host->ios.bus_width == MMC_BUS_WIDTH_8))
1149 return 0;
1150
Adrian Hunter51b12f72015-10-28 14:25:41 +02001151 /* Switch card to HS mode */
Wenkai Duadb24d42015-11-26 14:00:44 +02001152 val = EXT_CSD_TIMING_HS;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001153 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001154 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001155 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001156 true, false, true);
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001157 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001158 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001159 mmc_hostname(host), err);
1160 return err;
1161 }
1162
Adrian Hunter51b12f72015-10-28 14:25:41 +02001163 /* Set host controller to HS timing */
1164 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1165
Ziyuan Xu649c6052016-05-26 13:50:32 +08001166 /* Reduce frequency to HS frequency */
1167 max_dtr = card->ext_csd.hs_max_dtr;
1168 mmc_set_clock(host, max_dtr);
1169
Chaotian Jing08573ea2016-05-19 16:47:41 +08001170 err = mmc_switch_status(card);
1171 if (err)
1172 goto out_err;
Adrian Hunterd2302932015-10-28 14:25:43 +02001173
1174 /* Switch card to DDR */
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001175 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1176 EXT_CSD_BUS_WIDTH,
1177 EXT_CSD_DDR_BUS_WIDTH_8,
1178 card->ext_csd.generic_cmd6_time);
1179 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001180 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001181 mmc_hostname(host), err);
1182 return err;
1183 }
1184
Adrian Hunterd2302932015-10-28 14:25:43 +02001185 /* Switch card to HS400 */
Adrian Huntercc4f4142015-02-06 14:12:58 +02001186 val = EXT_CSD_TIMING_HS400 |
1187 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001188 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001189 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001190 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001191 true, false, true);
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001192 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001193 pr_err("%s: switch to hs400 failed, err:%d\n",
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001194 mmc_hostname(host), err);
1195 return err;
1196 }
1197
Adrian Hunterd2302932015-10-28 14:25:43 +02001198 /* Set host controller to HS400 timing and frequency */
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001199 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1200 mmc_set_bus_speed(card);
1201
Chaotian Jing08573ea2016-05-19 16:47:41 +08001202 err = mmc_switch_status(card);
1203 if (err)
1204 goto out_err;
Adrian Hunterd2302932015-10-28 14:25:43 +02001205
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001206 return 0;
Adrian Hunterd2302932015-10-28 14:25:43 +02001207
1208out_err:
1209 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1210 __func__, err);
1211 return err;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001212}
1213
Adrian Hunter6376f692015-05-07 13:10:20 +03001214int mmc_hs200_to_hs400(struct mmc_card *card)
1215{
1216 return mmc_select_hs400(card);
1217}
1218
Adrian Hunter6376f692015-05-07 13:10:20 +03001219int mmc_hs400_to_hs200(struct mmc_card *card)
1220{
1221 struct mmc_host *host = card->host;
Adrian Hunter6376f692015-05-07 13:10:20 +03001222 unsigned int max_dtr;
1223 int err;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001224 u8 val;
Adrian Hunter6376f692015-05-07 13:10:20 +03001225
Adrian Hunter6376f692015-05-07 13:10:20 +03001226 /* Reduce frequency to HS */
1227 max_dtr = card->ext_csd.hs_max_dtr;
1228 mmc_set_clock(host, max_dtr);
1229
1230 /* Switch HS400 to HS DDR */
Wenkai Duadb24d42015-11-26 14:00:44 +02001231 val = EXT_CSD_TIMING_HS;
Adrian Hunter6376f692015-05-07 13:10:20 +03001232 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001233 val, card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001234 true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001235 if (err)
1236 goto out_err;
1237
1238 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1239
Chaotian Jing08573ea2016-05-19 16:47:41 +08001240 err = mmc_switch_status(card);
1241 if (err)
1242 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001243
1244 /* Switch HS DDR to HS */
1245 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1246 EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001247 0, 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_HS);
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 to HS200 */
Adrian Huntercc4f4142015-02-06 14:12:58 +02001258 val = EXT_CSD_TIMING_HS200 |
1259 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Adrian Hunter6376f692015-05-07 13:10:20 +03001260 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001261 val, card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001262 true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001263 if (err)
1264 goto out_err;
1265
1266 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1267
Adrian Hunteref3d2322016-12-02 13:16:35 +02001268 /*
1269 * For HS200, CRC errors are not a reliable way to know the switch
1270 * failed. If there really is a problem, we would expect tuning will
1271 * fail and the result ends up the same.
1272 */
1273 err = __mmc_switch_status(card, false);
Chaotian Jing08573ea2016-05-19 16:47:41 +08001274 if (err)
1275 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001276
1277 mmc_set_bus_speed(card);
1278
1279 return 0;
1280
1281out_err:
1282 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1283 __func__, err);
1284 return err;
1285}
1286
Shawn Lin81ac2af2016-05-26 09:56:22 +08001287static int mmc_select_hs400es(struct mmc_card *card)
1288{
1289 struct mmc_host *host = card->host;
1290 int err = 0;
1291 u8 val;
1292
1293 if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1294 err = -ENOTSUPP;
1295 goto out_err;
1296 }
1297
Shawn Lin1720d352016-09-30 14:18:58 +08001298 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
1299 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1300
1301 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
1302 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1303
1304 /* If fails try again during next card power cycle */
1305 if (err)
1306 goto out_err;
1307
Shawn Lin81ac2af2016-05-26 09:56:22 +08001308 err = mmc_select_bus_width(card);
1309 if (err < 0)
1310 goto out_err;
1311
1312 /* Switch card to HS mode */
Ulf Hansson53e60652016-11-09 19:40:29 +01001313 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1314 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1315 card->ext_csd.generic_cmd6_time, 0,
1316 true, false, true);
1317 if (err) {
1318 pr_err("%s: switch to hs for hs400es failed, err:%d\n",
1319 mmc_hostname(host), err);
1320 goto out_err;
1321 }
1322
1323 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1324 err = mmc_switch_status(card);
Jungseung Lee67d35962016-08-24 19:34:09 +09001325 if (err)
Shawn Lin81ac2af2016-05-26 09:56:22 +08001326 goto out_err;
Shawn Lin81ac2af2016-05-26 09:56:22 +08001327
Shawn Lin4f255802016-09-30 14:18:59 +08001328 mmc_set_clock(host, card->ext_csd.hs_max_dtr);
1329
Shawn Lin81ac2af2016-05-26 09:56:22 +08001330 /* Switch card to DDR with strobe bit */
1331 val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1332 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1333 EXT_CSD_BUS_WIDTH,
1334 val,
1335 card->ext_csd.generic_cmd6_time);
1336 if (err) {
1337 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1338 mmc_hostname(host), err);
1339 goto out_err;
1340 }
1341
1342 /* Switch card to HS400 */
1343 val = EXT_CSD_TIMING_HS400 |
1344 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1345 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1346 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001347 card->ext_csd.generic_cmd6_time, 0,
Shawn Lin81ac2af2016-05-26 09:56:22 +08001348 true, false, true);
1349 if (err) {
1350 pr_err("%s: switch to hs400es failed, err:%d\n",
1351 mmc_hostname(host), err);
1352 goto out_err;
1353 }
1354
1355 /* Set host controller to HS400 timing and frequency */
1356 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1357
1358 /* Controller enable enhanced strobe function */
1359 host->ios.enhanced_strobe = true;
1360 if (host->ops->hs400_enhanced_strobe)
1361 host->ops->hs400_enhanced_strobe(host, &host->ios);
1362
1363 err = mmc_switch_status(card);
1364 if (err)
1365 goto out_err;
1366
1367 return 0;
1368
1369out_err:
1370 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1371 __func__, err);
1372 return err;
1373}
1374
Adrian Huntercc4f4142015-02-06 14:12:58 +02001375static void mmc_select_driver_type(struct mmc_card *card)
1376{
1377 int card_drv_type, drive_strength, drv_type;
1378
1379 card_drv_type = card->ext_csd.raw_driver_strength |
1380 mmc_driver_type_mask(0);
1381
1382 drive_strength = mmc_select_drive_strength(card,
1383 card->ext_csd.hs200_max_dtr,
1384 card_drv_type, &drv_type);
1385
1386 card->drive_strength = drive_strength;
1387
1388 if (drv_type)
1389 mmc_set_driver_type(card->host, drv_type);
1390}
1391
Seungwon Jeon577fb132014-04-23 17:08:44 +09001392/*
1393 * For device supporting HS200 mode, the following sequence
1394 * should be done before executing the tuning process.
1395 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1396 * 2. switch to HS200 mode
1397 * 3. set the clock to > 52Mhz and <=200MHz
1398 */
1399static int mmc_select_hs200(struct mmc_card *card)
1400{
1401 struct mmc_host *host = card->host;
Dong Aishenge51534c2016-04-21 00:51:30 +08001402 unsigned int old_timing, old_signal_voltage;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001403 int err = -EINVAL;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001404 u8 val;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001405
Dong Aishenge51534c2016-04-21 00:51:30 +08001406 old_signal_voltage = host->ios.signal_voltage;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001407 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1408 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1409
1410 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1411 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1412
1413 /* If fails try again during next card power cycle */
1414 if (err)
Dong Aishenge51534c2016-04-21 00:51:30 +08001415 return err;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001416
Adrian Huntercc4f4142015-02-06 14:12:58 +02001417 mmc_select_driver_type(card);
1418
Seungwon Jeon577fb132014-04-23 17:08:44 +09001419 /*
1420 * Set the bus width(4 or 8) with host's support and
1421 * switch to HS200 mode if bus width is set successfully.
1422 */
1423 err = mmc_select_bus_width(card);
Peter Griffin8b7be8f2016-06-08 11:27:52 +01001424 if (err > 0) {
Adrian Huntercc4f4142015-02-06 14:12:58 +02001425 val = EXT_CSD_TIMING_HS200 |
1426 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Ulf Hansson4509f84772014-01-08 16:09:33 +01001427 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001428 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001429 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001430 true, false, true);
Adrian Hunter1815e612015-10-28 14:25:40 +02001431 if (err)
1432 goto err;
1433 old_timing = host->ios.timing;
1434 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
Chaotian Jing08573ea2016-05-19 16:47:41 +08001435
Adrian Hunteref3d2322016-12-02 13:16:35 +02001436 /*
1437 * For HS200, CRC errors are not a reliable way to know the
1438 * switch failed. If there really is a problem, we would expect
1439 * tuning will fail and the result ends up the same.
1440 */
1441 err = __mmc_switch_status(card, false);
1442
Chaotian Jing08573ea2016-05-19 16:47:41 +08001443 /*
1444 * mmc_select_timing() assumes timing has not changed if
1445 * it is a switch error.
1446 */
1447 if (err == -EBADMSG)
1448 mmc_set_timing(host, old_timing);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001449 }
Girish K Sa4924c72012-01-11 14:04:52 -05001450err:
Dong Aishenge51534c2016-04-21 00:51:30 +08001451 if (err) {
1452 /* fall back to the old signal voltage, if fails report error */
1453 if (__mmc_set_signal_voltage(host, old_signal_voltage))
1454 err = -EIO;
1455
Adrian Hunter1815e612015-10-28 14:25:40 +02001456 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1457 __func__, err);
Dong Aishenge51534c2016-04-21 00:51:30 +08001458 }
Girish K Sa4924c72012-01-11 14:04:52 -05001459 return err;
1460}
1461
1462/*
Shawn Lin81ac2af2016-05-26 09:56:22 +08001463 * Activate High Speed, HS200 or HS400ES mode if supported.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001464 */
1465static int mmc_select_timing(struct mmc_card *card)
1466{
1467 int err = 0;
1468
Ulf Hansson148bcab2014-10-20 11:33:53 +02001469 if (!mmc_can_ext_csd(card))
Seungwon Jeon577fb132014-04-23 17:08:44 +09001470 goto bus_speed;
1471
Shawn Lin81ac2af2016-05-26 09:56:22 +08001472 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1473 err = mmc_select_hs400es(card);
1474 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
Seungwon Jeon577fb132014-04-23 17:08:44 +09001475 err = mmc_select_hs200(card);
1476 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1477 err = mmc_select_hs(card);
1478
1479 if (err && err != -EBADMSG)
1480 return err;
1481
Seungwon Jeon577fb132014-04-23 17:08:44 +09001482bus_speed:
1483 /*
1484 * Set the bus speed to the selected bus timing.
1485 * If timing is not selected, backward compatible is the default.
1486 */
1487 mmc_set_bus_speed(card);
Dong Aisheng0400ed02016-04-21 00:51:31 +08001488 return 0;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001489}
1490
1491/*
1492 * Execute tuning sequence to seek the proper bus operating
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001493 * conditions for HS200 and HS400, which sends CMD21 to the device.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001494 */
1495static int mmc_hs200_tuning(struct mmc_card *card)
1496{
1497 struct mmc_host *host = card->host;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001498
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001499 /*
1500 * Timing should be adjusted to the HS400 target
1501 * operation frequency for tuning process
1502 */
1503 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1504 host->ios.bus_width == MMC_BUS_WIDTH_8)
1505 if (host->ops->prepare_hs400_tuning)
1506 host->ops->prepare_hs400_tuning(host, &host->ios);
1507
Adrian Hunter63e415c2014-12-05 19:40:59 +02001508 return mmc_execute_tuning(card);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001509}
1510
1511/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001512 * Handle the detection and initialisation of a card.
1513 *
Deepak Saxena87693922008-06-16 19:20:57 -07001514 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001515 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001516 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001517static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001518 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001519{
1520 struct mmc_card *card;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001521 int err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001522 u32 cid[4];
Philip Rakityb676f032011-02-13 23:13:09 -08001523 u32 rocr;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001524
Pierre Ossmand84075c82007-08-09 13:23:56 +02001525 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001526
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001527 /* Set correct bus mode for MMC before attempting init */
1528 if (!mmc_host_is_spi(host))
1529 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1530
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001531 /*
1532 * Since we're changing the OCR value, we seem to
1533 * need to tell some cards to go back to the idle
1534 * state. We wait 1ms to give cards time to
1535 * respond.
Balaji T Kc3805462011-09-08 22:08:39 +05301536 * mmc_go_idle is needed for eMMC that are asleep
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001537 */
1538 mmc_go_idle(host);
1539
1540 /* The extra bit indicates that we support high capacity */
Philip Rakityb676f032011-02-13 23:13:09 -08001541 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +02001542 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001543 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001544
1545 /*
David Brownellaf517152007-08-08 09:11:32 -07001546 * For SPI, enable CRC as appropriate.
1547 */
1548 if (mmc_host_is_spi(host)) {
1549 err = mmc_spi_set_crc(host, use_spi_crc);
1550 if (err)
1551 goto err;
1552 }
1553
1554 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001555 * Fetch CID from card.
1556 */
David Brownellaf517152007-08-08 09:11:32 -07001557 if (mmc_host_is_spi(host))
1558 err = mmc_send_cid(host, cid);
1559 else
1560 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +02001561 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001562 goto err;
1563
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001564 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001565 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1566 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001567 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001568 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001569
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001570 card = oldcard;
1571 } else {
1572 /*
1573 * Allocate card structure.
1574 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +01001575 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001576 if (IS_ERR(card)) {
1577 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001578 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001579 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001580
Ulf Hansson69041152013-09-13 11:31:33 +02001581 card->ocr = ocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001582 card->type = MMC_TYPE_MMC;
1583 card->rca = 1;
1584 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1585 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001586
1587 /*
Doug Andersoneac86322014-12-02 15:42:45 -08001588 * Call the optional HC's init_card function to handle quirks.
1589 */
1590 if (host->ops->init_card)
1591 host->ops->init_card(host, card);
1592
1593 /*
David Brownellaf517152007-08-08 09:11:32 -07001594 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001595 */
David Brownellaf517152007-08-08 09:11:32 -07001596 if (!mmc_host_is_spi(host)) {
1597 err = mmc_set_relative_addr(card);
1598 if (err)
1599 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001600
David Brownellaf517152007-08-08 09:11:32 -07001601 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1602 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001603
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001604 if (!oldcard) {
1605 /*
1606 * Fetch CSD from card.
1607 */
1608 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001609 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001610 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001611
Pierre Ossmanbd766312007-05-01 16:11:57 +02001612 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001613 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001614 goto free_card;
1615 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001616 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001617 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001618 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001619
1620 /*
Sascha Hauer3d705d12014-08-19 10:45:51 +02001621 * handling only for cards supporting DSR and hosts requesting
1622 * DSR configuration
1623 */
1624 if (card->csd.dsr_imp && host->dsr_req)
1625 mmc_set_dsr(host);
1626
1627 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001628 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001629 */
David Brownellaf517152007-08-08 09:11:32 -07001630 if (!mmc_host_is_spi(host)) {
1631 err = mmc_select_card(card);
1632 if (err)
1633 goto free_card;
1634 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001635
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001636 if (!oldcard) {
Ulf Hansson076ec382014-10-20 13:37:24 +02001637 /* Read extended CSD. */
1638 err = mmc_read_ext_csd(card);
Pierre Ossman17b04292007-07-22 22:18:46 +02001639 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001640 goto free_card;
Philip Rakityb676f032011-02-13 23:13:09 -08001641
Masahiro Yamada87e88652016-04-15 20:16:12 +09001642 /*
1643 * If doing byte addressing, check if required to do sector
Philip Rakityb676f032011-02-13 23:13:09 -08001644 * addressing. Handle the case of <2GB cards needing sector
1645 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1646 * ocr register has bit 30 set for sector addressing.
1647 */
Masahiro Yamada87e88652016-04-15 20:16:12 +09001648 if (rocr & BIT(30))
Philip Rakityb676f032011-02-13 23:13:09 -08001649 mmc_card_set_blockaddr(card);
1650
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001651 /* Erase size depends on CSD and Extended CSD */
1652 mmc_set_erase_size(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001653 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001654
1655 /*
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001656 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001657 * bit. This bit will be lost every time after a reset or power off.
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001658 */
Grégory Soutadé69803d42014-09-15 17:47:09 +02001659 if (card->ext_csd.partition_setting_completed ||
Adrian Hunter83bb24a2012-03-13 05:19:13 -04001660 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001661 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001662 EXT_CSD_ERASE_GROUP_DEF, 1,
1663 card->ext_csd.generic_cmd6_time);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001664
1665 if (err && err != -EBADMSG)
1666 goto free_card;
1667
1668 if (err) {
1669 err = 0;
1670 /*
1671 * Just disable enhanced area off & sz
1672 * will try to enable ERASE_GROUP_DEF
1673 * during next time reinit
1674 */
1675 card->ext_csd.enhanced_area_offset = -EINVAL;
1676 card->ext_csd.enhanced_area_size = -EINVAL;
1677 } else {
1678 card->ext_csd.erase_group_def = 1;
1679 /*
1680 * enable ERASE_GRP_DEF successfully.
1681 * This will affect the erase size, so
1682 * here need to reset erase size
1683 */
1684 mmc_set_erase_size(card);
1685 }
1686 }
1687
1688 /*
Philip Rakity41e2a482011-03-19 14:10:33 -04001689 * Ensure eMMC user default partition is enabled
1690 */
Andrei Warkentin371a6892011-04-11 18:10:25 -05001691 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1692 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1693 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1694 card->ext_csd.part_config,
1695 card->ext_csd.part_time);
1696 if (err && err != -EBADMSG)
1697 goto free_card;
Philip Rakity41e2a482011-03-19 14:10:33 -04001698 }
1699
1700 /*
Ulf Hansson432356792013-06-10 17:03:45 +02001701 * Enable power_off_notification byte in the ext_csd register
Girish K Sbec87262011-10-13 12:04:16 +05301702 */
Ulf Hansson432356792013-06-10 17:03:45 +02001703 if (card->ext_csd.rev >= 6) {
Girish K Sbec87262011-10-13 12:04:16 +05301704 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1705 EXT_CSD_POWER_OFF_NOTIFICATION,
1706 EXT_CSD_POWER_ON,
1707 card->ext_csd.generic_cmd6_time);
1708 if (err && err != -EBADMSG)
1709 goto free_card;
Girish K Sbec87262011-10-13 12:04:16 +05301710
Girish K S96a85d52011-11-04 16:22:47 +05301711 /*
1712 * The err can be -EBADMSG or 0,
1713 * so check for success and update the flag
1714 */
1715 if (!err)
Ulf Hanssone6c08582012-10-05 12:45:39 -04001716 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
Girish K S96a85d52011-11-04 16:22:47 +05301717 }
Girish K Sbec87262011-10-13 12:04:16 +05301718
1719 /*
Seungwon Jeon577fb132014-04-23 17:08:44 +09001720 * Select timing interface
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001721 */
Seungwon Jeon577fb132014-04-23 17:08:44 +09001722 err = mmc_select_timing(card);
1723 if (err)
1724 goto free_card;
Girish K Sa4924c72012-01-11 14:04:52 -05001725
Girish K Sa4924c72012-01-11 14:04:52 -05001726 if (mmc_card_hs200(card)) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001727 err = mmc_hs200_tuning(card);
1728 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001729 goto free_card;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001730
1731 err = mmc_select_hs400(card);
1732 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001733 goto free_card;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001734 } else if (mmc_card_hs(card)) {
1735 /* Select the desired bus width optionally */
1736 err = mmc_select_bus_width(card);
Peter Griffin8b7be8f2016-06-08 11:27:52 +01001737 if (err > 0) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001738 err = mmc_select_hs_ddr(card);
1739 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001740 goto free_card;
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001741 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001742 }
1743
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001744 /*
Seungwon Jeon23850492014-04-23 17:08:05 +09001745 * Choose the power class with selected bus interface
1746 */
1747 mmc_select_powerclass(card);
1748
1749 /*
Subhash Jadavani52d09742012-03-06 17:59:12 +05301750 * Enable HPI feature (if supported)
1751 */
1752 if (card->ext_csd.hpi) {
1753 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1754 EXT_CSD_HPI_MGMT, 1,
1755 card->ext_csd.generic_cmd6_time);
1756 if (err && err != -EBADMSG)
1757 goto free_card;
1758 if (err) {
Joe Perches66061102014-09-12 14:56:56 -07001759 pr_warn("%s: Enabling HPI failed\n",
1760 mmc_hostname(card->host));
Subhash Jadavani52d09742012-03-06 17:59:12 +05301761 err = 0;
1762 } else
1763 card->ext_csd.hpi_en = 1;
1764 }
1765
1766 /*
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001767 * If cache size is higher than 0, this indicates
1768 * the existence of cache and it can be turned on.
1769 */
Pratibhasagar V53202262016-06-09 18:09:31 -04001770 if (!mmc_card_broken_hpi(card) &&
1771 card->ext_csd.cache_size > 0) {
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001772 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001773 EXT_CSD_CACHE_CTRL, 1,
1774 card->ext_csd.generic_cmd6_time);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001775 if (err && err != -EBADMSG)
1776 goto free_card;
1777
1778 /*
1779 * Only if no error, cache is turned on successfully.
1780 */
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001781 if (err) {
Joe Perches66061102014-09-12 14:56:56 -07001782 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1783 mmc_hostname(card->host), err);
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001784 card->ext_csd.cache_ctrl = 0;
1785 err = 0;
1786 } else {
1787 card->ext_csd.cache_ctrl = 1;
1788 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001789 }
1790
Seungwon Jeonabd9ac12013-02-06 17:01:43 +09001791 /*
1792 * The mandatory minimum values are defined for packed command.
1793 * read: 5, write: 3
1794 */
1795 if (card->ext_csd.max_packed_writes >= 3 &&
1796 card->ext_csd.max_packed_reads >= 5 &&
1797 host->caps2 & MMC_CAP2_PACKED_CMD) {
1798 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1799 EXT_CSD_EXP_EVENTS_CTRL,
1800 EXT_CSD_PACKED_EVENT_EN,
1801 card->ext_csd.generic_cmd6_time);
1802 if (err && err != -EBADMSG)
1803 goto free_card;
1804 if (err) {
1805 pr_warn("%s: Enabling packed event failed\n",
1806 mmc_hostname(card->host));
1807 card->ext_csd.packed_event_en = 0;
1808 err = 0;
1809 } else {
1810 card->ext_csd.packed_event_en = 1;
1811 }
1812 }
1813
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001814 if (!oldcard)
1815 host->card = card;
1816
Pierre Ossman17b04292007-07-22 22:18:46 +02001817 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001818
1819free_card:
1820 if (!oldcard)
1821 mmc_remove_card(card);
1822err:
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001823 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001824}
1825
Ulf Hansson07a68212013-04-19 15:12:11 +02001826static int mmc_can_sleep(struct mmc_card *card)
1827{
1828 return (card && card->ext_csd.rev >= 3);
1829}
1830
1831static int mmc_sleep(struct mmc_host *host)
1832{
Masahiro Yamadac7836d12016-12-19 20:51:18 +09001833 struct mmc_command cmd = {};
Ulf Hansson07a68212013-04-19 15:12:11 +02001834 struct mmc_card *card = host->card;
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001835 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
Ulf Hansson07a68212013-04-19 15:12:11 +02001836 int err;
1837
Adrian Hunter436f8da2015-05-07 13:10:18 +03001838 /* Re-tuning can't be done once the card is deselected */
1839 mmc_retune_hold(host);
1840
Ulf Hansson07a68212013-04-19 15:12:11 +02001841 err = mmc_deselect_cards(host);
1842 if (err)
Adrian Hunter436f8da2015-05-07 13:10:18 +03001843 goto out_release;
Ulf Hansson07a68212013-04-19 15:12:11 +02001844
1845 cmd.opcode = MMC_SLEEP_AWAKE;
1846 cmd.arg = card->rca << 16;
1847 cmd.arg |= 1 << 15;
1848
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001849 /*
1850 * If the max_busy_timeout of the host is specified, validate it against
1851 * the sleep cmd timeout. A failure means we need to prevent the host
1852 * from doing hw busy detection, which is done by converting to a R1
1853 * response instead of a R1B.
1854 */
1855 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1856 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1857 } else {
1858 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1859 cmd.busy_timeout = timeout_ms;
1860 }
1861
Ulf Hansson07a68212013-04-19 15:12:11 +02001862 err = mmc_wait_for_cmd(host, &cmd, 0);
1863 if (err)
Adrian Hunter436f8da2015-05-07 13:10:18 +03001864 goto out_release;
Ulf Hansson07a68212013-04-19 15:12:11 +02001865
1866 /*
1867 * If the host does not wait while the card signals busy, then we will
1868 * will have to wait the sleep/awake timeout. Note, we cannot use the
1869 * SEND_STATUS command to poll the status because that command (and most
1870 * others) is invalid while the card sleeps.
1871 */
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001872 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1873 mmc_delay(timeout_ms);
Ulf Hansson07a68212013-04-19 15:12:11 +02001874
Adrian Hunter436f8da2015-05-07 13:10:18 +03001875out_release:
1876 mmc_retune_release(host);
Ulf Hansson07a68212013-04-19 15:12:11 +02001877 return err;
1878}
1879
Ulf Hanssone6c08582012-10-05 12:45:39 -04001880static int mmc_can_poweroff_notify(const struct mmc_card *card)
1881{
1882 return card &&
1883 mmc_card_mmc(card) &&
1884 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1885}
1886
1887static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1888{
1889 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1890 int err;
1891
1892 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1893 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1894 timeout = card->ext_csd.power_off_longtime;
1895
Ulf Hansson878e2002013-09-09 11:57:57 +02001896 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1897 EXT_CSD_POWER_OFF_NOTIFICATION,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001898 notify_type, timeout, 0, true, false, false);
Ulf Hanssone6c08582012-10-05 12:45:39 -04001899 if (err)
1900 pr_err("%s: Power Off Notification timed out, %u\n",
1901 mmc_hostname(card->host), timeout);
1902
1903 /* Disable the power off notification after the switch operation. */
1904 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1905
1906 return err;
1907}
1908
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001909/*
1910 * Host is being removed. Free up the current card.
1911 */
1912static void mmc_remove(struct mmc_host *host)
1913{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001914 mmc_remove_card(host->card);
1915 host->card = NULL;
1916}
1917
1918/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001919 * Card detection - card is alive.
1920 */
1921static int mmc_alive(struct mmc_host *host)
1922{
1923 return mmc_send_status(host->card, NULL);
1924}
1925
1926/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001927 * Card detection callback from host.
1928 */
1929static void mmc_detect(struct mmc_host *host)
1930{
1931 int err;
1932
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001933 mmc_get_card(host->card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001934
1935 /*
1936 * Just check if our card has been removed.
1937 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001938 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001939
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001940 mmc_put_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001941
Pierre Ossman17b04292007-07-22 22:18:46 +02001942 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001943 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001944
1945 mmc_claim_host(host);
1946 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001947 mmc_power_off(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001948 mmc_release_host(host);
1949 }
1950}
1951
Ulf Hansson03d071f2013-06-10 17:03:43 +02001952static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001953{
Balaji T Kc3805462011-09-08 22:08:39 +05301954 int err = 0;
Ulf Hansson03d071f2013-06-10 17:03:43 +02001955 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1956 EXT_CSD_POWER_OFF_LONG;
Balaji T Kc3805462011-09-08 22:08:39 +05301957
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001958 mmc_claim_host(host);
Maya Erez881d9262013-01-28 16:44:22 -05001959
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001960 if (mmc_card_suspended(host->card))
1961 goto out;
1962
Ulf Hansson39b94312013-05-02 14:02:36 +02001963 if (mmc_card_doing_bkops(host->card)) {
1964 err = mmc_stop_bkops(host->card);
1965 if (err)
1966 goto out;
1967 }
1968
Ulf Hansson10e5d962013-12-16 16:23:22 +01001969 err = mmc_flush_cache(host->card);
Maya Erez881d9262013-01-28 16:44:22 -05001970 if (err)
1971 goto out;
1972
Ulf Hansson432356792013-06-10 17:03:45 +02001973 if (mmc_can_poweroff_notify(host->card) &&
Ulf Hansson53275c22013-06-10 17:03:46 +02001974 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
Ulf Hansson03d071f2013-06-10 17:03:43 +02001975 err = mmc_poweroff_notify(host->card, notify_type);
Ulf Hansson07a68212013-04-19 15:12:11 +02001976 else if (mmc_can_sleep(host->card))
1977 err = mmc_sleep(host);
Ulf Hanssone6c08582012-10-05 12:45:39 -04001978 else if (!mmc_host_is_spi(host))
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001979 err = mmc_deselect_cards(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001980
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001981 if (!err) {
Ulf Hansson74590262013-06-10 17:03:38 +02001982 mmc_power_off(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001983 mmc_card_set_suspended(host->card);
1984 }
Maya Erez881d9262013-01-28 16:44:22 -05001985out:
1986 mmc_release_host(host);
Balaji T Kc3805462011-09-08 22:08:39 +05301987 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001988}
1989
1990/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001991 * Suspend callback
Ulf Hansson03d071f2013-06-10 17:03:43 +02001992 */
1993static int mmc_suspend(struct mmc_host *host)
1994{
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001995 int err;
1996
1997 err = _mmc_suspend(host, true);
1998 if (!err) {
1999 pm_runtime_disable(&host->card->dev);
2000 pm_runtime_set_suspended(&host->card->dev);
2001 }
2002
2003 return err;
Ulf Hansson03d071f2013-06-10 17:03:43 +02002004}
2005
2006/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002007 * This function tries to determine if the same card is still present
2008 * and, if so, restore all state to it.
2009 */
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002010static int _mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002011{
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002012 int err = 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002013
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002014 mmc_claim_host(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002015
2016 if (!mmc_card_suspended(host->card))
2017 goto out;
2018
Ulf Hansson69041152013-09-13 11:31:33 +02002019 mmc_power_up(host, host->card->ocr);
Ulf Hansson69041152013-09-13 11:31:33 +02002020 err = mmc_init_card(host, host->card->ocr, host->card);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002021 mmc_card_clr_suspended(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002022
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002023out:
2024 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002025 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002026}
2027
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002028/*
2029 * Shutdown callback
2030 */
2031static int mmc_shutdown(struct mmc_host *host)
2032{
2033 int err = 0;
2034
2035 /*
2036 * In a specific case for poweroff notify, we need to resume the card
2037 * before we can shutdown it properly.
2038 */
2039 if (mmc_can_poweroff_notify(host->card) &&
2040 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002041 err = _mmc_resume(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002042
2043 if (!err)
2044 err = _mmc_suspend(host, false);
2045
2046 return err;
2047}
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002048
2049/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002050 * Callback for resume.
2051 */
2052static int mmc_resume(struct mmc_host *host)
2053{
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002054 pm_runtime_enable(&host->card->dev);
Ulf Hanssonc29536e2015-11-05 16:01:32 +01002055 return 0;
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002056}
2057
2058/*
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002059 * Callback for runtime_suspend.
2060 */
2061static int mmc_runtime_suspend(struct mmc_host *host)
2062{
2063 int err;
2064
2065 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2066 return 0;
2067
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002068 err = _mmc_suspend(host, true);
Ulf Hansson0cc81a82013-10-03 11:24:44 +02002069 if (err)
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002070 pr_err("%s: error %d doing aggressive suspend\n",
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002071 mmc_hostname(host), err);
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002072
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002073 return err;
2074}
2075
2076/*
2077 * Callback for runtime_resume.
2078 */
2079static int mmc_runtime_resume(struct mmc_host *host)
2080{
2081 int err;
2082
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002083 err = _mmc_resume(host);
Adrian Hunter520322d2015-12-14 15:51:27 +02002084 if (err && err != -ENOMEDIUM)
Ulf Hanssonc29536e2015-11-05 16:01:32 +01002085 pr_err("%s: error %d doing runtime resume\n",
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002086 mmc_hostname(host), err);
2087
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002088 return 0;
2089}
2090
Johan Rudholmf855a372015-01-12 15:38:05 +01002091int mmc_can_reset(struct mmc_card *card)
2092{
2093 u8 rst_n_function;
2094
2095 rst_n_function = card->ext_csd.rst_n_function;
2096 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2097 return 0;
2098 return 1;
2099}
2100EXPORT_SYMBOL(mmc_can_reset);
2101
2102static int mmc_reset(struct mmc_host *host)
2103{
2104 struct mmc_card *card = host->card;
Johan Rudholmf855a372015-01-12 15:38:05 +01002105
Adrian Hunter437db4c2016-05-04 10:38:21 +03002106 /*
2107 * In the case of recovery, we can't expect flushing the cache to work
2108 * always, but we have a go and ignore errors.
2109 */
2110 mmc_flush_cache(host->card);
2111
Gwendal Grignou4e6c7172016-04-01 16:04:22 -07002112 if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2113 mmc_can_reset(card)) {
2114 /* If the card accept RST_n signal, send it. */
2115 mmc_set_clock(host, host->f_init);
2116 host->ops->hw_reset(host);
2117 /* Set initial state and call mmc_set_ios */
2118 mmc_set_initial_state(host);
2119 } else {
2120 /* Do a brute force power cycle */
2121 mmc_power_cycle(host, card->ocr);
2122 }
Ulf Hansson364549d2015-06-01 11:14:57 +02002123 return mmc_init_card(host, card->ocr, card);
Johan Rudholmf855a372015-01-12 15:38:05 +01002124}
2125
Adrian Hunter9feae242009-09-22 16:44:32 -07002126static const struct mmc_bus_ops mmc_ops = {
2127 .remove = mmc_remove,
2128 .detect = mmc_detect,
Adrian Hunter9feae242009-09-22 16:44:32 -07002129 .suspend = mmc_suspend,
2130 .resume = mmc_resume,
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002131 .runtime_suspend = mmc_runtime_suspend,
2132 .runtime_resume = mmc_runtime_resume,
Adrian Hunterd3049502011-11-28 16:22:00 +02002133 .alive = mmc_alive,
Ulf Hansson486fdbb2013-06-10 17:03:44 +02002134 .shutdown = mmc_shutdown,
Johan Rudholmf855a372015-01-12 15:38:05 +01002135 .reset = mmc_reset,
Adrian Hunter9feae242009-09-22 16:44:32 -07002136};
2137
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002138/*
2139 * Starting point for MMC card init.
2140 */
Andy Ross807e8e42011-01-03 10:36:56 -08002141int mmc_attach_mmc(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002142{
2143 int err;
Ulf Hansson69041152013-09-13 11:31:33 +02002144 u32 ocr, rocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002145
Pierre Ossmand84075c82007-08-09 13:23:56 +02002146 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002147
Stefan Nilsson XK44669032011-09-15 17:50:38 +02002148 /* Set correct bus mode for MMC before attempting attach */
2149 if (!mmc_host_is_spi(host))
2150 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2151
Andy Ross807e8e42011-01-03 10:36:56 -08002152 err = mmc_send_op_cond(host, 0, &ocr);
2153 if (err)
2154 return err;
2155
Ulf Hansson2501c912013-10-30 01:00:18 +01002156 mmc_attach_bus(host, &mmc_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01002157 if (host->ocr_avail_mmc)
2158 host->ocr_avail = host->ocr_avail_mmc;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002159
2160 /*
David Brownellaf517152007-08-08 09:11:32 -07002161 * We need to get OCR a different way for SPI.
2162 */
2163 if (mmc_host_is_spi(host)) {
2164 err = mmc_spi_read_ocr(host, 1, &ocr);
2165 if (err)
2166 goto err;
2167 }
2168
Ulf Hansson69041152013-09-13 11:31:33 +02002169 rocr = mmc_select_voltage(host, ocr);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002170
2171 /*
2172 * Can we support the voltage of the card?
2173 */
Ulf Hansson69041152013-09-13 11:31:33 +02002174 if (!rocr) {
Pierre Ossman109b5be2007-07-23 00:12:10 +02002175 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002176 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02002177 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002178
2179 /*
2180 * Detect and init the card.
2181 */
Ulf Hansson69041152013-09-13 11:31:33 +02002182 err = mmc_init_card(host, rocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02002183 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002184 goto err;
2185
2186 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02002187 err = mmc_add_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002188 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002189 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002190
Sergei Shtylyov2860d062015-10-14 23:53:03 +03002191 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002192 return 0;
2193
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002194remove_card:
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002195 mmc_remove_card(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002196 mmc_claim_host(host);
Andy Ross807e8e42011-01-03 10:36:56 -08002197 host->card = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002198err:
2199 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002200
Girish K Sa3c76eb2011-10-11 11:44:09 +05302201 pr_err("%s: error %d whilst initialising MMC card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02002202 mmc_hostname(host), err);
2203
Pierre Ossmanadf66a02007-07-22 23:08:30 +02002204 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002205}