blob: d1c1e0f592f19d56f1b200aa4233f1c95e125054 [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
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100169 resp[3] = card->raw_scr[1];
170 resp[2] = card->raw_scr[0];
171
172 scr_struct = UNSTUFF_BITS(resp, 60, 4);
173 if (scr_struct != 0) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200174 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100175 mmc_hostname(card->host), scr_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200176 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100177 }
178
179 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
180 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200181
182 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100183}
184
185/*
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200186 * Fetches and decodes switch information
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100187 */
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200188static int mmc_read_switch(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100189{
190 int err;
191 u8 *status;
192
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200193 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200194 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200195
196 if (!(card->csd.cmdclass & CCC_SWITCH)) {
197 printk(KERN_WARNING "%s: card lacks mandatory switch "
198 "function, performance might suffer.\n",
199 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200200 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200201 }
202
Pierre Ossman17b04292007-07-22 22:18:46 +0200203 err = -EIO;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100204
205 status = kmalloc(64, GFP_KERNEL);
206 if (!status) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200207 printk(KERN_ERR "%s: could not allocate a buffer for "
208 "switch capabilities.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200209 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100210 }
211
212 err = mmc_sd_switch(card, 0, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200213 if (err) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200214 /*
215 * We all hosts that cannot perform the command
216 * to fail more gracefully
217 */
218 if (err != -EINVAL)
219 goto out;
220
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200221 printk(KERN_WARNING "%s: problem reading switch "
222 "capabilities, performance might suffer.\n",
223 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200224 err = 0;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200225
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100226 goto out;
227 }
228
229 if (status[13] & 0x02)
230 card->sw_caps.hs_max_dtr = 50000000;
231
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200232out:
233 kfree(status);
234
235 return err;
236}
237
238/*
239 * Test if the card supports high-speed mode and, if so, switch to it.
240 */
241static int mmc_switch_hs(struct mmc_card *card)
242{
243 int err;
244 u8 *status;
245
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200246 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200247 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200248
249 if (!(card->csd.cmdclass & CCC_SWITCH))
Pierre Ossman17b04292007-07-22 22:18:46 +0200250 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200251
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200252 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
Pierre Ossman17b04292007-07-22 22:18:46 +0200253 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200254
255 if (card->sw_caps.hs_max_dtr == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200256 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200257
Pierre Ossman17b04292007-07-22 22:18:46 +0200258 err = -EIO;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200259
260 status = kmalloc(64, GFP_KERNEL);
261 if (!status) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200262 printk(KERN_ERR "%s: could not allocate a buffer for "
263 "switch capabilities.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200264 return -ENOMEM;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200265 }
266
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100267 err = mmc_sd_switch(card, 1, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200268 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100269 goto out;
270
271 if ((status[16] & 0xF) != 1) {
272 printk(KERN_WARNING "%s: Problem switching card "
273 "into high-speed mode!\n",
274 mmc_hostname(card->host));
275 } else {
276 mmc_card_set_highspeed(card);
277 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
278 }
279
280out:
281 kfree(status);
282
283 return err;
284}
285
286/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200287 * Handle the detection and initialisation of a card.
288 *
289 * In the case of a resume, "curcard" will contain the card
290 * we're trying to reinitialise.
291 */
292static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
293 struct mmc_card *oldcard)
294{
295 struct mmc_card *card;
296 int err;
297 u32 cid[4];
298 unsigned int max_dtr;
299
300 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200301 WARN_ON(!host->claimed);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200302
303 /*
304 * Since we're changing the OCR value, we seem to
305 * need to tell some cards to go back to the idle
306 * state. We wait 1ms to give cards time to
307 * respond.
308 */
309 mmc_go_idle(host);
310
311 /*
312 * If SD_SEND_IF_COND indicates an SD 2.0
313 * compliant card and we should set bit 30
314 * of the ocr to indicate that we can handle
315 * block-addressed SDHC cards.
316 */
317 err = mmc_send_if_cond(host, ocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200318 if (!err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200319 ocr |= 1 << 30;
320
321 err = mmc_send_app_op_cond(host, ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +0200322 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200323 goto err;
324
325 /*
David Brownellaf517152007-08-08 09:11:32 -0700326 * For SPI, enable CRC as appropriate.
327 */
328 if (mmc_host_is_spi(host)) {
329 err = mmc_spi_set_crc(host, use_spi_crc);
330 if (err)
331 goto err;
332 }
333
334 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200335 * Fetch CID from card.
336 */
David Brownellaf517152007-08-08 09:11:32 -0700337 if (mmc_host_is_spi(host))
338 err = mmc_send_cid(host, cid);
339 else
340 err = mmc_all_send_cid(host, cid);
Pierre Ossman17b04292007-07-22 22:18:46 +0200341 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200342 goto err;
343
344 if (oldcard) {
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200345 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
346 err = -ENOENT;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200347 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200348 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200349
350 card = oldcard;
351 } else {
352 /*
353 * Allocate card structure.
354 */
355 card = mmc_alloc_card(host);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200356 if (IS_ERR(card)) {
357 err = PTR_ERR(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200358 goto err;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200359 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200360
361 card->type = MMC_TYPE_SD;
362 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
363 }
364
365 /*
David Brownellaf517152007-08-08 09:11:32 -0700366 * For native busses: get card RCA and quit open drain mode.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200367 */
David Brownellaf517152007-08-08 09:11:32 -0700368 if (!mmc_host_is_spi(host)) {
369 err = mmc_send_relative_addr(host, &card->rca);
370 if (err)
371 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200372
David Brownellaf517152007-08-08 09:11:32 -0700373 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
374 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200375
376 if (!oldcard) {
377 /*
378 * Fetch CSD from card.
379 */
380 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200381 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200382 goto free_card;
383
Pierre Ossmanbd766312007-05-01 16:11:57 +0200384 err = mmc_decode_csd(card);
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200385 if (err)
Pierre Ossmanbd766312007-05-01 16:11:57 +0200386 goto free_card;
387
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200388 mmc_decode_cid(card);
389 }
390
391 /*
392 * Select card, as all following commands rely on that.
393 */
David Brownellaf517152007-08-08 09:11:32 -0700394 if (!mmc_host_is_spi(host)) {
395 err = mmc_select_card(card);
396 if (err)
397 goto free_card;
398 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200399
400 if (!oldcard) {
401 /*
402 * Fetch SCR from card.
403 */
404 err = mmc_app_send_scr(card, card->raw_scr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200405 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200406 goto free_card;
407
Pierre Ossmanbd766312007-05-01 16:11:57 +0200408 err = mmc_decode_scr(card);
409 if (err < 0)
410 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200411
412 /*
413 * Fetch switch information from card.
414 */
415 err = mmc_read_switch(card);
Pierre Ossman17b04292007-07-22 22:18:46 +0200416 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200417 goto free_card;
418 }
419
420 /*
421 * Attempt to change to high-speed (if supported)
422 */
423 err = mmc_switch_hs(card);
Pierre Ossman17b04292007-07-22 22:18:46 +0200424 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200425 goto free_card;
426
427 /*
428 * Compute bus speed.
429 */
430 max_dtr = (unsigned int)-1;
431
432 if (mmc_card_highspeed(card)) {
433 if (max_dtr > card->sw_caps.hs_max_dtr)
434 max_dtr = card->sw_caps.hs_max_dtr;
435 } else if (max_dtr > card->csd.max_dtr) {
436 max_dtr = card->csd.max_dtr;
437 }
438
439 mmc_set_clock(host, max_dtr);
440
441 /*
442 * Switch to wider bus (if supported).
443 */
Pierre Ossman71651292007-06-06 20:23:25 +0200444 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200445 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
446 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
Pierre Ossman17b04292007-07-22 22:18:46 +0200447 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200448 goto free_card;
449
450 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
451 }
452
Pierre Ossmanc3bff2e2007-06-13 19:06:03 +0200453 /*
454 * Check if read-only switch is active.
455 */
456 if (!oldcard) {
457 if (!host->ops->get_ro) {
458 printk(KERN_WARNING "%s: host does not "
459 "support reading read-only "
460 "switch. assuming write-enable.\n",
461 mmc_hostname(host));
462 } else {
463 if (host->ops->get_ro(host))
464 mmc_card_set_readonly(card);
465 }
466 }
467
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200468 if (!oldcard)
469 host->card = card;
470
Pierre Ossman17b04292007-07-22 22:18:46 +0200471 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200472
473free_card:
474 if (!oldcard)
475 mmc_remove_card(card);
476err:
477
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200478 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200479}
480
481/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100482 * Host is being removed. Free up the current card.
483 */
484static void mmc_sd_remove(struct mmc_host *host)
485{
486 BUG_ON(!host);
487 BUG_ON(!host->card);
488
489 mmc_remove_card(host->card);
490 host->card = NULL;
491}
492
493/*
494 * Card detection callback from host.
495 */
496static void mmc_sd_detect(struct mmc_host *host)
497{
498 int err;
499
500 BUG_ON(!host);
501 BUG_ON(!host->card);
502
503 mmc_claim_host(host);
504
505 /*
506 * Just check if our card has been removed.
507 */
508 err = mmc_send_status(host->card, NULL);
509
510 mmc_release_host(host);
511
Pierre Ossman17b04292007-07-22 22:18:46 +0200512 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +0200513 mmc_sd_remove(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100514
515 mmc_claim_host(host);
516 mmc_detach_bus(host);
517 mmc_release_host(host);
518 }
519}
520
Pierre Ossman4101c162007-05-19 13:39:01 +0200521MMC_ATTR_FN(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
522 card->raw_cid[2], card->raw_cid[3]);
523MMC_ATTR_FN(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
524 card->raw_csd[2], card->raw_csd[3]);
525MMC_ATTR_FN(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
526MMC_ATTR_FN(date, "%02d/%04d\n", card->cid.month, card->cid.year);
527MMC_ATTR_FN(fwrev, "0x%x\n", card->cid.fwrev);
528MMC_ATTR_FN(hwrev, "0x%x\n", card->cid.hwrev);
529MMC_ATTR_FN(manfid, "0x%06x\n", card->cid.manfid);
530MMC_ATTR_FN(name, "%s\n", card->cid.prod_name);
531MMC_ATTR_FN(oemid, "0x%04x\n", card->cid.oemid);
532MMC_ATTR_FN(serial, "0x%08x\n", card->cid.serial);
533
534static struct device_attribute mmc_sd_dev_attrs[] = {
535 MMC_ATTR_RO(cid),
536 MMC_ATTR_RO(csd),
537 MMC_ATTR_RO(scr),
538 MMC_ATTR_RO(date),
539 MMC_ATTR_RO(fwrev),
540 MMC_ATTR_RO(hwrev),
541 MMC_ATTR_RO(manfid),
542 MMC_ATTR_RO(name),
543 MMC_ATTR_RO(oemid),
544 MMC_ATTR_RO(serial),
545 __ATTR_NULL,
546};
547
548/*
549 * Adds sysfs entries as relevant.
550 */
551static int mmc_sd_sysfs_add(struct mmc_host *host, struct mmc_card *card)
552{
553 int ret;
554
555 ret = mmc_add_attrs(card, mmc_sd_dev_attrs);
556 if (ret < 0)
557 return ret;
558
559 return 0;
560}
561
562/*
563 * Removes the sysfs entries added by mmc_sysfs_add().
564 */
565static void mmc_sd_sysfs_remove(struct mmc_host *host, struct mmc_card *card)
566{
567 mmc_remove_attrs(card, mmc_sd_dev_attrs);
568}
569
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200570#ifdef CONFIG_MMC_UNSAFE_RESUME
571
572/*
573 * Suspend callback from host.
574 */
575static void mmc_sd_suspend(struct mmc_host *host)
576{
577 BUG_ON(!host);
578 BUG_ON(!host->card);
579
580 mmc_claim_host(host);
David Brownellaf517152007-08-08 09:11:32 -0700581 if (!mmc_host_is_spi(host))
582 mmc_deselect_cards(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200583 host->card->state &= ~MMC_STATE_HIGHSPEED;
584 mmc_release_host(host);
585}
586
587/*
588 * Resume callback from host.
589 *
590 * This function tries to determine if the same card is still present
591 * and, if so, restore all state to it.
592 */
593static void mmc_sd_resume(struct mmc_host *host)
594{
595 int err;
596
597 BUG_ON(!host);
598 BUG_ON(!host->card);
599
600 mmc_claim_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200601 err = mmc_sd_init_card(host, host->ocr, host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200602 mmc_release_host(host);
603
Pierre Ossman17b04292007-07-22 22:18:46 +0200604 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +0200605 mmc_sd_remove(host);
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200606
607 mmc_claim_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200608 mmc_detach_bus(host);
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200609 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200610 }
611
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200612}
613
614#else
615
616#define mmc_sd_suspend NULL
617#define mmc_sd_resume NULL
618
619#endif
620
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100621static const struct mmc_bus_ops mmc_sd_ops = {
622 .remove = mmc_sd_remove,
623 .detect = mmc_sd_detect,
Pierre Ossman4101c162007-05-19 13:39:01 +0200624 .sysfs_add = mmc_sd_sysfs_add,
625 .sysfs_remove = mmc_sd_sysfs_remove,
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200626 .suspend = mmc_sd_suspend,
627 .resume = mmc_sd_resume,
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100628};
629
630/*
631 * Starting point for SD card init.
632 */
633int mmc_attach_sd(struct mmc_host *host, u32 ocr)
634{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100635 int err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100636
637 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200638 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100639
640 mmc_attach_bus(host, &mmc_sd_ops);
641
Philip Langdale55556da2007-03-16 19:39:00 -0700642 /*
David Brownellaf517152007-08-08 09:11:32 -0700643 * We need to get OCR a different way for SPI.
644 */
645 if (mmc_host_is_spi(host)) {
646 mmc_go_idle(host);
647
648 err = mmc_spi_read_ocr(host, 0, &ocr);
649 if (err)
650 goto err;
651 }
652
653 /*
Philip Langdale55556da2007-03-16 19:39:00 -0700654 * Sanity check the voltages that the card claims to
655 * support.
656 */
657 if (ocr & 0x7F) {
658 printk(KERN_WARNING "%s: card claims to support voltages "
659 "below the defined range. These will be ignored.\n",
660 mmc_hostname(host));
661 ocr &= ~0x7F;
662 }
663
664 if (ocr & MMC_VDD_165_195) {
665 printk(KERN_WARNING "%s: SD card claims to support the "
666 "incompletely defined 'low voltage range'. This "
667 "will be ignored.\n", mmc_hostname(host));
668 ocr &= ~MMC_VDD_165_195;
669 }
670
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100671 host->ocr = mmc_select_voltage(host, ocr);
672
673 /*
674 * Can we support the voltage(s) of the card(s)?
675 */
Pierre Ossman109b5be2007-07-23 00:12:10 +0200676 if (!host->ocr) {
677 err = -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100678 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +0200679 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100680
681 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200682 * Detect and init the card.
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100683 */
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200684 err = mmc_sd_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +0200685 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100686 goto err;
687
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100688 mmc_release_host(host);
689
Pierre Ossman4101c162007-05-19 13:39:01 +0200690 err = mmc_add_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100691 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200692 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100693
694 return 0;
695
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200696remove_card:
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200697 mmc_remove_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100698 host->card = NULL;
Pierre Ossman2986d0b2007-07-22 17:52:06 +0200699 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100700err:
701 mmc_detach_bus(host);
702 mmc_release_host(host);
703
Pierre Ossman109b5be2007-07-23 00:12:10 +0200704 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
705 mmc_hostname(host), err);
706
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200707 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100708}
709