blob: 6defddd795f673d8957fae8129768de279276fa1 [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
238/*
239 * Decode extended CSD.
240 */
241static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
242{
Namjae Jeone0c368d2011-10-06 23:41:38 +0900243 int err = 0, idx;
244 unsigned int part_size;
245 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700246
247 BUG_ON(!card);
248
249 if (!ext_csd)
250 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100251
Kyungmin Park6da24b72010-08-10 18:01:36 -0700252 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700253 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
Kyungmin Park6da24b72010-08-10 18:01:36 -0700254 if (card->csd.structure == 3) {
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700255 if (card->ext_csd.raw_ext_csd_structure > 2) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530256 pr_err("%s: unrecognised EXT_CSD structure "
Kyungmin Park6da24b72010-08-10 18:01:36 -0700257 "version %d\n", mmc_hostname(card->host),
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700258 card->ext_csd.raw_ext_csd_structure);
Kyungmin Park6da24b72010-08-10 18:01:36 -0700259 err = -EINVAL;
260 goto out;
261 }
262 }
263
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700264 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Kyungmin Park38ca2852011-07-26 17:12:37 +0900265 if (card->ext_csd.rev > 6) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530266 pr_err("%s: unrecognised EXT_CSD revision %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700267 mmc_hostname(card->host), card->ext_csd.rev);
Florin Malita00cedfa2007-10-13 12:27:20 -0400268 err = -EINVAL;
269 goto out;
Pierre Ossmand7604d72007-07-23 00:34:07 +0200270 }
271
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700272 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
273 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
274 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
275 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700276 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200277 card->ext_csd.sectors =
278 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
279 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
280 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
281 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700282
283 /* Cards with density > 2GiB are sector addressed */
284 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200285 mmc_card_set_blockaddr(card);
286 }
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700287 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
Adrian Hunter7198f3c2010-03-23 13:35:40 -0700288 switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
Girish K Sa4924c72012-01-11 14:04:52 -0500289 case EXT_CSD_CARD_TYPE_SDR_ALL:
290 case EXT_CSD_CARD_TYPE_SDR_ALL_DDR_1_8V:
291 case EXT_CSD_CARD_TYPE_SDR_ALL_DDR_1_2V:
292 case EXT_CSD_CARD_TYPE_SDR_ALL_DDR_52:
293 card->ext_csd.hs_max_dtr = 200000000;
294 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_SDR_200;
295 break;
296 case EXT_CSD_CARD_TYPE_SDR_1_2V_ALL:
297 case EXT_CSD_CARD_TYPE_SDR_1_2V_DDR_1_8V:
298 case EXT_CSD_CARD_TYPE_SDR_1_2V_DDR_1_2V:
299 case EXT_CSD_CARD_TYPE_SDR_1_2V_DDR_52:
300 card->ext_csd.hs_max_dtr = 200000000;
301 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_SDR_1_2V;
302 break;
303 case EXT_CSD_CARD_TYPE_SDR_1_8V_ALL:
304 case EXT_CSD_CARD_TYPE_SDR_1_8V_DDR_1_8V:
305 case EXT_CSD_CARD_TYPE_SDR_1_8V_DDR_1_2V:
306 case EXT_CSD_CARD_TYPE_SDR_1_8V_DDR_52:
307 card->ext_csd.hs_max_dtr = 200000000;
308 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_SDR_1_8V;
309 break;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -0400310 case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 |
311 EXT_CSD_CARD_TYPE_26:
312 card->ext_csd.hs_max_dtr = 52000000;
313 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52;
314 break;
315 case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 |
316 EXT_CSD_CARD_TYPE_26:
317 card->ext_csd.hs_max_dtr = 52000000;
318 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V;
319 break;
320 case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 |
321 EXT_CSD_CARD_TYPE_26:
322 card->ext_csd.hs_max_dtr = 52000000;
323 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V;
324 break;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100325 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
326 card->ext_csd.hs_max_dtr = 52000000;
327 break;
328 case EXT_CSD_CARD_TYPE_26:
329 card->ext_csd.hs_max_dtr = 26000000;
330 break;
331 default:
332 /* MMC v4 spec says this cannot happen */
Girish K Sa3c76eb2011-10-11 11:44:09 +0530333 pr_warning("%s: card is mmc v4 but doesn't "
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100334 "support any high-speed modes.\n",
335 mmc_hostname(card->host));
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100336 }
337
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700338 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
339 card->ext_csd.raw_erase_timeout_mult =
340 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
341 card->ext_csd.raw_hc_erase_grp_size =
342 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700343 if (card->ext_csd.rev >= 3) {
344 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500345 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
346
347 /* EXT_CSD value is in units of 10ms, but we store in ms */
348 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700349
350 /* Sleep / awake timeout in 100ns units */
351 if (sa_shift > 0 && sa_shift <= 0x17)
352 card->ext_csd.sa_timeout =
353 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700354 card->ext_csd.erase_group_def =
355 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
356 card->ext_csd.hc_erase_timeout = 300 *
357 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
358 card->ext_csd.hc_erase_size =
359 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500360
361 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500362
363 /*
364 * There are two boot regions of equal size, defined in
365 * multiples of 128K.
366 */
Namjae Jeone0c368d2011-10-06 23:41:38 +0900367 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
368 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
369 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
370 mmc_part_add(card, part_size,
371 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100372 "boot%d", idx, true,
373 MMC_BLK_DATA_AREA_BOOT);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900374 }
375 }
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700376 }
377
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700378 card->ext_csd.raw_hc_erase_gap_size =
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100379 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700380 card->ext_csd.raw_sec_trim_mult =
381 ext_csd[EXT_CSD_SEC_TRIM_MULT];
382 card->ext_csd.raw_sec_erase_mult =
383 ext_csd[EXT_CSD_SEC_ERASE_MULT];
384 card->ext_csd.raw_sec_feature_support =
385 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
386 card->ext_csd.raw_trim_mult =
387 ext_csd[EXT_CSD_TRIM_MULT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700388 if (card->ext_csd.rev >= 4) {
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800389 /*
390 * Enhanced area feature support -- check whether the eMMC
391 * card has the Enhanced area enabled. If so, export enhanced
392 * area offset and size to user by adding sysfs interface.
393 */
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400394 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800395 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700396 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
Namjae Jeone0c368d2011-10-06 23:41:38 +0900397 hc_erase_grp_sz =
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800398 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Namjae Jeone0c368d2011-10-06 23:41:38 +0900399 hc_wp_grp_sz =
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800400 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
401
402 card->ext_csd.enhanced_area_en = 1;
403 /*
404 * calculate the enhanced data area offset, in bytes
405 */
406 card->ext_csd.enhanced_area_offset =
407 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
408 (ext_csd[137] << 8) + ext_csd[136];
409 if (mmc_card_blockaddr(card))
410 card->ext_csd.enhanced_area_offset <<= 9;
411 /*
412 * calculate the enhanced data area size, in kilobytes
413 */
414 card->ext_csd.enhanced_area_size =
415 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
416 ext_csd[140];
417 card->ext_csd.enhanced_area_size *=
418 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
419 card->ext_csd.enhanced_area_size <<= 9;
420 } else {
421 /*
422 * If the enhanced area is not enabled, disable these
423 * device attributes.
424 */
425 card->ext_csd.enhanced_area_offset = -EINVAL;
426 card->ext_csd.enhanced_area_size = -EINVAL;
427 }
Namjae Jeone0c368d2011-10-06 23:41:38 +0900428
429 /*
430 * General purpose partition feature support --
431 * If ext_csd has the size of general purpose partitions,
432 * set size, part_cfg, partition name in mmc_part.
433 */
434 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
435 EXT_CSD_PART_SUPPORT_PART_EN) {
436 if (card->ext_csd.enhanced_area_en != 1) {
437 hc_erase_grp_sz =
438 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
439 hc_wp_grp_sz =
440 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
441
442 card->ext_csd.enhanced_area_en = 1;
443 }
444
445 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
446 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
447 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
448 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
449 continue;
450 part_size =
451 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
452 << 16) +
453 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
454 << 8) +
455 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
456 part_size *= (size_t)(hc_erase_grp_sz *
457 hc_wp_grp_sz);
458 mmc_part_add(card, part_size << 19,
459 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100460 "gp%d", idx, false,
461 MMC_BLK_DATA_AREA_GP);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900462 }
463 }
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700464 card->ext_csd.sec_trim_mult =
465 ext_csd[EXT_CSD_SEC_TRIM_MULT];
466 card->ext_csd.sec_erase_mult =
467 ext_csd[EXT_CSD_SEC_ERASE_MULT];
468 card->ext_csd.sec_feature_support =
469 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
470 card->ext_csd.trim_timeout = 300 *
471 ext_csd[EXT_CSD_TRIM_MULT];
Johan Rudholmadd710e2011-12-02 08:51:06 +0100472
473 /*
474 * Note that the call to mmc_part_add above defaults to read
475 * only. If this default assumption is changed, the call must
476 * take into account the value of boot_locked below.
477 */
478 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
479 card->ext_csd.boot_ro_lockable = true;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700480 }
481
Adrian Hunterb2499512011-08-29 16:42:11 +0300482 if (card->ext_csd.rev >= 5) {
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400483 /* check whether the eMMC card supports HPI */
484 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
485 card->ext_csd.hpi = 1;
486 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
487 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
488 else
489 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
490 /*
491 * Indicate the maximum timeout to close
492 * a command interrupted by HPI
493 */
494 card->ext_csd.out_of_int_time =
495 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
496 }
497
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500498 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
Adrian Hunterb2499512011-08-29 16:42:11 +0300499 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
500 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500501
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400502 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700503 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
504 card->erased_byte = 0xFF;
505 else
506 card->erased_byte = 0x0;
507
Seungwon Jeon336c7162011-10-25 09:43:12 +0900508 /* eMMC v4.5 or later */
Girish K Sbec87262011-10-13 12:04:16 +0530509 if (card->ext_csd.rev >= 6) {
Seungwon Jeon336c7162011-10-25 09:43:12 +0900510 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
511
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900512 card->ext_csd.generic_cmd6_time = 10 *
513 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
Girish K Sbec87262011-10-13 12:04:16 +0530514 card->ext_csd.power_off_longtime = 10 *
515 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900516
Seungwon Jeon336c7162011-10-25 09:43:12 +0900517 card->ext_csd.cache_size =
518 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
519 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
520 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
521 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
Saugata Das42659002011-12-21 13:09:17 +0530522
523 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
524 card->ext_csd.data_sector_size = 4096;
525 else
526 card->ext_csd.data_sector_size = 512;
527
528 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
529 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
530 card->ext_csd.data_tag_unit_size =
531 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
532 (card->ext_csd.data_sector_size);
533 } else {
534 card->ext_csd.data_tag_unit_size = 0;
535 }
Seungwon Jeon336c7162011-10-25 09:43:12 +0900536 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900537
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100538out:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700539 return err;
540}
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100541
Philip Rakity08ee80c2011-05-24 18:14:58 -0700542static inline void mmc_free_ext_csd(u8 *ext_csd)
543{
544 kfree(ext_csd);
545}
546
547
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700548static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700549{
550 u8 *bw_ext_csd;
551 int err;
552
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700553 if (bus_width == MMC_BUS_WIDTH_1)
554 return 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700555
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700556 err = mmc_get_ext_csd(card, &bw_ext_csd);
557
558 if (err || bw_ext_csd == NULL) {
Philip Rakity08ee80c2011-05-24 18:14:58 -0700559 if (bus_width != MMC_BUS_WIDTH_1)
560 err = -EINVAL;
561 goto out;
562 }
563
564 if (bus_width == MMC_BUS_WIDTH_1)
565 goto out;
566
567 /* only compare read only fields */
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100568 err = !((card->ext_csd.raw_partition_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700569 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700570 (card->ext_csd.raw_erased_mem_count ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700571 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700572 (card->ext_csd.rev ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700573 bw_ext_csd[EXT_CSD_REV]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700574 (card->ext_csd.raw_ext_csd_structure ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700575 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700576 (card->ext_csd.raw_card_type ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700577 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700578 (card->ext_csd.raw_s_a_timeout ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700579 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700580 (card->ext_csd.raw_hc_erase_gap_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700581 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700582 (card->ext_csd.raw_erase_timeout_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700583 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700584 (card->ext_csd.raw_hc_erase_grp_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700585 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700586 (card->ext_csd.raw_sec_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700587 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700588 (card->ext_csd.raw_sec_erase_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700589 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700590 (card->ext_csd.raw_sec_feature_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700591 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700592 (card->ext_csd.raw_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700593 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700594 (card->ext_csd.raw_sectors[0] ==
595 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
596 (card->ext_csd.raw_sectors[1] ==
597 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
598 (card->ext_csd.raw_sectors[2] ==
599 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
600 (card->ext_csd.raw_sectors[3] ==
601 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
Philip Rakity08ee80c2011-05-24 18:14:58 -0700602 if (err)
603 err = -EINVAL;
604
605out:
606 mmc_free_ext_csd(bw_ext_csd);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100607 return err;
608}
609
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100610MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
611 card->raw_cid[2], card->raw_cid[3]);
612MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
613 card->raw_csd[2], card->raw_csd[3]);
614MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700615MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
616MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100617MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
618MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
619MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
620MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
621MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
622MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800623MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
624 card->ext_csd.enhanced_area_offset);
625MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100626
627static struct attribute *mmc_std_attrs[] = {
628 &dev_attr_cid.attr,
629 &dev_attr_csd.attr,
630 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700631 &dev_attr_erase_size.attr,
632 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100633 &dev_attr_fwrev.attr,
634 &dev_attr_hwrev.attr,
635 &dev_attr_manfid.attr,
636 &dev_attr_name.attr,
637 &dev_attr_oemid.attr,
638 &dev_attr_serial.attr,
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800639 &dev_attr_enhanced_area_offset.attr,
640 &dev_attr_enhanced_area_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100641 NULL,
642};
643
644static struct attribute_group mmc_std_attr_group = {
645 .attrs = mmc_std_attrs,
646};
647
David Brownella4dbd672009-06-24 10:06:31 -0700648static const struct attribute_group *mmc_attr_groups[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100649 &mmc_std_attr_group,
650 NULL,
651};
652
653static struct device_type mmc_type = {
654 .groups = mmc_attr_groups,
655};
656
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100657/*
Girish K Sb87d8db2011-09-23 20:41:47 +0530658 * Select the PowerClass for the current bus width
659 * If power class is defined for 4/8 bit bus in the
660 * extended CSD register, select it by executing the
661 * mmc_switch command.
662 */
663static int mmc_select_powerclass(struct mmc_card *card,
664 unsigned int bus_width, u8 *ext_csd)
665{
666 int err = 0;
667 unsigned int pwrclass_val;
668 unsigned int index = 0;
669 struct mmc_host *host;
670
671 BUG_ON(!card);
672
673 host = card->host;
674 BUG_ON(!host);
675
676 if (ext_csd == NULL)
677 return 0;
678
679 /* Power class selection is supported for versions >= 4.0 */
680 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
681 return 0;
682
683 /* Power class values are defined only for 4/8 bit bus */
684 if (bus_width == EXT_CSD_BUS_WIDTH_1)
685 return 0;
686
687 switch (1 << host->ios.vdd) {
688 case MMC_VDD_165_195:
689 if (host->ios.clock <= 26000000)
690 index = EXT_CSD_PWR_CL_26_195;
691 else if (host->ios.clock <= 52000000)
692 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
693 EXT_CSD_PWR_CL_52_195 :
694 EXT_CSD_PWR_CL_DDR_52_195;
695 else if (host->ios.clock <= 200000000)
696 index = EXT_CSD_PWR_CL_200_195;
697 break;
698 case MMC_VDD_32_33:
699 case MMC_VDD_33_34:
700 case MMC_VDD_34_35:
701 case MMC_VDD_35_36:
702 if (host->ios.clock <= 26000000)
703 index = EXT_CSD_PWR_CL_26_360;
704 else if (host->ios.clock <= 52000000)
705 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
706 EXT_CSD_PWR_CL_52_360 :
707 EXT_CSD_PWR_CL_DDR_52_360;
708 else if (host->ios.clock <= 200000000)
709 index = EXT_CSD_PWR_CL_200_360;
710 break;
711 default:
712 pr_warning("%s: Voltage range not supported "
713 "for power class.\n", mmc_hostname(host));
714 return -EINVAL;
715 }
716
717 pwrclass_val = ext_csd[index];
718
719 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
720 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
721 EXT_CSD_PWR_CL_8BIT_SHIFT;
722 else
723 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
724 EXT_CSD_PWR_CL_4BIT_SHIFT;
725
726 /* If the power class is different from the default value */
727 if (pwrclass_val > 0) {
728 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
729 EXT_CSD_POWER_CLASS,
730 pwrclass_val,
Seungwon Jeon71fe3eb2011-10-18 13:20:57 +0900731 card->ext_csd.generic_cmd6_time);
Girish K Sb87d8db2011-09-23 20:41:47 +0530732 }
733
734 return err;
735}
736
737/*
Girish K Sa4924c72012-01-11 14:04:52 -0500738 * Selects the desired buswidth and switch to the HS200 mode
739 * if bus width set without error
740 */
741static int mmc_select_hs200(struct mmc_card *card)
742{
743 int idx, err = 0;
744 struct mmc_host *host;
745 static unsigned ext_csd_bits[] = {
746 EXT_CSD_BUS_WIDTH_4,
747 EXT_CSD_BUS_WIDTH_8,
748 };
749 static unsigned bus_widths[] = {
750 MMC_BUS_WIDTH_4,
751 MMC_BUS_WIDTH_8,
752 };
753
754 BUG_ON(!card);
755
756 host = card->host;
757
758 if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V &&
759 host->caps2 & MMC_CAP2_HS200_1_2V_SDR)
760 if (mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120, 0))
761 err = mmc_set_signal_voltage(host,
762 MMC_SIGNAL_VOLTAGE_180, 0);
763
764 /* If fails try again during next card power cycle */
765 if (err)
766 goto err;
767
768 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0;
769
770 /*
771 * Unlike SD, MMC cards dont have a configuration register to notify
772 * supported bus width. So bus test command should be run to identify
773 * the supported bus width or compare the ext csd values of current
774 * bus width and ext csd values of 1 bit mode read earlier.
775 */
776 for (; idx >= 0; idx--) {
777
778 /*
779 * Host is capable of 8bit transfer, then switch
780 * the device to work in 8bit transfer mode. If the
781 * mmc switch command returns error then switch to
782 * 4bit transfer mode. On success set the corresponding
783 * bus width on the host.
784 */
785 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
786 EXT_CSD_BUS_WIDTH,
787 ext_csd_bits[idx],
788 card->ext_csd.generic_cmd6_time);
789 if (err)
790 continue;
791
792 mmc_set_bus_width(card->host, bus_widths[idx]);
793
794 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
795 err = mmc_compare_ext_csds(card, bus_widths[idx]);
796 else
797 err = mmc_bus_test(card, bus_widths[idx]);
798 if (!err)
799 break;
800 }
801
802 /* switch to HS200 mode if bus width set successfully */
803 if (!err)
804 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
805 EXT_CSD_HS_TIMING, 2, 0);
806err:
807 return err;
808}
809
810/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200811 * Handle the detection and initialisation of a card.
812 *
Deepak Saxena87693922008-06-16 19:20:57 -0700813 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200814 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100815 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +0200816static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200817 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100818{
819 struct mmc_card *card;
Philip Rakity25d5c692010-11-07 16:22:28 -0500820 int err, ddr = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100821 u32 cid[4];
822 unsigned int max_dtr;
Philip Rakityb676f032011-02-13 23:13:09 -0800823 u32 rocr;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700824 u8 *ext_csd = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100825
826 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200827 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100828
Stefan Nilsson XK44669032011-09-15 17:50:38 +0200829 /* Set correct bus mode for MMC before attempting init */
830 if (!mmc_host_is_spi(host))
831 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
832
Ulf Hanssone7747472012-03-01 13:18:05 +0100833 /* Initialization should be done at 3.3 V I/O voltage. */
834 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
835
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100836 /*
837 * Since we're changing the OCR value, we seem to
838 * need to tell some cards to go back to the idle
839 * state. We wait 1ms to give cards time to
840 * respond.
Balaji T Kc3805462011-09-08 22:08:39 +0530841 * mmc_go_idle is needed for eMMC that are asleep
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100842 */
843 mmc_go_idle(host);
844
845 /* The extra bit indicates that we support high capacity */
Philip Rakityb676f032011-02-13 23:13:09 -0800846 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200847 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200848 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100849
850 /*
David Brownellaf517152007-08-08 09:11:32 -0700851 * For SPI, enable CRC as appropriate.
852 */
853 if (mmc_host_is_spi(host)) {
854 err = mmc_spi_set_crc(host, use_spi_crc);
855 if (err)
856 goto err;
857 }
858
859 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100860 * Fetch CID from card.
861 */
David Brownellaf517152007-08-08 09:11:32 -0700862 if (mmc_host_is_spi(host))
863 err = mmc_send_cid(host, cid);
864 else
865 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +0200866 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100867 goto err;
868
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200869 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200870 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
871 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200872 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200873 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100874
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200875 card = oldcard;
876 } else {
877 /*
878 * Allocate card structure.
879 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100880 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200881 if (IS_ERR(card)) {
882 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200883 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200884 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200885
886 card->type = MMC_TYPE_MMC;
887 card->rca = 1;
888 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
889 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100890
891 /*
David Brownellaf517152007-08-08 09:11:32 -0700892 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100893 */
David Brownellaf517152007-08-08 09:11:32 -0700894 if (!mmc_host_is_spi(host)) {
895 err = mmc_set_relative_addr(card);
896 if (err)
897 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100898
David Brownellaf517152007-08-08 09:11:32 -0700899 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
900 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100901
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200902 if (!oldcard) {
903 /*
904 * Fetch CSD from card.
905 */
906 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200907 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200908 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100909
Pierre Ossmanbd766312007-05-01 16:11:57 +0200910 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200911 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +0200912 goto free_card;
913 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200914 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +0200915 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200916 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100917
918 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200919 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100920 */
David Brownellaf517152007-08-08 09:11:32 -0700921 if (!mmc_host_is_spi(host)) {
922 err = mmc_select_card(card);
923 if (err)
924 goto free_card;
925 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100926
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200927 if (!oldcard) {
928 /*
David Brownellaf517152007-08-08 09:11:32 -0700929 * Fetch and process extended CSD.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200930 */
Philip Rakity08ee80c2011-05-24 18:14:58 -0700931
932 err = mmc_get_ext_csd(card, &ext_csd);
933 if (err)
934 goto free_card;
935 err = mmc_read_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200936 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200937 goto free_card;
Philip Rakityb676f032011-02-13 23:13:09 -0800938
939 /* If doing byte addressing, check if required to do sector
940 * addressing. Handle the case of <2GB cards needing sector
941 * addressing. See section 8.1 JEDEC Standard JED84-A441;
942 * ocr register has bit 30 set for sector addressing.
943 */
944 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
945 mmc_card_set_blockaddr(card);
946
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700947 /* Erase size depends on CSD and Extended CSD */
948 mmc_set_erase_size(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200949 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100950
951 /*
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800952 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300953 * bit. This bit will be lost every time after a reset or power off.
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800954 */
955 if (card->ext_csd.enhanced_area_en) {
956 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900957 EXT_CSD_ERASE_GROUP_DEF, 1,
958 card->ext_csd.generic_cmd6_time);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800959
960 if (err && err != -EBADMSG)
961 goto free_card;
962
963 if (err) {
964 err = 0;
965 /*
966 * Just disable enhanced area off & sz
967 * will try to enable ERASE_GROUP_DEF
968 * during next time reinit
969 */
970 card->ext_csd.enhanced_area_offset = -EINVAL;
971 card->ext_csd.enhanced_area_size = -EINVAL;
972 } else {
973 card->ext_csd.erase_group_def = 1;
974 /*
975 * enable ERASE_GRP_DEF successfully.
976 * This will affect the erase size, so
977 * here need to reset erase size
978 */
979 mmc_set_erase_size(card);
980 }
981 }
982
983 /*
Philip Rakity41e2a482011-03-19 14:10:33 -0400984 * Ensure eMMC user default partition is enabled
985 */
Andrei Warkentin371a6892011-04-11 18:10:25 -0500986 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
987 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
988 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
989 card->ext_csd.part_config,
990 card->ext_csd.part_time);
991 if (err && err != -EBADMSG)
992 goto free_card;
Philip Rakity41e2a482011-03-19 14:10:33 -0400993 }
994
995 /*
Girish K Sbec87262011-10-13 12:04:16 +0530996 * If the host supports the power_off_notify capability then
997 * set the notification byte in the ext_csd register of device
998 */
999 if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) &&
Girish K Sa80f1622011-11-15 11:55:46 +05301000 (card->ext_csd.rev >= 6)) {
Girish K Sbec87262011-10-13 12:04:16 +05301001 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1002 EXT_CSD_POWER_OFF_NOTIFICATION,
1003 EXT_CSD_POWER_ON,
1004 card->ext_csd.generic_cmd6_time);
1005 if (err && err != -EBADMSG)
1006 goto free_card;
Girish K Sbec87262011-10-13 12:04:16 +05301007
Girish K S96a85d52011-11-04 16:22:47 +05301008 /*
1009 * The err can be -EBADMSG or 0,
1010 * so check for success and update the flag
1011 */
1012 if (!err)
1013 card->poweroff_notify_state = MMC_POWERED_ON;
1014 }
Girish K Sbec87262011-10-13 12:04:16 +05301015
1016 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001017 * Activate high speed (if supported)
1018 */
Girish K Sa4924c72012-01-11 14:04:52 -05001019 if (card->ext_csd.hs_max_dtr != 0) {
1020 err = 0;
1021 if (card->ext_csd.hs_max_dtr > 52000000 &&
1022 host->caps2 & MMC_CAP2_HS200)
1023 err = mmc_select_hs200(card);
1024 else if (host->caps & MMC_CAP_MMC_HIGHSPEED)
1025 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Girish K S7488e922012-01-19 08:56:19 +05301026 EXT_CSD_HS_TIMING, 1,
1027 card->ext_csd.generic_cmd6_time);
Girish K Sa4924c72012-01-11 14:04:52 -05001028
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001029 if (err && err != -EBADMSG)
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001030 goto free_card;
1031
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001032 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301033 pr_warning("%s: switch to highspeed failed\n",
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001034 mmc_hostname(card->host));
1035 err = 0;
1036 } else {
Girish K Sa4924c72012-01-11 14:04:52 -05001037 if (card->ext_csd.hs_max_dtr > 52000000 &&
1038 host->caps2 & MMC_CAP2_HS200) {
1039 mmc_card_set_hs200(card);
1040 mmc_set_timing(card->host,
1041 MMC_TIMING_MMC_HS200);
1042 } else {
1043 mmc_card_set_highspeed(card);
1044 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1045 }
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001046 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001047 }
1048
1049 /*
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -04001050 * Enable HPI feature (if supported)
1051 */
1052 if (card->ext_csd.hpi) {
1053 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1054 EXT_CSD_HPI_MGMT, 1, 0);
1055 if (err && err != -EBADMSG)
1056 goto free_card;
1057 if (err) {
1058 pr_warning("%s: Enabling HPI failed\n",
1059 mmc_hostname(card->host));
1060 err = 0;
1061 } else
1062 card->ext_csd.hpi_en = 1;
1063 }
1064
1065 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001066 * Compute bus speed.
1067 */
1068 max_dtr = (unsigned int)-1;
1069
Girish K Sa4924c72012-01-11 14:04:52 -05001070 if (mmc_card_highspeed(card) || mmc_card_hs200(card)) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001071 if (max_dtr > card->ext_csd.hs_max_dtr)
1072 max_dtr = card->ext_csd.hs_max_dtr;
1073 } else if (max_dtr > card->csd.max_dtr) {
1074 max_dtr = card->csd.max_dtr;
1075 }
1076
1077 mmc_set_clock(host, max_dtr);
1078
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001079 /*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001080 * Indicate DDR mode (if supported).
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001081 */
1082 if (mmc_card_highspeed(card)) {
1083 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
Philip Rakity4c4cb172011-05-13 11:17:18 +05301084 && ((host->caps & (MMC_CAP_1_8V_DDR |
1085 MMC_CAP_UHS_DDR50))
1086 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
Adrian Hunter49e3b5a2010-10-11 12:43:50 +03001087 ddr = MMC_1_8V_DDR_MODE;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001088 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
Philip Rakity4c4cb172011-05-13 11:17:18 +05301089 && ((host->caps & (MMC_CAP_1_2V_DDR |
1090 MMC_CAP_UHS_DDR50))
1091 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
Adrian Hunter49e3b5a2010-10-11 12:43:50 +03001092 ddr = MMC_1_2V_DDR_MODE;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001093 }
1094
1095 /*
Girish K Sa4924c72012-01-11 14:04:52 -05001096 * Indicate HS200 SDR mode (if supported).
1097 */
1098 if (mmc_card_hs200(card)) {
1099 u32 ext_csd_bits;
1100 u32 bus_width = card->host->ios.bus_width;
1101
1102 /*
1103 * For devices supporting HS200 mode, the bus width has
1104 * to be set before executing the tuning function. If
1105 * set before tuning, then device will respond with CRC
1106 * errors for responses on CMD line. So for HS200 the
1107 * sequence will be
1108 * 1. set bus width 4bit / 8 bit (1 bit not supported)
1109 * 2. switch to HS200 mode
1110 * 3. set the clock to > 52Mhz <=200MHz and
1111 * 4. execute tuning for HS200
1112 */
1113 if ((host->caps2 & MMC_CAP2_HS200) &&
1114 card->host->ops->execute_tuning)
1115 err = card->host->ops->execute_tuning(card->host,
1116 MMC_SEND_TUNING_BLOCK_HS200);
1117 if (err) {
1118 pr_warning("%s: tuning execution failed\n",
1119 mmc_hostname(card->host));
1120 goto err;
1121 }
1122
1123 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1124 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
1125 err = mmc_select_powerclass(card, ext_csd_bits, ext_csd);
1126 if (err) {
1127 pr_err("%s: power class selection to bus width %d failed\n",
1128 mmc_hostname(card->host), 1 << bus_width);
1129 goto err;
1130 }
1131 }
1132
1133 /*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001134 * Activate wide bus and DDR (if supported).
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001135 */
Girish K Sa4924c72012-01-11 14:04:52 -05001136 if (!mmc_card_hs200(card) &&
Girish K S7488e922012-01-19 08:56:19 +05301137 (card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001138 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
Aries Lee22113ef2010-12-15 08:14:24 +01001139 static unsigned ext_csd_bits[][2] = {
1140 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
1141 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
1142 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
1143 };
1144 static unsigned bus_widths[] = {
1145 MMC_BUS_WIDTH_8,
1146 MMC_BUS_WIDTH_4,
1147 MMC_BUS_WIDTH_1
1148 };
1149 unsigned idx, bus_width = 0;
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001150
Aries Lee22113ef2010-12-15 08:14:24 +01001151 if (host->caps & MMC_CAP_8_BIT_DATA)
1152 idx = 0;
1153 else
1154 idx = 1;
1155 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1156 bus_width = bus_widths[idx];
1157 if (bus_width == MMC_BUS_WIDTH_1)
1158 ddr = 0; /* no DDR for 1-bit width */
Girish K Sb87d8db2011-09-23 20:41:47 +05301159 err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
1160 ext_csd);
1161 if (err)
1162 pr_err("%s: power class selection to "
1163 "bus width %d failed\n",
1164 mmc_hostname(card->host),
1165 1 << bus_width);
1166
Aries Lee22113ef2010-12-15 08:14:24 +01001167 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1168 EXT_CSD_BUS_WIDTH,
Andrei Warkentind3a8d952011-04-11 16:13:43 -05001169 ext_csd_bits[idx][0],
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001170 card->ext_csd.generic_cmd6_time);
Aries Lee22113ef2010-12-15 08:14:24 +01001171 if (!err) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301172 mmc_set_bus_width(card->host, bus_width);
Philip Rakity08ee80c2011-05-24 18:14:58 -07001173
Aries Lee22113ef2010-12-15 08:14:24 +01001174 /*
1175 * If controller can't handle bus width test,
Philip Rakity08ee80c2011-05-24 18:14:58 -07001176 * compare ext_csd previously read in 1 bit mode
1177 * against ext_csd at new bus width
Aries Lee22113ef2010-12-15 08:14:24 +01001178 */
1179 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
Philip Rakity08ee80c2011-05-24 18:14:58 -07001180 err = mmc_compare_ext_csds(card,
Philip Rakity08ee80c2011-05-24 18:14:58 -07001181 bus_width);
1182 else
1183 err = mmc_bus_test(card, bus_width);
Aries Lee22113ef2010-12-15 08:14:24 +01001184 if (!err)
1185 break;
1186 }
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001187 }
1188
Aries Lee22113ef2010-12-15 08:14:24 +01001189 if (!err && ddr) {
Girish K Sb87d8db2011-09-23 20:41:47 +05301190 err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
1191 ext_csd);
1192 if (err)
1193 pr_err("%s: power class selection to "
1194 "bus width %d ddr %d failed\n",
1195 mmc_hostname(card->host),
1196 1 << bus_width, ddr);
1197
Aries Lee22113ef2010-12-15 08:14:24 +01001198 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Andrei Warkentind3a8d952011-04-11 16:13:43 -05001199 EXT_CSD_BUS_WIDTH,
1200 ext_csd_bits[idx][1],
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001201 card->ext_csd.generic_cmd6_time);
Aries Lee22113ef2010-12-15 08:14:24 +01001202 }
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001203 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301204 pr_warning("%s: switch to bus width %d ddr %d "
Aries Lee22113ef2010-12-15 08:14:24 +01001205 "failed\n", mmc_hostname(card->host),
1206 1 << bus_width, ddr);
1207 goto free_card;
1208 } else if (ddr) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301209 /*
1210 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1211 * signaling.
1212 *
1213 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1214 *
1215 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1216 * in the JEDEC spec for DDR.
1217 *
1218 * Do not force change in vccq since we are obviously
1219 * working and no change to vccq is needed.
1220 *
1221 * WARNING: eMMC rules are NOT the same as SD DDR
1222 */
Girish K S913047e2011-12-15 17:27:42 +05301223 if (ddr == MMC_1_2V_DDR_MODE) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301224 err = mmc_set_signal_voltage(host,
1225 MMC_SIGNAL_VOLTAGE_120, 0);
1226 if (err)
1227 goto err;
1228 }
Aries Lee22113ef2010-12-15 08:14:24 +01001229 mmc_card_set_ddr_mode(card);
Philip Rakity4c4cb172011-05-13 11:17:18 +05301230 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1231 mmc_set_bus_width(card->host, bus_width);
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001232 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001233 }
1234
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001235 /*
1236 * If cache size is higher than 0, this indicates
1237 * the existence of cache and it can be turned on.
1238 */
1239 if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1240 card->ext_csd.cache_size > 0) {
1241 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001242 EXT_CSD_CACHE_CTRL, 1,
1243 card->ext_csd.generic_cmd6_time);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001244 if (err && err != -EBADMSG)
1245 goto free_card;
1246
1247 /*
1248 * Only if no error, cache is turned on successfully.
1249 */
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001250 if (err) {
1251 pr_warning("%s: Cache is supported, "
1252 "but failed to turn on (%d)\n",
1253 mmc_hostname(card->host), err);
1254 card->ext_csd.cache_ctrl = 0;
1255 err = 0;
1256 } else {
1257 card->ext_csd.cache_ctrl = 1;
1258 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001259 }
1260
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001261 if (!oldcard)
1262 host->card = card;
1263
Philip Rakity08ee80c2011-05-24 18:14:58 -07001264 mmc_free_ext_csd(ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001265 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001266
1267free_card:
1268 if (!oldcard)
1269 mmc_remove_card(card);
1270err:
Philip Rakity08ee80c2011-05-24 18:14:58 -07001271 mmc_free_ext_csd(ext_csd);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001272
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001273 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001274}
1275
1276/*
1277 * Host is being removed. Free up the current card.
1278 */
1279static void mmc_remove(struct mmc_host *host)
1280{
1281 BUG_ON(!host);
1282 BUG_ON(!host->card);
1283
1284 mmc_remove_card(host->card);
1285 host->card = NULL;
1286}
1287
1288/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001289 * Card detection - card is alive.
1290 */
1291static int mmc_alive(struct mmc_host *host)
1292{
1293 return mmc_send_status(host->card, NULL);
1294}
1295
1296/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001297 * Card detection callback from host.
1298 */
1299static void mmc_detect(struct mmc_host *host)
1300{
1301 int err;
1302
1303 BUG_ON(!host);
1304 BUG_ON(!host->card);
1305
1306 mmc_claim_host(host);
1307
1308 /*
1309 * Just check if our card has been removed.
1310 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001311 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001312
1313 mmc_release_host(host);
1314
Pierre Ossman17b04292007-07-22 22:18:46 +02001315 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001316 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001317
1318 mmc_claim_host(host);
1319 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001320 mmc_power_off(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001321 mmc_release_host(host);
1322 }
1323}
1324
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001325/*
1326 * Suspend callback from host.
1327 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001328static int mmc_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001329{
Balaji T Kc3805462011-09-08 22:08:39 +05301330 int err = 0;
1331
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001332 BUG_ON(!host);
1333 BUG_ON(!host->card);
1334
1335 mmc_claim_host(host);
Girish K S3e73c362012-01-31 15:44:03 +05301336 if (mmc_card_can_sleep(host)) {
Balaji T Kc3805462011-09-08 22:08:39 +05301337 err = mmc_card_sleep(host);
Girish K S3e73c362012-01-31 15:44:03 +05301338 if (!err)
1339 mmc_card_set_sleep(host->card);
1340 } else if (!mmc_host_is_spi(host))
David Brownellaf517152007-08-08 09:11:32 -07001341 mmc_deselect_cards(host);
Girish K S3e73c362012-01-31 15:44:03 +05301342 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001343 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001344
Balaji T Kc3805462011-09-08 22:08:39 +05301345 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001346}
1347
1348/*
1349 * Resume callback from host.
1350 *
1351 * This function tries to determine if the same card is still present
1352 * and, if so, restore all state to it.
1353 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001354static int mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001355{
1356 int err;
1357
1358 BUG_ON(!host);
1359 BUG_ON(!host->card);
1360
1361 mmc_claim_host(host);
Girish K S3e73c362012-01-31 15:44:03 +05301362 if (mmc_card_is_sleep(host->card)) {
1363 err = mmc_card_awake(host);
1364 mmc_card_clr_sleep(host->card);
1365 } else
1366 err = mmc_init_card(host, host->ocr, host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001367 mmc_release_host(host);
1368
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001369 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001370}
1371
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001372static int mmc_power_restore(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001373{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001374 int ret;
1375
Girish K S3e73c362012-01-31 15:44:03 +05301376 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
1377 mmc_card_clr_sleep(host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001378 mmc_claim_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001379 ret = mmc_init_card(host, host->ocr, host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001380 mmc_release_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001381
1382 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001383}
1384
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001385static int mmc_sleep(struct mmc_host *host)
1386{
1387 struct mmc_card *card = host->card;
1388 int err = -ENOSYS;
1389
1390 if (card && card->ext_csd.rev >= 3) {
1391 err = mmc_card_sleepawake(host, 1);
1392 if (err < 0)
1393 pr_debug("%s: Error %d while putting card into sleep",
1394 mmc_hostname(host), err);
1395 }
1396
1397 return err;
1398}
1399
1400static int mmc_awake(struct mmc_host *host)
1401{
1402 struct mmc_card *card = host->card;
1403 int err = -ENOSYS;
1404
1405 if (card && card->ext_csd.rev >= 3) {
1406 err = mmc_card_sleepawake(host, 0);
1407 if (err < 0)
1408 pr_debug("%s: Error %d while awaking sleeping card",
1409 mmc_hostname(host), err);
1410 }
1411
1412 return err;
1413}
1414
Adrian Hunter9feae242009-09-22 16:44:32 -07001415static const struct mmc_bus_ops mmc_ops = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001416 .awake = mmc_awake,
1417 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -07001418 .remove = mmc_remove,
1419 .detect = mmc_detect,
1420 .suspend = NULL,
1421 .resume = NULL,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001422 .power_restore = mmc_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001423 .alive = mmc_alive,
Adrian Hunter9feae242009-09-22 16:44:32 -07001424};
1425
1426static const struct mmc_bus_ops mmc_ops_unsafe = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001427 .awake = mmc_awake,
1428 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -07001429 .remove = mmc_remove,
1430 .detect = mmc_detect,
1431 .suspend = mmc_suspend,
1432 .resume = mmc_resume,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001433 .power_restore = mmc_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001434 .alive = mmc_alive,
Adrian Hunter9feae242009-09-22 16:44:32 -07001435};
1436
1437static void mmc_attach_bus_ops(struct mmc_host *host)
1438{
1439 const struct mmc_bus_ops *bus_ops;
1440
Matt Fleming71d7d3d2010-09-27 09:42:19 +01001441 if (!mmc_card_is_removable(host))
Adrian Hunter9feae242009-09-22 16:44:32 -07001442 bus_ops = &mmc_ops_unsafe;
1443 else
1444 bus_ops = &mmc_ops;
1445 mmc_attach_bus(host, bus_ops);
1446}
1447
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001448/*
1449 * Starting point for MMC card init.
1450 */
Andy Ross807e8e42011-01-03 10:36:56 -08001451int mmc_attach_mmc(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001452{
1453 int err;
Andy Ross807e8e42011-01-03 10:36:56 -08001454 u32 ocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001455
1456 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001457 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001458
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001459 /* Set correct bus mode for MMC before attempting attach */
1460 if (!mmc_host_is_spi(host))
1461 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1462
Andy Ross807e8e42011-01-03 10:36:56 -08001463 err = mmc_send_op_cond(host, 0, &ocr);
1464 if (err)
1465 return err;
1466
Adrian Hunter9feae242009-09-22 16:44:32 -07001467 mmc_attach_bus_ops(host);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001468 if (host->ocr_avail_mmc)
1469 host->ocr_avail = host->ocr_avail_mmc;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001470
1471 /*
David Brownellaf517152007-08-08 09:11:32 -07001472 * We need to get OCR a different way for SPI.
1473 */
1474 if (mmc_host_is_spi(host)) {
1475 err = mmc_spi_read_ocr(host, 1, &ocr);
1476 if (err)
1477 goto err;
1478 }
1479
1480 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001481 * Sanity check the voltages that the card claims to
1482 * support.
1483 */
1484 if (ocr & 0x7F) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301485 pr_warning("%s: card claims to support voltages "
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001486 "below the defined range. These will be ignored.\n",
1487 mmc_hostname(host));
1488 ocr &= ~0x7F;
1489 }
1490
1491 host->ocr = mmc_select_voltage(host, ocr);
1492
1493 /*
1494 * Can we support the voltage of the card?
1495 */
Pierre Ossman109b5be2007-07-23 00:12:10 +02001496 if (!host->ocr) {
1497 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001498 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001499 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001500
1501 /*
1502 * Detect and init the card.
1503 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001504 err = mmc_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001505 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001506 goto err;
1507
1508 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001509 err = mmc_add_card(host->card);
Andy Ross807e8e42011-01-03 10:36:56 -08001510 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001511 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001512 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001513
1514 return 0;
1515
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001516remove_card:
Andy Ross807e8e42011-01-03 10:36:56 -08001517 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001518 mmc_remove_card(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001519 mmc_claim_host(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001520 host->card = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001521err:
1522 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001523
Girish K Sa3c76eb2011-10-11 11:44:09 +05301524 pr_err("%s: error %d whilst initialising MMC card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02001525 mmc_hostname(host), err);
1526
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001527 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001528}