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

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *  linux/drivers/mmc/sdio.c
       3                 :            :  *
       4                 :            :  *  Copyright 2006-2007 Pierre Ossman
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU General Public License as published by
       8                 :            :  * the Free Software Foundation; either version 2 of the License, or (at
       9                 :            :  * your option) any later version.
      10                 :            :  */
      11                 :            : 
      12                 :            : #include <linux/err.h>
      13                 :            : #include <linux/module.h>
      14                 :            : #include <linux/pm_runtime.h>
      15                 :            : 
      16                 :            : #include <linux/mmc/host.h>
      17                 :            : #include <linux/mmc/card.h>
      18                 :            : #include <linux/mmc/mmc.h>
      19                 :            : #include <linux/mmc/sdio.h>
      20                 :            : #include <linux/mmc/sdio_func.h>
      21                 :            : #include <linux/mmc/sdio_ids.h>
      22                 :            : 
      23                 :            : #include "core.h"
      24                 :            : #include "bus.h"
      25                 :            : #include "sd.h"
      26                 :            : #include "sdio_bus.h"
      27                 :            : #include "mmc_ops.h"
      28                 :            : #include "sd_ops.h"
      29                 :            : #include "sdio_ops.h"
      30                 :            : #include "sdio_cis.h"
      31                 :            : 
      32                 :            : #ifdef CONFIG_MMC_EMBEDDED_SDIO
      33                 :            : #include <linux/mmc/sdio_ids.h>
      34                 :            : #endif
      35                 :            : 
      36                 :          0 : static int sdio_read_fbr(struct sdio_func *func)
      37                 :            : {
      38                 :            :         int ret;
      39                 :            :         unsigned char data;
      40                 :            : 
      41         [ #  # ]:          0 :         if (mmc_card_nonstd_func_interface(func->card)) {
      42                 :          0 :                 func->class = SDIO_CLASS_NONE;
      43                 :          0 :                 return 0;
      44                 :            :         }
      45                 :            : 
      46                 :          0 :         ret = mmc_io_rw_direct(func->card, 0, 0,
      47                 :          0 :                 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
      48         [ #  # ]:          0 :         if (ret)
      49                 :            :                 goto out;
      50                 :            : 
      51                 :          0 :         data &= 0x0f;
      52                 :            : 
      53         [ #  # ]:          0 :         if (data == 0x0f) {
      54                 :          0 :                 ret = mmc_io_rw_direct(func->card, 0, 0,
      55                 :          0 :                         SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
      56         [ #  # ]:          0 :                 if (ret)
      57                 :            :                         goto out;
      58                 :            :         }
      59                 :            : 
      60                 :          0 :         func->class = data;
      61                 :            : 
      62                 :            : out:
      63                 :          0 :         return ret;
      64                 :            : }
      65                 :            : 
      66                 :          0 : static int sdio_init_func(struct mmc_card *card, unsigned int fn)
      67                 :            : {
      68                 :            :         int ret;
      69                 :            :         struct sdio_func *func;
      70                 :            : 
      71         [ #  # ]:          0 :         BUG_ON(fn > SDIO_MAX_FUNCS);
      72                 :            : 
      73                 :          0 :         func = sdio_alloc_func(card);
      74         [ #  # ]:          0 :         if (IS_ERR(func))
      75                 :          0 :                 return PTR_ERR(func);
      76                 :            : 
      77                 :          0 :         func->num = fn;
      78                 :            : 
      79         [ #  # ]:          0 :         if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
      80                 :          0 :                 ret = sdio_read_fbr(func);
      81         [ #  # ]:          0 :                 if (ret)
      82                 :            :                         goto fail;
      83                 :            : 
      84                 :          0 :                 ret = sdio_read_func_cis(func);
      85         [ #  # ]:          0 :                 if (ret)
      86                 :            :                         goto fail;
      87                 :            :         } else {
      88                 :          0 :                 func->vendor = func->card->cis.vendor;
      89                 :          0 :                 func->device = func->card->cis.device;
      90                 :          0 :                 func->max_blksize = func->card->cis.blksize;
      91                 :            :         }
      92                 :            : 
      93                 :          0 :         card->sdio_func[fn - 1] = func;
      94                 :            : 
      95                 :          0 :         return 0;
      96                 :            : 
      97                 :            : fail:
      98                 :            :         /*
      99                 :            :          * It is okay to remove the function here even though we hold
     100                 :            :          * the host lock as we haven't registered the device yet.
     101                 :            :          */
     102                 :          0 :         sdio_remove_func(func);
     103                 :          0 :         return ret;
     104                 :            : }
     105                 :            : 
     106                 :          0 : static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
     107                 :            : {
     108                 :            :         int ret;
     109                 :            :         int cccr_vsn;
     110                 :          0 :         int uhs = ocr & R4_18V_PRESENT;
     111                 :            :         unsigned char data;
     112                 :            :         unsigned char speed;
     113                 :            : 
     114                 :          0 :         memset(&card->cccr, 0, sizeof(struct sdio_cccr));
     115                 :            : 
     116                 :          0 :         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
     117         [ #  # ]:          0 :         if (ret)
     118                 :            :                 goto out;
     119                 :            : 
     120                 :          0 :         cccr_vsn = data & 0x0f;
     121                 :            : 
     122         [ #  # ]:          0 :         if (cccr_vsn > SDIO_CCCR_REV_3_00) {
     123                 :          0 :                 pr_err("%s: unrecognised CCCR structure version %d\n",
     124                 :            :                         mmc_hostname(card->host), cccr_vsn);
     125                 :          0 :                 return -EINVAL;
     126                 :            :         }
     127                 :            : 
     128                 :          0 :         card->cccr.sdio_vsn = (data & 0xf0) >> 4;
     129                 :            : 
     130                 :          0 :         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
     131         [ #  # ]:          0 :         if (ret)
     132                 :            :                 goto out;
     133                 :            : 
     134         [ #  # ]:          0 :         if (data & SDIO_CCCR_CAP_SMB)
     135                 :          0 :                 card->cccr.multi_block = 1;
     136         [ #  # ]:          0 :         if (data & SDIO_CCCR_CAP_LSC)
     137                 :          0 :                 card->cccr.low_speed = 1;
     138         [ #  # ]:          0 :         if (data & SDIO_CCCR_CAP_4BLS)
     139                 :          0 :                 card->cccr.wide_bus = 1;
     140                 :            : 
     141         [ #  # ]:          0 :         if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
     142                 :          0 :                 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
     143         [ #  # ]:          0 :                 if (ret)
     144                 :            :                         goto out;
     145                 :            : 
     146         [ #  # ]:          0 :                 if (data & SDIO_POWER_SMPC)
     147                 :          0 :                         card->cccr.high_power = 1;
     148                 :            :         }
     149                 :            : 
     150         [ #  # ]:          0 :         if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
     151                 :          0 :                 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
     152         [ #  # ]:          0 :                 if (ret)
     153                 :            :                         goto out;
     154                 :            : 
     155                 :          0 :                 card->scr.sda_spec3 = 0;
     156                 :          0 :                 card->sw_caps.sd3_bus_mode = 0;
     157                 :          0 :                 card->sw_caps.sd3_drv_type = 0;
     158         [ #  # ]:          0 :                 if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
     159                 :          0 :                         card->scr.sda_spec3 = 1;
     160                 :          0 :                         ret = mmc_io_rw_direct(card, 0, 0,
     161                 :            :                                 SDIO_CCCR_UHS, 0, &data);
     162         [ #  # ]:          0 :                         if (ret)
     163                 :            :                                 goto out;
     164                 :            : 
     165         [ #  # ]:          0 :                         if (mmc_host_uhs(card->host)) {
     166         [ #  # ]:          0 :                                 if (data & SDIO_UHS_DDR50)
     167                 :            :                                         card->sw_caps.sd3_bus_mode
     168                 :          0 :                                                 |= SD_MODE_UHS_DDR50;
     169                 :            : 
     170         [ #  # ]:          0 :                                 if (data & SDIO_UHS_SDR50)
     171                 :            :                                         card->sw_caps.sd3_bus_mode
     172                 :          0 :                                                 |= SD_MODE_UHS_SDR50;
     173                 :            : 
     174         [ #  # ]:          0 :                                 if (data & SDIO_UHS_SDR104)
     175                 :            :                                         card->sw_caps.sd3_bus_mode
     176                 :          0 :                                                 |= SD_MODE_UHS_SDR104;
     177                 :            :                         }
     178                 :            : 
     179                 :          0 :                         ret = mmc_io_rw_direct(card, 0, 0,
     180                 :            :                                 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
     181         [ #  # ]:          0 :                         if (ret)
     182                 :            :                                 goto out;
     183                 :            : 
     184         [ #  # ]:          0 :                         if (data & SDIO_DRIVE_SDTA)
     185                 :          0 :                                 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
     186         [ #  # ]:          0 :                         if (data & SDIO_DRIVE_SDTC)
     187                 :          0 :                                 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
     188         [ #  # ]:          0 :                         if (data & SDIO_DRIVE_SDTD)
     189                 :          0 :                                 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
     190                 :            :                 }
     191                 :            : 
     192                 :            :                 /* if no uhs mode ensure we check for high speed */
     193         [ #  # ]:          0 :                 if (!card->sw_caps.sd3_bus_mode) {
     194         [ #  # ]:          0 :                         if (speed & SDIO_SPEED_SHS) {
     195                 :          0 :                                 card->cccr.high_speed = 1;
     196                 :          0 :                                 card->sw_caps.hs_max_dtr = 50000000;
     197                 :            :                         } else {
     198                 :          0 :                                 card->cccr.high_speed = 0;
     199                 :          0 :                                 card->sw_caps.hs_max_dtr = 25000000;
     200                 :            :                         }
     201                 :            :                 }
     202                 :            :         }
     203                 :            : 
     204                 :            : out:
     205                 :          0 :         return ret;
     206                 :            : }
     207                 :            : 
     208                 :          0 : static int sdio_enable_wide(struct mmc_card *card)
     209                 :            : {
     210                 :            :         int ret;
     211                 :            :         u8 ctrl;
     212                 :            : 
     213         [ #  # ]:          0 :         if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
     214                 :            :                 return 0;
     215                 :            : 
     216         [ #  # ]:          0 :         if (card->cccr.low_speed && !card->cccr.wide_bus)
     217                 :            :                 return 0;
     218                 :            : 
     219                 :          0 :         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
     220         [ #  # ]:          0 :         if (ret)
     221                 :            :                 return ret;
     222                 :            : 
     223         [ #  # ]:          0 :         if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
     224                 :          0 :                 pr_warning("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
     225                 :            :                            mmc_hostname(card->host), ctrl);
     226                 :            : 
     227                 :            :         /* set as 4-bit bus width */
     228                 :          0 :         ctrl &= ~SDIO_BUS_WIDTH_MASK;
     229                 :          0 :         ctrl |= SDIO_BUS_WIDTH_4BIT;
     230                 :            : 
     231                 :          0 :         ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
     232         [ #  # ]:          0 :         if (ret)
     233                 :          0 :                 return ret;
     234                 :            : 
     235                 :            :         return 1;
     236                 :            : }
     237                 :            : 
     238                 :            : /*
     239                 :            :  * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
     240                 :            :  * of the card. This may be required on certain setups of boards,
     241                 :            :  * controllers and embedded sdio device which do not need the card's
     242                 :            :  * pull-up. As a result, card detection is disabled and power is saved.
     243                 :            :  */
     244                 :          0 : static int sdio_disable_cd(struct mmc_card *card)
     245                 :            : {
     246                 :            :         int ret;
     247                 :            :         u8 ctrl;
     248                 :            : 
     249         [ #  # ]:          0 :         if (!mmc_card_disable_cd(card))
     250                 :            :                 return 0;
     251                 :            : 
     252                 :          0 :         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
     253         [ #  # ]:          0 :         if (ret)
     254                 :            :                 return ret;
     255                 :            : 
     256                 :          0 :         ctrl |= SDIO_BUS_CD_DISABLE;
     257                 :            : 
     258                 :          0 :         return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
     259                 :            : }
     260                 :            : 
     261                 :            : /*
     262                 :            :  * Devices that remain active during a system suspend are
     263                 :            :  * put back into 1-bit mode.
     264                 :            :  */
     265                 :          0 : static int sdio_disable_wide(struct mmc_card *card)
     266                 :            : {
     267                 :            :         int ret;
     268                 :            :         u8 ctrl;
     269                 :            : 
     270         [ #  # ]:          0 :         if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
     271                 :            :                 return 0;
     272                 :            : 
     273         [ #  # ]:          0 :         if (card->cccr.low_speed && !card->cccr.wide_bus)
     274                 :            :                 return 0;
     275                 :            : 
     276                 :          0 :         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
     277         [ #  # ]:          0 :         if (ret)
     278                 :            :                 return ret;
     279                 :            : 
     280         [ #  # ]:          0 :         if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
     281                 :            :                 return 0;
     282                 :            : 
     283                 :          0 :         ctrl &= ~SDIO_BUS_WIDTH_4BIT;
     284                 :          0 :         ctrl |= SDIO_BUS_ASYNC_INT;
     285                 :            : 
     286                 :          0 :         ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
     287         [ #  # ]:          0 :         if (ret)
     288                 :            :                 return ret;
     289                 :            : 
     290                 :          0 :         mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
     291                 :            : 
     292                 :          0 :         return 0;
     293                 :            : }
     294                 :            : 
     295                 :            : 
     296                 :          0 : static int sdio_enable_4bit_bus(struct mmc_card *card)
     297                 :            : {
     298                 :            :         int err;
     299                 :            : 
     300         [ #  # ]:          0 :         if (card->type == MMC_TYPE_SDIO)
     301                 :          0 :                 return sdio_enable_wide(card);
     302                 :            : 
     303 [ #  # ][ #  # ]:          0 :         if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
     304                 :          0 :                 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
     305                 :          0 :                 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
     306         [ #  # ]:          0 :                 if (err)
     307                 :            :                         return err;
     308                 :            :         } else
     309                 :            :                 return 0;
     310                 :            : 
     311                 :          0 :         err = sdio_enable_wide(card);
     312         [ #  # ]:          0 :         if (err <= 0)
     313                 :          0 :                 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
     314                 :            : 
     315                 :          0 :         return err;
     316                 :            : }
     317                 :            : 
     318                 :            : 
     319                 :            : /*
     320                 :            :  * Test if the card supports high-speed mode and, if so, switch to it.
     321                 :            :  */
     322                 :          0 : static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
     323                 :            : {
     324                 :            :         int ret;
     325                 :            :         u8 speed;
     326                 :            : 
     327         [ #  # ]:          0 :         if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
     328                 :            :                 return 0;
     329                 :            : 
     330         [ #  # ]:          0 :         if (!card->cccr.high_speed)
     331                 :            :                 return 0;
     332                 :            : 
     333                 :          0 :         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
     334         [ #  # ]:          0 :         if (ret)
     335                 :            :                 return ret;
     336                 :            : 
     337         [ #  # ]:          0 :         if (enable)
     338                 :          0 :                 speed |= SDIO_SPEED_EHS;
     339                 :            :         else
     340                 :          0 :                 speed &= ~SDIO_SPEED_EHS;
     341                 :            : 
     342                 :          0 :         ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
     343         [ #  # ]:          0 :         if (ret)
     344                 :          0 :                 return ret;
     345                 :            : 
     346                 :            :         return 1;
     347                 :            : }
     348                 :            : 
     349                 :            : /*
     350                 :            :  * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
     351                 :            :  */
     352                 :          0 : static int sdio_enable_hs(struct mmc_card *card)
     353                 :            : {
     354                 :            :         int ret;
     355                 :            : 
     356                 :          0 :         ret = mmc_sdio_switch_hs(card, true);
     357 [ #  # ][ #  # ]:          0 :         if (ret <= 0 || card->type == MMC_TYPE_SDIO)
     358                 :            :                 return ret;
     359                 :            : 
     360                 :          0 :         ret = mmc_sd_switch_hs(card);
     361         [ #  # ]:          0 :         if (ret <= 0)
     362                 :          0 :                 mmc_sdio_switch_hs(card, false);
     363                 :            : 
     364                 :          0 :         return ret;
     365                 :            : }
     366                 :            : 
     367                 :          0 : static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
     368                 :            : {
     369                 :            :         unsigned max_dtr;
     370                 :            : 
     371         [ #  # ]:          0 :         if (mmc_card_highspeed(card)) {
     372                 :            :                 /*
     373                 :            :                  * The SDIO specification doesn't mention how
     374                 :            :                  * the CIS transfer speed register relates to
     375                 :            :                  * high-speed, but it seems that 50 MHz is
     376                 :            :                  * mandatory.
     377                 :            :                  */
     378                 :            :                 max_dtr = 50000000;
     379                 :            :         } else {
     380                 :          0 :                 max_dtr = card->cis.max_dtr;
     381                 :            :         }
     382                 :            : 
     383         [ #  # ]:          0 :         if (card->type == MMC_TYPE_SD_COMBO)
     384                 :          0 :                 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
     385                 :            : 
     386                 :          0 :         return max_dtr;
     387                 :            : }
     388                 :            : 
     389                 :            : static unsigned char host_drive_to_sdio_drive(int host_strength)
     390                 :            : {
     391                 :            :         switch (host_strength) {
     392                 :            :         case MMC_SET_DRIVER_TYPE_A:
     393                 :            :                 return SDIO_DTSx_SET_TYPE_A;
     394                 :            :         case MMC_SET_DRIVER_TYPE_B:
     395                 :            :                 return SDIO_DTSx_SET_TYPE_B;
     396                 :            :         case MMC_SET_DRIVER_TYPE_C:
     397                 :            :                 return SDIO_DTSx_SET_TYPE_C;
     398                 :            :         case MMC_SET_DRIVER_TYPE_D:
     399                 :            :                 return SDIO_DTSx_SET_TYPE_D;
     400                 :            :         default:
     401                 :            :                 return SDIO_DTSx_SET_TYPE_B;
     402                 :            :         }
     403                 :            : }
     404                 :            : 
     405                 :          0 : static void sdio_select_driver_type(struct mmc_card *card)
     406                 :            : {
     407                 :            :         int host_drv_type = SD_DRIVER_TYPE_B;
     408                 :            :         int card_drv_type = SD_DRIVER_TYPE_B;
     409                 :            :         int drive_strength;
     410                 :            :         unsigned char card_strength;
     411                 :            :         int err;
     412                 :            : 
     413                 :            :         /*
     414                 :            :          * If the host doesn't support any of the Driver Types A,C or D,
     415                 :            :          * or there is no board specific handler then default Driver
     416                 :            :          * Type B is used.
     417                 :            :          */
     418         [ #  # ]:          0 :         if (!(card->host->caps &
     419                 :            :                 (MMC_CAP_DRIVER_TYPE_A |
     420                 :            :                  MMC_CAP_DRIVER_TYPE_C |
     421                 :            :                  MMC_CAP_DRIVER_TYPE_D)))
     422                 :          0 :                 return;
     423                 :            : 
     424         [ #  # ]:          0 :         if (!card->host->ops->select_drive_strength)
     425                 :            :                 return;
     426                 :            : 
     427         [ #  # ]:          0 :         if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
     428                 :            :                 host_drv_type |= SD_DRIVER_TYPE_A;
     429                 :            : 
     430         [ #  # ]:          0 :         if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
     431                 :          0 :                 host_drv_type |= SD_DRIVER_TYPE_C;
     432                 :            : 
     433         [ #  # ]:          0 :         if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
     434                 :          0 :                 host_drv_type |= SD_DRIVER_TYPE_D;
     435                 :            : 
     436         [ #  # ]:          0 :         if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
     437                 :            :                 card_drv_type |= SD_DRIVER_TYPE_A;
     438                 :            : 
     439         [ #  # ]:          0 :         if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
     440                 :          0 :                 card_drv_type |= SD_DRIVER_TYPE_C;
     441                 :            : 
     442         [ #  # ]:          0 :         if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
     443                 :          0 :                 card_drv_type |= SD_DRIVER_TYPE_D;
     444                 :            : 
     445                 :            :         /*
     446                 :            :          * The drive strength that the hardware can support
     447                 :            :          * depends on the board design.  Pass the appropriate
     448                 :            :          * information and let the hardware specific code
     449                 :            :          * return what is possible given the options
     450                 :            :          */
     451                 :          0 :         drive_strength = card->host->ops->select_drive_strength(
     452                 :            :                 card->sw_caps.uhs_max_dtr,
     453                 :            :                 host_drv_type, card_drv_type);
     454                 :            : 
     455                 :            :         /* if error just use default for drive strength B */
     456                 :          0 :         err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
     457                 :            :                 &card_strength);
     458         [ #  # ]:          0 :         if (err)
     459                 :            :                 return;
     460                 :            : 
     461         [ #  # ]:          0 :         card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
     462                 :          0 :         card_strength |= host_drive_to_sdio_drive(drive_strength);
     463                 :            : 
     464                 :          0 :         err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
     465                 :            :                 card_strength, NULL);
     466                 :            : 
     467                 :            :         /* if error default to drive strength B */
     468         [ #  # ]:          0 :         if (!err)
     469                 :          0 :                 mmc_set_driver_type(card->host, drive_strength);
     470                 :            : }
     471                 :            : 
     472                 :            : 
     473                 :          0 : static int sdio_set_bus_speed_mode(struct mmc_card *card)
     474                 :            : {
     475                 :            :         unsigned int bus_speed, timing;
     476                 :            :         int err;
     477                 :            :         unsigned char speed;
     478                 :            : 
     479                 :            :         /*
     480                 :            :          * If the host doesn't support any of the UHS-I modes, fallback on
     481                 :            :          * default speed.
     482                 :            :          */
     483         [ #  # ]:          0 :         if (!mmc_host_uhs(card->host))
     484                 :            :                 return 0;
     485                 :            : 
     486                 :            :         bus_speed = SDIO_SPEED_SDR12;
     487                 :            :         timing = MMC_TIMING_UHS_SDR12;
     488 [ #  # ][ #  # ]:          0 :         if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
     489                 :          0 :             (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
     490                 :            :                         bus_speed = SDIO_SPEED_SDR104;
     491                 :            :                         timing = MMC_TIMING_UHS_SDR104;
     492                 :          0 :                         card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
     493                 :          0 :                         card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
     494 [ #  # ][ #  # ]:          0 :         } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
     495                 :          0 :                    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
     496                 :            :                         bus_speed = SDIO_SPEED_DDR50;
     497                 :            :                         timing = MMC_TIMING_UHS_DDR50;
     498                 :          0 :                         card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
     499                 :          0 :                         card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
     500         [ #  # ]:          0 :         } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
     501         [ #  # ]:          0 :                     MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
     502                 :            :                     SD_MODE_UHS_SDR50)) {
     503                 :            :                         bus_speed = SDIO_SPEED_SDR50;
     504                 :            :                         timing = MMC_TIMING_UHS_SDR50;
     505                 :          0 :                         card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
     506                 :          0 :                         card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
     507         [ #  # ]:          0 :         } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
     508         [ #  # ]:          0 :                     MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
     509                 :          0 :                    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
     510                 :            :                         bus_speed = SDIO_SPEED_SDR25;
     511                 :            :                         timing = MMC_TIMING_UHS_SDR25;
     512                 :          0 :                         card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
     513                 :          0 :                         card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
     514         [ #  # ]:          0 :         } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
     515                 :            :                     MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
     516         [ #  # ]:          0 :                     MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
     517                 :            :                     SD_MODE_UHS_SDR12)) {
     518                 :            :                         bus_speed = SDIO_SPEED_SDR12;
     519                 :            :                         timing = MMC_TIMING_UHS_SDR12;
     520                 :          0 :                         card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
     521                 :          0 :                         card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
     522                 :            :         }
     523                 :            : 
     524                 :          0 :         err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
     525         [ #  # ]:          0 :         if (err)
     526                 :            :                 return err;
     527                 :            : 
     528                 :          0 :         speed &= ~SDIO_SPEED_BSS_MASK;
     529                 :          0 :         speed |= bus_speed;
     530                 :          0 :         err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
     531         [ #  # ]:          0 :         if (err)
     532                 :            :                 return err;
     533                 :            : 
     534         [ #  # ]:          0 :         if (bus_speed) {
     535                 :          0 :                 mmc_set_timing(card->host, timing);
     536                 :          0 :                 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
     537                 :            :         }
     538                 :            : 
     539                 :            :         return 0;
     540                 :            : }
     541                 :            : 
     542                 :            : /*
     543                 :            :  * UHS-I specific initialization procedure
     544                 :            :  */
     545                 :          0 : static int mmc_sdio_init_uhs_card(struct mmc_card *card)
     546                 :            : {
     547                 :            :         int err;
     548                 :            : 
     549         [ #  # ]:          0 :         if (!card->scr.sda_spec3)
     550                 :            :                 return 0;
     551                 :            : 
     552                 :            :         /*
     553                 :            :          * Switch to wider bus (if supported).
     554                 :            :          */
     555         [ #  # ]:          0 :         if (card->host->caps & MMC_CAP_4_BIT_DATA) {
     556                 :          0 :                 err = sdio_enable_4bit_bus(card);
     557         [ #  # ]:          0 :                 if (err > 0) {
     558                 :          0 :                         mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
     559                 :            :                         err = 0;
     560                 :            :                 }
     561                 :            :         }
     562                 :            : 
     563                 :            :         /* Set the driver strength for the card */
     564                 :          0 :         sdio_select_driver_type(card);
     565                 :            : 
     566                 :            :         /* Set bus speed mode of the card */
     567                 :          0 :         err = sdio_set_bus_speed_mode(card);
     568         [ #  # ]:          0 :         if (err)
     569                 :            :                 goto out;
     570                 :            : 
     571                 :            :         /*
     572                 :            :          * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
     573                 :            :          * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
     574                 :            :          */
     575 [ #  # ][ #  # ]:          0 :         if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning &&
                 [ #  # ]
     576                 :          0 :                         ((card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR50) ||
     577                 :            :                          (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104))) {
     578                 :            :                 mmc_host_clk_hold(card->host);
     579                 :          0 :                 err = card->host->ops->execute_tuning(card->host,
     580                 :            :                                                       MMC_SEND_TUNING_BLOCK);
     581                 :            :                 mmc_host_clk_release(card->host);
     582                 :            :         }
     583                 :            : 
     584                 :            : out:
     585                 :            : 
     586                 :          0 :         return err;
     587                 :            : }
     588                 :            : 
     589                 :            : /*
     590                 :            :  * Handle the detection and initialisation of a card.
     591                 :            :  *
     592                 :            :  * In the case of a resume, "oldcard" will contain the card
     593                 :            :  * we're trying to reinitialise.
     594                 :            :  */
     595                 :          0 : static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
     596                 :            :                               struct mmc_card *oldcard, int powered_resume)
     597                 :            : {
     598                 :            :         struct mmc_card *card;
     599                 :            :         int err;
     600                 :            :         int retries = 10;
     601                 :          0 :         u32 rocr = 0;
     602                 :            :         u32 ocr_card = ocr;
     603                 :            : 
     604         [ #  # ]:          0 :         BUG_ON(!host);
     605         [ #  # ]:          0 :         WARN_ON(!host->claimed);
     606                 :            : 
     607                 :            :         /* to query card if 1.8V signalling is supported */
     608         [ #  # ]:          0 :         if (mmc_host_uhs(host))
     609                 :          0 :                 ocr |= R4_18V_PRESENT;
     610                 :            : 
     611                 :            : try_again:
     612         [ #  # ]:          0 :         if (!retries) {
     613                 :          0 :                 pr_warning("%s: Skipping voltage switch\n",
     614                 :            :                                 mmc_hostname(host));
     615                 :          0 :                 ocr &= ~R4_18V_PRESENT;
     616                 :            :         }
     617                 :            : 
     618                 :            :         /*
     619                 :            :          * Inform the card of the voltage
     620                 :            :          */
     621         [ #  # ]:          0 :         if (!powered_resume) {
     622                 :          0 :                 err = mmc_send_io_op_cond(host, ocr, &rocr);
     623         [ #  # ]:          0 :                 if (err)
     624                 :            :                         goto err;
     625                 :            :         }
     626                 :            : 
     627                 :            :         /*
     628                 :            :          * For SPI, enable CRC as appropriate.
     629                 :            :          */
     630         [ #  # ]:          0 :         if (mmc_host_is_spi(host)) {
     631                 :          0 :                 err = mmc_spi_set_crc(host, use_spi_crc);
     632         [ #  # ]:          0 :                 if (err)
     633                 :            :                         goto err;
     634                 :            :         }
     635                 :            : 
     636                 :            :         /*
     637                 :            :          * Allocate card structure.
     638                 :            :          */
     639                 :          0 :         card = mmc_alloc_card(host, NULL);
     640         [ #  # ]:          0 :         if (IS_ERR(card)) {
     641                 :            :                 err = PTR_ERR(card);
     642                 :          0 :                 goto err;
     643                 :            :         }
     644                 :            : 
     645   [ #  #  #  # ]:          0 :         if ((rocr & R4_MEMORY_PRESENT) &&
     646                 :          0 :             mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
     647                 :          0 :                 card->type = MMC_TYPE_SD_COMBO;
     648                 :            : 
     649 [ #  # ][ #  # ]:          0 :                 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
                 [ #  # ]
     650                 :          0 :                     memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
     651                 :          0 :                         mmc_remove_card(card);
     652                 :          0 :                         return -ENOENT;
     653                 :            :                 }
     654                 :            :         } else {
     655                 :          0 :                 card->type = MMC_TYPE_SDIO;
     656                 :            : 
     657 [ #  # ][ #  # ]:          0 :                 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
     658                 :          0 :                         mmc_remove_card(card);
     659                 :          0 :                         return -ENOENT;
     660                 :            :                 }
     661                 :            :         }
     662                 :            : 
     663                 :            :         /*
     664                 :            :          * Call the optional HC's init_card function to handle quirks.
     665                 :            :          */
     666         [ #  # ]:          0 :         if (host->ops->init_card)
     667                 :          0 :                 host->ops->init_card(host, card);
     668                 :            : 
     669                 :            :         /*
     670                 :            :          * If the host and card support UHS-I mode request the card
     671                 :            :          * to switch to 1.8V signaling level.  No 1.8v signalling if
     672                 :            :          * UHS mode is not enabled to maintain compatibility and some
     673                 :            :          * systems that claim 1.8v signalling in fact do not support
     674                 :            :          * it.
     675                 :            :          */
     676 [ #  # ][ #  # ]:          0 :         if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
     677                 :          0 :                 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
     678                 :            :                                         ocr);
     679         [ #  # ]:          0 :                 if (err == -EAGAIN) {
     680                 :          0 :                         sdio_reset(host);
     681                 :          0 :                         mmc_go_idle(host);
     682                 :          0 :                         mmc_send_if_cond(host, host->ocr_avail);
     683                 :          0 :                         mmc_remove_card(card);
     684                 :          0 :                         retries--;
     685                 :          0 :                         goto try_again;
     686         [ #  # ]:          0 :                 } else if (err) {
     687                 :          0 :                         ocr &= ~R4_18V_PRESENT;
     688                 :            :                 }
     689                 :            :                 err = 0;
     690                 :            :         } else {
     691                 :          0 :                 ocr &= ~R4_18V_PRESENT;
     692                 :            :         }
     693                 :            : 
     694                 :            :         /*
     695                 :            :          * For native busses:  set card RCA and quit open drain mode.
     696                 :            :          */
     697 [ #  # ][ #  # ]:          0 :         if (!powered_resume && !mmc_host_is_spi(host)) {
     698                 :          0 :                 err = mmc_send_relative_addr(host, &card->rca);
     699         [ #  # ]:          0 :                 if (err)
     700                 :            :                         goto remove;
     701                 :            : 
     702                 :            :                 /*
     703                 :            :                  * Update oldcard with the new RCA received from the SDIO
     704                 :            :                  * device -- we're doing this so that it's updated in the
     705                 :            :                  * "card" struct when oldcard overwrites that later.
     706                 :            :                  */
     707         [ #  # ]:          0 :                 if (oldcard)
     708                 :          0 :                         oldcard->rca = card->rca;
     709                 :            :         }
     710                 :            : 
     711                 :            :         /*
     712                 :            :          * Read CSD, before selecting the card
     713                 :            :          */
     714 [ #  # ][ #  # ]:          0 :         if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
     715                 :          0 :                 err = mmc_sd_get_csd(host, card);
     716         [ #  # ]:          0 :                 if (err)
     717                 :            :                         return err;
     718                 :            : 
     719                 :          0 :                 mmc_decode_cid(card);
     720                 :            :         }
     721                 :            : 
     722                 :            :         /*
     723                 :            :          * Select card, as all following commands rely on that.
     724                 :            :          */
     725 [ #  # ][ #  # ]:          0 :         if (!powered_resume && !mmc_host_is_spi(host)) {
     726                 :          0 :                 err = mmc_select_card(card);
     727         [ #  # ]:          0 :                 if (err)
     728                 :            :                         goto remove;
     729                 :            :         }
     730                 :            : 
     731         [ #  # ]:          0 :         if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
     732                 :            :                 /*
     733                 :            :                  * This is non-standard SDIO device, meaning it doesn't
     734                 :            :                  * have any CIA (Common I/O area) registers present.
     735                 :            :                  * It's host's responsibility to fill cccr and cis
     736                 :            :                  * structures in init_card().
     737                 :            :                  */
     738                 :          0 :                 mmc_set_clock(host, card->cis.max_dtr);
     739                 :            : 
     740         [ #  # ]:          0 :                 if (card->cccr.high_speed) {
     741                 :          0 :                         mmc_card_set_highspeed(card);
     742                 :          0 :                         mmc_set_timing(card->host, MMC_TIMING_SD_HS);
     743                 :            :                 }
     744                 :            : 
     745                 :            :                 goto finish;
     746                 :            :         }
     747                 :            : 
     748                 :            : #ifdef CONFIG_MMC_EMBEDDED_SDIO
     749                 :            :         if (host->embedded_sdio_data.cccr)
     750                 :            :                 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
     751                 :            :         else {
     752                 :            : #endif
     753                 :            :                 /*
     754                 :            :                  * Read the common registers.
     755                 :            :                  */
     756                 :          0 :                 err = sdio_read_cccr(card,  ocr);
     757         [ #  # ]:          0 :                 if (err)
     758                 :            :                         goto remove;
     759                 :            : #ifdef CONFIG_MMC_EMBEDDED_SDIO
     760                 :            :         }
     761                 :            : #endif
     762                 :            : 
     763                 :            : #ifdef CONFIG_MMC_EMBEDDED_SDIO
     764                 :            :         if (host->embedded_sdio_data.cis)
     765                 :            :                 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
     766                 :            :         else {
     767                 :            : #endif
     768                 :            :                 /*
     769                 :            :                  * Read the common CIS tuples.
     770                 :            :                  */
     771                 :          0 :                 err = sdio_read_common_cis(card);
     772         [ #  # ]:          0 :                 if (err)
     773                 :            :                         goto remove;
     774                 :            : #ifdef CONFIG_MMC_EMBEDDED_SDIO
     775                 :            :         }
     776                 :            : #endif
     777                 :            : 
     778         [ #  # ]:          0 :         if (oldcard) {
     779                 :          0 :                 int same = (card->cis.vendor == oldcard->cis.vendor &&
     780                 :            :                             card->cis.device == oldcard->cis.device);
     781                 :          0 :                 mmc_remove_card(card);
     782         [ #  # ]:          0 :                 if (!same)
     783                 :            :                         return -ENOENT;
     784                 :            : 
     785                 :            :                 card = oldcard;
     786                 :            :         }
     787                 :          0 :         card->ocr = ocr_card;
     788                 :          0 :         mmc_fixup_device(card, NULL);
     789                 :            : 
     790         [ #  # ]:          0 :         if (card->type == MMC_TYPE_SD_COMBO) {
     791                 :          0 :                 err = mmc_sd_setup_card(host, card, oldcard != NULL);
     792                 :            :                 /* handle as SDIO-only card if memory init failed */
     793         [ #  # ]:          0 :                 if (err) {
     794                 :          0 :                         mmc_go_idle(host);
     795         [ #  # ]:          0 :                         if (mmc_host_is_spi(host))
     796                 :            :                                 /* should not fail, as it worked previously */
     797                 :          0 :                                 mmc_spi_set_crc(host, use_spi_crc);
     798                 :          0 :                         card->type = MMC_TYPE_SDIO;
     799                 :            :                 } else
     800                 :          0 :                         card->dev.type = &sd_type;
     801                 :            :         }
     802                 :            : 
     803                 :            :         /*
     804                 :            :          * If needed, disconnect card detection pull-up resistor.
     805                 :            :          */
     806                 :          0 :         err = sdio_disable_cd(card);
     807         [ #  # ]:          0 :         if (err)
     808                 :            :                 goto remove;
     809                 :            : 
     810                 :            :         /* Initialization sequence for UHS-I cards */
     811                 :            :         /* Only if card supports 1.8v and UHS signaling */
     812 [ #  # ][ #  # ]:          0 :         if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
     813                 :          0 :                 err = mmc_sdio_init_uhs_card(card);
     814         [ #  # ]:          0 :                 if (err)
     815                 :            :                         goto remove;
     816                 :            : 
     817                 :            :                 /* Card is an ultra-high-speed card */
     818                 :          0 :                 mmc_card_set_uhs(card);
     819                 :            :         } else {
     820                 :            :                 /*
     821                 :            :                  * Switch to high-speed (if supported).
     822                 :            :                  */
     823                 :          0 :                 err = sdio_enable_hs(card);
     824         [ #  # ]:          0 :                 if (err > 0)
     825                 :          0 :                         mmc_sd_go_highspeed(card);
     826         [ #  # ]:          0 :                 else if (err)
     827                 :            :                         goto remove;
     828                 :            : 
     829                 :            :                 /*
     830                 :            :                  * Change to the card's maximum speed.
     831                 :            :                  */
     832                 :          0 :                 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
     833                 :            : 
     834                 :            :                 /*
     835                 :            :                  * Switch to wider bus (if supported).
     836                 :            :                  */
     837                 :          0 :                 err = sdio_enable_4bit_bus(card);
     838         [ #  # ]:          0 :                 if (err > 0)
     839                 :          0 :                         mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
     840         [ #  # ]:          0 :                 else if (err)
     841                 :            :                         goto remove;
     842                 :            :         }
     843                 :            : finish:
     844         [ #  # ]:          0 :         if (!oldcard)
     845                 :          0 :                 host->card = card;
     846                 :            :         return 0;
     847                 :            : 
     848                 :            : remove:
     849         [ #  # ]:          0 :         if (!oldcard)
     850                 :          0 :                 mmc_remove_card(card);
     851                 :            : 
     852                 :            : err:
     853                 :          0 :         return err;
     854                 :            : }
     855                 :            : 
     856                 :            : /*
     857                 :            :  * Host is being removed. Free up the current card.
     858                 :            :  */
     859                 :          0 : static void mmc_sdio_remove(struct mmc_host *host)
     860                 :            : {
     861                 :            :         int i;
     862                 :            : 
     863         [ #  # ]:          0 :         BUG_ON(!host);
     864         [ #  # ]:          0 :         BUG_ON(!host->card);
     865                 :            : 
     866         [ #  # ]:          0 :         for (i = 0;i < host->card->sdio_funcs;i++) {
     867         [ #  # ]:          0 :                 if (host->card->sdio_func[i]) {
     868                 :          0 :                         sdio_remove_func(host->card->sdio_func[i]);
     869                 :          0 :                         host->card->sdio_func[i] = NULL;
     870                 :            :                 }
     871                 :            :         }
     872                 :            : 
     873                 :          0 :         mmc_remove_card(host->card);
     874                 :          0 :         host->card = NULL;
     875                 :          0 : }
     876                 :            : 
     877                 :            : /*
     878                 :            :  * Card detection - card is alive.
     879                 :            :  */
     880                 :          0 : static int mmc_sdio_alive(struct mmc_host *host)
     881                 :            : {
     882                 :          0 :         return mmc_select_card(host->card);
     883                 :            : }
     884                 :            : 
     885                 :            : /*
     886                 :            :  * Card detection callback from host.
     887                 :            :  */
     888                 :          0 : static void mmc_sdio_detect(struct mmc_host *host)
     889                 :            : {
     890                 :            :         int err;
     891                 :            : 
     892         [ #  # ]:          0 :         BUG_ON(!host);
     893         [ #  # ]:          0 :         BUG_ON(!host->card);
     894                 :            : 
     895                 :            :         /* Make sure card is powered before detecting it */
     896                 :            :         if (host->caps & MMC_CAP_POWER_OFF_CARD) {
     897                 :            :                 err = pm_runtime_get_sync(&host->card->dev);
     898                 :            :                 if (err < 0) {
     899                 :            :                         pm_runtime_put_noidle(&host->card->dev);
     900                 :            :                         goto out;
     901                 :            :                 }
     902                 :            :         }
     903                 :            : 
     904                 :            :         mmc_claim_host(host);
     905                 :            : 
     906                 :            :         /*
     907                 :            :          * Just check if our card has been removed.
     908                 :            :          */
     909                 :          0 :         err = _mmc_detect_card_removed(host);
     910                 :            : 
     911                 :          0 :         mmc_release_host(host);
     912                 :            : 
     913                 :            :         /*
     914                 :            :          * Tell PM core it's OK to power off the card now.
     915                 :            :          *
     916                 :            :          * The _sync variant is used in order to ensure that the card
     917                 :            :          * is left powered off in case an error occurred, and the card
     918                 :            :          * is going to be removed.
     919                 :            :          *
     920                 :            :          * Since there is no specific reason to believe a new user
     921                 :            :          * is about to show up at this point, the _sync variant is
     922                 :            :          * desirable anyway.
     923                 :            :          */
     924                 :            :         if (host->caps & MMC_CAP_POWER_OFF_CARD)
     925                 :            :                 pm_runtime_put_sync(&host->card->dev);
     926                 :            : 
     927                 :            : out:
     928         [ #  # ]:          0 :         if (err) {
     929                 :          0 :                 mmc_sdio_remove(host);
     930                 :            : 
     931                 :            :                 mmc_claim_host(host);
     932                 :          0 :                 mmc_detach_bus(host);
     933                 :          0 :                 mmc_power_off(host);
     934                 :          0 :                 mmc_release_host(host);
     935                 :            :         }
     936                 :          0 : }
     937                 :            : 
     938                 :            : /*
     939                 :            :  * SDIO pre_suspend.  We need to suspend all functions separately.
     940                 :            :  * Therefore all registered functions must have drivers with suspend
     941                 :            :  * and resume methods.  Failing that we simply remove the whole card.
     942                 :            :  */
     943                 :          0 : static int mmc_sdio_pre_suspend(struct mmc_host *host)
     944                 :            : {
     945                 :            :         int i, err = 0;
     946                 :            : 
     947         [ #  # ]:          0 :         for (i = 0; i < host->card->sdio_funcs; i++) {
     948                 :          0 :                 struct sdio_func *func = host->card->sdio_func[i];
     949 [ #  # ][ #  # ]:          0 :                 if (func && sdio_func_present(func) && func->dev.driver) {
                 [ #  # ]
     950                 :          0 :                         const struct dev_pm_ops *pmops = func->dev.driver->pm;
     951 [ #  # ][ #  # ]:          0 :                         if (!pmops || !pmops->suspend || !pmops->resume) {
                 [ #  # ]
     952                 :            :                                 /* force removal of entire card in that case */
     953                 :            :                                 err = -ENOSYS;
     954                 :            :                                 break;
     955                 :            :                         }
     956                 :            :                 }
     957                 :            :         }
     958                 :            : 
     959                 :          0 :         return err;
     960                 :            : }
     961                 :            : 
     962                 :            : /*
     963                 :            :  * SDIO suspend.  Suspend all functions separately.
     964                 :            :  */
     965                 :          0 : static int mmc_sdio_suspend(struct mmc_host *host)
     966                 :            : {
     967                 :            :         int i, err = 0;
     968                 :            : 
     969         [ #  # ]:          0 :         for (i = 0; i < host->card->sdio_funcs; i++) {
     970                 :          0 :                 struct sdio_func *func = host->card->sdio_func[i];
     971 [ #  # ][ #  # ]:          0 :                 if (func && sdio_func_present(func) && func->dev.driver) {
                 [ #  # ]
     972                 :          0 :                         const struct dev_pm_ops *pmops = func->dev.driver->pm;
     973                 :          0 :                         err = pmops->suspend(&func->dev);
     974         [ #  # ]:          0 :                         if (err)
     975                 :            :                                 break;
     976                 :            :                 }
     977                 :            :         }
     978 [ #  # ][ #  # ]:          0 :         while (err && --i >= 0) {
     979                 :          0 :                 struct sdio_func *func = host->card->sdio_func[i];
     980 [ #  # ][ #  # ]:          0 :                 if (func && sdio_func_present(func) && func->dev.driver) {
                 [ #  # ]
     981                 :          0 :                         const struct dev_pm_ops *pmops = func->dev.driver->pm;
     982                 :          0 :                         pmops->resume(&func->dev);
     983                 :            :                 }
     984                 :            :         }
     985                 :            : 
     986 [ #  # ][ #  # ]:          0 :         if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
                 [ #  # ]
     987                 :            :                 mmc_claim_host(host);
     988                 :          0 :                 sdio_disable_wide(host->card);
     989                 :          0 :                 mmc_release_host(host);
     990                 :            :         }
     991                 :            : 
     992 [ #  # ][ #  # ]:          0 :         if (!err && !mmc_card_keep_power(host))
     993                 :          0 :                 mmc_power_off(host);
     994                 :            : 
     995                 :          0 :         return err;
     996                 :            : }
     997                 :            : 
     998                 :          0 : static int mmc_sdio_resume(struct mmc_host *host)
     999                 :            : {
    1000                 :            :         int i, err = 0;
    1001                 :            : 
    1002         [ #  # ]:          0 :         BUG_ON(!host);
    1003         [ #  # ]:          0 :         BUG_ON(!host->card);
    1004                 :            : 
    1005                 :            :         /* Basic card reinitialization. */
    1006                 :            :         mmc_claim_host(host);
    1007                 :            : 
    1008                 :            :         /* Restore power if needed */
    1009         [ #  # ]:          0 :         if (!mmc_card_keep_power(host)) {
    1010                 :          0 :                 mmc_power_up(host, host->card->ocr);
    1011                 :            :                 /*
    1012                 :            :                  * Tell runtime PM core we just powered up the card,
    1013                 :            :                  * since it still believes the card is powered off.
    1014                 :            :                  * Note that currently runtime PM is only enabled
    1015                 :            :                  * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
    1016                 :            :                  */
    1017                 :            :                 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
    1018                 :            :                         pm_runtime_disable(&host->card->dev);
    1019                 :            :                         pm_runtime_set_active(&host->card->dev);
    1020                 :            :                         pm_runtime_enable(&host->card->dev);
    1021                 :            :                 }
    1022                 :            :         }
    1023                 :            : 
    1024                 :            :         /* No need to reinitialize powered-resumed nonremovable cards */
    1025 [ #  # ][ #  # ]:          0 :         if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
    1026                 :          0 :                 sdio_reset(host);
    1027                 :          0 :                 mmc_go_idle(host);
    1028                 :          0 :                 err = mmc_sdio_init_card(host, host->card->ocr, host->card,
    1029                 :            :                                         mmc_card_keep_power(host));
    1030 [ #  # ][ #  # ]:          0 :         } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
    1031                 :            :                 /* We may have switched to 1-bit mode during suspend */
    1032                 :          0 :                 err = sdio_enable_4bit_bus(host->card);
    1033         [ #  # ]:          0 :                 if (err > 0) {
    1034                 :          0 :                         mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
    1035                 :            :                         err = 0;
    1036                 :            :                 }
    1037                 :            :         }
    1038                 :            : 
    1039 [ #  # ][ #  # ]:          0 :         if (!err && host->sdio_irqs)
    1040                 :          0 :                 wake_up_process(host->sdio_irq_thread);
    1041                 :          0 :         mmc_release_host(host);
    1042                 :            : 
    1043                 :            :         /*
    1044                 :            :          * If the card looked to be the same as before suspending, then
    1045                 :            :          * we proceed to resume all card functions.  If one of them returns
    1046                 :            :          * an error then we simply return that error to the core and the
    1047                 :            :          * card will be redetected as new.  It is the responsibility of
    1048                 :            :          * the function driver to perform further tests with the extra
    1049                 :            :          * knowledge it has of the card to confirm the card is indeed the
    1050                 :            :          * same as before suspending (same MAC address for network cards,
    1051                 :            :          * etc.) and return an error otherwise.
    1052                 :            :          */
    1053 [ #  # ][ #  # ]:          0 :         for (i = 0; !err && i < host->card->sdio_funcs; i++) {
    1054                 :          0 :                 struct sdio_func *func = host->card->sdio_func[i];
    1055 [ #  # ][ #  # ]:          0 :                 if (func && sdio_func_present(func) && func->dev.driver) {
                 [ #  # ]
    1056                 :          0 :                         const struct dev_pm_ops *pmops = func->dev.driver->pm;
    1057                 :          0 :                         err = pmops->resume(&func->dev);
    1058                 :            :                 }
    1059                 :            :         }
    1060                 :            : 
    1061                 :          0 :         host->pm_flags &= ~MMC_PM_KEEP_POWER;
    1062                 :          0 :         return err;
    1063                 :            : }
    1064                 :            : 
    1065                 :          0 : static int mmc_sdio_power_restore(struct mmc_host *host)
    1066                 :            : {
    1067                 :            :         int ret;
    1068                 :            : 
    1069         [ #  # ]:          0 :         BUG_ON(!host);
    1070         [ #  # ]:          0 :         BUG_ON(!host->card);
    1071                 :            : 
    1072                 :            :         mmc_claim_host(host);
    1073                 :            : 
    1074                 :            :         /*
    1075                 :            :          * Reset the card by performing the same steps that are taken by
    1076                 :            :          * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
    1077                 :            :          *
    1078                 :            :          * sdio_reset() is technically not needed. Having just powered up the
    1079                 :            :          * hardware, it should already be in reset state. However, some
    1080                 :            :          * platforms (such as SD8686 on OLPC) do not instantly cut power,
    1081                 :            :          * meaning that a reset is required when restoring power soon after
    1082                 :            :          * powering off. It is harmless in other cases.
    1083                 :            :          *
    1084                 :            :          * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
    1085                 :            :          * is not necessary for non-removable cards. However, it is required
    1086                 :            :          * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
    1087                 :            :          * harmless in other situations.
    1088                 :            :          *
    1089                 :            :          */
    1090                 :            : 
    1091                 :          0 :         sdio_reset(host);
    1092                 :          0 :         mmc_go_idle(host);
    1093                 :          0 :         mmc_send_if_cond(host, host->ocr_avail);
    1094                 :            : 
    1095                 :          0 :         ret = mmc_send_io_op_cond(host, 0, NULL);
    1096         [ #  # ]:          0 :         if (ret)
    1097                 :            :                 goto out;
    1098                 :            : 
    1099                 :          0 :         ret = mmc_sdio_init_card(host, host->card->ocr, host->card,
    1100                 :            :                                 mmc_card_keep_power(host));
    1101 [ #  # ][ #  # ]:          0 :         if (!ret && host->sdio_irqs)
    1102                 :            :                 mmc_signal_sdio_irq(host);
    1103                 :            : 
    1104                 :            : out:
    1105                 :          0 :         mmc_release_host(host);
    1106                 :            : 
    1107                 :          0 :         return ret;
    1108                 :            : }
    1109                 :            : 
    1110                 :          0 : static int mmc_sdio_runtime_suspend(struct mmc_host *host)
    1111                 :            : {
    1112                 :            :         /* No references to the card, cut the power to it. */
    1113                 :          0 :         mmc_power_off(host);
    1114                 :          0 :         return 0;
    1115                 :            : }
    1116                 :            : 
    1117                 :          0 : static int mmc_sdio_runtime_resume(struct mmc_host *host)
    1118                 :            : {
    1119                 :            :         /* Restore power and re-initialize. */
    1120                 :          0 :         mmc_power_up(host, host->card->ocr);
    1121                 :          0 :         return mmc_sdio_power_restore(host);
    1122                 :            : }
    1123                 :            : 
    1124                 :            : static const struct mmc_bus_ops mmc_sdio_ops = {
    1125                 :            :         .remove = mmc_sdio_remove,
    1126                 :            :         .detect = mmc_sdio_detect,
    1127                 :            :         .pre_suspend = mmc_sdio_pre_suspend,
    1128                 :            :         .suspend = mmc_sdio_suspend,
    1129                 :            :         .resume = mmc_sdio_resume,
    1130                 :            :         .runtime_suspend = mmc_sdio_runtime_suspend,
    1131                 :            :         .runtime_resume = mmc_sdio_runtime_resume,
    1132                 :            :         .power_restore = mmc_sdio_power_restore,
    1133                 :            :         .alive = mmc_sdio_alive,
    1134                 :            : };
    1135                 :            : 
    1136                 :            : 
    1137                 :            : /*
    1138                 :            :  * Starting point for SDIO card init.
    1139                 :            :  */
    1140                 :          0 : int mmc_attach_sdio(struct mmc_host *host)
    1141                 :            : {
    1142                 :            :         int err, i, funcs;
    1143                 :            :         u32 ocr, rocr;
    1144                 :            :         struct mmc_card *card;
    1145                 :            : 
    1146         [ #  # ]:          0 :         BUG_ON(!host);
    1147         [ #  # ]:          0 :         WARN_ON(!host->claimed);
    1148                 :            : 
    1149                 :          0 :         err = mmc_send_io_op_cond(host, 0, &ocr);
    1150         [ #  # ]:          0 :         if (err)
    1151                 :            :                 return err;
    1152                 :            : 
    1153                 :          0 :         mmc_attach_bus(host, &mmc_sdio_ops);
    1154         [ #  # ]:          0 :         if (host->ocr_avail_sdio)
    1155                 :          0 :                 host->ocr_avail = host->ocr_avail_sdio;
    1156                 :            : 
    1157                 :            : 
    1158                 :          0 :         rocr = mmc_select_voltage(host, ocr);
    1159                 :            : 
    1160                 :            :         /*
    1161                 :            :          * Can we support the voltage(s) of the card(s)?
    1162                 :            :          */
    1163         [ #  # ]:          0 :         if (!rocr) {
    1164                 :            :                 err = -EINVAL;
    1165                 :            :                 goto err;
    1166                 :            :         }
    1167                 :            : 
    1168                 :            :         /*
    1169                 :            :          * Detect and init the card.
    1170                 :            :          */
    1171                 :          0 :         err = mmc_sdio_init_card(host, rocr, NULL, 0);
    1172         [ #  # ]:          0 :         if (err)
    1173                 :            :                 goto err;
    1174                 :            : 
    1175                 :          0 :         card = host->card;
    1176                 :            : 
    1177                 :            :         /*
    1178                 :            :          * Enable runtime PM only if supported by host+card+board
    1179                 :            :          */
    1180                 :            :         if (host->caps & MMC_CAP_POWER_OFF_CARD) {
    1181                 :            :                 /*
    1182                 :            :                  * Let runtime PM core know our card is active
    1183                 :            :                  */
    1184                 :            :                 err = pm_runtime_set_active(&card->dev);
    1185                 :            :                 if (err)
    1186                 :            :                         goto remove;
    1187                 :            : 
    1188                 :            :                 /*
    1189                 :            :                  * Enable runtime PM for this card
    1190                 :            :                  */
    1191                 :            :                 pm_runtime_enable(&card->dev);
    1192                 :            :         }
    1193                 :            : 
    1194                 :            :         /*
    1195                 :            :          * The number of functions on the card is encoded inside
    1196                 :            :          * the ocr.
    1197                 :            :          */
    1198                 :          0 :         funcs = (ocr & 0x70000000) >> 28;
    1199                 :          0 :         card->sdio_funcs = 0;
    1200                 :            : 
    1201                 :            : #ifdef CONFIG_MMC_EMBEDDED_SDIO
    1202                 :            :         if (host->embedded_sdio_data.funcs)
    1203                 :            :                 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
    1204                 :            : #endif
    1205                 :            : 
    1206                 :            :         /*
    1207                 :            :          * Initialize (but don't add) all present functions.
    1208                 :            :          */
    1209         [ #  # ]:          0 :         for (i = 0; i < funcs; i++, card->sdio_funcs++) {
    1210                 :            : #ifdef CONFIG_MMC_EMBEDDED_SDIO
    1211                 :            :                 if (host->embedded_sdio_data.funcs) {
    1212                 :            :                         struct sdio_func *tmp;
    1213                 :            : 
    1214                 :            :                         tmp = sdio_alloc_func(host->card);
    1215                 :            :                         if (IS_ERR(tmp))
    1216                 :            :                                 goto remove;
    1217                 :            :                         tmp->num = (i + 1);
    1218                 :            :                         card->sdio_func[i] = tmp;
    1219                 :            :                         tmp->class = host->embedded_sdio_data.funcs[i].f_class;
    1220                 :            :                         tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
    1221                 :            :                         tmp->vendor = card->cis.vendor;
    1222                 :            :                         tmp->device = card->cis.device;
    1223                 :            :                 } else {
    1224                 :            : #endif
    1225                 :          0 :                         err = sdio_init_func(host->card, i + 1);
    1226         [ #  # ]:          0 :                         if (err)
    1227                 :            :                                 goto remove;
    1228                 :            : #ifdef CONFIG_MMC_EMBEDDED_SDIO
    1229                 :            :                 }
    1230                 :            : #endif
    1231                 :            :                 /*
    1232                 :            :                  * Enable Runtime PM for this func (if supported)
    1233                 :            :                  */
    1234                 :            :                 if (host->caps & MMC_CAP_POWER_OFF_CARD)
    1235                 :            :                         pm_runtime_enable(&card->sdio_func[i]->dev);
    1236                 :            :         }
    1237                 :            : 
    1238                 :            :         /*
    1239                 :            :          * First add the card to the driver model...
    1240                 :            :          */
    1241                 :          0 :         mmc_release_host(host);
    1242                 :          0 :         err = mmc_add_card(host->card);
    1243         [ #  # ]:          0 :         if (err)
    1244                 :            :                 goto remove_added;
    1245                 :            : 
    1246                 :            :         /*
    1247                 :            :          * ...then the SDIO functions.
    1248                 :            :          */
    1249         [ #  # ]:          0 :         for (i = 0;i < funcs;i++) {
    1250                 :          0 :                 err = sdio_add_func(host->card->sdio_func[i]);
    1251         [ #  # ]:          0 :                 if (err)
    1252                 :            :                         goto remove_added;
    1253                 :            :         }
    1254                 :            : 
    1255                 :            :         mmc_claim_host(host);
    1256                 :          0 :         return 0;
    1257                 :            : 
    1258                 :            : 
    1259                 :            : remove_added:
    1260                 :            :         /* Remove without lock if the device has been added. */
    1261                 :          0 :         mmc_sdio_remove(host);
    1262                 :            :         mmc_claim_host(host);
    1263                 :            : remove:
    1264                 :            :         /* And with lock if it hasn't been added. */
    1265                 :          0 :         mmc_release_host(host);
    1266         [ #  # ]:          0 :         if (host->card)
    1267                 :          0 :                 mmc_sdio_remove(host);
    1268                 :            :         mmc_claim_host(host);
    1269                 :            : err:
    1270                 :          0 :         mmc_detach_bus(host);
    1271                 :            : 
    1272                 :          0 :         pr_err("%s: error %d whilst initialising SDIO card\n",
    1273                 :            :                 mmc_hostname(host), err);
    1274                 :            : 
    1275                 :          0 :         return err;
    1276                 :            : }
    1277                 :            : 
    1278                 :          0 : int sdio_reset_comm(struct mmc_card *card)
    1279                 :            : {
    1280                 :          0 :         struct mmc_host *host = card->host;
    1281                 :            :         u32 ocr, rocr;
    1282                 :            :         int err;
    1283                 :            : 
    1284                 :          0 :         printk("%s():\n", __func__);
    1285                 :            :         mmc_claim_host(host);
    1286                 :            : 
    1287                 :          0 :         mmc_go_idle(host);
    1288                 :            : 
    1289                 :          0 :         mmc_set_clock(host, host->f_min);
    1290                 :            : 
    1291                 :          0 :         err = mmc_send_io_op_cond(host, 0, &ocr);
    1292         [ #  # ]:          0 :         if (err)
    1293                 :            :                 goto err;
    1294                 :            : 
    1295                 :          0 :         rocr = mmc_select_voltage(host, ocr);
    1296         [ #  # ]:          0 :         if (!rocr) {
    1297                 :            :                 err = -EINVAL;
    1298                 :            :                 goto err;
    1299                 :            :         }
    1300                 :            : 
    1301                 :          0 :         err = mmc_sdio_init_card(host, rocr, card, 0);
    1302         [ #  # ]:          0 :         if (err)
    1303                 :            :                 goto err;
    1304                 :            : 
    1305                 :          0 :         mmc_release_host(host);
    1306                 :          0 :         return 0;
    1307                 :            : err:
    1308                 :          0 :         printk("%s: Error resetting SDIO communications (%d)\n",
    1309                 :            :                mmc_hostname(host), err);
    1310                 :          0 :         mmc_release_host(host);
    1311                 :          0 :         return err;
    1312                 :            : }
    1313                 :            : EXPORT_SYMBOL(sdio_reset_comm);

Generated by: LCOV version 1.9