LCOV - code coverage report
Current view: top level - lkbce/drivers/mmc/core - sd.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 331 383 86.4 %
Date: 2017-01-25 Functions: 20 23 87.0 %

          Line data    Source code
       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             : 
      15             : #include <linux/mmc/host.h>
      16             : #include <linux/mmc/card.h>
      17             : #include <linux/mmc/mmc.h>
      18             : #include <linux/mmc/sd.h>
      19             : 
      20             : #include "core.h"
      21             : #include "bus.h"
      22             : #include "mmc_ops.h"
      23             : #include "sd_ops.h"
      24             : 
      25           1 : static const unsigned int tran_exp[] = {
      26             :         10000,          100000,         1000000,        10000000,
      27             :         0,              0,              0,              0
      28             : };
      29             : 
      30           1 : static const unsigned char tran_mant[] = {
      31             :         0,      10,     12,     13,     15,     20,     25,     30,
      32             :         35,     40,     45,     50,     55,     60,     70,     80,
      33             : };
      34             : 
      35           1 : static const unsigned int tacc_exp[] = {
      36             :         1,      10,     100,    1000,   10000,  100000, 1000000, 10000000,
      37             : };
      38             : 
      39           1 : static const unsigned int tacc_mant[] = {
      40             :         0,      10,     12,     13,     15,     20,     25,     30,
      41             :         35,     40,     45,     50,     55,     60,     70,     80,
      42             : };
      43             : 
      44             : #define UNSTUFF_BITS(resp,start,size)                                   \
      45             :         ({                                                              \
      46             :                 const int __size = size;                                \
      47             :                 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;        \
      48             :                 const int __off = 3 - ((start) / 32);                   \
      49             :                 const int __shft = (start) & 31;                    \
      50             :                 u32 __res;                                              \
      51             :                                                                         \
      52             :                 __res = resp[__off] >> __shft;                            \
      53             :                 if (__size + __shft > 32)                            \
      54             :                         __res |= resp[__off-1] << ((32 - __shft) % 32);   \
      55             :                 __res & __mask;                                             \
      56             :         })
      57             : 
      58             : /*
      59             :  * Given the decoded CSD structure, decode the raw CID to our CID structure.
      60             :  */
      61             : static void mmc_decode_cid(struct mmc_card *card)
      62             : {
      63           6 :         u32 *resp = card->raw_cid;
      64           3 : 
      65           6 :         memset(&card->cid, 0, sizeof(struct mmc_cid));
      66           3 : 
      67           3 :         /*
      68           3 :          * SD doesn't currently have a version field so we will
      69           3 :          * have to assume we can parse this.
      70           3 :          */
      71          21 :         card->cid.manfid             = UNSTUFF_BITS(resp, 120, 8);
      72          21 :         card->cid.oemid                      = UNSTUFF_BITS(resp, 104, 16);
      73          21 :         card->cid.prod_name[0]               = UNSTUFF_BITS(resp, 96, 8);
      74          21 :         card->cid.prod_name[1]               = UNSTUFF_BITS(resp, 88, 8);
      75          21 :         card->cid.prod_name[2]               = UNSTUFF_BITS(resp, 80, 8);
      76          21 :         card->cid.prod_name[3]               = UNSTUFF_BITS(resp, 72, 8);
      77          21 :         card->cid.prod_name[4]               = UNSTUFF_BITS(resp, 64, 8);
      78          21 :         card->cid.hwrev                      = UNSTUFF_BITS(resp, 60, 4);
      79          21 :         card->cid.fwrev                      = UNSTUFF_BITS(resp, 56, 4);
      80          24 :         card->cid.serial             = UNSTUFF_BITS(resp, 24, 32);
      81          21 :         card->cid.year                       = UNSTUFF_BITS(resp, 12, 8);
      82          21 :         card->cid.month                      = UNSTUFF_BITS(resp, 8, 4);
      83           3 : 
      84           6 :         card->cid.year += 2000; /* SD cards year offset */
      85           6 : }
      86           3 : 
      87           3 : /*
      88           3 :  * Given a 128-bit response, decode to our card CSD structure.
      89           3 :  */
      90           3 : static int mmc_decode_csd(struct mmc_card *card)
      91           3 : {
      92           9 :         struct mmc_csd *csd = &card->csd;
      93           6 :         unsigned int e, m, csd_struct;
      94           9 :         u32 *resp = card->raw_csd;
      95           6 : 
      96          24 :         csd_struct = UNSTUFF_BITS(resp, 126, 2);
      97           6 : 
      98           6 :         switch (csd_struct) {
      99          15 :         case 0:
     100          24 :                 m = UNSTUFF_BITS(resp, 115, 4);
     101          24 :                 e = UNSTUFF_BITS(resp, 112, 3);
     102           9 :                 csd->tacc_ns  = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
     103          24 :                 csd->tacc_clks        = UNSTUFF_BITS(resp, 104, 8) * 100;
     104           6 : 
     105          24 :                 m = UNSTUFF_BITS(resp, 99, 4);
     106          24 :                 e = UNSTUFF_BITS(resp, 96, 3);
     107           9 :                 csd->max_dtr   = tran_exp[e] * tran_mant[m];
     108          24 :                 csd->cmdclass          = UNSTUFF_BITS(resp, 84, 12);
     109           6 : 
     110          24 :                 e = UNSTUFF_BITS(resp, 47, 3);
     111          27 :                 m = UNSTUFF_BITS(resp, 62, 12);
     112           9 :                 csd->capacity          = (1 + m) << (e + 2);
     113           6 : 
     114          24 :                 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
     115          24 :                 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
     116          24 :                 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
     117          24 :                 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
     118          24 :                 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
     119          24 :                 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
     120          24 :                 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
     121           9 :                 break;
     122          18 :         case 1:
     123           6 :                 /*
     124           3 :                  * This is a block-addressed SDHC card. Most
     125           3 :                  * interesting fields are unused and have fixed
     126           3 :                  * values. To avoid getting tripped by buggy cards,
     127           3 :                  * we assume those fixed values ourselves.
     128           3 :                  */
     129           6 :                 mmc_card_set_blockaddr(card);
     130           3 : 
     131           6 :                 csd->tacc_ns  = 0; /* Unused */
     132           6 :                 csd->tacc_clks        = 0; /* Unused */
     133           3 : 
     134          21 :                 m = UNSTUFF_BITS(resp, 99, 4);
     135          21 :                 e = UNSTUFF_BITS(resp, 96, 3);
     136           6 :                 csd->max_dtr   = tran_exp[e] * tran_mant[m];
     137          21 :                 csd->cmdclass          = UNSTUFF_BITS(resp, 84, 12);
     138           3 : 
     139          24 :                 m = UNSTUFF_BITS(resp, 48, 22);
     140           6 :                 csd->capacity     = (1 + m) << 10;
     141           3 : 
     142           6 :                 csd->read_blkbits = 9;
     143           6 :                 csd->read_partial = 0;
     144           6 :                 csd->write_misalign = 0;
     145           6 :                 csd->read_misalign = 0;
     146           6 :                 csd->r2w_factor = 4; /* Unused */
     147           6 :                 csd->write_blkbits = 9;
     148           6 :                 csd->write_partial = 0;
     149           6 :                 break;
     150           9 :         default:
     151          15 :                 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
     152           3 :                         mmc_hostname(card->host), csd_struct);
     153           6 :                 return -EINVAL;
     154           3 :         }
     155           3 : 
     156           6 :         return 0;
     157           3 : }
     158           3 : 
     159           3 : /*
     160           3 :  * Given a 64-bit response, decode to our card SCR structure.
     161           3 :  */
     162           3 : static int mmc_decode_scr(struct mmc_card *card)
     163           3 : {
     164           9 :         struct sd_scr *scr = &card->scr;
     165           6 :         unsigned int scr_struct;
     166           6 :         u32 resp[4];
     167           6 : 
     168           9 :         resp[3] = card->raw_scr[1];
     169           9 :         resp[2] = card->raw_scr[0];
     170           6 : 
     171          24 :         scr_struct = UNSTUFF_BITS(resp, 60, 4);
     172          12 :         if (scr_struct != 0) {
     173          15 :                 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
     174           6 :                         mmc_hostname(card->host), scr_struct);
     175           9 :                 return -EINVAL;
     176           6 :         }
     177           6 : 
     178          24 :         scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
     179          24 :         scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
     180           6 : 
     181           9 :         return 0;
     182           6 : }
     183           3 : 
     184           3 : /*
     185           3 :  * Fetches and decodes switch information
     186           3 :  */
     187           3 : static int mmc_read_switch(struct mmc_card *card)
     188           3 : {
     189           6 :         int err;
     190           6 :         u8 *status;
     191           6 : 
     192          12 :         if (card->scr.sda_vsn < SCR_SPEC_VER_1)
     193           9 :                 return 0;
     194           6 : 
     195           9 :         if (!(card->csd.cmdclass & CCC_SWITCH)) {
     196          12 :                 printk(KERN_WARNING "%s: card lacks mandatory switch "
     197           3 :                         "function, performance might suffer.\n",
     198             :                         mmc_hostname(card->host));
     199           3 :                 return 0;
     200             :         }
     201             : 
     202           3 :         err = -EIO;
     203             : 
     204           9 :         status = kmalloc(64, GFP_KERNEL);
     205           6 :         if (!status) {
     206           9 :                 printk(KERN_ERR "%s: could not allocate a buffer for "
     207             :                         "switch capabilities.\n", mmc_hostname(card->host));
     208           3 :                 return -ENOMEM;
     209             :         }
     210             : 
     211           6 :         err = mmc_sd_switch(card, 0, 0, 1, status);
     212           6 :         if (err) {
     213             :                 /* If the host or the card can't do the switch,
     214             :                  * fail more gracefully. */
     215          18 :                 if ((err != -EINVAL)
     216             :                  && (err != -ENOSYS)
     217             :                  && (err != -EFAULT))
     218           3 :                         goto out;
     219             : 
     220           9 :                 printk(KERN_WARNING "%s: problem reading switch "
     221             :                         "capabilities, performance might suffer.\n",
     222             :                         mmc_hostname(card->host));
     223           3 :                 err = 0;
     224             : 
     225           3 :                 goto out;
     226             :         }
     227             : 
     228           6 :         if (status[13] & 0x02)
     229           3 :                 card->sw_caps.hs_max_dtr = 50000000;
     230             : 
     231             : out:
     232           6 :         kfree(status);
     233           3 : 
     234           6 :         return err;
     235             : }
     236             : 
     237             : /*
     238             :  * Test if the card supports high-speed mode and, if so, switch to it.
     239             :  */
     240             : static int mmc_switch_hs(struct mmc_card *card)
     241             : {
     242          24 :         int err;
     243          24 :         u8 *status;
     244          24 : 
     245          72 :         if (card->scr.sda_vsn < SCR_SPEC_VER_1)
     246          48 :                 return 0;
     247             : 
     248          48 :         if (!(card->csd.cmdclass & CCC_SWITCH))
     249          24 :                 return 0;
     250             : 
     251          48 :         if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
     252          24 :                 return 0;
     253             : 
     254          48 :         if (card->sw_caps.hs_max_dtr == 0)
     255          24 :                 return 0;
     256             : 
     257          24 :         err = -EIO;
     258             : 
     259          72 :         status = kmalloc(64, GFP_KERNEL);
     260          48 :         if (!status) {
     261          72 :                 printk(KERN_ERR "%s: could not allocate a buffer for "
     262             :                         "switch capabilities.\n", mmc_hostname(card->host));
     263          24 :                 return -ENOMEM;
     264             :         }
     265             : 
     266          48 :         err = mmc_sd_switch(card, 1, 0, 1, status);
     267          48 :         if (err)
     268          24 :                 goto out;
     269             : 
     270          48 :         if ((status[16] & 0xF) != 1) {
     271          72 :                 printk(KERN_WARNING "%s: Problem switching card "
     272             :                         "into high-speed mode!\n",
     273             :                         mmc_hostname(card->host));
     274             :         } else {
     275          24 :                 mmc_card_set_highspeed(card);
     276          48 :                 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
     277             :         }
     278             : 
     279          48 : out:
     280          72 :         kfree(status);
     281             : 
     282          72 :         return err;
     283             : }
     284             : 
     285           4 : MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
     286           1 :         card->raw_cid[2], card->raw_cid[3]);
     287           4 : MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
     288           1 :         card->raw_csd[2], card->raw_csd[3]);
     289           6 : MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
     290           6 : MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
     291           8 : MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
     292           7 : MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
     293           8 : MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
     294           7 : MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
     295           8 : MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
     296           8 : MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
     297           3 : 
     298           3 : 
     299           4 : static struct attribute *sd_std_attrs[] = {
     300           2 :         &dev_attr_cid.attr,
     301           1 :         &dev_attr_csd.attr,
     302             :         &dev_attr_scr.attr,
     303             :         &dev_attr_date.attr,
     304             :         &dev_attr_fwrev.attr,
     305             :         &dev_attr_hwrev.attr,
     306             :         &dev_attr_manfid.attr,
     307             :         &dev_attr_name.attr,
     308             :         &dev_attr_oemid.attr,
     309             :         &dev_attr_serial.attr,
     310             :         NULL,
     311             : };
     312             : 
     313           1 : static struct attribute_group sd_std_attr_group = {
     314             :         .attrs = sd_std_attrs,
     315             : };
     316             : 
     317           1 : static const struct attribute_group *sd_attr_groups[] = {
     318             :         &sd_std_attr_group,
     319             :         NULL,
     320             : };
     321             : 
     322           1 : static struct device_type sd_type = {
     323             :         .groups = sd_attr_groups,
     324             : };
     325             : 
     326             : /*
     327             :  * Handle the detection and initialisation of a card.
     328             :  *
     329             :  * In the case of a resume, "oldcard" will contain the card
     330             :  * we're trying to reinitialise.
     331             :  */
     332             : static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
     333             :         struct mmc_card *oldcard)
     334           3 : {
     335           3 :         struct mmc_card *card;
     336           3 :         int err;
     337           3 :         u32 cid[4];
     338           3 :         unsigned int max_dtr;
     339           3 : 
     340          21 :         BUG_ON(!host);
     341          27 :         WARN_ON(!host->claimed);
     342           3 : 
     343           3 :         /*
     344           3 :          * Since we're changing the OCR value, we seem to
     345           3 :          * need to tell some cards to go back to the idle
     346           3 :          * state.  We wait 1ms to give cards time to
     347             :          * respond.
     348             :          */
     349           9 :         mmc_go_idle(host);
     350             : 
     351             :         /*
     352             :          * If SD_SEND_IF_COND indicates an SD 2.0
     353             :          * compliant card and we should set bit 30
     354             :          * of the ocr to indicate that we can handle
     355             :          * block-addressed SDHC cards.
     356             :          */
     357           6 :         err = mmc_send_if_cond(host, ocr);
     358           6 :         if (!err)
     359           3 :                 ocr |= 1 << 30;
     360             : 
     361           9 :         err = mmc_send_app_op_cond(host, ocr, NULL);
     362           6 :         if (err)
     363           3 :                 goto err;
     364             : 
     365             :         /*
     366             :          * Fetch CID from card.
     367             :          */
     368           6 :         if (mmc_host_is_spi(host))
     369          15 :                 err = mmc_send_cid(host, cid);
     370             :         else
     371           6 :                 err = mmc_all_send_cid(host, cid);
     372          12 :         if (err)
     373           6 :                 goto err;
     374             : 
     375          12 :         if (oldcard) {
     376          18 :                 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
     377           6 :                         err = -ENOENT;
     378           6 :                         goto err;
     379             :                 }
     380             : 
     381           6 :                 card = oldcard;
     382             :         } else {
     383             :                 /*
     384             :                  * Allocate card structure.
     385             :                  */
     386          18 :                 card = mmc_alloc_card(host, &sd_type);
     387          12 :                 if (IS_ERR(card)) {
     388           9 :                         err = PTR_ERR(card);
     389           3 :                         goto err;
     390             :                 }
     391             : 
     392           3 :                 card->type = MMC_TYPE_SD;
     393           3 :                 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
     394             :         }
     395             : 
     396             :         /*
     397             :          * For native busses:  get card RCA and quit open drain mode.
     398             :          */
     399          18 :         if (!mmc_host_is_spi(host)) {
     400          18 :                 err = mmc_send_relative_addr(host, &card->rca);
     401           6 :                 if (err)
     402           3 :                         goto free_card;
     403             : 
     404           6 :                 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
     405             :         }
     406             : 
     407          24 :         if (!oldcard) {
     408             :                 /*
     409             :                  * Fetch CSD from card.
     410             :                  */
     411          48 :                 err = mmc_send_csd(card, card->raw_csd);
     412           6 :                 if (err)
     413           3 :                         goto free_card;
     414             : 
     415           9 :                 err = mmc_decode_csd(card);
     416           6 :                 if (err)
     417           3 :                         goto free_card;
     418             : 
     419           6 :                 mmc_decode_cid(card);
     420             :         }
     421             : 
     422             :         /*
     423             :          * Select card, as all following commands rely on that.
     424             :          */
     425          30 :         if (!mmc_host_is_spi(host)) {
     426          30 :                 err = mmc_select_card(card);
     427           6 :                 if (err)
     428           3 :                         goto free_card;
     429             :         }
     430             : 
     431          36 :         if (!oldcard) {
     432             :                 /*
     433             :                  * Fetch SCR from card.
     434             :                  */
     435          72 :                 err = mmc_app_send_scr(card, card->raw_scr);
     436           6 :                 if (err)
     437           3 :                         goto free_card;
     438             : 
     439           9 :                 err = mmc_decode_scr(card);
     440           6 :                 if (err < 0)
     441           3 :                         goto free_card;
     442             : 
     443             :                 /*
     444             :                  * Fetch switch information from card.
     445             :                  */
     446          18 :                 err = mmc_read_switch(card);
     447           6 :                 if (err)
     448           3 :                         goto free_card;
     449             :         }
     450             : 
     451             :         /*
     452             :          * For SPI, enable CRC as appropriate.
     453             :          * This CRC enable is located AFTER the reading of the
     454             :          * card registers because some SDHC cards are not able
     455             :          * to provide valid CRCs for non-512-byte blocks.
     456             :          */
     457          42 :         if (mmc_host_is_spi(host)) {
     458          42 :                 err = mmc_spi_set_crc(host, use_spi_crc);
     459           6 :                 if (err)
     460           3 :                         goto free_card;
     461             :         }
     462             : 
     463             :         /*
     464             :          * Attempt to change to high-speed (if supported)
     465             :          */
     466         144 :         err = mmc_switch_hs(card);
     467           6 :         if (err)
     468           3 :                 goto free_card;
     469             : 
     470             :         /*
     471             :          * Compute bus speed.
     472             :          */
     473           3 :         max_dtr = (unsigned int)-1;
     474             : 
     475           6 :         if (mmc_card_highspeed(card)) {
     476           6 :                 if (max_dtr > card->sw_caps.hs_max_dtr)
     477           3 :                         max_dtr = card->sw_caps.hs_max_dtr;
     478           6 :         } else if (max_dtr > card->csd.max_dtr) {
     479           3 :                 max_dtr = card->csd.max_dtr;
     480             :         }
     481             : 
     482           6 :         mmc_set_clock(host, max_dtr);
     483             : 
     484             :         /*
     485             :          * Switch to wider bus (if supported).
     486             :          */
     487          15 :         if ((host->caps & MMC_CAP_4_BIT_DATA) &&
     488             :                 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
     489           9 :                 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
     490           6 :                 if (err)
     491           3 :                         goto free_card;
     492             : 
     493           6 :                 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
     494             :         }
     495             : 
     496             :         /*
     497             :          * Check if read-only switch is active.
     498             :          */
     499          12 :         if (!oldcard) {
     500          90 :                 if (!host->ops->get_ro || host->ops->get_ro(host) < 0) {
     501          30 :                         printk(KERN_WARNING "%s: host does not "
     502             :                                 "support reading read-only "
     503             :                                 "switch. assuming write-enable.\n",
     504             :                                 mmc_hostname(host));
     505             :                 } else {
     506          72 :                         if (host->ops->get_ro(host) > 0)
     507           6 :                                 mmc_card_set_readonly(card);
     508             :                 }
     509             :         }
     510             : 
     511          24 :         if (!oldcard)
     512          12 :                 host->card = card;
     513             : 
     514          12 :         return 0;
     515          30 : 
     516             : free_card:
     517          60 :         if (!oldcard)
     518         120 :                 mmc_remove_card(card);
     519             : err:
     520             : 
     521          45 :         return err;
     522             : }
     523          33 : 
     524             : /*
     525             :  * Host is being removed. Free up the current card.
     526             :  */
     527             : static void mmc_sd_remove(struct mmc_host *host)
     528             : {
     529          14 :         BUG_ON(!host);
     530          16 :         BUG_ON(!host->card);
     531             : 
     532           8 :         mmc_remove_card(host->card);
     533           2 :         host->card = NULL;
     534           2 : }
     535             : 
     536             : /*
     537             :  * Card detection callback from host.
     538             :  */
     539             : static void mmc_sd_detect(struct mmc_host *host)
     540             : {
     541           1 :         int err;
     542           1 : 
     543           7 :         BUG_ON(!host);
     544           7 :         BUG_ON(!host->card);
     545             : 
     546           2 :         mmc_claim_host(host);
     547             : 
     548             :         /*
     549             :          * Just check if our card has been removed.
     550             :          */
     551           2 :         err = mmc_send_status(host->card, NULL);
     552             : 
     553           2 :         mmc_release_host(host);
     554             : 
     555           2 :         if (err) {
     556           2 :                 mmc_sd_remove(host);
     557             : 
     558           2 :                 mmc_claim_host(host);
     559           2 :                 mmc_detach_bus(host);
     560           2 :                 mmc_release_host(host);
     561             :         }
     562           2 : }
     563             : 
     564             : /*
     565             :  * Suspend callback from host.
     566             :  */
     567             : static int mmc_sd_suspend(struct mmc_host *host)
     568             : {
     569           0 :         BUG_ON(!host);
     570           0 :         BUG_ON(!host->card);
     571             : 
     572           0 :         mmc_claim_host(host);
     573           0 :         if (!mmc_host_is_spi(host))
     574           0 :                 mmc_deselect_cards(host);
     575           0 :         host->card->state &= ~MMC_STATE_HIGHSPEED;
     576           0 :         mmc_release_host(host);
     577             : 
     578           0 :         return 0;
     579             : }
     580             : 
     581             : /*
     582             :  * Resume callback from host.
     583             :  *
     584             :  * This function tries to determine if the same card is still present
     585             :  * and, if so, restore all state to it.
     586             :  */
     587             : static int mmc_sd_resume(struct mmc_host *host)
     588             : {
     589           1 :         int err;
     590           1 : 
     591           7 :         BUG_ON(!host);
     592           7 :         BUG_ON(!host->card);
     593             : 
     594           2 :         mmc_claim_host(host);
     595          20 :         err = mmc_sd_init_card(host, host->ocr, host->card);
     596           2 :         mmc_release_host(host);
     597             : 
     598           1 :         return err;
     599             : }
     600             : 
     601             : static void mmc_sd_power_restore(struct mmc_host *host)
     602             : {
     603           2 :         host->card->state &= ~MMC_STATE_HIGHSPEED;
     604           4 :         mmc_claim_host(host);
     605          40 :         mmc_sd_init_card(host, host->ocr, host->card);
     606           4 :         mmc_release_host(host);
     607           2 : }
     608             : 
     609           1 : static const struct mmc_bus_ops mmc_sd_ops = {
     610             :         .remove = mmc_sd_remove,
     611             :         .detect = mmc_sd_detect,
     612             :         .suspend = NULL,
     613             :         .resume = NULL,
     614             :         .power_restore = mmc_sd_power_restore,
     615             : };
     616             : 
     617           1 : static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
     618             :         .remove = mmc_sd_remove,
     619             :         .detect = mmc_sd_detect,
     620             :         .suspend = mmc_sd_suspend,
     621             :         .resume = mmc_sd_resume,
     622             :         .power_restore = mmc_sd_power_restore,
     623             : };
     624             : 
     625             : static void mmc_sd_attach_bus_ops(struct mmc_host *host)
     626             : {
     627           0 :         const struct mmc_bus_ops *bus_ops;
     628             : 
     629           0 :         if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable)
     630           0 :                 bus_ops = &mmc_sd_ops_unsafe;
     631             :         else
     632           0 :                 bus_ops = &mmc_sd_ops;
     633           0 :         mmc_attach_bus(host, bus_ops);
     634           0 : }
     635             : 
     636             : /*
     637             :  * Starting point for SD card init.
     638             :  */
     639             : int mmc_attach_sd(struct mmc_host *host, u32 ocr)
     640             : {
     641           0 :         int err;
     642           0 : 
     643           0 :         BUG_ON(!host);
     644           0 :         WARN_ON(!host->claimed);
     645           0 : 
     646           0 :         mmc_sd_attach_bus_ops(host);
     647           0 : 
     648             :         /*
     649             :          * We need to get OCR a different way for SPI.
     650             :          */
     651           0 :         if (mmc_host_is_spi(host)) {
     652           0 :                 mmc_go_idle(host);
     653             : 
     654           0 :                 err = mmc_spi_read_ocr(host, 0, &ocr);
     655           0 :                 if (err)
     656           0 :                         goto err;
     657             :         }
     658             : 
     659             :         /*
     660             :          * Sanity check the voltages that the card claims to
     661             :          * support.
     662             :          */
     663           0 :         if (ocr & 0x7F) {
     664           0 :                 printk(KERN_WARNING "%s: card claims to support voltages "
     665             :                        "below the defined range. These will be ignored.\n",
     666             :                        mmc_hostname(host));
     667           0 :                 ocr &= ~0x7F;
     668             :         }
     669             : 
     670           0 :         if (ocr & MMC_VDD_165_195) {
     671           0 :                 printk(KERN_WARNING "%s: SD card claims to support the "
     672             :                        "incompletely defined 'low voltage range'. This "
     673             :                        "will be ignored.\n", mmc_hostname(host));
     674           0 :                 ocr &= ~MMC_VDD_165_195;
     675             :         }
     676             : 
     677           0 :         host->ocr = mmc_select_voltage(host, ocr);
     678             : 
     679             :         /*
     680             :          * Can we support the voltage(s) of the card(s)?
     681             :          */
     682           0 :         if (!host->ocr) {
     683           0 :                 err = -EINVAL;
     684           0 :                 goto err;
     685             :         }
     686             : 
     687             :         /*
     688             :          * Detect and init the card.
     689             :          */
     690           0 :         err = mmc_sd_init_card(host, host->ocr, NULL);
     691           0 :         if (err)
     692           0 :                 goto err;
     693             : 
     694           0 :         mmc_release_host(host);
     695             : 
     696           0 :         err = mmc_add_card(host->card);
     697           0 :         if (err)
     698           0 :                 goto remove_card;
     699             : 
     700           0 :         return 0;
     701           0 : 
     702             : remove_card:
     703           0 :         mmc_remove_card(host->card);
     704           0 :         host->card = NULL;
     705           0 :         mmc_claim_host(host);
     706             : err:
     707           0 :         mmc_detach_bus(host);
     708           0 :         mmc_release_host(host);
     709             : 
     710           0 :         printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
     711             :                 mmc_hostname(host), err);
     712             : 
     713           0 :         return err;
     714             : }
     715             : 

Generated by: LCOV version 1.10