blob: ccba3869c0293a92cb27cfcaa73d855b01156ba9 [file] [log] [blame]
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/core/mmc.c
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
6 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010015
16#include <linux/mmc/host.h>
17#include <linux/mmc/card.h>
18#include <linux/mmc/mmc.h>
19
20#include "core.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020021#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010022#include "mmc_ops.h"
23
24static const unsigned int tran_exp[] = {
25 10000, 100000, 1000000, 10000000,
26 0, 0, 0, 0
27};
28
29static const unsigned char tran_mant[] = {
30 0, 10, 12, 13, 15, 20, 25, 30,
31 35, 40, 45, 50, 55, 60, 70, 80,
32};
33
34static const unsigned int tacc_exp[] = {
35 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
36};
37
38static const unsigned int tacc_mant[] = {
39 0, 10, 12, 13, 15, 20, 25, 30,
40 35, 40, 45, 50, 55, 60, 70, 80,
41};
42
43#define UNSTUFF_BITS(resp,start,size) \
44 ({ \
45 const int __size = size; \
46 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
47 const int __off = 3 - ((start) / 32); \
48 const int __shft = (start) & 31; \
49 u32 __res; \
50 \
51 __res = resp[__off] >> __shft; \
52 if (__size + __shft > 32) \
53 __res |= resp[__off-1] << ((32 - __shft) % 32); \
54 __res & __mask; \
55 })
56
57/*
58 * Given the decoded CSD structure, decode the raw CID to our CID structure.
59 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020060static int mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010061{
62 u32 *resp = card->raw_cid;
63
64 /*
65 * The selection of the format here is based upon published
66 * specs from sandisk and from what people have reported.
67 */
68 switch (card->csd.mmca_vsn) {
69 case 0: /* MMC v1.0 - v1.2 */
70 case 1: /* MMC v1.4 */
71 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
72 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
73 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
74 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
75 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
76 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
77 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
78 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
79 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
80 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
81 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
82 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
83 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
84 break;
85
86 case 2: /* MMC v2.0 - v2.2 */
87 case 3: /* MMC v3.1 - v3.3 */
88 case 4: /* MMC v4 */
89 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
90 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
91 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
92 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
93 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
94 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
95 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
96 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
97 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
98 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
99 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
100 break;
101
102 default:
Pierre Ossmanfacba912007-07-24 21:53:43 +0200103 printk(KERN_ERR "%s: card has unknown MMCA version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100104 mmc_hostname(card->host), card->csd.mmca_vsn);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200105 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100106 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200107
108 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100109}
110
111/*
112 * Given a 128-bit response, decode to our card CSD structure.
113 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200114static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100115{
116 struct mmc_csd *csd = &card->csd;
Kyungmin Park6da24b72010-08-10 18:01:36 -0700117 unsigned int e, m;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100118 u32 *resp = card->raw_csd;
119
120 /*
121 * We only understand CSD structure v1.1 and v1.2.
122 * v1.2 has extra information in bits 15, 11 and 10.
Kyungmin Park6da24b72010-08-10 18:01:36 -0700123 * We also support eMMC v4.4 & v4.41.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100124 */
Kyungmin Park6da24b72010-08-10 18:01:36 -0700125 csd->structure = UNSTUFF_BITS(resp, 126, 2);
126 if (csd->structure == 0) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200127 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
Kyungmin Park6da24b72010-08-10 18:01:36 -0700128 mmc_hostname(card->host), csd->structure);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200129 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100130 }
131
132 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
133 m = UNSTUFF_BITS(resp, 115, 4);
134 e = UNSTUFF_BITS(resp, 112, 3);
135 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
136 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
137
138 m = UNSTUFF_BITS(resp, 99, 4);
139 e = UNSTUFF_BITS(resp, 96, 3);
140 csd->max_dtr = tran_exp[e] * tran_mant[m];
141 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
142
143 e = UNSTUFF_BITS(resp, 47, 3);
144 m = UNSTUFF_BITS(resp, 62, 12);
145 csd->capacity = (1 + m) << (e + 2);
146
147 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
148 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
149 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
150 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
151 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
152 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
153 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200154
155 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100156}
157
158/*
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200159 * Read and decode extended CSD.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100160 */
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200161static int mmc_read_ext_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100162{
163 int err;
164 u8 *ext_csd;
165
166 BUG_ON(!card);
167
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100168 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
Pierre Ossman17b04292007-07-22 22:18:46 +0200169 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100170
171 /*
172 * As the ext_csd is so large and mostly unused, we don't store the
173 * raw block in mmc_card.
174 */
175 ext_csd = kmalloc(512, GFP_KERNEL);
176 if (!ext_csd) {
177 printk(KERN_ERR "%s: could not allocate a buffer to "
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200178 "receive the ext_csd.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200179 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100180 }
181
182 err = mmc_send_ext_csd(card, ext_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200183 if (err) {
Wolfgang Mueesd08ebed2009-09-22 16:45:26 -0700184 /* If the host or the card can't do the switch,
185 * fail more gracefully. */
186 if ((err != -EINVAL)
187 && (err != -ENOSYS)
188 && (err != -EFAULT))
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200189 goto out;
190
191 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100192 * High capacity cards should have this "magic" size
193 * stored in their CSD.
194 */
195 if (card->csd.capacity == (4096 * 512)) {
196 printk(KERN_ERR "%s: unable to read EXT_CSD "
197 "on a possible high capacity card. "
198 "Card will be ignored.\n",
199 mmc_hostname(card->host));
200 } else {
201 printk(KERN_WARNING "%s: unable to read "
202 "EXT_CSD, performance might "
203 "suffer.\n",
204 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200205 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100206 }
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200207
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100208 goto out;
209 }
210
Kyungmin Park6da24b72010-08-10 18:01:36 -0700211 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
212 if (card->csd.structure == 3) {
213 int ext_csd_struct = ext_csd[EXT_CSD_STRUCTURE];
214 if (ext_csd_struct > 2) {
215 printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
216 "version %d\n", mmc_hostname(card->host),
217 ext_csd_struct);
218 err = -EINVAL;
219 goto out;
220 }
221 }
222
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700223 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
Adrian Hunter11723ab2010-01-08 14:43:01 -0800224 if (card->ext_csd.rev > 5) {
Kyungmin Park6da24b72010-08-10 18:01:36 -0700225 printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n",
226 mmc_hostname(card->host), card->ext_csd.rev);
Florin Malita00cedfa2007-10-13 12:27:20 -0400227 err = -EINVAL;
228 goto out;
Pierre Ossmand7604d72007-07-23 00:34:07 +0200229 }
230
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700231 if (card->ext_csd.rev >= 2) {
Pierre Ossmand7604d72007-07-23 00:34:07 +0200232 card->ext_csd.sectors =
233 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
234 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
235 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
236 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
Hanumath Prasadfc8a0982010-08-10 18:01:45 -0700237
238 /* Cards with density > 2GiB are sector addressed */
239 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
Pierre Ossmand7604d72007-07-23 00:34:07 +0200240 mmc_card_set_blockaddr(card);
241 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100242
Adrian Hunter7198f3c2010-03-23 13:35:40 -0700243 switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100244 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
245 card->ext_csd.hs_max_dtr = 52000000;
246 break;
247 case EXT_CSD_CARD_TYPE_26:
248 card->ext_csd.hs_max_dtr = 26000000;
249 break;
250 default:
251 /* MMC v4 spec says this cannot happen */
252 printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
253 "support any high-speed modes.\n",
254 mmc_hostname(card->host));
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100255 }
256
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700257 if (card->ext_csd.rev >= 3) {
258 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
259
260 /* Sleep / awake timeout in 100ns units */
261 if (sa_shift > 0 && sa_shift <= 0x17)
262 card->ext_csd.sa_timeout =
263 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
264 }
265
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100266out:
267 kfree(ext_csd);
268
269 return err;
270}
271
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100272MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
273 card->raw_cid[2], card->raw_cid[3]);
274MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
275 card->raw_csd[2], card->raw_csd[3]);
276MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
277MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
278MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
279MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
280MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
281MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
282MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
283
284static struct attribute *mmc_std_attrs[] = {
285 &dev_attr_cid.attr,
286 &dev_attr_csd.attr,
287 &dev_attr_date.attr,
288 &dev_attr_fwrev.attr,
289 &dev_attr_hwrev.attr,
290 &dev_attr_manfid.attr,
291 &dev_attr_name.attr,
292 &dev_attr_oemid.attr,
293 &dev_attr_serial.attr,
294 NULL,
295};
296
297static struct attribute_group mmc_std_attr_group = {
298 .attrs = mmc_std_attrs,
299};
300
David Brownella4dbd672009-06-24 10:06:31 -0700301static const struct attribute_group *mmc_attr_groups[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100302 &mmc_std_attr_group,
303 NULL,
304};
305
306static struct device_type mmc_type = {
307 .groups = mmc_attr_groups,
308};
309
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100310/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200311 * Handle the detection and initialisation of a card.
312 *
Deepak Saxena87693922008-06-16 19:20:57 -0700313 * In the case of a resume, "oldcard" will contain the card
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200314 * we're trying to reinitialise.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100315 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +0200316static int mmc_init_card(struct mmc_host *host, u32 ocr,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200317 struct mmc_card *oldcard)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100318{
319 struct mmc_card *card;
320 int err;
321 u32 cid[4];
322 unsigned int max_dtr;
323
324 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200325 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100326
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100327 /*
328 * Since we're changing the OCR value, we seem to
329 * need to tell some cards to go back to the idle
330 * state. We wait 1ms to give cards time to
331 * respond.
332 */
333 mmc_go_idle(host);
334
335 /* The extra bit indicates that we support high capacity */
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200336 err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +0200337 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200338 goto err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100339
340 /*
David Brownellaf517152007-08-08 09:11:32 -0700341 * For SPI, enable CRC as appropriate.
342 */
343 if (mmc_host_is_spi(host)) {
344 err = mmc_spi_set_crc(host, use_spi_crc);
345 if (err)
346 goto err;
347 }
348
349 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100350 * Fetch CID from card.
351 */
David Brownellaf517152007-08-08 09:11:32 -0700352 if (mmc_host_is_spi(host))
353 err = mmc_send_cid(host, cid);
354 else
355 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +0200356 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100357 goto err;
358
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200359 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200360 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
361 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200362 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200363 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100364
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200365 card = oldcard;
366 } else {
367 /*
368 * Allocate card structure.
369 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100370 card = mmc_alloc_card(host, &mmc_type);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200371 if (IS_ERR(card)) {
372 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200373 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200374 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200375
376 card->type = MMC_TYPE_MMC;
377 card->rca = 1;
378 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
379 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100380
381 /*
David Brownellaf517152007-08-08 09:11:32 -0700382 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100383 */
David Brownellaf517152007-08-08 09:11:32 -0700384 if (!mmc_host_is_spi(host)) {
385 err = mmc_set_relative_addr(card);
386 if (err)
387 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100388
David Brownellaf517152007-08-08 09:11:32 -0700389 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
390 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100391
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200392 if (!oldcard) {
393 /*
394 * Fetch CSD from card.
395 */
396 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200397 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200398 goto free_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100399
Pierre Ossmanbd766312007-05-01 16:11:57 +0200400 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200401 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +0200402 goto free_card;
403 err = mmc_decode_cid(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200404 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +0200405 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200406 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100407
408 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200409 * Select card, as all following commands rely on that.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100410 */
David Brownellaf517152007-08-08 09:11:32 -0700411 if (!mmc_host_is_spi(host)) {
412 err = mmc_select_card(card);
413 if (err)
414 goto free_card;
415 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100416
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200417 if (!oldcard) {
418 /*
David Brownellaf517152007-08-08 09:11:32 -0700419 * Fetch and process extended CSD.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200420 */
421 err = mmc_read_ext_csd(card);
Pierre Ossman17b04292007-07-22 22:18:46 +0200422 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200423 goto free_card;
424 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100425
426 /*
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200427 * Activate high speed (if supported)
428 */
429 if ((card->ext_csd.hs_max_dtr != 0) &&
430 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
431 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
432 EXT_CSD_HS_TIMING, 1);
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700433 if (err && err != -EBADMSG)
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200434 goto free_card;
435
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700436 if (err) {
437 printk(KERN_WARNING "%s: switch to highspeed failed\n",
438 mmc_hostname(card->host));
439 err = 0;
440 } else {
441 mmc_card_set_highspeed(card);
442 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
443 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200444 }
445
446 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100447 * Compute bus speed.
448 */
449 max_dtr = (unsigned int)-1;
450
451 if (mmc_card_highspeed(card)) {
452 if (max_dtr > card->ext_csd.hs_max_dtr)
453 max_dtr = card->ext_csd.hs_max_dtr;
454 } else if (max_dtr > card->csd.max_dtr) {
455 max_dtr = card->csd.max_dtr;
456 }
457
458 mmc_set_clock(host, max_dtr);
459
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200460 /*
461 * Activate wide bus (if supported).
462 */
463 if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +0200464 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
465 unsigned ext_csd_bit, bus_width;
466
467 if (host->caps & MMC_CAP_8_BIT_DATA) {
468 ext_csd_bit = EXT_CSD_BUS_WIDTH_8;
469 bus_width = MMC_BUS_WIDTH_8;
470 } else {
471 ext_csd_bit = EXT_CSD_BUS_WIDTH_4;
472 bus_width = MMC_BUS_WIDTH_4;
473 }
474
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200475 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Jarkko Lavinenb30f8af2008-11-17 14:35:21 +0200476 EXT_CSD_BUS_WIDTH, ext_csd_bit);
477
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700478 if (err && err != -EBADMSG)
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200479 goto free_card;
480
Adrian Hunteref0b27d2009-09-22 16:44:37 -0700481 if (err) {
482 printk(KERN_WARNING "%s: switch to bus width %d "
483 "failed\n", mmc_hostname(card->host),
484 1 << bus_width);
485 err = 0;
486 } else {
487 mmc_set_bus_width(card->host, bus_width);
488 }
Pierre Ossman89a73cf2007-05-01 15:08:30 +0200489 }
490
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200491 if (!oldcard)
492 host->card = card;
493
Pierre Ossman17b04292007-07-22 22:18:46 +0200494 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200495
496free_card:
497 if (!oldcard)
498 mmc_remove_card(card);
499err:
500
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200501 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200502}
503
504/*
505 * Host is being removed. Free up the current card.
506 */
507static void mmc_remove(struct mmc_host *host)
508{
509 BUG_ON(!host);
510 BUG_ON(!host->card);
511
512 mmc_remove_card(host->card);
513 host->card = NULL;
514}
515
516/*
517 * Card detection callback from host.
518 */
519static void mmc_detect(struct mmc_host *host)
520{
521 int err;
522
523 BUG_ON(!host);
524 BUG_ON(!host->card);
525
526 mmc_claim_host(host);
527
528 /*
529 * Just check if our card has been removed.
530 */
531 err = mmc_send_status(host->card, NULL);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100532
533 mmc_release_host(host);
534
Pierre Ossman17b04292007-07-22 22:18:46 +0200535 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +0200536 mmc_remove(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200537
538 mmc_claim_host(host);
539 mmc_detach_bus(host);
540 mmc_release_host(host);
541 }
542}
543
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200544/*
545 * Suspend callback from host.
546 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700547static int mmc_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200548{
549 BUG_ON(!host);
550 BUG_ON(!host->card);
551
552 mmc_claim_host(host);
David Brownellaf517152007-08-08 09:11:32 -0700553 if (!mmc_host_is_spi(host))
554 mmc_deselect_cards(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200555 host->card->state &= ~MMC_STATE_HIGHSPEED;
556 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700557
558 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200559}
560
561/*
562 * Resume callback from host.
563 *
564 * This function tries to determine if the same card is still present
565 * and, if so, restore all state to it.
566 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700567static int mmc_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200568{
569 int err;
570
571 BUG_ON(!host);
572 BUG_ON(!host->card);
573
574 mmc_claim_host(host);
Pierre Ossman8c75dea2007-05-19 16:14:43 +0200575 err = mmc_init_card(host, host->ocr, host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200576 mmc_release_host(host);
577
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700578 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200579}
580
Adrian Huntereae1aee2009-09-22 16:44:33 -0700581static void mmc_power_restore(struct mmc_host *host)
582{
583 host->card->state &= ~MMC_STATE_HIGHSPEED;
584 mmc_claim_host(host);
585 mmc_init_card(host, host->ocr, host->card);
586 mmc_release_host(host);
587}
588
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700589static int mmc_sleep(struct mmc_host *host)
590{
591 struct mmc_card *card = host->card;
592 int err = -ENOSYS;
593
594 if (card && card->ext_csd.rev >= 3) {
595 err = mmc_card_sleepawake(host, 1);
596 if (err < 0)
597 pr_debug("%s: Error %d while putting card into sleep",
598 mmc_hostname(host), err);
599 }
600
601 return err;
602}
603
604static int mmc_awake(struct mmc_host *host)
605{
606 struct mmc_card *card = host->card;
607 int err = -ENOSYS;
608
609 if (card && card->ext_csd.rev >= 3) {
610 err = mmc_card_sleepawake(host, 0);
611 if (err < 0)
612 pr_debug("%s: Error %d while awaking sleeping card",
613 mmc_hostname(host), err);
614 }
615
616 return err;
617}
618
Adrian Hunter9feae242009-09-22 16:44:32 -0700619static const struct mmc_bus_ops mmc_ops = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700620 .awake = mmc_awake,
621 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -0700622 .remove = mmc_remove,
623 .detect = mmc_detect,
624 .suspend = NULL,
625 .resume = NULL,
Adrian Huntereae1aee2009-09-22 16:44:33 -0700626 .power_restore = mmc_power_restore,
Adrian Hunter9feae242009-09-22 16:44:32 -0700627};
628
629static const struct mmc_bus_ops mmc_ops_unsafe = {
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -0700630 .awake = mmc_awake,
631 .sleep = mmc_sleep,
Adrian Hunter9feae242009-09-22 16:44:32 -0700632 .remove = mmc_remove,
633 .detect = mmc_detect,
634 .suspend = mmc_suspend,
635 .resume = mmc_resume,
Adrian Huntereae1aee2009-09-22 16:44:33 -0700636 .power_restore = mmc_power_restore,
Adrian Hunter9feae242009-09-22 16:44:32 -0700637};
638
639static void mmc_attach_bus_ops(struct mmc_host *host)
640{
641 const struct mmc_bus_ops *bus_ops;
642
Ben Hutchingsbd68e082009-12-14 18:01:29 -0800643 if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable)
Adrian Hunter9feae242009-09-22 16:44:32 -0700644 bus_ops = &mmc_ops_unsafe;
645 else
646 bus_ops = &mmc_ops;
647 mmc_attach_bus(host, bus_ops);
648}
649
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200650/*
651 * Starting point for MMC card init.
652 */
653int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
654{
655 int err;
656
657 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200658 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200659
Adrian Hunter9feae242009-09-22 16:44:32 -0700660 mmc_attach_bus_ops(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200661
662 /*
David Brownellaf517152007-08-08 09:11:32 -0700663 * We need to get OCR a different way for SPI.
664 */
665 if (mmc_host_is_spi(host)) {
666 err = mmc_spi_read_ocr(host, 1, &ocr);
667 if (err)
668 goto err;
669 }
670
671 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200672 * Sanity check the voltages that the card claims to
673 * support.
674 */
675 if (ocr & 0x7F) {
676 printk(KERN_WARNING "%s: card claims to support voltages "
677 "below the defined range. These will be ignored.\n",
678 mmc_hostname(host));
679 ocr &= ~0x7F;
680 }
681
682 host->ocr = mmc_select_voltage(host, ocr);
683
684 /*
685 * Can we support the voltage of the card?
686 */
Pierre Ossman109b5be2007-07-23 00:12:10 +0200687 if (!host->ocr) {
688 err = -EINVAL;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200689 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200690 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200691
692 /*
693 * Detect and init the card.
694 */
Pierre Ossman8c75dea2007-05-19 16:14:43 +0200695 err = mmc_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +0200696 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200697 goto err;
698
699 mmc_release_host(host);
700
Pierre Ossman4101c162007-05-19 13:39:01 +0200701 err = mmc_add_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100702 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200703 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100704
705 return 0;
706
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200707remove_card:
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200708 mmc_remove_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100709 host->card = NULL;
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200710 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100711err:
712 mmc_detach_bus(host);
713 mmc_release_host(host);
714
Pierre Ossman109b5be2007-07-23 00:12:10 +0200715 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
716 mmc_hostname(host), err);
717
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200718 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100719}