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