LCOV - code coverage report
Current view: top level - drivers/mmc/core - sd.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 455 0.0 %
Date: 2014-02-18 Functions: 0 42 0.0 %
Branches: 0 319 0.0 %

           Branch data     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                 :            : #include <linux/slab.h>
      15                 :            : #include <linux/stat.h>
      16                 :            : #include <linux/pm_runtime.h>
      17                 :            : 
      18                 :            : #include <linux/mmc/host.h>
      19                 :            : #include <linux/mmc/card.h>
      20                 :            : #include <linux/mmc/mmc.h>
      21                 :            : #include <linux/mmc/sd.h>
      22                 :            : 
      23                 :            : #include "core.h"
      24                 :            : #include "bus.h"
      25                 :            : #include "mmc_ops.h"
      26                 :            : #include "sd.h"
      27                 :            : #include "sd_ops.h"
      28                 :            : 
      29                 :            : static const unsigned int tran_exp[] = {
      30                 :            :         10000,          100000,         1000000,        10000000,
      31                 :            :         0,              0,              0,              0
      32                 :            : };
      33                 :            : 
      34                 :            : static const unsigned char tran_mant[] = {
      35                 :            :         0,      10,     12,     13,     15,     20,     25,     30,
      36                 :            :         35,     40,     45,     50,     55,     60,     70,     80,
      37                 :            : };
      38                 :            : 
      39                 :            : static const unsigned int tacc_exp[] = {
      40                 :            :         1,      10,     100,    1000,   10000,  100000, 1000000, 10000000,
      41                 :            : };
      42                 :            : 
      43                 :            : static const unsigned int tacc_mant[] = {
      44                 :            :         0,      10,     12,     13,     15,     20,     25,     30,
      45                 :            :         35,     40,     45,     50,     55,     60,     70,     80,
      46                 :            : };
      47                 :            : 
      48                 :            : #define UNSTUFF_BITS(resp,start,size)                                   \
      49                 :            :         ({                                                              \
      50                 :            :                 const int __size = size;                                \
      51                 :            :                 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;        \
      52                 :            :                 const int __off = 3 - ((start) / 32);                   \
      53                 :            :                 const int __shft = (start) & 31;                    \
      54                 :            :                 u32 __res;                                              \
      55                 :            :                                                                         \
      56                 :            :                 __res = resp[__off] >> __shft;                            \
      57                 :            :                 if (__size + __shft > 32)                            \
      58                 :            :                         __res |= resp[__off-1] << ((32 - __shft) % 32);   \
      59                 :            :                 __res & __mask;                                             \
      60                 :            :         })
      61                 :            : 
      62                 :            : /*
      63                 :            :  * Given the decoded CSD structure, decode the raw CID to our CID structure.
      64                 :            :  */
      65                 :          0 : void mmc_decode_cid(struct mmc_card *card)
      66                 :            : {
      67                 :            :         u32 *resp = card->raw_cid;
      68                 :            : 
      69                 :          0 :         memset(&card->cid, 0, sizeof(struct mmc_cid));
      70                 :            : 
      71                 :            :         /*
      72                 :            :          * SD doesn't currently have a version field so we will
      73                 :            :          * have to assume we can parse this.
      74                 :            :          */
      75                 :          0 :         card->cid.manfid             = UNSTUFF_BITS(resp, 120, 8);
      76                 :          0 :         card->cid.oemid                      = UNSTUFF_BITS(resp, 104, 16);
      77                 :          0 :         card->cid.prod_name[0]               = UNSTUFF_BITS(resp, 96, 8);
      78                 :          0 :         card->cid.prod_name[1]               = UNSTUFF_BITS(resp, 88, 8);
      79                 :          0 :         card->cid.prod_name[2]               = UNSTUFF_BITS(resp, 80, 8);
      80                 :          0 :         card->cid.prod_name[3]               = UNSTUFF_BITS(resp, 72, 8);
      81                 :          0 :         card->cid.prod_name[4]               = UNSTUFF_BITS(resp, 64, 8);
      82                 :          0 :         card->cid.hwrev                      = UNSTUFF_BITS(resp, 60, 4);
      83                 :          0 :         card->cid.fwrev                      = UNSTUFF_BITS(resp, 56, 4);
      84                 :          0 :         card->cid.serial             = UNSTUFF_BITS(resp, 24, 32);
      85                 :          0 :         card->cid.year                       = UNSTUFF_BITS(resp, 12, 8);
      86                 :          0 :         card->cid.month                      = UNSTUFF_BITS(resp, 8, 4);
      87                 :            : 
      88                 :          0 :         card->cid.year += 2000; /* SD cards year offset */
      89                 :          0 : }
      90                 :            : 
      91                 :            : /*
      92                 :            :  * Given a 128-bit response, decode to our card CSD structure.
      93                 :            :  */
      94                 :          0 : static int mmc_decode_csd(struct mmc_card *card)
      95                 :            : {
      96                 :            :         struct mmc_csd *csd = &card->csd;
      97                 :            :         unsigned int e, m, csd_struct;
      98                 :            :         u32 *resp = card->raw_csd;
      99                 :            : 
     100                 :          0 :         csd_struct = UNSTUFF_BITS(resp, 126, 2);
     101                 :            : 
     102      [ #  #  # ]:          0 :         switch (csd_struct) {
     103                 :            :         case 0:
     104                 :          0 :                 m = UNSTUFF_BITS(resp, 115, 4);
     105                 :          0 :                 e = UNSTUFF_BITS(resp, 112, 3);
     106                 :          0 :                 csd->tacc_ns  = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
     107                 :          0 :                 csd->tacc_clks        = UNSTUFF_BITS(resp, 104, 8) * 100;
     108                 :            : 
     109                 :          0 :                 m = UNSTUFF_BITS(resp, 99, 4);
     110                 :          0 :                 e = UNSTUFF_BITS(resp, 96, 3);
     111                 :          0 :                 csd->max_dtr   = tran_exp[e] * tran_mant[m];
     112                 :          0 :                 csd->cmdclass          = UNSTUFF_BITS(resp, 84, 12);
     113                 :            : 
     114                 :          0 :                 e = UNSTUFF_BITS(resp, 47, 3);
     115                 :          0 :                 m = UNSTUFF_BITS(resp, 62, 12);
     116                 :          0 :                 csd->capacity          = (1 + m) << (e + 2);
     117                 :            : 
     118                 :          0 :                 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
     119                 :          0 :                 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
     120                 :          0 :                 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
     121                 :          0 :                 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
     122                 :          0 :                 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
     123                 :          0 :                 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
     124                 :          0 :                 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
     125                 :            : 
     126         [ #  # ]:          0 :                 if (UNSTUFF_BITS(resp, 46, 1)) {
     127                 :          0 :                         csd->erase_size = 1;
     128         [ #  # ]:          0 :                 } else if (csd->write_blkbits >= 9) {
     129                 :          0 :                         csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
     130                 :          0 :                         csd->erase_size <<= csd->write_blkbits - 9;
     131                 :            :                 }
     132                 :            :                 break;
     133                 :            :         case 1:
     134                 :            :                 /*
     135                 :            :                  * This is a block-addressed SDHC or SDXC card. Most
     136                 :            :                  * interesting fields are unused and have fixed
     137                 :            :                  * values. To avoid getting tripped by buggy cards,
     138                 :            :                  * we assume those fixed values ourselves.
     139                 :            :                  */
     140                 :          0 :                 mmc_card_set_blockaddr(card);
     141                 :            : 
     142                 :          0 :                 csd->tacc_ns  = 0; /* Unused */
     143                 :          0 :                 csd->tacc_clks        = 0; /* Unused */
     144                 :            : 
     145                 :          0 :                 m = UNSTUFF_BITS(resp, 99, 4);
     146                 :          0 :                 e = UNSTUFF_BITS(resp, 96, 3);
     147                 :          0 :                 csd->max_dtr   = tran_exp[e] * tran_mant[m];
     148                 :          0 :                 csd->cmdclass          = UNSTUFF_BITS(resp, 84, 12);
     149                 :          0 :                 csd->c_size    = UNSTUFF_BITS(resp, 48, 22);
     150                 :            : 
     151                 :            :                 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
     152         [ #  # ]:          0 :                 if (csd->c_size >= 0xFFFF)
     153                 :          0 :                         mmc_card_set_ext_capacity(card);
     154                 :            : 
     155                 :            :                 m = UNSTUFF_BITS(resp, 48, 22);
     156                 :          0 :                 csd->capacity     = (1 + m) << 10;
     157                 :            : 
     158                 :          0 :                 csd->read_blkbits = 9;
     159                 :          0 :                 csd->read_partial = 0;
     160                 :          0 :                 csd->write_misalign = 0;
     161                 :          0 :                 csd->read_misalign = 0;
     162                 :          0 :                 csd->r2w_factor = 4; /* Unused */
     163                 :          0 :                 csd->write_blkbits = 9;
     164                 :          0 :                 csd->write_partial = 0;
     165                 :          0 :                 csd->erase_size = 1;
     166                 :          0 :                 break;
     167                 :            :         default:
     168                 :          0 :                 pr_err("%s: unrecognised CSD structure version %d\n",
     169                 :            :                         mmc_hostname(card->host), csd_struct);
     170                 :          0 :                 return -EINVAL;
     171                 :            :         }
     172                 :            : 
     173                 :          0 :         card->erase_size = csd->erase_size;
     174                 :            : 
     175                 :          0 :         return 0;
     176                 :            : }
     177                 :            : 
     178                 :            : /*
     179                 :            :  * Given a 64-bit response, decode to our card SCR structure.
     180                 :            :  */
     181                 :          0 : static int mmc_decode_scr(struct mmc_card *card)
     182                 :            : {
     183                 :            :         struct sd_scr *scr = &card->scr;
     184                 :            :         unsigned int scr_struct;
     185                 :            :         u32 resp[4];
     186                 :            : 
     187                 :            :         resp[3] = card->raw_scr[1];
     188                 :          0 :         resp[2] = card->raw_scr[0];
     189                 :            : 
     190                 :          0 :         scr_struct = UNSTUFF_BITS(resp, 60, 4);
     191         [ #  # ]:          0 :         if (scr_struct != 0) {
     192                 :          0 :                 pr_err("%s: unrecognised SCR structure version %d\n",
     193                 :            :                         mmc_hostname(card->host), scr_struct);
     194                 :          0 :                 return -EINVAL;
     195                 :            :         }
     196                 :            : 
     197                 :          0 :         scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
     198                 :          0 :         scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
     199         [ #  # ]:          0 :         if (scr->sda_vsn == SCR_SPEC_VER_2)
     200                 :            :                 /* Check if Physical Layer Spec v3.0 is supported */
     201                 :          0 :                 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
     202                 :            : 
     203         [ #  # ]:          0 :         if (UNSTUFF_BITS(resp, 55, 1))
     204                 :          0 :                 card->erased_byte = 0xFF;
     205                 :            :         else
     206                 :          0 :                 card->erased_byte = 0x0;
     207                 :            : 
     208         [ #  # ]:          0 :         if (scr->sda_spec3)
     209                 :          0 :                 scr->cmds = UNSTUFF_BITS(resp, 32, 2);
     210                 :            :         return 0;
     211                 :            : }
     212                 :            : 
     213                 :            : /*
     214                 :            :  * Fetch and process SD Status register.
     215                 :            :  */
     216                 :          0 : static int mmc_read_ssr(struct mmc_card *card)
     217                 :            : {
     218                 :            :         unsigned int au, es, et, eo;
     219                 :            :         int err, i, max_au;
     220                 :            :         u32 *ssr;
     221                 :            : 
     222         [ #  # ]:          0 :         if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
     223                 :          0 :                 pr_warning("%s: card lacks mandatory SD Status "
     224                 :            :                         "function.\n", mmc_hostname(card->host));
     225                 :          0 :                 return 0;
     226                 :            :         }
     227                 :            : 
     228                 :            :         ssr = kmalloc(64, GFP_KERNEL);
     229         [ #  # ]:          0 :         if (!ssr)
     230                 :            :                 return -ENOMEM;
     231                 :            : 
     232                 :          0 :         err = mmc_app_sd_status(card, ssr);
     233         [ #  # ]:          0 :         if (err) {
     234                 :          0 :                 pr_warning("%s: problem reading SD Status "
     235                 :            :                         "register.\n", mmc_hostname(card->host));
     236                 :            :                 err = 0;
     237                 :          0 :                 goto out;
     238                 :            :         }
     239                 :            : 
     240         [ #  # ]:          0 :         for (i = 0; i < 16; i++)
     241         [ #  # ]:          0 :                 ssr[i] = be32_to_cpu(ssr[i]);
     242                 :            : 
     243                 :            :         /* SD3.0 increases max AU size to 64MB (0xF) from 4MB (0x9) */
     244         [ #  # ]:          0 :         max_au = card->scr.sda_spec3 ? 0xF : 0x9;
     245                 :            : 
     246                 :            :         /*
     247                 :            :          * UNSTUFF_BITS only works with four u32s so we have to offset the
     248                 :            :          * bitfield positions accordingly.
     249                 :            :          */
     250                 :          0 :         au = UNSTUFF_BITS(ssr, 428 - 384, 4);
     251         [ #  # ]:          0 :         if (au > 0 && au <= max_au) {
     252                 :          0 :                 card->ssr.au = 1 << (au + 4);
     253                 :          0 :                 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
     254                 :          0 :                 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
     255                 :          0 :                 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
     256         [ #  # ]:          0 :                 if (es && et) {
     257                 :          0 :                         card->ssr.erase_timeout = (et * 1000) / es;
     258                 :          0 :                         card->ssr.erase_offset = eo * 1000;
     259                 :            :                 }
     260                 :            :         } else {
     261                 :          0 :                 pr_warning("%s: SD Status: Invalid Allocation Unit "
     262                 :            :                         "size.\n", mmc_hostname(card->host));
     263                 :            :         }
     264                 :            : out:
     265                 :          0 :         kfree(ssr);
     266                 :          0 :         return err;
     267                 :            : }
     268                 :            : 
     269                 :            : /*
     270                 :            :  * Fetches and decodes switch information
     271                 :            :  */
     272                 :          0 : static int mmc_read_switch(struct mmc_card *card)
     273                 :            : {
     274                 :            :         int err;
     275                 :            :         u8 *status;
     276                 :            : 
     277         [ #  # ]:          0 :         if (card->scr.sda_vsn < SCR_SPEC_VER_1)
     278                 :            :                 return 0;
     279                 :            : 
     280         [ #  # ]:          0 :         if (!(card->csd.cmdclass & CCC_SWITCH)) {
     281                 :          0 :                 pr_warning("%s: card lacks mandatory switch "
     282                 :            :                         "function, performance might suffer.\n",
     283                 :            :                         mmc_hostname(card->host));
     284                 :          0 :                 return 0;
     285                 :            :         }
     286                 :            : 
     287                 :            :         err = -EIO;
     288                 :            : 
     289                 :            :         status = kmalloc(64, GFP_KERNEL);
     290         [ #  # ]:          0 :         if (!status) {
     291                 :          0 :                 pr_err("%s: could not allocate a buffer for "
     292                 :            :                         "switch capabilities.\n",
     293                 :            :                         mmc_hostname(card->host));
     294                 :          0 :                 return -ENOMEM;
     295                 :            :         }
     296                 :            : 
     297                 :            :         /*
     298                 :            :          * Find out the card's support bits with a mode 0 operation.
     299                 :            :          * The argument does not matter, as the support bits do not
     300                 :            :          * change with the arguments.
     301                 :            :          */
     302                 :          0 :         err = mmc_sd_switch(card, 0, 0, 0, status);
     303         [ #  # ]:          0 :         if (err) {
     304                 :            :                 /*
     305                 :            :                  * If the host or the card can't do the switch,
     306                 :            :                  * fail more gracefully.
     307                 :            :                  */
     308 [ #  # ][ #  # ]:          0 :                 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
     309                 :            :                         goto out;
     310                 :            : 
     311                 :          0 :                 pr_warning("%s: problem reading Bus Speed modes.\n",
     312                 :            :                         mmc_hostname(card->host));
     313                 :            :                 err = 0;
     314                 :            : 
     315                 :          0 :                 goto out;
     316                 :            :         }
     317                 :            : 
     318         [ #  # ]:          0 :         if (status[13] & SD_MODE_HIGH_SPEED)
     319                 :          0 :                 card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
     320                 :            : 
     321         [ #  # ]:          0 :         if (card->scr.sda_spec3) {
     322                 :          0 :                 card->sw_caps.sd3_bus_mode = status[13];
     323                 :            :                 /* Driver Strengths supported by the card */
     324                 :          0 :                 card->sw_caps.sd3_drv_type = status[9];
     325                 :            :         }
     326                 :            : 
     327                 :            : out:
     328                 :          0 :         kfree(status);
     329                 :            : 
     330                 :          0 :         return err;
     331                 :            : }
     332                 :            : 
     333                 :            : /*
     334                 :            :  * Test if the card supports high-speed mode and, if so, switch to it.
     335                 :            :  */
     336                 :          0 : int mmc_sd_switch_hs(struct mmc_card *card)
     337                 :            : {
     338                 :            :         int err;
     339                 :            :         u8 *status;
     340                 :            : 
     341         [ #  # ]:          0 :         if (card->scr.sda_vsn < SCR_SPEC_VER_1)
     342                 :            :                 return 0;
     343                 :            : 
     344         [ #  # ]:          0 :         if (!(card->csd.cmdclass & CCC_SWITCH))
     345                 :            :                 return 0;
     346                 :            : 
     347         [ #  # ]:          0 :         if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
     348                 :            :                 return 0;
     349                 :            : 
     350         [ #  # ]:          0 :         if (card->sw_caps.hs_max_dtr == 0)
     351                 :            :                 return 0;
     352                 :            : 
     353                 :            :         err = -EIO;
     354                 :            : 
     355                 :            :         status = kmalloc(64, GFP_KERNEL);
     356         [ #  # ]:          0 :         if (!status) {
     357                 :          0 :                 pr_err("%s: could not allocate a buffer for "
     358                 :            :                         "switch capabilities.\n", mmc_hostname(card->host));
     359                 :          0 :                 return -ENOMEM;
     360                 :            :         }
     361                 :            : 
     362                 :          0 :         err = mmc_sd_switch(card, 1, 0, 1, status);
     363         [ #  # ]:          0 :         if (err)
     364                 :            :                 goto out;
     365                 :            : 
     366         [ #  # ]:          0 :         if ((status[16] & 0xF) != 1) {
     367                 :          0 :                 pr_warning("%s: Problem switching card "
     368                 :            :                         "into high-speed mode!\n",
     369                 :            :                         mmc_hostname(card->host));
     370                 :            :                 err = 0;
     371                 :            :         } else {
     372                 :            :                 err = 1;
     373                 :            :         }
     374                 :            : 
     375                 :            : out:
     376                 :          0 :         kfree(status);
     377                 :            : 
     378                 :          0 :         return err;
     379                 :            : }
     380                 :            : 
     381                 :          0 : static int sd_select_driver_type(struct mmc_card *card, u8 *status)
     382                 :            : {
     383                 :            :         int host_drv_type = SD_DRIVER_TYPE_B;
     384                 :            :         int card_drv_type = SD_DRIVER_TYPE_B;
     385                 :            :         int drive_strength;
     386                 :            :         int err;
     387                 :            : 
     388                 :            :         /*
     389                 :            :          * If the host doesn't support any of the Driver Types A,C or D,
     390                 :            :          * or there is no board specific handler then default Driver
     391                 :            :          * Type B is used.
     392                 :            :          */
     393         [ #  # ]:          0 :         if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
     394                 :            :             | MMC_CAP_DRIVER_TYPE_D)))
     395                 :            :                 return 0;
     396                 :            : 
     397         [ #  # ]:          0 :         if (!card->host->ops->select_drive_strength)
     398                 :            :                 return 0;
     399                 :            : 
     400         [ #  # ]:          0 :         if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
     401                 :            :                 host_drv_type |= SD_DRIVER_TYPE_A;
     402                 :            : 
     403         [ #  # ]:          0 :         if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
     404                 :          0 :                 host_drv_type |= SD_DRIVER_TYPE_C;
     405                 :            : 
     406         [ #  # ]:          0 :         if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
     407                 :          0 :                 host_drv_type |= SD_DRIVER_TYPE_D;
     408                 :            : 
     409         [ #  # ]:          0 :         if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
     410                 :            :                 card_drv_type |= SD_DRIVER_TYPE_A;
     411                 :            : 
     412         [ #  # ]:          0 :         if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
     413                 :          0 :                 card_drv_type |= SD_DRIVER_TYPE_C;
     414                 :            : 
     415         [ #  # ]:          0 :         if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
     416                 :          0 :                 card_drv_type |= SD_DRIVER_TYPE_D;
     417                 :            : 
     418                 :            :         /*
     419                 :            :          * The drive strength that the hardware can support
     420                 :            :          * depends on the board design.  Pass the appropriate
     421                 :            :          * information and let the hardware specific code
     422                 :            :          * return what is possible given the options
     423                 :            :          */
     424                 :            :         mmc_host_clk_hold(card->host);
     425                 :          0 :         drive_strength = card->host->ops->select_drive_strength(
     426                 :            :                 card->sw_caps.uhs_max_dtr,
     427                 :            :                 host_drv_type, card_drv_type);
     428                 :            :         mmc_host_clk_release(card->host);
     429                 :            : 
     430                 :          0 :         err = mmc_sd_switch(card, 1, 2, drive_strength, status);
     431         [ #  # ]:          0 :         if (err)
     432                 :            :                 return err;
     433                 :            : 
     434         [ #  # ]:          0 :         if ((status[15] & 0xF) != drive_strength) {
     435                 :          0 :                 pr_warning("%s: Problem setting drive strength!\n",
     436                 :            :                         mmc_hostname(card->host));
     437                 :          0 :                 return 0;
     438                 :            :         }
     439                 :            : 
     440                 :          0 :         mmc_set_driver_type(card->host, drive_strength);
     441                 :            : 
     442                 :          0 :         return 0;
     443                 :            : }
     444                 :            : 
     445                 :          0 : static void sd_update_bus_speed_mode(struct mmc_card *card)
     446                 :            : {
     447                 :            :         /*
     448                 :            :          * If the host doesn't support any of the UHS-I modes, fallback on
     449                 :            :          * default speed.
     450                 :            :          */
     451         [ #  # ]:          0 :         if (!mmc_host_uhs(card->host)) {
     452                 :          0 :                 card->sd_bus_speed = 0;
     453                 :          0 :                 return;
     454                 :            :         }
     455                 :            : 
     456 [ #  # ][ #  # ]:          0 :         if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
     457                 :          0 :             (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
     458                 :          0 :                         card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
     459 [ #  # ][ #  # ]:          0 :         } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
     460                 :          0 :                    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
     461                 :          0 :                         card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
     462         [ #  # ]:          0 :         } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
     463         [ #  # ]:          0 :                     MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
     464                 :            :                     SD_MODE_UHS_SDR50)) {
     465                 :          0 :                         card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
     466         [ #  # ]:          0 :         } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
     467         [ #  # ]:          0 :                     MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
     468                 :          0 :                    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
     469                 :          0 :                         card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
     470         [ #  # ]:          0 :         } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
     471                 :            :                     MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
     472         [ #  # ]:          0 :                     MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
     473                 :            :                     SD_MODE_UHS_SDR12)) {
     474                 :          0 :                         card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
     475                 :            :         }
     476                 :            : }
     477                 :            : 
     478                 :          0 : static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
     479                 :            : {
     480                 :            :         int err;
     481                 :            :         unsigned int timing = 0;
     482                 :            : 
     483   [ #  #  #  #  :          0 :         switch (card->sd_bus_speed) {
                   #  # ]
     484                 :            :         case UHS_SDR104_BUS_SPEED:
     485                 :            :                 timing = MMC_TIMING_UHS_SDR104;
     486                 :          0 :                 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
     487                 :          0 :                 break;
     488                 :            :         case UHS_DDR50_BUS_SPEED:
     489                 :            :                 timing = MMC_TIMING_UHS_DDR50;
     490                 :          0 :                 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
     491                 :          0 :                 break;
     492                 :            :         case UHS_SDR50_BUS_SPEED:
     493                 :            :                 timing = MMC_TIMING_UHS_SDR50;
     494                 :          0 :                 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
     495                 :          0 :                 break;
     496                 :            :         case UHS_SDR25_BUS_SPEED:
     497                 :            :                 timing = MMC_TIMING_UHS_SDR25;
     498                 :          0 :                 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
     499                 :          0 :                 break;
     500                 :            :         case UHS_SDR12_BUS_SPEED:
     501                 :            :                 timing = MMC_TIMING_UHS_SDR12;
     502                 :          0 :                 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
     503                 :          0 :                 break;
     504                 :            :         default:
     505                 :            :                 return 0;
     506                 :            :         }
     507                 :            : 
     508                 :          0 :         err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
     509         [ #  # ]:          0 :         if (err)
     510                 :            :                 return err;
     511                 :            : 
     512         [ #  # ]:          0 :         if ((status[16] & 0xF) != card->sd_bus_speed)
     513                 :          0 :                 pr_warning("%s: Problem setting bus speed mode!\n",
     514                 :            :                         mmc_hostname(card->host));
     515                 :            :         else {
     516                 :          0 :                 mmc_set_timing(card->host, timing);
     517                 :          0 :                 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
     518                 :            :         }
     519                 :            : 
     520                 :            :         return 0;
     521                 :            : }
     522                 :            : 
     523                 :            : /* Get host's max current setting at its current voltage */
     524                 :          0 : static u32 sd_get_host_max_current(struct mmc_host *host)
     525                 :            : {
     526                 :            :         u32 voltage, max_current;
     527                 :            : 
     528                 :          0 :         voltage = 1 << host->ios.vdd;
     529   [ #  #  #  # ]:          0 :         switch (voltage) {
     530                 :            :         case MMC_VDD_165_195:
     531                 :          0 :                 max_current = host->max_current_180;
     532                 :          0 :                 break;
     533                 :            :         case MMC_VDD_29_30:
     534                 :            :         case MMC_VDD_30_31:
     535                 :          0 :                 max_current = host->max_current_300;
     536                 :          0 :                 break;
     537                 :            :         case MMC_VDD_32_33:
     538                 :            :         case MMC_VDD_33_34:
     539                 :          0 :                 max_current = host->max_current_330;
     540                 :          0 :                 break;
     541                 :            :         default:
     542                 :            :                 max_current = 0;
     543                 :            :         }
     544                 :            : 
     545                 :          0 :         return max_current;
     546                 :            : }
     547                 :            : 
     548                 :          0 : static int sd_set_current_limit(struct mmc_card *card, u8 *status)
     549                 :            : {
     550                 :            :         int current_limit = SD_SET_CURRENT_NO_CHANGE;
     551                 :            :         int err;
     552                 :            :         u32 max_current;
     553                 :            : 
     554                 :            :         /*
     555                 :            :          * Current limit switch is only defined for SDR50, SDR104, and DDR50
     556                 :            :          * bus speed modes. For other bus speed modes, we do not change the
     557                 :            :          * current limit.
     558                 :            :          */
     559         [ #  # ]:          0 :         if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) &&
     560                 :          0 :             (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) &&
     561                 :            :             (card->sd_bus_speed != UHS_DDR50_BUS_SPEED))
     562                 :            :                 return 0;
     563                 :            : 
     564                 :            :         /*
     565                 :            :          * Host has different current capabilities when operating at
     566                 :            :          * different voltages, so find out its max current first.
     567                 :            :          */
     568                 :          0 :         max_current = sd_get_host_max_current(card->host);
     569                 :            : 
     570                 :            :         /*
     571                 :            :          * We only check host's capability here, if we set a limit that is
     572                 :            :          * higher than the card's maximum current, the card will be using its
     573                 :            :          * maximum current, e.g. if the card's maximum current is 300ma, and
     574                 :            :          * when we set current limit to 200ma, the card will draw 200ma, and
     575                 :            :          * when we set current limit to 400/600/800ma, the card will draw its
     576                 :            :          * maximum 300ma from the host.
     577                 :            :          */
     578         [ #  # ]:          0 :         if (max_current >= 800)
     579                 :            :                 current_limit = SD_SET_CURRENT_LIMIT_800;
     580         [ #  # ]:          0 :         else if (max_current >= 600)
     581                 :            :                 current_limit = SD_SET_CURRENT_LIMIT_600;
     582         [ #  # ]:          0 :         else if (max_current >= 400)
     583                 :            :                 current_limit = SD_SET_CURRENT_LIMIT_400;
     584         [ #  # ]:          0 :         else if (max_current >= 200)
     585                 :            :                 current_limit = SD_SET_CURRENT_LIMIT_200;
     586                 :            : 
     587         [ #  # ]:          0 :         if (current_limit != SD_SET_CURRENT_NO_CHANGE) {
     588                 :          0 :                 err = mmc_sd_switch(card, 1, 3, current_limit, status);
     589         [ #  # ]:          0 :                 if (err)
     590                 :            :                         return err;
     591                 :            : 
     592         [ #  # ]:          0 :                 if (((status[15] >> 4) & 0x0F) != current_limit)
     593                 :          0 :                         pr_warning("%s: Problem setting current limit!\n",
     594                 :            :                                 mmc_hostname(card->host));
     595                 :            : 
     596                 :            :         }
     597                 :            : 
     598                 :            :         return 0;
     599                 :            : }
     600                 :            : 
     601                 :            : /*
     602                 :            :  * UHS-I specific initialization procedure
     603                 :            :  */
     604                 :          0 : static int mmc_sd_init_uhs_card(struct mmc_card *card)
     605                 :            : {
     606                 :            :         int err;
     607                 :            :         u8 *status;
     608                 :            : 
     609         [ #  # ]:          0 :         if (!card->scr.sda_spec3)
     610                 :            :                 return 0;
     611                 :            : 
     612         [ #  # ]:          0 :         if (!(card->csd.cmdclass & CCC_SWITCH))
     613                 :            :                 return 0;
     614                 :            : 
     615                 :            :         status = kmalloc(64, GFP_KERNEL);
     616         [ #  # ]:          0 :         if (!status) {
     617                 :          0 :                 pr_err("%s: could not allocate a buffer for "
     618                 :            :                         "switch capabilities.\n", mmc_hostname(card->host));
     619                 :          0 :                 return -ENOMEM;
     620                 :            :         }
     621                 :            : 
     622                 :            :         /* Set 4-bit bus width */
     623 [ #  # ][ #  # ]:          0 :         if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
     624                 :          0 :             (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
     625                 :          0 :                 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
     626         [ #  # ]:          0 :                 if (err)
     627                 :            :                         goto out;
     628                 :            : 
     629                 :          0 :                 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
     630                 :            :         }
     631                 :            : 
     632                 :            :         /*
     633                 :            :          * Select the bus speed mode depending on host
     634                 :            :          * and card capability.
     635                 :            :          */
     636                 :          0 :         sd_update_bus_speed_mode(card);
     637                 :            : 
     638                 :            :         /* Set the driver strength for the card */
     639                 :          0 :         err = sd_select_driver_type(card, status);
     640         [ #  # ]:          0 :         if (err)
     641                 :            :                 goto out;
     642                 :            : 
     643                 :            :         /* Set current limit for the card */
     644                 :          0 :         err = sd_set_current_limit(card, status);
     645         [ #  # ]:          0 :         if (err)
     646                 :            :                 goto out;
     647                 :            : 
     648                 :            :         /* Set bus speed mode of the card */
     649                 :          0 :         err = sd_set_bus_speed_mode(card, status);
     650         [ #  # ]:          0 :         if (err)
     651                 :            :                 goto out;
     652                 :            : 
     653                 :            :         /*
     654                 :            :          * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
     655                 :            :          * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
     656                 :            :          */
     657 [ #  # ][ #  # ]:          0 :         if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning &&
                 [ #  # ]
     658                 :          0 :                         (card->sd_bus_speed == UHS_SDR50_BUS_SPEED ||
     659                 :            :                          card->sd_bus_speed == UHS_SDR104_BUS_SPEED)) {
     660                 :            :                 mmc_host_clk_hold(card->host);
     661                 :          0 :                 err = card->host->ops->execute_tuning(card->host,
     662                 :            :                                                       MMC_SEND_TUNING_BLOCK);
     663                 :            :                 mmc_host_clk_release(card->host);
     664                 :            :         }
     665                 :            : 
     666                 :            : out:
     667                 :          0 :         kfree(status);
     668                 :            : 
     669                 :          0 :         return err;
     670                 :            : }
     671                 :            : 
     672                 :          0 : MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
     673                 :            :         card->raw_cid[2], card->raw_cid[3]);
     674                 :          0 : MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
     675                 :            :         card->raw_csd[2], card->raw_csd[3]);
     676                 :          0 : MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
     677                 :          0 : MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
     678                 :          0 : MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
     679                 :          0 : MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
     680                 :          0 : MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
     681                 :          0 : MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
     682                 :          0 : MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
     683                 :          0 : MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
     684                 :          0 : MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
     685                 :          0 : MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
     686                 :            : 
     687                 :            : 
     688                 :            : static struct attribute *sd_std_attrs[] = {
     689                 :            :         &dev_attr_cid.attr,
     690                 :            :         &dev_attr_csd.attr,
     691                 :            :         &dev_attr_scr.attr,
     692                 :            :         &dev_attr_date.attr,
     693                 :            :         &dev_attr_erase_size.attr,
     694                 :            :         &dev_attr_preferred_erase_size.attr,
     695                 :            :         &dev_attr_fwrev.attr,
     696                 :            :         &dev_attr_hwrev.attr,
     697                 :            :         &dev_attr_manfid.attr,
     698                 :            :         &dev_attr_name.attr,
     699                 :            :         &dev_attr_oemid.attr,
     700                 :            :         &dev_attr_serial.attr,
     701                 :            :         NULL,
     702                 :            : };
     703                 :            : 
     704                 :            : static struct attribute_group sd_std_attr_group = {
     705                 :            :         .attrs = sd_std_attrs,
     706                 :            : };
     707                 :            : 
     708                 :            : static const struct attribute_group *sd_attr_groups[] = {
     709                 :            :         &sd_std_attr_group,
     710                 :            :         NULL,
     711                 :            : };
     712                 :            : 
     713                 :            : struct device_type sd_type = {
     714                 :            :         .groups = sd_attr_groups,
     715                 :            : };
     716                 :            : 
     717                 :            : /*
     718                 :            :  * Fetch CID from card.
     719                 :            :  */
     720                 :          0 : int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
     721                 :            : {
     722                 :            :         int err;
     723                 :            :         u32 max_current;
     724                 :            :         int retries = 10;
     725                 :            :         u32 pocr = ocr;
     726                 :            : 
     727                 :            : try_again:
     728         [ #  # ]:          0 :         if (!retries) {
     729                 :          0 :                 ocr &= ~SD_OCR_S18R;
     730                 :          0 :                 pr_warning("%s: Skipping voltage switch\n",
     731                 :            :                         mmc_hostname(host));
     732                 :            :         }
     733                 :            : 
     734                 :            :         /*
     735                 :            :          * Since we're changing the OCR value, we seem to
     736                 :            :          * need to tell some cards to go back to the idle
     737                 :            :          * state.  We wait 1ms to give cards time to
     738                 :            :          * respond.
     739                 :            :          */
     740                 :          0 :         mmc_go_idle(host);
     741                 :            : 
     742                 :            :         /*
     743                 :            :          * If SD_SEND_IF_COND indicates an SD 2.0
     744                 :            :          * compliant card and we should set bit 30
     745                 :            :          * of the ocr to indicate that we can handle
     746                 :            :          * block-addressed SDHC cards.
     747                 :            :          */
     748                 :          0 :         err = mmc_send_if_cond(host, ocr);
     749         [ #  # ]:          0 :         if (!err)
     750                 :          0 :                 ocr |= SD_OCR_CCS;
     751                 :            : 
     752                 :            :         /*
     753                 :            :          * If the host supports one of UHS-I modes, request the card
     754                 :            :          * to switch to 1.8V signaling level. If the card has failed
     755                 :            :          * repeatedly to switch however, skip this.
     756                 :            :          */
     757 [ #  # ][ #  # ]:          0 :         if (retries && mmc_host_uhs(host))
     758                 :          0 :                 ocr |= SD_OCR_S18R;
     759                 :            : 
     760                 :            :         /*
     761                 :            :          * If the host can supply more than 150mA at current voltage,
     762                 :            :          * XPC should be set to 1.
     763                 :            :          */
     764                 :          0 :         max_current = sd_get_host_max_current(host);
     765         [ #  # ]:          0 :         if (max_current > 150)
     766                 :          0 :                 ocr |= SD_OCR_XPC;
     767                 :            : 
     768                 :          0 :         err = mmc_send_app_op_cond(host, ocr, rocr);
     769         [ #  # ]:          0 :         if (err)
     770                 :            :                 return err;
     771                 :            : 
     772                 :            :         /*
     773                 :            :          * In case CCS and S18A in the response is set, start Signal Voltage
     774                 :            :          * Switch procedure. SPI mode doesn't support CMD11.
     775                 :            :          */
     776 [ #  # ][ #  # ]:          0 :         if (!mmc_host_is_spi(host) && rocr &&
                 [ #  # ]
     777                 :          0 :            ((*rocr & 0x41000000) == 0x41000000)) {
     778                 :          0 :                 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
     779                 :            :                                         pocr);
     780         [ #  # ]:          0 :                 if (err == -EAGAIN) {
     781                 :          0 :                         retries--;
     782                 :          0 :                         goto try_again;
     783         [ #  # ]:          0 :                 } else if (err) {
     784                 :            :                         retries = 0;
     785                 :            :                         goto try_again;
     786                 :            :                 }
     787                 :            :         }
     788                 :            : 
     789         [ #  # ]:          0 :         if (mmc_host_is_spi(host))
     790                 :          0 :                 err = mmc_send_cid(host, cid);
     791                 :            :         else
     792                 :          0 :                 err = mmc_all_send_cid(host, cid);
     793                 :            : 
     794                 :          0 :         return err;
     795                 :            : }
     796                 :            : 
     797                 :          0 : int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
     798                 :            : {
     799                 :            :         int err;
     800                 :            : 
     801                 :            :         /*
     802                 :            :          * Fetch CSD from card.
     803                 :            :          */
     804                 :          0 :         err = mmc_send_csd(card, card->raw_csd);
     805         [ #  # ]:          0 :         if (err)
     806                 :            :                 return err;
     807                 :            : 
     808                 :          0 :         err = mmc_decode_csd(card);
     809         [ #  # ]:          0 :         if (err)
     810                 :          0 :                 return err;
     811                 :            : 
     812                 :            :         return 0;
     813                 :            : }
     814                 :            : 
     815                 :          0 : int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
     816                 :            :         bool reinit)
     817                 :            : {
     818                 :            :         int err;
     819                 :            : #ifdef CONFIG_MMC_PARANOID_SD_INIT
     820                 :            :         int retries;
     821                 :            : #endif
     822                 :            : 
     823         [ #  # ]:          0 :         if (!reinit) {
     824                 :            :                 /*
     825                 :            :                  * Fetch SCR from card.
     826                 :            :                  */
     827                 :          0 :                 err = mmc_app_send_scr(card, card->raw_scr);
     828         [ #  # ]:          0 :                 if (err)
     829                 :            :                         return err;
     830                 :            : 
     831                 :          0 :                 err = mmc_decode_scr(card);
     832         [ #  # ]:          0 :                 if (err)
     833                 :            :                         return err;
     834                 :            : 
     835                 :            :                 /*
     836                 :            :                  * Fetch and process SD Status register.
     837                 :            :                  */
     838                 :          0 :                 err = mmc_read_ssr(card);
     839         [ #  # ]:          0 :                 if (err)
     840                 :            :                         return err;
     841                 :            : 
     842                 :            :                 /* Erase init depends on CSD and SSR */
     843                 :          0 :                 mmc_init_erase(card);
     844                 :            : 
     845                 :            :                 /*
     846                 :            :                  * Fetch switch information from card.
     847                 :            :                  */
     848                 :            : #ifdef CONFIG_MMC_PARANOID_SD_INIT
     849                 :            :                 for (retries = 1; retries <= 3; retries++) {
     850                 :            :                         err = mmc_read_switch(card);
     851                 :            :                         if (!err) {
     852                 :            :                                 if (retries > 1) {
     853                 :            :                                         printk(KERN_WARNING
     854                 :            :                                                "%s: recovered\n", 
     855                 :            :                                                mmc_hostname(host));
     856                 :            :                                 }
     857                 :            :                                 break;
     858                 :            :                         } else {
     859                 :            :                                 printk(KERN_WARNING
     860                 :            :                                        "%s: read switch failed (attempt %d)\n",
     861                 :            :                                        mmc_hostname(host), retries);
     862                 :            :                         }
     863                 :            :                 }
     864                 :            : #else
     865                 :          0 :                 err = mmc_read_switch(card);
     866                 :            : #endif
     867                 :            : 
     868         [ #  # ]:          0 :                 if (err)
     869                 :            :                         return err;
     870                 :            :         }
     871                 :            : 
     872                 :            :         /*
     873                 :            :          * For SPI, enable CRC as appropriate.
     874                 :            :          * This CRC enable is located AFTER the reading of the
     875                 :            :          * card registers because some SDHC cards are not able
     876                 :            :          * to provide valid CRCs for non-512-byte blocks.
     877                 :            :          */
     878         [ #  # ]:          0 :         if (mmc_host_is_spi(host)) {
     879                 :          0 :                 err = mmc_spi_set_crc(host, use_spi_crc);
     880         [ #  # ]:          0 :                 if (err)
     881                 :            :                         return err;
     882                 :            :         }
     883                 :            : 
     884                 :            :         /*
     885                 :            :          * Check if read-only switch is active.
     886                 :            :          */
     887         [ #  # ]:          0 :         if (!reinit) {
     888                 :            :                 int ro = -1;
     889                 :            : 
     890         [ #  # ]:          0 :                 if (host->ops->get_ro) {
     891                 :            :                         mmc_host_clk_hold(card->host);
     892                 :          0 :                         ro = host->ops->get_ro(host);
     893                 :            :                         mmc_host_clk_release(card->host);
     894                 :            :                 }
     895                 :            : 
     896         [ #  # ]:          0 :                 if (ro < 0) {
     897                 :          0 :                         pr_warning("%s: host does not "
     898                 :            :                                 "support reading read-only "
     899                 :            :                                 "switch. assuming write-enable.\n",
     900                 :            :                                 mmc_hostname(host));
     901         [ #  # ]:          0 :                 } else if (ro > 0) {
     902                 :          0 :                         mmc_card_set_readonly(card);
     903                 :            :                 }
     904                 :            :         }
     905                 :            : 
     906                 :            :         return 0;
     907                 :            : }
     908                 :            : 
     909                 :          0 : unsigned mmc_sd_get_max_clock(struct mmc_card *card)
     910                 :            : {
     911                 :            :         unsigned max_dtr = (unsigned int)-1;
     912                 :            : 
     913 [ #  # ][ #  # ]:          0 :         if (mmc_card_highspeed(card)) {
     914 [ #  # ][ #  # ]:          0 :                 if (max_dtr > card->sw_caps.hs_max_dtr)
     915                 :            :                         max_dtr = card->sw_caps.hs_max_dtr;
     916 [ #  # ][ #  # ]:          0 :         } else if (max_dtr > card->csd.max_dtr) {
     917                 :            :                 max_dtr = card->csd.max_dtr;
     918                 :            :         }
     919                 :            : 
     920                 :          0 :         return max_dtr;
     921                 :            : }
     922                 :            : 
     923                 :          0 : void mmc_sd_go_highspeed(struct mmc_card *card)
     924                 :            : {
     925                 :          0 :         mmc_card_set_highspeed(card);
     926                 :          0 :         mmc_set_timing(card->host, MMC_TIMING_SD_HS);
     927                 :          0 : }
     928                 :            : 
     929                 :            : /*
     930                 :            :  * Handle the detection and initialisation of a card.
     931                 :            :  *
     932                 :            :  * In the case of a resume, "oldcard" will contain the card
     933                 :            :  * we're trying to reinitialise.
     934                 :            :  */
     935                 :          0 : static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
     936                 :            :         struct mmc_card *oldcard)
     937                 :            : {
     938                 :            :         struct mmc_card *card;
     939                 :            :         int err;
     940                 :            :         u32 cid[4];
     941                 :          0 :         u32 rocr = 0;
     942                 :            : 
     943         [ #  # ]:          0 :         BUG_ON(!host);
     944         [ #  # ]:          0 :         WARN_ON(!host->claimed);
     945                 :            : 
     946                 :          0 :         err = mmc_sd_get_cid(host, ocr, cid, &rocr);
     947         [ #  # ]:          0 :         if (err)
     948                 :            :                 return err;
     949                 :            : 
     950         [ #  # ]:          0 :         if (oldcard) {
     951         [ #  # ]:          0 :                 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
     952                 :            :                         return -ENOENT;
     953                 :            : 
     954                 :            :                 card = oldcard;
     955                 :            :         } else {
     956                 :            :                 /*
     957                 :            :                  * Allocate card structure.
     958                 :            :                  */
     959                 :          0 :                 card = mmc_alloc_card(host, &sd_type);
     960         [ #  # ]:          0 :                 if (IS_ERR(card))
     961                 :          0 :                         return PTR_ERR(card);
     962                 :            : 
     963                 :          0 :                 card->ocr = ocr;
     964                 :          0 :                 card->type = MMC_TYPE_SD;
     965                 :          0 :                 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
     966                 :            :         }
     967                 :            : 
     968                 :            :         /*
     969                 :            :          * For native busses:  get card RCA and quit open drain mode.
     970                 :            :          */
     971         [ #  # ]:          0 :         if (!mmc_host_is_spi(host)) {
     972                 :          0 :                 err = mmc_send_relative_addr(host, &card->rca);
     973         [ #  # ]:          0 :                 if (err)
     974                 :            :                         goto free_card;
     975                 :            :         }
     976                 :            : 
     977         [ #  # ]:          0 :         if (!oldcard) {
     978                 :          0 :                 err = mmc_sd_get_csd(host, card);
     979         [ #  # ]:          0 :                 if (err)
     980                 :            :                         goto free_card;
     981                 :            : 
     982                 :          0 :                 mmc_decode_cid(card);
     983                 :            :         }
     984                 :            : 
     985                 :            :         /*
     986                 :            :          * Select card, as all following commands rely on that.
     987                 :            :          */
     988         [ #  # ]:          0 :         if (!mmc_host_is_spi(host)) {
     989                 :          0 :                 err = mmc_select_card(card);
     990         [ #  # ]:          0 :                 if (err)
     991                 :            :                         goto free_card;
     992                 :            :         }
     993                 :            : 
     994                 :          0 :         err = mmc_sd_setup_card(host, card, oldcard != NULL);
     995         [ #  # ]:          0 :         if (err)
     996                 :            :                 goto free_card;
     997                 :            : 
     998                 :            :         /* Initialization sequence for UHS-I cards */
     999         [ #  # ]:          0 :         if (rocr & SD_ROCR_S18A) {
    1000                 :          0 :                 err = mmc_sd_init_uhs_card(card);
    1001         [ #  # ]:          0 :                 if (err)
    1002                 :            :                         goto free_card;
    1003                 :            : 
    1004                 :            :                 /* Card is an ultra-high-speed card */
    1005                 :          0 :                 mmc_card_set_uhs(card);
    1006                 :            :         } else {
    1007                 :            :                 /*
    1008                 :            :                  * Attempt to change to high-speed (if supported)
    1009                 :            :                  */
    1010                 :          0 :                 err = mmc_sd_switch_hs(card);
    1011         [ #  # ]:          0 :                 if (err > 0)
    1012                 :            :                         mmc_sd_go_highspeed(card);
    1013         [ #  # ]:          0 :                 else if (err)
    1014                 :            :                         goto free_card;
    1015                 :            : 
    1016                 :            :                 /*
    1017                 :            :                  * Set bus speed.
    1018                 :            :                  */
    1019                 :          0 :                 mmc_set_clock(host, mmc_sd_get_max_clock(card));
    1020                 :            : 
    1021                 :            :                 /*
    1022                 :            :                  * Switch to wider bus (if supported).
    1023                 :            :                  */
    1024 [ #  # ][ #  # ]:          0 :                 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
    1025                 :          0 :                         (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
    1026                 :          0 :                         err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
    1027         [ #  # ]:          0 :                         if (err)
    1028                 :            :                                 goto free_card;
    1029                 :            : 
    1030                 :          0 :                         mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
    1031                 :            :                 }
    1032                 :            :         }
    1033                 :            : 
    1034                 :          0 :         host->card = card;
    1035                 :          0 :         return 0;
    1036                 :            : 
    1037                 :            : free_card:
    1038         [ #  # ]:          0 :         if (!oldcard)
    1039                 :          0 :                 mmc_remove_card(card);
    1040                 :            : 
    1041                 :          0 :         return err;
    1042                 :            : }
    1043                 :            : 
    1044                 :            : /*
    1045                 :            :  * Host is being removed. Free up the current card.
    1046                 :            :  */
    1047                 :          0 : static void mmc_sd_remove(struct mmc_host *host)
    1048                 :            : {
    1049         [ #  # ]:          0 :         BUG_ON(!host);
    1050         [ #  # ]:          0 :         BUG_ON(!host->card);
    1051                 :            : 
    1052                 :          0 :         mmc_remove_card(host->card);
    1053                 :          0 :         host->card = NULL;
    1054                 :          0 : }
    1055                 :            : 
    1056                 :            : /*
    1057                 :            :  * Card detection - card is alive.
    1058                 :            :  */
    1059                 :          0 : static int mmc_sd_alive(struct mmc_host *host)
    1060                 :            : {
    1061                 :          0 :         return mmc_send_status(host->card, NULL);
    1062                 :            : }
    1063                 :            : 
    1064                 :            : /*
    1065                 :            :  * Card detection callback from host.
    1066                 :            :  */
    1067                 :          0 : static void mmc_sd_detect(struct mmc_host *host)
    1068                 :            : {
    1069                 :            :         int err = 0;
    1070                 :            : #ifdef CONFIG_MMC_PARANOID_SD_INIT
    1071                 :            :         int retries = 5;
    1072                 :            : #endif
    1073                 :            : 
    1074         [ #  # ]:          0 :         BUG_ON(!host);
    1075         [ #  # ]:          0 :         BUG_ON(!host->card);
    1076                 :            : 
    1077                 :          0 :         mmc_get_card(host->card);
    1078                 :            : 
    1079                 :            :         /*
    1080                 :            :          * Just check if our card has been removed.
    1081                 :            :          */
    1082                 :            : #ifdef CONFIG_MMC_PARANOID_SD_INIT
    1083                 :            :         while(retries) {
    1084                 :            :                 err = mmc_send_status(host->card, NULL);
    1085                 :            :                 if (err) {
    1086                 :            :                         retries--;
    1087                 :            :                         udelay(5);
    1088                 :            :                         continue;
    1089                 :            :                 }
    1090                 :            :                 break;
    1091                 :            :         }
    1092                 :            :         if (!retries) {
    1093                 :            :                 printk(KERN_ERR "%s(%s): Unable to re-detect card (%d)\n",
    1094                 :            :                        __func__, mmc_hostname(host), err);
    1095                 :            :         }
    1096                 :            : #else
    1097                 :          0 :         err = _mmc_detect_card_removed(host);
    1098                 :            : #endif
    1099                 :            : 
    1100                 :          0 :         mmc_put_card(host->card);
    1101                 :            : 
    1102         [ #  # ]:          0 :         if (err) {
    1103                 :          0 :                 mmc_sd_remove(host);
    1104                 :            : 
    1105                 :            :                 mmc_claim_host(host);
    1106                 :          0 :                 mmc_detach_bus(host);
    1107                 :          0 :                 mmc_power_off(host);
    1108                 :          0 :                 mmc_release_host(host);
    1109                 :            :         }
    1110                 :          0 : }
    1111                 :            : 
    1112                 :          0 : static int _mmc_sd_suspend(struct mmc_host *host)
    1113                 :            : {
    1114                 :            :         int err = 0;
    1115                 :            : 
    1116         [ #  # ]:          0 :         BUG_ON(!host);
    1117         [ #  # ]:          0 :         BUG_ON(!host->card);
    1118                 :            : 
    1119                 :            :         mmc_claim_host(host);
    1120                 :            : 
    1121         [ #  # ]:          0 :         if (mmc_card_suspended(host->card))
    1122                 :            :                 goto out;
    1123                 :            : 
    1124         [ #  # ]:          0 :         if (!mmc_host_is_spi(host))
    1125                 :          0 :                 err = mmc_deselect_cards(host);
    1126                 :          0 :         host->card->state &= ~MMC_STATE_HIGHSPEED;
    1127         [ #  # ]:          0 :         if (!err) {
    1128                 :          0 :                 mmc_power_off(host);
    1129                 :          0 :                 mmc_card_set_suspended(host->card);
    1130                 :            :         }
    1131                 :            : 
    1132                 :            : out:
    1133                 :          0 :         mmc_release_host(host);
    1134                 :          0 :         return err;
    1135                 :            : }
    1136                 :            : 
    1137                 :            : /*
    1138                 :            :  * Callback for suspend
    1139                 :            :  */
    1140                 :          0 : static int mmc_sd_suspend(struct mmc_host *host)
    1141                 :            : {
    1142                 :            :         int err;
    1143                 :            : 
    1144                 :          0 :         err = _mmc_sd_suspend(host);
    1145                 :            :         if (!err) {
    1146                 :            :                 pm_runtime_disable(&host->card->dev);
    1147                 :            :                 pm_runtime_set_suspended(&host->card->dev);
    1148                 :            :         }
    1149                 :            : 
    1150                 :          0 :         return err;
    1151                 :            : }
    1152                 :            : 
    1153                 :            : /*
    1154                 :            :  * This function tries to determine if the same card is still present
    1155                 :            :  * and, if so, restore all state to it.
    1156                 :            :  */
    1157                 :          0 : static int _mmc_sd_resume(struct mmc_host *host)
    1158                 :            : {
    1159                 :            :         int err = 0;
    1160                 :            : #ifdef CONFIG_MMC_PARANOID_SD_INIT
    1161                 :            :         int retries;
    1162                 :            : #endif
    1163                 :            : 
    1164         [ #  # ]:          0 :         BUG_ON(!host);
    1165         [ #  # ]:          0 :         BUG_ON(!host->card);
    1166                 :            : 
    1167                 :            :         mmc_claim_host(host);
    1168                 :            : 
    1169         [ #  # ]:          0 :         if (!mmc_card_suspended(host->card))
    1170                 :            :                 goto out;
    1171                 :            : 
    1172                 :          0 :         mmc_power_up(host, host->card->ocr);
    1173                 :            : #ifdef CONFIG_MMC_PARANOID_SD_INIT
    1174                 :            :         retries = 5;
    1175                 :            :         while (retries) {
    1176                 :            :                 err = mmc_sd_init_card(host, host->card->ocr, host->card);
    1177                 :            : 
    1178                 :            :                 if (err) {
    1179                 :            :                         printk(KERN_ERR "%s: Re-init card rc = %d (retries = %d)\n",
    1180                 :            :                                mmc_hostname(host), err, retries);
    1181                 :            :                         mdelay(5);
    1182                 :            :                         retries--;
    1183                 :            :                         continue;
    1184                 :            :                 }
    1185                 :            :                 break;
    1186                 :            :         }
    1187                 :            : #else
    1188                 :          0 :         err = mmc_sd_init_card(host, host->card->ocr, host->card);
    1189                 :            : #endif
    1190                 :          0 :         mmc_card_clr_suspended(host->card);
    1191                 :            : 
    1192                 :            : out:
    1193                 :          0 :         mmc_release_host(host);
    1194                 :          0 :         return err;
    1195                 :            : }
    1196                 :            : 
    1197                 :            : /*
    1198                 :            :  * Callback for resume
    1199                 :            :  */
    1200                 :          0 : static int mmc_sd_resume(struct mmc_host *host)
    1201                 :            : {
    1202                 :            :         int err = 0;
    1203                 :            : 
    1204         [ #  # ]:          0 :         if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
    1205                 :          0 :                 err = _mmc_sd_resume(host);
    1206                 :            :                 pm_runtime_set_active(&host->card->dev);
    1207                 :            :                 pm_runtime_mark_last_busy(&host->card->dev);
    1208                 :            :         }
    1209                 :            :         pm_runtime_enable(&host->card->dev);
    1210                 :            : 
    1211                 :          0 :         return err;
    1212                 :            : }
    1213                 :            : 
    1214                 :            : /*
    1215                 :            :  * Callback for runtime_suspend.
    1216                 :            :  */
    1217                 :          0 : static int mmc_sd_runtime_suspend(struct mmc_host *host)
    1218                 :            : {
    1219                 :            :         int err;
    1220                 :            : 
    1221         [ #  # ]:          0 :         if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
    1222                 :            :                 return 0;
    1223                 :            : 
    1224                 :          0 :         err = _mmc_sd_suspend(host);
    1225         [ #  # ]:          0 :         if (err)
    1226                 :          0 :                 pr_err("%s: error %d doing aggessive suspend\n",
    1227                 :            :                         mmc_hostname(host), err);
    1228                 :            : 
    1229                 :          0 :         return err;
    1230                 :            : }
    1231                 :            : 
    1232                 :            : /*
    1233                 :            :  * Callback for runtime_resume.
    1234                 :            :  */
    1235                 :          0 : static int mmc_sd_runtime_resume(struct mmc_host *host)
    1236                 :            : {
    1237                 :            :         int err;
    1238                 :            : 
    1239         [ #  # ]:          0 :         if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
    1240                 :            :                 return 0;
    1241                 :            : 
    1242                 :          0 :         err = _mmc_sd_resume(host);
    1243         [ #  # ]:          0 :         if (err)
    1244                 :          0 :                 pr_err("%s: error %d doing aggessive resume\n",
    1245                 :            :                         mmc_hostname(host), err);
    1246                 :            : 
    1247                 :            :         return 0;
    1248                 :            : }
    1249                 :            : 
    1250                 :          0 : static int mmc_sd_power_restore(struct mmc_host *host)
    1251                 :            : {
    1252                 :            :         int ret;
    1253                 :            : 
    1254                 :          0 :         host->card->state &= ~MMC_STATE_HIGHSPEED;
    1255                 :            :         mmc_claim_host(host);
    1256                 :          0 :         ret = mmc_sd_init_card(host, host->card->ocr, host->card);
    1257                 :          0 :         mmc_release_host(host);
    1258                 :            : 
    1259                 :          0 :         return ret;
    1260                 :            : }
    1261                 :            : 
    1262                 :            : static const struct mmc_bus_ops mmc_sd_ops = {
    1263                 :            :         .remove = mmc_sd_remove,
    1264                 :            :         .detect = mmc_sd_detect,
    1265                 :            :         .suspend = NULL,
    1266                 :            :         .resume = NULL,
    1267                 :            :         .power_restore = mmc_sd_power_restore,
    1268                 :            :         .alive = mmc_sd_alive,
    1269                 :            :         .shutdown = mmc_sd_suspend,
    1270                 :            : };
    1271                 :            : 
    1272                 :            : static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
    1273                 :            :         .remove = mmc_sd_remove,
    1274                 :            :         .detect = mmc_sd_detect,
    1275                 :            :         .runtime_suspend = mmc_sd_runtime_suspend,
    1276                 :            :         .runtime_resume = mmc_sd_runtime_resume,
    1277                 :            :         .suspend = mmc_sd_suspend,
    1278                 :            :         .resume = mmc_sd_resume,
    1279                 :            :         .power_restore = mmc_sd_power_restore,
    1280                 :            :         .alive = mmc_sd_alive,
    1281                 :            :         .shutdown = mmc_sd_suspend,
    1282                 :            : };
    1283                 :            : 
    1284                 :          0 : static void mmc_sd_attach_bus_ops(struct mmc_host *host)
    1285                 :            : {
    1286                 :            :         const struct mmc_bus_ops *bus_ops;
    1287                 :            : 
    1288         [ #  # ]:          0 :         if (!mmc_card_is_removable(host))
    1289                 :            :                 bus_ops = &mmc_sd_ops_unsafe;
    1290                 :            :         else
    1291                 :            :                 bus_ops = &mmc_sd_ops;
    1292                 :          0 :         mmc_attach_bus(host, bus_ops);
    1293                 :          0 : }
    1294                 :            : 
    1295                 :            : /*
    1296                 :            :  * Starting point for SD card init.
    1297                 :            :  */
    1298                 :          0 : int mmc_attach_sd(struct mmc_host *host)
    1299                 :            : {
    1300                 :            :         int err;
    1301                 :            :         u32 ocr, rocr;
    1302                 :            : #ifdef CONFIG_MMC_PARANOID_SD_INIT
    1303                 :            :         int retries;
    1304                 :            : #endif
    1305                 :            : 
    1306         [ #  # ]:          0 :         BUG_ON(!host);
    1307         [ #  # ]:          0 :         WARN_ON(!host->claimed);
    1308                 :            : 
    1309                 :          0 :         err = mmc_send_app_op_cond(host, 0, &ocr);
    1310         [ #  # ]:          0 :         if (err)
    1311                 :            :                 return err;
    1312                 :            : 
    1313                 :          0 :         mmc_sd_attach_bus_ops(host);
    1314         [ #  # ]:          0 :         if (host->ocr_avail_sd)
    1315                 :          0 :                 host->ocr_avail = host->ocr_avail_sd;
    1316                 :            : 
    1317                 :            :         /*
    1318                 :            :          * We need to get OCR a different way for SPI.
    1319                 :            :          */
    1320         [ #  # ]:          0 :         if (mmc_host_is_spi(host)) {
    1321                 :          0 :                 mmc_go_idle(host);
    1322                 :            : 
    1323                 :          0 :                 err = mmc_spi_read_ocr(host, 0, &ocr);
    1324         [ #  # ]:          0 :                 if (err)
    1325                 :            :                         goto err;
    1326                 :            :         }
    1327                 :            : 
    1328                 :          0 :         rocr = mmc_select_voltage(host, ocr);
    1329                 :            : 
    1330                 :            :         /*
    1331                 :            :          * Can we support the voltage(s) of the card(s)?
    1332                 :            :          */
    1333         [ #  # ]:          0 :         if (!rocr) {
    1334                 :            :                 err = -EINVAL;
    1335                 :            :                 goto err;
    1336                 :            :         }
    1337                 :            : 
    1338                 :            :         /*
    1339                 :            :          * Detect and init the card.
    1340                 :            :          */
    1341                 :            : #ifdef CONFIG_MMC_PARANOID_SD_INIT
    1342                 :            :         retries = 5;
    1343                 :            :         while (retries) {
    1344                 :            :                 err = mmc_sd_init_card(host, rocr, NULL);
    1345                 :            :                 if (err) {
    1346                 :            :                         retries--;
    1347                 :            :                         continue;
    1348                 :            :                 }
    1349                 :            :                 break;
    1350                 :            :         }
    1351                 :            : 
    1352                 :            :         if (!retries) {
    1353                 :            :                 printk(KERN_ERR "%s: mmc_sd_init_card() failure (err = %d)\n",
    1354                 :            :                        mmc_hostname(host), err);
    1355                 :            :                 goto err;
    1356                 :            :         }
    1357                 :            : #else
    1358                 :          0 :         err = mmc_sd_init_card(host, rocr, NULL);
    1359         [ #  # ]:          0 :         if (err)
    1360                 :            :                 goto err;
    1361                 :            : #endif
    1362                 :            : 
    1363                 :          0 :         mmc_release_host(host);
    1364                 :          0 :         err = mmc_add_card(host->card);
    1365                 :            :         mmc_claim_host(host);
    1366         [ #  # ]:          0 :         if (err)
    1367                 :            :                 goto remove_card;
    1368                 :            : 
    1369                 :            :         return 0;
    1370                 :            : 
    1371                 :            : remove_card:
    1372                 :          0 :         mmc_release_host(host);
    1373                 :          0 :         mmc_remove_card(host->card);
    1374                 :          0 :         host->card = NULL;
    1375                 :            :         mmc_claim_host(host);
    1376                 :            : err:
    1377                 :          0 :         mmc_detach_bus(host);
    1378                 :            : 
    1379                 :          0 :         pr_err("%s: error %d whilst initialising SD card\n",
    1380                 :            :                 mmc_hostname(host), err);
    1381                 :            : 
    1382                 :          0 :         return err;
    1383                 :            : }
    1384                 :            : 

Generated by: LCOV version 1.9