mmc: sdhci: reset sdclk before setting high speed enable
[deliverable/linux.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
455/*
456 * UHS-I specific initialization procedure
457 */
458static int mmc_sd_init_uhs_card(struct mmc_card *card)
459{
460 int err;
461 u8 *status;
462
463 if (!card->scr.sda_spec3)
464 return 0;
465
466 if (!(card->csd.cmdclass & CCC_SWITCH))
467 return 0;
468
469 status = kmalloc(64, GFP_KERNEL);
470 if (!status) {
471 printk(KERN_ERR "%s: could not allocate a buffer for "
472 "switch capabilities.\n", mmc_hostname(card->host));
473 return -ENOMEM;
474 }
475
476 /* Set 4-bit bus width */
477 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
478 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
479 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
480 if (err)
481 goto out;
482
483 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
484 }
485
486 /* Set the driver strength for the card */
487 err = sd_select_driver_type(card, status);
488
489out:
490 kfree(status);
491
492 return err;
493}
494
51ec92e2
PO
495MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
496 card->raw_cid[2], card->raw_cid[3]);
497MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
498 card->raw_csd[2], card->raw_csd[3]);
499MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
500MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
dfe86cba
AH
501MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
502MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
51ec92e2
PO
503MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
504MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
505MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
506MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
507MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
508MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
509
510
511static struct attribute *sd_std_attrs[] = {
512 &dev_attr_cid.attr,
513 &dev_attr_csd.attr,
514 &dev_attr_scr.attr,
515 &dev_attr_date.attr,
dfe86cba
AH
516 &dev_attr_erase_size.attr,
517 &dev_attr_preferred_erase_size.attr,
51ec92e2
PO
518 &dev_attr_fwrev.attr,
519 &dev_attr_hwrev.attr,
520 &dev_attr_manfid.attr,
521 &dev_attr_name.attr,
522 &dev_attr_oemid.attr,
523 &dev_attr_serial.attr,
524 NULL,
525};
526
527static struct attribute_group sd_std_attr_group = {
528 .attrs = sd_std_attrs,
529};
530
a4dbd674 531static const struct attribute_group *sd_attr_groups[] = {
51ec92e2
PO
532 &sd_std_attr_group,
533 NULL,
534};
535
71578a1e 536struct device_type sd_type = {
51ec92e2
PO
537 .groups = sd_attr_groups,
538};
539
7ea239d9 540/*
71578a1e 541 * Fetch CID from card.
7ea239d9 542 */
d6d50a15 543int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
7ea239d9 544{
7ea239d9 545 int err;
7ea239d9 546
7ea239d9
PO
547 /*
548 * Since we're changing the OCR value, we seem to
549 * need to tell some cards to go back to the idle
550 * state. We wait 1ms to give cards time to
551 * respond.
552 */
553 mmc_go_idle(host);
554
555 /*
556 * If SD_SEND_IF_COND indicates an SD 2.0
557 * compliant card and we should set bit 30
558 * of the ocr to indicate that we can handle
559 * block-addressed SDHC cards.
560 */
6abaa0c9 561 err = mmc_send_if_cond(host, ocr);
17b0429d 562 if (!err)
f2119df6 563 ocr |= SD_OCR_CCS;
7ea239d9 564
f2119df6
AN
565 /*
566 * If the host supports one of UHS-I modes, request the card
567 * to switch to 1.8V signaling level.
568 */
569 if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
570 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
571 ocr |= SD_OCR_S18R;
572
573 /* If the host can supply more than 150mA, XPC should be set to 1. */
574 if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
575 MMC_CAP_SET_XPC_180))
576 ocr |= SD_OCR_XPC;
577
578try_again:
d6d50a15 579 err = mmc_send_app_op_cond(host, ocr, rocr);
17b0429d 580 if (err)
71578a1e 581 return err;
7ea239d9 582
f2119df6
AN
583 /*
584 * In case CCS and S18A in the response is set, start Signal Voltage
585 * Switch procedure. SPI mode doesn't support CMD11.
586 */
d6d50a15
AN
587 if (!mmc_host_is_spi(host) && rocr &&
588 ((*rocr & 0x41000000) == 0x41000000)) {
f2119df6
AN
589 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
590 if (err) {
591 ocr &= ~SD_OCR_S18R;
592 goto try_again;
593 }
594 }
595
af517150
DB
596 if (mmc_host_is_spi(host))
597 err = mmc_send_cid(host, cid);
598 else
599 err = mmc_all_send_cid(host, cid);
71578a1e
MM
600
601 return err;
602}
603
604int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
605{
606 int err;
607
608 /*
609 * Fetch CSD from card.
610 */
611 err = mmc_send_csd(card, card->raw_csd);
17b0429d 612 if (err)
71578a1e 613 return err;
7ea239d9 614
71578a1e
MM
615 err = mmc_decode_csd(card);
616 if (err)
617 return err;
618
619 return 0;
620}
621
622int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
623 bool reinit)
624{
625 int err;
626
627 if (!reinit) {
628 /*
629 * Fetch SCR from card.
630 */
631 err = mmc_app_send_scr(card, card->raw_scr);
632 if (err)
633 return err;
634
635 err = mmc_decode_scr(card);
636 if (err)
637 return err;
638
dfe86cba
AH
639 /*
640 * Fetch and process SD Status register.
641 */
642 err = mmc_read_ssr(card);
643 if (err)
644 return err;
645
646 /* Erase init depends on CSD and SSR */
647 mmc_init_erase(card);
648
71578a1e
MM
649 /*
650 * Fetch switch information from card.
651 */
652 err = mmc_read_switch(card);
653 if (err)
654 return err;
655 }
656
657 /*
658 * For SPI, enable CRC as appropriate.
659 * This CRC enable is located AFTER the reading of the
660 * card registers because some SDHC cards are not able
661 * to provide valid CRCs for non-512-byte blocks.
662 */
663 if (mmc_host_is_spi(host)) {
664 err = mmc_spi_set_crc(host, use_spi_crc);
665 if (err)
666 return err;
667 }
668
669 /*
670 * Check if read-only switch is active.
671 */
672 if (!reinit) {
673 int ro = -1;
674
675 if (host->ops->get_ro)
676 ro = host->ops->get_ro(host);
677
678 if (ro < 0) {
679 printk(KERN_WARNING "%s: host does not "
680 "support reading read-only "
681 "switch. assuming write-enable.\n",
682 mmc_hostname(host));
683 } else if (ro > 0) {
684 mmc_card_set_readonly(card);
adf66a0d 685 }
71578a1e
MM
686 }
687
688 return 0;
689}
690
691unsigned mmc_sd_get_max_clock(struct mmc_card *card)
692{
693 unsigned max_dtr = (unsigned int)-1;
694
695 if (mmc_card_highspeed(card)) {
696 if (max_dtr > card->sw_caps.hs_max_dtr)
697 max_dtr = card->sw_caps.hs_max_dtr;
698 } else if (max_dtr > card->csd.max_dtr) {
699 max_dtr = card->csd.max_dtr;
700 }
701
702 return max_dtr;
703}
704
705void mmc_sd_go_highspeed(struct mmc_card *card)
706{
707 mmc_card_set_highspeed(card);
708 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
709}
710
711/*
712 * Handle the detection and initialisation of a card.
713 *
714 * In the case of a resume, "oldcard" will contain the card
715 * we're trying to reinitialise.
716 */
717static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
718 struct mmc_card *oldcard)
719{
720 struct mmc_card *card;
721 int err;
722 u32 cid[4];
d6d50a15 723 u32 rocr = 0;
71578a1e
MM
724
725 BUG_ON(!host);
726 WARN_ON(!host->claimed);
727
d6d50a15 728 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
71578a1e
MM
729 if (err)
730 return err;
731
732 if (oldcard) {
733 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
734 return -ENOENT;
7ea239d9 735
6abaa0c9
PO
736 card = oldcard;
737 } else {
738 /*
739 * Allocate card structure.
740 */
51ec92e2 741 card = mmc_alloc_card(host, &sd_type);
71578a1e
MM
742 if (IS_ERR(card))
743 return PTR_ERR(card);
6abaa0c9
PO
744
745 card->type = MMC_TYPE_SD;
746 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
747 }
7ea239d9
PO
748
749 /*
af517150 750 * For native busses: get card RCA and quit open drain mode.
7ea239d9 751 */
af517150
DB
752 if (!mmc_host_is_spi(host)) {
753 err = mmc_send_relative_addr(host, &card->rca);
754 if (err)
71578a1e 755 return err;
7ea239d9 756
af517150
DB
757 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
758 }
7ea239d9 759
6abaa0c9 760 if (!oldcard) {
71578a1e 761 err = mmc_sd_get_csd(host, card);
17b0429d 762 if (err)
71578a1e 763 return err;
bd766312 764
6abaa0c9
PO
765 mmc_decode_cid(card);
766 }
7ea239d9
PO
767
768 /*
6abaa0c9 769 * Select card, as all following commands rely on that.
7ea239d9 770 */
af517150
DB
771 if (!mmc_host_is_spi(host)) {
772 err = mmc_select_card(card);
773 if (err)
71578a1e 774 return err;
6abaa0c9 775 }
1addfcdb 776
71578a1e
MM
777 err = mmc_sd_setup_card(host, card, oldcard != NULL);
778 if (err)
779 goto free_card;
9d9f25c0 780
d6d50a15
AN
781 /* Initialization sequence for UHS-I cards */
782 if (rocr & SD_ROCR_S18A) {
783 err = mmc_sd_init_uhs_card(card);
17b0429d 784 if (err)
7ea239d9 785 goto free_card;
d6d50a15
AN
786 } else {
787 /*
788 * Attempt to change to high-speed (if supported)
789 */
790 err = mmc_sd_switch_hs(card);
791 if (err > 0)
792 mmc_sd_go_highspeed(card);
793 else if (err)
794 goto free_card;
795
796 /*
797 * Set bus speed.
798 */
799 mmc_set_clock(host, mmc_sd_get_max_clock(card));
7ea239d9 800
d6d50a15
AN
801 /*
802 * Switch to wider bus (if supported).
803 */
804 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
805 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
806 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
807 if (err)
808 goto free_card;
809
810 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
811 }
7ea239d9
PO
812 }
813
71578a1e 814 host->card = card;
17b0429d 815 return 0;
6abaa0c9
PO
816
817free_card:
818 if (!oldcard)
819 mmc_remove_card(card);
6abaa0c9 820
adf66a0d 821 return err;
6abaa0c9
PO
822}
823
824/*
825 * Host is being removed. Free up the current card.
826 */
827static void mmc_sd_remove(struct mmc_host *host)
828{
829 BUG_ON(!host);
830 BUG_ON(!host->card);
831
832 mmc_remove_card(host->card);
833 host->card = NULL;
834}
835
836/*
837 * Card detection callback from host.
838 */
839static void mmc_sd_detect(struct mmc_host *host)
840{
841 int err;
842
843 BUG_ON(!host);
844 BUG_ON(!host->card);
845
846 mmc_claim_host(host);
847
848 /*
849 * Just check if our card has been removed.
850 */
851 err = mmc_send_status(host->card, NULL);
7ea239d9
PO
852
853 mmc_release_host(host);
854
17b0429d 855 if (err) {
4101c16a 856 mmc_sd_remove(host);
6abaa0c9
PO
857
858 mmc_claim_host(host);
859 mmc_detach_bus(host);
860 mmc_release_host(host);
861 }
862}
863
6abaa0c9
PO
864/*
865 * Suspend callback from host.
866 */
95cdfb72 867static int mmc_sd_suspend(struct mmc_host *host)
6abaa0c9
PO
868{
869 BUG_ON(!host);
870 BUG_ON(!host->card);
871
872 mmc_claim_host(host);
af517150
DB
873 if (!mmc_host_is_spi(host))
874 mmc_deselect_cards(host);
6abaa0c9
PO
875 host->card->state &= ~MMC_STATE_HIGHSPEED;
876 mmc_release_host(host);
95cdfb72
NP
877
878 return 0;
6abaa0c9
PO
879}
880
881/*
882 * Resume callback from host.
883 *
884 * This function tries to determine if the same card is still present
885 * and, if so, restore all state to it.
886 */
95cdfb72 887static int mmc_sd_resume(struct mmc_host *host)
6abaa0c9
PO
888{
889 int err;
890
891 BUG_ON(!host);
892 BUG_ON(!host->card);
893
894 mmc_claim_host(host);
6abaa0c9 895 err = mmc_sd_init_card(host, host->ocr, host->card);
2986d0bf
PO
896 mmc_release_host(host);
897
95cdfb72 898 return err;
6abaa0c9
PO
899}
900
12ae637f 901static int mmc_sd_power_restore(struct mmc_host *host)
eae1aeee 902{
12ae637f
OBC
903 int ret;
904
eae1aeee
AH
905 host->card->state &= ~MMC_STATE_HIGHSPEED;
906 mmc_claim_host(host);
12ae637f 907 ret = mmc_sd_init_card(host, host->ocr, host->card);
eae1aeee 908 mmc_release_host(host);
12ae637f
OBC
909
910 return ret;
eae1aeee
AH
911}
912
6abaa0c9 913static const struct mmc_bus_ops mmc_sd_ops = {
9feae246
AH
914 .remove = mmc_sd_remove,
915 .detect = mmc_sd_detect,
916 .suspend = NULL,
917 .resume = NULL,
eae1aeee 918 .power_restore = mmc_sd_power_restore,
9feae246
AH
919};
920
921static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
6abaa0c9
PO
922 .remove = mmc_sd_remove,
923 .detect = mmc_sd_detect,
924 .suspend = mmc_sd_suspend,
925 .resume = mmc_sd_resume,
eae1aeee 926 .power_restore = mmc_sd_power_restore,
6abaa0c9
PO
927};
928
9feae246
AH
929static void mmc_sd_attach_bus_ops(struct mmc_host *host)
930{
931 const struct mmc_bus_ops *bus_ops;
932
71d7d3d1 933 if (!mmc_card_is_removable(host))
9feae246
AH
934 bus_ops = &mmc_sd_ops_unsafe;
935 else
936 bus_ops = &mmc_sd_ops;
937 mmc_attach_bus(host, bus_ops);
938}
939
6abaa0c9
PO
940/*
941 * Starting point for SD card init.
942 */
807e8e40 943int mmc_attach_sd(struct mmc_host *host)
6abaa0c9
PO
944{
945 int err;
807e8e40 946 u32 ocr;
6abaa0c9
PO
947
948 BUG_ON(!host);
d84075c8 949 WARN_ON(!host->claimed);
6abaa0c9 950
f2119df6
AN
951 /* Make sure we are at 3.3V signalling voltage */
952 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
953 if (err)
954 return err;
955
807e8e40
AR
956 err = mmc_send_app_op_cond(host, 0, &ocr);
957 if (err)
958 return err;
959
9feae246 960 mmc_sd_attach_bus_ops(host);
8f230f45
TI
961 if (host->ocr_avail_sd)
962 host->ocr_avail = host->ocr_avail_sd;
6abaa0c9 963
af517150
DB
964 /*
965 * We need to get OCR a different way for SPI.
966 */
967 if (mmc_host_is_spi(host)) {
968 mmc_go_idle(host);
969
970 err = mmc_spi_read_ocr(host, 0, &ocr);
971 if (err)
972 goto err;
973 }
974
6abaa0c9
PO
975 /*
976 * Sanity check the voltages that the card claims to
977 * support.
978 */
979 if (ocr & 0x7F) {
980 printk(KERN_WARNING "%s: card claims to support voltages "
981 "below the defined range. These will be ignored.\n",
982 mmc_hostname(host));
983 ocr &= ~0x7F;
984 }
985
8f230f45
TI
986 if ((ocr & MMC_VDD_165_195) &&
987 !(host->ocr_avail_sd & MMC_VDD_165_195)) {
6abaa0c9
PO
988 printk(KERN_WARNING "%s: SD card claims to support the "
989 "incompletely defined 'low voltage range'. This "
990 "will be ignored.\n", mmc_hostname(host));
991 ocr &= ~MMC_VDD_165_195;
992 }
993
994 host->ocr = mmc_select_voltage(host, ocr);
995
996 /*
997 * Can we support the voltage(s) of the card(s)?
998 */
109b5bed
PO
999 if (!host->ocr) {
1000 err = -EINVAL;
6abaa0c9 1001 goto err;
109b5bed 1002 }
6abaa0c9
PO
1003
1004 /*
1005 * Detect and init the card.
1006 */
1007 err = mmc_sd_init_card(host, host->ocr, NULL);
17b0429d 1008 if (err)
6abaa0c9
PO
1009 goto err;
1010
1011 mmc_release_host(host);
4101c16a 1012 err = mmc_add_card(host->card);
807e8e40 1013 mmc_claim_host(host);
7ea239d9 1014 if (err)
2986d0bf 1015 goto remove_card;
7ea239d9
PO
1016
1017 return 0;
1018
2986d0bf 1019remove_card:
807e8e40 1020 mmc_release_host(host);
6abaa0c9 1021 mmc_remove_card(host->card);
7ea239d9 1022 host->card = NULL;
2986d0bf 1023 mmc_claim_host(host);
7ea239d9
PO
1024err:
1025 mmc_detach_bus(host);
7ea239d9 1026
109b5bed
PO
1027 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
1028 mmc_hostname(host), err);
1029
adf66a0d 1030 return err;
7ea239d9
PO
1031}
1032
This page took 0.492442 seconds and 5 git commands to generate.