blob: 89f8c91f45fa839984e5e1e687ae22334ac543bc [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>
Asutosh Dasbbc84782013-02-11 15:31:35 +053020#include <linux/pm_runtime.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010021
22#include "core.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020023#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010024#include "mmc_ops.h"
Philip Rakity4c4cb172011-05-13 11:17:18 +053025#include "sd_ops.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010026
27static const unsigned int tran_exp[] = {
28 10000, 100000, 1000000, 10000000,
29 0, 0, 0, 0
30};
31
32static const unsigned char tran_mant[] = {
33 0, 10, 12, 13, 15, 20, 25, 30,
34 35, 40, 45, 50, 55, 60, 70, 80,
35};
36
37static const unsigned int tacc_exp[] = {
38 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
39};
40
41static const unsigned int tacc_mant[] = {
42 0, 10, 12, 13, 15, 20, 25, 30,
43 35, 40, 45, 50, 55, 60, 70, 80,
44};
45
46#define UNSTUFF_BITS(resp,start,size) \
47 ({ \
48 const int __size = size; \
49 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
50 const int __off = 3 - ((start) / 32); \
51 const int __shft = (start) & 31; \
52 u32 __res; \
53 \
54 __res = resp[__off] >> __shft; \
55 if (__size + __shft > 32) \
56 __res |= resp[__off-1] << ((32 - __shft) % 32); \
57 __res & __mask; \
58 })
59
60/*
61 * Given the decoded CSD structure, decode the raw CID to our CID structure.
62 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020063static int mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010064{
65 u32 *resp = card->raw_cid;
66
67 /*
68 * The selection of the format here is based upon published
69 * specs from sandisk and from what people have reported.
70 */
71 switch (card->csd.mmca_vsn) {
72 case 0: /* MMC v1.0 - v1.2 */
73 case 1: /* MMC v1.4 */
74 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
75 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
76 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
77 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
78 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
79 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
80 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
81 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
82 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
83 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
84 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
85 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
86 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
87 break;
88
89 case 2: /* MMC v2.0 - v2.2 */
90 case 3: /* MMC v3.1 - v3.3 */
91 case 4: /* MMC v4 */
92 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
93 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
94 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
95 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
96 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
97 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
98 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
99 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
100 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
101 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
102 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
103 break;
104
105 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530106 pr_err("%s: card has unknown MMCA version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100107 mmc_hostname(card->host), card->csd.mmca_vsn);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200108 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100109 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200110
111 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100112}
113
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700114static void mmc_set_erase_size(struct mmc_card *card)
115{
116 if (card->ext_csd.erase_group_def & 1)
117 card->erase_size = card->ext_csd.hc_erase_size;
118 else
119 card->erase_size = card->csd.erase_size;
120
121 mmc_init_erase(card);
122}
123
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100124/*
125 * Given a 128-bit response, decode to our card CSD structure.
126 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200127static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100128{
129 struct mmc_csd *csd = &card->csd;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700130 unsigned int e, m, a, b;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100131 u32 *resp = card->raw_csd;
132
133 /*
134 * We only understand CSD structure v1.1 and v1.2.
135 * v1.2 has extra information in bits 15, 11 and 10.
Kyungmin Park6da24b72010-08-10 18:01:36 -0700136 * We also support eMMC v4.4 & v4.41.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100137 */
Kyungmin Park6da24b72010-08-10 18:01:36 -0700138 csd->structure = UNSTUFF_BITS(resp, 126, 2);
139 if (csd->structure == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530140 pr_err("%s: unrecognised CSD structure version %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700141 mmc_hostname(card->host), csd->structure);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200142 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100143 }
144
145 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
146 m = UNSTUFF_BITS(resp, 115, 4);
147 e = UNSTUFF_BITS(resp, 112, 3);
148 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
149 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
150
151 m = UNSTUFF_BITS(resp, 99, 4);
152 e = UNSTUFF_BITS(resp, 96, 3);
153 csd->max_dtr = tran_exp[e] * tran_mant[m];
154 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
155
156 e = UNSTUFF_BITS(resp, 47, 3);
157 m = UNSTUFF_BITS(resp, 62, 12);
158 csd->capacity = (1 + m) << (e + 2);
159
160 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
161 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
162 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
163 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
164 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
165 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
166 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200167
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700168 if (csd->write_blkbits >= 9) {
169 a = UNSTUFF_BITS(resp, 42, 5);
170 b = UNSTUFF_BITS(resp, 37, 5);
171 csd->erase_size = (a + 1) * (b + 1);
172 csd->erase_size <<= csd->write_blkbits - 9;
173 }
174
Pierre Ossmanbd766312007-05-01 16:11:57 +0200175 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100176}
177
178/*
Philip Rakity08ee80c2011-05-24 18:14:58 -0700179 * Read extended CSD.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100180 */
Philip Rakity08ee80c2011-05-24 18:14:58 -0700181static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100182{
183 int err;
184 u8 *ext_csd;
185
186 BUG_ON(!card);
Philip Rakity08ee80c2011-05-24 18:14:58 -0700187 BUG_ON(!new_ext_csd);
188
189 *new_ext_csd = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100190
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100191 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
Pierre Ossman17b04292007-07-22 22:18:46 +0200192 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100193
194 /*
195 * As the ext_csd is so large and mostly unused, we don't store the
196 * raw block in mmc_card.
197 */
198 ext_csd = kmalloc(512, GFP_KERNEL);
199 if (!ext_csd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530200 pr_err("%s: could not allocate a buffer to "
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200201 "receive the ext_csd.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200202 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100203 }
204
205 err = mmc_send_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200206 if (err) {
Philip Rakity08ee80c2011-05-24 18:14:58 -0700207 kfree(ext_csd);
208 *new_ext_csd = NULL;
209
Wolfgang Mueesd08ebed2009-09-22 16:45:26 -0700210 /* If the host or the card can't do the switch,
211 * fail more gracefully. */
212 if ((err != -EINVAL)
213 && (err != -ENOSYS)
214 && (err != -EFAULT))
Philip Rakity08ee80c2011-05-24 18:14:58 -0700215 return err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200216
217 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100218 * High capacity cards should have this "magic" size
219 * stored in their CSD.
220 */
221 if (card->csd.capacity == (4096 * 512)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530222 pr_err("%s: unable to read EXT_CSD "
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100223 "on a possible high capacity card. "
224 "Card will be ignored.\n",
225 mmc_hostname(card->host));
226 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530227 pr_warning("%s: unable to read "
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100228 "EXT_CSD, performance might "
229 "suffer.\n",
230 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200231 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100232 }
Philip Rakity08ee80c2011-05-24 18:14:58 -0700233 } else
234 *new_ext_csd = ext_csd;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200235
Philip Rakity08ee80c2011-05-24 18:14:58 -0700236 return err;
237}
238
Seungwon Jeonfe201cd2012-04-25 16:17:37 +0900239static void mmc_select_card_type(struct mmc_card *card)
240{
241 struct mmc_host *host = card->host;
242 u8 card_type = card->ext_csd.raw_card_type & EXT_CSD_CARD_TYPE_MASK;
243 unsigned int caps = host->caps, caps2 = host->caps2;
244 unsigned int hs_max_dtr = 0;
245
246 if (card_type & EXT_CSD_CARD_TYPE_26)
247 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
248
249 if (caps & MMC_CAP_MMC_HIGHSPEED &&
250 card_type & EXT_CSD_CARD_TYPE_52)
251 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
252
253 if ((caps & MMC_CAP_1_8V_DDR &&
254 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) ||
255 (caps & MMC_CAP_1_2V_DDR &&
256 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V))
257 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
258
259 if ((caps2 & MMC_CAP2_HS200_1_8V_SDR &&
260 card_type & EXT_CSD_CARD_TYPE_SDR_1_8V) ||
261 (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
262 card_type & EXT_CSD_CARD_TYPE_SDR_1_2V))
263 hs_max_dtr = MMC_HS200_MAX_DTR;
264
265 card->ext_csd.hs_max_dtr = hs_max_dtr;
266 card->ext_csd.card_type = card_type;
267}
268
Philip Rakity08ee80c2011-05-24 18:14:58 -0700269/*
270 * Decode extended CSD.
271 */
272static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
273{
Namjae Jeone0c368d2011-10-06 23:41:38 +0900274 int err = 0, idx;
275 unsigned int part_size;
276 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700277
278 BUG_ON(!card);
279
280 if (!ext_csd)
281 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100282
Kyungmin Park6da24b72010-08-10 18:01:36 -0700283 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700284 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
Kyungmin Park6da24b72010-08-10 18:01:36 -0700285 if (card->csd.structure == 3) {
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700286 if (card->ext_csd.raw_ext_csd_structure > 2) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530287 pr_err("%s: unrecognised EXT_CSD structure "
Kyungmin Park6da24b72010-08-10 18:01:36 -0700288 "version %d\n", mmc_hostname(card->host),
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700289 card->ext_csd.raw_ext_csd_structure);
Kyungmin Park6da24b72010-08-10 18:01:36 -0700290 err = -EINVAL;
291 goto out;
292 }
293 }
294
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700295 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Subhash Jadavani7ca4a782013-02-01 11:09:58 +0530296 if (card->ext_csd.rev > 7) {
Stephen Boyd3f113aa2013-02-11 14:30:14 -0800297 pr_err("%s: unrecognised EXT_CSD revision %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700298 mmc_hostname(card->host), card->ext_csd.rev);
Florin Malita00cedfa2007-10-13 12:27:20 -0400299 err = -EINVAL;
300 goto out;
Pierre Ossmand7604d72007-07-23 00:34:07 +0200301 }
302
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700303 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
304 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
305 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
306 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700307 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200308 card->ext_csd.sectors =
309 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
310 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
311 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
312 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700313
314 /* Cards with density > 2GiB are sector addressed */
315 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200316 mmc_card_set_blockaddr(card);
317 }
Seungwon Jeonfe201cd2012-04-25 16:17:37 +0900318
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700319 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
Seungwon Jeonfe201cd2012-04-25 16:17:37 +0900320 mmc_select_card_type(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100321
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700322 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
323 card->ext_csd.raw_erase_timeout_mult =
324 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
325 card->ext_csd.raw_hc_erase_grp_size =
326 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700327 if (card->ext_csd.rev >= 3) {
328 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500329 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
330
331 /* EXT_CSD value is in units of 10ms, but we store in ms */
332 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700333
334 /* Sleep / awake timeout in 100ns units */
335 if (sa_shift > 0 && sa_shift <= 0x17)
336 card->ext_csd.sa_timeout =
337 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700338 card->ext_csd.erase_group_def =
339 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
340 card->ext_csd.hc_erase_timeout = 300 *
341 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
342 card->ext_csd.hc_erase_size =
343 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500344
345 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500346
347 /*
348 * There are two boot regions of equal size, defined in
349 * multiples of 128K.
350 */
Namjae Jeone0c368d2011-10-06 23:41:38 +0900351 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
352 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
353 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
354 mmc_part_add(card, part_size,
355 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100356 "boot%d", idx, true,
357 MMC_BLK_DATA_AREA_BOOT);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900358 }
359 }
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700360 }
361
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700362 card->ext_csd.raw_hc_erase_gap_size =
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100363 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700364 card->ext_csd.raw_sec_trim_mult =
365 ext_csd[EXT_CSD_SEC_TRIM_MULT];
366 card->ext_csd.raw_sec_erase_mult =
367 ext_csd[EXT_CSD_SEC_ERASE_MULT];
368 card->ext_csd.raw_sec_feature_support =
369 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
370 card->ext_csd.raw_trim_mult =
371 ext_csd[EXT_CSD_TRIM_MULT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700372 if (card->ext_csd.rev >= 4) {
Chuanxiao Dong709de992011-01-22 04:09:41 +0800373 /*
374 * Enhanced area feature support -- check whether the eMMC
375 * card has the Enhanced area enabled. If so, export enhanced
376 * area offset and size to user by adding sysfs interface.
377 */
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400378 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
Chuanxiao Dong709de992011-01-22 04:09:41 +0800379 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700380 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
Namjae Jeone0c368d2011-10-06 23:41:38 +0900381 hc_erase_grp_sz =
Chuanxiao Dong709de992011-01-22 04:09:41 +0800382 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Namjae Jeone0c368d2011-10-06 23:41:38 +0900383 hc_wp_grp_sz =
Chuanxiao Dong709de992011-01-22 04:09:41 +0800384 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
385
386 card->ext_csd.enhanced_area_en = 1;
387 /*
388 * calculate the enhanced data area offset, in bytes
389 */
390 card->ext_csd.enhanced_area_offset =
391 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
392 (ext_csd[137] << 8) + ext_csd[136];
393 if (mmc_card_blockaddr(card))
394 card->ext_csd.enhanced_area_offset <<= 9;
395 /*
396 * calculate the enhanced data area size, in kilobytes
397 */
398 card->ext_csd.enhanced_area_size =
399 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
400 ext_csd[140];
401 card->ext_csd.enhanced_area_size *=
402 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
403 card->ext_csd.enhanced_area_size <<= 9;
404 } else {
405 /*
406 * If the enhanced area is not enabled, disable these
407 * device attributes.
408 */
409 card->ext_csd.enhanced_area_offset = -EINVAL;
410 card->ext_csd.enhanced_area_size = -EINVAL;
411 }
Namjae Jeone0c368d2011-10-06 23:41:38 +0900412
413 /*
414 * General purpose partition feature support --
415 * If ext_csd has the size of general purpose partitions,
416 * set size, part_cfg, partition name in mmc_part.
417 */
418 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
419 EXT_CSD_PART_SUPPORT_PART_EN) {
420 if (card->ext_csd.enhanced_area_en != 1) {
421 hc_erase_grp_sz =
422 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
423 hc_wp_grp_sz =
424 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
425
426 card->ext_csd.enhanced_area_en = 1;
427 }
428
429 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
430 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
431 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
432 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
433 continue;
434 part_size =
435 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
436 << 16) +
437 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
438 << 8) +
439 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
440 part_size *= (size_t)(hc_erase_grp_sz *
441 hc_wp_grp_sz);
442 mmc_part_add(card, part_size << 19,
443 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100444 "gp%d", idx, false,
445 MMC_BLK_DATA_AREA_GP);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900446 }
447 }
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700448 card->ext_csd.sec_trim_mult =
449 ext_csd[EXT_CSD_SEC_TRIM_MULT];
450 card->ext_csd.sec_erase_mult =
451 ext_csd[EXT_CSD_SEC_ERASE_MULT];
452 card->ext_csd.sec_feature_support =
453 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
454 card->ext_csd.trim_timeout = 300 *
455 ext_csd[EXT_CSD_TRIM_MULT];
Johan Rudholmadd710e2011-12-02 08:51:06 +0100456
457 /*
458 * Note that the call to mmc_part_add above defaults to read
459 * only. If this default assumption is changed, the call must
460 * take into account the value of boot_locked below.
461 */
462 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
463 card->ext_csd.boot_ro_lockable = true;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700464 }
465
Adrian Hunterb2499512011-08-29 16:42:11 +0300466 if (card->ext_csd.rev >= 5) {
Jaehoon Chungc1a56a12012-09-24 14:44:45 +0200467 /* check whether the eMMC card supports BKOPS */
468 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
469 card->ext_csd.bkops = 1;
470 card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
471 card->ext_csd.raw_bkops_status =
472 ext_csd[EXT_CSD_BKOPS_STATUS];
Maya Ereze966c1c2012-09-29 06:30:47 +0200473 if (!card->ext_csd.bkops_en &&
474 card->host->caps2 & MMC_CAP2_INIT_BKOPS) {
475 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
476 EXT_CSD_BKOPS_EN, 1, 0);
477 if (err)
478 pr_warn("%s: Enabling BKOPS failed\n",
479 mmc_hostname(card->host));
480 else
481 card->ext_csd.bkops_en = 1;
482 }
Jaehoon Chungc1a56a12012-09-24 14:44:45 +0200483 }
484
Yaniv Gardib58afd62012-11-06 21:06:19 +0200485 pr_info("%s: BKOPS_EN bit = %d\n",
486 mmc_hostname(card->host), card->ext_csd.bkops_en);
487
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400488 /* check whether the eMMC card supports HPI */
489 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
490 card->ext_csd.hpi = 1;
491 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
492 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
493 else
494 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
495 /*
496 * Indicate the maximum timeout to close
497 * a command interrupted by HPI
498 */
499 card->ext_csd.out_of_int_time =
500 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
501 }
502
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500503 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
Adrian Hunterb2499512011-08-29 16:42:11 +0300504 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
Loic Pallardye12f9ba2012-08-06 15:12:27 +0000505
506 /*
507 * RPMB regions are defined in multiples of 128K.
508 */
509 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
510 if (ext_csd[EXT_CSD_RPMB_MULT]) {
511 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
512 EXT_CSD_PART_CONFIG_ACC_RPMB,
513 "rpmb", 0, false,
514 MMC_BLK_DATA_AREA_RPMB);
515 }
Adrian Hunterb2499512011-08-29 16:42:11 +0300516 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500517
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400518 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700519 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
520 card->erased_byte = 0xFF;
521 else
522 card->erased_byte = 0x0;
523
Seungwon Jeon336c7162011-10-25 09:43:12 +0900524 /* eMMC v4.5 or later */
Girish K Sbec87262011-10-13 12:04:16 +0530525 if (card->ext_csd.rev >= 6) {
Seungwon Jeon336c7162011-10-25 09:43:12 +0900526 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
527
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900528 card->ext_csd.generic_cmd6_time = 10 *
529 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
Girish K Sbec87262011-10-13 12:04:16 +0530530 card->ext_csd.power_off_longtime = 10 *
531 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900532
Seungwon Jeon336c7162011-10-25 09:43:12 +0900533 card->ext_csd.cache_size =
534 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
535 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
536 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
537 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
Saugata Das42659002011-12-21 13:09:17 +0530538
539 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
540 card->ext_csd.data_sector_size = 4096;
541 else
542 card->ext_csd.data_sector_size = 512;
543
544 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
545 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
546 card->ext_csd.data_tag_unit_size =
547 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
548 (card->ext_csd.data_sector_size);
549 } else {
550 card->ext_csd.data_tag_unit_size = 0;
551 }
Seungwon Jeonc70e9662012-09-27 16:12:47 +0200552
553 card->ext_csd.max_packed_writes =
554 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
555 card->ext_csd.max_packed_reads =
556 ext_csd[EXT_CSD_MAX_PACKED_READS];
Seungwon Jeon336c7162011-10-25 09:43:12 +0900557 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900558
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100559out:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700560 return err;
561}
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100562
Philip Rakity08ee80c2011-05-24 18:14:58 -0700563static inline void mmc_free_ext_csd(u8 *ext_csd)
564{
565 kfree(ext_csd);
566}
567
568
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700569static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700570{
571 u8 *bw_ext_csd;
572 int err;
573
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700574 if (bus_width == MMC_BUS_WIDTH_1)
575 return 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700576
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700577 err = mmc_get_ext_csd(card, &bw_ext_csd);
578
579 if (err || bw_ext_csd == NULL) {
Philip Rakity08ee80c2011-05-24 18:14:58 -0700580 if (bus_width != MMC_BUS_WIDTH_1)
581 err = -EINVAL;
582 goto out;
583 }
584
585 if (bus_width == MMC_BUS_WIDTH_1)
586 goto out;
587
588 /* only compare read only fields */
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100589 err = !((card->ext_csd.raw_partition_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700590 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700591 (card->ext_csd.raw_erased_mem_count ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700592 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700593 (card->ext_csd.rev ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700594 bw_ext_csd[EXT_CSD_REV]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700595 (card->ext_csd.raw_ext_csd_structure ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700596 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700597 (card->ext_csd.raw_card_type ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700598 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700599 (card->ext_csd.raw_s_a_timeout ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700600 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700601 (card->ext_csd.raw_hc_erase_gap_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700602 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700603 (card->ext_csd.raw_erase_timeout_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700604 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700605 (card->ext_csd.raw_hc_erase_grp_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700606 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700607 (card->ext_csd.raw_sec_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700608 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700609 (card->ext_csd.raw_sec_erase_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700610 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700611 (card->ext_csd.raw_sec_feature_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700612 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700613 (card->ext_csd.raw_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700614 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700615 (card->ext_csd.raw_sectors[0] ==
616 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
617 (card->ext_csd.raw_sectors[1] ==
618 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
619 (card->ext_csd.raw_sectors[2] ==
620 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
621 (card->ext_csd.raw_sectors[3] ==
622 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
Philip Rakity08ee80c2011-05-24 18:14:58 -0700623 if (err)
624 err = -EINVAL;
625
626out:
627 mmc_free_ext_csd(bw_ext_csd);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100628 return err;
629}
630
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100631MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
632 card->raw_cid[2], card->raw_cid[3]);
633MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
634 card->raw_csd[2], card->raw_csd[3]);
635MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700636MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
637MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100638MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
639MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
640MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
641MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
642MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
643MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Chuanxiao Dong709de992011-01-22 04:09:41 +0800644MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
645 card->ext_csd.enhanced_area_offset);
646MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
Loic Pallardy762a87b2012-08-06 15:12:29 +0000647MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
648MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100649
650static struct attribute *mmc_std_attrs[] = {
651 &dev_attr_cid.attr,
652 &dev_attr_csd.attr,
653 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700654 &dev_attr_erase_size.attr,
655 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100656 &dev_attr_fwrev.attr,
657 &dev_attr_hwrev.attr,
658 &dev_attr_manfid.attr,
659 &dev_attr_name.attr,
660 &dev_attr_oemid.attr,
661 &dev_attr_serial.attr,
Chuanxiao Dong709de992011-01-22 04:09:41 +0800662 &dev_attr_enhanced_area_offset.attr,
663 &dev_attr_enhanced_area_size.attr,
Loic Pallardy762a87b2012-08-06 15:12:29 +0000664 &dev_attr_raw_rpmb_size_mult.attr,
665 &dev_attr_rel_sectors.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100666 NULL,
667};
668
669static struct attribute_group mmc_std_attr_group = {
670 .attrs = mmc_std_attrs,
671};
672
David Brownella4dbd672009-06-24 10:06:31 -0700673static const struct attribute_group *mmc_attr_groups[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100674 &mmc_std_attr_group,
675 NULL,
676};
677
678static struct device_type mmc_type = {
679 .groups = mmc_attr_groups,
680};
681
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100682/*
Girish K Sb87d8db2011-09-23 20:41:47 +0530683 * Select the PowerClass for the current bus width
684 * If power class is defined for 4/8 bit bus in the
685 * extended CSD register, select it by executing the
686 * mmc_switch command.
687 */
688static int mmc_select_powerclass(struct mmc_card *card,
689 unsigned int bus_width, u8 *ext_csd)
690{
691 int err = 0;
692 unsigned int pwrclass_val;
693 unsigned int index = 0;
694 struct mmc_host *host;
695
696 BUG_ON(!card);
697
698 host = card->host;
699 BUG_ON(!host);
700
701 if (ext_csd == NULL)
702 return 0;
703
704 /* Power class selection is supported for versions >= 4.0 */
705 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
706 return 0;
707
708 /* Power class values are defined only for 4/8 bit bus */
709 if (bus_width == EXT_CSD_BUS_WIDTH_1)
710 return 0;
711
712 switch (1 << host->ios.vdd) {
713 case MMC_VDD_165_195:
714 if (host->ios.clock <= 26000000)
715 index = EXT_CSD_PWR_CL_26_195;
716 else if (host->ios.clock <= 52000000)
717 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
718 EXT_CSD_PWR_CL_52_195 :
719 EXT_CSD_PWR_CL_DDR_52_195;
720 else if (host->ios.clock <= 200000000)
721 index = EXT_CSD_PWR_CL_200_195;
722 break;
Subhash Jadavani93fc5a42012-04-03 12:25:58 +0530723 case MMC_VDD_27_28:
724 case MMC_VDD_28_29:
725 case MMC_VDD_29_30:
726 case MMC_VDD_30_31:
727 case MMC_VDD_31_32:
Girish K Sb87d8db2011-09-23 20:41:47 +0530728 case MMC_VDD_32_33:
729 case MMC_VDD_33_34:
730 case MMC_VDD_34_35:
731 case MMC_VDD_35_36:
732 if (host->ios.clock <= 26000000)
733 index = EXT_CSD_PWR_CL_26_360;
734 else if (host->ios.clock <= 52000000)
735 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
736 EXT_CSD_PWR_CL_52_360 :
737 EXT_CSD_PWR_CL_DDR_52_360;
738 else if (host->ios.clock <= 200000000)
739 index = EXT_CSD_PWR_CL_200_360;
740 break;
741 default:
742 pr_warning("%s: Voltage range not supported "
743 "for power class.\n", mmc_hostname(host));
744 return -EINVAL;
745 }
746
747 pwrclass_val = ext_csd[index];
748
749 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
750 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
751 EXT_CSD_PWR_CL_8BIT_SHIFT;
752 else
753 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
754 EXT_CSD_PWR_CL_4BIT_SHIFT;
755
756 /* If the power class is different from the default value */
757 if (pwrclass_val > 0) {
758 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
759 EXT_CSD_POWER_CLASS,
760 pwrclass_val,
Seungwon Jeon71fe3eb2011-10-18 13:20:57 +0900761 card->ext_csd.generic_cmd6_time);
Girish K Sb87d8db2011-09-23 20:41:47 +0530762 }
763
764 return err;
765}
766
767/*
Girish K Sa4924c72012-01-11 14:04:52 -0500768 * Selects the desired buswidth and switch to the HS200 mode
769 * if bus width set without error
770 */
771static int mmc_select_hs200(struct mmc_card *card)
772{
773 int idx, err = 0;
774 struct mmc_host *host;
775 static unsigned ext_csd_bits[] = {
776 EXT_CSD_BUS_WIDTH_4,
777 EXT_CSD_BUS_WIDTH_8,
778 };
779 static unsigned bus_widths[] = {
780 MMC_BUS_WIDTH_4,
781 MMC_BUS_WIDTH_8,
782 };
783
784 BUG_ON(!card);
785
786 host = card->host;
787
788 if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V &&
789 host->caps2 & MMC_CAP2_HS200_1_2V_SDR)
790 if (mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120, 0))
791 err = mmc_set_signal_voltage(host,
792 MMC_SIGNAL_VOLTAGE_180, 0);
793
794 /* If fails try again during next card power cycle */
795 if (err)
796 goto err;
797
798 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0;
799
800 /*
801 * Unlike SD, MMC cards dont have a configuration register to notify
802 * supported bus width. So bus test command should be run to identify
803 * the supported bus width or compare the ext csd values of current
804 * bus width and ext csd values of 1 bit mode read earlier.
805 */
806 for (; idx >= 0; idx--) {
807
808 /*
809 * Host is capable of 8bit transfer, then switch
810 * the device to work in 8bit transfer mode. If the
811 * mmc switch command returns error then switch to
812 * 4bit transfer mode. On success set the corresponding
813 * bus width on the host.
814 */
815 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
816 EXT_CSD_BUS_WIDTH,
817 ext_csd_bits[idx],
818 card->ext_csd.generic_cmd6_time);
819 if (err)
820 continue;
821
822 mmc_set_bus_width(card->host, bus_widths[idx]);
823
824 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
825 err = mmc_compare_ext_csds(card, bus_widths[idx]);
826 else
827 err = mmc_bus_test(card, bus_widths[idx]);
828 if (!err)
829 break;
830 }
831
832 /* switch to HS200 mode if bus width set successfully */
833 if (!err)
834 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
835 EXT_CSD_HS_TIMING, 2, 0);
836err:
837 return err;
838}
839
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +0530840/**
841 * mmc_change_bus_speed() - Change MMC card bus frequency at runtime
842 * @host: pointer to mmc host structure
843 * @freq: pointer to desired frequency to be set
844 *
845 * Change the MMC card bus frequency at runtime after the card is
846 * initialized. Callers are expected to make sure of the card's
847 * state (DATA/RCV/TRANSFER) beforing changing the frequency at runtime.
848 *
849 * If the frequency to change is greater than max. supported by card,
850 * *freq is changed to max. supported by card and if it is less than min.
851 * supported by host, *freq is changed to min. supported by host.
852 */
853static int mmc_change_bus_speed(struct mmc_host *host, unsigned long *freq)
854{
855 int err = 0;
856 struct mmc_card *card;
857
858 mmc_claim_host(host);
859 /*
860 * Assign card pointer after claiming host to avoid race
861 * conditions that may arise during removal of the card.
862 */
863 card = host->card;
864
865 if (!card || !freq) {
866 err = -EINVAL;
867 goto out;
868 }
869
870 if (mmc_card_highspeed(card) || mmc_card_hs200(card)
871 || mmc_card_ddr_mode(card)) {
872 if (*freq > card->ext_csd.hs_max_dtr)
873 *freq = card->ext_csd.hs_max_dtr;
874 } else if (*freq > card->csd.max_dtr) {
875 *freq = card->csd.max_dtr;
876 }
877
878 if (*freq < host->f_min)
879 *freq = host->f_min;
880
881 mmc_set_clock(host, (unsigned int) (*freq));
882
883 if (mmc_card_hs200(card) && card->host->ops->execute_tuning) {
884 /*
885 * We try to probe host driver for tuning for any
886 * frequency, it is host driver responsibility to
887 * perform actual tuning only when required.
888 */
889 mmc_host_clk_hold(card->host);
890 err = card->host->ops->execute_tuning(card->host,
891 MMC_SEND_TUNING_BLOCK_HS200);
892 mmc_host_clk_release(card->host);
893
894 if (err)
895 pr_warn("%s: %s: tuning execution failed %d\n",
896 mmc_hostname(card->host), __func__, err);
897 }
898out:
899 mmc_release_host(host);
900 return err;
901}
902
Girish K Sa4924c72012-01-11 14:04:52 -0500903/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200904 * Handle the detection and initialisation of a card.
905 *
Deepak Saxena87693922008-06-16 19:20:57 -0700906 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200907 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100908 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +0200909static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200910 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100911{
912 struct mmc_card *card;
Philip Rakity25d5c692010-11-07 16:22:28 -0500913 int err, ddr = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100914 u32 cid[4];
915 unsigned int max_dtr;
Philip Rakityb676f032011-02-13 23:13:09 -0800916 u32 rocr;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700917 u8 *ext_csd = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100918
919 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200920 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100921
Stefan Nilsson XK44669032011-09-15 17:50:38 +0200922 /* Set correct bus mode for MMC before attempting init */
923 if (!mmc_host_is_spi(host))
924 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
925
Ulf Hanssone7747472012-03-01 13:18:05 +0100926 /* Initialization should be done at 3.3 V I/O voltage. */
927 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
928
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100929 /*
930 * Since we're changing the OCR value, we seem to
931 * need to tell some cards to go back to the idle
932 * state. We wait 1ms to give cards time to
933 * respond.
Balaji T Kc3805462011-09-08 22:08:39 +0530934 * mmc_go_idle is needed for eMMC that are asleep
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100935 */
936 mmc_go_idle(host);
937
938 /* The extra bit indicates that we support high capacity */
Philip Rakityb676f032011-02-13 23:13:09 -0800939 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200940 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200941 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100942
943 /*
David Brownellaf517152007-08-08 09:11:32 -0700944 * For SPI, enable CRC as appropriate.
945 */
946 if (mmc_host_is_spi(host)) {
947 err = mmc_spi_set_crc(host, use_spi_crc);
948 if (err)
949 goto err;
950 }
951
952 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100953 * Fetch CID from card.
954 */
David Brownellaf517152007-08-08 09:11:32 -0700955 if (mmc_host_is_spi(host))
956 err = mmc_send_cid(host, cid);
957 else
958 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +0200959 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100960 goto err;
961
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200962 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200963 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
964 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200965 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200966 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100967
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200968 card = oldcard;
969 } else {
970 /*
971 * Allocate card structure.
972 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100973 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200974 if (IS_ERR(card)) {
975 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200976 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200977 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200978
979 card->type = MMC_TYPE_MMC;
980 card->rca = 1;
981 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
982 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100983
984 /*
David Brownellaf517152007-08-08 09:11:32 -0700985 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100986 */
David Brownellaf517152007-08-08 09:11:32 -0700987 if (!mmc_host_is_spi(host)) {
988 err = mmc_set_relative_addr(card);
989 if (err)
990 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100991
David Brownellaf517152007-08-08 09:11:32 -0700992 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
993 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100994
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200995 if (!oldcard) {
996 /*
997 * Fetch CSD from card.
998 */
999 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001000 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001001 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001002
Pierre Ossmanbd766312007-05-01 16:11:57 +02001003 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001004 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001005 goto free_card;
1006 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001007 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001008 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001009 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001010
1011 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001012 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001013 */
David Brownellaf517152007-08-08 09:11:32 -07001014 if (!mmc_host_is_spi(host)) {
1015 err = mmc_select_card(card);
1016 if (err)
1017 goto free_card;
1018 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001019
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001020 if (!oldcard) {
1021 /*
David Brownellaf517152007-08-08 09:11:32 -07001022 * Fetch and process extended CSD.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001023 */
Philip Rakity08ee80c2011-05-24 18:14:58 -07001024
1025 err = mmc_get_ext_csd(card, &ext_csd);
1026 if (err)
1027 goto free_card;
1028 err = mmc_read_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001029 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001030 goto free_card;
Philip Rakityb676f032011-02-13 23:13:09 -08001031
1032 /* If doing byte addressing, check if required to do sector
1033 * addressing. Handle the case of <2GB cards needing sector
1034 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1035 * ocr register has bit 30 set for sector addressing.
1036 */
1037 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
1038 mmc_card_set_blockaddr(card);
1039
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001040 /* Erase size depends on CSD and Extended CSD */
1041 mmc_set_erase_size(card);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042
1043 if (card->ext_csd.sectors && (rocr & MMC_CARD_SECTOR_ADDR))
1044 mmc_card_set_blockaddr(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001045 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001046
1047 /*
Chuanxiao Dong709de992011-01-22 04:09:41 +08001048 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001049 * bit. This bit will be lost every time after a reset or power off.
Chuanxiao Dong709de992011-01-22 04:09:41 +08001050 */
Adrian Hunter83bb24a2012-03-13 05:19:13 -04001051 if (card->ext_csd.enhanced_area_en ||
1052 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
Chuanxiao Dong709de992011-01-22 04:09:41 +08001053 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001054 EXT_CSD_ERASE_GROUP_DEF, 1,
1055 card->ext_csd.generic_cmd6_time);
Chuanxiao Dong709de992011-01-22 04:09:41 +08001056
1057 if (err && err != -EBADMSG)
1058 goto free_card;
1059
1060 if (err) {
1061 err = 0;
1062 /*
1063 * Just disable enhanced area off & sz
1064 * will try to enable ERASE_GROUP_DEF
1065 * during next time reinit
1066 */
1067 card->ext_csd.enhanced_area_offset = -EINVAL;
1068 card->ext_csd.enhanced_area_size = -EINVAL;
1069 } else {
1070 card->ext_csd.erase_group_def = 1;
1071 /*
1072 * enable ERASE_GRP_DEF successfully.
1073 * This will affect the erase size, so
1074 * here need to reset erase size
1075 */
1076 mmc_set_erase_size(card);
1077 }
1078 }
1079
1080 /*
Philip Rakity41e2a482011-03-19 14:10:33 -04001081 * Ensure eMMC user default partition is enabled
1082 */
Andrei Warkentin371a6892011-04-11 18:10:25 -05001083 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1084 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1085 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1086 card->ext_csd.part_config,
1087 card->ext_csd.part_time);
1088 if (err && err != -EBADMSG)
1089 goto free_card;
Oluwafemi Adeyemi151b4772013-01-03 11:32:53 -08001090 card->part_curr = card->ext_csd.part_config &
1091 EXT_CSD_PART_CONFIG_ACC_MASK;
Philip Rakity41e2a482011-03-19 14:10:33 -04001092 }
1093
1094 /*
Girish K Sbec87262011-10-13 12:04:16 +05301095 * If the host supports the power_off_notify capability then
1096 * set the notification byte in the ext_csd register of device
1097 */
1098 if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) &&
Girish K Sa80f1622011-11-15 11:55:46 +05301099 (card->ext_csd.rev >= 6)) {
Girish K Sbec87262011-10-13 12:04:16 +05301100 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1101 EXT_CSD_POWER_OFF_NOTIFICATION,
1102 EXT_CSD_POWER_ON,
1103 card->ext_csd.generic_cmd6_time);
1104 if (err && err != -EBADMSG)
1105 goto free_card;
Girish K Sbec87262011-10-13 12:04:16 +05301106
Girish K S96a85d52011-11-04 16:22:47 +05301107 /*
1108 * The err can be -EBADMSG or 0,
1109 * so check for success and update the flag
1110 */
1111 if (!err)
Maya Erezcec02a42012-10-19 20:18:17 +02001112 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
Girish K S96a85d52011-11-04 16:22:47 +05301113 }
Girish K Sbec87262011-10-13 12:04:16 +05301114
1115 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001116 * Activate high speed (if supported)
1117 */
Girish K Sa4924c72012-01-11 14:04:52 -05001118 if (card->ext_csd.hs_max_dtr != 0) {
1119 err = 0;
1120 if (card->ext_csd.hs_max_dtr > 52000000 &&
1121 host->caps2 & MMC_CAP2_HS200)
1122 err = mmc_select_hs200(card);
1123 else if (host->caps & MMC_CAP_MMC_HIGHSPEED)
1124 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Girish K S7488e922012-01-19 08:56:19 +05301125 EXT_CSD_HS_TIMING, 1,
1126 card->ext_csd.generic_cmd6_time);
Girish K Sa4924c72012-01-11 14:04:52 -05001127
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001128 if (err && err != -EBADMSG)
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001129 goto free_card;
1130
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001131 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301132 pr_warning("%s: switch to highspeed failed\n",
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001133 mmc_hostname(card->host));
1134 err = 0;
1135 } else {
Girish K Sa4924c72012-01-11 14:04:52 -05001136 if (card->ext_csd.hs_max_dtr > 52000000 &&
1137 host->caps2 & MMC_CAP2_HS200) {
1138 mmc_card_set_hs200(card);
1139 mmc_set_timing(card->host,
1140 MMC_TIMING_MMC_HS200);
1141 } else {
1142 mmc_card_set_highspeed(card);
1143 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1144 }
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001145 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001146 }
1147
1148 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001149 * Compute bus speed.
1150 */
1151 max_dtr = (unsigned int)-1;
1152
Girish K Sa4924c72012-01-11 14:04:52 -05001153 if (mmc_card_highspeed(card) || mmc_card_hs200(card)) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001154 if (max_dtr > card->ext_csd.hs_max_dtr)
1155 max_dtr = card->ext_csd.hs_max_dtr;
1156 } else if (max_dtr > card->csd.max_dtr) {
1157 max_dtr = card->csd.max_dtr;
1158 }
1159
1160 mmc_set_clock(host, max_dtr);
1161
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001162 /*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001163 * Indicate DDR mode (if supported).
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001164 */
1165 if (mmc_card_highspeed(card)) {
1166 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
Philip Rakity4c4cb172011-05-13 11:17:18 +05301167 && ((host->caps & (MMC_CAP_1_8V_DDR |
1168 MMC_CAP_UHS_DDR50))
1169 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
Adrian Hunter49e3b5a2010-10-11 12:43:50 +03001170 ddr = MMC_1_8V_DDR_MODE;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001171 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
Philip Rakity4c4cb172011-05-13 11:17:18 +05301172 && ((host->caps & (MMC_CAP_1_2V_DDR |
1173 MMC_CAP_UHS_DDR50))
1174 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
Adrian Hunter49e3b5a2010-10-11 12:43:50 +03001175 ddr = MMC_1_2V_DDR_MODE;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001176 }
1177
1178 /*
Girish K Sa4924c72012-01-11 14:04:52 -05001179 * Indicate HS200 SDR mode (if supported).
1180 */
1181 if (mmc_card_hs200(card)) {
1182 u32 ext_csd_bits;
1183 u32 bus_width = card->host->ios.bus_width;
1184
1185 /*
1186 * For devices supporting HS200 mode, the bus width has
1187 * to be set before executing the tuning function. If
1188 * set before tuning, then device will respond with CRC
1189 * errors for responses on CMD line. So for HS200 the
1190 * sequence will be
1191 * 1. set bus width 4bit / 8 bit (1 bit not supported)
1192 * 2. switch to HS200 mode
1193 * 3. set the clock to > 52Mhz <=200MHz and
1194 * 4. execute tuning for HS200
1195 */
1196 if ((host->caps2 & MMC_CAP2_HS200) &&
Subhash Jadavani52d09742012-03-06 17:59:12 +05301197 card->host->ops->execute_tuning) {
1198 mmc_host_clk_hold(card->host);
Girish K Sa4924c72012-01-11 14:04:52 -05001199 err = card->host->ops->execute_tuning(card->host,
1200 MMC_SEND_TUNING_BLOCK_HS200);
Subhash Jadavani52d09742012-03-06 17:59:12 +05301201 mmc_host_clk_release(card->host);
1202 }
Girish K Sa4924c72012-01-11 14:04:52 -05001203 if (err) {
1204 pr_warning("%s: tuning execution failed\n",
1205 mmc_hostname(card->host));
1206 goto err;
1207 }
1208
1209 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1210 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
1211 err = mmc_select_powerclass(card, ext_csd_bits, ext_csd);
Subhash Jadavani93fc5a42012-04-03 12:25:58 +05301212 if (err)
1213 pr_warning("%s: power class selection to bus width %d"
1214 " failed\n", mmc_hostname(card->host),
1215 1 << bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -05001216 }
1217
1218 /*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001219 * Activate wide bus and DDR (if supported).
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001220 */
Girish K Sa4924c72012-01-11 14:04:52 -05001221 if (!mmc_card_hs200(card) &&
Girish K S7488e922012-01-19 08:56:19 +05301222 (card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001223 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
Aries Lee22113ef2010-12-15 08:14:24 +01001224 static unsigned ext_csd_bits[][2] = {
1225 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
1226 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
1227 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
1228 };
1229 static unsigned bus_widths[] = {
1230 MMC_BUS_WIDTH_8,
1231 MMC_BUS_WIDTH_4,
1232 MMC_BUS_WIDTH_1
1233 };
1234 unsigned idx, bus_width = 0;
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001235
Aries Lee22113ef2010-12-15 08:14:24 +01001236 if (host->caps & MMC_CAP_8_BIT_DATA)
1237 idx = 0;
1238 else
1239 idx = 1;
1240 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1241 bus_width = bus_widths[idx];
1242 if (bus_width == MMC_BUS_WIDTH_1)
1243 ddr = 0; /* no DDR for 1-bit width */
Girish K Sb87d8db2011-09-23 20:41:47 +05301244 err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
1245 ext_csd);
1246 if (err)
Subhash Jadavani93fc5a42012-04-03 12:25:58 +05301247 pr_warning("%s: power class selection to "
1248 "bus width %d failed\n",
1249 mmc_hostname(card->host),
1250 1 << bus_width);
Girish K Sb87d8db2011-09-23 20:41:47 +05301251
Aries Lee22113ef2010-12-15 08:14:24 +01001252 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1253 EXT_CSD_BUS_WIDTH,
Andrei Warkentind3a8d952011-04-11 16:13:43 -05001254 ext_csd_bits[idx][0],
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001255 card->ext_csd.generic_cmd6_time);
Aries Lee22113ef2010-12-15 08:14:24 +01001256 if (!err) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301257 mmc_set_bus_width(card->host, bus_width);
Philip Rakity08ee80c2011-05-24 18:14:58 -07001258
Aries Lee22113ef2010-12-15 08:14:24 +01001259 /*
1260 * If controller can't handle bus width test,
Philip Rakity08ee80c2011-05-24 18:14:58 -07001261 * compare ext_csd previously read in 1 bit mode
1262 * against ext_csd at new bus width
Aries Lee22113ef2010-12-15 08:14:24 +01001263 */
1264 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
Philip Rakity08ee80c2011-05-24 18:14:58 -07001265 err = mmc_compare_ext_csds(card,
Philip Rakity08ee80c2011-05-24 18:14:58 -07001266 bus_width);
1267 else
1268 err = mmc_bus_test(card, bus_width);
Aries Lee22113ef2010-12-15 08:14:24 +01001269 if (!err)
1270 break;
1271 }
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001272 }
1273
Aries Lee22113ef2010-12-15 08:14:24 +01001274 if (!err && ddr) {
Girish K Sb87d8db2011-09-23 20:41:47 +05301275 err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
1276 ext_csd);
1277 if (err)
Subhash Jadavani93fc5a42012-04-03 12:25:58 +05301278 pr_warning("%s: power class selection to "
1279 "bus width %d ddr %d failed\n",
1280 mmc_hostname(card->host),
1281 1 << bus_width, ddr);
Girish K Sb87d8db2011-09-23 20:41:47 +05301282
Aries Lee22113ef2010-12-15 08:14:24 +01001283 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Andrei Warkentind3a8d952011-04-11 16:13:43 -05001284 EXT_CSD_BUS_WIDTH,
1285 ext_csd_bits[idx][1],
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001286 card->ext_csd.generic_cmd6_time);
Aries Lee22113ef2010-12-15 08:14:24 +01001287 }
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001288 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301289 pr_warning("%s: switch to bus width %d ddr %d "
Aries Lee22113ef2010-12-15 08:14:24 +01001290 "failed\n", mmc_hostname(card->host),
1291 1 << bus_width, ddr);
1292 goto free_card;
1293 } else if (ddr) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301294 /*
1295 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1296 * signaling.
1297 *
1298 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1299 *
1300 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1301 * in the JEDEC spec for DDR.
1302 *
1303 * Do not force change in vccq since we are obviously
1304 * working and no change to vccq is needed.
1305 *
1306 * WARNING: eMMC rules are NOT the same as SD DDR
1307 */
Girish K S913047e2011-12-15 17:27:42 +05301308 if (ddr == MMC_1_2V_DDR_MODE) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301309 err = mmc_set_signal_voltage(host,
1310 MMC_SIGNAL_VOLTAGE_120, 0);
1311 if (err)
1312 goto err;
1313 }
Aries Lee22113ef2010-12-15 08:14:24 +01001314 mmc_card_set_ddr_mode(card);
Philip Rakity4c4cb172011-05-13 11:17:18 +05301315 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1316 mmc_set_bus_width(card->host, bus_width);
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001317 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001318 }
1319
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001320 /*
Subhash Jadavani52d09742012-03-06 17:59:12 +05301321 * Enable HPI feature (if supported)
1322 */
1323 if (card->ext_csd.hpi) {
1324 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1325 EXT_CSD_HPI_MGMT, 1,
1326 card->ext_csd.generic_cmd6_time);
1327 if (err && err != -EBADMSG)
1328 goto free_card;
1329 if (err) {
1330 pr_warning("%s: Enabling HPI failed\n",
1331 mmc_hostname(card->host));
1332 err = 0;
1333 } else
1334 card->ext_csd.hpi_en = 1;
1335 }
1336
1337 /*
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001338 * If cache size is higher than 0, this indicates
1339 * the existence of cache and it can be turned on.
1340 */
1341 if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1342 card->ext_csd.cache_size > 0) {
1343 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001344 EXT_CSD_CACHE_CTRL, 1,
1345 card->ext_csd.generic_cmd6_time);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001346 if (err && err != -EBADMSG)
1347 goto free_card;
1348
1349 /*
1350 * Only if no error, cache is turned on successfully.
1351 */
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001352 if (err) {
1353 pr_warning("%s: Cache is supported, "
1354 "but failed to turn on (%d)\n",
1355 mmc_hostname(card->host), err);
1356 card->ext_csd.cache_ctrl = 0;
1357 err = 0;
1358 } else {
1359 card->ext_csd.cache_ctrl = 1;
1360 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001361 }
1362
Seungwon Jeonc70e9662012-09-27 16:12:47 +02001363 if ((host->caps2 & MMC_CAP2_PACKED_WR &&
1364 card->ext_csd.max_packed_writes > 0) ||
1365 (host->caps2 & MMC_CAP2_PACKED_RD &&
1366 card->ext_csd.max_packed_reads > 0)) {
1367 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1368 EXT_CSD_EXP_EVENTS_CTRL,
1369 EXT_CSD_PACKED_EVENT_EN,
1370 card->ext_csd.generic_cmd6_time);
1371 if (err && err != -EBADMSG)
1372 goto free_card;
1373 if (err) {
1374 pr_warning("%s: Enabling packed event failed\n",
1375 mmc_hostname(card->host));
1376 card->ext_csd.packed_event_en = 0;
1377 err = 0;
1378 } else {
1379 card->ext_csd.packed_event_en = 1;
1380 }
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001381
1382 }
1383
1384 if (!oldcard) {
1385 if ((host->caps2 & MMC_CAP2_PACKED_CMD) &&
1386 (card->ext_csd.max_packed_writes > 0)) {
1387 /*
1388 * We would like to keep the statistics in an index
1389 * that equals the num of packed requests
1390 * (1 to max_packed_writes)
1391 */
1392 card->wr_pack_stats.packing_events = kzalloc(
1393 (card->ext_csd.max_packed_writes + 1) *
1394 sizeof(*card->wr_pack_stats.packing_events),
1395 GFP_KERNEL);
1396 if (!card->wr_pack_stats.packing_events)
1397 goto free_card;
1398 }
Maya Erez5f360692012-10-10 03:47:54 +02001399
1400 if (card->ext_csd.bkops_en) {
1401 INIT_DELAYED_WORK(&card->bkops_info.dw,
1402 mmc_start_idle_time_bkops);
1403 INIT_WORK(&card->bkops_info.poll_for_completion,
1404 mmc_bkops_completion_polling);
1405
1406 /*
1407 * Calculate the time to start the BKOPs checking.
Maya Erez70b00222012-12-27 15:40:17 +02001408 * The host controller can set this time in order to
1409 * prevent a race condition before starting BKOPs
1410 * and going into suspend.
1411 * If the host controller didn't set this time,
Maya Erez5f360692012-10-10 03:47:54 +02001412 * a default value is used.
1413 */
1414 card->bkops_info.delay_ms = MMC_IDLE_BKOPS_TIME_MS;
Maya Erez70b00222012-12-27 15:40:17 +02001415 if (card->bkops_info.host_delay_ms)
1416 card->bkops_info.delay_ms =
1417 card->bkops_info.host_delay_ms;
Maya Erez5f360692012-10-10 03:47:54 +02001418 }
Seungwon Jeonc70e9662012-09-27 16:12:47 +02001419 }
1420
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001421 if (!oldcard)
1422 host->card = card;
1423
Philip Rakity08ee80c2011-05-24 18:14:58 -07001424 mmc_free_ext_csd(ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001425 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001426
1427free_card:
1428 if (!oldcard)
1429 mmc_remove_card(card);
1430err:
Philip Rakity08ee80c2011-05-24 18:14:58 -07001431 mmc_free_ext_csd(ext_csd);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001432
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001433 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001434}
1435
Maya Erezcec02a42012-10-19 20:18:17 +02001436static int mmc_can_poweroff_notify(const struct mmc_card *card)
1437{
1438 return card &&
1439 mmc_card_mmc(card) &&
1440 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1441}
1442
1443static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1444{
1445 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1446 int err;
1447
1448 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1449 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1450 timeout = card->ext_csd.power_off_longtime;
1451
1452 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1453 EXT_CSD_POWER_OFF_NOTIFICATION,
1454 notify_type, timeout);
1455 if (err)
1456 pr_err("%s: Power Off Notification timed out, %u\n",
1457 mmc_hostname(card->host), timeout);
1458
1459 /* Disable the power off notification after the switch operation. */
1460 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1461
1462 return err;
1463}
1464
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001465/*
1466 * Host is being removed. Free up the current card.
1467 */
1468static void mmc_remove(struct mmc_host *host)
1469{
1470 BUG_ON(!host);
1471 BUG_ON(!host->card);
1472
1473 mmc_remove_card(host->card);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001474
1475 mmc_claim_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001476 host->card = NULL;
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301477 mmc_exit_clk_scaling(host);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001479}
1480
1481/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001482 * Card detection - card is alive.
1483 */
1484static int mmc_alive(struct mmc_host *host)
1485{
1486 return mmc_send_status(host->card, NULL);
1487}
1488
1489/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001490 * Card detection callback from host.
1491 */
1492static void mmc_detect(struct mmc_host *host)
1493{
1494 int err;
1495
1496 BUG_ON(!host);
1497 BUG_ON(!host->card);
1498
Asutosh Dasbbc84782013-02-11 15:31:35 +05301499 mmc_rpm_hold(host, &host->card->dev);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001500 mmc_claim_host(host);
1501
1502 /*
1503 * Just check if our card has been removed.
1504 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001505 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001506
1507 mmc_release_host(host);
1508
Asutosh Dasbbc84782013-02-11 15:31:35 +05301509 /*
1510 * if detect fails, the device would be removed anyway;
1511 * the rpm framework would mark the device state suspended.
1512 */
1513 if (!err)
1514 mmc_rpm_release(host, &host->card->dev);
1515
Pierre Ossman17b04292007-07-22 22:18:46 +02001516 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001517 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001518
1519 mmc_claim_host(host);
1520 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001521 mmc_power_off(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001522 mmc_release_host(host);
1523 }
1524}
1525
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001526/*
1527 * Suspend callback from host.
1528 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001529static int mmc_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001530{
Balaji T Kc3805462011-09-08 22:08:39 +05301531 int err = 0;
1532
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001533 BUG_ON(!host);
1534 BUG_ON(!host->card);
1535
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301536 /*
1537 * Disable clock scaling before suspend and enable it after resume so
1538 * as to avoid clock scaling decisions kicking in during this window.
1539 */
1540 mmc_disable_clk_scaling(host);
1541
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001542 mmc_claim_host(host);
Maya Erez67a51382012-12-31 16:17:58 +02001543
1544 err = mmc_cache_ctrl(host, 0);
1545 if (err)
1546 goto out;
1547
Maya Erezcec02a42012-10-19 20:18:17 +02001548 if (mmc_can_poweroff_notify(host->card))
1549 err = mmc_poweroff_notify(host->card, EXT_CSD_POWER_OFF_SHORT);
1550 else if (mmc_card_can_sleep(host))
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02001551 err = mmc_card_sleep(host);
Maya Erezcec02a42012-10-19 20:18:17 +02001552 else if (!mmc_host_is_spi(host))
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02001553 mmc_deselect_cards(host);
1554 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001555
Maya Erez67a51382012-12-31 16:17:58 +02001556out:
1557 mmc_release_host(host);
Balaji T Kc3805462011-09-08 22:08:39 +05301558 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001559}
1560
1561/*
1562 * Resume callback from host.
1563 *
1564 * This function tries to determine if the same card is still present
1565 * and, if so, restore all state to it.
1566 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001567static int mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001568{
1569 int err;
1570
1571 BUG_ON(!host);
1572 BUG_ON(!host->card);
1573
1574 mmc_claim_host(host);
Krishna Konda07d1f382012-08-11 16:52:41 -07001575 err = mmc_init_card(host, host->ocr, host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001576 mmc_release_host(host);
1577
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301578 /*
1579 * We have done full initialization of the card,
1580 * reset the clk scale stats and current frequency.
1581 */
1582 if (mmc_can_scale_clk(host))
1583 mmc_init_clk_scaling(host);
1584
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001585 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001586}
1587
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001588static int mmc_power_restore(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001589{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001590 int ret;
1591
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301592 /* Disable clk scaling to avoid switching frequencies intermittently */
1593 mmc_disable_clk_scaling(host);
1594
Girish K S3e73c362012-01-31 15:44:03 +05301595 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001596 mmc_claim_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001597 ret = mmc_init_card(host, host->ocr, host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001598 mmc_release_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001599
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301600 if (mmc_can_scale_clk(host))
1601 mmc_init_clk_scaling(host);
1602
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001603 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001604}
1605
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001606static int mmc_sleep(struct mmc_host *host)
1607{
1608 struct mmc_card *card = host->card;
1609 int err = -ENOSYS;
1610
1611 if (card && card->ext_csd.rev >= 3) {
1612 err = mmc_card_sleepawake(host, 1);
1613 if (err < 0)
Sujit Reddy Thummaf43ab432012-06-19 14:18:27 +05301614 pr_warn("%s: Error %d while putting card into sleep",
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001615 mmc_hostname(host), err);
1616 }
1617
1618 return err;
1619}
1620
1621static int mmc_awake(struct mmc_host *host)
1622{
1623 struct mmc_card *card = host->card;
1624 int err = -ENOSYS;
1625
1626 if (card && card->ext_csd.rev >= 3) {
1627 err = mmc_card_sleepawake(host, 0);
1628 if (err < 0)
1629 pr_debug("%s: Error %d while awaking sleeping card",
1630 mmc_hostname(host), err);
1631 }
1632
1633 return err;
1634}
1635
Adrian Hunter9feae242009-09-22 16:44:32 -07001636static const struct mmc_bus_ops mmc_ops = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001637 .awake = mmc_awake,
1638 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -07001639 .remove = mmc_remove,
1640 .detect = mmc_detect,
1641 .suspend = NULL,
1642 .resume = NULL,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001643 .power_restore = mmc_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001644 .alive = mmc_alive,
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +05301645 .change_bus_speed = mmc_change_bus_speed,
Adrian Hunter9feae242009-09-22 16:44:32 -07001646};
1647
1648static const struct mmc_bus_ops mmc_ops_unsafe = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001649 .awake = mmc_awake,
1650 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -07001651 .remove = mmc_remove,
1652 .detect = mmc_detect,
1653 .suspend = mmc_suspend,
1654 .resume = mmc_resume,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001655 .power_restore = mmc_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001656 .alive = mmc_alive,
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +05301657 .change_bus_speed = mmc_change_bus_speed,
Adrian Hunter9feae242009-09-22 16:44:32 -07001658};
1659
1660static void mmc_attach_bus_ops(struct mmc_host *host)
1661{
1662 const struct mmc_bus_ops *bus_ops;
1663
Matt Fleming71d7d3d2010-09-27 09:42:19 +01001664 if (!mmc_card_is_removable(host))
Adrian Hunter9feae242009-09-22 16:44:32 -07001665 bus_ops = &mmc_ops_unsafe;
1666 else
1667 bus_ops = &mmc_ops;
1668 mmc_attach_bus(host, bus_ops);
1669}
1670
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001671/*
1672 * Starting point for MMC card init.
1673 */
Andy Ross807e8e42011-01-03 10:36:56 -08001674int mmc_attach_mmc(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001675{
1676 int err;
Andy Ross807e8e42011-01-03 10:36:56 -08001677 u32 ocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001678
1679 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001680 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001681
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001682 /* Set correct bus mode for MMC before attempting attach */
1683 if (!mmc_host_is_spi(host))
1684 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1685
Andy Ross807e8e42011-01-03 10:36:56 -08001686 err = mmc_send_op_cond(host, 0, &ocr);
1687 if (err)
1688 return err;
1689
Adrian Hunter9feae242009-09-22 16:44:32 -07001690 mmc_attach_bus_ops(host);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001691 if (host->ocr_avail_mmc)
1692 host->ocr_avail = host->ocr_avail_mmc;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001693
1694 /*
David Brownellaf517152007-08-08 09:11:32 -07001695 * We need to get OCR a different way for SPI.
1696 */
1697 if (mmc_host_is_spi(host)) {
1698 err = mmc_spi_read_ocr(host, 1, &ocr);
1699 if (err)
1700 goto err;
1701 }
1702
1703 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001704 * Sanity check the voltages that the card claims to
1705 * support.
1706 */
1707 if (ocr & 0x7F) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301708 pr_warning("%s: card claims to support voltages "
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001709 "below the defined range. These will be ignored.\n",
1710 mmc_hostname(host));
1711 ocr &= ~0x7F;
1712 }
1713
1714 host->ocr = mmc_select_voltage(host, ocr);
1715
1716 /*
1717 * Can we support the voltage of the card?
1718 */
Pierre Ossman109b5be2007-07-23 00:12:10 +02001719 if (!host->ocr) {
1720 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001721 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001722 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001723
1724 /*
1725 * Detect and init the card.
1726 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001727 err = mmc_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001728 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001729 goto err;
1730
1731 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001732 err = mmc_add_card(host->card);
Andy Ross807e8e42011-01-03 10:36:56 -08001733 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001734 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001735 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001736
Sujit Reddy Thumma1ed78ab2013-04-25 12:39:40 +05301737 mmc_init_clk_scaling(host);
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301738
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001739 return 0;
1740
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001741remove_card:
Andy Ross807e8e42011-01-03 10:36:56 -08001742 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001743 mmc_remove_card(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001744 mmc_claim_host(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001745 host->card = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001746err:
1747 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001748
Girish K Sa3c76eb2011-10-11 11:44:09 +05301749 pr_err("%s: error %d whilst initialising MMC card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02001750 mmc_hostname(host), err);
1751
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001752 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001753}