Merge remote branch 'tegra/linux-tegra-2.6.36' into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / core / sd.c
1 /*
2  *  linux/drivers/mmc/core/sd.c
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>
14 #include <linux/slab.h>
15
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/sd.h>
20
21 #include "core.h"
22 #include "bus.h"
23 #include "mmc_ops.h"
24 #include "sd_ops.h"
25
26 static const unsigned int tran_exp[] = {
27         10000,          100000,         1000000,        10000000,
28         0,              0,              0,              0
29 };
30
31 static 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
36 static const unsigned int tacc_exp[] = {
37         1,      10,     100,    1000,   10000,  100000, 1000000, 10000000,
38 };
39
40 static 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  */
62 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  */
91 static int mmc_decode_csd(struct mmc_card *card)
92 {
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
123                 if (UNSTUFF_BITS(resp, 46, 1)) {
124                         csd->erase_size = 1;
125                 } else if (csd->write_blkbits >= 9) {
126                         csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
127                         csd->erase_size <<= csd->write_blkbits - 9;
128                 }
129                 break;
130         case 1:
131                 /*
132                  * This is a block-addressed SDHC card. Most
133                  * interesting fields are unused and have fixed
134                  * values. To avoid getting tripped by buggy cards,
135                  * we assume those fixed values ourselves.
136                  */
137                 mmc_card_set_blockaddr(card);
138
139                 csd->tacc_ns     = 0; /* Unused */
140                 csd->tacc_clks   = 0; /* Unused */
141
142                 m = UNSTUFF_BITS(resp, 99, 4);
143                 e = UNSTUFF_BITS(resp, 96, 3);
144                 csd->max_dtr      = tran_exp[e] * tran_mant[m];
145                 csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
146
147                 m = UNSTUFF_BITS(resp, 48, 22);
148                 csd->capacity     = (1 + m) << 10;
149
150                 csd->read_blkbits = 9;
151                 csd->read_partial = 0;
152                 csd->write_misalign = 0;
153                 csd->read_misalign = 0;
154                 csd->r2w_factor = 4; /* Unused */
155                 csd->write_blkbits = 9;
156                 csd->write_partial = 0;
157                 csd->erase_size = 1;
158                 break;
159         default:
160                 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
161                         mmc_hostname(card->host), csd_struct);
162                 return -EINVAL;
163         }
164
165         card->erase_size = csd->erase_size;
166
167         return 0;
168 }
169
170 /*
171  * Given a 64-bit response, decode to our card SCR structure.
172  */
173 static int mmc_decode_scr(struct mmc_card *card)
174 {
175         struct sd_scr *scr = &card->scr;
176         unsigned int scr_struct;
177         u32 resp[4];
178
179         resp[3] = card->raw_scr[1];
180         resp[2] = card->raw_scr[0];
181
182         scr_struct = UNSTUFF_BITS(resp, 60, 4);
183         if (scr_struct != 0) {
184                 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
185                         mmc_hostname(card->host), scr_struct);
186                 return -EINVAL;
187         }
188
189         scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
190         scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
191
192         if (UNSTUFF_BITS(resp, 55, 1))
193                 card->erased_byte = 0xFF;
194         else
195                 card->erased_byte = 0x0;
196
197         return 0;
198 }
199
200 /*
201  * Fetch and process SD Status register.
202  */
203 static int mmc_read_ssr(struct mmc_card *card)
204 {
205         unsigned int au, es, et, eo;
206         int err, i;
207         u32 *ssr;
208
209         if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
210                 printk(KERN_WARNING "%s: card lacks mandatory SD Status "
211                         "function.\n", mmc_hostname(card->host));
212                 return 0;
213         }
214
215         ssr = kmalloc(64, GFP_KERNEL);
216         if (!ssr)
217                 return -ENOMEM;
218
219         err = mmc_app_sd_status(card, ssr);
220         if (err) {
221                 printk(KERN_WARNING "%s: problem reading SD Status "
222                         "register.\n", mmc_hostname(card->host));
223                 err = 0;
224                 goto out;
225         }
226
227         for (i = 0; i < 16; i++)
228                 ssr[i] = be32_to_cpu(ssr[i]);
229
230         /*
231          * UNSTUFF_BITS only works with four u32s so we have to offset the
232          * bitfield positions accordingly.
233          */
234         au = UNSTUFF_BITS(ssr, 428 - 384, 4);
235         if (au > 0 || au <= 9) {
236                 card->ssr.au = 1 << (au + 4);
237                 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
238                 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
239                 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
240                 if (es && et) {
241                         card->ssr.erase_timeout = (et * 1000) / es;
242                         card->ssr.erase_offset = eo * 1000;
243                 }
244         } else {
245                 printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit "
246                         "size.\n", mmc_hostname(card->host));
247         }
248 out:
249         kfree(ssr);
250         return err;
251 }
252
253 /*
254  * Fetches and decodes switch information
255  */
256 static int mmc_read_switch(struct mmc_card *card)
257 {
258         int err;
259         u8 *status;
260
261         if (card->scr.sda_vsn < SCR_SPEC_VER_1)
262                 return 0;
263
264         if (!(card->csd.cmdclass & CCC_SWITCH)) {
265                 printk(KERN_WARNING "%s: card lacks mandatory switch "
266                         "function, performance might suffer.\n",
267                         mmc_hostname(card->host));
268                 return 0;
269         }
270
271         err = -EIO;
272
273         status = kmalloc(64, GFP_KERNEL);
274         if (!status) {
275                 printk(KERN_ERR "%s: could not allocate a buffer for "
276                         "switch capabilities.\n", mmc_hostname(card->host));
277                 return -ENOMEM;
278         }
279
280         err = mmc_sd_switch(card, 0, 0, 1, status);
281         if (err) {
282                 /* If the host or the card can't do the switch,
283                  * fail more gracefully. */
284                 if ((err != -EINVAL)
285                  && (err != -ENOSYS)
286                  && (err != -EFAULT))
287                         goto out;
288
289                 printk(KERN_WARNING "%s: problem reading switch "
290                         "capabilities, performance might suffer.\n",
291                         mmc_hostname(card->host));
292                 err = 0;
293
294                 goto out;
295         }
296
297         if (status[13] & 0x02)
298                 card->sw_caps.hs_max_dtr = 50000000;
299
300 out:
301         kfree(status);
302
303         return err;
304 }
305
306 /*
307  * Test if the card supports high-speed mode and, if so, switch to it.
308  */
309 int mmc_sd_switch_hs(struct mmc_card *card)
310 {
311         int err;
312         u8 *status;
313
314         if (card->scr.sda_vsn < SCR_SPEC_VER_1)
315                 return 0;
316
317         if (!(card->csd.cmdclass & CCC_SWITCH))
318                 return 0;
319
320         if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
321                 return 0;
322
323         if (card->sw_caps.hs_max_dtr == 0)
324                 return 0;
325
326         err = -EIO;
327
328         status = kmalloc(64, GFP_KERNEL);
329         if (!status) {
330                 printk(KERN_ERR "%s: could not allocate a buffer for "
331                         "switch capabilities.\n", mmc_hostname(card->host));
332                 return -ENOMEM;
333         }
334
335         err = mmc_sd_switch(card, 1, 0, 1, status);
336         if (err)
337                 goto out;
338
339         if ((status[16] & 0xF) != 1) {
340                 printk(KERN_WARNING "%s: Problem switching card "
341                         "into high-speed mode!\n",
342                         mmc_hostname(card->host));
343                 err = 0;
344         } else {
345                 err = 1;
346         }
347
348 out:
349         kfree(status);
350
351         return err;
352 }
353
354 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
355         card->raw_cid[2], card->raw_cid[3]);
356 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
357         card->raw_csd[2], card->raw_csd[3]);
358 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
359 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
360 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
361 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
362 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
363 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
364 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
365 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
366 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
367 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
368
369
370 static struct attribute *sd_std_attrs[] = {
371         &dev_attr_cid.attr,
372         &dev_attr_csd.attr,
373         &dev_attr_scr.attr,
374         &dev_attr_date.attr,
375         &dev_attr_erase_size.attr,
376         &dev_attr_preferred_erase_size.attr,
377         &dev_attr_fwrev.attr,
378         &dev_attr_hwrev.attr,
379         &dev_attr_manfid.attr,
380         &dev_attr_name.attr,
381         &dev_attr_oemid.attr,
382         &dev_attr_serial.attr,
383         NULL,
384 };
385
386 static struct attribute_group sd_std_attr_group = {
387         .attrs = sd_std_attrs,
388 };
389
390 static const struct attribute_group *sd_attr_groups[] = {
391         &sd_std_attr_group,
392         NULL,
393 };
394
395 struct device_type sd_type = {
396         .groups = sd_attr_groups,
397 };
398
399 /*
400  * Fetch CID from card.
401  */
402 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid)
403 {
404         int err;
405
406         /*
407          * Since we're changing the OCR value, we seem to
408          * need to tell some cards to go back to the idle
409          * state.  We wait 1ms to give cards time to
410          * respond.
411          */
412         mmc_go_idle(host);
413
414         /*
415          * If SD_SEND_IF_COND indicates an SD 2.0
416          * compliant card and we should set bit 30
417          * of the ocr to indicate that we can handle
418          * block-addressed SDHC cards.
419          */
420         err = mmc_send_if_cond(host, ocr);
421         if (!err)
422                 ocr |= 1 << 30;
423
424         err = mmc_send_app_op_cond(host, ocr, NULL);
425         if (err)
426                 return err;
427
428         if (mmc_host_is_spi(host))
429                 err = mmc_send_cid(host, cid);
430         else
431                 err = mmc_all_send_cid(host, cid);
432
433         return err;
434 }
435
436 int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
437 {
438         int err;
439
440         /*
441          * Fetch CSD from card.
442          */
443         err = mmc_send_csd(card, card->raw_csd);
444         if (err)
445                 return err;
446
447         err = mmc_decode_csd(card);
448         if (err)
449                 return err;
450
451         return 0;
452 }
453
454 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
455         bool reinit)
456 {
457         int err;
458 #ifdef CONFIG_MMC_PARANOID_SD_INIT
459         int retries;
460 #endif
461
462         if (!reinit) {
463                 /*
464                  * Fetch SCR from card.
465                  */
466                 err = mmc_app_send_scr(card, card->raw_scr);
467                 if (err)
468                         return err;
469
470                 err = mmc_decode_scr(card);
471                 if (err)
472                         return err;
473
474                 /*
475                  * Fetch and process SD Status register.
476                  */
477                 err = mmc_read_ssr(card);
478                 if (err)
479                         return err;
480
481                 /* Erase init depends on CSD and SSR */
482                 mmc_init_erase(card);
483
484                 /*
485                  * Fetch switch information from card.
486                  */
487 #ifdef CONFIG_MMC_PARANOID_SD_INIT
488                 for (retries = 1; retries <= 3; retries++) {
489                         err = mmc_read_switch(card);
490                         if (!err) {
491                                 if (retries > 1) {
492                                         printk(KERN_WARNING
493                                                "%s: recovered\n", 
494                                                mmc_hostname(host));
495                                 }
496                                 break;
497                         } else {
498                                 printk(KERN_WARNING
499                                        "%s: read switch failed (attempt %d)\n",
500                                        mmc_hostname(host), retries);
501                         }
502                 }
503 #else
504                 err = mmc_read_switch(card);
505 #endif
506
507                 if (err)
508                         return err;
509         }
510
511         /*
512          * For SPI, enable CRC as appropriate.
513          * This CRC enable is located AFTER the reading of the
514          * card registers because some SDHC cards are not able
515          * to provide valid CRCs for non-512-byte blocks.
516          */
517         if (mmc_host_is_spi(host)) {
518                 err = mmc_spi_set_crc(host, use_spi_crc);
519                 if (err)
520                         return err;
521         }
522
523         /*
524          * Check if read-only switch is active.
525          */
526         if (!reinit) {
527                 int ro = -1;
528
529                 if (host->ops->get_ro)
530                         ro = host->ops->get_ro(host);
531
532                 if (ro < 0) {
533                         printk(KERN_WARNING "%s: host does not "
534                                 "support reading read-only "
535                                 "switch. assuming write-enable.\n",
536                                 mmc_hostname(host));
537                 } else if (ro > 0) {
538                         mmc_card_set_readonly(card);
539                 }
540         }
541
542         return 0;
543 }
544
545 unsigned mmc_sd_get_max_clock(struct mmc_card *card)
546 {
547         unsigned max_dtr = (unsigned int)-1;
548
549         if (mmc_card_highspeed(card)) {
550                 if (max_dtr > card->sw_caps.hs_max_dtr)
551                         max_dtr = card->sw_caps.hs_max_dtr;
552         } else if (max_dtr > card->csd.max_dtr) {
553                 max_dtr = card->csd.max_dtr;
554         }
555
556         return max_dtr;
557 }
558
559 void mmc_sd_go_highspeed(struct mmc_card *card)
560 {
561         mmc_card_set_highspeed(card);
562         mmc_set_timing(card->host, MMC_TIMING_SD_HS);
563 }
564
565 /*
566  * Handle the detection and initialisation of a card.
567  *
568  * In the case of a resume, "oldcard" will contain the card
569  * we're trying to reinitialise.
570  */
571 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
572         struct mmc_card *oldcard)
573 {
574         struct mmc_card *card;
575         int err;
576         u32 cid[4];
577
578         BUG_ON(!host);
579         WARN_ON(!host->claimed);
580
581         err = mmc_sd_get_cid(host, ocr, cid);
582         if (err)
583                 return err;
584
585         if (oldcard) {
586                 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
587                         return -ENOENT;
588
589                 card = oldcard;
590         } else {
591                 /*
592                  * Allocate card structure.
593                  */
594                 card = mmc_alloc_card(host, &sd_type);
595                 if (IS_ERR(card))
596                         return PTR_ERR(card);
597
598                 card->type = MMC_TYPE_SD;
599                 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
600         }
601
602         /*
603          * For native busses:  get card RCA and quit open drain mode.
604          */
605         if (!mmc_host_is_spi(host)) {
606                 err = mmc_send_relative_addr(host, &card->rca);
607                 if (err)
608                         return err;
609
610                 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
611         }
612
613         if (!oldcard) {
614                 err = mmc_sd_get_csd(host, card);
615                 if (err)
616                         return err;
617
618                 mmc_decode_cid(card);
619         }
620
621         /*
622          * Select card, as all following commands rely on that.
623          */
624         if (!mmc_host_is_spi(host)) {
625                 err = mmc_select_card(card);
626                 if (err)
627                         return err;
628         }
629
630         err = mmc_sd_setup_card(host, card, oldcard != NULL);
631         if (err)
632                 goto free_card;
633
634         /*
635          * Attempt to change to high-speed (if supported)
636          */
637         err = mmc_sd_switch_hs(card);
638         if (err > 0)
639                 mmc_sd_go_highspeed(card);
640         else if (err)
641                 goto free_card;
642
643         /*
644          * Set bus speed.
645          */
646         mmc_set_clock(host, mmc_sd_get_max_clock(card));
647
648         /*
649          * Switch to wider bus (if supported).
650          */
651         if ((host->caps & MMC_CAP_4_BIT_DATA) &&
652                 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
653                 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
654                 if (err)
655                         goto free_card;
656
657                 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
658         }
659
660         host->card = card;
661         return 0;
662
663 free_card:
664         if (!oldcard)
665                 mmc_remove_card(card);
666
667         return err;
668 }
669
670 /*
671  * Host is being removed. Free up the current card.
672  */
673 static void mmc_sd_remove(struct mmc_host *host)
674 {
675         BUG_ON(!host);
676         BUG_ON(!host->card);
677
678         mmc_remove_card(host->card);
679         host->card = NULL;
680 }
681
682 /*
683  * Card detection callback from host.
684  */
685 static void mmc_sd_detect(struct mmc_host *host)
686 {
687         int err = 0;
688 #ifdef CONFIG_MMC_PARANOID_SD_INIT
689         int retries = 5;
690 #endif
691
692         BUG_ON(!host);
693         BUG_ON(!host->card);
694        
695         mmc_claim_host(host);
696
697         /*
698          * Just check if our card has been removed.
699          */
700 #ifdef CONFIG_MMC_PARANOID_SD_INIT
701         while(retries) {
702                 err = mmc_send_status(host->card, NULL);
703                 if (err) {
704                         retries--;
705                         udelay(5);
706                         continue;
707                 }
708                 break;
709         }
710         if (!retries) {
711                 printk(KERN_ERR "%s(%s): Unable to re-detect card (%d)\n",
712                        __func__, mmc_hostname(host), err);
713         }
714 #else
715         err = mmc_send_status(host->card, NULL);
716 #endif
717         mmc_release_host(host);
718
719         if (err) {
720                 mmc_sd_remove(host);
721
722                 mmc_claim_host(host);
723                 mmc_detach_bus(host);
724                 mmc_release_host(host);
725         }
726 }
727
728 /*
729  * Suspend callback from host.
730  */
731 static int mmc_sd_suspend(struct mmc_host *host)
732 {
733         BUG_ON(!host);
734         BUG_ON(!host->card);
735
736         mmc_claim_host(host);
737         if (!mmc_host_is_spi(host))
738                 mmc_deselect_cards(host);
739         host->card->state &= ~MMC_STATE_HIGHSPEED;
740         mmc_release_host(host);
741
742         return 0;
743 }
744
745 /*
746  * Resume callback from host.
747  *
748  * This function tries to determine if the same card is still present
749  * and, if so, restore all state to it.
750  */
751 static int mmc_sd_resume(struct mmc_host *host)
752 {
753         int err;
754 #ifdef CONFIG_MMC_PARANOID_SD_INIT
755         int retries;
756 #endif
757
758         BUG_ON(!host);
759         BUG_ON(!host->card);
760
761         mmc_claim_host(host);
762 #ifdef CONFIG_MMC_PARANOID_SD_INIT
763         retries = 5;
764         while (retries) {
765                 err = mmc_sd_init_card(host, host->ocr, host->card);
766
767                 if (err) {
768                         printk(KERN_ERR "%s: Re-init card rc = %d (retries = %d)\n",
769                                mmc_hostname(host), err, retries);
770                         mdelay(5);
771                         retries--;
772                         continue;
773                 }
774                 break;
775         }
776 #else
777         err = mmc_sd_init_card(host, host->ocr, host->card);
778 #endif
779         mmc_release_host(host);
780
781         return err;
782 }
783
784 static void mmc_sd_power_restore(struct mmc_host *host)
785 {
786         host->card->state &= ~MMC_STATE_HIGHSPEED;
787         mmc_claim_host(host);
788         mmc_sd_init_card(host, host->ocr, host->card);
789         mmc_release_host(host);
790 }
791
792 static const struct mmc_bus_ops mmc_sd_ops = {
793         .remove = mmc_sd_remove,
794         .detect = mmc_sd_detect,
795         .suspend = NULL,
796         .resume = NULL,
797         .power_restore = mmc_sd_power_restore,
798 };
799
800 static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
801         .remove = mmc_sd_remove,
802         .detect = mmc_sd_detect,
803         .suspend = mmc_sd_suspend,
804         .resume = mmc_sd_resume,
805         .power_restore = mmc_sd_power_restore,
806 };
807
808 static void mmc_sd_attach_bus_ops(struct mmc_host *host)
809 {
810         const struct mmc_bus_ops *bus_ops;
811
812         if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable)
813                 bus_ops = &mmc_sd_ops_unsafe;
814         else
815                 bus_ops = &mmc_sd_ops;
816         mmc_attach_bus(host, bus_ops);
817 }
818
819 /*
820  * Starting point for SD card init.
821  */
822 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
823 {
824         int err;
825 #ifdef CONFIG_MMC_PARANOID_SD_INIT
826         int retries;
827 #endif
828
829         BUG_ON(!host);
830         WARN_ON(!host->claimed);
831
832         mmc_sd_attach_bus_ops(host);
833
834         /*
835          * We need to get OCR a different way for SPI.
836          */
837         if (mmc_host_is_spi(host)) {
838                 mmc_go_idle(host);
839
840                 err = mmc_spi_read_ocr(host, 0, &ocr);
841                 if (err)
842                         goto err;
843         }
844
845         /*
846          * Sanity check the voltages that the card claims to
847          * support.
848          */
849         if (ocr & 0x7F) {
850                 printk(KERN_WARNING "%s: card claims to support voltages "
851                        "below the defined range. These will be ignored.\n",
852                        mmc_hostname(host));
853                 ocr &= ~0x7F;
854         }
855
856         if (ocr & MMC_VDD_165_195) {
857                 printk(KERN_WARNING "%s: SD card claims to support the "
858                        "incompletely defined 'low voltage range'. This "
859                        "will be ignored.\n", mmc_hostname(host));
860                 ocr &= ~MMC_VDD_165_195;
861         }
862
863         host->ocr = mmc_select_voltage(host, ocr);
864
865         /*
866          * Can we support the voltage(s) of the card(s)?
867          */
868         if (!host->ocr) {
869                 err = -EINVAL;
870                 goto err;
871         }
872
873         /*
874          * Detect and init the card.
875          */
876 #ifdef CONFIG_MMC_PARANOID_SD_INIT
877         retries = 5;
878         while (retries) {
879                 err = mmc_sd_init_card(host, host->ocr, NULL);
880                 if (err) {
881                         retries--;
882                         continue;
883                 }
884                 break;
885         }
886
887         if (!retries) {
888                 printk(KERN_ERR "%s: mmc_sd_init_card() failure (err = %d)\n",
889                        mmc_hostname(host), err);
890                 goto err;
891         }
892 #else
893         err = mmc_sd_init_card(host, host->ocr, NULL);
894         if (err)
895                 goto err;
896 #endif
897
898         mmc_release_host(host);
899
900         err = mmc_add_card(host->card);
901         if (err)
902                 goto remove_card;
903
904         return 0;
905
906 remove_card:
907         mmc_remove_card(host->card);
908         host->card = NULL;
909         mmc_claim_host(host);
910 err:
911         mmc_detach_bus(host);
912         mmc_release_host(host);
913
914         printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
915                 mmc_hostname(host), err);
916
917         return err;
918 }
919