LCOV - code coverage report
Current view: top level - drivers/mmc/host - bcm2835-mmc.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 1 585 0.2 %
Date: 2020-09-30 20:25:40 Functions: 1 34 2.9 %
Branches: 0 404 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * BCM2835 MMC host driver.
       3                 :            :  *
       4                 :            :  * Author:      Gellert Weisz <gellert@raspberrypi.org>
       5                 :            :  *              Copyright 2014
       6                 :            :  *
       7                 :            :  * Based on
       8                 :            :  *  sdhci-bcm2708.c by Broadcom
       9                 :            :  *  sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
      10                 :            :  *  sdhci.c and sdhci-pci.c by Pierre Ossman
      11                 :            :  *
      12                 :            :  * This program is free software; you can redistribute it and/or modify it
      13                 :            :  * under the terms and conditions of the GNU General Public License,
      14                 :            :  * version 2, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * This program is distributed in the hope it will be useful, but WITHOUT
      17                 :            :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      18                 :            :  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
      19                 :            :  * more details.
      20                 :            :  *
      21                 :            :  * You should have received a copy of the GNU General Public License
      22                 :            :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      23                 :            :  */
      24                 :            : 
      25                 :            : #include <linux/delay.h>
      26                 :            : #include <linux/module.h>
      27                 :            : #include <linux/io.h>
      28                 :            : #include <linux/mmc/mmc.h>
      29                 :            : #include <linux/mmc/host.h>
      30                 :            : #include <linux/mmc/sd.h>
      31                 :            : #include <linux/scatterlist.h>
      32                 :            : #include <linux/of_address.h>
      33                 :            : #include <linux/of_irq.h>
      34                 :            : #include <linux/clk.h>
      35                 :            : #include <linux/platform_device.h>
      36                 :            : #include <linux/err.h>
      37                 :            : #include <linux/blkdev.h>
      38                 :            : #include <linux/dmaengine.h>
      39                 :            : #include <linux/dma-mapping.h>
      40                 :            : #include <linux/of_dma.h>
      41                 :            : #include <linux/swiotlb.h>
      42                 :            : 
      43                 :            : #include "sdhci.h"
      44                 :            : 
      45                 :            : 
      46                 :            : #define DRIVER_NAME "mmc-bcm2835"
      47                 :            : 
      48                 :            : #define DBG(f, x...) \
      49                 :            : pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
      50                 :            : 
      51                 :            : #ifndef CONFIG_MMC_BCM2835_DMA
      52                 :            :  #define FORCE_PIO
      53                 :            : #endif
      54                 :            : 
      55                 :            : 
      56                 :            : /* the inclusive limit in bytes under which PIO will be used instead of DMA */
      57                 :            : #ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
      58                 :            : #define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
      59                 :            : #else
      60                 :            : #define PIO_DMA_BARRIER 00
      61                 :            : #endif
      62                 :            : 
      63                 :            : #define MIN_FREQ 400000
      64                 :            : #define TIMEOUT_VAL 0xE
      65                 :            : #define BCM2835_SDHCI_WRITE_DELAY(f)    (((2 * 1000000) / f) + 1)
      66                 :            : 
      67                 :            : 
      68                 :            : unsigned mmc_debug;
      69                 :            : unsigned mmc_debug2;
      70                 :            : 
      71                 :            : struct bcm2835_host {
      72                 :            :         spinlock_t                              lock;
      73                 :            : 
      74                 :            :         void __iomem                    *ioaddr;
      75                 :            :         u32                                             bus_addr;
      76                 :            : 
      77                 :            :         struct mmc_host                 *mmc;
      78                 :            : 
      79                 :            :         u32                                             timeout;
      80                 :            : 
      81                 :            :         int                                             clock;  /* Current clock speed */
      82                 :            :         u8                                              pwr;    /* Current voltage */
      83                 :            : 
      84                 :            :         unsigned int                    max_clk;                /* Max possible freq */
      85                 :            :         unsigned int                    timeout_clk;    /* Timeout freq (KHz) */
      86                 :            :         unsigned int                    clk_mul;                /* Clock Muliplier value */
      87                 :            : 
      88                 :            :         struct tasklet_struct   finish_tasklet;         /* Tasklet structures */
      89                 :            : 
      90                 :            :         struct timer_list               timer;                  /* Timer for timeouts */
      91                 :            : 
      92                 :            :         struct sg_mapping_iter  sg_miter;               /* SG state for PIO */
      93                 :            :         unsigned int                    blocks;                 /* remaining PIO blocks */
      94                 :            : 
      95                 :            :         int                                             irq;                    /* Device IRQ */
      96                 :            : 
      97                 :            : 
      98                 :            :         u32                                             ier;                    /* cached registers */
      99                 :            : 
     100                 :            :         struct mmc_request              *mrq;                   /* Current request */
     101                 :            :         struct mmc_command              *cmd;                   /* Current command */
     102                 :            :         struct mmc_data                 *data;                  /* Current data request */
     103                 :            :         unsigned int                    data_early:1;           /* Data finished before cmd */
     104                 :            : 
     105                 :            :         wait_queue_head_t               buf_ready_int;          /* Waitqueue for Buffer Read Ready interrupt */
     106                 :            : 
     107                 :            :         u32                                             shadow;
     108                 :            : 
     109                 :            :         /*DMA part*/
     110                 :            :         struct dma_chan                 *dma_chan_rxtx;         /* DMA channel for reads and writes */
     111                 :            :         struct dma_slave_config         dma_cfg_rx;
     112                 :            :         struct dma_slave_config         dma_cfg_tx;
     113                 :            :         struct dma_async_tx_descriptor  *tx_desc;       /* descriptor */
     114                 :            : 
     115                 :            :         bool                                    have_dma;
     116                 :            :         bool                                    use_dma;
     117                 :            :         bool                                    wait_for_dma;
     118                 :            :         /*end of DMA part*/
     119                 :            : 
     120                 :            :         int                                             max_delay;      /* maximum length of time spent waiting */
     121                 :            : 
     122                 :            :         int                                             flags;                          /* Host attributes */
     123                 :            : #define SDHCI_REQ_USE_DMA       (1<<2)    /* Use DMA for this req. */
     124                 :            : #define SDHCI_DEVICE_DEAD       (1<<3)    /* Device unresponsive */
     125                 :            : #define SDHCI_AUTO_CMD12        (1<<6)    /* Auto CMD12 support */
     126                 :            : #define SDHCI_AUTO_CMD23        (1<<7)    /* Auto CMD23 support */
     127                 :            : #define SDHCI_SDIO_IRQ_ENABLED  (1<<9)    /* SDIO irq enabled */
     128                 :            : 
     129                 :            :         u32                             overclock_50;   /* frequency to use when 50MHz is requested (in MHz) */
     130                 :            :         u32                             max_overclock;  /* Highest reported */
     131                 :            : };
     132                 :            : 
     133                 :            : 
     134                 :          0 : static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg, int from)
     135                 :            : {
     136                 :            :         unsigned delay;
     137                 :            :         lockdep_assert_held_once(&host->lock);
     138                 :          0 :         writel(val, host->ioaddr + reg);
     139                 :          0 :         udelay(BCM2835_SDHCI_WRITE_DELAY(max(host->clock, MIN_FREQ)));
     140                 :            : 
     141                 :          0 :         delay = ((mmc_debug >> 16) & 0xf) << ((mmc_debug >> 20) & 0xf);
     142   [ #  #  #  # ]:          0 :         if (delay && !((1<<from) & mmc_debug2))
     143   [ #  #  #  # ]:          0 :                 udelay(delay);
     144                 :          0 : }
     145                 :            : 
     146                 :          0 : static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
     147                 :            : {
     148                 :            :         unsigned delay;
     149                 :            :         lockdep_assert_held_once(&host->lock);
     150                 :          0 :         writel(val, host->ioaddr + reg);
     151                 :            : 
     152                 :          0 :         delay = ((mmc_debug >> 24) & 0xf) << ((mmc_debug >> 28) & 0xf);
     153         [ #  # ]:          0 :         if (delay)
     154   [ #  #  #  # ]:          0 :                 udelay(delay);
     155                 :          0 : }
     156                 :            : 
     157                 :            : static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
     158                 :            : {
     159                 :            :         lockdep_assert_held_once(&host->lock);
     160                 :          0 :         return readl(host->ioaddr + reg);
     161                 :            : }
     162                 :            : 
     163                 :          0 : static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
     164                 :            : {
     165         [ #  # ]:          0 :         u32 oldval = (reg == SDHCI_COMMAND) ? host->shadow :
     166                 :          0 :                 bcm2835_mmc_readl(host, reg & ~3);
     167                 :          0 :         u32 word_num = (reg >> 1) & 1;
     168                 :          0 :         u32 word_shift = word_num * 16;
     169                 :          0 :         u32 mask = 0xffff << word_shift;
     170                 :          0 :         u32 newval = (oldval & ~mask) | (val << word_shift);
     171                 :            : 
     172         [ #  # ]:          0 :         if (reg == SDHCI_TRANSFER_MODE)
     173                 :          0 :                 host->shadow = newval;
     174                 :            :         else
     175                 :          0 :                 bcm2835_mmc_writel(host, newval, reg & ~3, 0);
     176                 :            : 
     177                 :          0 : }
     178                 :            : 
     179                 :          0 : static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
     180                 :            : {
     181                 :          0 :         u32 oldval = bcm2835_mmc_readl(host, reg & ~3);
     182                 :          0 :         u32 byte_num = reg & 3;
     183                 :          0 :         u32 byte_shift = byte_num * 8;
     184                 :          0 :         u32 mask = 0xff << byte_shift;
     185                 :          0 :         u32 newval = (oldval & ~mask) | (val << byte_shift);
     186                 :            : 
     187                 :          0 :         bcm2835_mmc_writel(host, newval, reg & ~3, 1);
     188                 :          0 : }
     189                 :            : 
     190                 :            : 
     191                 :            : static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
     192                 :            : {
     193                 :            :         u32 val = bcm2835_mmc_readl(host, (reg & ~3));
     194                 :            :         u32 word_num = (reg >> 1) & 1;
     195                 :            :         u32 word_shift = word_num * 16;
     196                 :          0 :         u32 word = (val >> word_shift) & 0xffff;
     197                 :            : 
     198                 :          0 :         return word;
     199                 :            : }
     200                 :            : 
     201                 :            : static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
     202                 :            : {
     203                 :          0 :         u32 val = bcm2835_mmc_readl(host, (reg & ~3));
     204                 :            :         u32 byte_num = reg & 3;
     205                 :            :         u32 byte_shift = byte_num * 8;
     206                 :          0 :         u32 byte = (val >> byte_shift) & 0xff;
     207                 :            : 
     208                 :          0 :         return byte;
     209                 :            : }
     210                 :            : 
     211                 :          0 : static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
     212                 :            : {
     213                 :            :         u32 ier;
     214                 :            : 
     215                 :            :         ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
     216                 :          0 :         ier &= ~clear;
     217                 :            :         /* change which requests generate IRQs - makes no difference to
     218                 :            :            the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
     219                 :          0 :         bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE, 2);
     220                 :          0 : }
     221                 :            : 
     222                 :            : 
     223                 :            : static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
     224                 :            : {
     225                 :            :         pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
     226                 :            :                 mmc_hostname(host->mmc));
     227                 :            : 
     228                 :            :         pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version:  0x%08x\n",
     229                 :            :                 bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
     230                 :            :                 bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
     231                 :            :         pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt:  0x%08x\n",
     232                 :            :                 bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
     233                 :            :                 bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
     234                 :            :         pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
     235                 :            :                 bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
     236                 :            :                 bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
     237                 :            :         pr_debug(DRIVER_NAME ": Present:  0x%08x | Host ctl: 0x%08x\n",
     238                 :            :                 bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
     239                 :            :                 bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
     240                 :            :         pr_debug(DRIVER_NAME ": Power:    0x%08x | Blk gap:  0x%08x\n",
     241                 :            :                 bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
     242                 :            :                 bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
     243                 :            :         pr_debug(DRIVER_NAME ": Wake-up:  0x%08x | Clock:    0x%08x\n",
     244                 :            :                 bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
     245                 :            :                 bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
     246                 :            :         pr_debug(DRIVER_NAME ": Timeout:  0x%08x | Int stat: 0x%08x\n",
     247                 :            :                 bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
     248                 :            :                 bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
     249                 :            :         pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
     250                 :            :                 bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
     251                 :            :                 bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
     252                 :            :         pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
     253                 :            :                 bcm2835_mmc_readw(host, SDHCI_AUTO_CMD_STATUS),
     254                 :            :                 bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
     255                 :            :         pr_debug(DRIVER_NAME ": Caps:     0x%08x | Caps_1:   0x%08x\n",
     256                 :            :                 bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
     257                 :            :                 bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
     258                 :            :         pr_debug(DRIVER_NAME ": Cmd:      0x%08x | Max curr: 0x%08x\n",
     259                 :            :                 bcm2835_mmc_readw(host, SDHCI_COMMAND),
     260                 :            :                 bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
     261                 :            :         pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
     262                 :            :                 bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
     263                 :            : 
     264                 :            :         pr_debug(DRIVER_NAME ": ===========================================\n");
     265                 :            : }
     266                 :            : 
     267                 :            : 
     268                 :          0 : static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
     269                 :            : {
     270                 :            :         unsigned long timeout;
     271                 :            :         unsigned long flags;
     272                 :            : 
     273                 :          0 :         spin_lock_irqsave(&host->lock, flags);
     274                 :          0 :         bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
     275                 :            : 
     276         [ #  # ]:          0 :         if (mask & SDHCI_RESET_ALL)
     277                 :          0 :                 host->clock = 0;
     278                 :            : 
     279                 :            :         /* Wait max 100 ms */
     280                 :            :         timeout = 100;
     281                 :            : 
     282                 :            :         /* hw clears the bit when it's done */
     283         [ #  # ]:          0 :         while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
     284         [ #  # ]:          0 :                 if (timeout == 0) {
     285                 :          0 :                         pr_err("%s: Reset 0x%x never completed.\n",
     286                 :            :                                 mmc_hostname(host->mmc), (int)mask);
     287                 :            :                         bcm2835_mmc_dumpregs(host);
     288                 :          0 :                         return;
     289                 :            :                 }
     290                 :          0 :                 timeout--;
     291                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
     292                 :          0 :                 mdelay(1);
     293                 :          0 :                 spin_lock_irqsave(&host->lock, flags);
     294                 :            :         }
     295                 :            : 
     296   [ #  #  #  # ]:          0 :         if (100-timeout > 10 && 100-timeout > host->max_delay) {
     297                 :          0 :                 host->max_delay = 100-timeout;
     298                 :          0 :                 pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
     299                 :            :         }
     300                 :            :         spin_unlock_irqrestore(&host->lock, flags);
     301                 :            : }
     302                 :            : 
     303                 :            : static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
     304                 :            : 
     305                 :          0 : static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
     306                 :            : {
     307                 :            :         unsigned long flags;
     308         [ #  # ]:          0 :         if (soft)
     309                 :          0 :                 bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
     310                 :            :         else
     311                 :          0 :                 bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
     312                 :            : 
     313                 :          0 :         host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
     314                 :            :                     SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
     315                 :            :                     SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
     316                 :            :                     SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
     317                 :            :                     SDHCI_INT_RESPONSE;
     318                 :            : 
     319                 :          0 :         spin_lock_irqsave(&host->lock, flags);
     320                 :          0 :         bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 3);
     321                 :          0 :         bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 3);
     322                 :            :         spin_unlock_irqrestore(&host->lock, flags);
     323                 :            : 
     324         [ #  # ]:          0 :         if (soft) {
     325                 :            :                 /* force clock reconfiguration */
     326                 :          0 :                 host->clock = 0;
     327                 :          0 :                 bcm2835_mmc_set_ios(host->mmc, &host->mmc->ios);
     328                 :            :         }
     329                 :          0 : }
     330                 :            : 
     331                 :            : 
     332                 :            : 
     333                 :            : static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
     334                 :            : 
     335                 :          0 : static void bcm2835_mmc_dma_complete(void *param)
     336                 :            : {
     337                 :            :         struct bcm2835_host *host = param;
     338                 :            :         struct dma_chan *dma_chan;
     339                 :            :         unsigned long flags;
     340                 :            :         u32 dir_data;
     341                 :            : 
     342                 :          0 :         spin_lock_irqsave(&host->lock, flags);
     343                 :            : 
     344                 :          0 :         host->use_dma = false;
     345                 :            : 
     346         [ #  # ]:          0 :         if (host->data) {
     347                 :          0 :                 dma_chan = host->dma_chan_rxtx;
     348         [ #  # ]:          0 :                 if (host->data->flags & MMC_DATA_WRITE)
     349                 :            :                         dir_data = DMA_TO_DEVICE;
     350                 :            :                 else
     351                 :            :                         dir_data = DMA_FROM_DEVICE;
     352                 :          0 :                 dma_unmap_sg(dma_chan->device->dev,
     353                 :            :                      host->data->sg, host->data->sg_len,
     354                 :            :                      dir_data);
     355         [ #  # ]:          0 :                 if (! (host->data->flags & MMC_DATA_WRITE))
     356                 :          0 :                         bcm2835_mmc_finish_data(host);
     357         [ #  # ]:          0 :         } else if (host->wait_for_dma) {
     358                 :          0 :                 host->wait_for_dma = false;
     359                 :          0 :                 tasklet_schedule(&host->finish_tasklet);
     360                 :            :         }
     361                 :            : 
     362                 :            :         spin_unlock_irqrestore(&host->lock, flags);
     363                 :          0 : }
     364                 :            : 
     365                 :          0 : static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
     366                 :            : {
     367                 :            :         unsigned long flags;
     368                 :            :         size_t blksize, len, chunk;
     369                 :            : 
     370                 :            :         u32 uninitialized_var(scratch);
     371                 :            :         u8 *buf;
     372                 :            : 
     373                 :          0 :         blksize = host->data->blksz;
     374                 :            :         chunk = 0;
     375                 :            : 
     376                 :          0 :         local_irq_save(flags);
     377                 :            : 
     378         [ #  # ]:          0 :         while (blksize) {
     379         [ #  # ]:          0 :                 if (!sg_miter_next(&host->sg_miter))
     380                 :          0 :                         BUG();
     381                 :            : 
     382                 :          0 :                 len = min(host->sg_miter.length, blksize);
     383                 :            : 
     384                 :          0 :                 blksize -= len;
     385                 :          0 :                 host->sg_miter.consumed = len;
     386                 :            : 
     387                 :          0 :                 buf = host->sg_miter.addr;
     388                 :            : 
     389         [ #  # ]:          0 :                 while (len) {
     390         [ #  # ]:          0 :                         if (chunk == 0) {
     391                 :            :                                 scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
     392                 :            :                                 chunk = 4;
     393                 :            :                         }
     394                 :            : 
     395                 :          0 :                         *buf = scratch & 0xFF;
     396                 :            : 
     397                 :          0 :                         buf++;
     398                 :          0 :                         scratch >>= 8;
     399                 :          0 :                         chunk--;
     400                 :          0 :                         len--;
     401                 :            :                 }
     402                 :            :         }
     403                 :            : 
     404                 :          0 :         sg_miter_stop(&host->sg_miter);
     405                 :            : 
     406         [ #  # ]:          0 :         local_irq_restore(flags);
     407                 :          0 : }
     408                 :            : 
     409                 :          0 : static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
     410                 :            : {
     411                 :            :         unsigned long flags;
     412                 :            :         size_t blksize, len, chunk;
     413                 :            :         u32 scratch;
     414                 :            :         u8 *buf;
     415                 :            : 
     416                 :          0 :         blksize = host->data->blksz;
     417                 :            :         chunk = 0;
     418                 :            :         chunk = 0;
     419                 :            :         scratch = 0;
     420                 :            : 
     421                 :          0 :         local_irq_save(flags);
     422                 :            : 
     423         [ #  # ]:          0 :         while (blksize) {
     424         [ #  # ]:          0 :                 if (!sg_miter_next(&host->sg_miter))
     425                 :          0 :                         BUG();
     426                 :            : 
     427                 :          0 :                 len = min(host->sg_miter.length, blksize);
     428                 :            : 
     429                 :          0 :                 blksize -= len;
     430                 :          0 :                 host->sg_miter.consumed = len;
     431                 :            : 
     432                 :          0 :                 buf = host->sg_miter.addr;
     433                 :            : 
     434         [ #  # ]:          0 :                 while (len) {
     435                 :          0 :                         scratch |= (u32)*buf << (chunk * 8);
     436                 :            : 
     437                 :          0 :                         buf++;
     438                 :          0 :                         chunk++;
     439                 :          0 :                         len--;
     440                 :            : 
     441   [ #  #  #  # ]:          0 :                         if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
     442                 :          0 :                                 mmc_raw_writel(host, scratch, SDHCI_BUFFER);
     443                 :            :                                 chunk = 0;
     444                 :            :                                 scratch = 0;
     445                 :            :                         }
     446                 :            :                 }
     447                 :            :         }
     448                 :            : 
     449                 :          0 :         sg_miter_stop(&host->sg_miter);
     450                 :            : 
     451         [ #  # ]:          0 :         local_irq_restore(flags);
     452                 :          0 : }
     453                 :            : 
     454                 :            : 
     455                 :          0 : static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
     456                 :            : {
     457                 :            :         u32 mask;
     458                 :            : 
     459         [ #  # ]:          0 :         BUG_ON(!host->data);
     460                 :            : 
     461         [ #  # ]:          0 :         if (host->blocks == 0)
     462                 :          0 :                 return;
     463                 :            : 
     464         [ #  # ]:          0 :         if (host->data->flags & MMC_DATA_READ)
     465                 :            :                 mask = SDHCI_DATA_AVAILABLE;
     466                 :            :         else
     467                 :            :                 mask = SDHCI_SPACE_AVAILABLE;
     468                 :            : 
     469         [ #  # ]:          0 :         while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
     470                 :            : 
     471         [ #  # ]:          0 :                 if (host->data->flags & MMC_DATA_READ)
     472                 :          0 :                         bcm2835_bcm2835_mmc_read_block_pio(host);
     473                 :            :                 else
     474                 :          0 :                         bcm2835_bcm2835_mmc_write_block_pio(host);
     475                 :            : 
     476                 :          0 :                 host->blocks--;
     477                 :            : 
     478                 :            :                 /* QUIRK used in sdhci.c removes the 'if' */
     479                 :            :                 /* but it seems this is unnecessary */
     480         [ #  # ]:          0 :                 if (host->blocks == 0)
     481                 :            :                         break;
     482                 :            : 
     483                 :            : 
     484                 :            :         }
     485                 :            : }
     486                 :            : 
     487                 :            : 
     488                 :          0 : static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
     489                 :            : {
     490                 :            :         u32 len, dir_data, dir_slave;
     491                 :            :         struct dma_async_tx_descriptor *desc = NULL;
     492                 :            :         struct dma_chan *dma_chan;
     493                 :            : 
     494                 :            : 
     495         [ #  # ]:          0 :         WARN_ON(!host->data);
     496                 :            : 
     497         [ #  # ]:          0 :         if (!host->data)
     498                 :            :                 return;
     499                 :            : 
     500         [ #  # ]:          0 :         if (host->blocks == 0)
     501                 :            :                 return;
     502                 :            : 
     503                 :          0 :         dma_chan = host->dma_chan_rxtx;
     504         [ #  # ]:          0 :         if (host->data->flags & MMC_DATA_READ) {
     505                 :            :                 dir_data = DMA_FROM_DEVICE;
     506                 :            :                 dir_slave = DMA_DEV_TO_MEM;
     507                 :            :         } else {
     508                 :            :                 dir_data = DMA_TO_DEVICE;
     509                 :            :                 dir_slave = DMA_MEM_TO_DEV;
     510                 :            :         }
     511                 :            : 
     512                 :            :         /* The parameters have already been validated, so this will not fail */
     513         [ #  # ]:          0 :         (void)dmaengine_slave_config(dma_chan,
     514                 :            :                                      (dir_data == DMA_FROM_DEVICE) ?
     515                 :            :                                      &host->dma_cfg_rx :
     516                 :            :                                      &host->dma_cfg_tx);
     517                 :            : 
     518         [ #  # ]:          0 :         BUG_ON(!dma_chan->device);
     519         [ #  # ]:          0 :         BUG_ON(!dma_chan->device->dev);
     520         [ #  # ]:          0 :         BUG_ON(!host->data->sg);
     521                 :            : 
     522                 :          0 :         len = dma_map_sg(dma_chan->device->dev, host->data->sg,
     523                 :            :                          host->data->sg_len, dir_data);
     524         [ #  # ]:          0 :         if (len > 0) {
     525                 :          0 :                 desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
     526                 :            :                                                len, dir_slave,
     527                 :            :                                                DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
     528                 :            :         } else {
     529                 :          0 :                 dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
     530                 :            :         }
     531         [ #  # ]:          0 :         if (desc) {
     532                 :            :                 unsigned long flags;
     533                 :          0 :                 spin_lock_irqsave(&host->lock, flags);
     534                 :          0 :                 bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
     535                 :            :                                                     SDHCI_INT_SPACE_AVAIL);
     536                 :          0 :                 host->tx_desc = desc;
     537                 :          0 :                 desc->callback = bcm2835_mmc_dma_complete;
     538                 :          0 :                 desc->callback_param = host;
     539                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
     540                 :            :                 dmaengine_submit(desc);
     541                 :            :                 dma_async_issue_pending(dma_chan);
     542                 :            :         } else {
     543                 :          0 :                 dma_unmap_sg(dma_chan->device->dev, host->data->sg, len, dir_data);
     544                 :            :         }
     545                 :            : 
     546                 :            : }
     547                 :            : 
     548                 :            : 
     549                 :            : 
     550                 :          0 : static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
     551                 :            : {
     552                 :            :         u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
     553                 :            :         u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
     554                 :            : 
     555         [ #  # ]:          0 :         if (host->use_dma)
     556                 :          0 :                 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
     557                 :            :         else
     558                 :          0 :                 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
     559                 :            : 
     560                 :          0 :         bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 4);
     561                 :          0 :         bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 4);
     562                 :          0 : }
     563                 :            : 
     564                 :            : 
     565                 :          0 : static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
     566                 :            : {
     567                 :            :         u8 count;
     568                 :          0 :         struct mmc_data *data = cmd->data;
     569                 :            : 
     570         [ #  # ]:          0 :         WARN_ON(host->data);
     571                 :            : 
     572   [ #  #  #  # ]:          0 :         if (data || (cmd->flags & MMC_RSP_BUSY)) {
     573                 :            :                 count = TIMEOUT_VAL;
     574                 :          0 :                 bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
     575                 :            :         }
     576                 :            : 
     577         [ #  # ]:          0 :         if (!data)
     578                 :          0 :                 return;
     579                 :            : 
     580                 :            :         /* Sanity checks */
     581         [ #  # ]:          0 :         BUG_ON(data->blksz * data->blocks > 524288);
     582         [ #  # ]:          0 :         BUG_ON(data->blksz > host->mmc->max_blk_size);
     583         [ #  # ]:          0 :         BUG_ON(data->blocks > 65535);
     584                 :            : 
     585                 :          0 :         host->data = data;
     586                 :          0 :         host->data_early = 0;
     587                 :          0 :         host->data->bytes_xfered = 0;
     588                 :            : 
     589                 :            : 
     590         [ #  # ]:          0 :         if (!(host->flags & SDHCI_REQ_USE_DMA)) {
     591                 :            :                 int flags;
     592                 :            : 
     593                 :            :                 flags = SG_MITER_ATOMIC;
     594         [ #  # ]:          0 :                 if (host->data->flags & MMC_DATA_READ)
     595                 :            :                         flags |= SG_MITER_TO_SG;
     596                 :            :                 else
     597                 :            :                         flags |= SG_MITER_FROM_SG;
     598                 :          0 :                 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
     599                 :          0 :                 host->blocks = data->blocks;
     600                 :            :         }
     601                 :            : 
     602   [ #  #  #  # ]:          0 :         host->use_dma = host->have_dma && data->blocks > PIO_DMA_BARRIER;
     603                 :            : 
     604                 :          0 :         bcm2835_mmc_set_transfer_irqs(host);
     605                 :            : 
     606                 :            :         /* Set the DMA boundary value and block size */
     607                 :          0 :         bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
     608                 :            :                 data->blksz), SDHCI_BLOCK_SIZE);
     609                 :          0 :         bcm2835_mmc_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
     610                 :            : 
     611         [ #  # ]:          0 :         BUG_ON(!host->data);
     612                 :            : }
     613                 :            : 
     614                 :          0 : static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
     615                 :            :         struct mmc_command *cmd)
     616                 :            : {
     617                 :            :         u16 mode;
     618                 :          0 :         struct mmc_data *data = cmd->data;
     619                 :            : 
     620         [ #  # ]:          0 :         if (data == NULL) {
     621                 :            :                 /* clear Auto CMD settings for no data CMDs */
     622                 :            :                 mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
     623                 :          0 :                 bcm2835_mmc_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
     624                 :            :                                 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
     625                 :          0 :                 return;
     626                 :            :         }
     627                 :            : 
     628         [ #  # ]:          0 :         WARN_ON(!host->data);
     629                 :            : 
     630                 :            :         mode = SDHCI_TRNS_BLK_CNT_EN;
     631                 :            : 
     632   [ #  #  #  # ]:          0 :         if ((mmc_op_multi(cmd->opcode) || data->blocks > 1)) {
     633                 :            :                 mode |= SDHCI_TRNS_MULTI;
     634                 :            : 
     635                 :            :                 /*
     636                 :            :                  * If we are sending CMD23, CMD12 never gets sent
     637                 :            :                  * on successful completion (so no Auto-CMD12).
     638                 :            :                  */
     639   [ #  #  #  # ]:          0 :                 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
     640                 :            :                         mode |= SDHCI_TRNS_AUTO_CMD12;
     641   [ #  #  #  # ]:          0 :                 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
     642                 :            :                         mode |= SDHCI_TRNS_AUTO_CMD23;
     643                 :          0 :                         bcm2835_mmc_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2, 5);
     644                 :            :                 }
     645                 :            :         }
     646                 :            : 
     647         [ #  # ]:          0 :         if (data->flags & MMC_DATA_READ)
     648                 :          0 :                 mode |= SDHCI_TRNS_READ;
     649         [ #  # ]:          0 :         if (host->flags & SDHCI_REQ_USE_DMA)
     650                 :          0 :                 mode |= SDHCI_TRNS_DMA;
     651                 :            : 
     652                 :          0 :         bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
     653                 :            : }
     654                 :            : 
     655                 :          0 : void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
     656                 :            : {
     657                 :            :         int flags;
     658                 :            :         u32 mask;
     659                 :            :         unsigned long timeout;
     660                 :            : 
     661         [ #  # ]:          0 :         WARN_ON(host->cmd);
     662                 :            : 
     663                 :            :         /* Wait max 10 ms */
     664                 :            :         timeout = 1000;
     665                 :            : 
     666                 :            :         mask = SDHCI_CMD_INHIBIT;
     667   [ #  #  #  # ]:          0 :         if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
     668                 :            :                 mask |= SDHCI_DATA_INHIBIT;
     669                 :            : 
     670                 :            :         /* We shouldn't wait for data inihibit for stop commands, even
     671                 :            :            though they might use busy signaling */
     672   [ #  #  #  # ]:          0 :         if (host->mrq->data && (cmd == host->mrq->data->stop))
     673                 :            :                 mask &= ~SDHCI_DATA_INHIBIT;
     674                 :            : 
     675         [ #  # ]:          0 :         while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
     676         [ #  # ]:          0 :                 if (timeout == 0) {
     677                 :          0 :                         pr_err("%s: Controller never released inhibit bit(s).\n",
     678                 :            :                                 mmc_hostname(host->mmc));
     679                 :            :                         bcm2835_mmc_dumpregs(host);
     680                 :          0 :                         cmd->error = -EIO;
     681                 :          0 :                         tasklet_schedule(&host->finish_tasklet);
     682                 :          0 :                         return;
     683                 :            :                 }
     684                 :          0 :                 timeout--;
     685                 :          0 :                 udelay(10);
     686                 :            :         }
     687                 :            : 
     688   [ #  #  #  # ]:          0 :         if ((1000-timeout)/100 > 1 && (1000-timeout)/100 > host->max_delay) {
     689                 :          0 :                 host->max_delay = (1000-timeout)/100;
     690                 :          0 :                 pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
     691                 :            :         }
     692                 :            : 
     693                 :          0 :         timeout = jiffies;
     694   [ #  #  #  # ]:          0 :         if (!cmd->data && cmd->busy_timeout > 9000)
     695                 :          0 :                 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
     696                 :            :         else
     697                 :          0 :                 timeout += 10 * HZ;
     698                 :          0 :         mod_timer(&host->timer, timeout);
     699                 :            : 
     700                 :          0 :         host->cmd = cmd;
     701                 :          0 :         host->use_dma = false;
     702                 :            : 
     703                 :          0 :         bcm2835_mmc_prepare_data(host, cmd);
     704                 :            : 
     705                 :          0 :         bcm2835_mmc_writel(host, cmd->arg, SDHCI_ARGUMENT, 6);
     706                 :            : 
     707                 :          0 :         bcm2835_mmc_set_transfer_mode(host, cmd);
     708                 :            : 
     709         [ #  # ]:          0 :         if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
     710                 :          0 :                 pr_err("%s: Unsupported response type!\n",
     711                 :            :                         mmc_hostname(host->mmc));
     712                 :          0 :                 cmd->error = -EINVAL;
     713                 :          0 :                 tasklet_schedule(&host->finish_tasklet);
     714                 :          0 :                 return;
     715                 :            :         }
     716                 :            : 
     717         [ #  # ]:          0 :         if (!(cmd->flags & MMC_RSP_PRESENT))
     718                 :            :                 flags = SDHCI_CMD_RESP_NONE;
     719         [ #  # ]:          0 :         else if (cmd->flags & MMC_RSP_136)
     720                 :            :                 flags = SDHCI_CMD_RESP_LONG;
     721         [ #  # ]:          0 :         else if (cmd->flags & MMC_RSP_BUSY)
     722                 :            :                 flags = SDHCI_CMD_RESP_SHORT_BUSY;
     723                 :            :         else
     724                 :            :                 flags = SDHCI_CMD_RESP_SHORT;
     725                 :            : 
     726         [ #  # ]:          0 :         if (cmd->flags & MMC_RSP_CRC)
     727                 :          0 :                 flags |= SDHCI_CMD_CRC;
     728         [ #  # ]:          0 :         if (cmd->flags & MMC_RSP_OPCODE)
     729                 :          0 :                 flags |= SDHCI_CMD_INDEX;
     730                 :            : 
     731         [ #  # ]:          0 :         if (cmd->data)
     732                 :          0 :                 flags |= SDHCI_CMD_DATA;
     733                 :            : 
     734                 :          0 :         bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
     735                 :            : }
     736                 :            : 
     737                 :            : 
     738                 :          0 : static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
     739                 :            : {
     740                 :            :         struct mmc_data *data;
     741                 :            : 
     742         [ #  # ]:          0 :         BUG_ON(!host->data);
     743                 :            : 
     744                 :            :         data = host->data;
     745                 :          0 :         host->data = NULL;
     746                 :            : 
     747         [ #  # ]:          0 :         if (data->error)
     748                 :          0 :                 data->bytes_xfered = 0;
     749                 :            :         else
     750                 :          0 :                 data->bytes_xfered = data->blksz * data->blocks;
     751                 :            : 
     752                 :            :         /*
     753                 :            :          * Need to send CMD12 if -
     754                 :            :          * a) open-ended multiblock transfer (no CMD23)
     755                 :            :          * b) error in multiblock transfer
     756                 :            :          */
     757   [ #  #  #  # ]:          0 :         if (data->stop &&
     758         [ #  # ]:          0 :             (data->error ||
     759                 :          0 :              !host->mrq->sbc)) {
     760                 :            : 
     761                 :            :                 /*
     762                 :            :                  * The controller needs a reset of internal state machines
     763                 :            :                  * upon error conditions.
     764                 :            :                  */
     765         [ #  # ]:          0 :                 if (data->error) {
     766                 :          0 :                         bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
     767                 :          0 :                         bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
     768                 :            :                 }
     769                 :            : 
     770                 :          0 :                 bcm2835_mmc_send_command(host, data->stop);
     771         [ #  # ]:          0 :         } else if (host->use_dma) {
     772                 :          0 :                 host->wait_for_dma = true;
     773                 :            :         } else {
     774                 :          0 :                 tasklet_schedule(&host->finish_tasklet);
     775                 :            :         }
     776                 :          0 : }
     777                 :            : 
     778                 :          0 : static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
     779                 :            : {
     780                 :            :         int i;
     781                 :            : 
     782         [ #  # ]:          0 :         BUG_ON(host->cmd == NULL);
     783                 :            : 
     784         [ #  # ]:          0 :         if (host->cmd->flags & MMC_RSP_PRESENT) {
     785         [ #  # ]:          0 :                 if (host->cmd->flags & MMC_RSP_136) {
     786                 :            :                         /* CRC is stripped so we need to do some shifting. */
     787         [ #  # ]:          0 :                         for (i = 0; i < 4; i++) {
     788                 :          0 :                                 host->cmd->resp[i] = bcm2835_mmc_readl(host,
     789                 :          0 :                                         SDHCI_RESPONSE + (3-i)*4) << 8;
     790         [ #  # ]:          0 :                                 if (i != 3)
     791                 :          0 :                                         host->cmd->resp[i] |=
     792                 :          0 :                                                 bcm2835_mmc_readb(host,
     793                 :            :                                                 SDHCI_RESPONSE + (3-i)*4-1);
     794                 :            :                         }
     795                 :            :                 } else {
     796                 :          0 :                         host->cmd->resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
     797                 :            :                 }
     798                 :            :         }
     799                 :            : 
     800                 :          0 :         host->cmd->error = 0;
     801                 :            : 
     802                 :            :         /* Finished CMD23, now send actual command. */
     803         [ #  # ]:          0 :         if (host->cmd == host->mrq->sbc) {
     804                 :          0 :                 host->cmd = NULL;
     805                 :          0 :                 bcm2835_mmc_send_command(host, host->mrq->cmd);
     806                 :            : 
     807   [ #  #  #  # ]:          0 :                 if (host->mrq->cmd->data && host->use_dma) {
     808                 :            :                         /* DMA transfer starts now, PIO starts after interrupt */
     809                 :          0 :                         bcm2835_mmc_transfer_dma(host);
     810                 :            :                 }
     811                 :            :         } else {
     812                 :            : 
     813                 :            :                 /* Processed actual command. */
     814   [ #  #  #  # ]:          0 :                 if (host->data && host->data_early)
     815                 :          0 :                         bcm2835_mmc_finish_data(host);
     816                 :            : 
     817         [ #  # ]:          0 :                 if (!host->cmd->data)
     818                 :          0 :                         tasklet_schedule(&host->finish_tasklet);
     819                 :            : 
     820                 :          0 :                 host->cmd = NULL;
     821                 :            :         }
     822                 :          0 : }
     823                 :            : 
     824                 :            : 
     825                 :          0 : static void bcm2835_mmc_timeout_timer(struct timer_list *t)
     826                 :            : {
     827                 :          0 :         struct bcm2835_host *host = from_timer(host, t, timer);
     828                 :            :         unsigned long flags;
     829                 :            : 
     830                 :          0 :         spin_lock_irqsave(&host->lock, flags);
     831                 :            : 
     832         [ #  # ]:          0 :         if (host->mrq) {
     833                 :          0 :                 pr_err("%s: Timeout waiting for hardware interrupt.\n",
     834                 :            :                         mmc_hostname(host->mmc));
     835                 :            :                 bcm2835_mmc_dumpregs(host);
     836                 :            : 
     837         [ #  # ]:          0 :                 if (host->data) {
     838                 :          0 :                         host->data->error = -ETIMEDOUT;
     839                 :          0 :                         bcm2835_mmc_finish_data(host);
     840                 :            :                 } else {
     841         [ #  # ]:          0 :                         if (host->cmd)
     842                 :          0 :                                 host->cmd->error = -ETIMEDOUT;
     843                 :            :                         else
     844                 :          0 :                                 host->mrq->cmd->error = -ETIMEDOUT;
     845                 :            : 
     846                 :          0 :                         tasklet_schedule(&host->finish_tasklet);
     847                 :            :                 }
     848                 :            :         }
     849                 :            : 
     850                 :            :         spin_unlock_irqrestore(&host->lock, flags);
     851                 :          0 : }
     852                 :            : 
     853                 :            : 
     854                 :          0 : static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
     855                 :            : {
     856         [ #  # ]:          0 :         if (!(host->flags & SDHCI_DEVICE_DEAD)) {
     857         [ #  # ]:          0 :                 if (enable)
     858                 :          0 :                         host->ier |= SDHCI_INT_CARD_INT;
     859                 :            :                 else
     860                 :          0 :                         host->ier &= ~SDHCI_INT_CARD_INT;
     861                 :            : 
     862                 :          0 :                 bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 7);
     863                 :          0 :                 bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 7);
     864                 :            :         }
     865                 :          0 : }
     866                 :            : 
     867                 :          0 : static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
     868                 :            : {
     869                 :            :         struct bcm2835_host *host = mmc_priv(mmc);
     870                 :            :         unsigned long flags;
     871                 :            : 
     872                 :          0 :         spin_lock_irqsave(&host->lock, flags);
     873         [ #  # ]:          0 :         if (enable)
     874                 :          0 :                 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
     875                 :            :         else
     876                 :          0 :                 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
     877                 :            : 
     878                 :          0 :         bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
     879                 :            :         spin_unlock_irqrestore(&host->lock, flags);
     880                 :          0 : }
     881                 :            : 
     882                 :          0 : static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
     883                 :            : {
     884                 :            : 
     885         [ #  # ]:          0 :         BUG_ON(intmask == 0);
     886                 :            : 
     887         [ #  # ]:          0 :         if (!host->cmd) {
     888                 :          0 :                 pr_err("%s: Got command interrupt 0x%08x even "
     889                 :            :                         "though no command operation was in progress.\n",
     890                 :            :                         mmc_hostname(host->mmc), (unsigned)intmask);
     891                 :            :                 bcm2835_mmc_dumpregs(host);
     892                 :            :                 return;
     893                 :            :         }
     894                 :            : 
     895         [ #  # ]:          0 :         if (intmask & SDHCI_INT_TIMEOUT)
     896                 :          0 :                 host->cmd->error = -ETIMEDOUT;
     897         [ #  # ]:          0 :         else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
     898                 :            :                         SDHCI_INT_INDEX)) {
     899                 :          0 :                         host->cmd->error = -EILSEQ;
     900                 :            :         }
     901                 :            : 
     902         [ #  # ]:          0 :         if (host->cmd->error) {
     903                 :          0 :                 tasklet_schedule(&host->finish_tasklet);
     904                 :          0 :                 return;
     905                 :            :         }
     906                 :            : 
     907         [ #  # ]:          0 :         if (intmask & SDHCI_INT_RESPONSE)
     908                 :          0 :                 bcm2835_mmc_finish_command(host);
     909                 :            : 
     910                 :            : }
     911                 :            : 
     912                 :          0 : static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
     913                 :            : {
     914                 :            :         struct dma_chan *dma_chan;
     915                 :            :         u32 dir_data;
     916                 :            : 
     917         [ #  # ]:          0 :         BUG_ON(intmask == 0);
     918                 :            : 
     919         [ #  # ]:          0 :         if (!host->data) {
     920                 :            :                 /*
     921                 :            :                  * The "data complete" interrupt is also used to
     922                 :            :                  * indicate that a busy state has ended. See comment
     923                 :            :                  * above in sdhci_cmd_irq().
     924                 :            :                  */
     925   [ #  #  #  # ]:          0 :                 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
     926         [ #  # ]:          0 :                         if (intmask & SDHCI_INT_DATA_END) {
     927                 :          0 :                                 bcm2835_mmc_finish_command(host);
     928                 :          0 :                                 return;
     929                 :            :                         }
     930                 :            :                 }
     931                 :            : 
     932                 :            :                 pr_debug("%s: Got data interrupt 0x%08x even "
     933                 :            :                         "though no data operation was in progress.\n",
     934                 :            :                         mmc_hostname(host->mmc), (unsigned)intmask);
     935                 :            :                 bcm2835_mmc_dumpregs(host);
     936                 :            : 
     937                 :            :                 return;
     938                 :            :         }
     939                 :            : 
     940         [ #  # ]:          0 :         if (intmask & SDHCI_INT_DATA_TIMEOUT)
     941                 :          0 :                 host->data->error = -ETIMEDOUT;
     942         [ #  # ]:          0 :         else if (intmask & SDHCI_INT_DATA_END_BIT)
     943                 :          0 :                 host->data->error = -EILSEQ;
     944   [ #  #  #  # ]:          0 :         else if ((intmask & SDHCI_INT_DATA_CRC) &&
     945                 :          0 :                 SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
     946                 :            :                         != MMC_BUS_TEST_R)
     947                 :          0 :                 host->data->error = -EILSEQ;
     948                 :            : 
     949         [ #  # ]:          0 :         if (host->use_dma) {
     950         [ #  # ]:          0 :                 if  (host->data->flags & MMC_DATA_WRITE) {
     951                 :            :                         /* IRQ handled here */
     952                 :            : 
     953                 :          0 :                         dma_chan = host->dma_chan_rxtx;
     954                 :            :                         dir_data = DMA_TO_DEVICE;
     955                 :          0 :                         dma_unmap_sg(dma_chan->device->dev,
     956                 :            :                                  host->data->sg, host->data->sg_len,
     957                 :            :                                  dir_data);
     958                 :            : 
     959                 :          0 :                         bcm2835_mmc_finish_data(host);
     960                 :            :                 }
     961                 :            : 
     962                 :            :         } else {
     963         [ #  # ]:          0 :                 if (host->data->error)
     964                 :          0 :                         bcm2835_mmc_finish_data(host);
     965                 :            :                 else {
     966         [ #  # ]:          0 :                         if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
     967                 :          0 :                                 bcm2835_mmc_transfer_pio(host);
     968                 :            : 
     969         [ #  # ]:          0 :                         if (intmask & SDHCI_INT_DATA_END) {
     970         [ #  # ]:          0 :                                 if (host->cmd) {
     971                 :            :                                         /*
     972                 :            :                                          * Data managed to finish before the
     973                 :            :                                          * command completed. Make sure we do
     974                 :            :                                          * things in the proper order.
     975                 :            :                                          */
     976                 :          0 :                                         host->data_early = 1;
     977                 :            :                                 } else {
     978                 :          0 :                                         bcm2835_mmc_finish_data(host);
     979                 :            :                                 }
     980                 :            :                         }
     981                 :            :                 }
     982                 :            :         }
     983                 :            : }
     984                 :            : 
     985                 :            : 
     986                 :          0 : static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
     987                 :            : {
     988                 :            :         irqreturn_t result = IRQ_NONE;
     989                 :            :         struct bcm2835_host *host = dev_id;
     990                 :            :         u32 intmask, mask, unexpected = 0;
     991                 :            :         int max_loops = 16;
     992                 :            : 
     993                 :            :         spin_lock(&host->lock);
     994                 :            : 
     995                 :            :         intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
     996                 :            : 
     997         [ #  # ]:          0 :         if (!intmask || intmask == 0xffffffff) {
     998                 :            :                 result = IRQ_NONE;
     999                 :            :                 goto out;
    1000                 :            :         }
    1001                 :            : 
    1002                 :            :         do {
    1003                 :            :                 /* Clear selected interrupts. */
    1004                 :          0 :                 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
    1005                 :            :                                   SDHCI_INT_BUS_POWER);
    1006                 :          0 :                 bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS, 8);
    1007                 :            : 
    1008                 :            : 
    1009         [ #  # ]:          0 :                 if (intmask & SDHCI_INT_CMD_MASK)
    1010                 :          0 :                         bcm2835_mmc_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
    1011                 :            : 
    1012         [ #  # ]:          0 :                 if (intmask & SDHCI_INT_DATA_MASK)
    1013                 :          0 :                         bcm2835_mmc_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
    1014                 :            : 
    1015         [ #  # ]:          0 :                 if (intmask & SDHCI_INT_BUS_POWER)
    1016                 :          0 :                         pr_err("%s: Card is consuming too much power!\n",
    1017                 :            :                                 mmc_hostname(host->mmc));
    1018                 :            : 
    1019         [ #  # ]:          0 :                 if (intmask & SDHCI_INT_CARD_INT) {
    1020                 :          0 :                         bcm2835_mmc_enable_sdio_irq_nolock(host, false);
    1021                 :          0 :                         sdio_signal_irq(host->mmc);
    1022                 :            :                 }
    1023                 :            : 
    1024                 :          0 :                 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
    1025                 :            :                              SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
    1026                 :            :                              SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
    1027                 :            :                              SDHCI_INT_CARD_INT);
    1028                 :            : 
    1029         [ #  # ]:          0 :                 if (intmask) {
    1030                 :          0 :                         unexpected |= intmask;
    1031                 :          0 :                         bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS, 9);
    1032                 :            :                 }
    1033                 :            : 
    1034         [ #  # ]:          0 :                 if (result == IRQ_NONE)
    1035                 :            :                         result = IRQ_HANDLED;
    1036                 :            : 
    1037                 :            :                 intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
    1038   [ #  #  #  # ]:          0 :         } while (intmask && --max_loops);
    1039                 :            : out:
    1040                 :            :         spin_unlock(&host->lock);
    1041                 :            : 
    1042         [ #  # ]:          0 :         if (unexpected) {
    1043                 :          0 :                 pr_err("%s: Unexpected interrupt 0x%08x.\n",
    1044                 :            :                            mmc_hostname(host->mmc), unexpected);
    1045                 :            :                 bcm2835_mmc_dumpregs(host);
    1046                 :            :         }
    1047                 :            : 
    1048                 :          0 :         return result;
    1049                 :            : }
    1050                 :            : 
    1051                 :            : 
    1052                 :          0 : static void bcm2835_mmc_ack_sdio_irq(struct mmc_host *mmc)
    1053                 :            : {
    1054                 :            :         struct bcm2835_host *host = mmc_priv(mmc);
    1055                 :            :         unsigned long flags;
    1056                 :            : 
    1057                 :          0 :         spin_lock_irqsave(&host->lock, flags);
    1058         [ #  # ]:          0 :         if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
    1059                 :          0 :                 bcm2835_mmc_enable_sdio_irq_nolock(host, true);
    1060                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1061                 :          0 : }
    1062                 :            : 
    1063                 :          0 : void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
    1064                 :            : {
    1065                 :            :         int div = 0; /* Initialized for compiler warning */
    1066                 :            :         int real_div = div, clk_mul = 1;
    1067                 :            :         u16 clk = 0;
    1068                 :            :         unsigned long timeout;
    1069                 :            :         unsigned int input_clock = clock;
    1070                 :            : 
    1071   [ #  #  #  # ]:          0 :         if (host->overclock_50 && (clock == 50000000))
    1072                 :          0 :                 clock = host->overclock_50 * 1000000 + 999999;
    1073                 :            : 
    1074                 :          0 :         host->mmc->actual_clock = 0;
    1075                 :            : 
    1076                 :          0 :         bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
    1077                 :            : 
    1078         [ #  # ]:          0 :         if (clock == 0)
    1079                 :            :                 return;
    1080                 :            : 
    1081                 :            :         /* Version 3.00 divisors must be a multiple of 2. */
    1082         [ #  # ]:          0 :         if (host->max_clk <= clock)
    1083                 :            :                 div = 1;
    1084                 :            :         else {
    1085         [ #  # ]:          0 :                 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
    1086                 :          0 :                          div += 2) {
    1087         [ #  # ]:          0 :                         if ((host->max_clk / div) <= clock)
    1088                 :            :                                 break;
    1089                 :            :                 }
    1090                 :            :         }
    1091                 :            : 
    1092                 :            :         real_div = div;
    1093                 :          0 :         div >>= 1;
    1094                 :            : 
    1095         [ #  # ]:          0 :         if (real_div)
    1096                 :          0 :                 clock = (host->max_clk * clk_mul) / real_div;
    1097                 :          0 :         host->mmc->actual_clock = clock;
    1098                 :            : 
    1099   [ #  #  #  # ]:          0 :         if ((clock > input_clock) && (clock > host->max_overclock)) {
    1100                 :          0 :                 pr_warn("%s: Overclocking to %dHz\n",
    1101                 :            :                         mmc_hostname(host->mmc), clock);
    1102                 :          0 :                 host->max_overclock = clock;
    1103                 :            :         }
    1104                 :            : 
    1105                 :          0 :         clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
    1106                 :          0 :         clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
    1107                 :          0 :                 << SDHCI_DIVIDER_HI_SHIFT;
    1108                 :          0 :         clk |= SDHCI_CLOCK_INT_EN;
    1109                 :          0 :         bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
    1110                 :            : 
    1111                 :            :         /* Wait max 20 ms */
    1112                 :            :         timeout = 20;
    1113         [ #  # ]:          0 :         while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
    1114                 :          0 :                 & SDHCI_CLOCK_INT_STABLE)) {
    1115         [ #  # ]:          0 :                 if (timeout == 0) {
    1116                 :          0 :                         pr_err("%s: Internal clock never "
    1117                 :            :                                 "stabilised.\n", mmc_hostname(host->mmc));
    1118                 :            :                         bcm2835_mmc_dumpregs(host);
    1119                 :            :                         return;
    1120                 :            :                 }
    1121                 :          0 :                 timeout--;
    1122                 :          0 :                 mdelay(1);
    1123                 :            :         }
    1124                 :            : 
    1125   [ #  #  #  # ]:          0 :         if (20-timeout > 10 && 20-timeout > host->max_delay) {
    1126                 :          0 :                 host->max_delay = 20-timeout;
    1127                 :          0 :                 pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
    1128                 :            :         }
    1129                 :            : 
    1130                 :          0 :         clk |= SDHCI_CLOCK_CARD_EN;
    1131                 :          0 :         bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
    1132                 :            : }
    1133                 :            : 
    1134                 :          0 : static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
    1135                 :            : {
    1136                 :            :         struct bcm2835_host *host;
    1137                 :            :         unsigned long flags;
    1138                 :            : 
    1139                 :            :         host = mmc_priv(mmc);
    1140                 :            : 
    1141                 :          0 :         spin_lock_irqsave(&host->lock, flags);
    1142                 :            : 
    1143         [ #  # ]:          0 :         WARN_ON(host->mrq != NULL);
    1144                 :            : 
    1145                 :          0 :         host->mrq = mrq;
    1146                 :            : 
    1147   [ #  #  #  # ]:          0 :         if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
    1148                 :          0 :                 bcm2835_mmc_send_command(host, mrq->sbc);
    1149                 :            :         else
    1150                 :          0 :                 bcm2835_mmc_send_command(host, mrq->cmd);
    1151                 :            : 
    1152                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1153                 :            : 
    1154   [ #  #  #  #  :          0 :         if (!(mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23)) && mrq->cmd->data && host->use_dma) {
             #  #  #  # ]
    1155                 :            :                 /* DMA transfer starts now, PIO starts after interrupt */
    1156                 :          0 :                 bcm2835_mmc_transfer_dma(host);
    1157                 :            :         }
    1158                 :          0 : }
    1159                 :            : 
    1160                 :            : 
    1161                 :          0 : static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
    1162                 :            : {
    1163                 :            : 
    1164                 :            :         struct bcm2835_host *host = mmc_priv(mmc);
    1165                 :            :         unsigned long flags;
    1166                 :            :         u8 ctrl;
    1167                 :            :         u16 clk, ctrl_2;
    1168                 :            : 
    1169                 :            :         pr_debug("bcm2835_mmc_set_ios: clock %d, pwr %d, bus_width %d, timing %d, vdd %d, drv_type %d\n",
    1170                 :            :                  ios->clock, ios->power_mode, ios->bus_width,
    1171                 :            :                  ios->timing, ios->signal_voltage, ios->drv_type);
    1172                 :            : 
    1173                 :          0 :         spin_lock_irqsave(&host->lock, flags);
    1174                 :            : 
    1175   [ #  #  #  # ]:          0 :         if (!ios->clock || ios->clock != host->clock) {
    1176                 :          0 :                 bcm2835_mmc_set_clock(host, ios->clock);
    1177                 :          0 :                 host->clock = ios->clock;
    1178                 :            :         }
    1179                 :            : 
    1180         [ #  # ]:          0 :         if (host->pwr != SDHCI_POWER_330) {
    1181                 :          0 :                 host->pwr = SDHCI_POWER_330;
    1182                 :          0 :                 bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
    1183                 :            :         }
    1184                 :            : 
    1185                 :            :         ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
    1186                 :            : 
    1187                 :            :         /* set bus width */
    1188                 :          0 :         ctrl &= ~SDHCI_CTRL_8BITBUS;
    1189         [ #  # ]:          0 :         if (ios->bus_width == MMC_BUS_WIDTH_4)
    1190                 :          0 :                 ctrl |= SDHCI_CTRL_4BITBUS;
    1191                 :            :         else
    1192                 :          0 :                 ctrl &= ~SDHCI_CTRL_4BITBUS;
    1193                 :            : 
    1194                 :          0 :         ctrl &= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
    1195                 :            : 
    1196                 :            : 
    1197                 :          0 :         bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
    1198                 :            :         /*
    1199                 :            :          * We only need to set Driver Strength if the
    1200                 :            :          * preset value enable is not set.
    1201                 :            :          */
    1202                 :            :         ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
    1203                 :          0 :         ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
    1204         [ #  # ]:          0 :         if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
    1205                 :          0 :                 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
    1206         [ #  # ]:          0 :         else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
    1207                 :          0 :                 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
    1208                 :            : 
    1209                 :          0 :         bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
    1210                 :            : 
    1211                 :            :         /* Reset SD Clock Enable */
    1212                 :            :         clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
    1213                 :          0 :         clk &= ~SDHCI_CLOCK_CARD_EN;
    1214                 :          0 :         bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
    1215                 :            : 
    1216                 :            :         /* Re-enable SD Clock */
    1217                 :          0 :         bcm2835_mmc_set_clock(host, host->clock);
    1218                 :          0 :         bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
    1219                 :            : 
    1220                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1221                 :          0 : }
    1222                 :            : 
    1223                 :            : 
    1224                 :            : static struct mmc_host_ops bcm2835_ops = {
    1225                 :            :         .request = bcm2835_mmc_request,
    1226                 :            :         .set_ios = bcm2835_mmc_set_ios,
    1227                 :            :         .enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
    1228                 :            :         .ack_sdio_irq = bcm2835_mmc_ack_sdio_irq,
    1229                 :            : };
    1230                 :            : 
    1231                 :            : 
    1232                 :          0 : static void bcm2835_mmc_tasklet_finish(unsigned long param)
    1233                 :            : {
    1234                 :            :         struct bcm2835_host *host;
    1235                 :            :         unsigned long flags;
    1236                 :            :         struct mmc_request *mrq;
    1237                 :            : 
    1238                 :          0 :         host = (struct bcm2835_host *)param;
    1239                 :            : 
    1240                 :          0 :         spin_lock_irqsave(&host->lock, flags);
    1241                 :            : 
    1242                 :            :         /*
    1243                 :            :          * If this tasklet gets rescheduled while running, it will
    1244                 :            :          * be run again afterwards but without any active request.
    1245                 :            :          */
    1246         [ #  # ]:          0 :         if (!host->mrq) {
    1247                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
    1248                 :          0 :                 return;
    1249                 :            :         }
    1250                 :            : 
    1251                 :          0 :         del_timer(&host->timer);
    1252                 :            : 
    1253                 :          0 :         mrq = host->mrq;
    1254                 :            : 
    1255                 :            :         /*
    1256                 :            :          * The controller needs a reset of internal state machines
    1257                 :            :          * upon error conditions.
    1258                 :            :          */
    1259   [ #  #  #  # ]:          0 :         if (!(host->flags & SDHCI_DEVICE_DEAD) &&
    1260   [ #  #  #  # ]:          0 :             ((mrq->cmd && mrq->cmd->error) ||
    1261   [ #  #  #  # ]:          0 :                  (mrq->data && (mrq->data->error ||
    1262         [ #  # ]:          0 :                   (mrq->data->stop && mrq->data->stop->error))))) {
    1263                 :            : 
    1264                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
    1265                 :          0 :                 bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
    1266                 :          0 :                 bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
    1267                 :          0 :                 spin_lock_irqsave(&host->lock, flags);
    1268                 :            :         }
    1269                 :            : 
    1270                 :          0 :         host->mrq = NULL;
    1271                 :          0 :         host->cmd = NULL;
    1272                 :          0 :         host->data = NULL;
    1273                 :            : 
    1274                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1275                 :          0 :         mmc_request_done(host->mmc, mrq);
    1276                 :            : }
    1277                 :            : 
    1278                 :            : 
    1279                 :            : 
    1280                 :          0 : static int bcm2835_mmc_add_host(struct bcm2835_host *host)
    1281                 :            : {
    1282                 :          0 :         struct mmc_host *mmc = host->mmc;
    1283                 :          0 :         struct device *dev = mmc->parent;
    1284                 :            : #ifndef FORCE_PIO
    1285                 :            :         struct dma_slave_config cfg;
    1286                 :            : #endif
    1287                 :            :         int ret;
    1288                 :            : 
    1289                 :          0 :         bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
    1290                 :            : 
    1291                 :          0 :         host->clk_mul = 0;
    1292                 :            : 
    1293   [ #  #  #  # ]:          0 :         if (!mmc->f_max || mmc->f_max > host->max_clk)
    1294                 :          0 :                 mmc->f_max = host->max_clk;
    1295                 :          0 :         mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
    1296                 :            : 
    1297                 :            :         /* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
    1298                 :          0 :         host->timeout_clk = mmc->f_max / 1000;
    1299                 :          0 :         mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
    1300                 :            : 
    1301                 :            :         /* host controller capabilities */
    1302                 :          0 :         mmc->caps |= MMC_CAP_CMD23 | MMC_CAP_ERASE | MMC_CAP_NEEDS_POLL |
    1303                 :            :                 MMC_CAP_SDIO_IRQ | MMC_CAP_SD_HIGHSPEED |
    1304                 :            :                 MMC_CAP_MMC_HIGHSPEED;
    1305                 :            : 
    1306                 :          0 :         mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
    1307                 :            : 
    1308                 :          0 :         host->flags = SDHCI_AUTO_CMD23;
    1309                 :            : 
    1310                 :          0 :         dev_info(dev, "mmc_debug:%x mmc_debug2:%x\n", mmc_debug, mmc_debug2);
    1311                 :            : #ifdef FORCE_PIO
    1312                 :            :         dev_info(dev, "Forcing PIO mode\n");
    1313                 :            :         host->have_dma = false;
    1314                 :            : #else
    1315         [ #  # ]:          0 :         if (IS_ERR_OR_NULL(host->dma_chan_rxtx)) {
    1316                 :          0 :                 dev_err(dev, "%s: Unable to initialise DMA channel. Falling back to PIO\n",
    1317                 :            :                         DRIVER_NAME);
    1318                 :          0 :                 host->have_dma = false;
    1319                 :            :         } else {
    1320                 :          0 :                 dev_info(dev, "DMA channel allocated");
    1321                 :            : 
    1322                 :          0 :                 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
    1323                 :          0 :                 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
    1324                 :          0 :                 cfg.slave_id = 11;              /* DREQ channel */
    1325                 :            : 
    1326                 :            :                 /* Validate the slave configurations */
    1327                 :            : 
    1328                 :          0 :                 cfg.direction = DMA_MEM_TO_DEV;
    1329                 :          0 :                 cfg.src_addr = 0;
    1330                 :          0 :                 cfg.dst_addr = host->bus_addr + SDHCI_BUFFER;
    1331                 :            : 
    1332                 :          0 :                 ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
    1333                 :            : 
    1334         [ #  # ]:          0 :                 if (ret == 0) {
    1335                 :          0 :                         host->dma_cfg_tx = cfg;
    1336                 :            : 
    1337                 :          0 :                         cfg.direction = DMA_DEV_TO_MEM;
    1338                 :          0 :                         cfg.src_addr = host->bus_addr + SDHCI_BUFFER;
    1339                 :          0 :                         cfg.dst_addr = 0;
    1340                 :            : 
    1341                 :          0 :                         ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
    1342                 :            :                 }
    1343                 :            : 
    1344         [ #  # ]:          0 :                 if (ret == 0) {
    1345                 :          0 :                         host->dma_cfg_rx = cfg;
    1346                 :            : 
    1347                 :          0 :                         host->have_dma = true;
    1348                 :            :                 } else {
    1349                 :          0 :                         pr_err("%s: unable to configure DMA channel. "
    1350                 :            :                                "Falling back to PIO\n",
    1351                 :            :                                mmc_hostname(mmc));
    1352                 :          0 :                         dma_release_channel(host->dma_chan_rxtx);
    1353                 :          0 :                         host->dma_chan_rxtx = NULL;
    1354                 :          0 :                         host->have_dma = false;
    1355                 :            :                 }
    1356                 :            :         }
    1357                 :            : #endif
    1358                 :          0 :         mmc->max_segs = 128;
    1359                 :            :         if (swiotlb_max_segment())
    1360                 :            :                 mmc->max_req_size = (1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE;
    1361                 :            :         else
    1362                 :          0 :                 mmc->max_req_size = 524288;
    1363                 :          0 :         mmc->max_seg_size = mmc->max_req_size;
    1364                 :          0 :         mmc->max_blk_size = 512;
    1365                 :          0 :         mmc->max_blk_count =  65535;
    1366                 :            : 
    1367                 :            :         /* report supported voltage ranges */
    1368                 :          0 :         mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
    1369                 :            : 
    1370                 :          0 :         tasklet_init(&host->finish_tasklet,
    1371                 :            :                 bcm2835_mmc_tasklet_finish, (unsigned long)host);
    1372                 :            : 
    1373                 :          0 :         timer_setup(&host->timer, bcm2835_mmc_timeout_timer, 0);
    1374                 :          0 :         init_waitqueue_head(&host->buf_ready_int);
    1375                 :            : 
    1376                 :          0 :         bcm2835_mmc_init(host, 0);
    1377                 :          0 :         ret = request_irq(host->irq, bcm2835_mmc_irq, IRQF_SHARED,
    1378                 :            :                                    mmc_hostname(mmc), host);
    1379         [ #  # ]:          0 :         if (ret) {
    1380                 :          0 :                 dev_err(dev, "Failed to request IRQ %d: %d\n", host->irq, ret);
    1381                 :          0 :                 goto untasklet;
    1382                 :            :         }
    1383                 :            : 
    1384                 :          0 :         ret = mmc_add_host(mmc);
    1385         [ #  # ]:          0 :         if (ret) {
    1386                 :          0 :                 dev_err(dev, "could not add MMC host\n");
    1387                 :            :                 goto free_irq;
    1388                 :            :         }
    1389                 :            : 
    1390                 :            :         return 0;
    1391                 :            : 
    1392                 :            : free_irq:
    1393                 :          0 :         free_irq(host->irq, host);
    1394                 :            : untasklet:
    1395                 :          0 :         tasklet_kill(&host->finish_tasklet);
    1396                 :            : 
    1397                 :          0 :         return ret;
    1398                 :            : }
    1399                 :            : 
    1400                 :          0 : static int bcm2835_mmc_probe(struct platform_device *pdev)
    1401                 :            : {
    1402                 :          0 :         struct device *dev = &pdev->dev;
    1403                 :          0 :         struct device_node *node = dev->of_node;
    1404                 :            :         struct clk *clk;
    1405                 :            :         struct resource *iomem;
    1406                 :            :         struct bcm2835_host *host;
    1407                 :            :         struct mmc_host *mmc;
    1408                 :            :         const __be32 *addr;
    1409                 :            :         int ret;
    1410                 :            : 
    1411                 :          0 :         mmc = mmc_alloc_host(sizeof(*host), dev);
    1412         [ #  # ]:          0 :         if (!mmc)
    1413                 :            :                 return -ENOMEM;
    1414                 :            : 
    1415                 :          0 :         mmc->ops = &bcm2835_ops;
    1416                 :            :         host = mmc_priv(mmc);
    1417                 :          0 :         host->mmc = mmc;
    1418                 :          0 :         host->timeout = msecs_to_jiffies(1000);
    1419                 :          0 :         spin_lock_init(&host->lock);
    1420                 :            : 
    1421                 :          0 :         iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    1422                 :          0 :         host->ioaddr = devm_ioremap_resource(dev, iomem);
    1423         [ #  # ]:          0 :         if (IS_ERR(host->ioaddr)) {
    1424                 :            :                 ret = PTR_ERR(host->ioaddr);
    1425                 :          0 :                 goto err;
    1426                 :            :         }
    1427                 :            : 
    1428                 :          0 :         addr = of_get_address(node, 0, NULL, NULL);
    1429         [ #  # ]:          0 :         if (!addr) {
    1430                 :          0 :                 dev_err(dev, "could not get DMA-register address\n");
    1431                 :            :                 ret = -ENODEV;
    1432                 :          0 :                 goto err;
    1433                 :            :         }
    1434                 :          0 :         host->bus_addr = be32_to_cpup(addr);
    1435                 :            :         pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
    1436                 :            :                  (unsigned long)host->ioaddr,
    1437                 :            :                  (unsigned long)iomem->start,
    1438                 :            :                  (unsigned long)host->bus_addr);
    1439                 :            : 
    1440                 :            : #ifndef FORCE_PIO
    1441         [ #  # ]:          0 :         if (node) {
    1442                 :          0 :                 host->dma_chan_rxtx = dma_request_slave_channel(dev, "rx-tx");
    1443         [ #  # ]:          0 :                 if (!host->dma_chan_rxtx)
    1444                 :          0 :                         host->dma_chan_rxtx =
    1445                 :          0 :                                 dma_request_slave_channel(dev, "tx");
    1446         [ #  # ]:          0 :                 if (!host->dma_chan_rxtx)
    1447                 :          0 :                         host->dma_chan_rxtx =
    1448                 :          0 :                                 dma_request_slave_channel(dev, "rx");
    1449                 :            :         } else {
    1450                 :            :                 dma_cap_mask_t mask;
    1451                 :            : 
    1452                 :            :                 dma_cap_zero(mask);
    1453                 :            :                 /* we don't care about the channel, any would work */
    1454                 :            :                 dma_cap_set(DMA_SLAVE, mask);
    1455                 :          0 :                 host->dma_chan_rxtx = dma_request_channel(mask, NULL, NULL);
    1456                 :            :         }
    1457                 :            : #endif
    1458                 :          0 :         clk = devm_clk_get(dev, NULL);
    1459         [ #  # ]:          0 :         if (IS_ERR(clk)) {
    1460                 :            :                 ret = PTR_ERR(clk);
    1461         [ #  # ]:          0 :                 if (ret == -EPROBE_DEFER)
    1462                 :          0 :                         dev_info(dev, "could not get clk, deferring probe\n");
    1463                 :            :                 else
    1464                 :          0 :                         dev_err(dev, "could not get clk\n");
    1465                 :            :                 goto err;
    1466                 :            :         }
    1467                 :            : 
    1468                 :          0 :         host->max_clk = clk_get_rate(clk);
    1469                 :            : 
    1470                 :          0 :         host->irq = platform_get_irq(pdev, 0);
    1471         [ #  # ]:          0 :         if (host->irq <= 0) {
    1472                 :          0 :                 dev_err(dev, "get IRQ failed\n");
    1473                 :            :                 ret = -EINVAL;
    1474                 :          0 :                 goto err;
    1475                 :            :         }
    1476                 :            : 
    1477         [ #  # ]:          0 :         if (node) {
    1478                 :          0 :                 mmc_of_parse(mmc);
    1479                 :            : 
    1480                 :            :                 /* Read any custom properties */
    1481                 :          0 :                 of_property_read_u32(node,
    1482                 :            :                                      "brcm,overclock-50",
    1483                 :            :                                      &host->overclock_50);
    1484                 :            :         } else {
    1485                 :          0 :                 mmc->caps |= MMC_CAP_4_BIT_DATA;
    1486                 :            :         }
    1487                 :            : 
    1488                 :          0 :         ret = bcm2835_mmc_add_host(host);
    1489         [ #  # ]:          0 :         if (ret)
    1490                 :            :                 goto err;
    1491                 :            : 
    1492                 :            :         platform_set_drvdata(pdev, host);
    1493                 :            : 
    1494                 :          0 :         return 0;
    1495                 :            : err:
    1496         [ #  # ]:          0 :         if (host->dma_chan_rxtx)
    1497                 :          0 :                 dma_release_channel(host->dma_chan_rxtx);
    1498                 :          0 :         mmc_free_host(mmc);
    1499                 :            : 
    1500                 :          0 :         return ret;
    1501                 :            : }
    1502                 :            : 
    1503                 :          0 : static int bcm2835_mmc_remove(struct platform_device *pdev)
    1504                 :            : {
    1505                 :            :         struct bcm2835_host *host = platform_get_drvdata(pdev);
    1506                 :            :         unsigned long flags;
    1507                 :            :         int dead;
    1508                 :            :         u32 scratch;
    1509                 :            : 
    1510                 :            :         dead = 0;
    1511                 :            :         scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
    1512         [ #  # ]:          0 :         if (scratch == (u32)-1)
    1513                 :            :                 dead = 1;
    1514                 :            : 
    1515                 :            : 
    1516         [ #  # ]:          0 :         if (dead) {
    1517                 :          0 :                 spin_lock_irqsave(&host->lock, flags);
    1518                 :            : 
    1519                 :          0 :                 host->flags |= SDHCI_DEVICE_DEAD;
    1520                 :            : 
    1521         [ #  # ]:          0 :                 if (host->mrq) {
    1522                 :          0 :                         pr_err("%s: Controller removed during "
    1523                 :            :                                 " transfer!\n", mmc_hostname(host->mmc));
    1524                 :            : 
    1525                 :          0 :                         host->mrq->cmd->error = -ENOMEDIUM;
    1526                 :          0 :                         tasklet_schedule(&host->finish_tasklet);
    1527                 :            :                 }
    1528                 :            : 
    1529                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
    1530                 :            :         }
    1531                 :            : 
    1532                 :          0 :         mmc_remove_host(host->mmc);
    1533                 :            : 
    1534         [ #  # ]:          0 :         if (!dead)
    1535                 :          0 :                 bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
    1536                 :            : 
    1537                 :          0 :         free_irq(host->irq, host);
    1538                 :            : 
    1539                 :          0 :         del_timer_sync(&host->timer);
    1540                 :            : 
    1541                 :          0 :         tasklet_kill(&host->finish_tasklet);
    1542                 :            : 
    1543         [ #  # ]:          0 :         if (host->dma_chan_rxtx)
    1544                 :          0 :                 dma_release_channel(host->dma_chan_rxtx);
    1545                 :            : 
    1546                 :          0 :         mmc_free_host(host->mmc);
    1547                 :            : 
    1548                 :          0 :         return 0;
    1549                 :            : }
    1550                 :            : 
    1551                 :            : 
    1552                 :            : static const struct of_device_id bcm2835_mmc_match[] = {
    1553                 :            :         { .compatible = "brcm,bcm2835-mmc" },
    1554                 :            :         { }
    1555                 :            : };
    1556                 :            : MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
    1557                 :            : 
    1558                 :            : 
    1559                 :            : 
    1560                 :            : static struct platform_driver bcm2835_mmc_driver = {
    1561                 :            :         .probe      = bcm2835_mmc_probe,
    1562                 :            :         .remove     = bcm2835_mmc_remove,
    1563                 :            :         .driver     = {
    1564                 :            :                 .name           = DRIVER_NAME,
    1565                 :            :                 .owner          = THIS_MODULE,
    1566                 :            :                 .of_match_table = bcm2835_mmc_match,
    1567                 :            :         },
    1568                 :            : };
    1569                 :        207 : module_platform_driver(bcm2835_mmc_driver);
    1570                 :            : 
    1571                 :            : module_param(mmc_debug, uint, 0644);
    1572                 :            : module_param(mmc_debug2, uint, 0644);
    1573                 :            : MODULE_ALIAS("platform:mmc-bcm2835");
    1574                 :            : MODULE_DESCRIPTION("BCM2835 SDHCI driver");
    1575                 :            : MODULE_LICENSE("GPL v2");
    1576                 :            : MODULE_AUTHOR("Gellert Weisz");

Generated by: LCOV version 1.14