blob: 00895c99d9bb95d04e16f9013fe9de3611ba2303 [file] [log] [blame]
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/core/sd.c
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-2007 Pierre Ossman, 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>
14
15#include <linux/mmc/host.h>
16#include <linux/mmc/card.h>
17#include <linux/mmc/mmc.h>
Pierre Ossman3373c0a2007-05-31 22:25:11 +020018#include <linux/mmc/sd.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010019
20#include "core.h"
21#include "sysfs.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020022#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010023#include "mmc_ops.h"
24#include "sd_ops.h"
25
Pierre Ossman7ea239d2006-12-31 00:11:32 +010026static const unsigned int tran_exp[] = {
27 10000, 100000, 1000000, 10000000,
28 0, 0, 0, 0
29};
30
31static const unsigned char tran_mant[] = {
32 0, 10, 12, 13, 15, 20, 25, 30,
33 35, 40, 45, 50, 55, 60, 70, 80,
34};
35
36static const unsigned int tacc_exp[] = {
37 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
38};
39
40static const unsigned int tacc_mant[] = {
41 0, 10, 12, 13, 15, 20, 25, 30,
42 35, 40, 45, 50, 55, 60, 70, 80,
43};
44
45#define UNSTUFF_BITS(resp,start,size) \
46 ({ \
47 const int __size = size; \
48 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
49 const int __off = 3 - ((start) / 32); \
50 const int __shft = (start) & 31; \
51 u32 __res; \
52 \
53 __res = resp[__off] >> __shft; \
54 if (__size + __shft > 32) \
55 __res |= resp[__off-1] << ((32 - __shft) % 32); \
56 __res & __mask; \
57 })
58
59/*
60 * Given the decoded CSD structure, decode the raw CID to our CID structure.
61 */
62static void mmc_decode_cid(struct mmc_card *card)
63{
64 u32 *resp = card->raw_cid;
65
66 memset(&card->cid, 0, sizeof(struct mmc_cid));
67
68 /*
69 * SD doesn't currently have a version field so we will
70 * have to assume we can parse this.
71 */
72 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
73 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
74 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
75 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
76 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
77 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
78 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
79 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
80 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
81 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
82 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
83 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
84
85 card->cid.year += 2000; /* SD cards year offset */
86}
87
88/*
89 * Given a 128-bit response, decode to our card CSD structure.
90 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020091static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010092{
93 struct mmc_csd *csd = &card->csd;
94 unsigned int e, m, csd_struct;
95 u32 *resp = card->raw_csd;
96
97 csd_struct = UNSTUFF_BITS(resp, 126, 2);
98
99 switch (csd_struct) {
100 case 0:
101 m = UNSTUFF_BITS(resp, 115, 4);
102 e = UNSTUFF_BITS(resp, 112, 3);
103 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
104 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
105
106 m = UNSTUFF_BITS(resp, 99, 4);
107 e = UNSTUFF_BITS(resp, 96, 3);
108 csd->max_dtr = tran_exp[e] * tran_mant[m];
109 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
110
111 e = UNSTUFF_BITS(resp, 47, 3);
112 m = UNSTUFF_BITS(resp, 62, 12);
113 csd->capacity = (1 + m) << (e + 2);
114
115 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
116 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
117 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
118 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
119 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
120 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
121 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
122 break;
123 case 1:
124 /*
125 * This is a block-addressed SDHC card. Most
126 * interesting fields are unused and have fixed
127 * values. To avoid getting tripped by buggy cards,
128 * we assume those fixed values ourselves.
129 */
130 mmc_card_set_blockaddr(card);
131
132 csd->tacc_ns = 0; /* Unused */
133 csd->tacc_clks = 0; /* Unused */
134
135 m = UNSTUFF_BITS(resp, 99, 4);
136 e = UNSTUFF_BITS(resp, 96, 3);
137 csd->max_dtr = tran_exp[e] * tran_mant[m];
138 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
139
140 m = UNSTUFF_BITS(resp, 48, 22);
141 csd->capacity = (1 + m) << 10;
142
143 csd->read_blkbits = 9;
144 csd->read_partial = 0;
145 csd->write_misalign = 0;
146 csd->read_misalign = 0;
147 csd->r2w_factor = 4; /* Unused */
148 csd->write_blkbits = 9;
149 csd->write_partial = 0;
150 break;
151 default:
Pierre Ossmanfacba912007-07-24 21:53:43 +0200152 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100153 mmc_hostname(card->host), csd_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200154 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100155 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200156
157 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100158}
159
160/*
161 * Given a 64-bit response, decode to our card SCR structure.
162 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200163static int mmc_decode_scr(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100164{
165 struct sd_scr *scr = &card->scr;
166 unsigned int scr_struct;
167 u32 resp[4];
168
169 BUG_ON(!mmc_card_sd(card));
170
171 resp[3] = card->raw_scr[1];
172 resp[2] = card->raw_scr[0];
173
174 scr_struct = UNSTUFF_BITS(resp, 60, 4);
175 if (scr_struct != 0) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200176 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100177 mmc_hostname(card->host), scr_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200178 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100179 }
180
181 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
182 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200183
184 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100185}
186
187/*
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200188 * Fetches and decodes switch information
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100189 */
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200190static int mmc_read_switch(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100191{
192 int err;
193 u8 *status;
194
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200195 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200196 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200197
198 if (!(card->csd.cmdclass & CCC_SWITCH)) {
199 printk(KERN_WARNING "%s: card lacks mandatory switch "
200 "function, performance might suffer.\n",
201 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200202 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200203 }
204
Pierre Ossman17b04292007-07-22 22:18:46 +0200205 err = -EIO;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100206
207 status = kmalloc(64, GFP_KERNEL);
208 if (!status) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200209 printk(KERN_ERR "%s: could not allocate a buffer for "
210 "switch capabilities.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200211 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100212 }
213
214 err = mmc_sd_switch(card, 0, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200215 if (err) {
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200216 printk(KERN_WARNING "%s: problem reading switch "
217 "capabilities, performance might suffer.\n",
218 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200219 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100220 goto out;
221 }
222
223 if (status[13] & 0x02)
224 card->sw_caps.hs_max_dtr = 50000000;
225
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200226out:
227 kfree(status);
228
229 return err;
230}
231
232/*
233 * Test if the card supports high-speed mode and, if so, switch to it.
234 */
235static int mmc_switch_hs(struct mmc_card *card)
236{
237 int err;
238 u8 *status;
239
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200240 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200241 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200242
243 if (!(card->csd.cmdclass & CCC_SWITCH))
Pierre Ossman17b04292007-07-22 22:18:46 +0200244 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200245
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200246 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
Pierre Ossman17b04292007-07-22 22:18:46 +0200247 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200248
249 if (card->sw_caps.hs_max_dtr == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200250 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200251
Pierre Ossman17b04292007-07-22 22:18:46 +0200252 err = -EIO;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200253
254 status = kmalloc(64, GFP_KERNEL);
255 if (!status) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200256 printk(KERN_ERR "%s: could not allocate a buffer for "
257 "switch capabilities.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200258 return -ENOMEM;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200259 }
260
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100261 err = mmc_sd_switch(card, 1, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200262 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100263 goto out;
264
265 if ((status[16] & 0xF) != 1) {
266 printk(KERN_WARNING "%s: Problem switching card "
267 "into high-speed mode!\n",
268 mmc_hostname(card->host));
269 } else {
270 mmc_card_set_highspeed(card);
271 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
272 }
273
274out:
275 kfree(status);
276
277 return err;
278}
279
280/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200281 * Handle the detection and initialisation of a card.
282 *
283 * In the case of a resume, "curcard" will contain the card
284 * we're trying to reinitialise.
285 */
286static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
287 struct mmc_card *oldcard)
288{
289 struct mmc_card *card;
290 int err;
291 u32 cid[4];
292 unsigned int max_dtr;
293
294 BUG_ON(!host);
295 BUG_ON(!host->claimed);
296
297 /*
298 * Since we're changing the OCR value, we seem to
299 * need to tell some cards to go back to the idle
300 * state. We wait 1ms to give cards time to
301 * respond.
302 */
303 mmc_go_idle(host);
304
305 /*
306 * If SD_SEND_IF_COND indicates an SD 2.0
307 * compliant card and we should set bit 30
308 * of the ocr to indicate that we can handle
309 * block-addressed SDHC cards.
310 */
311 err = mmc_send_if_cond(host, ocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200312 if (!err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200313 ocr |= 1 << 30;
314
315 err = mmc_send_app_op_cond(host, ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +0200316 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200317 goto err;
318
319 /*
320 * Fetch CID from card.
321 */
322 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +0200323 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200324 goto err;
325
326 if (oldcard) {
327 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
328 goto err;
329
330 card = oldcard;
331 } else {
332 /*
333 * Allocate card structure.
334 */
335 card = mmc_alloc_card(host);
336 if (IS_ERR(card))
337 goto err;
338
339 card->type = MMC_TYPE_SD;
340 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
341 }
342
343 /*
344 * Set card RCA.
345 */
346 err = mmc_send_relative_addr(host, &card->rca);
Pierre Ossman17b04292007-07-22 22:18:46 +0200347 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200348 goto free_card;
349
350 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
351
352 if (!oldcard) {
353 /*
354 * Fetch CSD from card.
355 */
356 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200357 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200358 goto free_card;
359
Pierre Ossmanbd766312007-05-01 16:11:57 +0200360 err = mmc_decode_csd(card);
361 if (err < 0)
362 goto free_card;
363
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200364 mmc_decode_cid(card);
365 }
366
367 /*
368 * Select card, as all following commands rely on that.
369 */
370 err = mmc_select_card(card);
Pierre Ossman17b04292007-07-22 22:18:46 +0200371 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200372 goto free_card;
373
374 if (!oldcard) {
375 /*
376 * Fetch SCR from card.
377 */
378 err = mmc_app_send_scr(card, card->raw_scr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200379 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200380 goto free_card;
381
Pierre Ossmanbd766312007-05-01 16:11:57 +0200382 err = mmc_decode_scr(card);
383 if (err < 0)
384 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200385
386 /*
387 * Fetch switch information from card.
388 */
389 err = mmc_read_switch(card);
Pierre Ossman17b04292007-07-22 22:18:46 +0200390 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200391 goto free_card;
392 }
393
394 /*
395 * Attempt to change to high-speed (if supported)
396 */
397 err = mmc_switch_hs(card);
Pierre Ossman17b04292007-07-22 22:18:46 +0200398 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200399 goto free_card;
400
401 /*
402 * Compute bus speed.
403 */
404 max_dtr = (unsigned int)-1;
405
406 if (mmc_card_highspeed(card)) {
407 if (max_dtr > card->sw_caps.hs_max_dtr)
408 max_dtr = card->sw_caps.hs_max_dtr;
409 } else if (max_dtr > card->csd.max_dtr) {
410 max_dtr = card->csd.max_dtr;
411 }
412
413 mmc_set_clock(host, max_dtr);
414
415 /*
416 * Switch to wider bus (if supported).
417 */
Pierre Ossman71651292007-06-06 20:23:25 +0200418 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200419 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
420 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
Pierre Ossman17b04292007-07-22 22:18:46 +0200421 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200422 goto free_card;
423
424 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
425 }
426
Pierre Ossmanc3bff2e2007-06-13 19:06:03 +0200427 /*
428 * Check if read-only switch is active.
429 */
430 if (!oldcard) {
431 if (!host->ops->get_ro) {
432 printk(KERN_WARNING "%s: host does not "
433 "support reading read-only "
434 "switch. assuming write-enable.\n",
435 mmc_hostname(host));
436 } else {
437 if (host->ops->get_ro(host))
438 mmc_card_set_readonly(card);
439 }
440 }
441
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200442 if (!oldcard)
443 host->card = card;
444
Pierre Ossman17b04292007-07-22 22:18:46 +0200445 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200446
447free_card:
448 if (!oldcard)
449 mmc_remove_card(card);
450err:
451
Pierre Ossman17b04292007-07-22 22:18:46 +0200452 return -EIO;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200453}
454
455/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100456 * Host is being removed. Free up the current card.
457 */
458static void mmc_sd_remove(struct mmc_host *host)
459{
460 BUG_ON(!host);
461 BUG_ON(!host->card);
462
463 mmc_remove_card(host->card);
464 host->card = NULL;
465}
466
467/*
468 * Card detection callback from host.
469 */
470static void mmc_sd_detect(struct mmc_host *host)
471{
472 int err;
473
474 BUG_ON(!host);
475 BUG_ON(!host->card);
476
477 mmc_claim_host(host);
478
479 /*
480 * Just check if our card has been removed.
481 */
482 err = mmc_send_status(host->card, NULL);
483
484 mmc_release_host(host);
485
Pierre Ossman17b04292007-07-22 22:18:46 +0200486 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +0200487 mmc_sd_remove(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100488
489 mmc_claim_host(host);
490 mmc_detach_bus(host);
491 mmc_release_host(host);
492 }
493}
494
Pierre Ossman4101c162007-05-19 13:39:01 +0200495MMC_ATTR_FN(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
496 card->raw_cid[2], card->raw_cid[3]);
497MMC_ATTR_FN(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
498 card->raw_csd[2], card->raw_csd[3]);
499MMC_ATTR_FN(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
500MMC_ATTR_FN(date, "%02d/%04d\n", card->cid.month, card->cid.year);
501MMC_ATTR_FN(fwrev, "0x%x\n", card->cid.fwrev);
502MMC_ATTR_FN(hwrev, "0x%x\n", card->cid.hwrev);
503MMC_ATTR_FN(manfid, "0x%06x\n", card->cid.manfid);
504MMC_ATTR_FN(name, "%s\n", card->cid.prod_name);
505MMC_ATTR_FN(oemid, "0x%04x\n", card->cid.oemid);
506MMC_ATTR_FN(serial, "0x%08x\n", card->cid.serial);
507
508static struct device_attribute mmc_sd_dev_attrs[] = {
509 MMC_ATTR_RO(cid),
510 MMC_ATTR_RO(csd),
511 MMC_ATTR_RO(scr),
512 MMC_ATTR_RO(date),
513 MMC_ATTR_RO(fwrev),
514 MMC_ATTR_RO(hwrev),
515 MMC_ATTR_RO(manfid),
516 MMC_ATTR_RO(name),
517 MMC_ATTR_RO(oemid),
518 MMC_ATTR_RO(serial),
519 __ATTR_NULL,
520};
521
522/*
523 * Adds sysfs entries as relevant.
524 */
525static int mmc_sd_sysfs_add(struct mmc_host *host, struct mmc_card *card)
526{
527 int ret;
528
529 ret = mmc_add_attrs(card, mmc_sd_dev_attrs);
530 if (ret < 0)
531 return ret;
532
533 return 0;
534}
535
536/*
537 * Removes the sysfs entries added by mmc_sysfs_add().
538 */
539static void mmc_sd_sysfs_remove(struct mmc_host *host, struct mmc_card *card)
540{
541 mmc_remove_attrs(card, mmc_sd_dev_attrs);
542}
543
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200544#ifdef CONFIG_MMC_UNSAFE_RESUME
545
546/*
547 * Suspend callback from host.
548 */
549static void mmc_sd_suspend(struct mmc_host *host)
550{
551 BUG_ON(!host);
552 BUG_ON(!host->card);
553
554 mmc_claim_host(host);
555 mmc_deselect_cards(host);
556 host->card->state &= ~MMC_STATE_HIGHSPEED;
557 mmc_release_host(host);
558}
559
560/*
561 * Resume callback from host.
562 *
563 * This function tries to determine if the same card is still present
564 * and, if so, restore all state to it.
565 */
566static void mmc_sd_resume(struct mmc_host *host)
567{
568 int err;
569
570 BUG_ON(!host);
571 BUG_ON(!host->card);
572
573 mmc_claim_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200574 err = mmc_sd_init_card(host, host->ocr, host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200575 mmc_release_host(host);
576
Pierre Ossman17b04292007-07-22 22:18:46 +0200577 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +0200578 mmc_sd_remove(host);
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200579
580 mmc_claim_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200581 mmc_detach_bus(host);
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200582 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200583 }
584
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200585}
586
587#else
588
589#define mmc_sd_suspend NULL
590#define mmc_sd_resume NULL
591
592#endif
593
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100594static const struct mmc_bus_ops mmc_sd_ops = {
595 .remove = mmc_sd_remove,
596 .detect = mmc_sd_detect,
Pierre Ossman4101c162007-05-19 13:39:01 +0200597 .sysfs_add = mmc_sd_sysfs_add,
598 .sysfs_remove = mmc_sd_sysfs_remove,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200599 .suspend = mmc_sd_suspend,
600 .resume = mmc_sd_resume,
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100601};
602
603/*
604 * Starting point for SD card init.
605 */
606int mmc_attach_sd(struct mmc_host *host, u32 ocr)
607{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100608 int err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100609
610 BUG_ON(!host);
611 BUG_ON(!host->claimed);
612
613 mmc_attach_bus(host, &mmc_sd_ops);
614
Philip Langdale55556da2007-03-16 19:39:00 -0700615 /*
616 * Sanity check the voltages that the card claims to
617 * support.
618 */
619 if (ocr & 0x7F) {
620 printk(KERN_WARNING "%s: card claims to support voltages "
621 "below the defined range. These will be ignored.\n",
622 mmc_hostname(host));
623 ocr &= ~0x7F;
624 }
625
626 if (ocr & MMC_VDD_165_195) {
627 printk(KERN_WARNING "%s: SD card claims to support the "
628 "incompletely defined 'low voltage range'. This "
629 "will be ignored.\n", mmc_hostname(host));
630 ocr &= ~MMC_VDD_165_195;
631 }
632
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100633 host->ocr = mmc_select_voltage(host, ocr);
634
635 /*
636 * Can we support the voltage(s) of the card(s)?
637 */
Pierre Ossman109b5be2007-07-23 00:12:10 +0200638 if (!host->ocr) {
639 err = -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100640 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200641 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100642
643 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200644 * Detect and init the card.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100645 */
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200646 err = mmc_sd_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +0200647 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100648 goto err;
649
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100650 mmc_release_host(host);
651
Pierre Ossman4101c162007-05-19 13:39:01 +0200652 err = mmc_add_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100653 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200654 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100655
656 return 0;
657
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200658remove_card:
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200659 mmc_remove_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100660 host->card = NULL;
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200661 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100662err:
663 mmc_detach_bus(host);
664 mmc_release_host(host);
665
Pierre Ossman109b5be2007-07-23 00:12:10 +0200666 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
667 mmc_hostname(host), err);
668
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100669 return 0;
670}
671