blob: 2c87dede584110919e2191a32e0b838c0c8cfc59 [file] [log] [blame]
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/core/mmc.c
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
6 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/err.h>
Hans de Goede81f8a7b2015-04-01 17:26:23 +020014#include <linux/of.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Gortmaker0205a902011-07-15 12:01:27 -040016#include <linux/stat.h>
Ulf Hansson0cb403a2013-10-10 14:20:05 +020017#include <linux/pm_runtime.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010018
19#include <linux/mmc/host.h>
20#include <linux/mmc/card.h>
21#include <linux/mmc/mmc.h>
22
23#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010024#include "card.h"
Adrian Hunter436f8da2015-05-07 13:10:18 +030025#include "host.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020026#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010027#include "mmc_ops.h"
Shawn Lin28fc64a2017-02-15 16:35:28 +080028#include "quirks.h"
Philip Rakity4c4cb172011-05-13 11:17:18 +053029#include "sd_ops.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010030
Ulf Hanssonfe1b5702016-11-04 18:32:33 +010031#define DEFAULT_CMD6_TIMEOUT_MS 500
32
Pierre Ossman7ea239d2006-12-31 00:11:32 +010033static const unsigned int tran_exp[] = {
34 10000, 100000, 1000000, 10000000,
35 0, 0, 0, 0
36};
37
38static const unsigned char tran_mant[] = {
39 0, 10, 12, 13, 15, 20, 25, 30,
40 35, 40, 45, 50, 55, 60, 70, 80,
41};
42
43static const unsigned int tacc_exp[] = {
44 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
45};
46
47static const unsigned int tacc_mant[] = {
48 0, 10, 12, 13, 15, 20, 25, 30,
49 35, 40, 45, 50, 55, 60, 70, 80,
50};
51
52#define UNSTUFF_BITS(resp,start,size) \
53 ({ \
54 const int __size = size; \
55 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
56 const int __off = 3 - ((start) / 32); \
57 const int __shft = (start) & 31; \
58 u32 __res; \
59 \
60 __res = resp[__off] >> __shft; \
61 if (__size + __shft > 32) \
62 __res |= resp[__off-1] << ((32 - __shft) % 32); \
63 __res & __mask; \
64 })
65
66/*
67 * Given the decoded CSD structure, decode the raw CID to our CID structure.
68 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020069static int mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010070{
71 u32 *resp = card->raw_cid;
72
73 /*
74 * The selection of the format here is based upon published
75 * specs from sandisk and from what people have reported.
76 */
77 switch (card->csd.mmca_vsn) {
78 case 0: /* MMC v1.0 - v1.2 */
79 case 1: /* MMC v1.4 */
80 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
81 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
82 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
83 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
84 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
85 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
86 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
87 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
88 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
89 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
90 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
91 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
92 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
93 break;
94
95 case 2: /* MMC v2.0 - v2.2 */
96 case 3: /* MMC v3.1 - v3.3 */
97 case 4: /* MMC v4 */
98 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
99 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
100 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
101 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
102 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
103 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
104 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
105 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800106 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100107 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
108 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
109 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
110 break;
111
112 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530113 pr_err("%s: card has unknown MMCA version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100114 mmc_hostname(card->host), card->csd.mmca_vsn);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200115 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100116 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200117
118 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100119}
120
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700121static void mmc_set_erase_size(struct mmc_card *card)
122{
123 if (card->ext_csd.erase_group_def & 1)
124 card->erase_size = card->ext_csd.hc_erase_size;
125 else
126 card->erase_size = card->csd.erase_size;
127
128 mmc_init_erase(card);
129}
130
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100131/*
132 * Given a 128-bit response, decode to our card CSD structure.
133 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200134static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100135{
136 struct mmc_csd *csd = &card->csd;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700137 unsigned int e, m, a, b;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100138 u32 *resp = card->raw_csd;
139
140 /*
141 * We only understand CSD structure v1.1 and v1.2.
142 * v1.2 has extra information in bits 15, 11 and 10.
Kyungmin Park6da24b72010-08-10 18:01:36 -0700143 * We also support eMMC v4.4 & v4.41.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100144 */
Kyungmin Park6da24b72010-08-10 18:01:36 -0700145 csd->structure = UNSTUFF_BITS(resp, 126, 2);
146 if (csd->structure == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530147 pr_err("%s: unrecognised CSD structure version %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700148 mmc_hostname(card->host), csd->structure);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200149 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100150 }
151
152 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
153 m = UNSTUFF_BITS(resp, 115, 4);
154 e = UNSTUFF_BITS(resp, 112, 3);
155 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
156 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
157
158 m = UNSTUFF_BITS(resp, 99, 4);
159 e = UNSTUFF_BITS(resp, 96, 3);
160 csd->max_dtr = tran_exp[e] * tran_mant[m];
161 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
162
163 e = UNSTUFF_BITS(resp, 47, 3);
164 m = UNSTUFF_BITS(resp, 62, 12);
165 csd->capacity = (1 + m) << (e + 2);
166
167 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
168 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
169 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
170 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
Sascha Hauer3d705d12014-08-19 10:45:51 +0200171 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100172 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
173 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
174 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200175
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700176 if (csd->write_blkbits >= 9) {
177 a = UNSTUFF_BITS(resp, 42, 5);
178 b = UNSTUFF_BITS(resp, 37, 5);
179 csd->erase_size = (a + 1) * (b + 1);
180 csd->erase_size <<= csd->write_blkbits - 9;
181 }
182
Pierre Ossmanbd766312007-05-01 16:11:57 +0200183 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100184}
185
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900186static void mmc_select_card_type(struct mmc_card *card)
187{
188 struct mmc_host *host = card->host;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900189 u8 card_type = card->ext_csd.raw_card_type;
Lee Jones5f1a4dd2012-11-14 12:35:51 +0000190 u32 caps = host->caps, caps2 = host->caps2;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900191 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900192 unsigned int avail_type = 0;
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900193
194 if (caps & MMC_CAP_MMC_HIGHSPEED &&
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900195 card_type & EXT_CSD_CARD_TYPE_HS_26) {
196 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
197 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
198 }
199
200 if (caps & MMC_CAP_MMC_HIGHSPEED &&
201 card_type & EXT_CSD_CARD_TYPE_HS_52) {
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900202 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900203 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
204 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900205
Ulf Hansson20f921b2017-01-25 12:04:17 +0100206 if (caps & (MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR) &&
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900207 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900208 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900209 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
210 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900211
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900212 if (caps & MMC_CAP_1_2V_DDR &&
213 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
214 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
215 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
216 }
217
218 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
219 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
Seungwon Jeon577fb132014-04-23 17:08:44 +0900220 hs200_max_dtr = MMC_HS200_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900221 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
222 }
223
224 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
225 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
Seungwon Jeon577fb132014-04-23 17:08:44 +0900226 hs200_max_dtr = MMC_HS200_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900227 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
228 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900229
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900230 if (caps2 & MMC_CAP2_HS400_1_8V &&
231 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
232 hs200_max_dtr = MMC_HS200_MAX_DTR;
233 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
234 }
235
236 if (caps2 & MMC_CAP2_HS400_1_2V &&
237 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
238 hs200_max_dtr = MMC_HS200_MAX_DTR;
239 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
240 }
241
Shawn Lin81ac2af2016-05-26 09:56:22 +0800242 if ((caps2 & MMC_CAP2_HS400_ES) &&
243 card->ext_csd.strobe_support &&
244 (avail_type & EXT_CSD_CARD_TYPE_HS400))
245 avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
246
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900247 card->ext_csd.hs_max_dtr = hs_max_dtr;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900248 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900249 card->mmc_avail_type = avail_type;
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900250}
251
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200252static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
253{
Grégory Soutadé994324b2014-09-15 17:47:11 +0200254 u8 hc_erase_grp_sz, hc_wp_grp_sz;
255
256 /*
257 * Disable these attributes by default
258 */
259 card->ext_csd.enhanced_area_offset = -EINVAL;
260 card->ext_csd.enhanced_area_size = -EINVAL;
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200261
262 /*
263 * Enhanced area feature support -- check whether the eMMC
264 * card has the Enhanced area enabled. If so, export enhanced
265 * area offset and size to user by adding sysfs interface.
266 */
267 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
268 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
Grégory Soutadé994324b2014-09-15 17:47:11 +0200269 if (card->ext_csd.partition_setting_completed) {
270 hc_erase_grp_sz =
271 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
272 hc_wp_grp_sz =
273 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200274
Grégory Soutadé994324b2014-09-15 17:47:11 +0200275 /*
276 * calculate the enhanced data area offset, in bytes
277 */
278 card->ext_csd.enhanced_area_offset =
Kuninori Morimotoded8a5f2015-05-11 07:34:53 +0000279 (((unsigned long long)ext_csd[139]) << 24) +
280 (((unsigned long long)ext_csd[138]) << 16) +
281 (((unsigned long long)ext_csd[137]) << 8) +
282 (((unsigned long long)ext_csd[136]));
Grégory Soutadé994324b2014-09-15 17:47:11 +0200283 if (mmc_card_blockaddr(card))
284 card->ext_csd.enhanced_area_offset <<= 9;
285 /*
286 * calculate the enhanced data area size, in kilobytes
287 */
288 card->ext_csd.enhanced_area_size =
289 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
290 ext_csd[140];
291 card->ext_csd.enhanced_area_size *=
292 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
293 card->ext_csd.enhanced_area_size <<= 9;
294 } else {
295 pr_warn("%s: defines enhanced area without partition setting complete\n",
296 mmc_hostname(card->host));
297 }
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200298 }
299}
300
Ulf Hansson4facdde2017-01-13 14:14:14 +0100301static void mmc_part_add(struct mmc_card *card, unsigned int size,
302 unsigned int part_cfg, char *name, int idx, bool ro,
303 int area_type)
304{
305 card->part[card->nr_parts].size = size;
306 card->part[card->nr_parts].part_cfg = part_cfg;
307 sprintf(card->part[card->nr_parts].name, name, idx);
308 card->part[card->nr_parts].force_ro = ro;
309 card->part[card->nr_parts].area_type = area_type;
310 card->nr_parts++;
311}
312
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200313static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
314{
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200315 int idx;
Grégory Soutadé994324b2014-09-15 17:47:11 +0200316 u8 hc_erase_grp_sz, hc_wp_grp_sz;
317 unsigned int part_size;
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200318
319 /*
320 * General purpose partition feature support --
321 * If ext_csd has the size of general purpose partitions,
322 * set size, part_cfg, partition name in mmc_part.
323 */
324 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
325 EXT_CSD_PART_SUPPORT_PART_EN) {
Grégory Soutadé994324b2014-09-15 17:47:11 +0200326 hc_erase_grp_sz =
327 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
328 hc_wp_grp_sz =
329 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200330
331 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
332 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
333 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
334 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
335 continue;
Grégory Soutadé994324b2014-09-15 17:47:11 +0200336 if (card->ext_csd.partition_setting_completed == 0) {
337 pr_warn("%s: has partition size defined without partition complete\n",
338 mmc_hostname(card->host));
339 break;
340 }
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200341 part_size =
342 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
343 << 16) +
344 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
345 << 8) +
346 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
347 part_size *= (size_t)(hc_erase_grp_sz *
348 hc_wp_grp_sz);
349 mmc_part_add(card, part_size << 19,
350 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
351 "gp%d", idx, false,
352 MMC_BLK_DATA_AREA_GP);
353 }
354 }
355}
356
Adrian Hunter1c447112016-05-05 08:12:28 +0300357/* Minimum partition switch timeout in milliseconds */
358#define MMC_MIN_PART_SWITCH_TIME 300
359
Philip Rakity08ee80c2011-05-24 18:14:58 -0700360/*
361 * Decode extended CSD.
362 */
Ulf Hansson076ec382014-10-20 13:37:24 +0200363static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700364{
Namjae Jeone0c368d2011-10-06 23:41:38 +0900365 int err = 0, idx;
366 unsigned int part_size;
Hans de Goede81f8a7b2015-04-01 17:26:23 +0200367 struct device_node *np;
368 bool broken_hpi = false;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700369
Kyungmin Park6da24b72010-08-10 18:01:36 -0700370 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700371 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
Kyungmin Park6da24b72010-08-10 18:01:36 -0700372 if (card->csd.structure == 3) {
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700373 if (card->ext_csd.raw_ext_csd_structure > 2) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530374 pr_err("%s: unrecognised EXT_CSD structure "
Kyungmin Park6da24b72010-08-10 18:01:36 -0700375 "version %d\n", mmc_hostname(card->host),
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700376 card->ext_csd.raw_ext_csd_structure);
Kyungmin Park6da24b72010-08-10 18:01:36 -0700377 err = -EINVAL;
378 goto out;
379 }
380 }
381
Hans de Goede81f8a7b2015-04-01 17:26:23 +0200382 np = mmc_of_find_child_device(card->host, 0);
383 if (np && of_device_is_compatible(np, "mmc-card"))
384 broken_hpi = of_property_read_bool(np, "broken-hpi");
385 of_node_put(np);
386
Romain Izard03a59432014-06-27 10:51:07 +0200387 /*
388 * The EXT_CSD format is meant to be forward compatible. As long
389 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
390 * are authorized, see JEDEC JESD84-B50 section B.8.
391 */
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700392 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Pierre Ossmand7604d72007-07-23 00:34:07 +0200393
Pratibhasagar V53202262016-06-09 18:09:31 -0400394 /* fixup device after ext_csd revision field is updated */
395 mmc_fixup_device(card, mmc_ext_csd_fixups);
396
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700397 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
398 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
399 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
400 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700401 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200402 card->ext_csd.sectors =
403 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
404 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
405 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
406 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700407
408 /* Cards with density > 2GiB are sector addressed */
409 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200410 mmc_card_set_blockaddr(card);
411 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900412
Shawn Lin81ac2af2016-05-26 09:56:22 +0800413 card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700414 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900415 mmc_select_card_type(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100416
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700417 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
418 card->ext_csd.raw_erase_timeout_mult =
419 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
420 card->ext_csd.raw_hc_erase_grp_size =
421 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700422 if (card->ext_csd.rev >= 3) {
423 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500424 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
425
426 /* EXT_CSD value is in units of 10ms, but we store in ms */
427 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
Adrian Hunter1c447112016-05-05 08:12:28 +0300428 /* Some eMMC set the value too low so set a minimum */
429 if (card->ext_csd.part_time &&
430 card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
431 card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700432
433 /* Sleep / awake timeout in 100ns units */
434 if (sa_shift > 0 && sa_shift <= 0x17)
435 card->ext_csd.sa_timeout =
436 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700437 card->ext_csd.erase_group_def =
438 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
439 card->ext_csd.hc_erase_timeout = 300 *
440 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
441 card->ext_csd.hc_erase_size =
442 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500443
444 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500445
446 /*
447 * There are two boot regions of equal size, defined in
448 * multiples of 128K.
449 */
Namjae Jeone0c368d2011-10-06 23:41:38 +0900450 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
451 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
452 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
453 mmc_part_add(card, part_size,
454 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100455 "boot%d", idx, true,
456 MMC_BLK_DATA_AREA_BOOT);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900457 }
458 }
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700459 }
460
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700461 card->ext_csd.raw_hc_erase_gap_size =
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100462 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700463 card->ext_csd.raw_sec_trim_mult =
464 ext_csd[EXT_CSD_SEC_TRIM_MULT];
465 card->ext_csd.raw_sec_erase_mult =
466 ext_csd[EXT_CSD_SEC_ERASE_MULT];
467 card->ext_csd.raw_sec_feature_support =
468 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
469 card->ext_csd.raw_trim_mult =
470 ext_csd[EXT_CSD_TRIM_MULT];
Philip Rakity836dc2f2013-04-04 20:18:11 +0100471 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
Adrian Hunterb097e072015-02-06 14:12:57 +0200472 card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700473 if (card->ext_csd.rev >= 4) {
Grégory Soutadé69803d42014-09-15 17:47:09 +0200474 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
475 EXT_CSD_PART_SETTING_COMPLETED)
476 card->ext_csd.partition_setting_completed = 1;
477 else
478 card->ext_csd.partition_setting_completed = 0;
479
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200480 mmc_manage_enhanced_area(card, ext_csd);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800481
Grégory Soutadéb4493ee2014-09-15 17:47:06 +0200482 mmc_manage_gp_partitions(card, ext_csd);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900483
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700484 card->ext_csd.sec_trim_mult =
485 ext_csd[EXT_CSD_SEC_TRIM_MULT];
486 card->ext_csd.sec_erase_mult =
487 ext_csd[EXT_CSD_SEC_ERASE_MULT];
488 card->ext_csd.sec_feature_support =
489 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
490 card->ext_csd.trim_timeout = 300 *
491 ext_csd[EXT_CSD_TRIM_MULT];
Johan Rudholmadd710e2011-12-02 08:51:06 +0100492
493 /*
494 * Note that the call to mmc_part_add above defaults to read
495 * only. If this default assumption is changed, the call must
496 * take into account the value of boot_locked below.
497 */
498 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
499 card->ext_csd.boot_ro_lockable = true;
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200500
501 /* Save power class values */
502 card->ext_csd.raw_pwr_cl_52_195 =
503 ext_csd[EXT_CSD_PWR_CL_52_195];
504 card->ext_csd.raw_pwr_cl_26_195 =
505 ext_csd[EXT_CSD_PWR_CL_26_195];
506 card->ext_csd.raw_pwr_cl_52_360 =
507 ext_csd[EXT_CSD_PWR_CL_52_360];
508 card->ext_csd.raw_pwr_cl_26_360 =
509 ext_csd[EXT_CSD_PWR_CL_26_360];
510 card->ext_csd.raw_pwr_cl_200_195 =
511 ext_csd[EXT_CSD_PWR_CL_200_195];
512 card->ext_csd.raw_pwr_cl_200_360 =
513 ext_csd[EXT_CSD_PWR_CL_200_360];
514 card->ext_csd.raw_pwr_cl_ddr_52_195 =
515 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
516 card->ext_csd.raw_pwr_cl_ddr_52_360 =
517 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900518 card->ext_csd.raw_pwr_cl_ddr_200_360 =
519 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700520 }
521
Adrian Hunterb2499512011-08-29 16:42:11 +0300522 if (card->ext_csd.rev >= 5) {
Romain Izard7c4f10a2013-06-14 14:25:44 +0200523 /* Adjust production date as per JEDEC JESD84-B451 */
524 if (card->cid.year < 2010)
525 card->cid.year += 16;
526
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000527 /* check whether the eMMC card supports BKOPS */
Pratibhasagar V53202262016-06-09 18:09:31 -0400528 if (!mmc_card_broken_hpi(card) &&
529 ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000530 card->ext_csd.bkops = 1;
Alexey Skidanov0501be62015-01-29 10:49:43 +0200531 card->ext_csd.man_bkops_en =
532 (ext_csd[EXT_CSD_BKOPS_EN] &
533 EXT_CSD_MANUAL_BKOPS_MASK);
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000534 card->ext_csd.raw_bkops_status =
535 ext_csd[EXT_CSD_BKOPS_STATUS];
Uri Yanai6962c2f2017-02-07 18:00:00 +0200536 if (card->ext_csd.man_bkops_en)
537 pr_debug("%s: MAN_BKOPS_EN bit is set\n",
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000538 mmc_hostname(card->host));
Uri Yanaiefff8e72017-02-07 18:00:01 +0200539 card->ext_csd.auto_bkops_en =
540 (ext_csd[EXT_CSD_BKOPS_EN] &
541 EXT_CSD_AUTO_BKOPS_MASK);
542 if (card->ext_csd.auto_bkops_en)
543 pr_debug("%s: AUTO_BKOPS_EN bit is set\n",
544 mmc_hostname(card->host));
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000545 }
546
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400547 /* check whether the eMMC card supports HPI */
Pratibhasagar V53202262016-06-09 18:09:31 -0400548 if (!mmc_card_broken_hpi(card) &&
549 !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400550 card->ext_csd.hpi = 1;
551 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
552 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
553 else
554 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
555 /*
556 * Indicate the maximum timeout to close
557 * a command interrupted by HPI
558 */
559 card->ext_csd.out_of_int_time =
560 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
561 }
562
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500563 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
Adrian Hunterb2499512011-08-29 16:42:11 +0300564 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
Loic Pallardy090d25f2012-11-17 18:08:24 -0500565
566 /*
567 * RPMB regions are defined in multiples of 128K.
568 */
569 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
Balaji T Kd0123cc2013-01-25 17:00:30 +0530570 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
Loic Pallardy090d25f2012-11-17 18:08:24 -0500571 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
572 EXT_CSD_PART_CONFIG_ACC_RPMB,
573 "rpmb", 0, false,
574 MMC_BLK_DATA_AREA_RPMB);
575 }
Adrian Hunterb2499512011-08-29 16:42:11 +0300576 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500577
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400578 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700579 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
580 card->erased_byte = 0xFF;
581 else
582 card->erased_byte = 0x0;
583
Seungwon Jeon336c7162011-10-25 09:43:12 +0900584 /* eMMC v4.5 or later */
Ulf Hanssonfe1b5702016-11-04 18:32:33 +0100585 card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS;
Girish K Sbec87262011-10-13 12:04:16 +0530586 if (card->ext_csd.rev >= 6) {
Seungwon Jeon336c7162011-10-25 09:43:12 +0900587 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
588
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900589 card->ext_csd.generic_cmd6_time = 10 *
590 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
Girish K Sbec87262011-10-13 12:04:16 +0530591 card->ext_csd.power_off_longtime = 10 *
592 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900593
Seungwon Jeon336c7162011-10-25 09:43:12 +0900594 card->ext_csd.cache_size =
595 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
596 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
597 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
598 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
Saugata Das42659002011-12-21 13:09:17 +0530599
600 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
601 card->ext_csd.data_sector_size = 4096;
602 else
603 card->ext_csd.data_sector_size = 512;
604
605 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
606 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
607 card->ext_csd.data_tag_unit_size =
608 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
609 (card->ext_csd.data_sector_size);
610 } else {
611 card->ext_csd.data_tag_unit_size = 0;
612 }
Seungwon Jeonabd9ac12013-02-06 17:01:43 +0900613
614 card->ext_csd.max_packed_writes =
615 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
616 card->ext_csd.max_packed_reads =
617 ext_csd[EXT_CSD_MAX_PACKED_READS];
Saugata Dasa5075eb2012-05-17 16:32:21 +0530618 } else {
619 card->ext_csd.data_sector_size = 512;
Seungwon Jeon336c7162011-10-25 09:43:12 +0900620 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900621
Gwendal Grignou0f762422014-10-16 11:27:16 -0700622 /* eMMC v5 or later */
623 if (card->ext_csd.rev >= 7) {
624 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
625 MMC_FIRMWARE_LEN);
626 card->ext_csd.ffu_capable =
627 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
628 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900629
630 card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO];
631 card->ext_csd.device_life_time_est_typ_a =
632 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
633 card->ext_csd.device_life_time_est_typ_b =
634 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
Gwendal Grignou0f762422014-10-16 11:27:16 -0700635 }
Adrian Hunter925ff3a2016-11-29 12:09:16 +0200636
637 /* eMMC v5.1 or later */
638 if (card->ext_csd.rev >= 8) {
639 card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] &
640 EXT_CSD_CMDQ_SUPPORTED;
641 card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] &
642 EXT_CSD_CMDQ_DEPTH_MASK) + 1;
643 /* Exclude inefficiently small queue depths */
644 if (card->ext_csd.cmdq_depth <= 2) {
645 card->ext_csd.cmdq_support = false;
646 card->ext_csd.cmdq_depth = 0;
647 }
648 if (card->ext_csd.cmdq_support) {
649 pr_debug("%s: Command Queue supported depth %u\n",
650 mmc_hostname(card->host),
651 card->ext_csd.cmdq_depth);
652 }
653 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100654out:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700655 return err;
656}
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100657
Ulf Hansson076ec382014-10-20 13:37:24 +0200658static int mmc_read_ext_csd(struct mmc_card *card)
659{
Ulf Hanssonc1977872014-10-20 14:08:16 +0200660 u8 *ext_csd;
Ulf Hansson076ec382014-10-20 13:37:24 +0200661 int err;
662
Ulf Hanssonc1977872014-10-20 14:08:16 +0200663 if (!mmc_can_ext_csd(card))
664 return 0;
665
Ulf Hansson076ec382014-10-20 13:37:24 +0200666 err = mmc_get_ext_csd(card, &ext_csd);
Ulf Hanssonc1977872014-10-20 14:08:16 +0200667 if (err) {
668 /* If the host or the card can't do the switch,
669 * fail more gracefully. */
670 if ((err != -EINVAL)
671 && (err != -ENOSYS)
672 && (err != -EFAULT))
673 return err;
674
675 /*
676 * High capacity cards should have this "magic" size
677 * stored in their CSD.
678 */
679 if (card->csd.capacity == (4096 * 512)) {
680 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
681 mmc_hostname(card->host));
682 } else {
683 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
684 mmc_hostname(card->host));
685 err = 0;
686 }
687
Ulf Hansson076ec382014-10-20 13:37:24 +0200688 return err;
Ulf Hanssonc1977872014-10-20 14:08:16 +0200689 }
Ulf Hansson076ec382014-10-20 13:37:24 +0200690
691 err = mmc_decode_ext_csd(card, ext_csd);
692 kfree(ext_csd);
693 return err;
694}
695
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700696static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700697{
698 u8 *bw_ext_csd;
699 int err;
700
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700701 if (bus_width == MMC_BUS_WIDTH_1)
702 return 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700703
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700704 err = mmc_get_ext_csd(card, &bw_ext_csd);
Ulf Hanssonc1977872014-10-20 14:08:16 +0200705 if (err)
706 return err;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700707
Philip Rakity08ee80c2011-05-24 18:14:58 -0700708 /* only compare read only fields */
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100709 err = !((card->ext_csd.raw_partition_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700710 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700711 (card->ext_csd.raw_erased_mem_count ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700712 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700713 (card->ext_csd.rev ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700714 bw_ext_csd[EXT_CSD_REV]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700715 (card->ext_csd.raw_ext_csd_structure ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700716 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700717 (card->ext_csd.raw_card_type ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700718 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700719 (card->ext_csd.raw_s_a_timeout ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700720 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700721 (card->ext_csd.raw_hc_erase_gap_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700722 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700723 (card->ext_csd.raw_erase_timeout_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700724 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700725 (card->ext_csd.raw_hc_erase_grp_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700726 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700727 (card->ext_csd.raw_sec_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700728 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700729 (card->ext_csd.raw_sec_erase_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700730 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700731 (card->ext_csd.raw_sec_feature_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700732 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700733 (card->ext_csd.raw_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700734 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700735 (card->ext_csd.raw_sectors[0] ==
736 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
737 (card->ext_csd.raw_sectors[1] ==
738 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
739 (card->ext_csd.raw_sectors[2] ==
740 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
741 (card->ext_csd.raw_sectors[3] ==
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200742 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
743 (card->ext_csd.raw_pwr_cl_52_195 ==
744 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
745 (card->ext_csd.raw_pwr_cl_26_195 ==
746 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
747 (card->ext_csd.raw_pwr_cl_52_360 ==
748 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
749 (card->ext_csd.raw_pwr_cl_26_360 ==
750 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
751 (card->ext_csd.raw_pwr_cl_200_195 ==
752 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
753 (card->ext_csd.raw_pwr_cl_200_360 ==
754 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
755 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
756 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
757 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900758 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
759 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
760 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
761
Philip Rakity08ee80c2011-05-24 18:14:58 -0700762 if (err)
763 err = -EINVAL;
764
Ulf Hansson00b41b52014-10-16 16:18:51 +0200765 kfree(bw_ext_csd);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100766 return err;
767}
768
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100769MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
770 card->raw_cid[2], card->raw_cid[3]);
771MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
772 card->raw_csd[2], card->raw_csd[3]);
773MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700774MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
775MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Gwendal Grignou0f762422014-10-16 11:27:16 -0700776MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100777MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
778MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
779MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
780MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800781MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900782MMC_DEV_ATTR(pre_eol_info, "%02x\n", card->ext_csd.pre_eol_info);
783MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n",
784 card->ext_csd.device_life_time_est_typ_a,
785 card->ext_csd.device_life_time_est_typ_b);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100786MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800787MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
788 card->ext_csd.enhanced_area_offset);
789MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
Loic Pallardy188cc042012-08-06 17:12:29 +0200790MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
791MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
Bojan Prtvar5fb06af2016-07-04 13:56:55 +0200792MMC_DEV_ATTR(ocr, "%08x\n", card->ocr);
Adrian Hunterb658af72017-03-13 14:36:37 +0200793MMC_DEV_ATTR(cmdq_en, "%d\n", card->ext_csd.cmdq_en);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100794
Gwendal Grignou0f762422014-10-16 11:27:16 -0700795static ssize_t mmc_fwrev_show(struct device *dev,
796 struct device_attribute *attr,
797 char *buf)
798{
799 struct mmc_card *card = mmc_dev_to_card(dev);
800
801 if (card->ext_csd.rev < 7) {
802 return sprintf(buf, "0x%x\n", card->cid.fwrev);
803 } else {
804 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
805 card->ext_csd.fwrev);
806 }
807}
808
809static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
810
Bojan Prtvar6825a602016-07-19 11:16:38 +0200811static ssize_t mmc_dsr_show(struct device *dev,
812 struct device_attribute *attr,
813 char *buf)
814{
815 struct mmc_card *card = mmc_dev_to_card(dev);
816 struct mmc_host *host = card->host;
817
818 if (card->csd.dsr_imp && host->dsr_req)
819 return sprintf(buf, "0x%x\n", host->dsr);
820 else
821 /* return default DSR value */
822 return sprintf(buf, "0x%x\n", 0x404);
823}
824
825static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
826
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100827static struct attribute *mmc_std_attrs[] = {
828 &dev_attr_cid.attr,
829 &dev_attr_csd.attr,
830 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700831 &dev_attr_erase_size.attr,
832 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100833 &dev_attr_fwrev.attr,
Gwendal Grignou0f762422014-10-16 11:27:16 -0700834 &dev_attr_ffu_capable.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100835 &dev_attr_hwrev.attr,
836 &dev_attr_manfid.attr,
837 &dev_attr_name.attr,
838 &dev_attr_oemid.attr,
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800839 &dev_attr_prv.attr,
Jungseung Lee46bc5c42016-12-22 12:37:34 +0900840 &dev_attr_pre_eol_info.attr,
841 &dev_attr_life_time.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100842 &dev_attr_serial.attr,
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800843 &dev_attr_enhanced_area_offset.attr,
844 &dev_attr_enhanced_area_size.attr,
Loic Pallardy188cc042012-08-06 17:12:29 +0200845 &dev_attr_raw_rpmb_size_mult.attr,
846 &dev_attr_rel_sectors.attr,
Bojan Prtvar5fb06af2016-07-04 13:56:55 +0200847 &dev_attr_ocr.attr,
Bojan Prtvar6825a602016-07-19 11:16:38 +0200848 &dev_attr_dsr.attr,
Adrian Hunterb658af72017-03-13 14:36:37 +0200849 &dev_attr_cmdq_en.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100850 NULL,
851};
Axel Lind1e58212014-03-08 15:05:27 +0800852ATTRIBUTE_GROUPS(mmc_std);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100853
854static struct device_type mmc_type = {
Axel Lind1e58212014-03-08 15:05:27 +0800855 .groups = mmc_std_groups,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100856};
857
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100858/*
Girish K Sb87d8db2011-09-23 20:41:47 +0530859 * Select the PowerClass for the current bus width
860 * If power class is defined for 4/8 bit bus in the
861 * extended CSD register, select it by executing the
862 * mmc_switch command.
863 */
Seungwon Jeon23850492014-04-23 17:08:05 +0900864static int __mmc_select_powerclass(struct mmc_card *card,
865 unsigned int bus_width)
Girish K Sb87d8db2011-09-23 20:41:47 +0530866{
Seungwon Jeon23850492014-04-23 17:08:05 +0900867 struct mmc_host *host = card->host;
868 struct mmc_ext_csd *ext_csd = &card->ext_csd;
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200869 unsigned int pwrclass_val = 0;
Seungwon Jeon23850492014-04-23 17:08:05 +0900870 int err = 0;
Girish K Sb87d8db2011-09-23 20:41:47 +0530871
Girish K Sb87d8db2011-09-23 20:41:47 +0530872 switch (1 << host->ios.vdd) {
873 case MMC_VDD_165_195:
Seungwon Jeon23850492014-04-23 17:08:05 +0900874 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
875 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
876 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200877 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
Seungwon Jeon23850492014-04-23 17:08:05 +0900878 ext_csd->raw_pwr_cl_52_195 :
879 ext_csd->raw_pwr_cl_ddr_52_195;
880 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
881 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
Girish K Sb87d8db2011-09-23 20:41:47 +0530882 break;
Subhash Jadavani93fc5a42012-04-03 12:25:58 +0530883 case MMC_VDD_27_28:
884 case MMC_VDD_28_29:
885 case MMC_VDD_29_30:
886 case MMC_VDD_30_31:
887 case MMC_VDD_31_32:
Girish K Sb87d8db2011-09-23 20:41:47 +0530888 case MMC_VDD_32_33:
889 case MMC_VDD_33_34:
890 case MMC_VDD_34_35:
891 case MMC_VDD_35_36:
Seungwon Jeon23850492014-04-23 17:08:05 +0900892 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
893 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
894 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200895 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
Seungwon Jeon23850492014-04-23 17:08:05 +0900896 ext_csd->raw_pwr_cl_52_360 :
897 ext_csd->raw_pwr_cl_ddr_52_360;
898 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900899 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
900 ext_csd->raw_pwr_cl_ddr_200_360 :
901 ext_csd->raw_pwr_cl_200_360;
Girish K Sb87d8db2011-09-23 20:41:47 +0530902 break;
903 default:
Joe Perches66061102014-09-12 14:56:56 -0700904 pr_warn("%s: Voltage range not supported for power class\n",
905 mmc_hostname(host));
Girish K Sb87d8db2011-09-23 20:41:47 +0530906 return -EINVAL;
907 }
908
Girish K Sb87d8db2011-09-23 20:41:47 +0530909 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
910 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
911 EXT_CSD_PWR_CL_8BIT_SHIFT;
912 else
913 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
914 EXT_CSD_PWR_CL_4BIT_SHIFT;
915
916 /* If the power class is different from the default value */
917 if (pwrclass_val > 0) {
918 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
919 EXT_CSD_POWER_CLASS,
920 pwrclass_val,
Seungwon Jeon71fe3eb2011-10-18 13:20:57 +0900921 card->ext_csd.generic_cmd6_time);
Girish K Sb87d8db2011-09-23 20:41:47 +0530922 }
923
924 return err;
925}
926
Seungwon Jeon23850492014-04-23 17:08:05 +0900927static int mmc_select_powerclass(struct mmc_card *card)
928{
929 struct mmc_host *host = card->host;
930 u32 bus_width, ext_csd_bits;
931 int err, ddr;
932
933 /* Power class selection is supported for versions >= 4.0 */
Ulf Hansson148bcab2014-10-20 11:33:53 +0200934 if (!mmc_can_ext_csd(card))
Seungwon Jeon23850492014-04-23 17:08:05 +0900935 return 0;
936
937 bus_width = host->ios.bus_width;
938 /* Power class values are defined only for 4/8 bit bus */
939 if (bus_width == MMC_BUS_WIDTH_1)
940 return 0;
941
942 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
943 if (ddr)
944 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
945 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
946 else
947 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
948 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
949
950 err = __mmc_select_powerclass(card, ext_csd_bits);
951 if (err)
952 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
953 mmc_hostname(host), 1 << bus_width, ddr);
954
955 return err;
956}
957
Girish K Sb87d8db2011-09-23 20:41:47 +0530958/*
Seungwon Jeon577fb132014-04-23 17:08:44 +0900959 * Set the bus speed for the selected speed mode.
Girish K Sa4924c72012-01-11 14:04:52 -0500960 */
Seungwon Jeon577fb132014-04-23 17:08:44 +0900961static void mmc_set_bus_speed(struct mmc_card *card)
Girish K Sa4924c72012-01-11 14:04:52 -0500962{
Seungwon Jeon577fb132014-04-23 17:08:44 +0900963 unsigned int max_dtr = (unsigned int)-1;
964
Seungwon Jeon0a5b6432014-04-23 17:14:58 +0900965 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
966 max_dtr > card->ext_csd.hs200_max_dtr)
Seungwon Jeon577fb132014-04-23 17:08:44 +0900967 max_dtr = card->ext_csd.hs200_max_dtr;
968 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
969 max_dtr = card->ext_csd.hs_max_dtr;
970 else if (max_dtr > card->csd.max_dtr)
971 max_dtr = card->csd.max_dtr;
972
973 mmc_set_clock(card->host, max_dtr);
974}
975
976/*
977 * Select the bus width amoung 4-bit and 8-bit(SDR).
978 * If the bus width is changed successfully, return the selected width value.
979 * Zero is returned instead of error value if the wide width is not supported.
980 */
981static int mmc_select_bus_width(struct mmc_card *card)
982{
Girish K Sa4924c72012-01-11 14:04:52 -0500983 static unsigned ext_csd_bits[] = {
Girish K Sa4924c72012-01-11 14:04:52 -0500984 EXT_CSD_BUS_WIDTH_8,
Seungwon Jeon577fb132014-04-23 17:08:44 +0900985 EXT_CSD_BUS_WIDTH_4,
Girish K Sa4924c72012-01-11 14:04:52 -0500986 };
987 static unsigned bus_widths[] = {
Girish K Sa4924c72012-01-11 14:04:52 -0500988 MMC_BUS_WIDTH_8,
Seungwon Jeon577fb132014-04-23 17:08:44 +0900989 MMC_BUS_WIDTH_4,
Girish K Sa4924c72012-01-11 14:04:52 -0500990 };
Seungwon Jeon577fb132014-04-23 17:08:44 +0900991 struct mmc_host *host = card->host;
992 unsigned idx, bus_width = 0;
993 int err = 0;
Girish K Sa4924c72012-01-11 14:04:52 -0500994
Alexandre Belloni1c2d26e2014-12-17 19:32:06 +0100995 if (!mmc_can_ext_csd(card) ||
Seungwon Jeon577fb132014-04-23 17:08:44 +0900996 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
997 return 0;
Girish K Sa4924c72012-01-11 14:04:52 -0500998
Seungwon Jeon577fb132014-04-23 17:08:44 +0900999 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
Girish K Sa4924c72012-01-11 14:04:52 -05001000
1001 /*
1002 * Unlike SD, MMC cards dont have a configuration register to notify
1003 * supported bus width. So bus test command should be run to identify
1004 * the supported bus width or compare the ext csd values of current
1005 * bus width and ext csd values of 1 bit mode read earlier.
1006 */
Seungwon Jeon577fb132014-04-23 17:08:44 +09001007 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
Girish K Sa4924c72012-01-11 14:04:52 -05001008 /*
1009 * Host is capable of 8bit transfer, then switch
1010 * the device to work in 8bit transfer mode. If the
1011 * mmc switch command returns error then switch to
1012 * 4bit transfer mode. On success set the corresponding
1013 * bus width on the host.
1014 */
1015 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1016 EXT_CSD_BUS_WIDTH,
1017 ext_csd_bits[idx],
1018 card->ext_csd.generic_cmd6_time);
1019 if (err)
1020 continue;
1021
Seungwon Jeon577fb132014-04-23 17:08:44 +09001022 bus_width = bus_widths[idx];
1023 mmc_set_bus_width(host, bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -05001024
Seungwon Jeon577fb132014-04-23 17:08:44 +09001025 /*
1026 * If controller can't handle bus width test,
1027 * compare ext_csd previously read in 1 bit mode
1028 * against ext_csd at new bus width
1029 */
Girish K Sa4924c72012-01-11 14:04:52 -05001030 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
Seungwon Jeon577fb132014-04-23 17:08:44 +09001031 err = mmc_compare_ext_csds(card, bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -05001032 else
Seungwon Jeon577fb132014-04-23 17:08:44 +09001033 err = mmc_bus_test(card, bus_width);
1034
1035 if (!err) {
1036 err = bus_width;
Girish K Sa4924c72012-01-11 14:04:52 -05001037 break;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001038 } else {
1039 pr_warn("%s: switch to bus width %d failed\n",
Wolfram Sanged9feec2016-01-29 09:27:50 +01001040 mmc_hostname(host), 1 << bus_width);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001041 }
Girish K Sa4924c72012-01-11 14:04:52 -05001042 }
1043
Seungwon Jeon577fb132014-04-23 17:08:44 +09001044 return err;
1045}
1046
1047/*
1048 * Switch to the high-speed mode
1049 */
1050static int mmc_select_hs(struct mmc_card *card)
1051{
1052 int err;
1053
1054 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1055 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
Ulf Hansson53e60652016-11-09 19:40:29 +01001056 card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
1057 true, true, true);
Jungseung Lee67d35962016-08-24 19:34:09 +09001058 if (err)
1059 pr_warn("%s: switch to high-speed failed, err:%d\n",
1060 mmc_hostname(card->host), err);
1061
Seungwon Jeon577fb132014-04-23 17:08:44 +09001062 return err;
1063}
1064
1065/*
1066 * Activate wide bus and DDR if supported.
1067 */
1068static int mmc_select_hs_ddr(struct mmc_card *card)
1069{
1070 struct mmc_host *host = card->host;
1071 u32 bus_width, ext_csd_bits;
1072 int err = 0;
1073
1074 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1075 return 0;
1076
1077 bus_width = host->ios.bus_width;
1078 if (bus_width == MMC_BUS_WIDTH_1)
1079 return 0;
1080
1081 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1082 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1083
Ulf Hanssone173f892016-11-09 21:00:26 +01001084 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1085 EXT_CSD_BUS_WIDTH,
1086 ext_csd_bits,
1087 card->ext_csd.generic_cmd6_time,
1088 MMC_TIMING_MMC_DDR52,
1089 true, true, true);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001090 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001091 pr_err("%s: switch to bus width %d ddr failed\n",
Seungwon Jeon577fb132014-04-23 17:08:44 +09001092 mmc_hostname(host), 1 << bus_width);
1093 return err;
1094 }
1095
1096 /*
1097 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1098 * signaling.
1099 *
1100 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1101 *
1102 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1103 * in the JEDEC spec for DDR.
1104 *
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001105 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1106 * host controller can support this, like some of the SDHCI
1107 * controller which connect to an eMMC device. Some of these
1108 * host controller still needs to use 1.8v vccq for supporting
1109 * DDR mode.
1110 *
1111 * So the sequence will be:
1112 * if (host and device can both support 1.2v IO)
1113 * use 1.2v IO;
1114 * else if (host and device can both support 1.8v IO)
1115 * use 1.8v IO;
1116 * so if host and device can only support 3.3v IO, this is the
1117 * last choice.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001118 *
1119 * WARNING: eMMC rules are NOT the same as SD DDR
1120 */
Ulf Hansson20f921b2017-01-25 12:04:17 +01001121 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001122 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
Ulf Hansson20f921b2017-01-25 12:04:17 +01001123 if (!err)
1124 return 0;
1125 }
Seungwon Jeon577fb132014-04-23 17:08:44 +09001126
Ulf Hansson20f921b2017-01-25 12:04:17 +01001127 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V &&
1128 host->caps & MMC_CAP_1_8V_DDR)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001129 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001130
1131 /* make sure vccq is 3.3v after switching disaster */
1132 if (err)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001133 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
Chuanxiao.Dong312449e2014-08-15 11:28:07 +08001134
Seungwon Jeon577fb132014-04-23 17:08:44 +09001135 return err;
1136}
1137
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001138static int mmc_select_hs400(struct mmc_card *card)
1139{
1140 struct mmc_host *host = card->host;
Adrian Hunter51b12f72015-10-28 14:25:41 +02001141 unsigned int max_dtr;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001142 int err = 0;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001143 u8 val;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001144
1145 /*
1146 * HS400 mode requires 8-bit bus width
1147 */
1148 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1149 host->ios.bus_width == MMC_BUS_WIDTH_8))
1150 return 0;
1151
Adrian Hunter51b12f72015-10-28 14:25:41 +02001152 /* Switch card to HS mode */
Wenkai Duadb24d42015-11-26 14:00:44 +02001153 val = EXT_CSD_TIMING_HS;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001154 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001155 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001156 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001157 true, false, true);
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001158 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001159 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001160 mmc_hostname(host), err);
1161 return err;
1162 }
1163
Adrian Hunter51b12f72015-10-28 14:25:41 +02001164 /* Set host controller to HS timing */
1165 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1166
Ziyuan Xu649c6052016-05-26 13:50:32 +08001167 /* Reduce frequency to HS frequency */
1168 max_dtr = card->ext_csd.hs_max_dtr;
1169 mmc_set_clock(host, max_dtr);
1170
Chaotian Jing08573ea2016-05-19 16:47:41 +08001171 err = mmc_switch_status(card);
1172 if (err)
1173 goto out_err;
Adrian Hunterd2302932015-10-28 14:25:43 +02001174
1175 /* Switch card to DDR */
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001176 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1177 EXT_CSD_BUS_WIDTH,
1178 EXT_CSD_DDR_BUS_WIDTH_8,
1179 card->ext_csd.generic_cmd6_time);
1180 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001181 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001182 mmc_hostname(host), err);
1183 return err;
1184 }
1185
Adrian Hunterd2302932015-10-28 14:25:43 +02001186 /* Switch card to HS400 */
Adrian Huntercc4f4142015-02-06 14:12:58 +02001187 val = EXT_CSD_TIMING_HS400 |
1188 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001189 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001190 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001191 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001192 true, false, true);
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001193 if (err) {
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001194 pr_err("%s: switch to hs400 failed, err:%d\n",
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001195 mmc_hostname(host), err);
1196 return err;
1197 }
1198
Adrian Hunterd2302932015-10-28 14:25:43 +02001199 /* Set host controller to HS400 timing and frequency */
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001200 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1201 mmc_set_bus_speed(card);
1202
Chaotian Jing08573ea2016-05-19 16:47:41 +08001203 err = mmc_switch_status(card);
1204 if (err)
1205 goto out_err;
Adrian Hunterd2302932015-10-28 14:25:43 +02001206
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001207 return 0;
Adrian Hunterd2302932015-10-28 14:25:43 +02001208
1209out_err:
1210 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1211 __func__, err);
1212 return err;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001213}
1214
Adrian Hunter6376f692015-05-07 13:10:20 +03001215int mmc_hs200_to_hs400(struct mmc_card *card)
1216{
1217 return mmc_select_hs400(card);
1218}
1219
Adrian Hunter6376f692015-05-07 13:10:20 +03001220int mmc_hs400_to_hs200(struct mmc_card *card)
1221{
1222 struct mmc_host *host = card->host;
Adrian Hunter6376f692015-05-07 13:10:20 +03001223 unsigned int max_dtr;
1224 int err;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001225 u8 val;
Adrian Hunter6376f692015-05-07 13:10:20 +03001226
Adrian Hunter6376f692015-05-07 13:10:20 +03001227 /* Reduce frequency to HS */
1228 max_dtr = card->ext_csd.hs_max_dtr;
1229 mmc_set_clock(host, max_dtr);
1230
1231 /* Switch HS400 to HS DDR */
Wenkai Duadb24d42015-11-26 14:00:44 +02001232 val = EXT_CSD_TIMING_HS;
Adrian Hunter6376f692015-05-07 13:10:20 +03001233 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001234 val, card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001235 true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001236 if (err)
1237 goto out_err;
1238
1239 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1240
Chaotian Jing08573ea2016-05-19 16:47:41 +08001241 err = mmc_switch_status(card);
1242 if (err)
1243 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001244
1245 /* Switch HS DDR to HS */
1246 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1247 EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001248 0, true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001249 if (err)
1250 goto out_err;
1251
1252 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1253
Chaotian Jing08573ea2016-05-19 16:47:41 +08001254 err = mmc_switch_status(card);
1255 if (err)
1256 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001257
1258 /* Switch HS to HS200 */
Adrian Huntercc4f4142015-02-06 14:12:58 +02001259 val = EXT_CSD_TIMING_HS200 |
1260 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Adrian Hunter6376f692015-05-07 13:10:20 +03001261 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001262 val, card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001263 true, false, true);
Adrian Hunter6376f692015-05-07 13:10:20 +03001264 if (err)
1265 goto out_err;
1266
1267 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1268
Adrian Hunteref3d2322016-12-02 13:16:35 +02001269 /*
1270 * For HS200, CRC errors are not a reliable way to know the switch
1271 * failed. If there really is a problem, we would expect tuning will
1272 * fail and the result ends up the same.
1273 */
1274 err = __mmc_switch_status(card, false);
Chaotian Jing08573ea2016-05-19 16:47:41 +08001275 if (err)
1276 goto out_err;
Adrian Hunter6376f692015-05-07 13:10:20 +03001277
1278 mmc_set_bus_speed(card);
1279
1280 return 0;
1281
1282out_err:
1283 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1284 __func__, err);
1285 return err;
1286}
1287
Shawn Lin81ac2af2016-05-26 09:56:22 +08001288static int mmc_select_hs400es(struct mmc_card *card)
1289{
1290 struct mmc_host *host = card->host;
1291 int err = 0;
1292 u8 val;
1293
1294 if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1295 err = -ENOTSUPP;
1296 goto out_err;
1297 }
1298
Shawn Lin1720d352016-09-30 14:18:58 +08001299 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001300 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
Shawn Lin1720d352016-09-30 14:18:58 +08001301
1302 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001303 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
Shawn Lin1720d352016-09-30 14:18:58 +08001304
1305 /* If fails try again during next card power cycle */
1306 if (err)
1307 goto out_err;
1308
Shawn Lin81ac2af2016-05-26 09:56:22 +08001309 err = mmc_select_bus_width(card);
1310 if (err < 0)
1311 goto out_err;
1312
1313 /* Switch card to HS mode */
Ulf Hansson53e60652016-11-09 19:40:29 +01001314 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1315 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1316 card->ext_csd.generic_cmd6_time, 0,
1317 true, false, true);
1318 if (err) {
1319 pr_err("%s: switch to hs for hs400es failed, err:%d\n",
1320 mmc_hostname(host), err);
1321 goto out_err;
1322 }
1323
1324 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1325 err = mmc_switch_status(card);
Jungseung Lee67d35962016-08-24 19:34:09 +09001326 if (err)
Shawn Lin81ac2af2016-05-26 09:56:22 +08001327 goto out_err;
Shawn Lin81ac2af2016-05-26 09:56:22 +08001328
Shawn Lin4f255802016-09-30 14:18:59 +08001329 mmc_set_clock(host, card->ext_csd.hs_max_dtr);
1330
Shawn Lin81ac2af2016-05-26 09:56:22 +08001331 /* Switch card to DDR with strobe bit */
1332 val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1333 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1334 EXT_CSD_BUS_WIDTH,
1335 val,
1336 card->ext_csd.generic_cmd6_time);
1337 if (err) {
1338 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1339 mmc_hostname(host), err);
1340 goto out_err;
1341 }
1342
1343 /* Switch card to HS400 */
1344 val = EXT_CSD_TIMING_HS400 |
1345 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1346 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1347 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001348 card->ext_csd.generic_cmd6_time, 0,
Shawn Lin81ac2af2016-05-26 09:56:22 +08001349 true, false, true);
1350 if (err) {
1351 pr_err("%s: switch to hs400es failed, err:%d\n",
1352 mmc_hostname(host), err);
1353 goto out_err;
1354 }
1355
1356 /* Set host controller to HS400 timing and frequency */
1357 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1358
1359 /* Controller enable enhanced strobe function */
1360 host->ios.enhanced_strobe = true;
1361 if (host->ops->hs400_enhanced_strobe)
1362 host->ops->hs400_enhanced_strobe(host, &host->ios);
1363
1364 err = mmc_switch_status(card);
1365 if (err)
1366 goto out_err;
1367
1368 return 0;
1369
1370out_err:
1371 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1372 __func__, err);
1373 return err;
1374}
1375
Adrian Huntercc4f4142015-02-06 14:12:58 +02001376static void mmc_select_driver_type(struct mmc_card *card)
1377{
1378 int card_drv_type, drive_strength, drv_type;
1379
1380 card_drv_type = card->ext_csd.raw_driver_strength |
1381 mmc_driver_type_mask(0);
1382
1383 drive_strength = mmc_select_drive_strength(card,
1384 card->ext_csd.hs200_max_dtr,
1385 card_drv_type, &drv_type);
1386
1387 card->drive_strength = drive_strength;
1388
1389 if (drv_type)
1390 mmc_set_driver_type(card->host, drv_type);
1391}
1392
Seungwon Jeon577fb132014-04-23 17:08:44 +09001393/*
1394 * For device supporting HS200 mode, the following sequence
1395 * should be done before executing the tuning process.
1396 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1397 * 2. switch to HS200 mode
1398 * 3. set the clock to > 52Mhz and <=200MHz
1399 */
1400static int mmc_select_hs200(struct mmc_card *card)
1401{
1402 struct mmc_host *host = card->host;
Dong Aishenge51534c2016-04-21 00:51:30 +08001403 unsigned int old_timing, old_signal_voltage;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001404 int err = -EINVAL;
Adrian Huntercc4f4142015-02-06 14:12:58 +02001405 u8 val;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001406
Dong Aishenge51534c2016-04-21 00:51:30 +08001407 old_signal_voltage = host->ios.signal_voltage;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001408 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001409 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001410
1411 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001412 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001413
1414 /* If fails try again during next card power cycle */
1415 if (err)
Dong Aishenge51534c2016-04-21 00:51:30 +08001416 return err;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001417
Adrian Huntercc4f4142015-02-06 14:12:58 +02001418 mmc_select_driver_type(card);
1419
Seungwon Jeon577fb132014-04-23 17:08:44 +09001420 /*
1421 * Set the bus width(4 or 8) with host's support and
1422 * switch to HS200 mode if bus width is set successfully.
1423 */
1424 err = mmc_select_bus_width(card);
Peter Griffin8b7be8f2016-06-08 11:27:52 +01001425 if (err > 0) {
Adrian Huntercc4f4142015-02-06 14:12:58 +02001426 val = EXT_CSD_TIMING_HS200 |
1427 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
Ulf Hansson4509f84772014-01-08 16:09:33 +01001428 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Huntercc4f4142015-02-06 14:12:58 +02001429 EXT_CSD_HS_TIMING, val,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001430 card->ext_csd.generic_cmd6_time, 0,
Chaotian Jing08573ea2016-05-19 16:47:41 +08001431 true, false, true);
Adrian Hunter1815e612015-10-28 14:25:40 +02001432 if (err)
1433 goto err;
1434 old_timing = host->ios.timing;
1435 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
Chaotian Jing08573ea2016-05-19 16:47:41 +08001436
Adrian Hunteref3d2322016-12-02 13:16:35 +02001437 /*
1438 * For HS200, CRC errors are not a reliable way to know the
1439 * switch failed. If there really is a problem, we would expect
1440 * tuning will fail and the result ends up the same.
1441 */
1442 err = __mmc_switch_status(card, false);
1443
Chaotian Jing08573ea2016-05-19 16:47:41 +08001444 /*
1445 * mmc_select_timing() assumes timing has not changed if
1446 * it is a switch error.
1447 */
1448 if (err == -EBADMSG)
1449 mmc_set_timing(host, old_timing);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001450 }
Girish K Sa4924c72012-01-11 14:04:52 -05001451err:
Dong Aishenge51534c2016-04-21 00:51:30 +08001452 if (err) {
1453 /* fall back to the old signal voltage, if fails report error */
Ulf Hansson4e74b6b2017-01-25 11:12:34 +01001454 if (mmc_set_signal_voltage(host, old_signal_voltage))
Dong Aishenge51534c2016-04-21 00:51:30 +08001455 err = -EIO;
1456
Adrian Hunter1815e612015-10-28 14:25:40 +02001457 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1458 __func__, err);
Dong Aishenge51534c2016-04-21 00:51:30 +08001459 }
Girish K Sa4924c72012-01-11 14:04:52 -05001460 return err;
1461}
1462
1463/*
Shawn Lin81ac2af2016-05-26 09:56:22 +08001464 * Activate High Speed, HS200 or HS400ES mode if supported.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001465 */
1466static int mmc_select_timing(struct mmc_card *card)
1467{
1468 int err = 0;
1469
Ulf Hansson148bcab2014-10-20 11:33:53 +02001470 if (!mmc_can_ext_csd(card))
Seungwon Jeon577fb132014-04-23 17:08:44 +09001471 goto bus_speed;
1472
Shawn Lin81ac2af2016-05-26 09:56:22 +08001473 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1474 err = mmc_select_hs400es(card);
1475 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
Seungwon Jeon577fb132014-04-23 17:08:44 +09001476 err = mmc_select_hs200(card);
1477 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1478 err = mmc_select_hs(card);
1479
1480 if (err && err != -EBADMSG)
1481 return err;
1482
Seungwon Jeon577fb132014-04-23 17:08:44 +09001483bus_speed:
1484 /*
1485 * Set the bus speed to the selected bus timing.
1486 * If timing is not selected, backward compatible is the default.
1487 */
1488 mmc_set_bus_speed(card);
Dong Aisheng0400ed02016-04-21 00:51:31 +08001489 return 0;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001490}
1491
1492/*
1493 * Execute tuning sequence to seek the proper bus operating
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001494 * conditions for HS200 and HS400, which sends CMD21 to the device.
Seungwon Jeon577fb132014-04-23 17:08:44 +09001495 */
1496static int mmc_hs200_tuning(struct mmc_card *card)
1497{
1498 struct mmc_host *host = card->host;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001499
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001500 /*
1501 * Timing should be adjusted to the HS400 target
1502 * operation frequency for tuning process
1503 */
1504 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1505 host->ios.bus_width == MMC_BUS_WIDTH_8)
1506 if (host->ops->prepare_hs400_tuning)
1507 host->ops->prepare_hs400_tuning(host, &host->ios);
1508
Adrian Hunter63e415c2014-12-05 19:40:59 +02001509 return mmc_execute_tuning(card);
Seungwon Jeon577fb132014-04-23 17:08:44 +09001510}
1511
1512/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001513 * Handle the detection and initialisation of a card.
1514 *
Deepak Saxena87693922008-06-16 19:20:57 -07001515 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001516 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001517 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001518static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001519 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001520{
1521 struct mmc_card *card;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001522 int err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001523 u32 cid[4];
Philip Rakityb676f032011-02-13 23:13:09 -08001524 u32 rocr;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001525
Pierre Ossmand84075c82007-08-09 13:23:56 +02001526 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001527
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001528 /* Set correct bus mode for MMC before attempting init */
1529 if (!mmc_host_is_spi(host))
1530 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1531
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001532 /*
1533 * Since we're changing the OCR value, we seem to
1534 * need to tell some cards to go back to the idle
1535 * state. We wait 1ms to give cards time to
1536 * respond.
Balaji T Kc3805462011-09-08 22:08:39 +05301537 * mmc_go_idle is needed for eMMC that are asleep
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001538 */
1539 mmc_go_idle(host);
1540
1541 /* The extra bit indicates that we support high capacity */
Philip Rakityb676f032011-02-13 23:13:09 -08001542 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +02001543 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001544 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001545
1546 /*
David Brownellaf517152007-08-08 09:11:32 -07001547 * For SPI, enable CRC as appropriate.
1548 */
1549 if (mmc_host_is_spi(host)) {
1550 err = mmc_spi_set_crc(host, use_spi_crc);
1551 if (err)
1552 goto err;
1553 }
1554
1555 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001556 * Fetch CID from card.
1557 */
David Brownellaf517152007-08-08 09:11:32 -07001558 if (mmc_host_is_spi(host))
1559 err = mmc_send_cid(host, cid);
1560 else
1561 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +02001562 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001563 goto err;
1564
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001565 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001566 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1567 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001568 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001569 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001570
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001571 card = oldcard;
1572 } else {
1573 /*
1574 * Allocate card structure.
1575 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +01001576 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001577 if (IS_ERR(card)) {
1578 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001579 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001580 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001581
Ulf Hansson69041152013-09-13 11:31:33 +02001582 card->ocr = ocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001583 card->type = MMC_TYPE_MMC;
1584 card->rca = 1;
1585 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1586 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001587
1588 /*
Doug Andersoneac86322014-12-02 15:42:45 -08001589 * Call the optional HC's init_card function to handle quirks.
1590 */
1591 if (host->ops->init_card)
1592 host->ops->init_card(host, card);
1593
1594 /*
David Brownellaf517152007-08-08 09:11:32 -07001595 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001596 */
David Brownellaf517152007-08-08 09:11:32 -07001597 if (!mmc_host_is_spi(host)) {
1598 err = mmc_set_relative_addr(card);
1599 if (err)
1600 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001601
David Brownellaf517152007-08-08 09:11:32 -07001602 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1603 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001604
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001605 if (!oldcard) {
1606 /*
1607 * Fetch CSD from card.
1608 */
1609 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001610 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001611 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001612
Pierre Ossmanbd766312007-05-01 16:11:57 +02001613 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001614 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001615 goto free_card;
1616 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001617 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001618 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001619 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001620
1621 /*
Sascha Hauer3d705d12014-08-19 10:45:51 +02001622 * handling only for cards supporting DSR and hosts requesting
1623 * DSR configuration
1624 */
1625 if (card->csd.dsr_imp && host->dsr_req)
1626 mmc_set_dsr(host);
1627
1628 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001629 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001630 */
David Brownellaf517152007-08-08 09:11:32 -07001631 if (!mmc_host_is_spi(host)) {
1632 err = mmc_select_card(card);
1633 if (err)
1634 goto free_card;
1635 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001636
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001637 if (!oldcard) {
Ulf Hansson076ec382014-10-20 13:37:24 +02001638 /* Read extended CSD. */
1639 err = mmc_read_ext_csd(card);
Pierre Ossman17b04292007-07-22 22:18:46 +02001640 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001641 goto free_card;
Philip Rakityb676f032011-02-13 23:13:09 -08001642
Masahiro Yamada87e88652016-04-15 20:16:12 +09001643 /*
1644 * If doing byte addressing, check if required to do sector
Philip Rakityb676f032011-02-13 23:13:09 -08001645 * addressing. Handle the case of <2GB cards needing sector
1646 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1647 * ocr register has bit 30 set for sector addressing.
1648 */
Masahiro Yamada87e88652016-04-15 20:16:12 +09001649 if (rocr & BIT(30))
Philip Rakityb676f032011-02-13 23:13:09 -08001650 mmc_card_set_blockaddr(card);
1651
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001652 /* Erase size depends on CSD and Extended CSD */
1653 mmc_set_erase_size(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001654 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001655
1656 /*
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001657 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001658 * bit. This bit will be lost every time after a reset or power off.
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001659 */
Grégory Soutadé69803d42014-09-15 17:47:09 +02001660 if (card->ext_csd.partition_setting_completed ||
Adrian Hunter83bb24a2012-03-13 05:19:13 -04001661 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001662 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001663 EXT_CSD_ERASE_GROUP_DEF, 1,
1664 card->ext_csd.generic_cmd6_time);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +08001665
1666 if (err && err != -EBADMSG)
1667 goto free_card;
1668
1669 if (err) {
1670 err = 0;
1671 /*
1672 * Just disable enhanced area off & sz
1673 * will try to enable ERASE_GROUP_DEF
1674 * during next time reinit
1675 */
1676 card->ext_csd.enhanced_area_offset = -EINVAL;
1677 card->ext_csd.enhanced_area_size = -EINVAL;
1678 } else {
1679 card->ext_csd.erase_group_def = 1;
1680 /*
1681 * enable ERASE_GRP_DEF successfully.
1682 * This will affect the erase size, so
1683 * here need to reset erase size
1684 */
1685 mmc_set_erase_size(card);
1686 }
1687 }
1688
1689 /*
Philip Rakity41e2a482011-03-19 14:10:33 -04001690 * Ensure eMMC user default partition is enabled
1691 */
Andrei Warkentin371a6892011-04-11 18:10:25 -05001692 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1693 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1694 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1695 card->ext_csd.part_config,
1696 card->ext_csd.part_time);
1697 if (err && err != -EBADMSG)
1698 goto free_card;
Philip Rakity41e2a482011-03-19 14:10:33 -04001699 }
1700
1701 /*
Ulf Hansson432356792013-06-10 17:03:45 +02001702 * Enable power_off_notification byte in the ext_csd register
Girish K Sbec87262011-10-13 12:04:16 +05301703 */
Ulf Hansson432356792013-06-10 17:03:45 +02001704 if (card->ext_csd.rev >= 6) {
Girish K Sbec87262011-10-13 12:04:16 +05301705 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1706 EXT_CSD_POWER_OFF_NOTIFICATION,
1707 EXT_CSD_POWER_ON,
1708 card->ext_csd.generic_cmd6_time);
1709 if (err && err != -EBADMSG)
1710 goto free_card;
Girish K Sbec87262011-10-13 12:04:16 +05301711
Girish K S96a85d52011-11-04 16:22:47 +05301712 /*
1713 * The err can be -EBADMSG or 0,
1714 * so check for success and update the flag
1715 */
1716 if (!err)
Ulf Hanssone6c08582012-10-05 12:45:39 -04001717 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
Girish K S96a85d52011-11-04 16:22:47 +05301718 }
Girish K Sbec87262011-10-13 12:04:16 +05301719
1720 /*
Seungwon Jeon577fb132014-04-23 17:08:44 +09001721 * Select timing interface
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001722 */
Seungwon Jeon577fb132014-04-23 17:08:44 +09001723 err = mmc_select_timing(card);
1724 if (err)
1725 goto free_card;
Girish K Sa4924c72012-01-11 14:04:52 -05001726
Girish K Sa4924c72012-01-11 14:04:52 -05001727 if (mmc_card_hs200(card)) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001728 err = mmc_hs200_tuning(card);
1729 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001730 goto free_card;
Seungwon Jeon0a5b6432014-04-23 17:14:58 +09001731
1732 err = mmc_select_hs400(card);
1733 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001734 goto free_card;
Guenter Roeck773dc112017-03-01 14:11:47 -08001735 } else if (!mmc_card_hs400es(card)) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001736 /* Select the desired bus width optionally */
1737 err = mmc_select_bus_width(card);
Anssi Hannula3d4ef322017-02-13 13:46:41 +02001738 if (err > 0 && mmc_card_hs(card)) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001739 err = mmc_select_hs_ddr(card);
1740 if (err)
Andrew Gabbasov4b75bff2014-10-01 07:14:11 -05001741 goto free_card;
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001742 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001743 }
1744
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001745 /*
Seungwon Jeon23850492014-04-23 17:08:05 +09001746 * Choose the power class with selected bus interface
1747 */
1748 mmc_select_powerclass(card);
1749
1750 /*
Subhash Jadavani52d09742012-03-06 17:59:12 +05301751 * Enable HPI feature (if supported)
1752 */
1753 if (card->ext_csd.hpi) {
1754 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1755 EXT_CSD_HPI_MGMT, 1,
1756 card->ext_csd.generic_cmd6_time);
1757 if (err && err != -EBADMSG)
1758 goto free_card;
1759 if (err) {
Joe Perches66061102014-09-12 14:56:56 -07001760 pr_warn("%s: Enabling HPI failed\n",
1761 mmc_hostname(card->host));
Subhash Jadavani52d09742012-03-06 17:59:12 +05301762 err = 0;
1763 } else
1764 card->ext_csd.hpi_en = 1;
1765 }
1766
1767 /*
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001768 * If cache size is higher than 0, this indicates
1769 * the existence of cache and it can be turned on.
1770 */
Pratibhasagar V53202262016-06-09 18:09:31 -04001771 if (!mmc_card_broken_hpi(card) &&
1772 card->ext_csd.cache_size > 0) {
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001773 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001774 EXT_CSD_CACHE_CTRL, 1,
1775 card->ext_csd.generic_cmd6_time);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001776 if (err && err != -EBADMSG)
1777 goto free_card;
1778
1779 /*
1780 * Only if no error, cache is turned on successfully.
1781 */
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001782 if (err) {
Joe Perches66061102014-09-12 14:56:56 -07001783 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1784 mmc_hostname(card->host), err);
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001785 card->ext_csd.cache_ctrl = 0;
1786 err = 0;
1787 } else {
1788 card->ext_csd.cache_ctrl = 1;
1789 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001790 }
1791
Seungwon Jeonabd9ac12013-02-06 17:01:43 +09001792 /*
Adrian Hunter9d4579a2017-03-13 14:36:38 +02001793 * In some cases (e.g. RPMB or mmc_test), the Command Queue must be
1794 * disabled for a time, so a flag is needed to indicate to re-enable the
1795 * Command Queue.
1796 */
1797 card->reenable_cmdq = card->ext_csd.cmdq_en;
1798
1799 /*
Seungwon Jeonabd9ac12013-02-06 17:01:43 +09001800 * The mandatory minimum values are defined for packed command.
1801 * read: 5, write: 3
1802 */
1803 if (card->ext_csd.max_packed_writes >= 3 &&
1804 card->ext_csd.max_packed_reads >= 5 &&
1805 host->caps2 & MMC_CAP2_PACKED_CMD) {
1806 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1807 EXT_CSD_EXP_EVENTS_CTRL,
1808 EXT_CSD_PACKED_EVENT_EN,
1809 card->ext_csd.generic_cmd6_time);
1810 if (err && err != -EBADMSG)
1811 goto free_card;
1812 if (err) {
1813 pr_warn("%s: Enabling packed event failed\n",
1814 mmc_hostname(card->host));
1815 card->ext_csd.packed_event_en = 0;
1816 err = 0;
1817 } else {
1818 card->ext_csd.packed_event_en = 1;
1819 }
1820 }
1821
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001822 if (!oldcard)
1823 host->card = card;
1824
Pierre Ossman17b04292007-07-22 22:18:46 +02001825 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001826
1827free_card:
1828 if (!oldcard)
1829 mmc_remove_card(card);
1830err:
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001831 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001832}
1833
Ulf Hansson07a68212013-04-19 15:12:11 +02001834static int mmc_can_sleep(struct mmc_card *card)
1835{
1836 return (card && card->ext_csd.rev >= 3);
1837}
1838
1839static int mmc_sleep(struct mmc_host *host)
1840{
Masahiro Yamadac7836d12016-12-19 20:51:18 +09001841 struct mmc_command cmd = {};
Ulf Hansson07a68212013-04-19 15:12:11 +02001842 struct mmc_card *card = host->card;
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001843 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
Ulf Hansson07a68212013-04-19 15:12:11 +02001844 int err;
1845
Adrian Hunter436f8da2015-05-07 13:10:18 +03001846 /* Re-tuning can't be done once the card is deselected */
1847 mmc_retune_hold(host);
1848
Ulf Hansson07a68212013-04-19 15:12:11 +02001849 err = mmc_deselect_cards(host);
1850 if (err)
Adrian Hunter436f8da2015-05-07 13:10:18 +03001851 goto out_release;
Ulf Hansson07a68212013-04-19 15:12:11 +02001852
1853 cmd.opcode = MMC_SLEEP_AWAKE;
1854 cmd.arg = card->rca << 16;
1855 cmd.arg |= 1 << 15;
1856
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001857 /*
1858 * If the max_busy_timeout of the host is specified, validate it against
1859 * the sleep cmd timeout. A failure means we need to prevent the host
1860 * from doing hw busy detection, which is done by converting to a R1
1861 * response instead of a R1B.
1862 */
1863 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1864 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1865 } else {
1866 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1867 cmd.busy_timeout = timeout_ms;
1868 }
1869
Ulf Hansson07a68212013-04-19 15:12:11 +02001870 err = mmc_wait_for_cmd(host, &cmd, 0);
1871 if (err)
Adrian Hunter436f8da2015-05-07 13:10:18 +03001872 goto out_release;
Ulf Hansson07a68212013-04-19 15:12:11 +02001873
1874 /*
1875 * If the host does not wait while the card signals busy, then we will
1876 * will have to wait the sleep/awake timeout. Note, we cannot use the
1877 * SEND_STATUS command to poll the status because that command (and most
1878 * others) is invalid while the card sleeps.
1879 */
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001880 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1881 mmc_delay(timeout_ms);
Ulf Hansson07a68212013-04-19 15:12:11 +02001882
Adrian Hunter436f8da2015-05-07 13:10:18 +03001883out_release:
1884 mmc_retune_release(host);
Ulf Hansson07a68212013-04-19 15:12:11 +02001885 return err;
1886}
1887
Ulf Hanssone6c08582012-10-05 12:45:39 -04001888static int mmc_can_poweroff_notify(const struct mmc_card *card)
1889{
1890 return card &&
1891 mmc_card_mmc(card) &&
1892 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1893}
1894
1895static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1896{
1897 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1898 int err;
1899
1900 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1901 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1902 timeout = card->ext_csd.power_off_longtime;
1903
Ulf Hansson878e2002013-09-09 11:57:57 +02001904 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1905 EXT_CSD_POWER_OFF_NOTIFICATION,
Ulf Hanssonaa33ce32016-11-09 17:33:36 +01001906 notify_type, timeout, 0, true, false, false);
Ulf Hanssone6c08582012-10-05 12:45:39 -04001907 if (err)
1908 pr_err("%s: Power Off Notification timed out, %u\n",
1909 mmc_hostname(card->host), timeout);
1910
1911 /* Disable the power off notification after the switch operation. */
1912 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1913
1914 return err;
1915}
1916
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001917/*
1918 * Host is being removed. Free up the current card.
1919 */
1920static void mmc_remove(struct mmc_host *host)
1921{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001922 mmc_remove_card(host->card);
1923 host->card = NULL;
1924}
1925
1926/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001927 * Card detection - card is alive.
1928 */
1929static int mmc_alive(struct mmc_host *host)
1930{
1931 return mmc_send_status(host->card, NULL);
1932}
1933
1934/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001935 * Card detection callback from host.
1936 */
1937static void mmc_detect(struct mmc_host *host)
1938{
1939 int err;
1940
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001941 mmc_get_card(host->card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001942
1943 /*
1944 * Just check if our card has been removed.
1945 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001946 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001947
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001948 mmc_put_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001949
Pierre Ossman17b04292007-07-22 22:18:46 +02001950 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001951 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001952
1953 mmc_claim_host(host);
1954 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001955 mmc_power_off(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001956 mmc_release_host(host);
1957 }
1958}
1959
Ulf Hansson03d071f2013-06-10 17:03:43 +02001960static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001961{
Balaji T Kc3805462011-09-08 22:08:39 +05301962 int err = 0;
Ulf Hansson03d071f2013-06-10 17:03:43 +02001963 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1964 EXT_CSD_POWER_OFF_LONG;
Balaji T Kc3805462011-09-08 22:08:39 +05301965
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001966 mmc_claim_host(host);
Maya Erez881d9262013-01-28 16:44:22 -05001967
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001968 if (mmc_card_suspended(host->card))
1969 goto out;
1970
Ulf Hansson39b94312013-05-02 14:02:36 +02001971 if (mmc_card_doing_bkops(host->card)) {
1972 err = mmc_stop_bkops(host->card);
1973 if (err)
1974 goto out;
1975 }
1976
Ulf Hansson10e5d962013-12-16 16:23:22 +01001977 err = mmc_flush_cache(host->card);
Maya Erez881d9262013-01-28 16:44:22 -05001978 if (err)
1979 goto out;
1980
Ulf Hansson432356792013-06-10 17:03:45 +02001981 if (mmc_can_poweroff_notify(host->card) &&
Ulf Hansson53275c22013-06-10 17:03:46 +02001982 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
Ulf Hansson03d071f2013-06-10 17:03:43 +02001983 err = mmc_poweroff_notify(host->card, notify_type);
Ulf Hansson07a68212013-04-19 15:12:11 +02001984 else if (mmc_can_sleep(host->card))
1985 err = mmc_sleep(host);
Ulf Hanssone6c08582012-10-05 12:45:39 -04001986 else if (!mmc_host_is_spi(host))
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001987 err = mmc_deselect_cards(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001988
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001989 if (!err) {
Ulf Hansson74590262013-06-10 17:03:38 +02001990 mmc_power_off(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001991 mmc_card_set_suspended(host->card);
1992 }
Maya Erez881d9262013-01-28 16:44:22 -05001993out:
1994 mmc_release_host(host);
Balaji T Kc3805462011-09-08 22:08:39 +05301995 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001996}
1997
1998/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001999 * Suspend callback
Ulf Hansson03d071f2013-06-10 17:03:43 +02002000 */
2001static int mmc_suspend(struct mmc_host *host)
2002{
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002003 int err;
2004
2005 err = _mmc_suspend(host, true);
2006 if (!err) {
2007 pm_runtime_disable(&host->card->dev);
2008 pm_runtime_set_suspended(&host->card->dev);
2009 }
2010
2011 return err;
Ulf Hansson03d071f2013-06-10 17:03:43 +02002012}
2013
2014/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002015 * This function tries to determine if the same card is still present
2016 * and, if so, restore all state to it.
2017 */
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002018static int _mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002019{
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002020 int err = 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002021
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002022 mmc_claim_host(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002023
2024 if (!mmc_card_suspended(host->card))
2025 goto out;
2026
Ulf Hansson69041152013-09-13 11:31:33 +02002027 mmc_power_up(host, host->card->ocr);
Ulf Hansson69041152013-09-13 11:31:33 +02002028 err = mmc_init_card(host, host->card->ocr, host->card);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002029 mmc_card_clr_suspended(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002030
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002031out:
2032 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002033 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002034}
2035
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002036/*
2037 * Shutdown callback
2038 */
2039static int mmc_shutdown(struct mmc_host *host)
2040{
2041 int err = 0;
2042
2043 /*
2044 * In a specific case for poweroff notify, we need to resume the card
2045 * before we can shutdown it properly.
2046 */
2047 if (mmc_can_poweroff_notify(host->card) &&
2048 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002049 err = _mmc_resume(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02002050
2051 if (!err)
2052 err = _mmc_suspend(host, false);
2053
2054 return err;
2055}
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002056
2057/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002058 * Callback for resume.
2059 */
2060static int mmc_resume(struct mmc_host *host)
2061{
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002062 pm_runtime_enable(&host->card->dev);
Ulf Hanssonc29536e2015-11-05 16:01:32 +01002063 return 0;
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002064}
2065
2066/*
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002067 * Callback for runtime_suspend.
2068 */
2069static int mmc_runtime_suspend(struct mmc_host *host)
2070{
2071 int err;
2072
2073 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2074 return 0;
2075
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002076 err = _mmc_suspend(host, true);
Ulf Hansson0cc81a82013-10-03 11:24:44 +02002077 if (err)
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09002078 pr_err("%s: error %d doing aggressive suspend\n",
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002079 mmc_hostname(host), err);
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002080
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002081 return err;
2082}
2083
2084/*
2085 * Callback for runtime_resume.
2086 */
2087static int mmc_runtime_resume(struct mmc_host *host)
2088{
2089 int err;
2090
Ulf Hansson0cb403a2013-10-10 14:20:05 +02002091 err = _mmc_resume(host);
Adrian Hunter520322d2015-12-14 15:51:27 +02002092 if (err && err != -ENOMEDIUM)
Ulf Hanssonc29536e2015-11-05 16:01:32 +01002093 pr_err("%s: error %d doing runtime resume\n",
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002094 mmc_hostname(host), err);
2095
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002096 return 0;
2097}
2098
Johan Rudholmf855a372015-01-12 15:38:05 +01002099int mmc_can_reset(struct mmc_card *card)
2100{
2101 u8 rst_n_function;
2102
2103 rst_n_function = card->ext_csd.rst_n_function;
2104 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2105 return 0;
2106 return 1;
2107}
2108EXPORT_SYMBOL(mmc_can_reset);
2109
2110static int mmc_reset(struct mmc_host *host)
2111{
2112 struct mmc_card *card = host->card;
Johan Rudholmf855a372015-01-12 15:38:05 +01002113
Adrian Hunter437db4c2016-05-04 10:38:21 +03002114 /*
2115 * In the case of recovery, we can't expect flushing the cache to work
2116 * always, but we have a go and ignore errors.
2117 */
2118 mmc_flush_cache(host->card);
2119
Gwendal Grignou4e6c7172016-04-01 16:04:22 -07002120 if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2121 mmc_can_reset(card)) {
2122 /* If the card accept RST_n signal, send it. */
2123 mmc_set_clock(host, host->f_init);
2124 host->ops->hw_reset(host);
2125 /* Set initial state and call mmc_set_ios */
2126 mmc_set_initial_state(host);
2127 } else {
2128 /* Do a brute force power cycle */
2129 mmc_power_cycle(host, card->ocr);
2130 }
Ulf Hansson364549d2015-06-01 11:14:57 +02002131 return mmc_init_card(host, card->ocr, card);
Johan Rudholmf855a372015-01-12 15:38:05 +01002132}
2133
Adrian Hunter9feae242009-09-22 16:44:32 -07002134static const struct mmc_bus_ops mmc_ops = {
2135 .remove = mmc_remove,
2136 .detect = mmc_detect,
Adrian Hunter9feae242009-09-22 16:44:32 -07002137 .suspend = mmc_suspend,
2138 .resume = mmc_resume,
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02002139 .runtime_suspend = mmc_runtime_suspend,
2140 .runtime_resume = mmc_runtime_resume,
Adrian Hunterd3049502011-11-28 16:22:00 +02002141 .alive = mmc_alive,
Ulf Hansson486fdbb2013-06-10 17:03:44 +02002142 .shutdown = mmc_shutdown,
Johan Rudholmf855a372015-01-12 15:38:05 +01002143 .reset = mmc_reset,
Adrian Hunter9feae242009-09-22 16:44:32 -07002144};
2145
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002146/*
2147 * Starting point for MMC card init.
2148 */
Andy Ross807e8e42011-01-03 10:36:56 -08002149int mmc_attach_mmc(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002150{
2151 int err;
Ulf Hansson69041152013-09-13 11:31:33 +02002152 u32 ocr, rocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002153
Pierre Ossmand84075c82007-08-09 13:23:56 +02002154 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002155
Stefan Nilsson XK44669032011-09-15 17:50:38 +02002156 /* Set correct bus mode for MMC before attempting attach */
2157 if (!mmc_host_is_spi(host))
2158 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2159
Andy Ross807e8e42011-01-03 10:36:56 -08002160 err = mmc_send_op_cond(host, 0, &ocr);
2161 if (err)
2162 return err;
2163
Ulf Hansson2501c912013-10-30 01:00:18 +01002164 mmc_attach_bus(host, &mmc_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01002165 if (host->ocr_avail_mmc)
2166 host->ocr_avail = host->ocr_avail_mmc;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002167
2168 /*
David Brownellaf517152007-08-08 09:11:32 -07002169 * We need to get OCR a different way for SPI.
2170 */
2171 if (mmc_host_is_spi(host)) {
2172 err = mmc_spi_read_ocr(host, 1, &ocr);
2173 if (err)
2174 goto err;
2175 }
2176
Ulf Hansson69041152013-09-13 11:31:33 +02002177 rocr = mmc_select_voltage(host, ocr);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002178
2179 /*
2180 * Can we support the voltage of the card?
2181 */
Ulf Hansson69041152013-09-13 11:31:33 +02002182 if (!rocr) {
Pierre Ossman109b5be2007-07-23 00:12:10 +02002183 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002184 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02002185 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002186
2187 /*
2188 * Detect and init the card.
2189 */
Ulf Hansson69041152013-09-13 11:31:33 +02002190 err = mmc_init_card(host, rocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02002191 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002192 goto err;
2193
2194 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02002195 err = mmc_add_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002196 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002197 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002198
Sergei Shtylyov2860d062015-10-14 23:53:03 +03002199 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002200 return 0;
2201
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002202remove_card:
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002203 mmc_remove_card(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02002204 mmc_claim_host(host);
Andy Ross807e8e42011-01-03 10:36:56 -08002205 host->card = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002206err:
2207 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002208
Girish K Sa3c76eb2011-10-11 11:44:09 +05302209 pr_err("%s: error %d whilst initialising MMC card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02002210 mmc_hostname(host), err);
2211
Pierre Ossmanadf66a02007-07-22 23:08:30 +02002212 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002213}