blob: bec6786efd19a3f45873d33d99254078d6e90b62 [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>
Ulf Hansson0cb403a2013-10-10 14:20:05 +020016#include <linux/pm_runtime.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010017
18#include <linux/mmc/host.h>
19#include <linux/mmc/card.h>
20#include <linux/mmc/mmc.h>
21
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);
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800100 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100101 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 Jeon96cf5f02012-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;
Lee Jones5f1a4dd2012-11-14 12:35:51 +0000244 u32 caps = host->caps, caps2 = host->caps2;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900245 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900246 unsigned int avail_type = 0;
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900247
248 if (caps & MMC_CAP_MMC_HIGHSPEED &&
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900249 card_type & EXT_CSD_CARD_TYPE_HS_26) {
250 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
251 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
252 }
253
254 if (caps & MMC_CAP_MMC_HIGHSPEED &&
255 card_type & EXT_CSD_CARD_TYPE_HS_52) {
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900256 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900257 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
258 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900259
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900260 if (caps & MMC_CAP_1_8V_DDR &&
261 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900262 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900263 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
264 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900265
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900266 if (caps & MMC_CAP_1_2V_DDR &&
267 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
268 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
269 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
270 }
271
272 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
273 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
Seungwon Jeon577fb132014-04-23 17:08:44 +0900274 hs200_max_dtr = MMC_HS200_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900275 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
276 }
277
278 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
279 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
Seungwon Jeon577fb132014-04-23 17:08:44 +0900280 hs200_max_dtr = MMC_HS200_MAX_DTR;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900281 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
282 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900283
284 card->ext_csd.hs_max_dtr = hs_max_dtr;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900285 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
Seungwon Jeon2415c0e2014-04-23 17:07:58 +0900286 card->mmc_avail_type = avail_type;
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900287}
288
Philip Rakity08ee80c2011-05-24 18:14:58 -0700289/*
290 * Decode extended CSD.
291 */
292static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
293{
Namjae Jeone0c368d2011-10-06 23:41:38 +0900294 int err = 0, idx;
295 unsigned int part_size;
296 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700297
298 BUG_ON(!card);
299
300 if (!ext_csd)
301 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100302
Kyungmin Park6da24b72010-08-10 18:01:36 -0700303 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700304 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
Kyungmin Park6da24b72010-08-10 18:01:36 -0700305 if (card->csd.structure == 3) {
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700306 if (card->ext_csd.raw_ext_csd_structure > 2) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530307 pr_err("%s: unrecognised EXT_CSD structure "
Kyungmin Park6da24b72010-08-10 18:01:36 -0700308 "version %d\n", mmc_hostname(card->host),
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700309 card->ext_csd.raw_ext_csd_structure);
Kyungmin Park6da24b72010-08-10 18:01:36 -0700310 err = -EINVAL;
311 goto out;
312 }
313 }
314
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700315 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Yuvaraj Kumar C D6636bad2013-05-21 15:08:43 +0530316 if (card->ext_csd.rev > 7) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530317 pr_err("%s: unrecognised EXT_CSD revision %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700318 mmc_hostname(card->host), card->ext_csd.rev);
Florin Malita00cedfa2007-10-13 12:27:20 -0400319 err = -EINVAL;
320 goto out;
Pierre Ossmand7604d72007-07-23 00:34:07 +0200321 }
322
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700323 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
324 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
325 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
326 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700327 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200328 card->ext_csd.sectors =
329 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
330 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
331 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
332 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700333
334 /* Cards with density > 2GiB are sector addressed */
335 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200336 mmc_card_set_blockaddr(card);
337 }
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900338
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700339 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
Seungwon Jeon96cf5f02012-04-25 16:17:37 +0900340 mmc_select_card_type(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100341
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700342 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
343 card->ext_csd.raw_erase_timeout_mult =
344 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
345 card->ext_csd.raw_hc_erase_grp_size =
346 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700347 if (card->ext_csd.rev >= 3) {
348 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500349 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
350
351 /* EXT_CSD value is in units of 10ms, but we store in ms */
352 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700353
354 /* Sleep / awake timeout in 100ns units */
355 if (sa_shift > 0 && sa_shift <= 0x17)
356 card->ext_csd.sa_timeout =
357 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700358 card->ext_csd.erase_group_def =
359 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
360 card->ext_csd.hc_erase_timeout = 300 *
361 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
362 card->ext_csd.hc_erase_size =
363 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500364
365 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
Andrei Warkentin371a6892011-04-11 18:10:25 -0500366
367 /*
368 * There are two boot regions of equal size, defined in
369 * multiples of 128K.
370 */
Namjae Jeone0c368d2011-10-06 23:41:38 +0900371 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
372 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
373 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
374 mmc_part_add(card, part_size,
375 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100376 "boot%d", idx, true,
377 MMC_BLK_DATA_AREA_BOOT);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900378 }
379 }
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700380 }
381
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700382 card->ext_csd.raw_hc_erase_gap_size =
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100383 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700384 card->ext_csd.raw_sec_trim_mult =
385 ext_csd[EXT_CSD_SEC_TRIM_MULT];
386 card->ext_csd.raw_sec_erase_mult =
387 ext_csd[EXT_CSD_SEC_ERASE_MULT];
388 card->ext_csd.raw_sec_feature_support =
389 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
390 card->ext_csd.raw_trim_mult =
391 ext_csd[EXT_CSD_TRIM_MULT];
Philip Rakity836dc2f2013-04-04 20:18:11 +0100392 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700393 if (card->ext_csd.rev >= 4) {
Chuanxiao Dong709de992011-01-22 04:09:41 +0800394 /*
395 * Enhanced area feature support -- check whether the eMMC
396 * card has the Enhanced area enabled. If so, export enhanced
397 * area offset and size to user by adding sysfs interface.
398 */
399 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700400 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
Namjae Jeone0c368d2011-10-06 23:41:38 +0900401 hc_erase_grp_sz =
Chuanxiao Dong709de992011-01-22 04:09:41 +0800402 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
Namjae Jeone0c368d2011-10-06 23:41:38 +0900403 hc_wp_grp_sz =
Chuanxiao Dong709de992011-01-22 04:09:41 +0800404 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
405
406 card->ext_csd.enhanced_area_en = 1;
407 /*
408 * calculate the enhanced data area offset, in bytes
409 */
410 card->ext_csd.enhanced_area_offset =
411 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
412 (ext_csd[137] << 8) + ext_csd[136];
413 if (mmc_card_blockaddr(card))
414 card->ext_csd.enhanced_area_offset <<= 9;
415 /*
416 * calculate the enhanced data area size, in kilobytes
417 */
418 card->ext_csd.enhanced_area_size =
419 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
420 ext_csd[140];
421 card->ext_csd.enhanced_area_size *=
422 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
423 card->ext_csd.enhanced_area_size <<= 9;
424 } else {
425 /*
426 * If the enhanced area is not enabled, disable these
427 * device attributes.
428 */
429 card->ext_csd.enhanced_area_offset = -EINVAL;
430 card->ext_csd.enhanced_area_size = -EINVAL;
431 }
Namjae Jeone0c368d2011-10-06 23:41:38 +0900432
433 /*
434 * General purpose partition feature support --
435 * If ext_csd has the size of general purpose partitions,
436 * set size, part_cfg, partition name in mmc_part.
437 */
438 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
439 EXT_CSD_PART_SUPPORT_PART_EN) {
440 if (card->ext_csd.enhanced_area_en != 1) {
441 hc_erase_grp_sz =
442 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
443 hc_wp_grp_sz =
444 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
445
446 card->ext_csd.enhanced_area_en = 1;
447 }
448
449 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
450 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
451 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
452 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
453 continue;
454 part_size =
455 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
456 << 16) +
457 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
458 << 8) +
459 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
460 part_size *= (size_t)(hc_erase_grp_sz *
461 hc_wp_grp_sz);
462 mmc_part_add(card, part_size << 19,
463 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
Johan Rudholmadd710e2011-12-02 08:51:06 +0100464 "gp%d", idx, false,
465 MMC_BLK_DATA_AREA_GP);
Namjae Jeone0c368d2011-10-06 23:41:38 +0900466 }
467 }
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700468 card->ext_csd.sec_trim_mult =
469 ext_csd[EXT_CSD_SEC_TRIM_MULT];
470 card->ext_csd.sec_erase_mult =
471 ext_csd[EXT_CSD_SEC_ERASE_MULT];
472 card->ext_csd.sec_feature_support =
473 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
474 card->ext_csd.trim_timeout = 300 *
475 ext_csd[EXT_CSD_TRIM_MULT];
Johan Rudholmadd710e2011-12-02 08:51:06 +0100476
477 /*
478 * Note that the call to mmc_part_add above defaults to read
479 * only. If this default assumption is changed, the call must
480 * take into account the value of boot_locked below.
481 */
482 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
483 card->ext_csd.boot_ro_lockable = true;
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200484
485 /* Save power class values */
486 card->ext_csd.raw_pwr_cl_52_195 =
487 ext_csd[EXT_CSD_PWR_CL_52_195];
488 card->ext_csd.raw_pwr_cl_26_195 =
489 ext_csd[EXT_CSD_PWR_CL_26_195];
490 card->ext_csd.raw_pwr_cl_52_360 =
491 ext_csd[EXT_CSD_PWR_CL_52_360];
492 card->ext_csd.raw_pwr_cl_26_360 =
493 ext_csd[EXT_CSD_PWR_CL_26_360];
494 card->ext_csd.raw_pwr_cl_200_195 =
495 ext_csd[EXT_CSD_PWR_CL_200_195];
496 card->ext_csd.raw_pwr_cl_200_360 =
497 ext_csd[EXT_CSD_PWR_CL_200_360];
498 card->ext_csd.raw_pwr_cl_ddr_52_195 =
499 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
500 card->ext_csd.raw_pwr_cl_ddr_52_360 =
501 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700502 }
503
Adrian Hunterb2499512011-08-29 16:42:11 +0300504 if (card->ext_csd.rev >= 5) {
Romain Izard7c4f10a2013-06-14 14:25:44 +0200505 /* Adjust production date as per JEDEC JESD84-B451 */
506 if (card->cid.year < 2010)
507 card->cid.year += 16;
508
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000509 /* check whether the eMMC card supports BKOPS */
510 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
511 card->ext_csd.bkops = 1;
512 card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
513 card->ext_csd.raw_bkops_status =
514 ext_csd[EXT_CSD_BKOPS_STATUS];
515 if (!card->ext_csd.bkops_en)
516 pr_info("%s: BKOPS_EN bit is not set\n",
517 mmc_hostname(card->host));
518 }
519
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400520 /* check whether the eMMC card supports HPI */
521 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
522 card->ext_csd.hpi = 1;
523 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
524 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
525 else
526 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
527 /*
528 * Indicate the maximum timeout to close
529 * a command interrupted by HPI
530 */
531 card->ext_csd.out_of_int_time =
532 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
533 }
534
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500535 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
Adrian Hunterb2499512011-08-29 16:42:11 +0300536 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
Loic Pallardy090d25f2012-11-17 18:08:24 -0500537
538 /*
539 * RPMB regions are defined in multiples of 128K.
540 */
541 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
Balaji T Kd0123cc2013-01-25 17:00:30 +0530542 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
Loic Pallardy090d25f2012-11-17 18:08:24 -0500543 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
544 EXT_CSD_PART_CONFIG_ACC_RPMB,
545 "rpmb", 0, false,
546 MMC_BLK_DATA_AREA_RPMB);
547 }
Adrian Hunterb2499512011-08-29 16:42:11 +0300548 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500549
Andrei Warkentin5238acb2011-09-24 12:12:30 -0400550 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700551 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
552 card->erased_byte = 0xFF;
553 else
554 card->erased_byte = 0x0;
555
Seungwon Jeon336c7162011-10-25 09:43:12 +0900556 /* eMMC v4.5 or later */
Girish K Sbec87262011-10-13 12:04:16 +0530557 if (card->ext_csd.rev >= 6) {
Seungwon Jeon336c7162011-10-25 09:43:12 +0900558 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
559
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900560 card->ext_csd.generic_cmd6_time = 10 *
561 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
Girish K Sbec87262011-10-13 12:04:16 +0530562 card->ext_csd.power_off_longtime = 10 *
563 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +0900564
Seungwon Jeon336c7162011-10-25 09:43:12 +0900565 card->ext_csd.cache_size =
566 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
567 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
568 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
569 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
Saugata Das42659002011-12-21 13:09:17 +0530570
571 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
572 card->ext_csd.data_sector_size = 4096;
573 else
574 card->ext_csd.data_sector_size = 512;
575
576 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
577 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
578 card->ext_csd.data_tag_unit_size =
579 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
580 (card->ext_csd.data_sector_size);
581 } else {
582 card->ext_csd.data_tag_unit_size = 0;
583 }
Seungwon Jeonabd9ac12013-02-06 17:01:43 +0900584
585 card->ext_csd.max_packed_writes =
586 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
587 card->ext_csd.max_packed_reads =
588 ext_csd[EXT_CSD_MAX_PACKED_READS];
Saugata Dasa5075eb2012-05-17 16:32:21 +0530589 } else {
590 card->ext_csd.data_sector_size = 512;
Seungwon Jeon336c7162011-10-25 09:43:12 +0900591 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900592
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100593out:
Philip Rakity08ee80c2011-05-24 18:14:58 -0700594 return err;
595}
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100596
Philip Rakity08ee80c2011-05-24 18:14:58 -0700597static inline void mmc_free_ext_csd(u8 *ext_csd)
598{
599 kfree(ext_csd);
600}
601
602
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700603static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
Philip Rakity08ee80c2011-05-24 18:14:58 -0700604{
605 u8 *bw_ext_csd;
606 int err;
607
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700608 if (bus_width == MMC_BUS_WIDTH_1)
609 return 0;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700610
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700611 err = mmc_get_ext_csd(card, &bw_ext_csd);
612
613 if (err || bw_ext_csd == NULL) {
Philippe De Swertf6f44592012-04-11 23:32:05 +0300614 err = -EINVAL;
Philip Rakity08ee80c2011-05-24 18:14:58 -0700615 goto out;
616 }
617
Philip Rakity08ee80c2011-05-24 18:14:58 -0700618 /* only compare read only fields */
Jurgen Heeksdd13b4e2012-02-01 13:30:55 +0100619 err = !((card->ext_csd.raw_partition_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700620 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700621 (card->ext_csd.raw_erased_mem_count ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700622 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700623 (card->ext_csd.rev ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700624 bw_ext_csd[EXT_CSD_REV]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700625 (card->ext_csd.raw_ext_csd_structure ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700626 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700627 (card->ext_csd.raw_card_type ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700628 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700629 (card->ext_csd.raw_s_a_timeout ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700630 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700631 (card->ext_csd.raw_hc_erase_gap_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700632 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700633 (card->ext_csd.raw_erase_timeout_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700634 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700635 (card->ext_csd.raw_hc_erase_grp_size ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700636 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700637 (card->ext_csd.raw_sec_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700638 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700639 (card->ext_csd.raw_sec_erase_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700640 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700641 (card->ext_csd.raw_sec_feature_support ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700642 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700643 (card->ext_csd.raw_trim_mult ==
Philip Rakity08ee80c2011-05-24 18:14:58 -0700644 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
Philip Rakityf39b2dd92011-07-07 09:04:55 -0700645 (card->ext_csd.raw_sectors[0] ==
646 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
647 (card->ext_csd.raw_sectors[1] ==
648 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
649 (card->ext_csd.raw_sectors[2] ==
650 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
651 (card->ext_csd.raw_sectors[3] ==
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200652 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
653 (card->ext_csd.raw_pwr_cl_52_195 ==
654 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
655 (card->ext_csd.raw_pwr_cl_26_195 ==
656 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
657 (card->ext_csd.raw_pwr_cl_52_360 ==
658 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
659 (card->ext_csd.raw_pwr_cl_26_360 ==
660 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
661 (card->ext_csd.raw_pwr_cl_200_195 ==
662 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
663 (card->ext_csd.raw_pwr_cl_200_360 ==
664 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
665 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
666 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
667 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
668 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]));
Philip Rakity08ee80c2011-05-24 18:14:58 -0700669 if (err)
670 err = -EINVAL;
671
672out:
673 mmc_free_ext_csd(bw_ext_csd);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100674 return err;
675}
676
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100677MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
678 card->raw_cid[2], card->raw_cid[3]);
679MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
680 card->raw_csd[2], card->raw_csd[3]);
681MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700682MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
683MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100684MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
685MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
686MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
687MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
688MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800689MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100690MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Chuanxiao Dong709de992011-01-22 04:09:41 +0800691MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
692 card->ext_csd.enhanced_area_offset);
693MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
Loic Pallardy188cc042012-08-06 17:12:29 +0200694MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
695MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100696
697static struct attribute *mmc_std_attrs[] = {
698 &dev_attr_cid.attr,
699 &dev_attr_csd.attr,
700 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700701 &dev_attr_erase_size.attr,
702 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100703 &dev_attr_fwrev.attr,
704 &dev_attr_hwrev.attr,
705 &dev_attr_manfid.attr,
706 &dev_attr_name.attr,
707 &dev_attr_oemid.attr,
Bernie Thompson51e7e8b2013-02-27 12:19:17 -0800708 &dev_attr_prv.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100709 &dev_attr_serial.attr,
Chuanxiao Dong709de992011-01-22 04:09:41 +0800710 &dev_attr_enhanced_area_offset.attr,
711 &dev_attr_enhanced_area_size.attr,
Loic Pallardy188cc042012-08-06 17:12:29 +0200712 &dev_attr_raw_rpmb_size_mult.attr,
713 &dev_attr_rel_sectors.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100714 NULL,
715};
Axel Lind1e58212014-03-08 15:05:27 +0800716ATTRIBUTE_GROUPS(mmc_std);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100717
718static struct device_type mmc_type = {
Axel Lind1e58212014-03-08 15:05:27 +0800719 .groups = mmc_std_groups,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100720};
721
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100722/*
Girish K Sb87d8db2011-09-23 20:41:47 +0530723 * Select the PowerClass for the current bus width
724 * If power class is defined for 4/8 bit bus in the
725 * extended CSD register, select it by executing the
726 * mmc_switch command.
727 */
Seungwon Jeon23850492014-04-23 17:08:05 +0900728static int __mmc_select_powerclass(struct mmc_card *card,
729 unsigned int bus_width)
Girish K Sb87d8db2011-09-23 20:41:47 +0530730{
Seungwon Jeon23850492014-04-23 17:08:05 +0900731 struct mmc_host *host = card->host;
732 struct mmc_ext_csd *ext_csd = &card->ext_csd;
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200733 unsigned int pwrclass_val = 0;
Seungwon Jeon23850492014-04-23 17:08:05 +0900734 int err = 0;
Girish K Sb87d8db2011-09-23 20:41:47 +0530735
Girish K Sb87d8db2011-09-23 20:41:47 +0530736 /* Power class selection is supported for versions >= 4.0 */
737 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
738 return 0;
739
740 /* Power class values are defined only for 4/8 bit bus */
741 if (bus_width == EXT_CSD_BUS_WIDTH_1)
742 return 0;
743
744 switch (1 << host->ios.vdd) {
745 case MMC_VDD_165_195:
Seungwon Jeon23850492014-04-23 17:08:05 +0900746 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
747 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
748 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200749 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
Seungwon Jeon23850492014-04-23 17:08:05 +0900750 ext_csd->raw_pwr_cl_52_195 :
751 ext_csd->raw_pwr_cl_ddr_52_195;
752 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
753 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
Girish K Sb87d8db2011-09-23 20:41:47 +0530754 break;
Subhash Jadavani93fc5a42012-04-03 12:25:58 +0530755 case MMC_VDD_27_28:
756 case MMC_VDD_28_29:
757 case MMC_VDD_29_30:
758 case MMC_VDD_30_31:
759 case MMC_VDD_31_32:
Girish K Sb87d8db2011-09-23 20:41:47 +0530760 case MMC_VDD_32_33:
761 case MMC_VDD_33_34:
762 case MMC_VDD_34_35:
763 case MMC_VDD_35_36:
Seungwon Jeon23850492014-04-23 17:08:05 +0900764 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
765 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
766 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
Fredrik Soderstedt60443712013-04-23 16:27:07 +0200767 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
Seungwon Jeon23850492014-04-23 17:08:05 +0900768 ext_csd->raw_pwr_cl_52_360 :
769 ext_csd->raw_pwr_cl_ddr_52_360;
770 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
771 pwrclass_val = ext_csd->raw_pwr_cl_200_360;
Girish K Sb87d8db2011-09-23 20:41:47 +0530772 break;
773 default:
774 pr_warning("%s: Voltage range not supported "
775 "for power class.\n", mmc_hostname(host));
776 return -EINVAL;
777 }
778
Girish K Sb87d8db2011-09-23 20:41:47 +0530779 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
780 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
781 EXT_CSD_PWR_CL_8BIT_SHIFT;
782 else
783 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
784 EXT_CSD_PWR_CL_4BIT_SHIFT;
785
786 /* If the power class is different from the default value */
787 if (pwrclass_val > 0) {
788 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
789 EXT_CSD_POWER_CLASS,
790 pwrclass_val,
Seungwon Jeon71fe3eb2011-10-18 13:20:57 +0900791 card->ext_csd.generic_cmd6_time);
Girish K Sb87d8db2011-09-23 20:41:47 +0530792 }
793
794 return err;
795}
796
Seungwon Jeon23850492014-04-23 17:08:05 +0900797static int mmc_select_powerclass(struct mmc_card *card)
798{
799 struct mmc_host *host = card->host;
800 u32 bus_width, ext_csd_bits;
801 int err, ddr;
802
803 /* Power class selection is supported for versions >= 4.0 */
804 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
805 return 0;
806
807 bus_width = host->ios.bus_width;
808 /* Power class values are defined only for 4/8 bit bus */
809 if (bus_width == MMC_BUS_WIDTH_1)
810 return 0;
811
812 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
813 if (ddr)
814 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
815 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
816 else
817 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
818 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
819
820 err = __mmc_select_powerclass(card, ext_csd_bits);
821 if (err)
822 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
823 mmc_hostname(host), 1 << bus_width, ddr);
824
825 return err;
826}
827
Girish K Sb87d8db2011-09-23 20:41:47 +0530828/*
Seungwon Jeon577fb132014-04-23 17:08:44 +0900829 * Set the bus speed for the selected speed mode.
Girish K Sa4924c72012-01-11 14:04:52 -0500830 */
Seungwon Jeon577fb132014-04-23 17:08:44 +0900831static void mmc_set_bus_speed(struct mmc_card *card)
Girish K Sa4924c72012-01-11 14:04:52 -0500832{
Seungwon Jeon577fb132014-04-23 17:08:44 +0900833 unsigned int max_dtr = (unsigned int)-1;
834
835 if (mmc_card_hs200(card) && max_dtr > card->ext_csd.hs200_max_dtr)
836 max_dtr = card->ext_csd.hs200_max_dtr;
837 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
838 max_dtr = card->ext_csd.hs_max_dtr;
839 else if (max_dtr > card->csd.max_dtr)
840 max_dtr = card->csd.max_dtr;
841
842 mmc_set_clock(card->host, max_dtr);
843}
844
845/*
846 * Select the bus width amoung 4-bit and 8-bit(SDR).
847 * If the bus width is changed successfully, return the selected width value.
848 * Zero is returned instead of error value if the wide width is not supported.
849 */
850static int mmc_select_bus_width(struct mmc_card *card)
851{
Girish K Sa4924c72012-01-11 14:04:52 -0500852 static unsigned ext_csd_bits[] = {
Girish K Sa4924c72012-01-11 14:04:52 -0500853 EXT_CSD_BUS_WIDTH_8,
Seungwon Jeon577fb132014-04-23 17:08:44 +0900854 EXT_CSD_BUS_WIDTH_4,
Girish K Sa4924c72012-01-11 14:04:52 -0500855 };
856 static unsigned bus_widths[] = {
Girish K Sa4924c72012-01-11 14:04:52 -0500857 MMC_BUS_WIDTH_8,
Seungwon Jeon577fb132014-04-23 17:08:44 +0900858 MMC_BUS_WIDTH_4,
Girish K Sa4924c72012-01-11 14:04:52 -0500859 };
Seungwon Jeon577fb132014-04-23 17:08:44 +0900860 struct mmc_host *host = card->host;
861 unsigned idx, bus_width = 0;
862 int err = 0;
Girish K Sa4924c72012-01-11 14:04:52 -0500863
Seungwon Jeon577fb132014-04-23 17:08:44 +0900864 if ((card->csd.mmca_vsn < CSD_SPEC_VER_4) &&
865 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
866 return 0;
Girish K Sa4924c72012-01-11 14:04:52 -0500867
Seungwon Jeon577fb132014-04-23 17:08:44 +0900868 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
Girish K Sa4924c72012-01-11 14:04:52 -0500869
870 /*
871 * Unlike SD, MMC cards dont have a configuration register to notify
872 * supported bus width. So bus test command should be run to identify
873 * the supported bus width or compare the ext csd values of current
874 * bus width and ext csd values of 1 bit mode read earlier.
875 */
Seungwon Jeon577fb132014-04-23 17:08:44 +0900876 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
Girish K Sa4924c72012-01-11 14:04:52 -0500877 /*
878 * Host is capable of 8bit transfer, then switch
879 * the device to work in 8bit transfer mode. If the
880 * mmc switch command returns error then switch to
881 * 4bit transfer mode. On success set the corresponding
882 * bus width on the host.
883 */
884 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
885 EXT_CSD_BUS_WIDTH,
886 ext_csd_bits[idx],
887 card->ext_csd.generic_cmd6_time);
888 if (err)
889 continue;
890
Seungwon Jeon577fb132014-04-23 17:08:44 +0900891 bus_width = bus_widths[idx];
892 mmc_set_bus_width(host, bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -0500893
Seungwon Jeon577fb132014-04-23 17:08:44 +0900894 /*
895 * If controller can't handle bus width test,
896 * compare ext_csd previously read in 1 bit mode
897 * against ext_csd at new bus width
898 */
Girish K Sa4924c72012-01-11 14:04:52 -0500899 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
Seungwon Jeon577fb132014-04-23 17:08:44 +0900900 err = mmc_compare_ext_csds(card, bus_width);
Girish K Sa4924c72012-01-11 14:04:52 -0500901 else
Seungwon Jeon577fb132014-04-23 17:08:44 +0900902 err = mmc_bus_test(card, bus_width);
903
904 if (!err) {
905 err = bus_width;
Girish K Sa4924c72012-01-11 14:04:52 -0500906 break;
Seungwon Jeon577fb132014-04-23 17:08:44 +0900907 } else {
908 pr_warn("%s: switch to bus width %d failed\n",
909 mmc_hostname(host), ext_csd_bits[idx]);
910 }
Girish K Sa4924c72012-01-11 14:04:52 -0500911 }
912
Seungwon Jeon577fb132014-04-23 17:08:44 +0900913 return err;
914}
915
916/*
917 * Switch to the high-speed mode
918 */
919static int mmc_select_hs(struct mmc_card *card)
920{
921 int err;
922
923 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
924 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
925 card->ext_csd.generic_cmd6_time,
926 true, true, true);
Girish K Sa4924c72012-01-11 14:04:52 -0500927 if (!err)
Seungwon Jeon577fb132014-04-23 17:08:44 +0900928 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
929
930 return err;
931}
932
933/*
934 * Activate wide bus and DDR if supported.
935 */
936static int mmc_select_hs_ddr(struct mmc_card *card)
937{
938 struct mmc_host *host = card->host;
939 u32 bus_width, ext_csd_bits;
940 int err = 0;
941
942 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
943 return 0;
944
945 bus_width = host->ios.bus_width;
946 if (bus_width == MMC_BUS_WIDTH_1)
947 return 0;
948
949 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
950 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
951
952 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
953 EXT_CSD_BUS_WIDTH,
954 ext_csd_bits,
955 card->ext_csd.generic_cmd6_time);
956 if (err) {
957 pr_warn("%s: switch to bus width %d ddr failed\n",
958 mmc_hostname(host), 1 << bus_width);
959 return err;
960 }
961
962 /*
963 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
964 * signaling.
965 *
966 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
967 *
968 * 1.8V vccq at 3.3V core voltage (vcc) is not required
969 * in the JEDEC spec for DDR.
970 *
971 * Do not force change in vccq since we are obviously
972 * working and no change to vccq is needed.
973 *
974 * WARNING: eMMC rules are NOT the same as SD DDR
975 */
976 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
977 err = __mmc_set_signal_voltage(host,
978 MMC_SIGNAL_VOLTAGE_120);
979 if (err)
980 return err;
981 }
982
983 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
984
985 return err;
986}
987
988/*
989 * For device supporting HS200 mode, the following sequence
990 * should be done before executing the tuning process.
991 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
992 * 2. switch to HS200 mode
993 * 3. set the clock to > 52Mhz and <=200MHz
994 */
995static int mmc_select_hs200(struct mmc_card *card)
996{
997 struct mmc_host *host = card->host;
998 int err = -EINVAL;
999
1000 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1001 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1002
1003 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1004 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1005
1006 /* If fails try again during next card power cycle */
1007 if (err)
1008 goto err;
1009
1010 /*
1011 * Set the bus width(4 or 8) with host's support and
1012 * switch to HS200 mode if bus width is set successfully.
1013 */
1014 err = mmc_select_bus_width(card);
1015 if (!IS_ERR_VALUE(err)) {
Ulf Hansson4509f84772014-01-08 16:09:33 +01001016 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeon577fb132014-04-23 17:08:44 +09001017 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200,
1018 card->ext_csd.generic_cmd6_time,
1019 true, true, true);
1020 if (!err)
1021 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1022 }
Girish K Sa4924c72012-01-11 14:04:52 -05001023err:
1024 return err;
1025}
1026
1027/*
Seungwon Jeon577fb132014-04-23 17:08:44 +09001028 * Activate High Speed or HS200 mode if supported.
1029 */
1030static int mmc_select_timing(struct mmc_card *card)
1031{
1032 int err = 0;
1033
1034 if ((card->csd.mmca_vsn < CSD_SPEC_VER_4 &&
1035 card->ext_csd.hs_max_dtr == 0))
1036 goto bus_speed;
1037
1038 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1039 err = mmc_select_hs200(card);
1040 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1041 err = mmc_select_hs(card);
1042
1043 if (err && err != -EBADMSG)
1044 return err;
1045
1046 if (err) {
1047 pr_warn("%s: switch to %s failed\n",
1048 mmc_card_hs(card) ? "high-speed" :
1049 (mmc_card_hs200(card) ? "hs200" : ""),
1050 mmc_hostname(card->host));
1051 err = 0;
1052 }
1053
1054bus_speed:
1055 /*
1056 * Set the bus speed to the selected bus timing.
1057 * If timing is not selected, backward compatible is the default.
1058 */
1059 mmc_set_bus_speed(card);
1060 return err;
1061}
1062
1063/*
1064 * Execute tuning sequence to seek the proper bus operating
1065 * conditions for HS200, which sends CMD21 to the device.
1066 */
1067static int mmc_hs200_tuning(struct mmc_card *card)
1068{
1069 struct mmc_host *host = card->host;
1070 int err = 0;
1071
1072 if (host->ops->execute_tuning) {
1073 mmc_host_clk_hold(host);
1074 err = host->ops->execute_tuning(host,
1075 MMC_SEND_TUNING_BLOCK_HS200);
1076 mmc_host_clk_release(host);
1077
1078 if (err)
1079 pr_warn("%s: tuning execution failed\n",
1080 mmc_hostname(host));
1081 }
1082
1083 return err;
1084}
1085
1086/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001087 * Handle the detection and initialisation of a card.
1088 *
Deepak Saxena87693922008-06-16 19:20:57 -07001089 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001090 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001091 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +02001092static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001093 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001094{
1095 struct mmc_card *card;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001096 int err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001097 u32 cid[4];
Philip Rakityb676f032011-02-13 23:13:09 -08001098 u32 rocr;
Philip Rakity08ee80c2011-05-24 18:14:58 -07001099 u8 *ext_csd = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001100
1101 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001102 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001103
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001104 /* Set correct bus mode for MMC before attempting init */
1105 if (!mmc_host_is_spi(host))
1106 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1107
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001108 /*
1109 * Since we're changing the OCR value, we seem to
1110 * need to tell some cards to go back to the idle
1111 * state. We wait 1ms to give cards time to
1112 * respond.
Balaji T Kc3805462011-09-08 22:08:39 +05301113 * mmc_go_idle is needed for eMMC that are asleep
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001114 */
1115 mmc_go_idle(host);
1116
1117 /* The extra bit indicates that we support high capacity */
Philip Rakityb676f032011-02-13 23:13:09 -08001118 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +02001119 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001120 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001121
1122 /*
David Brownellaf517152007-08-08 09:11:32 -07001123 * For SPI, enable CRC as appropriate.
1124 */
1125 if (mmc_host_is_spi(host)) {
1126 err = mmc_spi_set_crc(host, use_spi_crc);
1127 if (err)
1128 goto err;
1129 }
1130
1131 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001132 * Fetch CID from card.
1133 */
David Brownellaf517152007-08-08 09:11:32 -07001134 if (mmc_host_is_spi(host))
1135 err = mmc_send_cid(host, cid);
1136 else
1137 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +02001138 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001139 goto err;
1140
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001141 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001142 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1143 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001144 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001145 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001146
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001147 card = oldcard;
1148 } else {
1149 /*
1150 * Allocate card structure.
1151 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +01001152 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001153 if (IS_ERR(card)) {
1154 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001155 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001156 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001157
Ulf Hansson69041152013-09-13 11:31:33 +02001158 card->ocr = ocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001159 card->type = MMC_TYPE_MMC;
1160 card->rca = 1;
1161 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1162 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001163
1164 /*
David Brownellaf517152007-08-08 09:11:32 -07001165 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001166 */
David Brownellaf517152007-08-08 09:11:32 -07001167 if (!mmc_host_is_spi(host)) {
1168 err = mmc_set_relative_addr(card);
1169 if (err)
1170 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001171
David Brownellaf517152007-08-08 09:11:32 -07001172 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1173 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001174
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001175 if (!oldcard) {
1176 /*
1177 * Fetch CSD from card.
1178 */
1179 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001180 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001181 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001182
Pierre Ossmanbd766312007-05-01 16:11:57 +02001183 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001184 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001185 goto free_card;
1186 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001187 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +02001188 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001189 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001190
1191 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001192 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001193 */
David Brownellaf517152007-08-08 09:11:32 -07001194 if (!mmc_host_is_spi(host)) {
1195 err = mmc_select_card(card);
1196 if (err)
1197 goto free_card;
1198 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001199
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001200 if (!oldcard) {
1201 /*
David Brownellaf517152007-08-08 09:11:32 -07001202 * Fetch and process extended CSD.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001203 */
Philip Rakity08ee80c2011-05-24 18:14:58 -07001204
1205 err = mmc_get_ext_csd(card, &ext_csd);
1206 if (err)
1207 goto free_card;
1208 err = mmc_read_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001209 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001210 goto free_card;
Philip Rakityb676f032011-02-13 23:13:09 -08001211
1212 /* If doing byte addressing, check if required to do sector
1213 * addressing. Handle the case of <2GB cards needing sector
1214 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1215 * ocr register has bit 30 set for sector addressing.
1216 */
1217 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
1218 mmc_card_set_blockaddr(card);
1219
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001220 /* Erase size depends on CSD and Extended CSD */
1221 mmc_set_erase_size(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001222 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001223
1224 /*
Chuanxiao Dong709de992011-01-22 04:09:41 +08001225 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001226 * bit. This bit will be lost every time after a reset or power off.
Chuanxiao Dong709de992011-01-22 04:09:41 +08001227 */
Adrian Hunter83bb24a2012-03-13 05:19:13 -04001228 if (card->ext_csd.enhanced_area_en ||
1229 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
Chuanxiao Dong709de992011-01-22 04:09:41 +08001230 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeonb23cf0b2011-09-23 14:15:29 +09001231 EXT_CSD_ERASE_GROUP_DEF, 1,
1232 card->ext_csd.generic_cmd6_time);
Chuanxiao Dong709de992011-01-22 04:09:41 +08001233
1234 if (err && err != -EBADMSG)
1235 goto free_card;
1236
1237 if (err) {
1238 err = 0;
1239 /*
1240 * Just disable enhanced area off & sz
1241 * will try to enable ERASE_GROUP_DEF
1242 * during next time reinit
1243 */
1244 card->ext_csd.enhanced_area_offset = -EINVAL;
1245 card->ext_csd.enhanced_area_size = -EINVAL;
1246 } else {
1247 card->ext_csd.erase_group_def = 1;
1248 /*
1249 * enable ERASE_GRP_DEF successfully.
1250 * This will affect the erase size, so
1251 * here need to reset erase size
1252 */
1253 mmc_set_erase_size(card);
1254 }
1255 }
1256
1257 /*
Philip Rakity41e2a482011-03-19 14:10:33 -04001258 * Ensure eMMC user default partition is enabled
1259 */
Andrei Warkentin371a6892011-04-11 18:10:25 -05001260 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1261 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1262 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1263 card->ext_csd.part_config,
1264 card->ext_csd.part_time);
1265 if (err && err != -EBADMSG)
1266 goto free_card;
Philip Rakity41e2a482011-03-19 14:10:33 -04001267 }
1268
1269 /*
Ulf Hansson432356792013-06-10 17:03:45 +02001270 * Enable power_off_notification byte in the ext_csd register
Girish K Sbec87262011-10-13 12:04:16 +05301271 */
Ulf Hansson432356792013-06-10 17:03:45 +02001272 if (card->ext_csd.rev >= 6) {
Girish K Sbec87262011-10-13 12:04:16 +05301273 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1274 EXT_CSD_POWER_OFF_NOTIFICATION,
1275 EXT_CSD_POWER_ON,
1276 card->ext_csd.generic_cmd6_time);
1277 if (err && err != -EBADMSG)
1278 goto free_card;
Girish K Sbec87262011-10-13 12:04:16 +05301279
Girish K S96a85d52011-11-04 16:22:47 +05301280 /*
1281 * The err can be -EBADMSG or 0,
1282 * so check for success and update the flag
1283 */
1284 if (!err)
Ulf Hanssone6c08582012-10-05 12:45:39 -04001285 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
Girish K S96a85d52011-11-04 16:22:47 +05301286 }
Girish K Sbec87262011-10-13 12:04:16 +05301287
1288 /*
Seungwon Jeon577fb132014-04-23 17:08:44 +09001289 * Select timing interface
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001290 */
Seungwon Jeon577fb132014-04-23 17:08:44 +09001291 err = mmc_select_timing(card);
1292 if (err)
1293 goto free_card;
Girish K Sa4924c72012-01-11 14:04:52 -05001294
Girish K Sa4924c72012-01-11 14:04:52 -05001295 if (mmc_card_hs200(card)) {
Seungwon Jeon577fb132014-04-23 17:08:44 +09001296 err = mmc_hs200_tuning(card);
1297 if (err)
Girish K Sa4924c72012-01-11 14:04:52 -05001298 goto err;
Seungwon Jeon577fb132014-04-23 17:08:44 +09001299 } else if (mmc_card_hs(card)) {
1300 /* Select the desired bus width optionally */
1301 err = mmc_select_bus_width(card);
1302 if (!IS_ERR_VALUE(err)) {
1303 err = mmc_select_hs_ddr(card);
1304 if (err)
1305 goto err;
Adrian Hunteref0b27d2009-09-22 16:44:37 -07001306 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +02001307 }
1308
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001309 /*
Seungwon Jeon23850492014-04-23 17:08:05 +09001310 * Choose the power class with selected bus interface
1311 */
1312 mmc_select_powerclass(card);
1313
1314 /*
Subhash Jadavani52d09742012-03-06 17:59:12 +05301315 * Enable HPI feature (if supported)
1316 */
1317 if (card->ext_csd.hpi) {
1318 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1319 EXT_CSD_HPI_MGMT, 1,
1320 card->ext_csd.generic_cmd6_time);
1321 if (err && err != -EBADMSG)
1322 goto free_card;
1323 if (err) {
1324 pr_warning("%s: Enabling HPI failed\n",
1325 mmc_hostname(card->host));
1326 err = 0;
1327 } else
1328 card->ext_csd.hpi_en = 1;
1329 }
1330
1331 /*
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001332 * If cache size is higher than 0, this indicates
1333 * the existence of cache and it can be turned on.
1334 */
Ulf Hansson7536d3f2013-12-18 11:59:17 +01001335 if (card->ext_csd.cache_size > 0) {
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001336 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001337 EXT_CSD_CACHE_CTRL, 1,
1338 card->ext_csd.generic_cmd6_time);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001339 if (err && err != -EBADMSG)
1340 goto free_card;
1341
1342 /*
1343 * Only if no error, cache is turned on successfully.
1344 */
Seungwon Jeon8bc06782011-12-09 17:47:17 +09001345 if (err) {
1346 pr_warning("%s: Cache is supported, "
1347 "but failed to turn on (%d)\n",
1348 mmc_hostname(card->host), err);
1349 card->ext_csd.cache_ctrl = 0;
1350 err = 0;
1351 } else {
1352 card->ext_csd.cache_ctrl = 1;
1353 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001354 }
1355
Seungwon Jeonabd9ac12013-02-06 17:01:43 +09001356 /*
1357 * The mandatory minimum values are defined for packed command.
1358 * read: 5, write: 3
1359 */
1360 if (card->ext_csd.max_packed_writes >= 3 &&
1361 card->ext_csd.max_packed_reads >= 5 &&
1362 host->caps2 & MMC_CAP2_PACKED_CMD) {
1363 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1364 EXT_CSD_EXP_EVENTS_CTRL,
1365 EXT_CSD_PACKED_EVENT_EN,
1366 card->ext_csd.generic_cmd6_time);
1367 if (err && err != -EBADMSG)
1368 goto free_card;
1369 if (err) {
1370 pr_warn("%s: Enabling packed event failed\n",
1371 mmc_hostname(card->host));
1372 card->ext_csd.packed_event_en = 0;
1373 err = 0;
1374 } else {
1375 card->ext_csd.packed_event_en = 1;
1376 }
1377 }
1378
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001379 if (!oldcard)
1380 host->card = card;
1381
Philip Rakity08ee80c2011-05-24 18:14:58 -07001382 mmc_free_ext_csd(ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +02001383 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001384
1385free_card:
1386 if (!oldcard)
1387 mmc_remove_card(card);
1388err:
Philip Rakity08ee80c2011-05-24 18:14:58 -07001389 mmc_free_ext_csd(ext_csd);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001390
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001391 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001392}
1393
Ulf Hansson07a68212013-04-19 15:12:11 +02001394static int mmc_can_sleep(struct mmc_card *card)
1395{
1396 return (card && card->ext_csd.rev >= 3);
1397}
1398
1399static int mmc_sleep(struct mmc_host *host)
1400{
1401 struct mmc_command cmd = {0};
1402 struct mmc_card *card = host->card;
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001403 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
Ulf Hansson07a68212013-04-19 15:12:11 +02001404 int err;
1405
Ulf Hansson07a68212013-04-19 15:12:11 +02001406 err = mmc_deselect_cards(host);
1407 if (err)
1408 return err;
1409
1410 cmd.opcode = MMC_SLEEP_AWAKE;
1411 cmd.arg = card->rca << 16;
1412 cmd.arg |= 1 << 15;
1413
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001414 /*
1415 * If the max_busy_timeout of the host is specified, validate it against
1416 * the sleep cmd timeout. A failure means we need to prevent the host
1417 * from doing hw busy detection, which is done by converting to a R1
1418 * response instead of a R1B.
1419 */
1420 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1421 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1422 } else {
1423 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1424 cmd.busy_timeout = timeout_ms;
1425 }
1426
Ulf Hansson07a68212013-04-19 15:12:11 +02001427 err = mmc_wait_for_cmd(host, &cmd, 0);
1428 if (err)
1429 return err;
1430
1431 /*
1432 * If the host does not wait while the card signals busy, then we will
1433 * will have to wait the sleep/awake timeout. Note, we cannot use the
1434 * SEND_STATUS command to poll the status because that command (and most
1435 * others) is invalid while the card sleeps.
1436 */
Ulf Hanssoncb962e02014-01-14 23:17:36 +01001437 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1438 mmc_delay(timeout_ms);
Ulf Hansson07a68212013-04-19 15:12:11 +02001439
1440 return err;
1441}
1442
Ulf Hanssone6c08582012-10-05 12:45:39 -04001443static int mmc_can_poweroff_notify(const struct mmc_card *card)
1444{
1445 return card &&
1446 mmc_card_mmc(card) &&
1447 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1448}
1449
1450static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1451{
1452 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1453 int err;
1454
1455 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1456 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1457 timeout = card->ext_csd.power_off_longtime;
1458
Ulf Hansson878e2002013-09-09 11:57:57 +02001459 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1460 EXT_CSD_POWER_OFF_NOTIFICATION,
Ulf Hansson4509f84772014-01-08 16:09:33 +01001461 notify_type, timeout, true, false, false);
Ulf Hanssone6c08582012-10-05 12:45:39 -04001462 if (err)
1463 pr_err("%s: Power Off Notification timed out, %u\n",
1464 mmc_hostname(card->host), timeout);
1465
1466 /* Disable the power off notification after the switch operation. */
1467 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1468
1469 return err;
1470}
1471
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001472/*
1473 * Host is being removed. Free up the current card.
1474 */
1475static void mmc_remove(struct mmc_host *host)
1476{
1477 BUG_ON(!host);
1478 BUG_ON(!host->card);
1479
1480 mmc_remove_card(host->card);
1481 host->card = NULL;
1482}
1483
1484/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001485 * Card detection - card is alive.
1486 */
1487static int mmc_alive(struct mmc_host *host)
1488{
1489 return mmc_send_status(host->card, NULL);
1490}
1491
1492/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001493 * Card detection callback from host.
1494 */
1495static void mmc_detect(struct mmc_host *host)
1496{
1497 int err;
1498
1499 BUG_ON(!host);
1500 BUG_ON(!host->card);
1501
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001502 mmc_get_card(host->card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001503
1504 /*
1505 * Just check if our card has been removed.
1506 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001507 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001508
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001509 mmc_put_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001510
Pierre Ossman17b04292007-07-22 22:18:46 +02001511 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001512 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001513
1514 mmc_claim_host(host);
1515 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001516 mmc_power_off(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001517 mmc_release_host(host);
1518 }
1519}
1520
Ulf Hansson03d071f2013-06-10 17:03:43 +02001521static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001522{
Balaji T Kc3805462011-09-08 22:08:39 +05301523 int err = 0;
Ulf Hansson03d071f2013-06-10 17:03:43 +02001524 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1525 EXT_CSD_POWER_OFF_LONG;
Balaji T Kc3805462011-09-08 22:08:39 +05301526
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001527 BUG_ON(!host);
1528 BUG_ON(!host->card);
1529
1530 mmc_claim_host(host);
Maya Erez881d9262013-01-28 16:44:22 -05001531
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001532 if (mmc_card_suspended(host->card))
1533 goto out;
1534
Ulf Hansson39b94312013-05-02 14:02:36 +02001535 if (mmc_card_doing_bkops(host->card)) {
1536 err = mmc_stop_bkops(host->card);
1537 if (err)
1538 goto out;
1539 }
1540
Ulf Hansson10e5d962013-12-16 16:23:22 +01001541 err = mmc_flush_cache(host->card);
Maya Erez881d9262013-01-28 16:44:22 -05001542 if (err)
1543 goto out;
1544
Ulf Hansson432356792013-06-10 17:03:45 +02001545 if (mmc_can_poweroff_notify(host->card) &&
Ulf Hansson53275c22013-06-10 17:03:46 +02001546 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
Ulf Hansson03d071f2013-06-10 17:03:43 +02001547 err = mmc_poweroff_notify(host->card, notify_type);
Ulf Hansson07a68212013-04-19 15:12:11 +02001548 else if (mmc_can_sleep(host->card))
1549 err = mmc_sleep(host);
Ulf Hanssone6c08582012-10-05 12:45:39 -04001550 else if (!mmc_host_is_spi(host))
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001551 err = mmc_deselect_cards(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001552
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001553 if (!err) {
Ulf Hansson74590262013-06-10 17:03:38 +02001554 mmc_power_off(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001555 mmc_card_set_suspended(host->card);
1556 }
Maya Erez881d9262013-01-28 16:44:22 -05001557out:
1558 mmc_release_host(host);
Balaji T Kc3805462011-09-08 22:08:39 +05301559 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001560}
1561
1562/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001563 * Suspend callback
Ulf Hansson03d071f2013-06-10 17:03:43 +02001564 */
1565static int mmc_suspend(struct mmc_host *host)
1566{
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001567 int err;
1568
1569 err = _mmc_suspend(host, true);
1570 if (!err) {
1571 pm_runtime_disable(&host->card->dev);
1572 pm_runtime_set_suspended(&host->card->dev);
1573 }
1574
1575 return err;
Ulf Hansson03d071f2013-06-10 17:03:43 +02001576}
1577
1578/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001579 * This function tries to determine if the same card is still present
1580 * and, if so, restore all state to it.
1581 */
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001582static int _mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001583{
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001584 int err = 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001585
1586 BUG_ON(!host);
1587 BUG_ON(!host->card);
1588
1589 mmc_claim_host(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001590
1591 if (!mmc_card_suspended(host->card))
1592 goto out;
1593
Ulf Hansson69041152013-09-13 11:31:33 +02001594 mmc_power_up(host, host->card->ocr);
Ulf Hansson69041152013-09-13 11:31:33 +02001595 err = mmc_init_card(host, host->card->ocr, host->card);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001596 mmc_card_clr_suspended(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001597
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001598out:
1599 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001600 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001601}
1602
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001603/*
1604 * Shutdown callback
1605 */
1606static int mmc_shutdown(struct mmc_host *host)
1607{
1608 int err = 0;
1609
1610 /*
1611 * In a specific case for poweroff notify, we need to resume the card
1612 * before we can shutdown it properly.
1613 */
1614 if (mmc_can_poweroff_notify(host->card) &&
1615 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001616 err = _mmc_resume(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001617
1618 if (!err)
1619 err = _mmc_suspend(host, false);
1620
1621 return err;
1622}
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001623
1624/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001625 * Callback for resume.
1626 */
1627static int mmc_resume(struct mmc_host *host)
1628{
Ulf Hansson4d223782013-10-10 17:22:23 +02001629 int err = 0;
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001630
Ulf Hansson4d223782013-10-10 17:22:23 +02001631 if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1632 err = _mmc_resume(host);
1633 pm_runtime_set_active(&host->card->dev);
1634 pm_runtime_mark_last_busy(&host->card->dev);
1635 }
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001636 pm_runtime_enable(&host->card->dev);
1637
1638 return err;
1639}
1640
1641/*
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001642 * Callback for runtime_suspend.
1643 */
1644static int mmc_runtime_suspend(struct mmc_host *host)
1645{
1646 int err;
1647
1648 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1649 return 0;
1650
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001651 err = _mmc_suspend(host, true);
Ulf Hansson0cc81a82013-10-03 11:24:44 +02001652 if (err)
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001653 pr_err("%s: error %d doing aggessive suspend\n",
1654 mmc_hostname(host), err);
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001655
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001656 return err;
1657}
1658
1659/*
1660 * Callback for runtime_resume.
1661 */
1662static int mmc_runtime_resume(struct mmc_host *host)
1663{
1664 int err;
1665
Ulf Hansson4d223782013-10-10 17:22:23 +02001666 if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001667 return 0;
1668
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001669 err = _mmc_resume(host);
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001670 if (err)
1671 pr_err("%s: error %d doing aggessive resume\n",
1672 mmc_hostname(host), err);
1673
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001674 return 0;
1675}
1676
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001677static int mmc_power_restore(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001678{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001679 int ret;
1680
Adrian Huntereae1aee2009-09-22 16:44:33 -07001681 mmc_claim_host(host);
Ulf Hansson69041152013-09-13 11:31:33 +02001682 ret = mmc_init_card(host, host->card->ocr, host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001683 mmc_release_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001684
1685 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001686}
1687
Adrian Hunter9feae242009-09-22 16:44:32 -07001688static const struct mmc_bus_ops mmc_ops = {
1689 .remove = mmc_remove,
1690 .detect = mmc_detect,
Adrian Hunter9feae242009-09-22 16:44:32 -07001691 .suspend = mmc_suspend,
1692 .resume = mmc_resume,
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001693 .runtime_suspend = mmc_runtime_suspend,
1694 .runtime_resume = mmc_runtime_resume,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001695 .power_restore = mmc_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001696 .alive = mmc_alive,
Ulf Hansson486fdbb2013-06-10 17:03:44 +02001697 .shutdown = mmc_shutdown,
Adrian Hunter9feae242009-09-22 16:44:32 -07001698};
1699
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001700/*
1701 * Starting point for MMC card init.
1702 */
Andy Ross807e8e42011-01-03 10:36:56 -08001703int mmc_attach_mmc(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001704{
1705 int err;
Ulf Hansson69041152013-09-13 11:31:33 +02001706 u32 ocr, rocr;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001707
1708 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001709 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001710
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001711 /* Set correct bus mode for MMC before attempting attach */
1712 if (!mmc_host_is_spi(host))
1713 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1714
Andy Ross807e8e42011-01-03 10:36:56 -08001715 err = mmc_send_op_cond(host, 0, &ocr);
1716 if (err)
1717 return err;
1718
Ulf Hansson2501c912013-10-30 01:00:18 +01001719 mmc_attach_bus(host, &mmc_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001720 if (host->ocr_avail_mmc)
1721 host->ocr_avail = host->ocr_avail_mmc;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001722
1723 /*
David Brownellaf517152007-08-08 09:11:32 -07001724 * We need to get OCR a different way for SPI.
1725 */
1726 if (mmc_host_is_spi(host)) {
1727 err = mmc_spi_read_ocr(host, 1, &ocr);
1728 if (err)
1729 goto err;
1730 }
1731
Ulf Hansson69041152013-09-13 11:31:33 +02001732 rocr = mmc_select_voltage(host, ocr);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001733
1734 /*
1735 * Can we support the voltage of the card?
1736 */
Ulf Hansson69041152013-09-13 11:31:33 +02001737 if (!rocr) {
Pierre Ossman109b5be2007-07-23 00:12:10 +02001738 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001739 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001740 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001741
1742 /*
1743 * Detect and init the card.
1744 */
Ulf Hansson69041152013-09-13 11:31:33 +02001745 err = mmc_init_card(host, rocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001746 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001747 goto err;
1748
1749 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001750 err = mmc_add_card(host->card);
Andy Ross807e8e42011-01-03 10:36:56 -08001751 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001752 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001753 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001754
1755 return 0;
1756
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001757remove_card:
Andy Ross807e8e42011-01-03 10:36:56 -08001758 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001759 mmc_remove_card(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001760 mmc_claim_host(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001761 host->card = NULL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001762err:
1763 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001764
Girish K Sa3c76eb2011-10-11 11:44:09 +05301765 pr_err("%s: error %d whilst initialising MMC card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02001766 mmc_hostname(host), err);
1767
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001768 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001769}