mmc: sd: add support for uhs bus speed mode selection
[linux-2.6-block.git] / drivers / mmc / core / sd.c
CommitLineData
7ea239d9 1/*
70f10482 2 * linux/drivers/mmc/core/sd.c
7ea239d9
PO
3 *
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>
5a0e3ad6 14#include <linux/slab.h>
7ea239d9
PO
15
16#include <linux/mmc/host.h>
17#include <linux/mmc/card.h>
18#include <linux/mmc/mmc.h>
3373c0ae 19#include <linux/mmc/sd.h>
7ea239d9
PO
20
21#include "core.h"
4101c16a 22#include "bus.h"
7ea239d9 23#include "mmc_ops.h"
ce101496 24#include "sd.h"
7ea239d9
PO
25#include "sd_ops.h"
26
7ea239d9
PO
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 */
71578a1e 63void mmc_decode_cid(struct mmc_card *card)
7ea239d9
PO
64{
65 u32 *resp = card->raw_cid;
66
67 memset(&card->cid, 0, sizeof(struct mmc_cid));
68
69 /*
70 * SD doesn't currently have a version field so we will
71 * have to assume we can parse this.
72 */
73 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
74 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
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.hwrev = UNSTUFF_BITS(resp, 60, 4);
81 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
82 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
83 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
84 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
85
86 card->cid.year += 2000; /* SD cards year offset */
87}
88
89/*
90 * Given a 128-bit response, decode to our card CSD structure.
91 */
bd766312 92static int mmc_decode_csd(struct mmc_card *card)
7ea239d9
PO
93{
94 struct mmc_csd *csd = &card->csd;
95 unsigned int e, m, csd_struct;
96 u32 *resp = card->raw_csd;
97
98 csd_struct = UNSTUFF_BITS(resp, 126, 2);
99
100 switch (csd_struct) {
101 case 0:
102 m = UNSTUFF_BITS(resp, 115, 4);
103 e = UNSTUFF_BITS(resp, 112, 3);
104 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
105 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
106
107 m = UNSTUFF_BITS(resp, 99, 4);
108 e = UNSTUFF_BITS(resp, 96, 3);
109 csd->max_dtr = tran_exp[e] * tran_mant[m];
110 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
111
112 e = UNSTUFF_BITS(resp, 47, 3);
113 m = UNSTUFF_BITS(resp, 62, 12);
114 csd->capacity = (1 + m) << (e + 2);
115
116 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
117 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
118 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
119 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
120 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
121 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
122 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
dfe86cba
AH
123
124 if (UNSTUFF_BITS(resp, 46, 1)) {
125 csd->erase_size = 1;
126 } else if (csd->write_blkbits >= 9) {
127 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
128 csd->erase_size <<= csd->write_blkbits - 9;
129 }
7ea239d9
PO
130 break;
131 case 1:
132 /*
133 * This is a block-addressed SDHC card. Most
134 * interesting fields are unused and have fixed
135 * values. To avoid getting tripped by buggy cards,
136 * we assume those fixed values ourselves.
137 */
138 mmc_card_set_blockaddr(card);
139
140 csd->tacc_ns = 0; /* Unused */
141 csd->tacc_clks = 0; /* Unused */
142
143 m = UNSTUFF_BITS(resp, 99, 4);
144 e = UNSTUFF_BITS(resp, 96, 3);
145 csd->max_dtr = tran_exp[e] * tran_mant[m];
146 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
147
148 m = UNSTUFF_BITS(resp, 48, 22);
149 csd->capacity = (1 + m) << 10;
150
151 csd->read_blkbits = 9;
152 csd->read_partial = 0;
153 csd->write_misalign = 0;
154 csd->read_misalign = 0;
155 csd->r2w_factor = 4; /* Unused */
156 csd->write_blkbits = 9;
157 csd->write_partial = 0;
dfe86cba 158 csd->erase_size = 1;
7ea239d9
PO
159 break;
160 default:
facba917 161 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
7ea239d9 162 mmc_hostname(card->host), csd_struct);
bd766312 163 return -EINVAL;
7ea239d9 164 }
bd766312 165
dfe86cba
AH
166 card->erase_size = csd->erase_size;
167
bd766312 168 return 0;
7ea239d9
PO
169}
170
171/*
172 * Given a 64-bit response, decode to our card SCR structure.
173 */
bd766312 174static int mmc_decode_scr(struct mmc_card *card)
7ea239d9
PO
175{
176 struct sd_scr *scr = &card->scr;
177 unsigned int scr_struct;
178 u32 resp[4];
179
7ea239d9
PO
180 resp[3] = card->raw_scr[1];
181 resp[2] = card->raw_scr[0];
182
183 scr_struct = UNSTUFF_BITS(resp, 60, 4);
184 if (scr_struct != 0) {
facba917 185 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
7ea239d9 186 mmc_hostname(card->host), scr_struct);
bd766312 187 return -EINVAL;
7ea239d9
PO
188 }
189
190 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
191 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
013909c4
AN
192 if (scr->sda_vsn == SCR_SPEC_VER_2)
193 /* Check if Physical Layer Spec v3.0 is supported */
194 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
bd766312 195
dfe86cba
AH
196 if (UNSTUFF_BITS(resp, 55, 1))
197 card->erased_byte = 0xFF;
198 else
199 card->erased_byte = 0x0;
200
bd766312 201 return 0;
7ea239d9
PO
202}
203
dfe86cba
AH
204/*
205 * Fetch and process SD Status register.
206 */
207static int mmc_read_ssr(struct mmc_card *card)
208{
209 unsigned int au, es, et, eo;
210 int err, i;
211 u32 *ssr;
212
213 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
214 printk(KERN_WARNING "%s: card lacks mandatory SD Status "
215 "function.\n", mmc_hostname(card->host));
216 return 0;
217 }
218
219 ssr = kmalloc(64, GFP_KERNEL);
220 if (!ssr)
221 return -ENOMEM;
222
223 err = mmc_app_sd_status(card, ssr);
224 if (err) {
225 printk(KERN_WARNING "%s: problem reading SD Status "
226 "register.\n", mmc_hostname(card->host));
227 err = 0;
228 goto out;
229 }
230
231 for (i = 0; i < 16; i++)
232 ssr[i] = be32_to_cpu(ssr[i]);
233
234 /*
235 * UNSTUFF_BITS only works with four u32s so we have to offset the
236 * bitfield positions accordingly.
237 */
238 au = UNSTUFF_BITS(ssr, 428 - 384, 4);
239 if (au > 0 || au <= 9) {
240 card->ssr.au = 1 << (au + 4);
241 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
242 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
243 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
244 if (es && et) {
245 card->ssr.erase_timeout = (et * 1000) / es;
246 card->ssr.erase_offset = eo * 1000;
247 }
248 } else {
249 printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit "
250 "size.\n", mmc_hostname(card->host));
251 }
252out:
253 kfree(ssr);
254 return err;
255}
256
7ea239d9 257/*
1addfcdb 258 * Fetches and decodes switch information
7ea239d9 259 */
1addfcdb 260static int mmc_read_switch(struct mmc_card *card)
7ea239d9
PO
261{
262 int err;
263 u8 *status;
264
3373c0ae 265 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
17b0429d 266 return 0;
3373c0ae
PO
267
268 if (!(card->csd.cmdclass & CCC_SWITCH)) {
269 printk(KERN_WARNING "%s: card lacks mandatory switch "
270 "function, performance might suffer.\n",
271 mmc_hostname(card->host));
17b0429d 272 return 0;
3373c0ae
PO
273 }
274
17b0429d 275 err = -EIO;
7ea239d9
PO
276
277 status = kmalloc(64, GFP_KERNEL);
278 if (!status) {
facba917 279 printk(KERN_ERR "%s: could not allocate a buffer for "
013909c4
AN
280 "switch capabilities.\n",
281 mmc_hostname(card->host));
17b0429d 282 return -ENOMEM;
7ea239d9
PO
283 }
284
013909c4 285 /* Find out the supported Bus Speed Modes. */
7ea239d9 286 err = mmc_sd_switch(card, 0, 0, 1, status);
17b0429d 287 if (err) {
013909c4
AN
288 /*
289 * If the host or the card can't do the switch,
290 * fail more gracefully.
291 */
292 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
adf66a0d
PO
293 goto out;
294
013909c4 295 printk(KERN_WARNING "%s: problem reading Bus Speed modes.\n",
3373c0ae 296 mmc_hostname(card->host));
17b0429d 297 err = 0;
adf66a0d 298
7ea239d9
PO
299 goto out;
300 }
301
013909c4
AN
302 if (card->scr.sda_spec3) {
303 card->sw_caps.sd3_bus_mode = status[13];
304
305 /* Find out Driver Strengths supported by the card */
306 err = mmc_sd_switch(card, 0, 2, 1, status);
307 if (err) {
308 /*
309 * If the host or the card can't do the switch,
310 * fail more gracefully.
311 */
312 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
313 goto out;
314
315 printk(KERN_WARNING "%s: problem reading "
316 "Driver Strength.\n",
317 mmc_hostname(card->host));
318 err = 0;
319
320 goto out;
321 }
322
323 card->sw_caps.sd3_drv_type = status[9];
324
325 /* Find out Current Limits supported by the card */
326 err = mmc_sd_switch(card, 0, 3, 1, status);
327 if (err) {
328 /*
329 * If the host or the card can't do the switch,
330 * fail more gracefully.
331 */
332 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
333 goto out;
334
335 printk(KERN_WARNING "%s: problem reading "
336 "Current Limit.\n",
337 mmc_hostname(card->host));
338 err = 0;
339
340 goto out;
341 }
342
343 card->sw_caps.sd3_curr_limit = status[7];
344 } else {
345 if (status[13] & 0x02)
346 card->sw_caps.hs_max_dtr = 50000000;
347 }
7ea239d9 348
1addfcdb
PO
349out:
350 kfree(status);
351
352 return err;
353}
354
355/*
356 * Test if the card supports high-speed mode and, if so, switch to it.
357 */
71578a1e 358int mmc_sd_switch_hs(struct mmc_card *card)
1addfcdb
PO
359{
360 int err;
361 u8 *status;
362
3373c0ae 363 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
17b0429d 364 return 0;
3373c0ae
PO
365
366 if (!(card->csd.cmdclass & CCC_SWITCH))
17b0429d 367 return 0;
3373c0ae 368
1addfcdb 369 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
17b0429d 370 return 0;
1addfcdb
PO
371
372 if (card->sw_caps.hs_max_dtr == 0)
17b0429d 373 return 0;
1addfcdb 374
17b0429d 375 err = -EIO;
1addfcdb
PO
376
377 status = kmalloc(64, GFP_KERNEL);
378 if (!status) {
facba917
PO
379 printk(KERN_ERR "%s: could not allocate a buffer for "
380 "switch capabilities.\n", mmc_hostname(card->host));
17b0429d 381 return -ENOMEM;
1addfcdb
PO
382 }
383
7ea239d9 384 err = mmc_sd_switch(card, 1, 0, 1, status);
17b0429d 385 if (err)
7ea239d9
PO
386 goto out;
387
388 if ((status[16] & 0xF) != 1) {
389 printk(KERN_WARNING "%s: Problem switching card "
390 "into high-speed mode!\n",
391 mmc_hostname(card->host));
71578a1e 392 err = 0;
7ea239d9 393 } else {
71578a1e 394 err = 1;
7ea239d9
PO
395 }
396
397out:
398 kfree(status);
399
400 return err;
401}
402
d6d50a15
AN
403static int sd_select_driver_type(struct mmc_card *card, u8 *status)
404{
405 int host_drv_type = 0, card_drv_type = 0;
406 int err;
407
408 /*
409 * If the host doesn't support any of the Driver Types A,C or D,
410 * default Driver Type B is used.
411 */
412 if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
413 | MMC_CAP_DRIVER_TYPE_D)))
414 return 0;
415
416 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A) {
417 host_drv_type = MMC_SET_DRIVER_TYPE_A;
418 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
419 card_drv_type = MMC_SET_DRIVER_TYPE_A;
420 else if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_B)
421 card_drv_type = MMC_SET_DRIVER_TYPE_B;
422 else if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
423 card_drv_type = MMC_SET_DRIVER_TYPE_C;
424 } else if (card->host->caps & MMC_CAP_DRIVER_TYPE_C) {
425 host_drv_type = MMC_SET_DRIVER_TYPE_C;
426 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
427 card_drv_type = MMC_SET_DRIVER_TYPE_C;
428 } else if (!(card->host->caps & MMC_CAP_DRIVER_TYPE_D)) {
429 /*
430 * If we are here, that means only the default driver type
431 * B is supported by the host.
432 */
433 host_drv_type = MMC_SET_DRIVER_TYPE_B;
434 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_B)
435 card_drv_type = MMC_SET_DRIVER_TYPE_B;
436 else if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
437 card_drv_type = MMC_SET_DRIVER_TYPE_C;
438 }
439
440 err = mmc_sd_switch(card, 1, 2, card_drv_type, status);
441 if (err)
442 return err;
443
444 if ((status[15] & 0xF) != card_drv_type) {
445 printk(KERN_WARNING "%s: Problem setting driver strength!\n",
446 mmc_hostname(card->host));
447 return 0;
448 }
449
450 mmc_set_driver_type(card->host, host_drv_type);
451
452 return 0;
453}
454
49c468fc
AN
455static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
456{
457 unsigned int bus_speed = 0, timing = 0;
458 int err;
459
460 /*
461 * If the host doesn't support any of the UHS-I modes, fallback on
462 * default speed.
463 */
464 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
465 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50)))
466 return 0;
467
468 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
469 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
470 bus_speed = UHS_SDR104_BUS_SPEED;
471 timing = MMC_TIMING_UHS_SDR104;
472 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
473 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
474 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
475 bus_speed = UHS_DDR50_BUS_SPEED;
476 timing = MMC_TIMING_UHS_DDR50;
477 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
478 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
479 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
480 SD_MODE_UHS_SDR50)) {
481 bus_speed = UHS_SDR50_BUS_SPEED;
482 timing = MMC_TIMING_UHS_SDR50;
483 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
484 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
485 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
486 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
487 bus_speed = UHS_SDR25_BUS_SPEED;
488 timing = MMC_TIMING_UHS_SDR25;
489 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
490 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
491 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
492 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
493 SD_MODE_UHS_SDR12)) {
494 bus_speed = UHS_SDR12_BUS_SPEED;
495 timing = MMC_TIMING_UHS_SDR12;
496 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
497 }
498
499 card->sd_bus_speed = bus_speed;
500 err = mmc_sd_switch(card, 1, 0, bus_speed, status);
501 if (err)
502 return err;
503
504 if ((status[16] & 0xF) != bus_speed)
505 printk(KERN_WARNING "%s: Problem setting bus speed mode!\n",
506 mmc_hostname(card->host));
507 else {
508 mmc_set_timing(card->host, timing);
509 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
510 }
511
512 return 0;
513}
514
d6d50a15
AN
515/*
516 * UHS-I specific initialization procedure
517 */
518static int mmc_sd_init_uhs_card(struct mmc_card *card)
519{
520 int err;
521 u8 *status;
522
523 if (!card->scr.sda_spec3)
524 return 0;
525
526 if (!(card->csd.cmdclass & CCC_SWITCH))
527 return 0;
528
529 status = kmalloc(64, GFP_KERNEL);
530 if (!status) {
531 printk(KERN_ERR "%s: could not allocate a buffer for "
532 "switch capabilities.\n", mmc_hostname(card->host));
533 return -ENOMEM;
534 }
535
536 /* Set 4-bit bus width */
537 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
538 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
539 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
540 if (err)
541 goto out;
542
543 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
544 }
545
546 /* Set the driver strength for the card */
547 err = sd_select_driver_type(card, status);
49c468fc
AN
548 if (err)
549 goto out;
550
551 /* Set bus speed mode of the card */
552 err = sd_set_bus_speed_mode(card, status);
d6d50a15
AN
553
554out:
555 kfree(status);
556
557 return err;
558}
559
51ec92e2
PO
560MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
561 card->raw_cid[2], card->raw_cid[3]);
562MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
563 card->raw_csd[2], card->raw_csd[3]);
564MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
565MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
dfe86cba
AH
566MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
567MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
51ec92e2
PO
568MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
569MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
570MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
571MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
572MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
573MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
574
575
576static struct attribute *sd_std_attrs[] = {
577 &dev_attr_cid.attr,
578 &dev_attr_csd.attr,
579 &dev_attr_scr.attr,
580 &dev_attr_date.attr,
dfe86cba
AH
581 &dev_attr_erase_size.attr,
582 &dev_attr_preferred_erase_size.attr,
51ec92e2
PO
583 &dev_attr_fwrev.attr,
584 &dev_attr_hwrev.attr,
585 &dev_attr_manfid.attr,
586 &dev_attr_name.attr,
587 &dev_attr_oemid.attr,
588 &dev_attr_serial.attr,
589 NULL,
590};
591
592static struct attribute_group sd_std_attr_group = {
593 .attrs = sd_std_attrs,
594};
595
a4dbd674 596static const struct attribute_group *sd_attr_groups[] = {
51ec92e2
PO
597 &sd_std_attr_group,
598 NULL,
599};
600
71578a1e 601struct device_type sd_type = {
51ec92e2
PO
602 .groups = sd_attr_groups,
603};
604
7ea239d9 605/*
71578a1e 606 * Fetch CID from card.
7ea239d9 607 */
d6d50a15 608int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
7ea239d9 609{
7ea239d9 610 int err;
7ea239d9 611
7ea239d9
PO
612 /*
613 * Since we're changing the OCR value, we seem to
614 * need to tell some cards to go back to the idle
615 * state. We wait 1ms to give cards time to
616 * respond.
617 */
618 mmc_go_idle(host);
619
620 /*
621 * If SD_SEND_IF_COND indicates an SD 2.0
622 * compliant card and we should set bit 30
623 * of the ocr to indicate that we can handle
624 * block-addressed SDHC cards.
625 */
6abaa0c9 626 err = mmc_send_if_cond(host, ocr);
17b0429d 627 if (!err)
f2119df6 628 ocr |= SD_OCR_CCS;
7ea239d9 629
f2119df6
AN
630 /*
631 * If the host supports one of UHS-I modes, request the card
632 * to switch to 1.8V signaling level.
633 */
634 if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
635 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
636 ocr |= SD_OCR_S18R;
637
638 /* If the host can supply more than 150mA, XPC should be set to 1. */
639 if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
640 MMC_CAP_SET_XPC_180))
641 ocr |= SD_OCR_XPC;
642
643try_again:
d6d50a15 644 err = mmc_send_app_op_cond(host, ocr, rocr);
17b0429d 645 if (err)
71578a1e 646 return err;
7ea239d9 647
f2119df6
AN
648 /*
649 * In case CCS and S18A in the response is set, start Signal Voltage
650 * Switch procedure. SPI mode doesn't support CMD11.
651 */
d6d50a15
AN
652 if (!mmc_host_is_spi(host) && rocr &&
653 ((*rocr & 0x41000000) == 0x41000000)) {
f2119df6
AN
654 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
655 if (err) {
656 ocr &= ~SD_OCR_S18R;
657 goto try_again;
658 }
659 }
660
af517150
DB
661 if (mmc_host_is_spi(host))
662 err = mmc_send_cid(host, cid);
663 else
664 err = mmc_all_send_cid(host, cid);
71578a1e
MM
665
666 return err;
667}
668
669int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
670{
671 int err;
672
673 /*
674 * Fetch CSD from card.
675 */
676 err = mmc_send_csd(card, card->raw_csd);
17b0429d 677 if (err)
71578a1e 678 return err;
7ea239d9 679
71578a1e
MM
680 err = mmc_decode_csd(card);
681 if (err)
682 return err;
683
684 return 0;
685}
686
687int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
688 bool reinit)
689{
690 int err;
691
692 if (!reinit) {
693 /*
694 * Fetch SCR from card.
695 */
696 err = mmc_app_send_scr(card, card->raw_scr);
697 if (err)
698 return err;
699
700 err = mmc_decode_scr(card);
701 if (err)
702 return err;
703
dfe86cba
AH
704 /*
705 * Fetch and process SD Status register.
706 */
707 err = mmc_read_ssr(card);
708 if (err)
709 return err;
710
711 /* Erase init depends on CSD and SSR */
712 mmc_init_erase(card);
713
71578a1e
MM
714 /*
715 * Fetch switch information from card.
716 */
717 err = mmc_read_switch(card);
718 if (err)
719 return err;
720 }
721
722 /*
723 * For SPI, enable CRC as appropriate.
724 * This CRC enable is located AFTER the reading of the
725 * card registers because some SDHC cards are not able
726 * to provide valid CRCs for non-512-byte blocks.
727 */
728 if (mmc_host_is_spi(host)) {
729 err = mmc_spi_set_crc(host, use_spi_crc);
730 if (err)
731 return err;
732 }
733
734 /*
735 * Check if read-only switch is active.
736 */
737 if (!reinit) {
738 int ro = -1;
739
740 if (host->ops->get_ro)
741 ro = host->ops->get_ro(host);
742
743 if (ro < 0) {
744 printk(KERN_WARNING "%s: host does not "
745 "support reading read-only "
746 "switch. assuming write-enable.\n",
747 mmc_hostname(host));
748 } else if (ro > 0) {
749 mmc_card_set_readonly(card);
adf66a0d 750 }
71578a1e
MM
751 }
752
753 return 0;
754}
755
756unsigned mmc_sd_get_max_clock(struct mmc_card *card)
757{
758 unsigned max_dtr = (unsigned int)-1;
759
760 if (mmc_card_highspeed(card)) {
761 if (max_dtr > card->sw_caps.hs_max_dtr)
762 max_dtr = card->sw_caps.hs_max_dtr;
763 } else if (max_dtr > card->csd.max_dtr) {
764 max_dtr = card->csd.max_dtr;
765 }
766
767 return max_dtr;
768}
769
770void mmc_sd_go_highspeed(struct mmc_card *card)
771{
772 mmc_card_set_highspeed(card);
773 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
774}
775
776/*
777 * Handle the detection and initialisation of a card.
778 *
779 * In the case of a resume, "oldcard" will contain the card
780 * we're trying to reinitialise.
781 */
782static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
783 struct mmc_card *oldcard)
784{
785 struct mmc_card *card;
786 int err;
787 u32 cid[4];
d6d50a15 788 u32 rocr = 0;
71578a1e
MM
789
790 BUG_ON(!host);
791 WARN_ON(!host->claimed);
792
d6d50a15 793 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
71578a1e
MM
794 if (err)
795 return err;
796
797 if (oldcard) {
798 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
799 return -ENOENT;
7ea239d9 800
6abaa0c9
PO
801 card = oldcard;
802 } else {
803 /*
804 * Allocate card structure.
805 */
51ec92e2 806 card = mmc_alloc_card(host, &sd_type);
71578a1e
MM
807 if (IS_ERR(card))
808 return PTR_ERR(card);
6abaa0c9
PO
809
810 card->type = MMC_TYPE_SD;
811 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
812 }
7ea239d9
PO
813
814 /*
af517150 815 * For native busses: get card RCA and quit open drain mode.
7ea239d9 816 */
af517150
DB
817 if (!mmc_host_is_spi(host)) {
818 err = mmc_send_relative_addr(host, &card->rca);
819 if (err)
71578a1e 820 return err;
7ea239d9 821
af517150
DB
822 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
823 }
7ea239d9 824
6abaa0c9 825 if (!oldcard) {
71578a1e 826 err = mmc_sd_get_csd(host, card);
17b0429d 827 if (err)
71578a1e 828 return err;
bd766312 829
6abaa0c9
PO
830 mmc_decode_cid(card);
831 }
7ea239d9
PO
832
833 /*
6abaa0c9 834 * Select card, as all following commands rely on that.
7ea239d9 835 */
af517150
DB
836 if (!mmc_host_is_spi(host)) {
837 err = mmc_select_card(card);
838 if (err)
71578a1e 839 return err;
6abaa0c9 840 }
1addfcdb 841
71578a1e
MM
842 err = mmc_sd_setup_card(host, card, oldcard != NULL);
843 if (err)
844 goto free_card;
9d9f25c0 845
d6d50a15
AN
846 /* Initialization sequence for UHS-I cards */
847 if (rocr & SD_ROCR_S18A) {
848 err = mmc_sd_init_uhs_card(card);
17b0429d 849 if (err)
7ea239d9 850 goto free_card;
d6d50a15
AN
851 } else {
852 /*
853 * Attempt to change to high-speed (if supported)
854 */
855 err = mmc_sd_switch_hs(card);
856 if (err > 0)
857 mmc_sd_go_highspeed(card);
858 else if (err)
859 goto free_card;
860
861 /*
862 * Set bus speed.
863 */
864 mmc_set_clock(host, mmc_sd_get_max_clock(card));
7ea239d9 865
d6d50a15
AN
866 /*
867 * Switch to wider bus (if supported).
868 */
869 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
870 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
871 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
872 if (err)
873 goto free_card;
874
875 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
876 }
7ea239d9
PO
877 }
878
71578a1e 879 host->card = card;
17b0429d 880 return 0;
6abaa0c9
PO
881
882free_card:
883 if (!oldcard)
884 mmc_remove_card(card);
6abaa0c9 885
adf66a0d 886 return err;
6abaa0c9
PO
887}
888
889/*
890 * Host is being removed. Free up the current card.
891 */
892static void mmc_sd_remove(struct mmc_host *host)
893{
894 BUG_ON(!host);
895 BUG_ON(!host->card);
896
897 mmc_remove_card(host->card);
898 host->card = NULL;
899}
900
901/*
902 * Card detection callback from host.
903 */
904static void mmc_sd_detect(struct mmc_host *host)
905{
906 int err;
907
908 BUG_ON(!host);
909 BUG_ON(!host->card);
910
911 mmc_claim_host(host);
912
913 /*
914 * Just check if our card has been removed.
915 */
916 err = mmc_send_status(host->card, NULL);
7ea239d9
PO
917
918 mmc_release_host(host);
919
17b0429d 920 if (err) {
4101c16a 921 mmc_sd_remove(host);
6abaa0c9
PO
922
923 mmc_claim_host(host);
924 mmc_detach_bus(host);
925 mmc_release_host(host);
926 }
927}
928
6abaa0c9
PO
929/*
930 * Suspend callback from host.
931 */
95cdfb72 932static int mmc_sd_suspend(struct mmc_host *host)
6abaa0c9
PO
933{
934 BUG_ON(!host);
935 BUG_ON(!host->card);
936
937 mmc_claim_host(host);
af517150
DB
938 if (!mmc_host_is_spi(host))
939 mmc_deselect_cards(host);
6abaa0c9
PO
940 host->card->state &= ~MMC_STATE_HIGHSPEED;
941 mmc_release_host(host);
95cdfb72
NP
942
943 return 0;
6abaa0c9
PO
944}
945
946/*
947 * Resume callback from host.
948 *
949 * This function tries to determine if the same card is still present
950 * and, if so, restore all state to it.
951 */
95cdfb72 952static int mmc_sd_resume(struct mmc_host *host)
6abaa0c9
PO
953{
954 int err;
955
956 BUG_ON(!host);
957 BUG_ON(!host->card);
958
959 mmc_claim_host(host);
6abaa0c9 960 err = mmc_sd_init_card(host, host->ocr, host->card);
2986d0bf
PO
961 mmc_release_host(host);
962
95cdfb72 963 return err;
6abaa0c9
PO
964}
965
12ae637f 966static int mmc_sd_power_restore(struct mmc_host *host)
eae1aeee 967{
12ae637f
OBC
968 int ret;
969
eae1aeee
AH
970 host->card->state &= ~MMC_STATE_HIGHSPEED;
971 mmc_claim_host(host);
12ae637f 972 ret = mmc_sd_init_card(host, host->ocr, host->card);
eae1aeee 973 mmc_release_host(host);
12ae637f
OBC
974
975 return ret;
eae1aeee
AH
976}
977
6abaa0c9 978static const struct mmc_bus_ops mmc_sd_ops = {
9feae246
AH
979 .remove = mmc_sd_remove,
980 .detect = mmc_sd_detect,
981 .suspend = NULL,
982 .resume = NULL,
eae1aeee 983 .power_restore = mmc_sd_power_restore,
9feae246
AH
984};
985
986static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
6abaa0c9
PO
987 .remove = mmc_sd_remove,
988 .detect = mmc_sd_detect,
989 .suspend = mmc_sd_suspend,
990 .resume = mmc_sd_resume,
eae1aeee 991 .power_restore = mmc_sd_power_restore,
6abaa0c9
PO
992};
993
9feae246
AH
994static void mmc_sd_attach_bus_ops(struct mmc_host *host)
995{
996 const struct mmc_bus_ops *bus_ops;
997
71d7d3d1 998 if (!mmc_card_is_removable(host))
9feae246
AH
999 bus_ops = &mmc_sd_ops_unsafe;
1000 else
1001 bus_ops = &mmc_sd_ops;
1002 mmc_attach_bus(host, bus_ops);
1003}
1004
6abaa0c9
PO
1005/*
1006 * Starting point for SD card init.
1007 */
807e8e40 1008int mmc_attach_sd(struct mmc_host *host)
6abaa0c9
PO
1009{
1010 int err;
807e8e40 1011 u32 ocr;
6abaa0c9
PO
1012
1013 BUG_ON(!host);
d84075c8 1014 WARN_ON(!host->claimed);
6abaa0c9 1015
f2119df6
AN
1016 /* Make sure we are at 3.3V signalling voltage */
1017 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1018 if (err)
1019 return err;
1020
807e8e40
AR
1021 err = mmc_send_app_op_cond(host, 0, &ocr);
1022 if (err)
1023 return err;
1024
9feae246 1025 mmc_sd_attach_bus_ops(host);
8f230f45
TI
1026 if (host->ocr_avail_sd)
1027 host->ocr_avail = host->ocr_avail_sd;
6abaa0c9 1028
af517150
DB
1029 /*
1030 * We need to get OCR a different way for SPI.
1031 */
1032 if (mmc_host_is_spi(host)) {
1033 mmc_go_idle(host);
1034
1035 err = mmc_spi_read_ocr(host, 0, &ocr);
1036 if (err)
1037 goto err;
1038 }
1039
6abaa0c9
PO
1040 /*
1041 * Sanity check the voltages that the card claims to
1042 * support.
1043 */
1044 if (ocr & 0x7F) {
1045 printk(KERN_WARNING "%s: card claims to support voltages "
1046 "below the defined range. These will be ignored.\n",
1047 mmc_hostname(host));
1048 ocr &= ~0x7F;
1049 }
1050
8f230f45
TI
1051 if ((ocr & MMC_VDD_165_195) &&
1052 !(host->ocr_avail_sd & MMC_VDD_165_195)) {
6abaa0c9
PO
1053 printk(KERN_WARNING "%s: SD card claims to support the "
1054 "incompletely defined 'low voltage range'. This "
1055 "will be ignored.\n", mmc_hostname(host));
1056 ocr &= ~MMC_VDD_165_195;
1057 }
1058
1059 host->ocr = mmc_select_voltage(host, ocr);
1060
1061 /*
1062 * Can we support the voltage(s) of the card(s)?
1063 */
109b5bed
PO
1064 if (!host->ocr) {
1065 err = -EINVAL;
6abaa0c9 1066 goto err;
109b5bed 1067 }
6abaa0c9
PO
1068
1069 /*
1070 * Detect and init the card.
1071 */
1072 err = mmc_sd_init_card(host, host->ocr, NULL);
17b0429d 1073 if (err)
6abaa0c9
PO
1074 goto err;
1075
1076 mmc_release_host(host);
4101c16a 1077 err = mmc_add_card(host->card);
807e8e40 1078 mmc_claim_host(host);
7ea239d9 1079 if (err)
2986d0bf 1080 goto remove_card;
7ea239d9
PO
1081
1082 return 0;
1083
2986d0bf 1084remove_card:
807e8e40 1085 mmc_release_host(host);
6abaa0c9 1086 mmc_remove_card(host->card);
7ea239d9 1087 host->card = NULL;
2986d0bf 1088 mmc_claim_host(host);
7ea239d9
PO
1089err:
1090 mmc_detach_bus(host);
7ea239d9 1091
109b5bed
PO
1092 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
1093 mmc_hostname(host), err);
1094
adf66a0d 1095 return err;
7ea239d9
PO
1096}
1097