blob: c2334d636fe89b2a8a8147e32621e97f7f6d8d8b [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>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010015
16#include <linux/mmc/host.h>
17#include <linux/mmc/card.h>
18#include <linux/mmc/mmc.h>
19
20#include "core.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020021#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010022#include "mmc_ops.h"
Philip Rakity4c4cb172011-05-13 11:17:18 +053023#include "sd_ops.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010024
25static const unsigned int tran_exp[] = {
26 10000, 100000, 1000000, 10000000,
27 0, 0, 0, 0
28};
29
30static const unsigned char tran_mant[] = {
31 0, 10, 12, 13, 15, 20, 25, 30,
32 35, 40, 45, 50, 55, 60, 70, 80,
33};
34
35static const unsigned int tacc_exp[] = {
36 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
37};
38
39static const unsigned int tacc_mant[] = {
40 0, 10, 12, 13, 15, 20, 25, 30,
41 35, 40, 45, 50, 55, 60, 70, 80,
42};
43
44#define UNSTUFF_BITS(resp,start,size) \
45 ({ \
46 const int __size = size; \
47 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
48 const int __off = 3 - ((start) / 32); \
49 const int __shft = (start) & 31; \
50 u32 __res; \
51 \
52 __res = resp[__off] >> __shft; \
53 if (__size + __shft > 32) \
54 __res |= resp[__off-1] << ((32 - __shft) % 32); \
55 __res & __mask; \
56 })
57
58/*
59 * Given the decoded CSD structure, decode the raw CID to our CID structure.
60 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020061static int mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010062{
63 u32 *resp = card->raw_cid;
64
65 /*
66 * The selection of the format here is based upon published
67 * specs from sandisk and from what people have reported.
68 */
69 switch (card->csd.mmca_vsn) {
70 case 0: /* MMC v1.0 - v1.2 */
71 case 1: /* MMC v1.4 */
72 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
73 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
74 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
75 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
76 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
77 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
78 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
79 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
80 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
81 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
82 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
83 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
84 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
85 break;
86
87 case 2: /* MMC v2.0 - v2.2 */
88 case 3: /* MMC v3.1 - v3.3 */
89 case 4: /* MMC v4 */
90 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
91 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
92 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
93 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
94 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
95 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
96 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
97 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
98 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
99 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
100 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
101 break;
102
103 default:
Pierre Ossmanfacba912007-07-24 21:53:43 +0200104 printk(KERN_ERR "%s: card has unknown MMCA version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100105 mmc_hostname(card->host), card->csd.mmca_vsn);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200106 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100107 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200108
109 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100110}
111
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700112static void mmc_set_erase_size(struct mmc_card *card)
113{
114 if (card->ext_csd.erase_group_def & 1)
115 card->erase_size = card->ext_csd.hc_erase_size;
116 else
117 card->erase_size = card->csd.erase_size;
118
119 mmc_init_erase(card);
120}
121
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100122/*
123 * Given a 128-bit response, decode to our card CSD structure.
124 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200125static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100126{
127 struct mmc_csd *csd = &card->csd;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700128 unsigned int e, m, a, b;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100129 u32 *resp = card->raw_csd;
130
131 /*
132 * We only understand CSD structure v1.1 and v1.2.
133 * v1.2 has extra information in bits 15, 11 and 10.
Kyungmin Park6da24b72010-08-10 18:01:36 -0700134 * We also support eMMC v4.4 & v4.41.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100135 */
Kyungmin Park6da24b72010-08-10 18:01:36 -0700136 csd->structure = UNSTUFF_BITS(resp, 126, 2);
137 if (csd->structure == 0) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200138 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700139 mmc_hostname(card->host), csd->structure);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200140 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100141 }
142
143 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
144 m = UNSTUFF_BITS(resp, 115, 4);
145 e = UNSTUFF_BITS(resp, 112, 3);
146 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
147 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
148
149 m = UNSTUFF_BITS(resp, 99, 4);
150 e = UNSTUFF_BITS(resp, 96, 3);
151 csd->max_dtr = tran_exp[e] * tran_mant[m];
152 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
153
154 e = UNSTUFF_BITS(resp, 47, 3);
155 m = UNSTUFF_BITS(resp, 62, 12);
156 csd->capacity = (1 + m) << (e + 2);
157
158 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
159 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
160 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
161 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
162 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
163 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
164 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200165
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700166 if (csd->write_blkbits >= 9) {
167 a = UNSTUFF_BITS(resp, 42, 5);
168 b = UNSTUFF_BITS(resp, 37, 5);
169 csd->erase_size = (a + 1) * (b + 1);
170 csd->erase_size <<= csd->write_blkbits - 9;
171 }
172
Pierre Ossmanbd766312007-05-01 16:11:57 +0200173 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100174}
175
176/*
Philip Rakity08ee80c2011-05-24 18:14:58 -0700177 * Read extended CSD.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100178 */
Philip Rakity08ee80c2011-05-24 18:14:58 -0700179static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100180{
181 int err;
182 u8 *ext_csd;
183
184 BUG_ON(!card);
Philip Rakity08ee80c2011-05-24 18:14:58 -0700185 BUG_ON(!new_ext_csd);
186
187 *new_ext_csd = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100188
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100189 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
Pierre Ossman17b04292007-07-22 22:18:46 +0200190 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100191
192 /*
193 * As the ext_csd is so large and mostly unused, we don't store the
194 * raw block in mmc_card.
195 */
196 ext_csd = kmalloc(512, GFP_KERNEL);
197 if (!ext_csd) {
198 printk(KERN_ERR "%s: could not allocate a buffer to "
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200199 "receive the ext_csd.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200200 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100201 }
202
203 err = mmc_send_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200204 if (err) {
Philip Rakity08ee80c2011-05-24 18:14:58 -0700205 kfree(ext_csd);
206 *new_ext_csd = NULL;
207
Wolfgang Mueesd08ebed2009-09-22 16:45:26 -0700208 /* If the host or the card can't do the switch,
209 * fail more gracefully. */
210 if ((err != -EINVAL)
211 && (err != -ENOSYS)
212 && (err != -EFAULT))
Philip Rakity08ee80c2011-05-24 18:14:58 -0700213 return err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200214
215 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100216 * High capacity cards should have this "magic" size
217 * stored in their CSD.
218 */
219 if (card->csd.capacity == (4096 * 512)) {
220 printk(KERN_ERR "%s: unable to read EXT_CSD "
221 "on a possible high capacity card. "
222 "Card will be ignored.\n",
223 mmc_hostname(card->host));
224 } else {
225 printk(KERN_WARNING "%s: unable to read "
226 "EXT_CSD, performance might "
227 "suffer.\n",
228 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200229 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100230 }
Philip Rakity08ee80c2011-05-24 18:14:58 -0700231 } else
232 *new_ext_csd = ext_csd;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200233
Philip Rakity08ee80c2011-05-24 18:14:58 -0700234 return err;
235}
236
237/*
238 * Decode extended CSD.
239 */
240static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
241{
242 int err = 0;
243
244 BUG_ON(!card);
245
246 if (!ext_csd)
247 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100248
Kyungmin Park6da24b72010-08-10 18:01:36 -0700249 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700250 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
Kyungmin Park6da24b72010-08-10 18:01:36 -0700251 if (card->csd.structure == 3) {
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700252 if (card->ext_csd.raw_ext_csd_structure > 2) {
Kyungmin Park6da24b72010-08-10 18:01:36 -0700253 printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
254 "version %d\n", mmc_hostname(card->host),
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700255 card->ext_csd.raw_ext_csd_structure);
Kyungmin Park6da24b72010-08-10 18:01:36 -0700256 err = -EINVAL;
257 goto out;
258 }
259 }
260
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700261 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Kyungmin Park38ca2852011-07-26 17:12:37 +0900262 if (card->ext_csd.rev > 6) {
Kyungmin Park6da24b72010-08-10 18:01:36 -0700263 printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n",
264 mmc_hostname(card->host), card->ext_csd.rev);
Florin Malita00cedfa2007-10-13 12:27:20 -0400265 err = -EINVAL;
266 goto out;
Pierre Ossmand7604d72007-07-23 00:34:07 +0200267 }
268
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700269 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
270 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
271 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
272 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700273 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200274 card->ext_csd.sectors =
275 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
276 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
277 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
278 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700279
280 /* Cards with density > 2GiB are sector addressed */
281 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200282 mmc_card_set_blockaddr(card);
283 }
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700284 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
Adrian Hunter7198f3c2010-03-23 13:35:40 -0700285 switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
Hanumath Prasaddfc13e82010-09-30 17:37:23 -0400286 case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 |
287 EXT_CSD_CARD_TYPE_26:
288 card->ext_csd.hs_max_dtr = 52000000;
289 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52;
290 break;
291 case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 |
292 EXT_CSD_CARD_TYPE_26:
293 card->ext_csd.hs_max_dtr = 52000000;
294 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V;
295 break;
296 case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 |
297 EXT_CSD_CARD_TYPE_26:
298 card->ext_csd.hs_max_dtr = 52000000;
299 card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V;
300 break;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100301 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
302 card->ext_csd.hs_max_dtr = 52000000;
303 break;
304 case EXT_CSD_CARD_TYPE_26:
305 card->ext_csd.hs_max_dtr = 26000000;
306 break;
307 default:
308 /* MMC v4 spec says this cannot happen */
309 printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
310 "support any high-speed modes.\n",
311 mmc_hostname(card->host));
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100312 }
313
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700314 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
315 card->ext_csd.raw_erase_timeout_mult =
316 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
317 card->ext_csd.raw_hc_erase_grp_size =
318 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700319 if (card->ext_csd.rev >= 3) {
320 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500321 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
322
323 /* EXT_CSD value is in units of 10ms, but we store in ms */
324 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700325
326 /* Sleep / awake timeout in 100ns units */
327 if (sa_shift > 0 && sa_shift <= 0x17)
328 card->ext_csd.sa_timeout =
329 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700330 card->ext_csd.erase_group_def =
331 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
332 card->ext_csd.hc_erase_timeout = 300 *
333 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
334 card->ext_csd.hc_erase_size =
335 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500336
337 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500338
339 /*
340 * There are two boot regions of equal size, defined in
341 * multiples of 128K.
342 */
343 card->ext_csd.boot_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700344 }
345
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700346 card->ext_csd.raw_hc_erase_gap_size =
347 ext_csd[EXT_CSD_PARTITION_ATTRIBUTE];
348 card->ext_csd.raw_sec_trim_mult =
349 ext_csd[EXT_CSD_SEC_TRIM_MULT];
350 card->ext_csd.raw_sec_erase_mult =
351 ext_csd[EXT_CSD_SEC_ERASE_MULT];
352 card->ext_csd.raw_sec_feature_support =
353 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
354 card->ext_csd.raw_trim_mult =
355 ext_csd[EXT_CSD_TRIM_MULT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700356 if (card->ext_csd.rev >= 4) {
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800357 /*
358 * Enhanced area feature support -- check whether the eMMC
359 * card has the Enhanced area enabled. If so, export enhanced
360 * area offset and size to user by adding sysfs interface.
361 */
362 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700363 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800364 u8 hc_erase_grp_sz =
365 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
366 u8 hc_wp_grp_sz =
367 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
368
369 card->ext_csd.enhanced_area_en = 1;
370 /*
371 * calculate the enhanced data area offset, in bytes
372 */
373 card->ext_csd.enhanced_area_offset =
374 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
375 (ext_csd[137] << 8) + ext_csd[136];
376 if (mmc_card_blockaddr(card))
377 card->ext_csd.enhanced_area_offset <<= 9;
378 /*
379 * calculate the enhanced data area size, in kilobytes
380 */
381 card->ext_csd.enhanced_area_size =
382 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
383 ext_csd[140];
384 card->ext_csd.enhanced_area_size *=
385 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
386 card->ext_csd.enhanced_area_size <<= 9;
387 } else {
388 /*
389 * If the enhanced area is not enabled, disable these
390 * device attributes.
391 */
392 card->ext_csd.enhanced_area_offset = -EINVAL;
393 card->ext_csd.enhanced_area_size = -EINVAL;
394 }
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700395 card->ext_csd.sec_trim_mult =
396 ext_csd[EXT_CSD_SEC_TRIM_MULT];
397 card->ext_csd.sec_erase_mult =
398 ext_csd[EXT_CSD_SEC_ERASE_MULT];
399 card->ext_csd.sec_feature_support =
400 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
401 card->ext_csd.trim_timeout = 300 *
402 ext_csd[EXT_CSD_TRIM_MULT];
403 }
404
Adrian Hunterb2499512011-08-29 16:42:11 +0300405 if (card->ext_csd.rev >= 5) {
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500406 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
Adrian Hunterb2499512011-08-29 16:42:11 +0300407 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
408 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500409
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700410 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
411 card->erased_byte = 0xFF;
412 else
413 card->erased_byte = 0x0;
414
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100415out:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700416 return err;
417}
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100418
Philip Rakity08ee80c2011-05-24 18:14:58 -0700419static inline void mmc_free_ext_csd(u8 *ext_csd)
420{
421 kfree(ext_csd);
422}
423
424
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700425static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700426{
427 u8 *bw_ext_csd;
428 int err;
429
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700430 if (bus_width == MMC_BUS_WIDTH_1)
431 return 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700432
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700433 err = mmc_get_ext_csd(card, &bw_ext_csd);
434
435 if (err || bw_ext_csd == NULL) {
Philip Rakity08ee80c2011-05-24 18:14:58 -0700436 if (bus_width != MMC_BUS_WIDTH_1)
437 err = -EINVAL;
438 goto out;
439 }
440
441 if (bus_width == MMC_BUS_WIDTH_1)
442 goto out;
443
444 /* only compare read only fields */
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700445 err = (!(card->ext_csd.raw_partition_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700446 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700447 (card->ext_csd.raw_erased_mem_count ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700448 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700449 (card->ext_csd.rev ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700450 bw_ext_csd[EXT_CSD_REV]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700451 (card->ext_csd.raw_ext_csd_structure ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700452 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700453 (card->ext_csd.raw_card_type ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700454 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700455 (card->ext_csd.raw_s_a_timeout ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700456 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700457 (card->ext_csd.raw_hc_erase_gap_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700458 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700459 (card->ext_csd.raw_erase_timeout_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700460 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700461 (card->ext_csd.raw_hc_erase_grp_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700462 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700463 (card->ext_csd.raw_sec_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700464 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700465 (card->ext_csd.raw_sec_erase_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700466 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700467 (card->ext_csd.raw_sec_feature_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700468 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700469 (card->ext_csd.raw_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700470 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700471 (card->ext_csd.raw_sectors[0] ==
472 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
473 (card->ext_csd.raw_sectors[1] ==
474 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
475 (card->ext_csd.raw_sectors[2] ==
476 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
477 (card->ext_csd.raw_sectors[3] ==
478 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
Philip Rakity08ee80c2011-05-24 18:14:58 -0700479 if (err)
480 err = -EINVAL;
481
482out:
483 mmc_free_ext_csd(bw_ext_csd);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100484 return err;
485}
486
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100487MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
488 card->raw_cid[2], card->raw_cid[3]);
489MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
490 card->raw_csd[2], card->raw_csd[3]);
491MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700492MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
493MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100494MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
495MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
496MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
497MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
498MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
499MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800500MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
501 card->ext_csd.enhanced_area_offset);
502MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100503
504static struct attribute *mmc_std_attrs[] = {
505 &dev_attr_cid.attr,
506 &dev_attr_csd.attr,
507 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700508 &dev_attr_erase_size.attr,
509 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100510 &dev_attr_fwrev.attr,
511 &dev_attr_hwrev.attr,
512 &dev_attr_manfid.attr,
513 &dev_attr_name.attr,
514 &dev_attr_oemid.attr,
515 &dev_attr_serial.attr,
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800516 &dev_attr_enhanced_area_offset.attr,
517 &dev_attr_enhanced_area_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100518 NULL,
519};
520
521static struct attribute_group mmc_std_attr_group = {
522 .attrs = mmc_std_attrs,
523};
524
David Brownella4dbd672009-06-24 10:06:31 -0700525static const struct attribute_group *mmc_attr_groups[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100526 &mmc_std_attr_group,
527 NULL,
528};
529
530static struct device_type mmc_type = {
531 .groups = mmc_attr_groups,
532};
533
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100534/*
Girish K Sb87d8db2011-09-23 20:41:47 +0530535 * Select the PowerClass for the current bus width
536 * If power class is defined for 4/8 bit bus in the
537 * extended CSD register, select it by executing the
538 * mmc_switch command.
539 */
540static int mmc_select_powerclass(struct mmc_card *card,
541 unsigned int bus_width, u8 *ext_csd)
542{
543 int err = 0;
544 unsigned int pwrclass_val;
545 unsigned int index = 0;
546 struct mmc_host *host;
547
548 BUG_ON(!card);
549
550 host = card->host;
551 BUG_ON(!host);
552
553 if (ext_csd == NULL)
554 return 0;
555
556 /* Power class selection is supported for versions >= 4.0 */
557 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
558 return 0;
559
560 /* Power class values are defined only for 4/8 bit bus */
561 if (bus_width == EXT_CSD_BUS_WIDTH_1)
562 return 0;
563
564 switch (1 << host->ios.vdd) {
565 case MMC_VDD_165_195:
566 if (host->ios.clock <= 26000000)
567 index = EXT_CSD_PWR_CL_26_195;
568 else if (host->ios.clock <= 52000000)
569 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
570 EXT_CSD_PWR_CL_52_195 :
571 EXT_CSD_PWR_CL_DDR_52_195;
572 else if (host->ios.clock <= 200000000)
573 index = EXT_CSD_PWR_CL_200_195;
574 break;
575 case MMC_VDD_32_33:
576 case MMC_VDD_33_34:
577 case MMC_VDD_34_35:
578 case MMC_VDD_35_36:
579 if (host->ios.clock <= 26000000)
580 index = EXT_CSD_PWR_CL_26_360;
581 else if (host->ios.clock <= 52000000)
582 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
583 EXT_CSD_PWR_CL_52_360 :
584 EXT_CSD_PWR_CL_DDR_52_360;
585 else if (host->ios.clock <= 200000000)
586 index = EXT_CSD_PWR_CL_200_360;
587 break;
588 default:
589 pr_warning("%s: Voltage range not supported "
590 "for power class.\n", mmc_hostname(host));
591 return -EINVAL;
592 }
593
594 pwrclass_val = ext_csd[index];
595
596 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
597 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
598 EXT_CSD_PWR_CL_8BIT_SHIFT;
599 else
600 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
601 EXT_CSD_PWR_CL_4BIT_SHIFT;
602
603 /* If the power class is different from the default value */
604 if (pwrclass_val > 0) {
605 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
606 EXT_CSD_POWER_CLASS,
607 pwrclass_val,
608 0);
609 }
610
611 return err;
612}
613
614/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200615 * Handle the detection and initialisation of a card.
616 *
Deepak Saxena87693922008-06-16 19:20:57 -0700617 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200618 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100619 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +0200620static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200621 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100622{
623 struct mmc_card *card;
Philip Rakity25d5c692010-11-07 16:22:28 -0500624 int err, ddr = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100625 u32 cid[4];
626 unsigned int max_dtr;
Philip Rakityb676f032011-02-13 23:13:09 -0800627 u32 rocr;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700628 u8 *ext_csd = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100629
630 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200631 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100632
Stefan Nilsson XK44669032011-09-15 17:50:38 +0200633 /* Set correct bus mode for MMC before attempting init */
634 if (!mmc_host_is_spi(host))
635 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
636
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100637 /*
638 * Since we're changing the OCR value, we seem to
639 * need to tell some cards to go back to the idle
640 * state. We wait 1ms to give cards time to
641 * respond.
Balaji T Kc3805462011-09-08 22:08:39 +0530642 * mmc_go_idle is needed for eMMC that are asleep
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100643 */
644 mmc_go_idle(host);
645
646 /* The extra bit indicates that we support high capacity */
Philip Rakityb676f032011-02-13 23:13:09 -0800647 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200648 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200649 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100650
651 /*
David Brownellaf517152007-08-08 09:11:32 -0700652 * For SPI, enable CRC as appropriate.
653 */
654 if (mmc_host_is_spi(host)) {
655 err = mmc_spi_set_crc(host, use_spi_crc);
656 if (err)
657 goto err;
658 }
659
660 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100661 * Fetch CID from card.
662 */
David Brownellaf517152007-08-08 09:11:32 -0700663 if (mmc_host_is_spi(host))
664 err = mmc_send_cid(host, cid);
665 else
666 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +0200667 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100668 goto err;
669
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200670 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200671 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
672 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200673 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200674 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100675
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200676 card = oldcard;
677 } else {
678 /*
679 * Allocate card structure.
680 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100681 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200682 if (IS_ERR(card)) {
683 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200684 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200685 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200686
687 card->type = MMC_TYPE_MMC;
688 card->rca = 1;
689 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
690 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100691
692 /*
David Brownellaf517152007-08-08 09:11:32 -0700693 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100694 */
David Brownellaf517152007-08-08 09:11:32 -0700695 if (!mmc_host_is_spi(host)) {
696 err = mmc_set_relative_addr(card);
697 if (err)
698 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100699
David Brownellaf517152007-08-08 09:11:32 -0700700 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
701 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100702
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200703 if (!oldcard) {
704 /*
705 * Fetch CSD from card.
706 */
707 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200708 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200709 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100710
Pierre Ossmanbd766312007-05-01 16:11:57 +0200711 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200712 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +0200713 goto free_card;
714 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200715 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +0200716 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200717 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100718
719 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200720 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100721 */
David Brownellaf517152007-08-08 09:11:32 -0700722 if (!mmc_host_is_spi(host)) {
723 err = mmc_select_card(card);
724 if (err)
725 goto free_card;
726 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100727
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200728 if (!oldcard) {
729 /*
David Brownellaf517152007-08-08 09:11:32 -0700730 * Fetch and process extended CSD.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200731 */
Philip Rakity08ee80c2011-05-24 18:14:58 -0700732
733 err = mmc_get_ext_csd(card, &ext_csd);
734 if (err)
735 goto free_card;
736 err = mmc_read_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200737 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200738 goto free_card;
Philip Rakityb676f032011-02-13 23:13:09 -0800739
740 /* If doing byte addressing, check if required to do sector
741 * addressing. Handle the case of <2GB cards needing sector
742 * addressing. See section 8.1 JEDEC Standard JED84-A441;
743 * ocr register has bit 30 set for sector addressing.
744 */
745 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
746 mmc_card_set_blockaddr(card);
747
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700748 /* Erase size depends on CSD and Extended CSD */
749 mmc_set_erase_size(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200750 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100751
752 /*
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800753 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300754 * bit. This bit will be lost every time after a reset or power off.
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800755 */
756 if (card->ext_csd.enhanced_area_en) {
757 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500758 EXT_CSD_ERASE_GROUP_DEF, 1, 0);
Chuanxiao Dong709de99d2011-01-22 04:09:41 +0800759
760 if (err && err != -EBADMSG)
761 goto free_card;
762
763 if (err) {
764 err = 0;
765 /*
766 * Just disable enhanced area off & sz
767 * will try to enable ERASE_GROUP_DEF
768 * during next time reinit
769 */
770 card->ext_csd.enhanced_area_offset = -EINVAL;
771 card->ext_csd.enhanced_area_size = -EINVAL;
772 } else {
773 card->ext_csd.erase_group_def = 1;
774 /*
775 * enable ERASE_GRP_DEF successfully.
776 * This will affect the erase size, so
777 * here need to reset erase size
778 */
779 mmc_set_erase_size(card);
780 }
781 }
782
783 /*
Philip Rakity41e2a482011-03-19 14:10:33 -0400784 * Ensure eMMC user default partition is enabled
785 */
Andrei Warkentin371a6892011-04-11 18:10:25 -0500786 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
787 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
788 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
789 card->ext_csd.part_config,
790 card->ext_csd.part_time);
791 if (err && err != -EBADMSG)
792 goto free_card;
Philip Rakity41e2a482011-03-19 14:10:33 -0400793 }
794
795 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200796 * Activate high speed (if supported)
797 */
798 if ((card->ext_csd.hs_max_dtr != 0) &&
799 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
800 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500801 EXT_CSD_HS_TIMING, 1, 0);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700802 if (err && err != -EBADMSG)
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200803 goto free_card;
804
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700805 if (err) {
806 printk(KERN_WARNING "%s: switch to highspeed failed\n",
807 mmc_hostname(card->host));
808 err = 0;
809 } else {
810 mmc_card_set_highspeed(card);
811 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
812 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200813 }
814
815 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100816 * Compute bus speed.
817 */
818 max_dtr = (unsigned int)-1;
819
820 if (mmc_card_highspeed(card)) {
821 if (max_dtr > card->ext_csd.hs_max_dtr)
822 max_dtr = card->ext_csd.hs_max_dtr;
823 } else if (max_dtr > card->csd.max_dtr) {
824 max_dtr = card->csd.max_dtr;
825 }
826
827 mmc_set_clock(host, max_dtr);
828
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200829 /*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300830 * Indicate DDR mode (if supported).
Hanumath Prasaddfc13e82010-09-30 17:37:23 -0400831 */
832 if (mmc_card_highspeed(card)) {
833 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
Philip Rakity4c4cb172011-05-13 11:17:18 +0530834 && ((host->caps & (MMC_CAP_1_8V_DDR |
835 MMC_CAP_UHS_DDR50))
836 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
Adrian Hunter49e3b5a2010-10-11 12:43:50 +0300837 ddr = MMC_1_8V_DDR_MODE;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -0400838 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
Philip Rakity4c4cb172011-05-13 11:17:18 +0530839 && ((host->caps & (MMC_CAP_1_2V_DDR |
840 MMC_CAP_UHS_DDR50))
841 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
Adrian Hunter49e3b5a2010-10-11 12:43:50 +0300842 ddr = MMC_1_2V_DDR_MODE;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -0400843 }
844
845 /*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300846 * Activate wide bus and DDR (if supported).
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200847 */
848 if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +0200849 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
Aries Lee22113ef2010-12-15 08:14:24 +0100850 static unsigned ext_csd_bits[][2] = {
851 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
852 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
853 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
854 };
855 static unsigned bus_widths[] = {
856 MMC_BUS_WIDTH_8,
857 MMC_BUS_WIDTH_4,
858 MMC_BUS_WIDTH_1
859 };
860 unsigned idx, bus_width = 0;
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +0200861
Aries Lee22113ef2010-12-15 08:14:24 +0100862 if (host->caps & MMC_CAP_8_BIT_DATA)
863 idx = 0;
864 else
865 idx = 1;
866 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
867 bus_width = bus_widths[idx];
868 if (bus_width == MMC_BUS_WIDTH_1)
869 ddr = 0; /* no DDR for 1-bit width */
Girish K Sb87d8db2011-09-23 20:41:47 +0530870 err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
871 ext_csd);
872 if (err)
873 pr_err("%s: power class selection to "
874 "bus width %d failed\n",
875 mmc_hostname(card->host),
876 1 << bus_width);
877
Aries Lee22113ef2010-12-15 08:14:24 +0100878 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
879 EXT_CSD_BUS_WIDTH,
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500880 ext_csd_bits[idx][0],
881 0);
Aries Lee22113ef2010-12-15 08:14:24 +0100882 if (!err) {
Philip Rakity4c4cb172011-05-13 11:17:18 +0530883 mmc_set_bus_width(card->host, bus_width);
Philip Rakity08ee80c2011-05-24 18:14:58 -0700884
Aries Lee22113ef2010-12-15 08:14:24 +0100885 /*
886 * If controller can't handle bus width test,
Philip Rakity08ee80c2011-05-24 18:14:58 -0700887 * compare ext_csd previously read in 1 bit mode
888 * against ext_csd at new bus width
Aries Lee22113ef2010-12-15 08:14:24 +0100889 */
890 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
Philip Rakity08ee80c2011-05-24 18:14:58 -0700891 err = mmc_compare_ext_csds(card,
Philip Rakity08ee80c2011-05-24 18:14:58 -0700892 bus_width);
893 else
894 err = mmc_bus_test(card, bus_width);
Aries Lee22113ef2010-12-15 08:14:24 +0100895 if (!err)
896 break;
897 }
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +0200898 }
899
Aries Lee22113ef2010-12-15 08:14:24 +0100900 if (!err && ddr) {
Girish K Sb87d8db2011-09-23 20:41:47 +0530901 err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
902 ext_csd);
903 if (err)
904 pr_err("%s: power class selection to "
905 "bus width %d ddr %d failed\n",
906 mmc_hostname(card->host),
907 1 << bus_width, ddr);
908
Aries Lee22113ef2010-12-15 08:14:24 +0100909 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Andrei Warkentind3a8d952011-04-11 16:13:43 -0500910 EXT_CSD_BUS_WIDTH,
911 ext_csd_bits[idx][1],
912 0);
Aries Lee22113ef2010-12-15 08:14:24 +0100913 }
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700914 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300915 printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
Aries Lee22113ef2010-12-15 08:14:24 +0100916 "failed\n", mmc_hostname(card->host),
917 1 << bus_width, ddr);
918 goto free_card;
919 } else if (ddr) {
Philip Rakity4c4cb172011-05-13 11:17:18 +0530920 /*
921 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
922 * signaling.
923 *
924 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
925 *
926 * 1.8V vccq at 3.3V core voltage (vcc) is not required
927 * in the JEDEC spec for DDR.
928 *
929 * Do not force change in vccq since we are obviously
930 * working and no change to vccq is needed.
931 *
932 * WARNING: eMMC rules are NOT the same as SD DDR
933 */
934 if (ddr == EXT_CSD_CARD_TYPE_DDR_1_2V) {
935 err = mmc_set_signal_voltage(host,
936 MMC_SIGNAL_VOLTAGE_120, 0);
937 if (err)
938 goto err;
939 }
Aries Lee22113ef2010-12-15 08:14:24 +0100940 mmc_card_set_ddr_mode(card);
Philip Rakity4c4cb172011-05-13 11:17:18 +0530941 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
942 mmc_set_bus_width(card->host, bus_width);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700943 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200944 }
945
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200946 if (!oldcard)
947 host->card = card;
948
Philip Rakity08ee80c2011-05-24 18:14:58 -0700949 mmc_free_ext_csd(ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200950 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200951
952free_card:
953 if (!oldcard)
954 mmc_remove_card(card);
955err:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700956 mmc_free_ext_csd(ext_csd);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200957
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200958 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200959}
960
961/*
962 * Host is being removed. Free up the current card.
963 */
964static void mmc_remove(struct mmc_host *host)
965{
966 BUG_ON(!host);
967 BUG_ON(!host->card);
968
969 mmc_remove_card(host->card);
970 host->card = NULL;
971}
972
973/*
974 * Card detection callback from host.
975 */
976static void mmc_detect(struct mmc_host *host)
977{
978 int err;
979
980 BUG_ON(!host);
981 BUG_ON(!host->card);
982
983 mmc_claim_host(host);
984
985 /*
986 * Just check if our card has been removed.
987 */
988 err = mmc_send_status(host->card, NULL);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100989
990 mmc_release_host(host);
991
Pierre Ossman17b04292007-07-22 22:18:46 +0200992 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +0200993 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200994
995 mmc_claim_host(host);
996 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -0400997 mmc_power_off(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200998 mmc_release_host(host);
999 }
1000}
1001
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001002/*
1003 * Suspend callback from host.
1004 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001005static int mmc_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001006{
Balaji T Kc3805462011-09-08 22:08:39 +05301007 int err = 0;
1008
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001009 BUG_ON(!host);
1010 BUG_ON(!host->card);
1011
1012 mmc_claim_host(host);
Balaji T Kc3805462011-09-08 22:08:39 +05301013 if (mmc_card_can_sleep(host))
1014 err = mmc_card_sleep(host);
1015 else if (!mmc_host_is_spi(host))
David Brownellaf517152007-08-08 09:11:32 -07001016 mmc_deselect_cards(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001017 host->card->state &= ~MMC_STATE_HIGHSPEED;
1018 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001019
Balaji T Kc3805462011-09-08 22:08:39 +05301020 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001021}
1022
1023/*
1024 * Resume callback from host.
1025 *
1026 * This function tries to determine if the same card is still present
1027 * and, if so, restore all state to it.
1028 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001029static int mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001030{
1031 int err;
1032
1033 BUG_ON(!host);
1034 BUG_ON(!host->card);
1035
1036 mmc_claim_host(host);
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001037 err = mmc_init_card(host, host->ocr, host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001038 mmc_release_host(host);
1039
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001040 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001041}
1042
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001043static int mmc_power_restore(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001044{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001045 int ret;
1046
Adrian Huntereae1aee2009-09-22 16:44:33 -07001047 host->card->state &= ~MMC_STATE_HIGHSPEED;
1048 mmc_claim_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001049 ret = mmc_init_card(host, host->ocr, host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001050 mmc_release_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001051
1052 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001053}
1054
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001055static int mmc_sleep(struct mmc_host *host)
1056{
1057 struct mmc_card *card = host->card;
1058 int err = -ENOSYS;
1059
1060 if (card && card->ext_csd.rev >= 3) {
1061 err = mmc_card_sleepawake(host, 1);
1062 if (err < 0)
1063 pr_debug("%s: Error %d while putting card into sleep",
1064 mmc_hostname(host), err);
1065 }
1066
1067 return err;
1068}
1069
1070static int mmc_awake(struct mmc_host *host)
1071{
1072 struct mmc_card *card = host->card;
1073 int err = -ENOSYS;
1074
1075 if (card && card->ext_csd.rev >= 3) {
1076 err = mmc_card_sleepawake(host, 0);
1077 if (err < 0)
1078 pr_debug("%s: Error %d while awaking sleeping card",
1079 mmc_hostname(host), err);
1080 }
1081
1082 return err;
1083}
1084
Adrian Hunter9feae242009-09-22 16:44:32 -07001085static const struct mmc_bus_ops mmc_ops = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001086 .awake = mmc_awake,
1087 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -07001088 .remove = mmc_remove,
1089 .detect = mmc_detect,
1090 .suspend = NULL,
1091 .resume = NULL,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001092 .power_restore = mmc_power_restore,
Adrian Hunter9feae242009-09-22 16:44:32 -07001093};
1094
1095static const struct mmc_bus_ops mmc_ops_unsafe = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001096 .awake = mmc_awake,
1097 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -07001098 .remove = mmc_remove,
1099 .detect = mmc_detect,
1100 .suspend = mmc_suspend,
1101 .resume = mmc_resume,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001102 .power_restore = mmc_power_restore,
Adrian Hunter9feae242009-09-22 16:44:32 -07001103};
1104
1105static void mmc_attach_bus_ops(struct mmc_host *host)
1106{
1107 const struct mmc_bus_ops *bus_ops;
1108
Matt Fleming71d7d3d2010-09-27 09:42:19 +01001109 if (!mmc_card_is_removable(host))
Adrian Hunter9feae242009-09-22 16:44:32 -07001110 bus_ops = &mmc_ops_unsafe;
1111 else
1112 bus_ops = &mmc_ops;
1113 mmc_attach_bus(host, bus_ops);
1114}
1115
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001116/*
1117 * Starting point for MMC card init.
1118 */
Andy Ross807e8e42011-01-03 10:36:56 -08001119int mmc_attach_mmc(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001120{
1121 int err;
Andy Ross807e8e42011-01-03 10:36:56 -08001122 u32 ocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001123
1124 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001125 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001126
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001127 /* Set correct bus mode for MMC before attempting attach */
1128 if (!mmc_host_is_spi(host))
1129 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1130
Andy Ross807e8e42011-01-03 10:36:56 -08001131 err = mmc_send_op_cond(host, 0, &ocr);
1132 if (err)
1133 return err;
1134
Adrian Hunter9feae242009-09-22 16:44:32 -07001135 mmc_attach_bus_ops(host);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001136 if (host->ocr_avail_mmc)
1137 host->ocr_avail = host->ocr_avail_mmc;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001138
1139 /*
David Brownellaf517152007-08-08 09:11:32 -07001140 * We need to get OCR a different way for SPI.
1141 */
1142 if (mmc_host_is_spi(host)) {
1143 err = mmc_spi_read_ocr(host, 1, &ocr);
1144 if (err)
1145 goto err;
1146 }
1147
1148 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001149 * Sanity check the voltages that the card claims to
1150 * support.
1151 */
1152 if (ocr & 0x7F) {
1153 printk(KERN_WARNING "%s: card claims to support voltages "
1154 "below the defined range. These will be ignored.\n",
1155 mmc_hostname(host));
1156 ocr &= ~0x7F;
1157 }
1158
1159 host->ocr = mmc_select_voltage(host, ocr);
1160
1161 /*
1162 * Can we support the voltage of the card?
1163 */
Pierre Ossman109b5be2007-07-23 00:12:10 +02001164 if (!host->ocr) {
1165 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001166 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001167 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001168
1169 /*
1170 * Detect and init the card.
1171 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001172 err = mmc_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001173 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001174 goto err;
1175
1176 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001177 err = mmc_add_card(host->card);
Andy Ross807e8e42011-01-03 10:36:56 -08001178 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001179 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001180 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001181
1182 return 0;
1183
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001184remove_card:
Andy Ross807e8e42011-01-03 10:36:56 -08001185 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001186 mmc_remove_card(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001187 mmc_claim_host(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001188 host->card = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001189err:
1190 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001191
Pierre Ossman109b5be2007-07-23 00:12:10 +02001192 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
1193 mmc_hostname(host), err);
1194
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001195 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001196}