blob: 9b9c1ed85f6bd481294a6fc937d3b8bfb6d4fa3d [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>
Tatyana Brokhmand8127382013-05-28 09:45:17 +030021#include <linux/reboot.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010022
23#include "core.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020024#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010025#include "mmc_ops.h"
Philip Rakity4c4cb172011-05-13 11:17:18 +053026#include "sd_ops.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010027
28static const unsigned int tran_exp[] = {
29 10000, 100000, 1000000, 10000000,
30 0, 0, 0, 0
31};
32
33static const unsigned char tran_mant[] = {
34 0, 10, 12, 13, 15, 20, 25, 30,
35 35, 40, 45, 50, 55, 60, 70, 80,
36};
37
38static const unsigned int tacc_exp[] = {
39 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
40};
41
42static const unsigned int tacc_mant[] = {
43 0, 10, 12, 13, 15, 20, 25, 30,
44 35, 40, 45, 50, 55, 60, 70, 80,
45};
46
47#define UNSTUFF_BITS(resp,start,size) \
48 ({ \
49 const int __size = size; \
50 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
51 const int __off = 3 - ((start) / 32); \
52 const int __shft = (start) & 31; \
53 u32 __res; \
54 \
55 __res = resp[__off] >> __shft; \
56 if (__size + __shft > 32) \
57 __res |= resp[__off-1] << ((32 - __shft) % 32); \
58 __res & __mask; \
59 })
60
61/*
62 * Given the decoded CSD structure, decode the raw CID to our CID structure.
63 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020064static int mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010065{
66 u32 *resp = card->raw_cid;
67
68 /*
69 * The selection of the format here is based upon published
70 * specs from sandisk and from what people have reported.
71 */
72 switch (card->csd.mmca_vsn) {
73 case 0: /* MMC v1.0 - v1.2 */
74 case 1: /* MMC v1.4 */
75 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
76 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
77 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
78 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
79 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
80 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
81 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
82 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
83 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
84 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
85 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
86 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
87 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
88 break;
89
90 case 2: /* MMC v2.0 - v2.2 */
91 case 3: /* MMC v3.1 - v3.3 */
92 case 4: /* MMC v4 */
93 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
94 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
95 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
96 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
97 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
98 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
99 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
100 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
101 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
102 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
103 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
104 break;
105
106 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530107 pr_err("%s: card has unknown MMCA version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100108 mmc_hostname(card->host), card->csd.mmca_vsn);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200109 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100110 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200111
112 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100113}
114
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700115static void mmc_set_erase_size(struct mmc_card *card)
116{
117 if (card->ext_csd.erase_group_def & 1)
118 card->erase_size = card->ext_csd.hc_erase_size;
119 else
120 card->erase_size = card->csd.erase_size;
121
122 mmc_init_erase(card);
123}
124
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100125/*
126 * Given a 128-bit response, decode to our card CSD structure.
127 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200128static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100129{
130 struct mmc_csd *csd = &card->csd;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700131 unsigned int e, m, a, b;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100132 u32 *resp = card->raw_csd;
133
134 /*
135 * We only understand CSD structure v1.1 and v1.2.
136 * v1.2 has extra information in bits 15, 11 and 10.
Kyungmin Park6da24b72010-08-10 18:01:36 -0700137 * We also support eMMC v4.4 & v4.41.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100138 */
Kyungmin Park6da24b72010-08-10 18:01:36 -0700139 csd->structure = UNSTUFF_BITS(resp, 126, 2);
140 if (csd->structure == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530141 pr_err("%s: unrecognised CSD structure version %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700142 mmc_hostname(card->host), csd->structure);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200143 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100144 }
145
146 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
147 m = UNSTUFF_BITS(resp, 115, 4);
148 e = UNSTUFF_BITS(resp, 112, 3);
149 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
150 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
151
152 m = UNSTUFF_BITS(resp, 99, 4);
153 e = UNSTUFF_BITS(resp, 96, 3);
154 csd->max_dtr = tran_exp[e] * tran_mant[m];
155 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
156
157 e = UNSTUFF_BITS(resp, 47, 3);
158 m = UNSTUFF_BITS(resp, 62, 12);
159 csd->capacity = (1 + m) << (e + 2);
160
161 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
162 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
163 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
164 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
165 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
166 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
167 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200168
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700169 if (csd->write_blkbits >= 9) {
170 a = UNSTUFF_BITS(resp, 42, 5);
171 b = UNSTUFF_BITS(resp, 37, 5);
172 csd->erase_size = (a + 1) * (b + 1);
173 csd->erase_size <<= csd->write_blkbits - 9;
174 }
175
Pierre Ossmanbd766312007-05-01 16:11:57 +0200176 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100177}
178
179/*
Philip Rakity08ee80c2011-05-24 18:14:58 -0700180 * Read extended CSD.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100181 */
Philip Rakity08ee80c2011-05-24 18:14:58 -0700182static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100183{
184 int err;
185 u8 *ext_csd;
186
187 BUG_ON(!card);
Philip Rakity08ee80c2011-05-24 18:14:58 -0700188 BUG_ON(!new_ext_csd);
189
190 *new_ext_csd = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100191
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100192 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
Pierre Ossman17b04292007-07-22 22:18:46 +0200193 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100194
195 /*
196 * As the ext_csd is so large and mostly unused, we don't store the
197 * raw block in mmc_card.
198 */
199 ext_csd = kmalloc(512, GFP_KERNEL);
200 if (!ext_csd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530201 pr_err("%s: could not allocate a buffer to "
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200202 "receive the ext_csd.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200203 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100204 }
205
206 err = mmc_send_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200207 if (err) {
Philip Rakity08ee80c2011-05-24 18:14:58 -0700208 kfree(ext_csd);
209 *new_ext_csd = NULL;
210
Wolfgang Mueesd08ebed2009-09-22 16:45:26 -0700211 /* If the host or the card can't do the switch,
212 * fail more gracefully. */
213 if ((err != -EINVAL)
214 && (err != -ENOSYS)
215 && (err != -EFAULT))
Philip Rakity08ee80c2011-05-24 18:14:58 -0700216 return err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200217
218 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100219 * High capacity cards should have this "magic" size
220 * stored in their CSD.
221 */
222 if (card->csd.capacity == (4096 * 512)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530223 pr_err("%s: unable to read EXT_CSD "
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100224 "on a possible high capacity card. "
225 "Card will be ignored.\n",
226 mmc_hostname(card->host));
227 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530228 pr_warning("%s: unable to read "
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100229 "EXT_CSD, performance might "
230 "suffer.\n",
231 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200232 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100233 }
Philip Rakity08ee80c2011-05-24 18:14:58 -0700234 } else
235 *new_ext_csd = ext_csd;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200236
Philip Rakity08ee80c2011-05-24 18:14:58 -0700237 return err;
238}
239
Seungwon Jeonfe201cd2012-04-25 16:17:37 +0900240static void mmc_select_card_type(struct mmc_card *card)
241{
242 struct mmc_host *host = card->host;
243 u8 card_type = card->ext_csd.raw_card_type & EXT_CSD_CARD_TYPE_MASK;
244 unsigned int caps = host->caps, caps2 = host->caps2;
245 unsigned int hs_max_dtr = 0;
246
247 if (card_type & EXT_CSD_CARD_TYPE_26)
248 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
249
250 if (caps & MMC_CAP_MMC_HIGHSPEED &&
251 card_type & EXT_CSD_CARD_TYPE_52)
252 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
253
254 if ((caps & MMC_CAP_1_8V_DDR &&
255 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) ||
256 (caps & MMC_CAP_1_2V_DDR &&
257 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V))
258 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
259
260 if ((caps2 & MMC_CAP2_HS200_1_8V_SDR &&
261 card_type & EXT_CSD_CARD_TYPE_SDR_1_8V) ||
262 (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
263 card_type & EXT_CSD_CARD_TYPE_SDR_1_2V))
264 hs_max_dtr = MMC_HS200_MAX_DTR;
265
266 card->ext_csd.hs_max_dtr = hs_max_dtr;
267 card->ext_csd.card_type = card_type;
268}
269
Philip Rakity08ee80c2011-05-24 18:14:58 -0700270/*
271 * Decode extended CSD.
272 */
273static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
274{
Namjae Jeone0c368d2011-10-06 23:41:38 +0900275 int err = 0, idx;
276 unsigned int part_size;
277 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700278
279 BUG_ON(!card);
280
281 if (!ext_csd)
282 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100283
Kyungmin Park6da24b72010-08-10 18:01:36 -0700284 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700285 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
Kyungmin Park6da24b72010-08-10 18:01:36 -0700286 if (card->csd.structure == 3) {
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700287 if (card->ext_csd.raw_ext_csd_structure > 2) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530288 pr_err("%s: unrecognised EXT_CSD structure "
Kyungmin Park6da24b72010-08-10 18:01:36 -0700289 "version %d\n", mmc_hostname(card->host),
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700290 card->ext_csd.raw_ext_csd_structure);
Kyungmin Park6da24b72010-08-10 18:01:36 -0700291 err = -EINVAL;
292 goto out;
293 }
294 }
295
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700296 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Subhash Jadavani7ca4a782013-02-01 11:09:58 +0530297 if (card->ext_csd.rev > 7) {
Stephen Boyd3f113aa2013-02-11 14:30:14 -0800298 pr_err("%s: unrecognised EXT_CSD revision %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700299 mmc_hostname(card->host), card->ext_csd.rev);
Florin Malita00cedfa2007-10-13 12:27:20 -0400300 err = -EINVAL;
301 goto out;
Pierre Ossmand7604d72007-07-23 00:34:07 +0200302 }
303
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700304 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
305 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
306 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
307 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700308 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200309 card->ext_csd.sectors =
310 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
311 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
312 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
313 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700314
315 /* Cards with density > 2GiB are sector addressed */
316 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200317 mmc_card_set_blockaddr(card);
318 }
Seungwon Jeonfe201cd2012-04-25 16:17:37 +0900319
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700320 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
Seungwon Jeonfe201cd2012-04-25 16:17:37 +0900321 mmc_select_card_type(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100322
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700323 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
324 card->ext_csd.raw_erase_timeout_mult =
325 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
326 card->ext_csd.raw_hc_erase_grp_size =
327 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700328 if (card->ext_csd.rev >= 3) {
329 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500330 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
331
332 /* EXT_CSD value is in units of 10ms, but we store in ms */
333 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700334
335 /* Sleep / awake timeout in 100ns units */
336 if (sa_shift > 0 && sa_shift <= 0x17)
337 card->ext_csd.sa_timeout =
338 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700339 card->ext_csd.erase_group_def =
340 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
341 card->ext_csd.hc_erase_timeout = 300 *
342 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
343 card->ext_csd.hc_erase_size =
344 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500345
346 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500347
348 /*
349 * There are two boot regions of equal size, defined in
350 * multiples of 128K.
351 */
Namjae Jeone0c368d2011-10-06 23:41:38 +0900352 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
353 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
354 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
355 mmc_part_add(card, part_size,
356 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100357 "boot%d", idx, true,
358 MMC_BLK_DATA_AREA_BOOT);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900359 }
360 }
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700361 }
362
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700363 card->ext_csd.raw_hc_erase_gap_size =
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100364 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700365 card->ext_csd.raw_sec_trim_mult =
366 ext_csd[EXT_CSD_SEC_TRIM_MULT];
367 card->ext_csd.raw_sec_erase_mult =
368 ext_csd[EXT_CSD_SEC_ERASE_MULT];
369 card->ext_csd.raw_sec_feature_support =
370 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
371 card->ext_csd.raw_trim_mult =
372 ext_csd[EXT_CSD_TRIM_MULT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700373 if (card->ext_csd.rev >= 4) {
Chuanxiao Dong709de992011-01-22 04:09:41 +0800374 /*
375 * Enhanced area feature support -- check whether the eMMC
376 * card has the Enhanced area enabled. If so, export enhanced
377 * area offset and size to user by adding sysfs interface.
378 */
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400379 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
Chuanxiao Dong709de992011-01-22 04:09:41 +0800380 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700381 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
Namjae Jeone0c368d2011-10-06 23:41:38 +0900382 hc_erase_grp_sz =
Chuanxiao Dong709de992011-01-22 04:09:41 +0800383 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Namjae Jeone0c368d2011-10-06 23:41:38 +0900384 hc_wp_grp_sz =
Chuanxiao Dong709de992011-01-22 04:09:41 +0800385 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
386
387 card->ext_csd.enhanced_area_en = 1;
388 /*
389 * calculate the enhanced data area offset, in bytes
390 */
391 card->ext_csd.enhanced_area_offset =
392 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
393 (ext_csd[137] << 8) + ext_csd[136];
394 if (mmc_card_blockaddr(card))
395 card->ext_csd.enhanced_area_offset <<= 9;
396 /*
397 * calculate the enhanced data area size, in kilobytes
398 */
399 card->ext_csd.enhanced_area_size =
400 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
401 ext_csd[140];
402 card->ext_csd.enhanced_area_size *=
403 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
404 card->ext_csd.enhanced_area_size <<= 9;
405 } else {
406 /*
407 * If the enhanced area is not enabled, disable these
408 * device attributes.
409 */
410 card->ext_csd.enhanced_area_offset = -EINVAL;
411 card->ext_csd.enhanced_area_size = -EINVAL;
412 }
Namjae Jeone0c368d2011-10-06 23:41:38 +0900413
414 /*
415 * General purpose partition feature support --
416 * If ext_csd has the size of general purpose partitions,
417 * set size, part_cfg, partition name in mmc_part.
418 */
419 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
420 EXT_CSD_PART_SUPPORT_PART_EN) {
421 if (card->ext_csd.enhanced_area_en != 1) {
422 hc_erase_grp_sz =
423 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
424 hc_wp_grp_sz =
425 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
426
427 card->ext_csd.enhanced_area_en = 1;
428 }
429
430 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
431 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
432 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
433 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
434 continue;
435 part_size =
436 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
437 << 16) +
438 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
439 << 8) +
440 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
441 part_size *= (size_t)(hc_erase_grp_sz *
442 hc_wp_grp_sz);
443 mmc_part_add(card, part_size << 19,
444 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100445 "gp%d", idx, false,
446 MMC_BLK_DATA_AREA_GP);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900447 }
448 }
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700449 card->ext_csd.sec_trim_mult =
450 ext_csd[EXT_CSD_SEC_TRIM_MULT];
451 card->ext_csd.sec_erase_mult =
452 ext_csd[EXT_CSD_SEC_ERASE_MULT];
453 card->ext_csd.sec_feature_support =
454 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
455 card->ext_csd.trim_timeout = 300 *
456 ext_csd[EXT_CSD_TRIM_MULT];
Johan Rudholmadd710e2011-12-02 08:51:06 +0100457
458 /*
459 * Note that the call to mmc_part_add above defaults to read
460 * only. If this default assumption is changed, the call must
461 * take into account the value of boot_locked below.
462 */
463 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
464 card->ext_csd.boot_ro_lockable = true;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700465 }
466
Adrian Hunterb2499512011-08-29 16:42:11 +0300467 if (card->ext_csd.rev >= 5) {
Jaehoon Chungc1a56a12012-09-24 14:44:45 +0200468 /* check whether the eMMC card supports BKOPS */
469 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
470 card->ext_csd.bkops = 1;
471 card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
472 card->ext_csd.raw_bkops_status =
473 ext_csd[EXT_CSD_BKOPS_STATUS];
Maya Ereze966c1c2012-09-29 06:30:47 +0200474 if (!card->ext_csd.bkops_en &&
475 card->host->caps2 & MMC_CAP2_INIT_BKOPS) {
476 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
477 EXT_CSD_BKOPS_EN, 1, 0);
478 if (err)
479 pr_warn("%s: Enabling BKOPS failed\n",
480 mmc_hostname(card->host));
481 else
482 card->ext_csd.bkops_en = 1;
483 }
Jaehoon Chungc1a56a12012-09-24 14:44:45 +0200484 }
485
Yaniv Gardib58afd62012-11-06 21:06:19 +0200486 pr_info("%s: BKOPS_EN bit = %d\n",
487 mmc_hostname(card->host), card->ext_csd.bkops_en);
488
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400489 /* check whether the eMMC card supports HPI */
490 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
491 card->ext_csd.hpi = 1;
492 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
493 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
494 else
495 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
496 /*
497 * Indicate the maximum timeout to close
498 * a command interrupted by HPI
499 */
500 card->ext_csd.out_of_int_time =
501 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
502 }
503
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500504 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
Adrian Hunterb2499512011-08-29 16:42:11 +0300505 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
Loic Pallardye12f9ba2012-08-06 15:12:27 +0000506
507 /*
508 * RPMB regions are defined in multiples of 128K.
509 */
510 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
511 if (ext_csd[EXT_CSD_RPMB_MULT]) {
512 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
513 EXT_CSD_PART_CONFIG_ACC_RPMB,
514 "rpmb", 0, false,
515 MMC_BLK_DATA_AREA_RPMB);
516 }
Adrian Hunterb2499512011-08-29 16:42:11 +0300517 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500518
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400519 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700520 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
521 card->erased_byte = 0xFF;
522 else
523 card->erased_byte = 0x0;
524
Seungwon Jeon336c7162011-10-25 09:43:12 +0900525 /* eMMC v4.5 or later */
Girish K Sbec87262011-10-13 12:04:16 +0530526 if (card->ext_csd.rev >= 6) {
Seungwon Jeon336c7162011-10-25 09:43:12 +0900527 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
528
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900529 card->ext_csd.generic_cmd6_time = 10 *
530 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
Girish K Sbec87262011-10-13 12:04:16 +0530531 card->ext_csd.power_off_longtime = 10 *
532 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900533
Seungwon Jeon336c7162011-10-25 09:43:12 +0900534 card->ext_csd.cache_size =
535 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
536 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
537 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
538 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
Saugata Das42659002011-12-21 13:09:17 +0530539
540 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
541 card->ext_csd.data_sector_size = 4096;
542 else
543 card->ext_csd.data_sector_size = 512;
544
545 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
546 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
547 card->ext_csd.data_tag_unit_size =
548 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
549 (card->ext_csd.data_sector_size);
550 } else {
551 card->ext_csd.data_tag_unit_size = 0;
552 }
Seungwon Jeonc70e9662012-09-27 16:12:47 +0200553
554 card->ext_csd.max_packed_writes =
555 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
556 card->ext_csd.max_packed_reads =
557 ext_csd[EXT_CSD_MAX_PACKED_READS];
Seungwon Jeon336c7162011-10-25 09:43:12 +0900558 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900559
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100560out:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700561 return err;
562}
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100563
Philip Rakity08ee80c2011-05-24 18:14:58 -0700564static inline void mmc_free_ext_csd(u8 *ext_csd)
565{
566 kfree(ext_csd);
567}
568
569
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700570static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700571{
572 u8 *bw_ext_csd;
573 int err;
574
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700575 if (bus_width == MMC_BUS_WIDTH_1)
576 return 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700577
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700578 err = mmc_get_ext_csd(card, &bw_ext_csd);
579
580 if (err || bw_ext_csd == NULL) {
Philip Rakity08ee80c2011-05-24 18:14:58 -0700581 if (bus_width != MMC_BUS_WIDTH_1)
582 err = -EINVAL;
583 goto out;
584 }
585
586 if (bus_width == MMC_BUS_WIDTH_1)
587 goto out;
588
589 /* only compare read only fields */
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100590 err = !((card->ext_csd.raw_partition_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700591 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700592 (card->ext_csd.raw_erased_mem_count ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700593 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700594 (card->ext_csd.rev ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700595 bw_ext_csd[EXT_CSD_REV]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700596 (card->ext_csd.raw_ext_csd_structure ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700597 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700598 (card->ext_csd.raw_card_type ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700599 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700600 (card->ext_csd.raw_s_a_timeout ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700601 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700602 (card->ext_csd.raw_hc_erase_gap_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700603 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700604 (card->ext_csd.raw_erase_timeout_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700605 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700606 (card->ext_csd.raw_hc_erase_grp_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700607 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700608 (card->ext_csd.raw_sec_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700609 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700610 (card->ext_csd.raw_sec_erase_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700611 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700612 (card->ext_csd.raw_sec_feature_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700613 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700614 (card->ext_csd.raw_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700615 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
Philip Rakityf39b2dd2011-07-07 09:04:55 -0700616 (card->ext_csd.raw_sectors[0] ==
617 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
618 (card->ext_csd.raw_sectors[1] ==
619 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
620 (card->ext_csd.raw_sectors[2] ==
621 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
622 (card->ext_csd.raw_sectors[3] ==
623 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
Philip Rakity08ee80c2011-05-24 18:14:58 -0700624 if (err)
625 err = -EINVAL;
626
627out:
628 mmc_free_ext_csd(bw_ext_csd);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100629 return err;
630}
631
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100632MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
633 card->raw_cid[2], card->raw_cid[3]);
634MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
635 card->raw_csd[2], card->raw_csd[3]);
636MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700637MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
638MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100639MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
640MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
641MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
642MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
643MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
644MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Chuanxiao Dong709de992011-01-22 04:09:41 +0800645MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
646 card->ext_csd.enhanced_area_offset);
647MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
Loic Pallardy762a87b2012-08-06 15:12:29 +0000648MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
649MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100650
651static struct attribute *mmc_std_attrs[] = {
652 &dev_attr_cid.attr,
653 &dev_attr_csd.attr,
654 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700655 &dev_attr_erase_size.attr,
656 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100657 &dev_attr_fwrev.attr,
658 &dev_attr_hwrev.attr,
659 &dev_attr_manfid.attr,
660 &dev_attr_name.attr,
661 &dev_attr_oemid.attr,
662 &dev_attr_serial.attr,
Chuanxiao Dong709de992011-01-22 04:09:41 +0800663 &dev_attr_enhanced_area_offset.attr,
664 &dev_attr_enhanced_area_size.attr,
Loic Pallardy762a87b2012-08-06 15:12:29 +0000665 &dev_attr_raw_rpmb_size_mult.attr,
666 &dev_attr_rel_sectors.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100667 NULL,
668};
669
670static struct attribute_group mmc_std_attr_group = {
671 .attrs = mmc_std_attrs,
672};
673
David Brownella4dbd672009-06-24 10:06:31 -0700674static const struct attribute_group *mmc_attr_groups[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100675 &mmc_std_attr_group,
676 NULL,
677};
678
679static struct device_type mmc_type = {
680 .groups = mmc_attr_groups,
681};
682
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100683/*
Girish K Sb87d8db2011-09-23 20:41:47 +0530684 * Select the PowerClass for the current bus width
685 * If power class is defined for 4/8 bit bus in the
686 * extended CSD register, select it by executing the
687 * mmc_switch command.
688 */
689static int mmc_select_powerclass(struct mmc_card *card,
690 unsigned int bus_width, u8 *ext_csd)
691{
692 int err = 0;
693 unsigned int pwrclass_val;
694 unsigned int index = 0;
695 struct mmc_host *host;
696
697 BUG_ON(!card);
698
699 host = card->host;
700 BUG_ON(!host);
701
702 if (ext_csd == NULL)
703 return 0;
704
705 /* Power class selection is supported for versions >= 4.0 */
706 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
707 return 0;
708
709 /* Power class values are defined only for 4/8 bit bus */
710 if (bus_width == EXT_CSD_BUS_WIDTH_1)
711 return 0;
712
713 switch (1 << host->ios.vdd) {
714 case MMC_VDD_165_195:
715 if (host->ios.clock <= 26000000)
716 index = EXT_CSD_PWR_CL_26_195;
717 else if (host->ios.clock <= 52000000)
718 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
719 EXT_CSD_PWR_CL_52_195 :
720 EXT_CSD_PWR_CL_DDR_52_195;
721 else if (host->ios.clock <= 200000000)
722 index = EXT_CSD_PWR_CL_200_195;
723 break;
Subhash Jadavani93fc5a42012-04-03 12:25:58 +0530724 case MMC_VDD_27_28:
725 case MMC_VDD_28_29:
726 case MMC_VDD_29_30:
727 case MMC_VDD_30_31:
728 case MMC_VDD_31_32:
Girish K Sb87d8db2011-09-23 20:41:47 +0530729 case MMC_VDD_32_33:
730 case MMC_VDD_33_34:
731 case MMC_VDD_34_35:
732 case MMC_VDD_35_36:
733 if (host->ios.clock <= 26000000)
734 index = EXT_CSD_PWR_CL_26_360;
735 else if (host->ios.clock <= 52000000)
736 index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
737 EXT_CSD_PWR_CL_52_360 :
738 EXT_CSD_PWR_CL_DDR_52_360;
739 else if (host->ios.clock <= 200000000)
740 index = EXT_CSD_PWR_CL_200_360;
741 break;
742 default:
743 pr_warning("%s: Voltage range not supported "
744 "for power class.\n", mmc_hostname(host));
745 return -EINVAL;
746 }
747
748 pwrclass_val = ext_csd[index];
749
750 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
751 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
752 EXT_CSD_PWR_CL_8BIT_SHIFT;
753 else
754 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
755 EXT_CSD_PWR_CL_4BIT_SHIFT;
756
757 /* If the power class is different from the default value */
758 if (pwrclass_val > 0) {
759 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
760 EXT_CSD_POWER_CLASS,
761 pwrclass_val,
Seungwon Jeon71fe3eb2011-10-18 13:20:57 +0900762 card->ext_csd.generic_cmd6_time);
Girish K Sb87d8db2011-09-23 20:41:47 +0530763 }
764
765 return err;
766}
767
768/*
Girish K Sa4924c72012-01-11 14:04:52 -0500769 * Selects the desired buswidth and switch to the HS200 mode
770 * if bus width set without error
771 */
772static int mmc_select_hs200(struct mmc_card *card)
773{
774 int idx, err = 0;
775 struct mmc_host *host;
776 static unsigned ext_csd_bits[] = {
777 EXT_CSD_BUS_WIDTH_4,
778 EXT_CSD_BUS_WIDTH_8,
779 };
780 static unsigned bus_widths[] = {
781 MMC_BUS_WIDTH_4,
782 MMC_BUS_WIDTH_8,
783 };
784
785 BUG_ON(!card);
786
787 host = card->host;
788
789 if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V &&
790 host->caps2 & MMC_CAP2_HS200_1_2V_SDR)
791 if (mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120, 0))
792 err = mmc_set_signal_voltage(host,
793 MMC_SIGNAL_VOLTAGE_180, 0);
794
795 /* If fails try again during next card power cycle */
796 if (err)
797 goto err;
798
799 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0;
800
801 /*
802 * Unlike SD, MMC cards dont have a configuration register to notify
803 * supported bus width. So bus test command should be run to identify
804 * the supported bus width or compare the ext csd values of current
805 * bus width and ext csd values of 1 bit mode read earlier.
806 */
807 for (; idx >= 0; idx--) {
808
809 /*
810 * Host is capable of 8bit transfer, then switch
811 * the device to work in 8bit transfer mode. If the
812 * mmc switch command returns error then switch to
813 * 4bit transfer mode. On success set the corresponding
814 * bus width on the host.
815 */
816 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
817 EXT_CSD_BUS_WIDTH,
818 ext_csd_bits[idx],
819 card->ext_csd.generic_cmd6_time);
820 if (err)
821 continue;
822
823 mmc_set_bus_width(card->host, bus_widths[idx]);
824
825 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
826 err = mmc_compare_ext_csds(card, bus_widths[idx]);
827 else
828 err = mmc_bus_test(card, bus_widths[idx]);
829 if (!err)
830 break;
831 }
832
833 /* switch to HS200 mode if bus width set successfully */
834 if (!err)
835 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
836 EXT_CSD_HS_TIMING, 2, 0);
837err:
838 return err;
839}
840
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +0530841/**
842 * mmc_change_bus_speed() - Change MMC card bus frequency at runtime
843 * @host: pointer to mmc host structure
844 * @freq: pointer to desired frequency to be set
845 *
846 * Change the MMC card bus frequency at runtime after the card is
847 * initialized. Callers are expected to make sure of the card's
848 * state (DATA/RCV/TRANSFER) beforing changing the frequency at runtime.
849 *
850 * If the frequency to change is greater than max. supported by card,
851 * *freq is changed to max. supported by card and if it is less than min.
852 * supported by host, *freq is changed to min. supported by host.
853 */
854static int mmc_change_bus_speed(struct mmc_host *host, unsigned long *freq)
855{
856 int err = 0;
857 struct mmc_card *card;
858
859 mmc_claim_host(host);
860 /*
861 * Assign card pointer after claiming host to avoid race
862 * conditions that may arise during removal of the card.
863 */
864 card = host->card;
865
866 if (!card || !freq) {
867 err = -EINVAL;
868 goto out;
869 }
870
871 if (mmc_card_highspeed(card) || mmc_card_hs200(card)
872 || mmc_card_ddr_mode(card)) {
873 if (*freq > card->ext_csd.hs_max_dtr)
874 *freq = card->ext_csd.hs_max_dtr;
875 } else if (*freq > card->csd.max_dtr) {
876 *freq = card->csd.max_dtr;
877 }
878
879 if (*freq < host->f_min)
880 *freq = host->f_min;
881
882 mmc_set_clock(host, (unsigned int) (*freq));
883
884 if (mmc_card_hs200(card) && card->host->ops->execute_tuning) {
885 /*
886 * We try to probe host driver for tuning for any
887 * frequency, it is host driver responsibility to
888 * perform actual tuning only when required.
889 */
890 mmc_host_clk_hold(card->host);
891 err = card->host->ops->execute_tuning(card->host,
892 MMC_SEND_TUNING_BLOCK_HS200);
893 mmc_host_clk_release(card->host);
894
895 if (err)
896 pr_warn("%s: %s: tuning execution failed %d\n",
897 mmc_hostname(card->host), __func__, err);
898 }
899out:
900 mmc_release_host(host);
901 return err;
902}
903
Tatyana Brokhmand8127382013-05-28 09:45:17 +0300904static int mmc_reboot_notify(struct notifier_block *notify_block,
905 unsigned long event, void *unused)
906{
907 struct mmc_card *card = container_of(
908 notify_block, struct mmc_card, reboot_notify);
909
910 if (event != SYS_RESTART)
911 card->issue_long_pon = true;
912 else
913 card->issue_long_pon = false;
914
915 return NOTIFY_OK;
916}
917
Girish K Sa4924c72012-01-11 14:04:52 -0500918/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200919 * Handle the detection and initialisation of a card.
920 *
Deepak Saxena87693922008-06-16 19:20:57 -0700921 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200922 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100923 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +0200924static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200925 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100926{
927 struct mmc_card *card;
Philip Rakity25d5c692010-11-07 16:22:28 -0500928 int err, ddr = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100929 u32 cid[4];
930 unsigned int max_dtr;
Philip Rakityb676f032011-02-13 23:13:09 -0800931 u32 rocr;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700932 u8 *ext_csd = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100933
934 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200935 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100936
Stefan Nilsson XK44669032011-09-15 17:50:38 +0200937 /* Set correct bus mode for MMC before attempting init */
938 if (!mmc_host_is_spi(host))
939 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
940
Ulf Hanssone7747472012-03-01 13:18:05 +0100941 /* Initialization should be done at 3.3 V I/O voltage. */
942 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
943
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100944 /*
945 * Since we're changing the OCR value, we seem to
946 * need to tell some cards to go back to the idle
947 * state. We wait 1ms to give cards time to
948 * respond.
Balaji T Kc3805462011-09-08 22:08:39 +0530949 * mmc_go_idle is needed for eMMC that are asleep
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100950 */
951 mmc_go_idle(host);
952
953 /* The extra bit indicates that we support high capacity */
Philip Rakityb676f032011-02-13 23:13:09 -0800954 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200955 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200956 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100957
958 /*
David Brownellaf517152007-08-08 09:11:32 -0700959 * For SPI, enable CRC as appropriate.
960 */
961 if (mmc_host_is_spi(host)) {
962 err = mmc_spi_set_crc(host, use_spi_crc);
963 if (err)
964 goto err;
965 }
966
967 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100968 * Fetch CID from card.
969 */
David Brownellaf517152007-08-08 09:11:32 -0700970 if (mmc_host_is_spi(host))
971 err = mmc_send_cid(host, cid);
972 else
973 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +0200974 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100975 goto err;
976
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200977 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200978 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
979 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200980 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200981 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100982
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200983 card = oldcard;
984 } else {
985 /*
986 * Allocate card structure.
987 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100988 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200989 if (IS_ERR(card)) {
990 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200991 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200992 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200993
994 card->type = MMC_TYPE_MMC;
995 card->rca = 1;
996 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
Tatyana Brokhmand8127382013-05-28 09:45:17 +0300997 card->reboot_notify.notifier_call = mmc_reboot_notify;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200998 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100999
1000 /*
David Brownellaf517152007-08-08 09:11:32 -07001001 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001002 */
David Brownellaf517152007-08-08 09:11:32 -07001003 if (!mmc_host_is_spi(host)) {
1004 err = mmc_set_relative_addr(card);
1005 if (err)
1006 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001007
David Brownellaf517152007-08-08 09:11:32 -07001008 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1009 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001010
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001011 if (!oldcard) {
1012 /*
1013 * Fetch CSD from card.
1014 */
1015 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001016 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001017 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001018
Pierre Ossmanbd766312007-05-01 16:11:57 +02001019 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001020 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001021 goto free_card;
1022 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001023 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001024 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001025 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001026
1027 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001028 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001029 */
David Brownellaf517152007-08-08 09:11:32 -07001030 if (!mmc_host_is_spi(host)) {
1031 err = mmc_select_card(card);
1032 if (err)
1033 goto free_card;
1034 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001035
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001036 if (!oldcard) {
1037 /*
David Brownellaf517152007-08-08 09:11:32 -07001038 * Fetch and process extended CSD.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001039 */
Philip Rakity08ee80c2011-05-24 18:14:58 -07001040
1041 err = mmc_get_ext_csd(card, &ext_csd);
1042 if (err)
1043 goto free_card;
1044 err = mmc_read_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001045 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001046 goto free_card;
Philip Rakityb676f032011-02-13 23:13:09 -08001047
1048 /* If doing byte addressing, check if required to do sector
1049 * addressing. Handle the case of <2GB cards needing sector
1050 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1051 * ocr register has bit 30 set for sector addressing.
1052 */
1053 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
1054 mmc_card_set_blockaddr(card);
1055
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001056 /* Erase size depends on CSD and Extended CSD */
1057 mmc_set_erase_size(card);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058
1059 if (card->ext_csd.sectors && (rocr & MMC_CARD_SECTOR_ADDR))
1060 mmc_card_set_blockaddr(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001061 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001062
1063 /*
Chuanxiao Dong709de992011-01-22 04:09:41 +08001064 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001065 * bit. This bit will be lost every time after a reset or power off.
Chuanxiao Dong709de992011-01-22 04:09:41 +08001066 */
Adrian Hunter83bb24a2012-03-13 05:19:13 -04001067 if (card->ext_csd.enhanced_area_en ||
1068 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
Chuanxiao Dong709de992011-01-22 04:09:41 +08001069 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001070 EXT_CSD_ERASE_GROUP_DEF, 1,
1071 card->ext_csd.generic_cmd6_time);
Chuanxiao Dong709de992011-01-22 04:09:41 +08001072
1073 if (err && err != -EBADMSG)
1074 goto free_card;
1075
1076 if (err) {
1077 err = 0;
1078 /*
1079 * Just disable enhanced area off & sz
1080 * will try to enable ERASE_GROUP_DEF
1081 * during next time reinit
1082 */
1083 card->ext_csd.enhanced_area_offset = -EINVAL;
1084 card->ext_csd.enhanced_area_size = -EINVAL;
1085 } else {
1086 card->ext_csd.erase_group_def = 1;
1087 /*
1088 * enable ERASE_GRP_DEF successfully.
1089 * This will affect the erase size, so
1090 * here need to reset erase size
1091 */
1092 mmc_set_erase_size(card);
1093 }
1094 }
1095
1096 /*
Philip Rakity41e2a482011-03-19 14:10:33 -04001097 * Ensure eMMC user default partition is enabled
1098 */
Andrei Warkentin371a6892011-04-11 18:10:25 -05001099 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1100 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1101 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1102 card->ext_csd.part_config,
1103 card->ext_csd.part_time);
1104 if (err && err != -EBADMSG)
1105 goto free_card;
Oluwafemi Adeyemi151b4772013-01-03 11:32:53 -08001106 card->part_curr = card->ext_csd.part_config &
1107 EXT_CSD_PART_CONFIG_ACC_MASK;
Philip Rakity41e2a482011-03-19 14:10:33 -04001108 }
1109
1110 /*
Girish K Sbec87262011-10-13 12:04:16 +05301111 * If the host supports the power_off_notify capability then
1112 * set the notification byte in the ext_csd register of device
1113 */
1114 if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) &&
Girish K Sa80f1622011-11-15 11:55:46 +05301115 (card->ext_csd.rev >= 6)) {
Girish K Sbec87262011-10-13 12:04:16 +05301116 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1117 EXT_CSD_POWER_OFF_NOTIFICATION,
1118 EXT_CSD_POWER_ON,
1119 card->ext_csd.generic_cmd6_time);
1120 if (err && err != -EBADMSG)
1121 goto free_card;
Girish K Sbec87262011-10-13 12:04:16 +05301122
Girish K S96a85d52011-11-04 16:22:47 +05301123 /*
1124 * The err can be -EBADMSG or 0,
1125 * so check for success and update the flag
1126 */
1127 if (!err)
Maya Erezcec02a42012-10-19 20:18:17 +02001128 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
Girish K S96a85d52011-11-04 16:22:47 +05301129 }
Girish K Sbec87262011-10-13 12:04:16 +05301130
1131 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001132 * Activate high speed (if supported)
1133 */
Girish K Sa4924c72012-01-11 14:04:52 -05001134 if (card->ext_csd.hs_max_dtr != 0) {
1135 err = 0;
1136 if (card->ext_csd.hs_max_dtr > 52000000 &&
1137 host->caps2 & MMC_CAP2_HS200)
1138 err = mmc_select_hs200(card);
1139 else if (host->caps & MMC_CAP_MMC_HIGHSPEED)
1140 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Girish K S7488e922012-01-19 08:56:19 +05301141 EXT_CSD_HS_TIMING, 1,
1142 card->ext_csd.generic_cmd6_time);
Girish K Sa4924c72012-01-11 14:04:52 -05001143
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001144 if (err && err != -EBADMSG)
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001145 goto free_card;
1146
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001147 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301148 pr_warning("%s: switch to highspeed failed\n",
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001149 mmc_hostname(card->host));
1150 err = 0;
1151 } else {
Girish K Sa4924c72012-01-11 14:04:52 -05001152 if (card->ext_csd.hs_max_dtr > 52000000 &&
1153 host->caps2 & MMC_CAP2_HS200) {
1154 mmc_card_set_hs200(card);
1155 mmc_set_timing(card->host,
1156 MMC_TIMING_MMC_HS200);
1157 } else {
1158 mmc_card_set_highspeed(card);
1159 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1160 }
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001161 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001162 }
1163
1164 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001165 * Compute bus speed.
1166 */
1167 max_dtr = (unsigned int)-1;
1168
Girish K Sa4924c72012-01-11 14:04:52 -05001169 if (mmc_card_highspeed(card) || mmc_card_hs200(card)) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001170 if (max_dtr > card->ext_csd.hs_max_dtr)
1171 max_dtr = card->ext_csd.hs_max_dtr;
1172 } else if (max_dtr > card->csd.max_dtr) {
1173 max_dtr = card->csd.max_dtr;
1174 }
1175
1176 mmc_set_clock(host, max_dtr);
1177
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001178 /*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001179 * Indicate DDR mode (if supported).
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001180 */
1181 if (mmc_card_highspeed(card)) {
1182 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
Philip Rakity4c4cb172011-05-13 11:17:18 +05301183 && ((host->caps & (MMC_CAP_1_8V_DDR |
1184 MMC_CAP_UHS_DDR50))
1185 == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
Adrian Hunter49e3b5a2010-10-11 12:43:50 +03001186 ddr = MMC_1_8V_DDR_MODE;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001187 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
Philip Rakity4c4cb172011-05-13 11:17:18 +05301188 && ((host->caps & (MMC_CAP_1_2V_DDR |
1189 MMC_CAP_UHS_DDR50))
1190 == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
Adrian Hunter49e3b5a2010-10-11 12:43:50 +03001191 ddr = MMC_1_2V_DDR_MODE;
Hanumath Prasaddfc13e82010-09-30 17:37:23 -04001192 }
1193
1194 /*
Girish K Sa4924c72012-01-11 14:04:52 -05001195 * Indicate HS200 SDR mode (if supported).
1196 */
1197 if (mmc_card_hs200(card)) {
1198 u32 ext_csd_bits;
1199 u32 bus_width = card->host->ios.bus_width;
1200
1201 /*
1202 * For devices supporting HS200 mode, the bus width has
1203 * to be set before executing the tuning function. If
1204 * set before tuning, then device will respond with CRC
1205 * errors for responses on CMD line. So for HS200 the
1206 * sequence will be
1207 * 1. set bus width 4bit / 8 bit (1 bit not supported)
1208 * 2. switch to HS200 mode
1209 * 3. set the clock to > 52Mhz <=200MHz and
1210 * 4. execute tuning for HS200
1211 */
1212 if ((host->caps2 & MMC_CAP2_HS200) &&
Subhash Jadavani52d09742012-03-06 17:59:12 +05301213 card->host->ops->execute_tuning) {
1214 mmc_host_clk_hold(card->host);
Girish K Sa4924c72012-01-11 14:04:52 -05001215 err = card->host->ops->execute_tuning(card->host,
1216 MMC_SEND_TUNING_BLOCK_HS200);
Subhash Jadavani52d09742012-03-06 17:59:12 +05301217 mmc_host_clk_release(card->host);
1218 }
Girish K Sa4924c72012-01-11 14:04:52 -05001219 if (err) {
1220 pr_warning("%s: tuning execution failed\n",
1221 mmc_hostname(card->host));
1222 goto err;
1223 }
1224
1225 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1226 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
1227 err = mmc_select_powerclass(card, ext_csd_bits, ext_csd);
Subhash Jadavani93fc5a42012-04-03 12:25:58 +05301228 if (err)
1229 pr_warning("%s: power class selection to bus width %d"
1230 " failed\n", mmc_hostname(card->host),
1231 1 << bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -05001232 }
1233
1234 /*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001235 * Activate wide bus and DDR (if supported).
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001236 */
Girish K Sa4924c72012-01-11 14:04:52 -05001237 if (!mmc_card_hs200(card) &&
Girish K S7488e922012-01-19 08:56:19 +05301238 (card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001239 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
Aries Lee22113ef2010-12-15 08:14:24 +01001240 static unsigned ext_csd_bits[][2] = {
1241 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
1242 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
1243 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
1244 };
1245 static unsigned bus_widths[] = {
1246 MMC_BUS_WIDTH_8,
1247 MMC_BUS_WIDTH_4,
1248 MMC_BUS_WIDTH_1
1249 };
1250 unsigned idx, bus_width = 0;
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001251
Aries Lee22113ef2010-12-15 08:14:24 +01001252 if (host->caps & MMC_CAP_8_BIT_DATA)
1253 idx = 0;
1254 else
1255 idx = 1;
1256 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1257 bus_width = bus_widths[idx];
1258 if (bus_width == MMC_BUS_WIDTH_1)
1259 ddr = 0; /* no DDR for 1-bit width */
Girish K Sb87d8db2011-09-23 20:41:47 +05301260 err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
1261 ext_csd);
1262 if (err)
Subhash Jadavani93fc5a42012-04-03 12:25:58 +05301263 pr_warning("%s: power class selection to "
1264 "bus width %d failed\n",
1265 mmc_hostname(card->host),
1266 1 << bus_width);
Girish K Sb87d8db2011-09-23 20:41:47 +05301267
Aries Lee22113ef2010-12-15 08:14:24 +01001268 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1269 EXT_CSD_BUS_WIDTH,
Andrei Warkentind3a8d952011-04-11 16:13:43 -05001270 ext_csd_bits[idx][0],
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001271 card->ext_csd.generic_cmd6_time);
Aries Lee22113ef2010-12-15 08:14:24 +01001272 if (!err) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301273 mmc_set_bus_width(card->host, bus_width);
Philip Rakity08ee80c2011-05-24 18:14:58 -07001274
Aries Lee22113ef2010-12-15 08:14:24 +01001275 /*
1276 * If controller can't handle bus width test,
Philip Rakity08ee80c2011-05-24 18:14:58 -07001277 * compare ext_csd previously read in 1 bit mode
1278 * against ext_csd at new bus width
Aries Lee22113ef2010-12-15 08:14:24 +01001279 */
1280 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
Philip Rakity08ee80c2011-05-24 18:14:58 -07001281 err = mmc_compare_ext_csds(card,
Philip Rakity08ee80c2011-05-24 18:14:58 -07001282 bus_width);
1283 else
1284 err = mmc_bus_test(card, bus_width);
Aries Lee22113ef2010-12-15 08:14:24 +01001285 if (!err)
1286 break;
1287 }
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +02001288 }
1289
Aries Lee22113ef2010-12-15 08:14:24 +01001290 if (!err && ddr) {
Girish K Sb87d8db2011-09-23 20:41:47 +05301291 err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
1292 ext_csd);
1293 if (err)
Subhash Jadavani93fc5a42012-04-03 12:25:58 +05301294 pr_warning("%s: power class selection to "
1295 "bus width %d ddr %d failed\n",
1296 mmc_hostname(card->host),
1297 1 << bus_width, ddr);
Girish K Sb87d8db2011-09-23 20:41:47 +05301298
Aries Lee22113ef2010-12-15 08:14:24 +01001299 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Andrei Warkentind3a8d952011-04-11 16:13:43 -05001300 EXT_CSD_BUS_WIDTH,
1301 ext_csd_bits[idx][1],
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001302 card->ext_csd.generic_cmd6_time);
Aries Lee22113ef2010-12-15 08:14:24 +01001303 }
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001304 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301305 pr_warning("%s: switch to bus width %d ddr %d "
Aries Lee22113ef2010-12-15 08:14:24 +01001306 "failed\n", mmc_hostname(card->host),
1307 1 << bus_width, ddr);
1308 goto free_card;
1309 } else if (ddr) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301310 /*
1311 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1312 * signaling.
1313 *
1314 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1315 *
1316 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1317 * in the JEDEC spec for DDR.
1318 *
1319 * Do not force change in vccq since we are obviously
1320 * working and no change to vccq is needed.
1321 *
1322 * WARNING: eMMC rules are NOT the same as SD DDR
1323 */
Girish K S913047e2011-12-15 17:27:42 +05301324 if (ddr == MMC_1_2V_DDR_MODE) {
Philip Rakity4c4cb172011-05-13 11:17:18 +05301325 err = mmc_set_signal_voltage(host,
1326 MMC_SIGNAL_VOLTAGE_120, 0);
1327 if (err)
1328 goto err;
1329 }
Aries Lee22113ef2010-12-15 08:14:24 +01001330 mmc_card_set_ddr_mode(card);
Philip Rakity4c4cb172011-05-13 11:17:18 +05301331 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1332 mmc_set_bus_width(card->host, bus_width);
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001333 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001334 }
1335
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001336 /*
Subhash Jadavani52d09742012-03-06 17:59:12 +05301337 * Enable HPI feature (if supported)
1338 */
1339 if (card->ext_csd.hpi) {
1340 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1341 EXT_CSD_HPI_MGMT, 1,
1342 card->ext_csd.generic_cmd6_time);
1343 if (err && err != -EBADMSG)
1344 goto free_card;
1345 if (err) {
1346 pr_warning("%s: Enabling HPI failed\n",
1347 mmc_hostname(card->host));
1348 err = 0;
1349 } else
1350 card->ext_csd.hpi_en = 1;
1351 }
1352
1353 /*
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001354 * If cache size is higher than 0, this indicates
1355 * the existence of cache and it can be turned on.
1356 */
1357 if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1358 card->ext_csd.cache_size > 0) {
1359 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001360 EXT_CSD_CACHE_CTRL, 1,
1361 card->ext_csd.generic_cmd6_time);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001362 if (err && err != -EBADMSG)
1363 goto free_card;
1364
1365 /*
1366 * Only if no error, cache is turned on successfully.
1367 */
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001368 if (err) {
1369 pr_warning("%s: Cache is supported, "
1370 "but failed to turn on (%d)\n",
1371 mmc_hostname(card->host), err);
1372 card->ext_csd.cache_ctrl = 0;
1373 err = 0;
1374 } else {
1375 card->ext_csd.cache_ctrl = 1;
1376 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001377 }
1378
Seungwon Jeonc70e9662012-09-27 16:12:47 +02001379 if ((host->caps2 & MMC_CAP2_PACKED_WR &&
1380 card->ext_csd.max_packed_writes > 0) ||
1381 (host->caps2 & MMC_CAP2_PACKED_RD &&
1382 card->ext_csd.max_packed_reads > 0)) {
1383 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1384 EXT_CSD_EXP_EVENTS_CTRL,
1385 EXT_CSD_PACKED_EVENT_EN,
1386 card->ext_csd.generic_cmd6_time);
1387 if (err && err != -EBADMSG)
1388 goto free_card;
1389 if (err) {
1390 pr_warning("%s: Enabling packed event failed\n",
1391 mmc_hostname(card->host));
1392 card->ext_csd.packed_event_en = 0;
1393 err = 0;
1394 } else {
1395 card->ext_csd.packed_event_en = 1;
1396 }
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001397
1398 }
1399
1400 if (!oldcard) {
1401 if ((host->caps2 & MMC_CAP2_PACKED_CMD) &&
1402 (card->ext_csd.max_packed_writes > 0)) {
1403 /*
1404 * We would like to keep the statistics in an index
1405 * that equals the num of packed requests
1406 * (1 to max_packed_writes)
1407 */
1408 card->wr_pack_stats.packing_events = kzalloc(
1409 (card->ext_csd.max_packed_writes + 1) *
1410 sizeof(*card->wr_pack_stats.packing_events),
1411 GFP_KERNEL);
1412 if (!card->wr_pack_stats.packing_events)
1413 goto free_card;
1414 }
Maya Erez5f360692012-10-10 03:47:54 +02001415
1416 if (card->ext_csd.bkops_en) {
1417 INIT_DELAYED_WORK(&card->bkops_info.dw,
1418 mmc_start_idle_time_bkops);
Maya Erez5f360692012-10-10 03:47:54 +02001419
1420 /*
1421 * Calculate the time to start the BKOPs checking.
Maya Erez70b00222012-12-27 15:40:17 +02001422 * The host controller can set this time in order to
1423 * prevent a race condition before starting BKOPs
1424 * and going into suspend.
1425 * If the host controller didn't set this time,
Maya Erez5f360692012-10-10 03:47:54 +02001426 * a default value is used.
1427 */
1428 card->bkops_info.delay_ms = MMC_IDLE_BKOPS_TIME_MS;
Maya Erez70b00222012-12-27 15:40:17 +02001429 if (card->bkops_info.host_delay_ms)
1430 card->bkops_info.delay_ms =
1431 card->bkops_info.host_delay_ms;
Maya Erez5f360692012-10-10 03:47:54 +02001432 }
Seungwon Jeonc70e9662012-09-27 16:12:47 +02001433 }
1434
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001435 if (!oldcard)
1436 host->card = card;
1437
Philip Rakity08ee80c2011-05-24 18:14:58 -07001438 mmc_free_ext_csd(ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001439 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001440
1441free_card:
1442 if (!oldcard)
1443 mmc_remove_card(card);
1444err:
Philip Rakity08ee80c2011-05-24 18:14:58 -07001445 mmc_free_ext_csd(ext_csd);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001446
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001447 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001448}
1449
Maya Erezcec02a42012-10-19 20:18:17 +02001450static int mmc_can_poweroff_notify(const struct mmc_card *card)
1451{
1452 return card &&
1453 mmc_card_mmc(card) &&
1454 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1455}
1456
1457static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1458{
1459 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1460 int err;
1461
1462 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1463 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1464 timeout = card->ext_csd.power_off_longtime;
1465
1466 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1467 EXT_CSD_POWER_OFF_NOTIFICATION,
1468 notify_type, timeout);
1469 if (err)
1470 pr_err("%s: Power Off Notification timed out, %u\n",
1471 mmc_hostname(card->host), timeout);
1472
1473 /* Disable the power off notification after the switch operation. */
1474 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1475
1476 return err;
1477}
1478
Tatyana Brokhmand8127382013-05-28 09:45:17 +03001479int mmc_send_long_pon(struct mmc_card *card)
1480{
1481 int err = 0;
1482 struct mmc_host *host = card->host;
1483
1484 mmc_claim_host(host);
1485 if (card->issue_long_pon && mmc_can_poweroff_notify(card)) {
1486 err = mmc_poweroff_notify(host->card, EXT_CSD_POWER_OFF_LONG);
1487 if (err)
1488 pr_warning("%s: error %d sending Long PON",
1489 mmc_hostname(host), err);
1490 }
1491 mmc_release_host(host);
1492 return err;
1493}
1494
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001495/*
1496 * Host is being removed. Free up the current card.
1497 */
1498static void mmc_remove(struct mmc_host *host)
1499{
1500 BUG_ON(!host);
1501 BUG_ON(!host->card);
1502
Tatyana Brokhmand8127382013-05-28 09:45:17 +03001503 unregister_reboot_notifier(&host->card->reboot_notify);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001504 mmc_remove_card(host->card);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001505
1506 mmc_claim_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001507 host->card = NULL;
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301508 mmc_exit_clk_scaling(host);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001509 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001510}
1511
1512/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001513 * Card detection - card is alive.
1514 */
1515static int mmc_alive(struct mmc_host *host)
1516{
1517 return mmc_send_status(host->card, NULL);
1518}
1519
1520/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001521 * Card detection callback from host.
1522 */
1523static void mmc_detect(struct mmc_host *host)
1524{
1525 int err;
1526
1527 BUG_ON(!host);
1528 BUG_ON(!host->card);
1529
Asutosh Dasbbc84782013-02-11 15:31:35 +05301530 mmc_rpm_hold(host, &host->card->dev);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001531 mmc_claim_host(host);
1532
1533 /*
1534 * Just check if our card has been removed.
1535 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001536 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001537
1538 mmc_release_host(host);
1539
Asutosh Dasbbc84782013-02-11 15:31:35 +05301540 /*
1541 * if detect fails, the device would be removed anyway;
1542 * the rpm framework would mark the device state suspended.
1543 */
1544 if (!err)
1545 mmc_rpm_release(host, &host->card->dev);
1546
Pierre Ossman17b04292007-07-22 22:18:46 +02001547 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001548 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001549
1550 mmc_claim_host(host);
1551 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001552 mmc_power_off(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001553 mmc_release_host(host);
1554 }
1555}
1556
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001557/*
1558 * Suspend callback from host.
1559 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001560static int mmc_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001561{
Balaji T Kc3805462011-09-08 22:08:39 +05301562 int err = 0;
1563
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001564 BUG_ON(!host);
1565 BUG_ON(!host->card);
1566
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301567 /*
1568 * Disable clock scaling before suspend and enable it after resume so
1569 * as to avoid clock scaling decisions kicking in during this window.
1570 */
1571 mmc_disable_clk_scaling(host);
1572
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001573 mmc_claim_host(host);
Maya Erez67a51382012-12-31 16:17:58 +02001574
1575 err = mmc_cache_ctrl(host, 0);
1576 if (err)
1577 goto out;
1578
Maya Erezcec02a42012-10-19 20:18:17 +02001579 if (mmc_can_poweroff_notify(host->card))
1580 err = mmc_poweroff_notify(host->card, EXT_CSD_POWER_OFF_SHORT);
1581 else if (mmc_card_can_sleep(host))
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02001582 err = mmc_card_sleep(host);
Maya Erezcec02a42012-10-19 20:18:17 +02001583 else if (!mmc_host_is_spi(host))
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02001584 mmc_deselect_cards(host);
1585 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001586
Maya Erez67a51382012-12-31 16:17:58 +02001587out:
1588 mmc_release_host(host);
Balaji T Kc3805462011-09-08 22:08:39 +05301589 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001590}
1591
1592/*
1593 * Resume callback from host.
1594 *
1595 * This function tries to determine if the same card is still present
1596 * and, if so, restore all state to it.
1597 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001598static int mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001599{
1600 int err;
1601
1602 BUG_ON(!host);
1603 BUG_ON(!host->card);
1604
1605 mmc_claim_host(host);
Krishna Konda07d1f382012-08-11 16:52:41 -07001606 err = mmc_init_card(host, host->ocr, host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001607 mmc_release_host(host);
1608
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301609 /*
1610 * We have done full initialization of the card,
1611 * reset the clk scale stats and current frequency.
1612 */
1613 if (mmc_can_scale_clk(host))
1614 mmc_init_clk_scaling(host);
1615
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001616 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001617}
1618
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001619static int mmc_power_restore(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001620{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001621 int ret;
1622
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301623 /* Disable clk scaling to avoid switching frequencies intermittently */
1624 mmc_disable_clk_scaling(host);
1625
Girish K S3e73c362012-01-31 15:44:03 +05301626 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001627 mmc_claim_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001628 ret = mmc_init_card(host, host->ocr, host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001629 mmc_release_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001630
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301631 if (mmc_can_scale_clk(host))
1632 mmc_init_clk_scaling(host);
1633
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001634 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001635}
1636
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001637static int mmc_sleep(struct mmc_host *host)
1638{
1639 struct mmc_card *card = host->card;
1640 int err = -ENOSYS;
1641
1642 if (card && card->ext_csd.rev >= 3) {
1643 err = mmc_card_sleepawake(host, 1);
1644 if (err < 0)
Sujit Reddy Thummaf43ab432012-06-19 14:18:27 +05301645 pr_warn("%s: Error %d while putting card into sleep",
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001646 mmc_hostname(host), err);
1647 }
1648
1649 return err;
1650}
1651
1652static int mmc_awake(struct mmc_host *host)
1653{
1654 struct mmc_card *card = host->card;
1655 int err = -ENOSYS;
1656
1657 if (card && card->ext_csd.rev >= 3) {
1658 err = mmc_card_sleepawake(host, 0);
1659 if (err < 0)
1660 pr_debug("%s: Error %d while awaking sleeping card",
1661 mmc_hostname(host), err);
1662 }
1663
1664 return err;
1665}
1666
Adrian Hunter9feae242009-09-22 16:44:32 -07001667static const struct mmc_bus_ops mmc_ops = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001668 .awake = mmc_awake,
1669 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -07001670 .remove = mmc_remove,
1671 .detect = mmc_detect,
1672 .suspend = NULL,
1673 .resume = NULL,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001674 .power_restore = mmc_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001675 .alive = mmc_alive,
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +05301676 .change_bus_speed = mmc_change_bus_speed,
Adrian Hunter9feae242009-09-22 16:44:32 -07001677};
1678
1679static const struct mmc_bus_ops mmc_ops_unsafe = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001680 .awake = mmc_awake,
1681 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -07001682 .remove = mmc_remove,
1683 .detect = mmc_detect,
1684 .suspend = mmc_suspend,
1685 .resume = mmc_resume,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001686 .power_restore = mmc_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001687 .alive = mmc_alive,
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +05301688 .change_bus_speed = mmc_change_bus_speed,
Adrian Hunter9feae242009-09-22 16:44:32 -07001689};
1690
1691static void mmc_attach_bus_ops(struct mmc_host *host)
1692{
1693 const struct mmc_bus_ops *bus_ops;
1694
Matt Fleming71d7d3d2010-09-27 09:42:19 +01001695 if (!mmc_card_is_removable(host))
Adrian Hunter9feae242009-09-22 16:44:32 -07001696 bus_ops = &mmc_ops_unsafe;
1697 else
1698 bus_ops = &mmc_ops;
1699 mmc_attach_bus(host, bus_ops);
1700}
1701
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001702/*
1703 * Starting point for MMC card init.
1704 */
Andy Ross807e8e42011-01-03 10:36:56 -08001705int mmc_attach_mmc(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001706{
1707 int err;
Andy Ross807e8e42011-01-03 10:36:56 -08001708 u32 ocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001709
1710 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001711 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001712
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001713 /* Set correct bus mode for MMC before attempting attach */
1714 if (!mmc_host_is_spi(host))
1715 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1716
Andy Ross807e8e42011-01-03 10:36:56 -08001717 err = mmc_send_op_cond(host, 0, &ocr);
1718 if (err)
1719 return err;
1720
Adrian Hunter9feae242009-09-22 16:44:32 -07001721 mmc_attach_bus_ops(host);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001722 if (host->ocr_avail_mmc)
1723 host->ocr_avail = host->ocr_avail_mmc;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001724
1725 /*
David Brownellaf517152007-08-08 09:11:32 -07001726 * We need to get OCR a different way for SPI.
1727 */
1728 if (mmc_host_is_spi(host)) {
1729 err = mmc_spi_read_ocr(host, 1, &ocr);
1730 if (err)
1731 goto err;
1732 }
1733
1734 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001735 * Sanity check the voltages that the card claims to
1736 * support.
1737 */
1738 if (ocr & 0x7F) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301739 pr_warning("%s: card claims to support voltages "
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001740 "below the defined range. These will be ignored.\n",
1741 mmc_hostname(host));
1742 ocr &= ~0x7F;
1743 }
1744
1745 host->ocr = mmc_select_voltage(host, ocr);
1746
1747 /*
1748 * Can we support the voltage of the card?
1749 */
Pierre Ossman109b5be2007-07-23 00:12:10 +02001750 if (!host->ocr) {
1751 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001752 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001753 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001754
1755 /*
1756 * Detect and init the card.
1757 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001758 err = mmc_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001759 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001760 goto err;
1761
1762 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001763 err = mmc_add_card(host->card);
Andy Ross807e8e42011-01-03 10:36:56 -08001764 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001765 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001766 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001767
Sujit Reddy Thumma1ed78ab2013-04-25 12:39:40 +05301768 mmc_init_clk_scaling(host);
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301769
Tatyana Brokhmand8127382013-05-28 09:45:17 +03001770 register_reboot_notifier(&host->card->reboot_notify);
1771
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001772 return 0;
1773
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001774remove_card:
Andy Ross807e8e42011-01-03 10:36:56 -08001775 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001776 mmc_remove_card(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001777 mmc_claim_host(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001778 host->card = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001779err:
1780 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001781
Girish K Sa3c76eb2011-10-11 11:44:09 +05301782 pr_err("%s: error %d whilst initialising MMC card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02001783 mmc_hostname(host), err);
1784
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001785 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001786}