blob: 258b203397aa88a381a9e4f007eacf77b9452aa3 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Paul Gortmaker0205a902011-07-15 12:01:27 -040015#include <linux/stat.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010016
17#include <linux/mmc/host.h>
18#include <linux/mmc/card.h>
19#include <linux/mmc/mmc.h>
20
21#include "core.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020022#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010023#include "mmc_ops.h"
Philip Rakity4c4cb172011-05-13 11:17:18 +053024#include "sd_ops.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010025
26static const unsigned int tran_exp[] = {
27 10000, 100000, 1000000, 10000000,
28 0, 0, 0, 0
29};
30
31static const unsigned char tran_mant[] = {
32 0, 10, 12, 13, 15, 20, 25, 30,
33 35, 40, 45, 50, 55, 60, 70, 80,
34};
35
36static const unsigned int tacc_exp[] = {
37 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
38};
39
40static const unsigned int tacc_mant[] = {
41 0, 10, 12, 13, 15, 20, 25, 30,
42 35, 40, 45, 50, 55, 60, 70, 80,
43};
44
45#define UNSTUFF_BITS(resp,start,size) \
46 ({ \
47 const int __size = size; \
48 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
49 const int __off = 3 - ((start) / 32); \
50 const int __shft = (start) & 31; \
51 u32 __res; \
52 \
53 __res = resp[__off] >> __shft; \
54 if (__size + __shft > 32) \
55 __res |= resp[__off-1] << ((32 - __shft) % 32); \
56 __res & __mask; \
57 })
58
59/*
60 * Given the decoded CSD structure, decode the raw CID to our CID structure.
61 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020062static int mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010063{
64 u32 *resp = card->raw_cid;
65
66 /*
67 * The selection of the format here is based upon published
68 * specs from sandisk and from what people have reported.
69 */
70 switch (card->csd.mmca_vsn) {
71 case 0: /* MMC v1.0 - v1.2 */
72 case 1: /* MMC v1.4 */
73 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
74 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
75 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
76 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
77 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
78 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
79 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
80 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
81 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
82 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
83 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
84 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
85 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
86 break;
87
88 case 2: /* MMC v2.0 - v2.2 */
89 case 3: /* MMC v3.1 - v3.3 */
90 case 4: /* MMC v4 */
91 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
92 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
93 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
94 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
95 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
96 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
97 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
98 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
99 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
100 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
101 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
102 break;
103
104 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530105 pr_err("%s: card has unknown MMCA version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100106 mmc_hostname(card->host), card->csd.mmca_vsn);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200107 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100108 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200109
110 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100111}
112
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700113static void mmc_set_erase_size(struct mmc_card *card)
114{
115 if (card->ext_csd.erase_group_def & 1)
116 card->erase_size = card->ext_csd.hc_erase_size;
117 else
118 card->erase_size = card->csd.erase_size;
119
120 mmc_init_erase(card);
121}
122
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100123/*
124 * Given a 128-bit response, decode to our card CSD structure.
125 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200126static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100127{
128 struct mmc_csd *csd = &card->csd;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700129 unsigned int e, m, a, b;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100130 u32 *resp = card->raw_csd;
131
132 /*
133 * We only understand CSD structure v1.1 and v1.2.
134 * v1.2 has extra information in bits 15, 11 and 10.
Kyungmin Park6da24b72010-08-10 18:01:36 -0700135 * We also support eMMC v4.4 & v4.41.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100136 */
Kyungmin Park6da24b72010-08-10 18:01:36 -0700137 csd->structure = UNSTUFF_BITS(resp, 126, 2);
138 if (csd->structure == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530139 pr_err("%s: unrecognised CSD structure version %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700140 mmc_hostname(card->host), csd->structure);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200141 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100142 }
143
144 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
145 m = UNSTUFF_BITS(resp, 115, 4);
146 e = UNSTUFF_BITS(resp, 112, 3);
147 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
148 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
149
150 m = UNSTUFF_BITS(resp, 99, 4);
151 e = UNSTUFF_BITS(resp, 96, 3);
152 csd->max_dtr = tran_exp[e] * tran_mant[m];
153 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
154
155 e = UNSTUFF_BITS(resp, 47, 3);
156 m = UNSTUFF_BITS(resp, 62, 12);
157 csd->capacity = (1 + m) << (e + 2);
158
159 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
160 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
161 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
162 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
163 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
164 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
165 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200166
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700167 if (csd->write_blkbits >= 9) {
168 a = UNSTUFF_BITS(resp, 42, 5);
169 b = UNSTUFF_BITS(resp, 37, 5);
170 csd->erase_size = (a + 1) * (b + 1);
171 csd->erase_size <<= csd->write_blkbits - 9;
172 }
173
Pierre Ossmanbd766312007-05-01 16:11:57 +0200174 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100175}
176
177/*
Philip Rakity08ee80c2011-05-24 18:14:58 -0700178 * Read extended CSD.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100179 */
Philip Rakity08ee80c2011-05-24 18:14:58 -0700180static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100181{
182 int err;
183 u8 *ext_csd;
184
185 BUG_ON(!card);
Philip Rakity08ee80c2011-05-24 18:14:58 -0700186 BUG_ON(!new_ext_csd);
187
188 *new_ext_csd = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100189
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100190 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
Pierre Ossman17b04292007-07-22 22:18:46 +0200191 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100192
193 /*
194 * As the ext_csd is so large and mostly unused, we don't store the
195 * raw block in mmc_card.
196 */
197 ext_csd = kmalloc(512, GFP_KERNEL);
198 if (!ext_csd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530199 pr_err("%s: could not allocate a buffer to "
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200200 "receive the ext_csd.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200201 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100202 }
203
204 err = mmc_send_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200205 if (err) {
Philip Rakity08ee80c2011-05-24 18:14:58 -0700206 kfree(ext_csd);
207 *new_ext_csd = NULL;
208
Wolfgang Mueesd08ebed2009-09-22 16:45:26 -0700209 /* If the host or the card can't do the switch,
210 * fail more gracefully. */
211 if ((err != -EINVAL)
212 && (err != -ENOSYS)
213 && (err != -EFAULT))
Philip Rakity08ee80c2011-05-24 18:14:58 -0700214 return err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200215
216 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100217 * High capacity cards should have this "magic" size
218 * stored in their CSD.
219 */
220 if (card->csd.capacity == (4096 * 512)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530221 pr_err("%s: unable to read EXT_CSD "
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100222 "on a possible high capacity card. "
223 "Card will be ignored.\n",
224 mmc_hostname(card->host));
225 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530226 pr_warning("%s: unable to read "
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100227 "EXT_CSD, performance might "
228 "suffer.\n",
229 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200230 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100231 }
Philip Rakity08ee80c2011-05-24 18:14:58 -0700232 } else
233 *new_ext_csd = ext_csd;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200234
Philip Rakity08ee80c2011-05-24 18:14:58 -0700235 return err;
236}
237
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900238static void mmc_select_card_type(struct mmc_card *card)
239{
240 struct mmc_host *host = card->host;
241 u8 card_type = card->ext_csd.raw_card_type & EXT_CSD_CARD_TYPE_MASK;
242 unsigned int caps = host->caps, caps2 = host->caps2;
243 unsigned int hs_max_dtr = 0;
244
245 if (card_type & EXT_CSD_CARD_TYPE_26)
246 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
247
248 if (caps & MMC_CAP_MMC_HIGHSPEED &&
249 card_type & EXT_CSD_CARD_TYPE_52)
250 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
251
252 if ((caps & MMC_CAP_1_8V_DDR &&
253 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) ||
254 (caps & MMC_CAP_1_2V_DDR &&
255 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V))
256 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
257
258 if ((caps2 & MMC_CAP2_HS200_1_8V_SDR &&
259 card_type & EXT_CSD_CARD_TYPE_SDR_1_8V) ||
260 (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
261 card_type & EXT_CSD_CARD_TYPE_SDR_1_2V))
262 hs_max_dtr = MMC_HS200_MAX_DTR;
263
264 card->ext_csd.hs_max_dtr = hs_max_dtr;
265 card->ext_csd.card_type = card_type;
266}
267
Philip Rakity08ee80c2011-05-24 18:14:58 -0700268/*
269 * Decode extended CSD.
270 */
271static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
272{
Namjae Jeone0c368d2011-10-06 23:41:38 +0900273 int err = 0, idx;
274 unsigned int part_size;
275 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700276
277 BUG_ON(!card);
278
279 if (!ext_csd)
280 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100281
Kyungmin Park6da24b72010-08-10 18:01:36 -0700282 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700283 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
Kyungmin Park6da24b72010-08-10 18:01:36 -0700284 if (card->csd.structure == 3) {
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700285 if (card->ext_csd.raw_ext_csd_structure > 2) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530286 pr_err("%s: unrecognised EXT_CSD structure "
Kyungmin Park6da24b72010-08-10 18:01:36 -0700287 "version %d\n", mmc_hostname(card->host),
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700288 card->ext_csd.raw_ext_csd_structure);
Kyungmin Park6da24b72010-08-10 18:01:36 -0700289 err = -EINVAL;
290 goto out;
291 }
292 }
293
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700294 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Kyungmin Park38ca2852011-07-26 17:12:37 +0900295 if (card->ext_csd.rev > 6) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530296 pr_err("%s: unrecognised EXT_CSD revision %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700297 mmc_hostname(card->host), card->ext_csd.rev);
Florin Malita00cedfa2007-10-13 12:27:20 -0400298 err = -EINVAL;
299 goto out;
Pierre Ossmand7604d72007-07-23 00:34:07 +0200300 }
301
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700302 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
303 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
304 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
305 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700306 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200307 card->ext_csd.sectors =
308 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
309 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
310 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
311 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700312
313 /* Cards with density > 2GiB are sector addressed */
314 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200315 mmc_card_set_blockaddr(card);
316 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900317
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700318 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900319 mmc_select_card_type(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100320
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700321 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
322 card->ext_csd.raw_erase_timeout_mult =
323 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
324 card->ext_csd.raw_hc_erase_grp_size =
325 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700326 if (card->ext_csd.rev >= 3) {
327 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500328 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
329
330 /* EXT_CSD value is in units of 10ms, but we store in ms */
331 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700332
333 /* Sleep / awake timeout in 100ns units */
334 if (sa_shift > 0 && sa_shift <= 0x17)
335 card->ext_csd.sa_timeout =
336 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700337 card->ext_csd.erase_group_def =
338 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
339 card->ext_csd.hc_erase_timeout = 300 *
340 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
341 card->ext_csd.hc_erase_size =
342 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500343
344 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500345
346 /*
347 * There are two boot regions of equal size, defined in
348 * multiples of 128K.
349 */
Namjae Jeone0c368d2011-10-06 23:41:38 +0900350 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
351 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
352 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
353 mmc_part_add(card, part_size,
354 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100355 "boot%d", idx, true,
356 MMC_BLK_DATA_AREA_BOOT);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900357 }
358 }
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700359 }
360
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700361 card->ext_csd.raw_hc_erase_gap_size =
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100362 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700363 card->ext_csd.raw_sec_trim_mult =
364 ext_csd[EXT_CSD_SEC_TRIM_MULT];
365 card->ext_csd.raw_sec_erase_mult =
366 ext_csd[EXT_CSD_SEC_ERASE_MULT];
367 card->ext_csd.raw_sec_feature_support =
368 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
369 card->ext_csd.raw_trim_mult =
370 ext_csd[EXT_CSD_TRIM_MULT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700371 if (card->ext_csd.rev >= 4) {
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800372 /*
373 * Enhanced area feature support -- check whether the eMMC
374 * card has the Enhanced area enabled. If so, export enhanced
375 * area offset and size to user by adding sysfs interface.
376 */
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400377 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800378 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700379 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
Namjae Jeone0c368d2011-10-06 23:41:38 +0900380 hc_erase_grp_sz =
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800381 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Namjae Jeone0c368d2011-10-06 23:41:38 +0900382 hc_wp_grp_sz =
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800383 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
384
385 card->ext_csd.enhanced_area_en = 1;
386 /*
387 * calculate the enhanced data area offset, in bytes
388 */
389 card->ext_csd.enhanced_area_offset =
390 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
391 (ext_csd[137] << 8) + ext_csd[136];
392 if (mmc_card_blockaddr(card))
393 card->ext_csd.enhanced_area_offset <<= 9;
394 /*
395 * calculate the enhanced data area size, in kilobytes
396 */
397 card->ext_csd.enhanced_area_size =
398 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
399 ext_csd[140];
400 card->ext_csd.enhanced_area_size *=
401 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
402 card->ext_csd.enhanced_area_size <<= 9;
403 } else {
404 /*
405 * If the enhanced area is not enabled, disable these
406 * device attributes.
407 */
408 card->ext_csd.enhanced_area_offset = -EINVAL;
409 card->ext_csd.enhanced_area_size = -EINVAL;
410 }
Namjae Jeone0c368d2011-10-06 23:41:38 +0900411
412 /*
413 * General purpose partition feature support --
414 * If ext_csd has the size of general purpose partitions,
415 * set size, part_cfg, partition name in mmc_part.
416 */
417 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
418 EXT_CSD_PART_SUPPORT_PART_EN) {
419 if (card->ext_csd.enhanced_area_en != 1) {
420 hc_erase_grp_sz =
421 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
422 hc_wp_grp_sz =
423 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
424
425 card->ext_csd.enhanced_area_en = 1;
426 }
427
428 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
429 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
430 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
431 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
432 continue;
433 part_size =
434 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
435 << 16) +
436 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
437 << 8) +
438 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
439 part_size *= (size_t)(hc_erase_grp_sz *
440 hc_wp_grp_sz);
441 mmc_part_add(card, part_size << 19,
442 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100443 "gp%d", idx, false,
444 MMC_BLK_DATA_AREA_GP);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900445 }
446 }
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700447 card->ext_csd.sec_trim_mult =
448 ext_csd[EXT_CSD_SEC_TRIM_MULT];
449 card->ext_csd.sec_erase_mult =
450 ext_csd[EXT_CSD_SEC_ERASE_MULT];
451 card->ext_csd.sec_feature_support =
452 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
453 card->ext_csd.trim_timeout = 300 *
454 ext_csd[EXT_CSD_TRIM_MULT];
Johan Rudholmadd710e2011-12-02 08:51:06 +0100455
456 /*
457 * Note that the call to mmc_part_add above defaults to read
458 * only. If this default assumption is changed, the call must
459 * take into account the value of boot_locked below.
460 */
461 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
462 card->ext_csd.boot_ro_lockable = true;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700463 }
464
Adrian Hunterb2499512011-08-29 16:42:11 +0300465 if (card->ext_csd.rev >= 5) {
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400466 /* check whether the eMMC card supports HPI */
467 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
468 card->ext_csd.hpi = 1;
469 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
470 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
471 else
472 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
473 /*
474 * Indicate the maximum timeout to close
475 * a command interrupted by HPI
476 */
477 card->ext_csd.out_of_int_time =
478 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
479 }
480
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500481 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
Adrian Hunterb2499512011-08-29 16:42:11 +0300482 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
483 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500484
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400485 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700486 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
487 card->erased_byte = 0xFF;
488 else
489 card->erased_byte = 0x0;
490
Seungwon Jeon336c7162011-10-25 09:43:12 +0900491 /* eMMC v4.5 or later */
Girish K Sbec87262011-10-13 12:04:16 +0530492 if (card->ext_csd.rev >= 6) {
Seungwon Jeon336c7162011-10-25 09:43:12 +0900493 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
494
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900495 card->ext_csd.generic_cmd6_time = 10 *
496 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
Girish K Sbec87262011-10-13 12:04:16 +0530497 card->ext_csd.power_off_longtime = 10 *
498 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900499
Seungwon Jeon336c7162011-10-25 09:43:12 +0900500 card->ext_csd.cache_size =
501 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
502 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
503 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
504 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
Saugata Das42659002011-12-21 13:09:17 +0530505
506 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
507 card->ext_csd.data_sector_size = 4096;
508 else
509 card->ext_csd.data_sector_size = 512;
510
511 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
512 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
513 card->ext_csd.data_tag_unit_size =
514 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
515 (card->ext_csd.data_sector_size);
516 } else {
517 card->ext_csd.data_tag_unit_size = 0;
518 }
Saugata Dasa5075eb2012-05-17 16:32:21 +0530519 } else {
520 card->ext_csd.data_sector_size = 512;
Seungwon Jeon336c7162011-10-25 09:43:12 +0900521 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900522
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100523out:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700524 return err;
525}
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100526
Philip Rakity08ee80c2011-05-24 18:14:58 -0700527static inline void mmc_free_ext_csd(u8 *ext_csd)
528{
529 kfree(ext_csd);
530}
531
532
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700533static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700534{
535 u8 *bw_ext_csd;
536 int err;
537
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700538 if (bus_width == MMC_BUS_WIDTH_1)
539 return 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700540
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700541 err = mmc_get_ext_csd(card, &bw_ext_csd);
542
543 if (err || bw_ext_csd == NULL) {
Philippe De Swertf6f44592012-04-11 23:32:05 +0300544 err = -EINVAL;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700545 goto out;
546 }
547
Philip Rakity08ee80c2011-05-24 18:14:58 -0700548 /* only compare read only fields */
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100549 err = !((card->ext_csd.raw_partition_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700550 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700551 (card->ext_csd.raw_erased_mem_count ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700552 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700553 (card->ext_csd.rev ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700554 bw_ext_csd[EXT_CSD_REV]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700555 (card->ext_csd.raw_ext_csd_structure ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700556 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700557 (card->ext_csd.raw_card_type ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700558 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700559 (card->ext_csd.raw_s_a_timeout ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700560 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700561 (card->ext_csd.raw_hc_erase_gap_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700562 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700563 (card->ext_csd.raw_erase_timeout_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700564 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700565 (card->ext_csd.raw_hc_erase_grp_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700566 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700567 (card->ext_csd.raw_sec_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700568 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700569 (card->ext_csd.raw_sec_erase_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700570 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700571 (card->ext_csd.raw_sec_feature_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700572 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700573 (card->ext_csd.raw_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700574 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700575 (card->ext_csd.raw_sectors[0] ==
576 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
577 (card->ext_csd.raw_sectors[1] ==
578 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
579 (card->ext_csd.raw_sectors[2] ==
580 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
581 (card->ext_csd.raw_sectors[3] ==
582 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
Philip Rakity08ee80c2011-05-24 18:14:58 -0700583 if (err)
584 err = -EINVAL;
585
586out:
587 mmc_free_ext_csd(bw_ext_csd);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100588 return err;
589}
590
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100591MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
592 card->raw_cid[2], card->raw_cid[3]);
593MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
594 card->raw_csd[2], card->raw_csd[3]);
595MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700596MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
597MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100598MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
599MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
600MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
601MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
602MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
603MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800604MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
605 card->ext_csd.enhanced_area_offset);
606MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100607
608static struct attribute *mmc_std_attrs[] = {
609 &dev_attr_cid.attr,
610 &dev_attr_csd.attr,
611 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700612 &dev_attr_erase_size.attr,
613 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100614 &dev_attr_fwrev.attr,
615 &dev_attr_hwrev.attr,
616 &dev_attr_manfid.attr,
617 &dev_attr_name.attr,
618 &dev_attr_oemid.attr,
619 &dev_attr_serial.attr,
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800620 &dev_attr_enhanced_area_offset.attr,
621 &dev_attr_enhanced_area_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100622 NULL,
623};
624
625static struct attribute_group mmc_std_attr_group = {
626 .attrs = mmc_std_attrs,
627};
628
David Brownella4dbd672009-06-24 10:06:31 -0700629static const struct attribute_group *mmc_attr_groups[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100630 &mmc_std_attr_group,
631 NULL,
632};
633
634static struct device_type mmc_type = {
635 .groups = mmc_attr_groups,
636};
637
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100638/*
Girish K Sb87d8db2011-09-23 20:41:47 +0530639 * Select the PowerClass for the current bus width
640 * If power class is defined for 4/8 bit bus in the
641 * extended CSD register, select it by executing the
642 * mmc_switch command.
643 */
644static int mmc_select_powerclass(struct mmc_card *card,
645 unsigned int bus_width, u8 *ext_csd)
646{
647 int err = 0;
648 unsigned int pwrclass_val;
649 unsigned int index = 0;
650 struct mmc_host *host;
651
652 BUG_ON(!card);
653
654 host = card->host;
655 BUG_ON(!host);
656
657 if (ext_csd == NULL)
658 return 0;
659
660 /* Power class selection is supported for versions >= 4.0 */
661 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
662 return 0;
663
664 /* Power class values are defined only for 4/8 bit bus */
665 if (bus_width == EXT_CSD_BUS_WIDTH_1)
666 return 0;
667
668 switch (1 << host->ios.vdd) {
669 case MMC_VDD_165_195:
670 if (host->ios.clock <= 26000000)
671 index = EXT_CSD_PWR_CL_26_195;
672 else if (host->ios.clock <= 52000000)
673 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
674 EXT_CSD_PWR_CL_52_195 :
675 EXT_CSD_PWR_CL_DDR_52_195;
676 else if (host->ios.clock <= 200000000)
677 index = EXT_CSD_PWR_CL_200_195;
678 break;
Subhash Jadavani93fc5a42012-04-03 12:25:58 +0530679 case MMC_VDD_27_28:
680 case MMC_VDD_28_29:
681 case MMC_VDD_29_30:
682 case MMC_VDD_30_31:
683 case MMC_VDD_31_32:
Girish K Sb87d8db2011-09-23 20:41:47 +0530684 case MMC_VDD_32_33:
685 case MMC_VDD_33_34:
686 case MMC_VDD_34_35:
687 case MMC_VDD_35_36:
688 if (host->ios.clock <= 26000000)
689 index = EXT_CSD_PWR_CL_26_360;
690 else if (host->ios.clock <= 52000000)
691 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
692 EXT_CSD_PWR_CL_52_360 :
693 EXT_CSD_PWR_CL_DDR_52_360;
694 else if (host->ios.clock <= 200000000)
695 index = EXT_CSD_PWR_CL_200_360;
696 break;
697 default:
698 pr_warning("%s: Voltage range not supported "
699 "for power class.\n", mmc_hostname(host));
700 return -EINVAL;
701 }
702
703 pwrclass_val = ext_csd[index];
704
705 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
706 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
707 EXT_CSD_PWR_CL_8BIT_SHIFT;
708 else
709 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
710 EXT_CSD_PWR_CL_4BIT_SHIFT;
711
712 /* If the power class is different from the default value */
713 if (pwrclass_val > 0) {
714 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
715 EXT_CSD_POWER_CLASS,
716 pwrclass_val,
Seungwon Jeon71fe3eb2011-10-18 13:20:57 +0900717 card->ext_csd.generic_cmd6_time);
Girish K Sb87d8db2011-09-23 20:41:47 +0530718 }
719
Subhash Jadavani3d935762012-04-12 19:36:07 +0530720 if (err)
721 pr_err("%s: power class selection for ext_csd_bus_width %d"
722 " failed\n", mmc_hostname(card->host), bus_width);
723
Girish K Sb87d8db2011-09-23 20:41:47 +0530724 return err;
725}
726
727/*
Girish K Sa4924c72012-01-11 14:04:52 -0500728 * Selects the desired buswidth and switch to the HS200 mode
729 * if bus width set without error
730 */
731static int mmc_select_hs200(struct mmc_card *card)
732{
Seungwon Jeon10942aa2012-04-25 16:18:01 +0900733 int idx, err = -EINVAL;
Girish K Sa4924c72012-01-11 14:04:52 -0500734 struct mmc_host *host;
735 static unsigned ext_csd_bits[] = {
736 EXT_CSD_BUS_WIDTH_4,
737 EXT_CSD_BUS_WIDTH_8,
738 };
739 static unsigned bus_widths[] = {
740 MMC_BUS_WIDTH_4,
741 MMC_BUS_WIDTH_8,
742 };
743
744 BUG_ON(!card);
745
746 host = card->host;
747
748 if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V &&
Seungwon Jeon10942aa2012-04-25 16:18:01 +0900749 host->caps2 & MMC_CAP2_HS200_1_2V_SDR)
750 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120, 0);
751
752 if (err && card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_8V &&
753 host->caps2 & MMC_CAP2_HS200_1_8V_SDR)
754 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, 0);
Girish K Sa4924c72012-01-11 14:04:52 -0500755
756 /* If fails try again during next card power cycle */
757 if (err)
758 goto err;
759
760 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0;
761
762 /*
763 * Unlike SD, MMC cards dont have a configuration register to notify
764 * supported bus width. So bus test command should be run to identify
765 * the supported bus width or compare the ext csd values of current
766 * bus width and ext csd values of 1 bit mode read earlier.
767 */
768 for (; idx >= 0; idx--) {
769
770 /*
771 * Host is capable of 8bit transfer, then switch
772 * the device to work in 8bit transfer mode. If the
773 * mmc switch command returns error then switch to
774 * 4bit transfer mode. On success set the corresponding
775 * bus width on the host.
776 */
777 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
778 EXT_CSD_BUS_WIDTH,
779 ext_csd_bits[idx],
780 card->ext_csd.generic_cmd6_time);
781 if (err)
782 continue;
783
784 mmc_set_bus_width(card->host, bus_widths[idx]);
785
786 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
787 err = mmc_compare_ext_csds(card, bus_widths[idx]);
788 else
789 err = mmc_bus_test(card, bus_widths[idx]);
790 if (!err)
791 break;
792 }
793
794 /* switch to HS200 mode if bus width set successfully */
795 if (!err)
796 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
797 EXT_CSD_HS_TIMING, 2, 0);
798err:
799 return err;
800}
801
802/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200803 * Handle the detection and initialisation of a card.
804 *
Deepak Saxena87693922008-06-16 19:20:57 -0700805 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200806 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100807 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +0200808static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200809 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100810{
811 struct mmc_card *card;
Philip Rakity25d5c692010-11-07 16:22:28 -0500812 int err, ddr = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100813 u32 cid[4];
814 unsigned int max_dtr;
Philip Rakityb676f032011-02-13 23:13:09 -0800815 u32 rocr;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700816 u8 *ext_csd = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100817
818 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200819 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100820
Stefan Nilsson XK44669032011-09-15 17:50:38 +0200821 /* Set correct bus mode for MMC before attempting init */
822 if (!mmc_host_is_spi(host))
823 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
824
Ulf Hanssone7747472012-03-01 13:18:05 +0100825 /* Initialization should be done at 3.3 V I/O voltage. */
826 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
827
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100828 /*
829 * Since we're changing the OCR value, we seem to
830 * need to tell some cards to go back to the idle
831 * state. We wait 1ms to give cards time to
832 * respond.
Balaji T Kc3805462011-09-08 22:08:39 +0530833 * mmc_go_idle is needed for eMMC that are asleep
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100834 */
835 mmc_go_idle(host);
836
837 /* The extra bit indicates that we support high capacity */
Philip Rakityb676f032011-02-13 23:13:09 -0800838 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200839 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200840 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100841
842 /*
David Brownellaf517152007-08-08 09:11:32 -0700843 * For SPI, enable CRC as appropriate.
844 */
845 if (mmc_host_is_spi(host)) {
846 err = mmc_spi_set_crc(host, use_spi_crc);
847 if (err)
848 goto err;
849 }
850
851 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100852 * Fetch CID from card.
853 */
David Brownellaf517152007-08-08 09:11:32 -0700854 if (mmc_host_is_spi(host))
855 err = mmc_send_cid(host, cid);
856 else
857 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +0200858 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100859 goto err;
860
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200861 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200862 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
863 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200864 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200865 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100866
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200867 card = oldcard;
868 } else {
869 /*
870 * Allocate card structure.
871 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100872 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200873 if (IS_ERR(card)) {
874 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200875 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200876 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200877
878 card->type = MMC_TYPE_MMC;
879 card->rca = 1;
880 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
881 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100882
883 /*
David Brownellaf517152007-08-08 09:11:32 -0700884 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100885 */
David Brownellaf517152007-08-08 09:11:32 -0700886 if (!mmc_host_is_spi(host)) {
887 err = mmc_set_relative_addr(card);
888 if (err)
889 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100890
David Brownellaf517152007-08-08 09:11:32 -0700891 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
892 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100893
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200894 if (!oldcard) {
895 /*
896 * Fetch CSD from card.
897 */
898 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200899 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200900 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100901
Pierre Ossmanbd766312007-05-01 16:11:57 +0200902 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200903 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +0200904 goto free_card;
905 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200906 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +0200907 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200908 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100909
910 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200911 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100912 */
David Brownellaf517152007-08-08 09:11:32 -0700913 if (!mmc_host_is_spi(host)) {
914 err = mmc_select_card(card);
915 if (err)
916 goto free_card;
917 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100918
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200919 if (!oldcard) {
920 /*
David Brownellaf517152007-08-08 09:11:32 -0700921 * Fetch and process extended CSD.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200922 */
Philip Rakity08ee80c2011-05-24 18:14:58 -0700923
924 err = mmc_get_ext_csd(card, &ext_csd);
925 if (err)
926 goto free_card;
927 err = mmc_read_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200928 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200929 goto free_card;
Philip Rakityb676f032011-02-13 23:13:09 -0800930
931 /* If doing byte addressing, check if required to do sector
932 * addressing. Handle the case of <2GB cards needing sector
933 * addressing. See section 8.1 JEDEC Standard JED84-A441;
934 * ocr register has bit 30 set for sector addressing.
935 */
936 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
937 mmc_card_set_blockaddr(card);
938
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700939 /* Erase size depends on CSD and Extended CSD */
940 mmc_set_erase_size(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200941 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100942
943 /*
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800944 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300945 * bit. This bit will be lost every time after a reset or power off.
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800946 */
Adrian Hunter83bb24a2012-03-13 05:19:13 -0400947 if (card->ext_csd.enhanced_area_en ||
948 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800949 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900950 EXT_CSD_ERASE_GROUP_DEF, 1,
951 card->ext_csd.generic_cmd6_time);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800952
953 if (err && err != -EBADMSG)
954 goto free_card;
955
956 if (err) {
957 err = 0;
958 /*
959 * Just disable enhanced area off & sz
960 * will try to enable ERASE_GROUP_DEF
961 * during next time reinit
962 */
963 card->ext_csd.enhanced_area_offset = -EINVAL;
964 card->ext_csd.enhanced_area_size = -EINVAL;
965 } else {
966 card->ext_csd.erase_group_def = 1;
967 /*
968 * enable ERASE_GRP_DEF successfully.
969 * This will affect the erase size, so
970 * here need to reset erase size
971 */
972 mmc_set_erase_size(card);
973 }
974 }
975
976 /*
Philip Rakity41e2a482011-03-19 14:10:33 -0400977 * Ensure eMMC user default partition is enabled
978 */
Andrei Warkentin371a6892011-04-11 18:10:25 -0500979 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
980 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
981 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
982 card->ext_csd.part_config,
983 card->ext_csd.part_time);
984 if (err && err != -EBADMSG)
985 goto free_card;
Philip Rakity41e2a482011-03-19 14:10:33 -0400986 }
987
988 /*
Girish K Sbec87262011-10-13 12:04:16 +0530989 * If the host supports the power_off_notify capability then
990 * set the notification byte in the ext_csd register of device
991 */
992 if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) &&
Girish K Sa80f1622011-11-15 11:55:46 +0530993 (card->ext_csd.rev >= 6)) {
Girish K Sbec87262011-10-13 12:04:16 +0530994 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
995 EXT_CSD_POWER_OFF_NOTIFICATION,
996 EXT_CSD_POWER_ON,
997 card->ext_csd.generic_cmd6_time);
998 if (err && err != -EBADMSG)
999 goto free_card;
Girish K Sbec87262011-10-13 12:04:16 +05301000
Girish K S96a85d52011-11-04 16:22:47 +05301001 /*
1002 * The err can be -EBADMSG or 0,
1003 * so check for success and update the flag
1004 */
1005 if (!err)
1006 card->poweroff_notify_state = MMC_POWERED_ON;
1007 }
Girish K Sbec87262011-10-13 12:04:16 +05301008
1009 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001010 * Activate high speed (if supported)
1011 */
Girish K Sa4924c72012-01-11 14:04:52 -05001012 if (card->ext_csd.hs_max_dtr != 0) {
1013 err = 0;
1014 if (card->ext_csd.hs_max_dtr > 52000000 &&
1015 host->caps2 & MMC_CAP2_HS200)
1016 err = mmc_select_hs200(card);
1017 else if (host->caps & MMC_CAP_MMC_HIGHSPEED)
1018 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Girish K S7488e922012-01-19 08:56:19 +05301019 EXT_CSD_HS_TIMING, 1,
1020 card->ext_csd.generic_cmd6_time);
Girish K Sa4924c72012-01-11 14:04:52 -05001021
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001022 if (err && err != -EBADMSG)
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001023 goto free_card;
1024
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001025 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301026 pr_warning("%s: switch to highspeed failed\n",
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001027 mmc_hostname(card->host));
1028 err = 0;
1029 } else {
Girish K Sa4924c72012-01-11 14:04:52 -05001030 if (card->ext_csd.hs_max_dtr > 52000000 &&
1031 host->caps2 & MMC_CAP2_HS200) {
1032 mmc_card_set_hs200(card);
1033 mmc_set_timing(card->host,
1034 MMC_TIMING_MMC_HS200);
1035 } else {
1036 mmc_card_set_highspeed(card);
1037 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1038 }
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001039 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001040 }
1041
1042 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001043 * Compute bus speed.
1044 */
1045 max_dtr = (unsigned int)-1;
1046
Girish K Sa4924c72012-01-11 14:04:52 -05001047 if (mmc_card_highspeed(card) || mmc_card_hs200(card)) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001048 if (max_dtr > card->ext_csd.hs_max_dtr)
1049 max_dtr = card->ext_csd.hs_max_dtr;
1050 } else if (max_dtr > card->csd.max_dtr) {
1051 max_dtr = card->csd.max_dtr;
1052 }
1053
1054 mmc_set_clock(host, max_dtr);
1055
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001056 /*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001057 * Indicate DDR mode (if supported).
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001058 */
1059 if (mmc_card_highspeed(card)) {
1060 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
Philip Rakity4c4cb172011-05-13 11:17:18 +05301061 && ((host->caps & (MMC_CAP_1_8V_DDR |
1062 MMC_CAP_UHS_DDR50))
1063 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
Adrian Hunter49e3b5a2010-10-11 12:43:50 +03001064 ddr = MMC_1_8V_DDR_MODE;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001065 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
Philip Rakity4c4cb172011-05-13 11:17:18 +05301066 && ((host->caps & (MMC_CAP_1_2V_DDR |
1067 MMC_CAP_UHS_DDR50))
1068 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
Adrian Hunter49e3b5a2010-10-11 12:43:50 +03001069 ddr = MMC_1_2V_DDR_MODE;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001070 }
1071
1072 /*
Girish K Sa4924c72012-01-11 14:04:52 -05001073 * Indicate HS200 SDR mode (if supported).
1074 */
1075 if (mmc_card_hs200(card)) {
1076 u32 ext_csd_bits;
1077 u32 bus_width = card->host->ios.bus_width;
1078
1079 /*
1080 * For devices supporting HS200 mode, the bus width has
1081 * to be set before executing the tuning function. If
1082 * set before tuning, then device will respond with CRC
1083 * errors for responses on CMD line. So for HS200 the
1084 * sequence will be
1085 * 1. set bus width 4bit / 8 bit (1 bit not supported)
1086 * 2. switch to HS200 mode
1087 * 3. set the clock to > 52Mhz <=200MHz and
1088 * 4. execute tuning for HS200
1089 */
1090 if ((host->caps2 & MMC_CAP2_HS200) &&
Subhash Jadavani52d09742012-03-06 17:59:12 +05301091 card->host->ops->execute_tuning) {
1092 mmc_host_clk_hold(card->host);
Girish K Sa4924c72012-01-11 14:04:52 -05001093 err = card->host->ops->execute_tuning(card->host,
1094 MMC_SEND_TUNING_BLOCK_HS200);
Subhash Jadavani52d09742012-03-06 17:59:12 +05301095 mmc_host_clk_release(card->host);
1096 }
Girish K Sa4924c72012-01-11 14:04:52 -05001097 if (err) {
1098 pr_warning("%s: tuning execution failed\n",
1099 mmc_hostname(card->host));
1100 goto err;
1101 }
1102
1103 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1104 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
1105 err = mmc_select_powerclass(card, ext_csd_bits, ext_csd);
Subhash Jadavani93fc5a42012-04-03 12:25:58 +05301106 if (err)
Subhash Jadavani3d935762012-04-12 19:36:07 +05301107 goto err;
Girish K Sa4924c72012-01-11 14:04:52 -05001108 }
1109
1110 /*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001111 * Activate wide bus and DDR (if supported).
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001112 */
Girish K Sa4924c72012-01-11 14:04:52 -05001113 if (!mmc_card_hs200(card) &&
Girish K S7488e922012-01-19 08:56:19 +05301114 (card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001115 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
Aries Lee22113ef2010-12-15 08:14:24 +01001116 static unsigned ext_csd_bits[][2] = {
1117 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
1118 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
1119 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
1120 };
1121 static unsigned bus_widths[] = {
1122 MMC_BUS_WIDTH_8,
1123 MMC_BUS_WIDTH_4,
1124 MMC_BUS_WIDTH_1
1125 };
1126 unsigned idx, bus_width = 0;
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001127
Aries Lee22113ef2010-12-15 08:14:24 +01001128 if (host->caps & MMC_CAP_8_BIT_DATA)
1129 idx = 0;
1130 else
1131 idx = 1;
1132 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1133 bus_width = bus_widths[idx];
1134 if (bus_width == MMC_BUS_WIDTH_1)
1135 ddr = 0; /* no DDR for 1-bit width */
Girish K Sb87d8db2011-09-23 20:41:47 +05301136 err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
1137 ext_csd);
1138 if (err)
Subhash Jadavani3d935762012-04-12 19:36:07 +05301139 goto err;
Girish K Sb87d8db2011-09-23 20:41:47 +05301140
Aries Lee22113ef2010-12-15 08:14:24 +01001141 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1142 EXT_CSD_BUS_WIDTH,
Andrei Warkentind3a8d952011-04-11 16:13:43 -05001143 ext_csd_bits[idx][0],
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001144 card->ext_csd.generic_cmd6_time);
Aries Lee22113ef2010-12-15 08:14:24 +01001145 if (!err) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301146 mmc_set_bus_width(card->host, bus_width);
Philip Rakity08ee80c2011-05-24 18:14:58 -07001147
Aries Lee22113ef2010-12-15 08:14:24 +01001148 /*
1149 * If controller can't handle bus width test,
Philip Rakity08ee80c2011-05-24 18:14:58 -07001150 * compare ext_csd previously read in 1 bit mode
1151 * against ext_csd at new bus width
Aries Lee22113ef2010-12-15 08:14:24 +01001152 */
1153 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
Philip Rakity08ee80c2011-05-24 18:14:58 -07001154 err = mmc_compare_ext_csds(card,
Philip Rakity08ee80c2011-05-24 18:14:58 -07001155 bus_width);
1156 else
1157 err = mmc_bus_test(card, bus_width);
Aries Lee22113ef2010-12-15 08:14:24 +01001158 if (!err)
1159 break;
1160 }
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001161 }
1162
Aries Lee22113ef2010-12-15 08:14:24 +01001163 if (!err && ddr) {
Girish K Sb87d8db2011-09-23 20:41:47 +05301164 err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
1165 ext_csd);
1166 if (err)
Subhash Jadavani3d935762012-04-12 19:36:07 +05301167 goto err;
Girish K Sb87d8db2011-09-23 20:41:47 +05301168
Aries Lee22113ef2010-12-15 08:14:24 +01001169 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Andrei Warkentind3a8d952011-04-11 16:13:43 -05001170 EXT_CSD_BUS_WIDTH,
1171 ext_csd_bits[idx][1],
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001172 card->ext_csd.generic_cmd6_time);
Aries Lee22113ef2010-12-15 08:14:24 +01001173 }
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001174 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301175 pr_warning("%s: switch to bus width %d ddr %d "
Aries Lee22113ef2010-12-15 08:14:24 +01001176 "failed\n", mmc_hostname(card->host),
1177 1 << bus_width, ddr);
1178 goto free_card;
1179 } else if (ddr) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301180 /*
1181 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1182 * signaling.
1183 *
1184 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1185 *
1186 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1187 * in the JEDEC spec for DDR.
1188 *
1189 * Do not force change in vccq since we are obviously
1190 * working and no change to vccq is needed.
1191 *
1192 * WARNING: eMMC rules are NOT the same as SD DDR
1193 */
Girish K S913047e2011-12-15 17:27:42 +05301194 if (ddr == MMC_1_2V_DDR_MODE) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301195 err = mmc_set_signal_voltage(host,
1196 MMC_SIGNAL_VOLTAGE_120, 0);
1197 if (err)
1198 goto err;
1199 }
Aries Lee22113ef2010-12-15 08:14:24 +01001200 mmc_card_set_ddr_mode(card);
Philip Rakity4c4cb172011-05-13 11:17:18 +05301201 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1202 mmc_set_bus_width(card->host, bus_width);
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001203 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001204 }
1205
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001206 /*
Subhash Jadavani52d09742012-03-06 17:59:12 +05301207 * Enable HPI feature (if supported)
1208 */
1209 if (card->ext_csd.hpi) {
1210 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1211 EXT_CSD_HPI_MGMT, 1,
1212 card->ext_csd.generic_cmd6_time);
1213 if (err && err != -EBADMSG)
1214 goto free_card;
1215 if (err) {
1216 pr_warning("%s: Enabling HPI failed\n",
1217 mmc_hostname(card->host));
1218 err = 0;
1219 } else
1220 card->ext_csd.hpi_en = 1;
1221 }
1222
1223 /*
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001224 * If cache size is higher than 0, this indicates
1225 * the existence of cache and it can be turned on.
1226 */
1227 if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1228 card->ext_csd.cache_size > 0) {
1229 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001230 EXT_CSD_CACHE_CTRL, 1,
1231 card->ext_csd.generic_cmd6_time);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001232 if (err && err != -EBADMSG)
1233 goto free_card;
1234
1235 /*
1236 * Only if no error, cache is turned on successfully.
1237 */
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001238 if (err) {
1239 pr_warning("%s: Cache is supported, "
1240 "but failed to turn on (%d)\n",
1241 mmc_hostname(card->host), err);
1242 card->ext_csd.cache_ctrl = 0;
1243 err = 0;
1244 } else {
1245 card->ext_csd.cache_ctrl = 1;
1246 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001247 }
1248
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001249 if (!oldcard)
1250 host->card = card;
1251
Philip Rakity08ee80c2011-05-24 18:14:58 -07001252 mmc_free_ext_csd(ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001253 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001254
1255free_card:
1256 if (!oldcard)
1257 mmc_remove_card(card);
1258err:
Philip Rakity08ee80c2011-05-24 18:14:58 -07001259 mmc_free_ext_csd(ext_csd);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001260
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001261 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001262}
1263
1264/*
1265 * Host is being removed. Free up the current card.
1266 */
1267static void mmc_remove(struct mmc_host *host)
1268{
1269 BUG_ON(!host);
1270 BUG_ON(!host->card);
1271
1272 mmc_remove_card(host->card);
1273 host->card = NULL;
1274}
1275
1276/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001277 * Card detection - card is alive.
1278 */
1279static int mmc_alive(struct mmc_host *host)
1280{
1281 return mmc_send_status(host->card, NULL);
1282}
1283
1284/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001285 * Card detection callback from host.
1286 */
1287static void mmc_detect(struct mmc_host *host)
1288{
1289 int err;
1290
1291 BUG_ON(!host);
1292 BUG_ON(!host->card);
1293
1294 mmc_claim_host(host);
1295
1296 /*
1297 * Just check if our card has been removed.
1298 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001299 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001300
1301 mmc_release_host(host);
1302
Pierre Ossman17b04292007-07-22 22:18:46 +02001303 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001304 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001305
1306 mmc_claim_host(host);
1307 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001308 mmc_power_off(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001309 mmc_release_host(host);
1310 }
1311}
1312
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001313/*
1314 * Suspend callback from host.
1315 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001316static int mmc_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001317{
Balaji T Kc3805462011-09-08 22:08:39 +05301318 int err = 0;
1319
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001320 BUG_ON(!host);
1321 BUG_ON(!host->card);
1322
1323 mmc_claim_host(host);
Girish K S3e73c362012-01-31 15:44:03 +05301324 if (mmc_card_can_sleep(host)) {
Balaji T Kc3805462011-09-08 22:08:39 +05301325 err = mmc_card_sleep(host);
Girish K S3e73c362012-01-31 15:44:03 +05301326 if (!err)
1327 mmc_card_set_sleep(host->card);
1328 } else if (!mmc_host_is_spi(host))
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001329 err = mmc_deselect_cards(host);
Girish K S3e73c362012-01-31 15:44:03 +05301330 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001331 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001332
Balaji T Kc3805462011-09-08 22:08:39 +05301333 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001334}
1335
1336/*
1337 * Resume callback from host.
1338 *
1339 * This function tries to determine if the same card is still present
1340 * and, if so, restore all state to it.
1341 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001342static int mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001343{
1344 int err;
1345
1346 BUG_ON(!host);
1347 BUG_ON(!host->card);
1348
1349 mmc_claim_host(host);
Girish K S3e73c362012-01-31 15:44:03 +05301350 if (mmc_card_is_sleep(host->card)) {
1351 err = mmc_card_awake(host);
1352 mmc_card_clr_sleep(host->card);
1353 } else
1354 err = mmc_init_card(host, host->ocr, host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001355 mmc_release_host(host);
1356
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001357 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001358}
1359
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001360static int mmc_power_restore(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001361{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001362 int ret;
1363
Girish K S3e73c362012-01-31 15:44:03 +05301364 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
1365 mmc_card_clr_sleep(host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001366 mmc_claim_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001367 ret = mmc_init_card(host, host->ocr, host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001368 mmc_release_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001369
1370 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001371}
1372
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001373static int mmc_sleep(struct mmc_host *host)
1374{
1375 struct mmc_card *card = host->card;
1376 int err = -ENOSYS;
1377
1378 if (card && card->ext_csd.rev >= 3) {
1379 err = mmc_card_sleepawake(host, 1);
1380 if (err < 0)
1381 pr_debug("%s: Error %d while putting card into sleep",
1382 mmc_hostname(host), err);
1383 }
1384
1385 return err;
1386}
1387
1388static int mmc_awake(struct mmc_host *host)
1389{
1390 struct mmc_card *card = host->card;
1391 int err = -ENOSYS;
1392
1393 if (card && card->ext_csd.rev >= 3) {
1394 err = mmc_card_sleepawake(host, 0);
1395 if (err < 0)
1396 pr_debug("%s: Error %d while awaking sleeping card",
1397 mmc_hostname(host), err);
1398 }
1399
1400 return err;
1401}
1402
Adrian Hunter9feae242009-09-22 16:44:32 -07001403static const struct mmc_bus_ops mmc_ops = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001404 .awake = mmc_awake,
1405 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -07001406 .remove = mmc_remove,
1407 .detect = mmc_detect,
1408 .suspend = NULL,
1409 .resume = NULL,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001410 .power_restore = mmc_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001411 .alive = mmc_alive,
Adrian Hunter9feae242009-09-22 16:44:32 -07001412};
1413
1414static const struct mmc_bus_ops mmc_ops_unsafe = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001415 .awake = mmc_awake,
1416 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -07001417 .remove = mmc_remove,
1418 .detect = mmc_detect,
1419 .suspend = mmc_suspend,
1420 .resume = mmc_resume,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001421 .power_restore = mmc_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001422 .alive = mmc_alive,
Adrian Hunter9feae242009-09-22 16:44:32 -07001423};
1424
1425static void mmc_attach_bus_ops(struct mmc_host *host)
1426{
1427 const struct mmc_bus_ops *bus_ops;
1428
Matt Fleming71d7d3d2010-09-27 09:42:19 +01001429 if (!mmc_card_is_removable(host))
Adrian Hunter9feae242009-09-22 16:44:32 -07001430 bus_ops = &mmc_ops_unsafe;
1431 else
1432 bus_ops = &mmc_ops;
1433 mmc_attach_bus(host, bus_ops);
1434}
1435
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001436/*
1437 * Starting point for MMC card init.
1438 */
Andy Ross807e8e42011-01-03 10:36:56 -08001439int mmc_attach_mmc(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001440{
1441 int err;
Andy Ross807e8e42011-01-03 10:36:56 -08001442 u32 ocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001443
1444 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001445 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001446
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001447 /* Set correct bus mode for MMC before attempting attach */
1448 if (!mmc_host_is_spi(host))
1449 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1450
Andy Ross807e8e42011-01-03 10:36:56 -08001451 err = mmc_send_op_cond(host, 0, &ocr);
1452 if (err)
1453 return err;
1454
Adrian Hunter9feae242009-09-22 16:44:32 -07001455 mmc_attach_bus_ops(host);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001456 if (host->ocr_avail_mmc)
1457 host->ocr_avail = host->ocr_avail_mmc;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001458
1459 /*
David Brownellaf517152007-08-08 09:11:32 -07001460 * We need to get OCR a different way for SPI.
1461 */
1462 if (mmc_host_is_spi(host)) {
1463 err = mmc_spi_read_ocr(host, 1, &ocr);
1464 if (err)
1465 goto err;
1466 }
1467
1468 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001469 * Sanity check the voltages that the card claims to
1470 * support.
1471 */
1472 if (ocr & 0x7F) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301473 pr_warning("%s: card claims to support voltages "
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001474 "below the defined range. These will be ignored.\n",
1475 mmc_hostname(host));
1476 ocr &= ~0x7F;
1477 }
1478
1479 host->ocr = mmc_select_voltage(host, ocr);
1480
1481 /*
1482 * Can we support the voltage of the card?
1483 */
Pierre Ossman109b5be2007-07-23 00:12:10 +02001484 if (!host->ocr) {
1485 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001486 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001487 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001488
1489 /*
1490 * Detect and init the card.
1491 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001492 err = mmc_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001493 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001494 goto err;
1495
1496 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001497 err = mmc_add_card(host->card);
Andy Ross807e8e42011-01-03 10:36:56 -08001498 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001499 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001500 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001501
1502 return 0;
1503
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001504remove_card:
Andy Ross807e8e42011-01-03 10:36:56 -08001505 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001506 mmc_remove_card(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001507 mmc_claim_host(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001508 host->card = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001509err:
1510 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001511
Girish K Sa3c76eb2011-10-11 11:44:09 +05301512 pr_err("%s: error %d whilst initialising MMC card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02001513 mmc_hostname(host), err);
1514
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001515 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001516}