LCOV - code coverage report
Current view: top level - drivers/mmc/host - bcm2835-sdhost.c (source / functions) Hit Total Coverage
Test: Real Lines: 594 839 70.8 %
Date: 2020-10-17 15:46:16 Functions: 1 35 2.9 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * BCM2835 SD host driver.
       3                 :            :  *
       4                 :            :  * Author:      Phil Elwell <phil@raspberrypi.org>
       5                 :            :  *              Copyright (C) 2015-2016 Raspberry Pi (Trading) Ltd.
       6                 :            :  *
       7                 :            :  * Based on
       8                 :            :  *  mmc-bcm2835.c by Gellert Weisz
       9                 :            :  * which is, in turn, based on
      10                 :            :  *  sdhci-bcm2708.c by Broadcom
      11                 :            :  *  sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
      12                 :            :  *  sdhci.c and sdhci-pci.c by Pierre Ossman
      13                 :            :  *
      14                 :            :  * This program is free software; you can redistribute it and/or modify it
      15                 :            :  * under the terms and conditions of the GNU General Public License,
      16                 :            :  * version 2, as published by the Free Software Foundation.
      17                 :            :  *
      18                 :            :  * This program is distributed in the hope it will be useful, but WITHOUT
      19                 :            :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
      21                 :            :  * more details.
      22                 :            :  *
      23                 :            :  * You should have received a copy of the GNU General Public License
      24                 :            :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      25                 :            :  */
      26                 :            : 
      27                 :            : #define FIFO_READ_THRESHOLD     4
      28                 :            : #define FIFO_WRITE_THRESHOLD    4
      29                 :            : #define ALLOW_CMD23_READ        1
      30                 :            : #define ALLOW_CMD23_WRITE       0
      31                 :            : #define ENABLE_LOG              1
      32                 :            : #define SDDATA_FIFO_PIO_BURST   8
      33                 :            : #define CMD_DALLY_US            1
      34                 :            : 
      35                 :            : #include <linux/delay.h>
      36                 :            : #include <linux/module.h>
      37                 :            : #include <linux/io.h>
      38                 :            : #include <linux/mmc/mmc.h>
      39                 :            : #include <linux/mmc/host.h>
      40                 :            : #include <linux/mmc/sd.h>
      41                 :            : #include <linux/mmc/sdio.h>
      42                 :            : #include <linux/scatterlist.h>
      43                 :            : #include <linux/of_address.h>
      44                 :            : #include <linux/of_irq.h>
      45                 :            : #include <linux/clk.h>
      46                 :            : #include <linux/platform_device.h>
      47                 :            : #include <linux/err.h>
      48                 :            : #include <linux/blkdev.h>
      49                 :            : #include <linux/dmaengine.h>
      50                 :            : #include <linux/dma-mapping.h>
      51                 :            : #include <linux/of_dma.h>
      52                 :            : #include <linux/time.h>
      53                 :            : #include <linux/workqueue.h>
      54                 :            : #include <linux/interrupt.h>
      55                 :            : #include <linux/highmem.h>
      56                 :            : #include <soc/bcm2835/raspberrypi-firmware.h>
      57                 :            : 
      58                 :            : /* For mmc_card_blockaddr */
      59                 :            : #include "../core/card.h"
      60                 :            : 
      61                 :            : #define DRIVER_NAME "sdhost-bcm2835"
      62                 :            : 
      63                 :            : #define SDCMD  0x00 /* Command to SD card              - 16 R/W */
      64                 :            : #define SDARG  0x04 /* Argument to SD card             - 32 R/W */
      65                 :            : #define SDTOUT 0x08 /* Start value for timeout counter - 32 R/W */
      66                 :            : #define SDCDIV 0x0c /* Start value for clock divider   - 11 R/W */
      67                 :            : #define SDRSP0 0x10 /* SD card response (31:0)         - 32 R   */
      68                 :            : #define SDRSP1 0x14 /* SD card response (63:32)        - 32 R   */
      69                 :            : #define SDRSP2 0x18 /* SD card response (95:64)        - 32 R   */
      70                 :            : #define SDRSP3 0x1c /* SD card response (127:96)       - 32 R   */
      71                 :            : #define SDHSTS 0x20 /* SD host status                  - 11 R   */
      72                 :            : #define SDVDD  0x30 /* SD card power control           -  1 R/W */
      73                 :            : #define SDEDM  0x34 /* Emergency Debug Mode            - 13 R/W */
      74                 :            : #define SDHCFG 0x38 /* Host configuration              -  2 R/W */
      75                 :            : #define SDHBCT 0x3c /* Host byte count (debug)         - 32 R/W */
      76                 :            : #define SDDATA 0x40 /* Data to/from SD card            - 32 R/W */
      77                 :            : #define SDHBLC 0x50 /* Host block count (SDIO/SDHC)    -  9 R/W */
      78                 :            : 
      79                 :            : #define SDCMD_NEW_FLAG                  0x8000
      80                 :            : #define SDCMD_FAIL_FLAG                 0x4000
      81                 :            : #define SDCMD_BUSYWAIT                  0x800
      82                 :            : #define SDCMD_NO_RESPONSE               0x400
      83                 :            : #define SDCMD_LONG_RESPONSE             0x200
      84                 :            : #define SDCMD_WRITE_CMD                 0x80
      85                 :            : #define SDCMD_READ_CMD                  0x40
      86                 :            : #define SDCMD_CMD_MASK                  0x3f
      87                 :            : 
      88                 :            : #define SDCDIV_MAX_CDIV                 0x7ff
      89                 :            : 
      90                 :            : #define SDHSTS_BUSY_IRPT                0x400
      91                 :            : #define SDHSTS_BLOCK_IRPT               0x200
      92                 :            : #define SDHSTS_SDIO_IRPT                0x100
      93                 :            : #define SDHSTS_REW_TIME_OUT             0x80
      94                 :            : #define SDHSTS_CMD_TIME_OUT             0x40
      95                 :            : #define SDHSTS_CRC16_ERROR              0x20
      96                 :            : #define SDHSTS_CRC7_ERROR               0x10
      97                 :            : #define SDHSTS_FIFO_ERROR               0x08
      98                 :            : /* Reserved */
      99                 :            : /* Reserved */
     100                 :            : #define SDHSTS_DATA_FLAG                0x01
     101                 :            : 
     102                 :            : #define SDHSTS_TRANSFER_ERROR_MASK      (SDHSTS_CRC7_ERROR|SDHSTS_CRC16_ERROR|SDHSTS_REW_TIME_OUT|SDHSTS_FIFO_ERROR)
     103                 :            : #define SDHSTS_ERROR_MASK               (SDHSTS_CMD_TIME_OUT|SDHSTS_TRANSFER_ERROR_MASK)
     104                 :            : 
     105                 :            : #define SDHCFG_BUSY_IRPT_EN     (1<<10)
     106                 :            : #define SDHCFG_BLOCK_IRPT_EN    (1<<8)
     107                 :            : #define SDHCFG_SDIO_IRPT_EN     (1<<5)
     108                 :            : #define SDHCFG_DATA_IRPT_EN     (1<<4)
     109                 :            : #define SDHCFG_SLOW_CARD        (1<<3)
     110                 :            : #define SDHCFG_WIDE_EXT_BUS     (1<<2)
     111                 :            : #define SDHCFG_WIDE_INT_BUS     (1<<1)
     112                 :            : #define SDHCFG_REL_CMD_LINE     (1<<0)
     113                 :            : 
     114                 :            : #define SDEDM_FORCE_DATA_MODE   (1<<19)
     115                 :            : #define SDEDM_CLOCK_PULSE       (1<<20)
     116                 :            : #define SDEDM_BYPASS            (1<<21)
     117                 :            : 
     118                 :            : #define SDEDM_WRITE_THRESHOLD_SHIFT 9
     119                 :            : #define SDEDM_READ_THRESHOLD_SHIFT 14
     120                 :            : #define SDEDM_THRESHOLD_MASK     0x1f
     121                 :            : 
     122                 :            : #define SDEDM_FSM_MASK           0xf
     123                 :            : #define SDEDM_FSM_IDENTMODE      0x0
     124                 :            : #define SDEDM_FSM_DATAMODE       0x1
     125                 :            : #define SDEDM_FSM_READDATA       0x2
     126                 :            : #define SDEDM_FSM_WRITEDATA      0x3
     127                 :            : #define SDEDM_FSM_READWAIT       0x4
     128                 :            : #define SDEDM_FSM_READCRC        0x5
     129                 :            : #define SDEDM_FSM_WRITECRC       0x6
     130                 :            : #define SDEDM_FSM_WRITEWAIT1     0x7
     131                 :            : #define SDEDM_FSM_POWERDOWN      0x8
     132                 :            : #define SDEDM_FSM_POWERUP        0x9
     133                 :            : #define SDEDM_FSM_WRITESTART1    0xa
     134                 :            : #define SDEDM_FSM_WRITESTART2    0xb
     135                 :            : #define SDEDM_FSM_GENPULSES      0xc
     136                 :            : #define SDEDM_FSM_WRITEWAIT2     0xd
     137                 :            : #define SDEDM_FSM_STARTPOWDOWN   0xf
     138                 :            : 
     139                 :            : #define SDDATA_FIFO_WORDS        16
     140                 :            : 
     141                 :            : #define USE_CMD23_FLAGS          ((ALLOW_CMD23_READ * MMC_DATA_READ) | \
     142                 :            :                                   (ALLOW_CMD23_WRITE * MMC_DATA_WRITE))
     143                 :            : 
     144                 :            : #define MHZ 1000000
     145                 :            : 
     146                 :            : 
     147                 :            : struct bcm2835_host {
     148                 :            :         spinlock_t              lock;
     149                 :            : 
     150                 :            :         void __iomem            *ioaddr;
     151                 :            :         phys_addr_t             bus_addr;
     152                 :            : 
     153                 :            :         struct mmc_host         *mmc;
     154                 :            : 
     155                 :            :         u32                     pio_timeout;    /* In jiffies */
     156                 :            : 
     157                 :            :         int                     clock;          /* Current clock speed */
     158                 :            : 
     159                 :            :         bool                    slow_card;      /* Force 11-bit divisor */
     160                 :            : 
     161                 :            :         unsigned int            max_clk;        /* Max possible freq */
     162                 :            : 
     163                 :            :         struct tasklet_struct   finish_tasklet; /* Tasklet structures */
     164                 :            : 
     165                 :            :         struct work_struct      cmd_wait_wq;    /* Workqueue function */
     166                 :            : 
     167                 :            :         struct timer_list       timer;          /* Timer for timeouts */
     168                 :            : 
     169                 :            :         struct sg_mapping_iter  sg_miter;       /* SG state for PIO */
     170                 :            :         unsigned int            blocks;         /* remaining PIO blocks */
     171                 :            : 
     172                 :            :         int                     irq;            /* Device IRQ */
     173                 :            : 
     174                 :            :         u32                     cmd_quick_poll_retries;
     175                 :            :         u32                     ns_per_fifo_word;
     176                 :            : 
     177                 :            :         /* cached registers */
     178                 :            :         u32                     hcfg;
     179                 :            :         u32                     cdiv;
     180                 :            : 
     181                 :            :         struct mmc_request              *mrq;                   /* Current request */
     182                 :            :         struct mmc_command              *cmd;                   /* Current command */
     183                 :            :         struct mmc_data                 *data;                  /* Current data request */
     184                 :            :         unsigned int                    data_complete:1;        /* Data finished before cmd */
     185                 :            : 
     186                 :            :         unsigned int                    flush_fifo:1;           /* Drain the fifo when finishing */
     187                 :            : 
     188                 :            :         unsigned int                    use_busy:1;             /* Wait for busy interrupt */
     189                 :            : 
     190                 :            :         unsigned int                    use_sbc:1;              /* Send CMD23 */
     191                 :            : 
     192                 :            :         unsigned int                    debug:1;                /* Enable debug output */
     193                 :            :         unsigned int                    firmware_sets_cdiv:1;   /* Let the firmware manage the clock */
     194                 :            :         unsigned int                    reset_clock:1;          /* Reset the clock fore the next request */
     195                 :            : 
     196                 :            :         /*DMA part*/
     197                 :            :         struct dma_chan                 *dma_chan_rxtx;         /* DMA channel for reads and writes */
     198                 :            :         struct dma_chan                 *dma_chan;              /* Channel in use */
     199                 :            :         struct dma_slave_config         dma_cfg_rx;
     200                 :            :         struct dma_slave_config         dma_cfg_tx;
     201                 :            :         struct dma_async_tx_descriptor  *dma_desc;
     202                 :            :         u32                             dma_dir;
     203                 :            :         u32                             drain_words;
     204                 :            :         struct page                     *drain_page;
     205                 :            :         u32                             drain_offset;
     206                 :            : 
     207                 :            :         bool                            allow_dma;
     208                 :            :         bool                            use_dma;
     209                 :            :         /*end of DMA part*/
     210                 :            : 
     211                 :            :         int                             max_delay;      /* maximum length of time spent waiting */
     212                 :            :         struct timeval                  stop_time;      /* when the last stop was issued */
     213                 :            :         u32                             delay_after_stop; /* minimum time between stop and subsequent data transfer */
     214                 :            :         u32                             delay_after_this_stop; /* minimum time between this stop and subsequent data transfer */
     215                 :            :         u32                             user_overclock_50; /* User's preferred frequency to use when 50MHz is requested (in MHz) */
     216                 :            :         u32                             overclock_50;   /* frequency to use when 50MHz is requested (in MHz) */
     217                 :            :         u32                             overclock;      /* Current frequency if overclocked, else zero */
     218                 :            :         u32                             pio_limit;      /* Maximum block count for PIO (0 = always DMA) */
     219                 :            : 
     220                 :            :         u32                             sectors;        /* Cached card size in sectors */
     221                 :            : };
     222                 :            : 
     223                 :            : #if ENABLE_LOG
     224                 :            : 
     225                 :            : struct log_entry_struct {
     226                 :            :         char event[4];
     227                 :            :         u32 timestamp;
     228                 :            :         u32 param1;
     229                 :            :         u32 param2;
     230                 :            : };
     231                 :            : 
     232                 :            : typedef struct log_entry_struct LOG_ENTRY_T;
     233                 :            : 
     234                 :            : LOG_ENTRY_T *sdhost_log_buf;
     235                 :            : dma_addr_t sdhost_log_addr;
     236                 :            : static u32 sdhost_log_idx;
     237                 :            : static spinlock_t log_lock;
     238                 :            : static void __iomem *timer_base;
     239                 :            : 
     240                 :            : #define LOG_ENTRIES (256*1)
     241                 :            : #define LOG_SIZE (sizeof(LOG_ENTRY_T)*LOG_ENTRIES)
     242                 :            : 
     243                 :          3 : static void log_init(struct device *dev, u32 bus_to_phys)
     244                 :            : {
     245                 :          3 :         spin_lock_init(&log_lock);
     246                 :          3 :         sdhost_log_buf = dma_alloc_coherent(dev, LOG_SIZE, &sdhost_log_addr,
     247                 :            :                                              GFP_KERNEL);
     248                 :          3 :         if (sdhost_log_buf) {
     249                 :          3 :                 pr_info("sdhost: log_buf @ %p (%llx)\n",
     250                 :            :                         sdhost_log_buf, (u64)sdhost_log_addr);
     251                 :          3 :                 timer_base = ioremap_nocache(bus_to_phys + 0x7e003000, SZ_4K);
     252                 :          3 :                 if (!timer_base)
     253                 :          0 :                         pr_err("sdhost: failed to remap timer\n");
     254                 :            :         }
     255                 :            :         else
     256                 :          0 :                 pr_err("sdhost: failed to allocate log buf\n");
     257                 :          3 : }
     258                 :            : 
     259                 :          3 : static void log_event_impl(const char *event, u32 param1, u32 param2)
     260                 :            : {
     261                 :          3 :         if (sdhost_log_buf) {
     262                 :            :                 LOG_ENTRY_T *entry;
     263                 :            :                 unsigned long flags;
     264                 :            : 
     265                 :          3 :                 spin_lock_irqsave(&log_lock, flags);
     266                 :            : 
     267                 :          3 :                 entry = sdhost_log_buf + sdhost_log_idx;
     268                 :          3 :                 memcpy(entry->event, event, 4);
     269                 :          3 :                 entry->timestamp = (readl(timer_base + 4) & 0x3fffffff) +
     270                 :          3 :                         (smp_processor_id()<<30);
     271                 :          3 :                 entry->param1 = param1;
     272                 :          3 :                 entry->param2 = param2;
     273                 :          3 :                 sdhost_log_idx = (sdhost_log_idx + 1) % LOG_ENTRIES;
     274                 :            : 
     275                 :            :                 spin_unlock_irqrestore(&log_lock, flags);
     276                 :            :         }
     277                 :          3 : }
     278                 :            : 
     279                 :          0 : static void log_dump(void)
     280                 :            : {
     281                 :          0 :         if (sdhost_log_buf) {
     282                 :            :                 LOG_ENTRY_T *entry;
     283                 :            :                 unsigned long flags;
     284                 :            :                 int idx;
     285                 :            : 
     286                 :          0 :                 spin_lock_irqsave(&log_lock, flags);
     287                 :            : 
     288                 :          0 :                 idx = sdhost_log_idx;
     289                 :            :                 do {
     290                 :          0 :                         entry = sdhost_log_buf + idx;
     291                 :          0 :                         if (entry->event[0] != '\0')
     292                 :          0 :                                 pr_info("[%08x] %.4s %x %x\n",
     293                 :            :                                        entry->timestamp,
     294                 :            :                                        entry->event,
     295                 :            :                                        entry->param1,
     296                 :            :                                        entry->param2);
     297                 :          0 :                         idx = (idx + 1) % LOG_ENTRIES;
     298                 :          0 :                 } while (idx != sdhost_log_idx);
     299                 :            : 
     300                 :            :                 spin_unlock_irqrestore(&log_lock, flags);
     301                 :            :         }
     302                 :          0 : }
     303                 :            : 
     304                 :            : #define log_event(event, param1, param2) log_event_impl(event, (u32)(uintptr_t)param1, (u32)(uintptr_t)param2)
     305                 :            : 
     306                 :            : #else
     307                 :            : 
     308                 :            : #define log_init(x) (void)0
     309                 :            : #define log_event(event, param1, param2) (void)0
     310                 :            : #define log_dump() (void)0
     311                 :            : 
     312                 :            : #endif
     313                 :            : 
     314                 :            : static inline void do_gettimeofday(struct timeval *tv)
     315                 :            : {
     316                 :            :         struct timespec64 now;
     317                 :            : 
     318                 :          3 :         ktime_get_real_ts64(&now);
     319                 :          3 :         tv->tv_sec = now.tv_sec;
     320                 :          3 :         tv->tv_usec = now.tv_nsec/1000;
     321                 :            : }
     322                 :            : 
     323                 :            : static inline void bcm2835_sdhost_write(struct bcm2835_host *host, u32 val, int reg)
     324                 :            : {
     325                 :          3 :         writel(val, host->ioaddr + reg);
     326                 :            : }
     327                 :            : 
     328                 :            : static inline u32 bcm2835_sdhost_read(struct bcm2835_host *host, int reg)
     329                 :            : {
     330                 :          3 :         return readl(host->ioaddr + reg);
     331                 :            : }
     332                 :            : 
     333                 :            : static inline u32 bcm2835_sdhost_read_relaxed(struct bcm2835_host *host, int reg)
     334                 :            : {
     335                 :            :         return readl_relaxed(host->ioaddr + reg);
     336                 :            : }
     337                 :            : 
     338                 :          0 : static void bcm2835_sdhost_dumpcmd(struct bcm2835_host *host,
     339                 :            :                                    struct mmc_command *cmd,
     340                 :            :                                    const char *label)
     341                 :            : {
     342                 :          0 :         if (cmd)
     343                 :          0 :                 pr_info("%s:%c%s op %d arg 0x%x flags 0x%x - resp %08x %08x %08x %08x, err %d\n",
     344                 :            :                         mmc_hostname(host->mmc),
     345                 :            :                         (cmd == host->cmd) ? '>' : ' ',
     346                 :            :                         label, cmd->opcode, cmd->arg, cmd->flags,
     347                 :            :                         cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3],
     348                 :            :                         cmd->error);
     349                 :          0 : }
     350                 :            : 
     351                 :          0 : static void bcm2835_sdhost_dumpregs(struct bcm2835_host *host)
     352                 :            : {
     353                 :          0 :         if (host->mrq)
     354                 :            :         {
     355                 :          0 :                 bcm2835_sdhost_dumpcmd(host, host->mrq->sbc, "sbc");
     356                 :          0 :                 bcm2835_sdhost_dumpcmd(host, host->mrq->cmd, "cmd");
     357                 :          0 :                 if (host->mrq->data)
     358                 :          0 :                         pr_info("%s: data blocks %x blksz %x - err %d\n",
     359                 :            :                                mmc_hostname(host->mmc),
     360                 :            :                                host->mrq->data->blocks,
     361                 :            :                                host->mrq->data->blksz,
     362                 :            :                                host->mrq->data->error);
     363                 :          0 :                 bcm2835_sdhost_dumpcmd(host, host->mrq->stop, "stop");
     364                 :            :         }
     365                 :            : 
     366                 :          0 :         pr_info("%s: =========== REGISTER DUMP ===========\n",
     367                 :            :                 mmc_hostname(host->mmc));
     368                 :            : 
     369                 :          0 :         pr_info("%s: SDCMD  0x%08x\n",
     370                 :            :                 mmc_hostname(host->mmc),
     371                 :            :                 bcm2835_sdhost_read(host, SDCMD));
     372                 :          0 :         pr_info("%s: SDARG  0x%08x\n",
     373                 :            :                 mmc_hostname(host->mmc),
     374                 :            :                 bcm2835_sdhost_read(host, SDARG));
     375                 :          0 :         pr_info("%s: SDTOUT 0x%08x\n",
     376                 :            :                 mmc_hostname(host->mmc),
     377                 :            :                 bcm2835_sdhost_read(host, SDTOUT));
     378                 :          0 :         pr_info("%s: SDCDIV 0x%08x\n",
     379                 :            :                 mmc_hostname(host->mmc),
     380                 :            :                 bcm2835_sdhost_read(host, SDCDIV));
     381                 :          0 :         pr_info("%s: SDRSP0 0x%08x\n",
     382                 :            :                 mmc_hostname(host->mmc),
     383                 :            :                 bcm2835_sdhost_read(host, SDRSP0));
     384                 :          0 :         pr_info("%s: SDRSP1 0x%08x\n",
     385                 :            :                 mmc_hostname(host->mmc),
     386                 :            :                 bcm2835_sdhost_read(host, SDRSP1));
     387                 :          0 :         pr_info("%s: SDRSP2 0x%08x\n",
     388                 :            :                 mmc_hostname(host->mmc),
     389                 :            :                 bcm2835_sdhost_read(host, SDRSP2));
     390                 :          0 :         pr_info("%s: SDRSP3 0x%08x\n",
     391                 :            :                 mmc_hostname(host->mmc),
     392                 :            :                 bcm2835_sdhost_read(host, SDRSP3));
     393                 :          0 :         pr_info("%s: SDHSTS 0x%08x\n",
     394                 :            :                 mmc_hostname(host->mmc),
     395                 :            :                 bcm2835_sdhost_read(host, SDHSTS));
     396                 :          0 :         pr_info("%s: SDVDD  0x%08x\n",
     397                 :            :                 mmc_hostname(host->mmc),
     398                 :            :                 bcm2835_sdhost_read(host, SDVDD));
     399                 :          0 :         pr_info("%s: SDEDM  0x%08x\n",
     400                 :            :                 mmc_hostname(host->mmc),
     401                 :            :                 bcm2835_sdhost_read(host, SDEDM));
     402                 :          0 :         pr_info("%s: SDHCFG 0x%08x\n",
     403                 :            :                 mmc_hostname(host->mmc),
     404                 :            :                 bcm2835_sdhost_read(host, SDHCFG));
     405                 :          0 :         pr_info("%s: SDHBCT 0x%08x\n",
     406                 :            :                 mmc_hostname(host->mmc),
     407                 :            :                 bcm2835_sdhost_read(host, SDHBCT));
     408                 :          0 :         pr_info("%s: SDHBLC 0x%08x\n",
     409                 :            :                 mmc_hostname(host->mmc),
     410                 :            :                 bcm2835_sdhost_read(host, SDHBLC));
     411                 :            : 
     412                 :          0 :         pr_info("%s: ===========================================\n",
     413                 :            :                 mmc_hostname(host->mmc));
     414                 :          0 : }
     415                 :            : 
     416                 :            : static void bcm2835_sdhost_set_power(struct bcm2835_host *host, bool on)
     417                 :            : {
     418                 :            :         bcm2835_sdhost_write(host, on ? 1 : 0, SDVDD);
     419                 :            : }
     420                 :            : 
     421                 :          3 : static void bcm2835_sdhost_reset_internal(struct bcm2835_host *host)
     422                 :            : {
     423                 :            :         u32 temp;
     424                 :            : 
     425                 :          3 :         if (host->debug)
     426                 :          0 :                 pr_info("%s: reset\n", mmc_hostname(host->mmc));
     427                 :            : 
     428                 :            :         bcm2835_sdhost_set_power(host, false);
     429                 :            : 
     430                 :            :         bcm2835_sdhost_write(host, 0, SDCMD);
     431                 :            :         bcm2835_sdhost_write(host, 0, SDARG);
     432                 :            :         bcm2835_sdhost_write(host, 0xf00000, SDTOUT);
     433                 :            :         bcm2835_sdhost_write(host, 0, SDCDIV);
     434                 :            :         bcm2835_sdhost_write(host, 0x7f8, SDHSTS); /* Write 1s to clear */
     435                 :            :         bcm2835_sdhost_write(host, 0, SDHCFG);
     436                 :            :         bcm2835_sdhost_write(host, 0, SDHBCT);
     437                 :            :         bcm2835_sdhost_write(host, 0, SDHBLC);
     438                 :            : 
     439                 :            :         /* Limit fifo usage due to silicon bug */
     440                 :            :         temp = bcm2835_sdhost_read(host, SDEDM);
     441                 :          3 :         temp &= ~((SDEDM_THRESHOLD_MASK<<SDEDM_READ_THRESHOLD_SHIFT) |
     442                 :            :                   (SDEDM_THRESHOLD_MASK<<SDEDM_WRITE_THRESHOLD_SHIFT));
     443                 :          3 :         temp |= (FIFO_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |
     444                 :            :                 (FIFO_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);
     445                 :            :         bcm2835_sdhost_write(host, temp, SDEDM);
     446                 :          3 :         mdelay(10);
     447                 :            :         bcm2835_sdhost_set_power(host, true);
     448                 :          3 :         mdelay(10);
     449                 :          3 :         host->clock = 0;
     450                 :          3 :         host->sectors = 0;
     451                 :          3 :         bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
     452                 :            :         bcm2835_sdhost_write(host, SDCDIV_MAX_CDIV, SDCDIV);
     453                 :          3 : }
     454                 :            : 
     455                 :          3 : static void bcm2835_sdhost_reset(struct mmc_host *mmc)
     456                 :            : {
     457                 :            :         struct bcm2835_host *host = mmc_priv(mmc);
     458                 :            :         unsigned long flags;
     459                 :          3 :         spin_lock_irqsave(&host->lock, flags);
     460                 :          3 :         log_event("RST<", 0, 0);
     461                 :            : 
     462                 :          3 :         bcm2835_sdhost_reset_internal(host);
     463                 :            : 
     464                 :            :         spin_unlock_irqrestore(&host->lock, flags);
     465                 :          3 : }
     466                 :            : 
     467                 :            : static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
     468                 :            : 
     469                 :          3 : static void bcm2835_sdhost_init(struct bcm2835_host *host, int soft)
     470                 :            : {
     471                 :            :         pr_debug("bcm2835_sdhost_init(%d)\n", soft);
     472                 :            : 
     473                 :            :         /* Set interrupt enables */
     474                 :          3 :         host->hcfg = SDHCFG_BUSY_IRPT_EN;
     475                 :            : 
     476                 :          3 :         bcm2835_sdhost_reset_internal(host);
     477                 :            : 
     478                 :          3 :         if (soft) {
     479                 :            :                 /* force clock reconfiguration */
     480                 :          0 :                 host->clock = 0;
     481                 :          0 :                 bcm2835_sdhost_set_ios(host->mmc, &host->mmc->ios);
     482                 :            :         }
     483                 :          3 : }
     484                 :            : 
     485                 :          3 : static void bcm2835_sdhost_wait_transfer_complete(struct bcm2835_host *host)
     486                 :            : {
     487                 :            :         int timediff;
     488                 :            :         u32 alternate_idle;
     489                 :            :         u32 edm;
     490                 :            : 
     491                 :          3 :         alternate_idle = (host->mrq->data->flags & MMC_DATA_READ) ?
     492                 :            :                 SDEDM_FSM_READWAIT : SDEDM_FSM_WRITESTART1;
     493                 :            : 
     494                 :            :         edm = bcm2835_sdhost_read(host, SDEDM);
     495                 :            : 
     496                 :          3 :         log_event("WTC<", edm, 0);
     497                 :            : 
     498                 :            :         timediff = 0;
     499                 :            : 
     500                 :            :         while (1) {
     501                 :          3 :                 u32 fsm = edm & SDEDM_FSM_MASK;
     502                 :          3 :                 if ((fsm == SDEDM_FSM_IDENTMODE) ||
     503                 :            :                     (fsm == SDEDM_FSM_DATAMODE))
     504                 :            :                         break;
     505                 :          2 :                 if (fsm == alternate_idle) {
     506                 :          2 :                         bcm2835_sdhost_write(host,
     507                 :            :                                              edm | SDEDM_FORCE_DATA_MODE,
     508                 :            :                                              SDEDM);
     509                 :            :                         break;
     510                 :            :                 }
     511                 :            : 
     512                 :          2 :                 timediff++;
     513                 :          2 :                 if (timediff == 100000) {
     514                 :          0 :                         pr_err("%s: wait_transfer_complete - still waiting after %d retries\n",
     515                 :            :                                mmc_hostname(host->mmc),
     516                 :            :                                timediff);
     517                 :          0 :                         log_dump();
     518                 :          0 :                         bcm2835_sdhost_dumpregs(host);
     519                 :          0 :                         host->mrq->data->error = -ETIMEDOUT;
     520                 :          0 :                         log_event("WTC!", edm, 0);
     521                 :          3 :                         return;
     522                 :            :                 }
     523                 :          2 :                 cpu_relax();
     524                 :            :                 edm = bcm2835_sdhost_read(host, SDEDM);
     525                 :            :         }
     526                 :          3 :         log_event("WTC>", edm, 0);
     527                 :            : }
     528                 :            : 
     529                 :            : static void bcm2835_sdhost_finish_data(struct bcm2835_host *host);
     530                 :            : 
     531                 :          3 : static void bcm2835_sdhost_dma_complete(void *param)
     532                 :            : {
     533                 :            :         struct bcm2835_host *host = param;
     534                 :          3 :         struct mmc_data *data = host->data;
     535                 :            :         unsigned long flags;
     536                 :            : 
     537                 :          3 :         spin_lock_irqsave(&host->lock, flags);
     538                 :          3 :         log_event("DMA<", host->data, bcm2835_sdhost_read(host, SDHSTS));
     539                 :          3 :         log_event("DMA ", bcm2835_sdhost_read(host, SDCMD),
     540                 :            :                   bcm2835_sdhost_read(host, SDEDM));
     541                 :            : 
     542                 :          3 :         if (host->dma_chan) {
     543                 :          3 :                 dma_unmap_sg(host->dma_chan->device->dev,
     544                 :            :                              data->sg, data->sg_len,
     545                 :            :                              host->dma_dir);
     546                 :            : 
     547                 :          3 :                 host->dma_chan = NULL;
     548                 :            :         }
     549                 :            : 
     550                 :          3 :         if (host->drain_words) {
     551                 :            :                 void *page;
     552                 :            :                 u32 *buf;
     553                 :            : 
     554                 :          3 :                 if (host->drain_offset & PAGE_MASK) {
     555                 :          3 :                         host->drain_page += host->drain_offset >> PAGE_SHIFT;
     556                 :          3 :                         host->drain_offset &= ~PAGE_MASK;
     557                 :            :                 }
     558                 :            : 
     559                 :          3 :                 page = kmap_atomic(host->drain_page);
     560                 :          3 :                 buf = page + host->drain_offset;
     561                 :            : 
     562                 :          3 :                 while (host->drain_words) {
     563                 :            :                         u32 edm = bcm2835_sdhost_read(host, SDEDM);
     564                 :          3 :                         if ((edm >> 4) & 0x1f)
     565                 :          3 :                                 *(buf++) = bcm2835_sdhost_read(host,
     566                 :            :                                                                SDDATA);
     567                 :          3 :                         host->drain_words--;
     568                 :            :                 }
     569                 :            : 
     570                 :            :                 kunmap_atomic(page);
     571                 :            :         }
     572                 :            : 
     573                 :          3 :         bcm2835_sdhost_finish_data(host);
     574                 :            : 
     575                 :          3 :         log_event("DMA>", host->data, 0);
     576                 :            :         spin_unlock_irqrestore(&host->lock, flags);
     577                 :          3 : }
     578                 :            : 
     579                 :          3 : static void bcm2835_sdhost_read_block_pio(struct bcm2835_host *host)
     580                 :            : {
     581                 :            :         unsigned long flags;
     582                 :            :         size_t blksize, len;
     583                 :            :         u32 *buf;
     584                 :            :         unsigned long wait_max;
     585                 :            : 
     586                 :          3 :         blksize = host->data->blksz;
     587                 :            : 
     588                 :          3 :         wait_max = jiffies + msecs_to_jiffies(host->pio_timeout);
     589                 :            : 
     590                 :          3 :         local_irq_save(flags);
     591                 :            : 
     592                 :          3 :         while (blksize) {
     593                 :            :                 int copy_words;
     594                 :            :                 u32 hsts = 0;
     595                 :            : 
     596                 :          3 :                 if (!sg_miter_next(&host->sg_miter)) {
     597                 :          0 :                         host->data->error = -EINVAL;
     598                 :          0 :                         break;
     599                 :            :                 }
     600                 :            : 
     601                 :          3 :                 len = min(host->sg_miter.length, blksize);
     602                 :          3 :                 if (len % 4) {
     603                 :          0 :                         host->data->error = -EINVAL;
     604                 :          0 :                         break;
     605                 :            :                 }
     606                 :            : 
     607                 :          3 :                 blksize -= len;
     608                 :          3 :                 host->sg_miter.consumed = len;
     609                 :            : 
     610                 :          3 :                 buf = (u32 *)host->sg_miter.addr;
     611                 :            : 
     612                 :          3 :                 copy_words = len/4;
     613                 :            : 
     614                 :          3 :                 while (copy_words) {
     615                 :            :                         int burst_words, words;
     616                 :            :                         u32 edm;
     617                 :            : 
     618                 :            :                         burst_words = SDDATA_FIFO_PIO_BURST;
     619                 :          3 :                         if (burst_words > copy_words)
     620                 :            :                                 burst_words = copy_words;
     621                 :            :                         edm = bcm2835_sdhost_read(host, SDEDM);
     622                 :          3 :                         words = ((edm >> 4) & 0x1f);
     623                 :            : 
     624                 :          3 :                         if (words < burst_words) {
     625                 :          2 :                                 int fsm_state = (edm & SDEDM_FSM_MASK);
     626                 :          2 :                                 if ((fsm_state != SDEDM_FSM_READDATA) &&
     627                 :          2 :                                     (fsm_state != SDEDM_FSM_READWAIT) &&
     628                 :            :                                     (fsm_state != SDEDM_FSM_READCRC)) {
     629                 :            :                                         hsts = bcm2835_sdhost_read(host,
     630                 :            :                                                                    SDHSTS);
     631                 :          0 :                                         pr_info("%s: fsm %x, hsts %x\n",
     632                 :            :                                                mmc_hostname(host->mmc),
     633                 :            :                                                fsm_state, hsts);
     634                 :          0 :                                         if (hsts & SDHSTS_ERROR_MASK)
     635                 :            :                                                 break;
     636                 :            :                                 }
     637                 :            : 
     638                 :          2 :                                 if (time_after(jiffies, wait_max)) {
     639                 :          0 :                                         pr_err("%s: PIO read timeout - EDM %x\n",
     640                 :            :                                                mmc_hostname(host->mmc),
     641                 :            :                                                edm);
     642                 :            :                                         hsts = SDHSTS_REW_TIME_OUT;
     643                 :          0 :                                         break;
     644                 :            :                                 }
     645                 :          2 :                                 ndelay((burst_words - words) *
     646                 :            :                                        host->ns_per_fifo_word);
     647                 :          2 :                                 continue;
     648                 :          3 :                         } else if (words > copy_words) {
     649                 :            :                                 words = copy_words;
     650                 :            :                         }
     651                 :            : 
     652                 :          3 :                         copy_words -= words;
     653                 :            : 
     654                 :          3 :                         while (words) {
     655                 :          3 :                                 *(buf++) = bcm2835_sdhost_read(host, SDDATA);
     656                 :          3 :                                 words--;
     657                 :            :                         }
     658                 :            :                 }
     659                 :            : 
     660                 :          3 :                 if (hsts & SDHSTS_ERROR_MASK)
     661                 :            :                         break;
     662                 :            :         }
     663                 :            : 
     664                 :          3 :         sg_miter_stop(&host->sg_miter);
     665                 :            : 
     666                 :          3 :         local_irq_restore(flags);
     667                 :          3 : }
     668                 :            : 
     669                 :          3 : static void bcm2835_sdhost_write_block_pio(struct bcm2835_host *host)
     670                 :            : {
     671                 :            :         unsigned long flags;
     672                 :            :         size_t blksize, len;
     673                 :            :         u32 *buf;
     674                 :            :         unsigned long wait_max;
     675                 :            : 
     676                 :          3 :         blksize = host->data->blksz;
     677                 :            : 
     678                 :          3 :         wait_max = jiffies + msecs_to_jiffies(host->pio_timeout);
     679                 :            : 
     680                 :          3 :         local_irq_save(flags);
     681                 :            : 
     682                 :          3 :         while (blksize) {
     683                 :            :                 int copy_words;
     684                 :            :                 u32 hsts = 0;
     685                 :            : 
     686                 :          3 :                 if (!sg_miter_next(&host->sg_miter)) {
     687                 :          0 :                         host->data->error = -EINVAL;
     688                 :          0 :                         break;
     689                 :            :                 }
     690                 :            : 
     691                 :          3 :                 len = min(host->sg_miter.length, blksize);
     692                 :          3 :                 if (len % 4) {
     693                 :          0 :                         host->data->error = -EINVAL;
     694                 :          0 :                         break;
     695                 :            :                 }
     696                 :            : 
     697                 :          3 :                 blksize -= len;
     698                 :          3 :                 host->sg_miter.consumed = len;
     699                 :            : 
     700                 :          3 :                 buf = (u32 *)host->sg_miter.addr;
     701                 :            : 
     702                 :          3 :                 copy_words = len/4;
     703                 :            : 
     704                 :          3 :                 while (copy_words) {
     705                 :            :                         int burst_words, words;
     706                 :            :                         u32 edm;
     707                 :            : 
     708                 :            :                         burst_words = SDDATA_FIFO_PIO_BURST;
     709                 :          3 :                         if (burst_words > copy_words)
     710                 :            :                                 burst_words = copy_words;
     711                 :            :                         edm = bcm2835_sdhost_read(host, SDEDM);
     712                 :          3 :                         words = SDDATA_FIFO_WORDS - ((edm >> 4) & 0x1f);
     713                 :            : 
     714                 :          3 :                         if (words < burst_words) {
     715                 :          2 :                                 int fsm_state = (edm & SDEDM_FSM_MASK);
     716                 :          2 :                                 if ((fsm_state != SDEDM_FSM_WRITEDATA) &&
     717                 :          2 :                                     (fsm_state != SDEDM_FSM_WRITESTART1) &&
     718                 :            :                                     (fsm_state != SDEDM_FSM_WRITESTART2)) {
     719                 :            :                                         hsts = bcm2835_sdhost_read(host,
     720                 :            :                                                                    SDHSTS);
     721                 :          0 :                                         pr_info("%s: fsm %x, hsts %x\n",
     722                 :            :                                                mmc_hostname(host->mmc),
     723                 :            :                                                fsm_state, hsts);
     724                 :          0 :                                         if (hsts & SDHSTS_ERROR_MASK)
     725                 :            :                                                 break;
     726                 :            :                                 }
     727                 :            : 
     728                 :          2 :                                 if (time_after(jiffies, wait_max)) {
     729                 :          0 :                                         pr_err("%s: PIO write timeout - EDM %x\n",
     730                 :            :                                                mmc_hostname(host->mmc),
     731                 :            :                                                edm);
     732                 :            :                                         hsts = SDHSTS_REW_TIME_OUT;
     733                 :          0 :                                         break;
     734                 :            :                                 }
     735                 :          2 :                                 ndelay((burst_words - words) *
     736                 :            :                                        host->ns_per_fifo_word);
     737                 :          2 :                                 continue;
     738                 :          3 :                         } else if (words > copy_words) {
     739                 :            :                                 words = copy_words;
     740                 :            :                         }
     741                 :            : 
     742                 :          3 :                         copy_words -= words;
     743                 :            : 
     744                 :          3 :                         while (words) {
     745                 :          3 :                                 bcm2835_sdhost_write(host, *(buf++), SDDATA);
     746                 :          3 :                                 words--;
     747                 :            :                         }
     748                 :            :                 }
     749                 :            : 
     750                 :          3 :                 if (hsts & SDHSTS_ERROR_MASK)
     751                 :            :                         break;
     752                 :            :         }
     753                 :            : 
     754                 :          3 :         sg_miter_stop(&host->sg_miter);
     755                 :            : 
     756                 :          3 :         local_irq_restore(flags);
     757                 :          3 : }
     758                 :            : 
     759                 :          3 : static void bcm2835_sdhost_transfer_pio(struct bcm2835_host *host)
     760                 :            : {
     761                 :            :         u32 sdhsts;
     762                 :            :         bool is_read;
     763                 :          3 :         BUG_ON(!host->data);
     764                 :          3 :         log_event("XFP<", host->data, host->blocks);
     765                 :            : 
     766                 :          3 :         is_read = (host->data->flags & MMC_DATA_READ) != 0;
     767                 :          3 :         if (is_read)
     768                 :          3 :                 bcm2835_sdhost_read_block_pio(host);
     769                 :            :         else
     770                 :          3 :                 bcm2835_sdhost_write_block_pio(host);
     771                 :            : 
     772                 :            :         sdhsts = bcm2835_sdhost_read(host, SDHSTS);
     773                 :          3 :         if (sdhsts & (SDHSTS_CRC16_ERROR |
     774                 :            :                       SDHSTS_CRC7_ERROR |
     775                 :            :                       SDHSTS_FIFO_ERROR)) {
     776                 :          0 :                 pr_err("%s: %s transfer error - HSTS %x\n",
     777                 :            :                        mmc_hostname(host->mmc),
     778                 :            :                        is_read ? "read" : "write",
     779                 :            :                        sdhsts);
     780                 :          0 :                 host->data->error = -EILSEQ;
     781                 :          3 :         } else if ((sdhsts & (SDHSTS_CMD_TIME_OUT |
     782                 :            :                               SDHSTS_REW_TIME_OUT))) {
     783                 :          0 :                 pr_err("%s: %s timeout error - HSTS %x\n",
     784                 :            :                        mmc_hostname(host->mmc),
     785                 :            :                        is_read ? "read" : "write",
     786                 :            :                        sdhsts);
     787                 :          0 :                 host->data->error = -ETIMEDOUT;
     788                 :            :         }
     789                 :          3 :         log_event("XFP>", host->data, host->blocks);
     790                 :          3 : }
     791                 :            : 
     792                 :          3 : static void bcm2835_sdhost_prepare_dma(struct bcm2835_host *host,
     793                 :            :         struct mmc_data *data)
     794                 :            : {
     795                 :            :         int len, dir_data, dir_slave;
     796                 :            :         struct dma_async_tx_descriptor *desc = NULL;
     797                 :            :         struct dma_chan *dma_chan;
     798                 :            : 
     799                 :          3 :         log_event("PRD<", data, 0);
     800                 :            :         pr_debug("bcm2835_sdhost_prepare_dma()\n");
     801                 :            : 
     802                 :          3 :         dma_chan = host->dma_chan_rxtx;
     803                 :          3 :         if (data->flags & MMC_DATA_READ) {
     804                 :            :                 dir_data = DMA_FROM_DEVICE;
     805                 :            :                 dir_slave = DMA_DEV_TO_MEM;
     806                 :            :         } else {
     807                 :            :                 dir_data = DMA_TO_DEVICE;
     808                 :            :                 dir_slave = DMA_MEM_TO_DEV;
     809                 :            :         }
     810                 :          3 :         log_event("PRD1", dma_chan, 0);
     811                 :            : 
     812                 :          3 :         BUG_ON(!dma_chan->device);
     813                 :          3 :         BUG_ON(!dma_chan->device->dev);
     814                 :          3 :         BUG_ON(!data->sg);
     815                 :            : 
     816                 :            :         /* The block doesn't manage the FIFO DREQs properly for multi-block
     817                 :            :            transfers, so don't attempt to DMA the final few words.
     818                 :            :            Unfortunately this requires the final sg entry to be trimmed.
     819                 :            :            N.B. This code demands that the overspill is contained in
     820                 :            :            a single sg entry.
     821                 :            :         */
     822                 :            : 
     823                 :          3 :         host->drain_words = 0;
     824                 :          3 :         if ((data->blocks > 1) && (dir_data == DMA_FROM_DEVICE)) {
     825                 :            :                 struct scatterlist *sg;
     826                 :            :                 u32 len;
     827                 :            :                 int i;
     828                 :            : 
     829                 :          3 :                 len = min((u32)(FIFO_READ_THRESHOLD - 1) * 4,
     830                 :            :                           (u32)data->blocks * data->blksz);
     831                 :            : 
     832                 :          3 :                 for_each_sg(data->sg, sg, data->sg_len, i) {
     833                 :          3 :                         if (sg_is_last(sg)) {
     834                 :          3 :                                 BUG_ON(sg->length < len);
     835                 :          3 :                                 sg->length -= len;
     836                 :          3 :                                 host->drain_page = sg_page(sg);
     837                 :          3 :                                 host->drain_offset = sg->offset + sg->length;
     838                 :            :                         }
     839                 :            :                 }
     840                 :          3 :                 host->drain_words = len/4;
     841                 :            :         }
     842                 :            : 
     843                 :            :         /* The parameters have already been validated, so this will not fail */
     844                 :          3 :         (void)dmaengine_slave_config(dma_chan,
     845                 :            :                                      (dir_data == DMA_FROM_DEVICE) ?
     846                 :            :                                      &host->dma_cfg_rx :
     847                 :            :                                      &host->dma_cfg_tx);
     848                 :            : 
     849                 :          3 :         len = dma_map_sg(dma_chan->device->dev, data->sg, data->sg_len,
     850                 :            :                          dir_data);
     851                 :            : 
     852                 :          3 :         log_event("PRD2", len, 0);
     853                 :          3 :         if (len > 0)
     854                 :          3 :                 desc = dmaengine_prep_slave_sg(dma_chan, data->sg,
     855                 :            :                                                len, dir_slave,
     856                 :            :                                                DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
     857                 :          3 :         log_event("PRD3", desc, 0);
     858                 :            : 
     859                 :          3 :         if (desc) {
     860                 :          3 :                 desc->callback = bcm2835_sdhost_dma_complete;
     861                 :          3 :                 desc->callback_param = host;
     862                 :          3 :                 host->dma_desc = desc;
     863                 :          3 :                 host->dma_chan = dma_chan;
     864                 :          3 :                 host->dma_dir = dir_data;
     865                 :            :         }
     866                 :          3 :         log_event("PDM>", data, 0);
     867                 :          3 : }
     868                 :            : 
     869                 :          3 : static void bcm2835_sdhost_start_dma(struct bcm2835_host *host)
     870                 :            : {
     871                 :          3 :         log_event("SDMA", host->data, host->dma_chan);
     872                 :          3 :         dmaengine_submit(host->dma_desc);
     873                 :          3 :         dma_async_issue_pending(host->dma_chan);
     874                 :          3 : }
     875                 :            : 
     876                 :            : static void bcm2835_sdhost_set_transfer_irqs(struct bcm2835_host *host)
     877                 :            : {
     878                 :            :         u32 all_irqs = SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN |
     879                 :            :                 SDHCFG_BUSY_IRPT_EN;
     880                 :          3 :         if (host->dma_desc)
     881                 :          3 :                 host->hcfg = (host->hcfg & ~all_irqs) |
     882                 :            :                         SDHCFG_BUSY_IRPT_EN;
     883                 :            :         else
     884                 :          3 :                 host->hcfg = (host->hcfg & ~all_irqs) |
     885                 :          3 :                         SDHCFG_DATA_IRPT_EN |
     886                 :            :                         SDHCFG_BUSY_IRPT_EN;
     887                 :            : 
     888                 :          3 :         bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
     889                 :            : }
     890                 :            : 
     891                 :          3 : static void bcm2835_sdhost_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
     892                 :            : {
     893                 :          3 :         struct mmc_data *data = cmd->data;
     894                 :            : 
     895                 :          3 :         WARN_ON(host->data);
     896                 :            : 
     897                 :          3 :         host->data = data;
     898                 :          3 :         if (!data)
     899                 :          3 :                 return;
     900                 :            : 
     901                 :            :         /* Sanity checks */
     902                 :          3 :         BUG_ON(data->blksz * data->blocks > 524288);
     903                 :          3 :         BUG_ON(data->blksz > host->mmc->max_blk_size);
     904                 :          3 :         BUG_ON(data->blocks > 65535);
     905                 :            : 
     906                 :          3 :         host->data_complete = 0;
     907                 :          3 :         host->flush_fifo = 0;
     908                 :          3 :         host->data->bytes_xfered = 0;
     909                 :            : 
     910                 :          3 :         if (!host->sectors && host->mmc->card) {
     911                 :            :                 struct mmc_card *card = host->mmc->card;
     912                 :          3 :                 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
     913                 :            :                         /*
     914                 :            :                          * The EXT_CSD sector count is in number of 512 byte
     915                 :            :                          * sectors.
     916                 :            :                          */
     917                 :          0 :                         host->sectors = card->ext_csd.sectors;
     918                 :            :                 } else {
     919                 :            :                         /*
     920                 :            :                          * The CSD capacity field is in units of read_blkbits.
     921                 :            :                          * set_capacity takes units of 512 bytes.
     922                 :            :                          */
     923                 :          3 :                         host->sectors = card->csd.capacity <<
     924                 :          3 :                                 (card->csd.read_blkbits - 9);
     925                 :            :                 }
     926                 :            :         }
     927                 :            : 
     928                 :          3 :         if (!host->dma_desc) {
     929                 :            :                 /* Use PIO */
     930                 :            :                 int flags = SG_MITER_ATOMIC;
     931                 :            : 
     932                 :          3 :                 if (data->flags & MMC_DATA_READ)
     933                 :            :                         flags |= SG_MITER_TO_SG;
     934                 :            :                 else
     935                 :            :                         flags |= SG_MITER_FROM_SG;
     936                 :          3 :                 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
     937                 :          3 :                 host->blocks = data->blocks;
     938                 :            :         }
     939                 :            : 
     940                 :            :         bcm2835_sdhost_set_transfer_irqs(host);
     941                 :            : 
     942                 :          3 :         bcm2835_sdhost_write(host, data->blksz, SDHBCT);
     943                 :          3 :         bcm2835_sdhost_write(host, data->blocks, SDHBLC);
     944                 :            : 
     945                 :          3 :         BUG_ON(!host->data);
     946                 :            : }
     947                 :            : 
     948                 :          3 : bool bcm2835_sdhost_send_command(struct bcm2835_host *host,
     949                 :            :                                  struct mmc_command *cmd)
     950                 :            : {
     951                 :            :         u32 sdcmd, sdhsts;
     952                 :            :         unsigned long timeout;
     953                 :            :         int delay;
     954                 :            : 
     955                 :          3 :         WARN_ON(host->cmd);
     956                 :          3 :         log_event("CMD<", cmd->opcode, cmd->arg);
     957                 :            : 
     958                 :            :         if (cmd->data)
     959                 :            :                 pr_debug("%s: send_command %d 0x%x "
     960                 :            :                          "(flags 0x%x) - %s %d*%d\n",
     961                 :            :                          mmc_hostname(host->mmc),
     962                 :            :                          cmd->opcode, cmd->arg, cmd->flags,
     963                 :            :                          (cmd->data->flags & MMC_DATA_READ) ?
     964                 :            :                          "read" : "write", cmd->data->blocks,
     965                 :            :                          cmd->data->blksz);
     966                 :            :         else
     967                 :            :                 pr_debug("%s: send_command %d 0x%x (flags 0x%x)\n",
     968                 :            :                          mmc_hostname(host->mmc),
     969                 :            :                          cmd->opcode, cmd->arg, cmd->flags);
     970                 :            : 
     971                 :            :         /* Wait max 100 ms */
     972                 :            :         timeout = 10000;
     973                 :            : 
     974                 :          3 :         while (bcm2835_sdhost_read(host, SDCMD) & SDCMD_NEW_FLAG) {
     975                 :          0 :                 if (timeout == 0) {
     976                 :          0 :                         pr_warn("%s: previous command never completed.\n",
     977                 :            :                                 mmc_hostname(host->mmc));
     978                 :          0 :                         if (host->debug)
     979                 :          0 :                                 bcm2835_sdhost_dumpregs(host);
     980                 :          0 :                         cmd->error = -EILSEQ;
     981                 :          0 :                         tasklet_schedule(&host->finish_tasklet);
     982                 :          0 :                         return false;
     983                 :            :                 }
     984                 :          0 :                 timeout--;
     985                 :          0 :                 udelay(10);
     986                 :            :         }
     987                 :            : 
     988                 :          3 :         delay = (10000 - timeout)/100;
     989                 :          3 :         if (delay > host->max_delay) {
     990                 :          0 :                 host->max_delay = delay;
     991                 :          0 :                 pr_warning("%s: controller hung for %d ms\n",
     992                 :            :                            mmc_hostname(host->mmc),
     993                 :            :                            host->max_delay);
     994                 :            :         }
     995                 :            : 
     996                 :          3 :         timeout = jiffies;
     997                 :          3 :         if (!cmd->data && cmd->busy_timeout > 9000)
     998                 :          0 :                 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
     999                 :            :         else
    1000                 :          3 :                 timeout += 10 * HZ;
    1001                 :          3 :         mod_timer(&host->timer, timeout);
    1002                 :            : 
    1003                 :          3 :         host->cmd = cmd;
    1004                 :            : 
    1005                 :            :         /* Clear any error flags */
    1006                 :            :         sdhsts = bcm2835_sdhost_read(host, SDHSTS);
    1007                 :          3 :         if (sdhsts & SDHSTS_ERROR_MASK)
    1008                 :            :                 bcm2835_sdhost_write(host, sdhsts, SDHSTS);
    1009                 :            : 
    1010                 :          3 :         if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
    1011                 :          0 :                 pr_err("%s: unsupported response type!\n",
    1012                 :            :                         mmc_hostname(host->mmc));
    1013                 :          0 :                 cmd->error = -EINVAL;
    1014                 :          0 :                 tasklet_schedule(&host->finish_tasklet);
    1015                 :          0 :                 return false;
    1016                 :            :         }
    1017                 :            : 
    1018                 :          3 :         bcm2835_sdhost_prepare_data(host, cmd);
    1019                 :            : 
    1020                 :          3 :         bcm2835_sdhost_write(host, cmd->arg, SDARG);
    1021                 :            : 
    1022                 :          3 :         sdcmd = cmd->opcode & SDCMD_CMD_MASK;
    1023                 :            : 
    1024                 :          3 :         host->use_busy = 0;
    1025                 :          3 :         if (!(cmd->flags & MMC_RSP_PRESENT)) {
    1026                 :          3 :                 sdcmd |= SDCMD_NO_RESPONSE;
    1027                 :            :         } else {
    1028                 :          3 :                 if (cmd->flags & MMC_RSP_136)
    1029                 :          3 :                         sdcmd |= SDCMD_LONG_RESPONSE;
    1030                 :          3 :                 if (cmd->flags & MMC_RSP_BUSY) {
    1031                 :          3 :                         sdcmd |= SDCMD_BUSYWAIT;
    1032                 :          3 :                         host->use_busy = 1;
    1033                 :            :                 }
    1034                 :            :         }
    1035                 :            : 
    1036                 :          3 :         if (cmd->data) {
    1037                 :          3 :                 log_event("CMDD", cmd->data->blocks, cmd->data->blksz);
    1038                 :          3 :                 if (host->delay_after_this_stop) {
    1039                 :            :                         struct timeval now;
    1040                 :            :                         int time_since_stop;
    1041                 :            :                         do_gettimeofday(&now);
    1042                 :          1 :                         time_since_stop = (now.tv_sec - host->stop_time.tv_sec);
    1043                 :          1 :                         if (time_since_stop < 2) {
    1044                 :            :                                 /* Possibly less than one second */
    1045                 :          1 :                                 time_since_stop = time_since_stop * 1000000 +
    1046                 :          1 :                                         (now.tv_usec - host->stop_time.tv_usec);
    1047                 :          1 :                                 if (time_since_stop <
    1048                 :          1 :                                     host->delay_after_this_stop)
    1049                 :          1 :                                         udelay(host->delay_after_this_stop -
    1050                 :            :                                                time_since_stop);
    1051                 :            :                         }
    1052                 :            :                 }
    1053                 :            : 
    1054                 :          3 :                 host->delay_after_this_stop = host->delay_after_stop;
    1055                 :          3 :                 if ((cmd->data->flags & MMC_DATA_READ) && !host->use_sbc) {
    1056                 :            :                         /* See if read crosses one of the hazardous sectors */
    1057                 :            :                         u32 first_blk, last_blk;
    1058                 :            : 
    1059                 :            :                         /* Intentionally include the following sector because
    1060                 :            :                            without CMD23/SBC the read may run on. */
    1061                 :          3 :                         first_blk = host->mrq->cmd->arg;
    1062                 :          3 :                         last_blk = first_blk + cmd->data->blocks;
    1063                 :            : 
    1064                 :          3 :                         if (((last_blk >= (host->sectors - 64)) &&
    1065                 :          3 :                              (first_blk <= (host->sectors - 64))) ||
    1066                 :          3 :                             ((last_blk >= (host->sectors - 32)) &&
    1067                 :            :                              (first_blk <= (host->sectors - 32)))) {
    1068                 :          1 :                                 host->delay_after_this_stop =
    1069                 :          1 :                                         max(250u, host->delay_after_stop);
    1070                 :            :                         }
    1071                 :            :                 }
    1072                 :            : 
    1073                 :          3 :                 if (cmd->data->flags & MMC_DATA_WRITE)
    1074                 :          3 :                         sdcmd |= SDCMD_WRITE_CMD;
    1075                 :          3 :                 if (cmd->data->flags & MMC_DATA_READ)
    1076                 :          3 :                         sdcmd |= SDCMD_READ_CMD;
    1077                 :            :         }
    1078                 :            : 
    1079                 :          3 :         bcm2835_sdhost_write(host, sdcmd | SDCMD_NEW_FLAG, SDCMD);
    1080                 :            : 
    1081                 :          3 :         return true;
    1082                 :            : }
    1083                 :            : 
    1084                 :            : static void bcm2835_sdhost_finish_command(struct bcm2835_host *host,
    1085                 :            :                                           unsigned long *irq_flags);
    1086                 :            : static void bcm2835_sdhost_transfer_complete(struct bcm2835_host *host);
    1087                 :            : 
    1088                 :          3 : static void bcm2835_sdhost_finish_data(struct bcm2835_host *host)
    1089                 :            : {
    1090                 :            :         struct mmc_data *data;
    1091                 :            : 
    1092                 :          3 :         data = host->data;
    1093                 :          3 :         BUG_ON(!data);
    1094                 :            : 
    1095                 :          3 :         log_event("FDA<", host->mrq, host->cmd);
    1096                 :            :         pr_debug("finish_data(error %d, stop %d, sbc %d)\n",
    1097                 :            :                data->error, data->stop ? 1 : 0,
    1098                 :            :                host->mrq->sbc ? 1 : 0);
    1099                 :            : 
    1100                 :          3 :         host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
    1101                 :            :         bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
    1102                 :            : 
    1103                 :          3 :         data->bytes_xfered = data->error ? 0 : (data->blksz * data->blocks);
    1104                 :            : 
    1105                 :          3 :         host->data_complete = 1;
    1106                 :            : 
    1107                 :          3 :         if (host->cmd) {
    1108                 :            :                 /*
    1109                 :            :                  * Data managed to finish before the
    1110                 :            :                  * command completed. Make sure we do
    1111                 :            :                  * things in the proper order.
    1112                 :            :                  */
    1113                 :            :                 pr_debug("Finished early - HSTS %x\n",
    1114                 :            :                          bcm2835_sdhost_read(host, SDHSTS));
    1115                 :            :         }
    1116                 :            :         else
    1117                 :          3 :                 bcm2835_sdhost_transfer_complete(host);
    1118                 :          3 :         log_event("FDA>", host->mrq, host->cmd);
    1119                 :          3 : }
    1120                 :            : 
    1121                 :          3 : static void bcm2835_sdhost_transfer_complete(struct bcm2835_host *host)
    1122                 :            : {
    1123                 :            :         struct mmc_data *data;
    1124                 :            : 
    1125                 :          3 :         BUG_ON(host->cmd);
    1126                 :          3 :         BUG_ON(!host->data);
    1127                 :          3 :         BUG_ON(!host->data_complete);
    1128                 :            : 
    1129                 :            :         data = host->data;
    1130                 :          3 :         host->data = NULL;
    1131                 :            : 
    1132                 :          3 :         log_event("TCM<", data, data->error);
    1133                 :            :         pr_debug("transfer_complete(error %d, stop %d)\n",
    1134                 :            :                data->error, data->stop ? 1 : 0);
    1135                 :            : 
    1136                 :            :         /*
    1137                 :            :          * Need to send CMD12 if -
    1138                 :            :          * a) open-ended multiblock transfer (no CMD23)
    1139                 :            :          * b) error in multiblock transfer
    1140                 :            :          */
    1141                 :          3 :         if (host->mrq->stop && (data->error || !host->use_sbc)) {
    1142                 :          3 :                 if (bcm2835_sdhost_send_command(host, host->mrq->stop)) {
    1143                 :            :                         /* No busy, so poll for completion */
    1144                 :          3 :                         if (!host->use_busy)
    1145                 :          1 :                                 bcm2835_sdhost_finish_command(host, NULL);
    1146                 :            : 
    1147                 :          3 :                         if (host->delay_after_this_stop)
    1148                 :            :                                 do_gettimeofday(&host->stop_time);
    1149                 :            :                 }
    1150                 :            :         } else {
    1151                 :          3 :                 bcm2835_sdhost_wait_transfer_complete(host);
    1152                 :          3 :                 tasklet_schedule(&host->finish_tasklet);
    1153                 :            :         }
    1154                 :          3 :         log_event("TCM>", data, 0);
    1155                 :          3 : }
    1156                 :            : 
    1157                 :            : /* If irq_flags is valid, the caller is in a thread context and is allowed
    1158                 :            :    to sleep */
    1159                 :          3 : static void bcm2835_sdhost_finish_command(struct bcm2835_host *host,
    1160                 :            :                                           unsigned long *irq_flags)
    1161                 :            : {
    1162                 :            :         u32 sdcmd;
    1163                 :            :         u32 retries;
    1164                 :            : #ifdef DEBUG
    1165                 :            :         struct timeval before, after;
    1166                 :            :         int timediff = 0;
    1167                 :            : #endif
    1168                 :            : 
    1169                 :          3 :         log_event("FCM<", host->mrq, host->cmd);
    1170                 :            :         pr_debug("finish_command(%x)\n", bcm2835_sdhost_read(host, SDCMD));
    1171                 :            : 
    1172                 :          3 :         BUG_ON(!host->cmd || !host->mrq);
    1173                 :            : 
    1174                 :            :         /* Poll quickly at first */
    1175                 :            : 
    1176                 :          3 :         retries = host->cmd_quick_poll_retries;
    1177                 :          3 :         if (!retries) {
    1178                 :            :                 /* Work out how many polls take 1us by timing 10us */
    1179                 :            :                 struct timeval start, now;
    1180                 :            :                 int us_diff;
    1181                 :            : 
    1182                 :            :                 retries = 1;
    1183                 :            :                 do {
    1184                 :            :                         int i;
    1185                 :            : 
    1186                 :          3 :                         retries *= 2;
    1187                 :            : 
    1188                 :            :                         do_gettimeofday(&start);
    1189                 :            : 
    1190                 :          3 :                         for (i = 0; i < retries; i++) {
    1191                 :          3 :                                 cpu_relax();
    1192                 :            :                                 sdcmd = bcm2835_sdhost_read(host, SDCMD);
    1193                 :            :                         }
    1194                 :            : 
    1195                 :            :                         do_gettimeofday(&now);
    1196                 :          3 :                         us_diff = (now.tv_sec - start.tv_sec) * 1000000 +
    1197                 :          3 :                                 (now.tv_usec - start.tv_usec);
    1198                 :          3 :                 } while (us_diff < 10);
    1199                 :            : 
    1200                 :          3 :                 host->cmd_quick_poll_retries = ((retries * us_diff + 9)*CMD_DALLY_US)/10 + 1;
    1201                 :            :                 retries = 1; // We've already waited long enough this time
    1202                 :            :         }
    1203                 :            : 
    1204                 :          3 :         for (sdcmd = bcm2835_sdhost_read(host, SDCMD);
    1205                 :          3 :              (sdcmd & SDCMD_NEW_FLAG) && retries;
    1206                 :          2 :              retries--) {
    1207                 :          2 :                 cpu_relax();
    1208                 :            :                 sdcmd = bcm2835_sdhost_read(host, SDCMD);
    1209                 :            :         }
    1210                 :            : 
    1211                 :          3 :         if (!retries) {
    1212                 :            :                 unsigned long wait_max;
    1213                 :            : 
    1214                 :          2 :                 if (!irq_flags) {
    1215                 :            :                         /* Schedule the work */
    1216                 :          0 :                         log_event("CWWQ", 0, 0);
    1217                 :          0 :                         schedule_work(&host->cmd_wait_wq);
    1218                 :            :                         return;
    1219                 :            :                 }
    1220                 :            : 
    1221                 :            :                 /* Wait max 100 ms */
    1222                 :          2 :                 wait_max = jiffies + msecs_to_jiffies(100);
    1223                 :          2 :                 while (time_before(jiffies, wait_max)) {
    1224                 :          2 :                         spin_unlock_irqrestore(&host->lock, *irq_flags);
    1225                 :          2 :                         usleep_range(1, 10);
    1226                 :          2 :                         spin_lock_irqsave(&host->lock, *irq_flags);
    1227                 :            :                         sdcmd = bcm2835_sdhost_read(host, SDCMD);
    1228                 :          2 :                         if (!(sdcmd & SDCMD_NEW_FLAG))
    1229                 :            :                                 break;
    1230                 :            :                 }
    1231                 :            :         }
    1232                 :            : 
    1233                 :            :         /* Check for errors */
    1234                 :          3 :         if (sdcmd & SDCMD_NEW_FLAG) {
    1235                 :          0 :                 if (host->debug) {
    1236                 :          0 :                         pr_err("%s: command %d never completed.\n",
    1237                 :            :                                mmc_hostname(host->mmc), host->cmd->opcode);
    1238                 :          0 :                         bcm2835_sdhost_dumpregs(host);
    1239                 :            :                 }
    1240                 :          0 :                 host->cmd->error = -EILSEQ;
    1241                 :          0 :                 tasklet_schedule(&host->finish_tasklet);
    1242                 :          0 :                 return;
    1243                 :          3 :         } else if (sdcmd & SDCMD_FAIL_FLAG) {
    1244                 :            :                 u32 sdhsts = bcm2835_sdhost_read(host, SDHSTS);
    1245                 :            : 
    1246                 :            :                 /* Clear the errors */
    1247                 :            :                 bcm2835_sdhost_write(host, SDHSTS_ERROR_MASK, SDHSTS);
    1248                 :            : 
    1249                 :          3 :                 if (host->debug)
    1250                 :          0 :                         pr_info("%s: error detected - CMD %x, HSTS %03x, EDM %x\n",
    1251                 :            :                                 mmc_hostname(host->mmc), sdcmd, sdhsts,
    1252                 :            :                                 bcm2835_sdhost_read(host, SDEDM));
    1253                 :            : 
    1254                 :          3 :                 if ((sdhsts & SDHSTS_CRC7_ERROR) &&
    1255                 :          0 :                     (host->cmd->opcode == 1)) {
    1256                 :          0 :                         if (host->debug)
    1257                 :          0 :                                 pr_info("%s: ignoring CRC7 error for CMD1\n",
    1258                 :            :                                         mmc_hostname(host->mmc));
    1259                 :            :                 } else {
    1260                 :            :                         u32 edm, fsm;
    1261                 :            : 
    1262                 :          3 :                         if (sdhsts & SDHSTS_CMD_TIME_OUT) {
    1263                 :          3 :                                 if (host->debug)
    1264                 :          0 :                                         pr_warn("%s: command %d timeout\n",
    1265                 :            :                                                mmc_hostname(host->mmc),
    1266                 :            :                                                host->cmd->opcode);
    1267                 :          3 :                                 host->cmd->error = -ETIMEDOUT;
    1268                 :            :                         } else {
    1269                 :          0 :                                 pr_warn("%s: unexpected command %d error\n",
    1270                 :            :                                        mmc_hostname(host->mmc),
    1271                 :            :                                        host->cmd->opcode);
    1272                 :          0 :                                 host->cmd->error = -EILSEQ;
    1273                 :            :                         }
    1274                 :            : 
    1275                 :          3 :                         edm = readl(host->ioaddr + SDEDM);
    1276                 :          3 :                         fsm = edm & SDEDM_FSM_MASK;
    1277                 :          3 :                         if (fsm == SDEDM_FSM_READWAIT ||
    1278                 :          3 :                             fsm == SDEDM_FSM_WRITESTART1)
    1279                 :          0 :                                 writel(edm | SDEDM_FORCE_DATA_MODE,
    1280                 :            :                                        host->ioaddr + SDEDM);
    1281                 :          3 :                         tasklet_schedule(&host->finish_tasklet);
    1282                 :          3 :                         return;
    1283                 :            :                 }
    1284                 :            :         }
    1285                 :            : 
    1286                 :          3 :         if (host->cmd->flags & MMC_RSP_PRESENT) {
    1287                 :          3 :                 if (host->cmd->flags & MMC_RSP_136) {
    1288                 :            :                         int i;
    1289                 :          3 :                         for (i = 0; i < 4; i++)
    1290                 :          3 :                                 host->cmd->resp[3 - i] = bcm2835_sdhost_read(host, SDRSP0 + i*4);
    1291                 :            :                         pr_debug("%s: finish_command %08x %08x %08x %08x\n",
    1292                 :            :                                  mmc_hostname(host->mmc),
    1293                 :            :                                  host->cmd->resp[0], host->cmd->resp[1], host->cmd->resp[2], host->cmd->resp[3]);
    1294                 :          3 :                         log_event("RSP ", host->cmd->resp[0], host->cmd->resp[1]);
    1295                 :            :                 } else {
    1296                 :          3 :                         host->cmd->resp[0] = bcm2835_sdhost_read(host, SDRSP0);
    1297                 :            :                         pr_debug("%s: finish_command %08x\n",
    1298                 :            :                                  mmc_hostname(host->mmc),
    1299                 :            :                                  host->cmd->resp[0]);
    1300                 :          3 :                         log_event("RSP ", host->cmd->resp[0], 0);
    1301                 :            :                 }
    1302                 :            :         }
    1303                 :            : 
    1304                 :          3 :         if (host->cmd == host->mrq->sbc) {
    1305                 :            :                 /* Finished CMD23, now send actual command. */
    1306                 :          2 :                 host->cmd = NULL;
    1307                 :          2 :                 if (bcm2835_sdhost_send_command(host, host->mrq->cmd)) {
    1308                 :          2 :                         if (host->data && host->dma_desc)
    1309                 :            :                                 /* DMA transfer starts now, PIO starts after irq */
    1310                 :          2 :                                 bcm2835_sdhost_start_dma(host);
    1311                 :            : 
    1312                 :          2 :                         if (!host->use_busy)
    1313                 :          2 :                                 bcm2835_sdhost_finish_command(host, NULL);
    1314                 :            :                 }
    1315                 :          3 :         } else if (host->cmd == host->mrq->stop) {
    1316                 :            :                 /* Finished CMD12 */
    1317                 :          3 :                 tasklet_schedule(&host->finish_tasklet);
    1318                 :            :         } else {
    1319                 :            :                 /* Processed actual command. */
    1320                 :          3 :                 host->cmd = NULL;
    1321                 :          3 :                 if (!host->data)
    1322                 :          3 :                         tasklet_schedule(&host->finish_tasklet);
    1323                 :          3 :                 else if (host->data_complete)
    1324                 :          2 :                         bcm2835_sdhost_transfer_complete(host);
    1325                 :            :         }
    1326                 :          3 :         log_event("FCM>", host->mrq, host->cmd);
    1327                 :            : }
    1328                 :            : 
    1329                 :          0 : static void bcm2835_sdhost_timeout(struct timer_list *t)
    1330                 :            : {
    1331                 :          0 :         struct bcm2835_host *host = from_timer(host, t, timer);
    1332                 :            :         unsigned long flags;
    1333                 :            : 
    1334                 :          0 :         spin_lock_irqsave(&host->lock, flags);
    1335                 :          0 :         log_event("TIM<", 0, 0);
    1336                 :            : 
    1337                 :          0 :         if (host->mrq) {
    1338                 :          0 :                 pr_err("%s: timeout waiting for hardware interrupt.\n",
    1339                 :            :                         mmc_hostname(host->mmc));
    1340                 :          0 :                 log_dump();
    1341                 :          0 :                 bcm2835_sdhost_dumpregs(host);
    1342                 :            : 
    1343                 :          0 :                 if (host->data) {
    1344                 :          0 :                         host->data->error = -ETIMEDOUT;
    1345                 :          0 :                         bcm2835_sdhost_finish_data(host);
    1346                 :            :                 } else {
    1347                 :          0 :                         if (host->cmd)
    1348                 :          0 :                                 host->cmd->error = -ETIMEDOUT;
    1349                 :            :                         else
    1350                 :          0 :                                 host->mrq->cmd->error = -ETIMEDOUT;
    1351                 :            : 
    1352                 :            :                         pr_debug("timeout_timer tasklet_schedule\n");
    1353                 :          0 :                         tasklet_schedule(&host->finish_tasklet);
    1354                 :            :                 }
    1355                 :            :         }
    1356                 :            : 
    1357                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1358                 :          0 : }
    1359                 :            : 
    1360                 :          3 : static void bcm2835_sdhost_busy_irq(struct bcm2835_host *host, u32 intmask)
    1361                 :            : {
    1362                 :          3 :         log_event("IRQB", host->cmd, intmask);
    1363                 :          3 :         if (!host->cmd) {
    1364                 :          0 :                 pr_err("%s: got command busy interrupt 0x%08x even "
    1365                 :            :                         "though no command operation was in progress.\n",
    1366                 :            :                         mmc_hostname(host->mmc), (unsigned)intmask);
    1367                 :          0 :                 bcm2835_sdhost_dumpregs(host);
    1368                 :          0 :                 return;
    1369                 :            :         }
    1370                 :            : 
    1371                 :          3 :         if (!host->use_busy) {
    1372                 :          0 :                 pr_err("%s: got command busy interrupt 0x%08x even "
    1373                 :            :                         "though not expecting one.\n",
    1374                 :            :                         mmc_hostname(host->mmc), (unsigned)intmask);
    1375                 :          0 :                 bcm2835_sdhost_dumpregs(host);
    1376                 :          0 :                 return;
    1377                 :            :         }
    1378                 :          3 :         host->use_busy = 0;
    1379                 :            : 
    1380                 :          3 :         if (intmask & SDHSTS_ERROR_MASK)
    1381                 :            :         {
    1382                 :          0 :                 pr_err("sdhost_busy_irq: intmask %x, data %p\n", intmask, host->mrq->data);
    1383                 :          0 :                 if (intmask & SDHSTS_CRC7_ERROR)
    1384                 :          0 :                         host->cmd->error = -EILSEQ;
    1385                 :          0 :                 else if (intmask & (SDHSTS_CRC16_ERROR |
    1386                 :            :                                     SDHSTS_FIFO_ERROR)) {
    1387                 :          0 :                         if (host->mrq->data)
    1388                 :          0 :                                 host->mrq->data->error = -EILSEQ;
    1389                 :            :                         else
    1390                 :          0 :                                 host->cmd->error = -EILSEQ;
    1391                 :          0 :                 } else if (intmask & SDHSTS_REW_TIME_OUT) {
    1392                 :          0 :                         if (host->mrq->data)
    1393                 :          0 :                                 host->mrq->data->error = -ETIMEDOUT;
    1394                 :            :                         else
    1395                 :          0 :                                 host->cmd->error = -ETIMEDOUT;
    1396                 :          0 :                 } else if (intmask & SDHSTS_CMD_TIME_OUT)
    1397                 :          0 :                         host->cmd->error = -ETIMEDOUT;
    1398                 :            : 
    1399                 :          0 :                 if (host->debug) {
    1400                 :          0 :                         log_dump();
    1401                 :          0 :                         bcm2835_sdhost_dumpregs(host);
    1402                 :            :                 }
    1403                 :            :         }
    1404                 :            :         else
    1405                 :          3 :                 bcm2835_sdhost_finish_command(host, NULL);
    1406                 :            : }
    1407                 :            : 
    1408                 :          3 : static void bcm2835_sdhost_data_irq(struct bcm2835_host *host, u32 intmask)
    1409                 :            : {
    1410                 :            :         /* There are no dedicated data/space available interrupt
    1411                 :            :            status bits, so it is necessary to use the single shared
    1412                 :            :            data/space available FIFO status bits. It is therefore not
    1413                 :            :            an error to get here when there is no data transfer in
    1414                 :            :            progress. */
    1415                 :          3 :         log_event("IRQD", host->data, intmask);
    1416                 :          3 :         if (!host->data)
    1417                 :          3 :                 return;
    1418                 :            : 
    1419                 :          3 :         if (intmask & (SDHSTS_CRC16_ERROR |
    1420                 :            :                        SDHSTS_FIFO_ERROR |
    1421                 :            :                        SDHSTS_REW_TIME_OUT)) {
    1422                 :          0 :                 if (intmask & (SDHSTS_CRC16_ERROR |
    1423                 :            :                                SDHSTS_FIFO_ERROR))
    1424                 :          0 :                         host->data->error = -EILSEQ;
    1425                 :            :                 else
    1426                 :          0 :                         host->data->error = -ETIMEDOUT;
    1427                 :            : 
    1428                 :          0 :                 if (host->debug) {
    1429                 :          0 :                         log_dump();
    1430                 :          0 :                         bcm2835_sdhost_dumpregs(host);
    1431                 :            :                 }
    1432                 :            :         }
    1433                 :            : 
    1434                 :          3 :         if (host->data->error) {
    1435                 :          0 :                 bcm2835_sdhost_finish_data(host);
    1436                 :          3 :         } else if (host->data->flags & MMC_DATA_WRITE) {
    1437                 :            :                 /* Use the block interrupt for writes after the first block */
    1438                 :          3 :                 host->hcfg &= ~(SDHCFG_DATA_IRPT_EN);
    1439                 :          3 :                 host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
    1440                 :            :                 bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
    1441                 :          3 :                 bcm2835_sdhost_transfer_pio(host);
    1442                 :            :         } else {
    1443                 :          3 :                 bcm2835_sdhost_transfer_pio(host);
    1444                 :          3 :                 host->blocks--;
    1445                 :          3 :                 if ((host->blocks == 0) || host->data->error)
    1446                 :          3 :                         bcm2835_sdhost_finish_data(host);
    1447                 :            :         }
    1448                 :            : }
    1449                 :            : 
    1450                 :          3 : static void bcm2835_sdhost_block_irq(struct bcm2835_host *host, u32 intmask)
    1451                 :            : {
    1452                 :          3 :         log_event("IRQK", host->data, intmask);
    1453                 :          3 :         if (!host->data) {
    1454                 :          0 :                 pr_err("%s: got block interrupt 0x%08x even "
    1455                 :            :                         "though no data operation was in progress.\n",
    1456                 :            :                         mmc_hostname(host->mmc), (unsigned)intmask);
    1457                 :          0 :                 bcm2835_sdhost_dumpregs(host);
    1458                 :          3 :                 return;
    1459                 :            :         }
    1460                 :            : 
    1461                 :          3 :         if (intmask & (SDHSTS_CRC16_ERROR |
    1462                 :            :                        SDHSTS_FIFO_ERROR |
    1463                 :            :                        SDHSTS_REW_TIME_OUT)) {
    1464                 :          0 :                 if (intmask & (SDHSTS_CRC16_ERROR |
    1465                 :            :                                SDHSTS_FIFO_ERROR))
    1466                 :          0 :                         host->data->error = -EILSEQ;
    1467                 :            :                 else
    1468                 :          0 :                         host->data->error = -ETIMEDOUT;
    1469                 :            : 
    1470                 :          0 :                 if (host->debug) {
    1471                 :          0 :                         log_dump();
    1472                 :          0 :                         bcm2835_sdhost_dumpregs(host);
    1473                 :            :                 }
    1474                 :            :         }
    1475                 :            : 
    1476                 :          3 :         if (!host->dma_desc) {
    1477                 :          3 :                 BUG_ON(!host->blocks);
    1478                 :          3 :                 if (host->data->error || (--host->blocks == 0)) {
    1479                 :          3 :                         bcm2835_sdhost_finish_data(host);
    1480                 :            :                 } else {
    1481                 :          0 :                         bcm2835_sdhost_transfer_pio(host);
    1482                 :            :                 }
    1483                 :          0 :         } else if (host->data->flags & MMC_DATA_WRITE) {
    1484                 :          0 :                 bcm2835_sdhost_finish_data(host);
    1485                 :            :         }
    1486                 :            : }
    1487                 :            : 
    1488                 :          3 : static irqreturn_t bcm2835_sdhost_irq(int irq, void *dev_id)
    1489                 :            : {
    1490                 :            :         irqreturn_t result = IRQ_NONE;
    1491                 :            :         struct bcm2835_host *host = dev_id;
    1492                 :            :         u32 intmask;
    1493                 :            : 
    1494                 :            :         spin_lock(&host->lock);
    1495                 :            : 
    1496                 :            :         intmask = bcm2835_sdhost_read(host, SDHSTS);
    1497                 :          3 :         log_event("IRQ<", intmask, 0);
    1498                 :            : 
    1499                 :            :         bcm2835_sdhost_write(host,
    1500                 :            :                              SDHSTS_BUSY_IRPT |
    1501                 :            :                              SDHSTS_BLOCK_IRPT |
    1502                 :            :                              SDHSTS_SDIO_IRPT |
    1503                 :            :                              SDHSTS_DATA_FLAG,
    1504                 :            :                              SDHSTS);
    1505                 :            : 
    1506                 :          3 :         if (intmask & SDHSTS_BLOCK_IRPT) {
    1507                 :          3 :                 bcm2835_sdhost_block_irq(host, intmask);
    1508                 :            :                 result = IRQ_HANDLED;
    1509                 :            :         }
    1510                 :            : 
    1511                 :          3 :         if (intmask & SDHSTS_BUSY_IRPT) {
    1512                 :          3 :                 bcm2835_sdhost_busy_irq(host, intmask);
    1513                 :            :                 result = IRQ_HANDLED;
    1514                 :            :         }
    1515                 :            : 
    1516                 :            :         /* There is no true data interrupt status bit, so it is
    1517                 :            :            necessary to qualify the data flag with the interrupt
    1518                 :            :            enable bit */
    1519                 :          3 :         if ((intmask & SDHSTS_DATA_FLAG) &&
    1520                 :          3 :             (host->hcfg & SDHCFG_DATA_IRPT_EN)) {
    1521                 :          3 :                 bcm2835_sdhost_data_irq(host, intmask);
    1522                 :            :                 result = IRQ_HANDLED;
    1523                 :            :         }
    1524                 :            : 
    1525                 :          3 :         log_event("IRQ>", bcm2835_sdhost_read(host, SDHSTS), 0);
    1526                 :            :         spin_unlock(&host->lock);
    1527                 :            : 
    1528                 :          3 :         return result;
    1529                 :            : }
    1530                 :            : 
    1531                 :          3 : void bcm2835_sdhost_set_clock(struct bcm2835_host *host, unsigned int clock)
    1532                 :            : {
    1533                 :            :         int div = 0; /* Initialized for compiler warning */
    1534                 :            :         unsigned int input_clock = clock;
    1535                 :            :         unsigned long flags;
    1536                 :            : 
    1537                 :          3 :         if (host->debug)
    1538                 :          0 :                 pr_info("%s: set_clock(%d)\n", mmc_hostname(host->mmc), clock);
    1539                 :            : 
    1540                 :          3 :         if (host->overclock_50 && (clock == 50*MHZ))
    1541                 :          0 :                 clock = host->overclock_50 * MHZ + (MHZ - 1);
    1542                 :            : 
    1543                 :            :         /* The SDCDIV register has 11 bits, and holds (div - 2).
    1544                 :            :            But in data mode the max is 50MHz wihout a minimum, and only the
    1545                 :            :            bottom 3 bits are used. Since the switch over is automatic (unless
    1546                 :            :            we have marked the card as slow...), chosen values have to make
    1547                 :            :            sense in both modes.
    1548                 :            :            Ident mode must be 100-400KHz, so can range check the requested
    1549                 :            :            clock. CMD15 must be used to return to data mode, so this can be
    1550                 :            :            monitored.
    1551                 :            : 
    1552                 :            :            clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz
    1553                 :            :                            4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz
    1554                 :            : 
    1555                 :            :                          623->400KHz/27.8MHz
    1556                 :            :                          reset value (507)->491159/50MHz
    1557                 :            : 
    1558                 :            :            BUT, the 3-bit clock divisor in data mode is too small if the
    1559                 :            :            core clock is higher than 250MHz, so instead use the SLOW_CARD
    1560                 :            :            configuration bit to force the use of the ident clock divisor
    1561                 :            :            at all times.
    1562                 :            :         */
    1563                 :            : 
    1564                 :          3 :         host->mmc->actual_clock = 0;
    1565                 :            : 
    1566                 :          3 :         if (host->firmware_sets_cdiv) {
    1567                 :          2 :                 u32 msg[3] = { clock, 0, 0 };
    1568                 :            : 
    1569                 :          2 :                 rpi_firmware_property(rpi_firmware_get(NULL),
    1570                 :            :                                       RPI_FIRMWARE_SET_SDHOST_CLOCK,
    1571                 :            :                                       &msg, sizeof(msg));
    1572                 :            : 
    1573                 :          2 :                 clock = max(msg[1], msg[2]);
    1574                 :          2 :                 spin_lock_irqsave(&host->lock, flags);
    1575                 :            :         } else {
    1576                 :          1 :                 spin_lock_irqsave(&host->lock, flags);
    1577                 :          1 :                 if (clock < 100000) {
    1578                 :            :                         /* Can't stop the clock, but make it as slow as
    1579                 :            :                          * possible to show willing
    1580                 :            :                          */
    1581                 :          1 :                         host->cdiv = SDCDIV_MAX_CDIV;
    1582                 :            :                         bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
    1583                 :            :                         spin_unlock_irqrestore(&host->lock, flags);
    1584                 :          3 :                         return;
    1585                 :            :                 }
    1586                 :            : 
    1587                 :          1 :                 div = host->max_clk / clock;
    1588                 :          1 :                 if (div < 2)
    1589                 :            :                         div = 2;
    1590                 :          1 :                 if ((host->max_clk / div) > clock)
    1591                 :          0 :                         div++;
    1592                 :          1 :                 div -= 2;
    1593                 :            : 
    1594                 :          1 :                 if (div > SDCDIV_MAX_CDIV)
    1595                 :            :                         div = SDCDIV_MAX_CDIV;
    1596                 :            : 
    1597                 :          1 :                 clock = host->max_clk / (div + 2);
    1598                 :            : 
    1599                 :          1 :                 host->cdiv = div;
    1600                 :            :                 bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
    1601                 :            : 
    1602                 :          1 :                 if (host->debug)
    1603                 :          0 :                         pr_info("%s: clock=%d -> max_clk=%d, cdiv=%x "
    1604                 :            :                                 "(actual clock %d)\n",
    1605                 :            :                                 mmc_hostname(host->mmc), input_clock,
    1606                 :            :                                 host->max_clk, host->cdiv,
    1607                 :            :                                 clock);
    1608                 :            :         }
    1609                 :            : 
    1610                 :            :         /* Calibrate some delays */
    1611                 :            : 
    1612                 :          3 :         host->ns_per_fifo_word = (1000000000/clock) *
    1613                 :          3 :                 ((host->mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
    1614                 :            : 
    1615                 :          3 :         if (input_clock == 50 * MHZ) {
    1616                 :          2 :                 if (clock > input_clock) {
    1617                 :            :                         /* Save the closest value, to make it easier
    1618                 :            :                            to reduce in the event of error */
    1619                 :          0 :                         host->overclock_50 = (clock/MHZ);
    1620                 :            : 
    1621                 :          0 :                         if (clock != host->overclock) {
    1622                 :          0 :                                 pr_info("%s: overclocking to %dHz\n",
    1623                 :            :                                         mmc_hostname(host->mmc), clock);
    1624                 :          0 :                                 host->overclock = clock;
    1625                 :            :                         }
    1626                 :          2 :                 } else if (host->overclock) {
    1627                 :          0 :                         host->overclock = 0;
    1628                 :          0 :                         if (clock == 50 * MHZ)
    1629                 :          0 :                                 pr_warn("%s: cancelling overclock\n",
    1630                 :            :                                         mmc_hostname(host->mmc));
    1631                 :            :                 }
    1632                 :          3 :         } else if (input_clock == 0) {
    1633                 :            :                 /* Reset the preferred overclock when the clock is stopped.
    1634                 :            :                  * This always happens during initialisation. */
    1635                 :          2 :                 host->overclock_50 = host->user_overclock_50;
    1636                 :          2 :                 host->overclock = 0;
    1637                 :            :         }
    1638                 :            : 
    1639                 :            :         /* Set the timeout to 500ms */
    1640                 :          3 :         bcm2835_sdhost_write(host, clock/2, SDTOUT);
    1641                 :            : 
    1642                 :          3 :         host->mmc->actual_clock = clock;
    1643                 :          3 :         host->clock = input_clock;
    1644                 :          3 :         host->reset_clock = 0;
    1645                 :            : 
    1646                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1647                 :            : }
    1648                 :            : 
    1649                 :          3 : static void bcm2835_sdhost_request(struct mmc_host *mmc, struct mmc_request *mrq)
    1650                 :            : {
    1651                 :            :         struct bcm2835_host *host;
    1652                 :            :         unsigned long flags;
    1653                 :            :         u32 edm, fsm;
    1654                 :            : 
    1655                 :            :         host = mmc_priv(mmc);
    1656                 :            : 
    1657                 :          3 :         if (host->debug) {
    1658                 :          0 :                 struct mmc_command *cmd = mrq->cmd;
    1659                 :          0 :                 BUG_ON(!cmd);
    1660                 :          0 :                 if (cmd->data)
    1661                 :          0 :                         pr_info("%s: cmd %d 0x%x (flags 0x%x) - %s %d*%d\n",
    1662                 :            :                                 mmc_hostname(mmc),
    1663                 :            :                                 cmd->opcode, cmd->arg, cmd->flags,
    1664                 :            :                                 (cmd->data->flags & MMC_DATA_READ) ?
    1665                 :            :                                 "read" : "write", cmd->data->blocks,
    1666                 :            :                                 cmd->data->blksz);
    1667                 :            :                 else
    1668                 :          0 :                         pr_info("%s: cmd %d 0x%x (flags 0x%x)\n",
    1669                 :            :                                 mmc_hostname(mmc),
    1670                 :            :                                 cmd->opcode, cmd->arg, cmd->flags);
    1671                 :            :         }
    1672                 :            : 
    1673                 :            :         /* Reset the error statuses in case this is a retry */
    1674                 :          3 :         if (mrq->sbc)
    1675                 :          2 :                 mrq->sbc->error = 0;
    1676                 :          3 :         if (mrq->cmd)
    1677                 :          3 :                 mrq->cmd->error = 0;
    1678                 :          3 :         if (mrq->data)
    1679                 :          3 :                 mrq->data->error = 0;
    1680                 :          3 :         if (mrq->stop)
    1681                 :          3 :                 mrq->stop->error = 0;
    1682                 :            : 
    1683                 :          3 :         if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
    1684                 :          0 :                 pr_err("%s: unsupported block size (%d bytes)\n",
    1685                 :            :                        mmc_hostname(mmc), mrq->data->blksz);
    1686                 :          0 :                 mrq->cmd->error = -EINVAL;
    1687                 :          0 :                 mmc_request_done(mmc, mrq);
    1688                 :          0 :                 return;
    1689                 :            :         }
    1690                 :            : 
    1691                 :          3 :         if (host->use_dma && mrq->data &&
    1692                 :          3 :             (mrq->data->blocks > host->pio_limit))
    1693                 :          3 :                 bcm2835_sdhost_prepare_dma(host, mrq->data);
    1694                 :            : 
    1695                 :          3 :         if (host->reset_clock)
    1696                 :          0 :             bcm2835_sdhost_set_clock(host, host->clock);
    1697                 :            : 
    1698                 :          3 :         spin_lock_irqsave(&host->lock, flags);
    1699                 :            : 
    1700                 :          3 :         WARN_ON(host->mrq != NULL);
    1701                 :          3 :         host->mrq = mrq;
    1702                 :            : 
    1703                 :            :         edm = bcm2835_sdhost_read(host, SDEDM);
    1704                 :          3 :         fsm = edm & SDEDM_FSM_MASK;
    1705                 :            : 
    1706                 :          3 :         log_event("REQ<", mrq, edm);
    1707                 :          3 :         if ((fsm != SDEDM_FSM_IDENTMODE) &&
    1708                 :            :             (fsm != SDEDM_FSM_DATAMODE)) {
    1709                 :          0 :                 log_event("REQ!", mrq, edm);
    1710                 :          0 :                 if (host->debug) {
    1711                 :          0 :                         pr_warn("%s: previous command (%d) not complete (EDM %x)\n",
    1712                 :            :                                mmc_hostname(host->mmc),
    1713                 :            :                                bcm2835_sdhost_read(host, SDCMD) & SDCMD_CMD_MASK,
    1714                 :            :                                edm);
    1715                 :          0 :                         log_dump();
    1716                 :          0 :                         bcm2835_sdhost_dumpregs(host);
    1717                 :            :                 }
    1718                 :          0 :                 mrq->cmd->error = -EILSEQ;
    1719                 :          0 :                 tasklet_schedule(&host->finish_tasklet);
    1720                 :          0 :                 spin_unlock_irqrestore(&host->lock, flags);
    1721                 :            :                 return;
    1722                 :            :         }
    1723                 :            : 
    1724                 :          3 :         host->use_sbc = !!mrq->sbc &&
    1725                 :          2 :                 (host->mrq->data->flags & USE_CMD23_FLAGS);
    1726                 :          3 :         if (host->use_sbc) {
    1727                 :          2 :                 if (bcm2835_sdhost_send_command(host, mrq->sbc)) {
    1728                 :          2 :                         if (!host->use_busy)
    1729                 :          2 :                                 bcm2835_sdhost_finish_command(host, &flags);
    1730                 :            :                 }
    1731                 :          3 :         } else if (bcm2835_sdhost_send_command(host, mrq->cmd)) {
    1732                 :          3 :                 if (host->data && host->dma_desc)
    1733                 :            :                         /* DMA transfer starts now, PIO starts after irq */
    1734                 :          3 :                         bcm2835_sdhost_start_dma(host);
    1735                 :            : 
    1736                 :          3 :                 if (!host->use_busy)
    1737                 :          3 :                         bcm2835_sdhost_finish_command(host, &flags);
    1738                 :            :         }
    1739                 :            : 
    1740                 :          3 :         log_event("CMD ", mrq->cmd->opcode,
    1741                 :            :                    mrq->data ? (u32)mrq->data->blksz : 0);
    1742                 :            : 
    1743                 :          3 :         log_event("REQ>", mrq, 0);
    1744                 :          3 :         spin_unlock_irqrestore(&host->lock, flags);
    1745                 :            : }
    1746                 :            : 
    1747                 :          3 : static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
    1748                 :            : {
    1749                 :            : 
    1750                 :            :         struct bcm2835_host *host = mmc_priv(mmc);
    1751                 :            :         unsigned long flags;
    1752                 :            : 
    1753                 :          3 :         if (host->debug)
    1754                 :          0 :                 pr_info("%s: ios clock %d, pwr %d, bus_width %d, "
    1755                 :            :                         "timing %d, vdd %d, drv_type %d\n",
    1756                 :            :                         mmc_hostname(mmc),
    1757                 :            :                         ios->clock, ios->power_mode, ios->bus_width,
    1758                 :            :                         ios->timing, ios->signal_voltage, ios->drv_type);
    1759                 :            : 
    1760                 :          3 :         spin_lock_irqsave(&host->lock, flags);
    1761                 :            : 
    1762                 :          3 :         log_event("IOS<", ios->clock, 0);
    1763                 :            : 
    1764                 :            :         /* set bus width */
    1765                 :          3 :         host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
    1766                 :          3 :         if (ios->bus_width == MMC_BUS_WIDTH_4)
    1767                 :          3 :                 host->hcfg |= SDHCFG_WIDE_EXT_BUS;
    1768                 :            : 
    1769                 :          3 :         host->hcfg |= SDHCFG_WIDE_INT_BUS;
    1770                 :            : 
    1771                 :            :         /* Disable clever clock switching, to cope with fast core clocks */
    1772                 :          3 :         host->hcfg |= SDHCFG_SLOW_CARD;
    1773                 :            : 
    1774                 :            :         bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
    1775                 :            : 
    1776                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1777                 :            : 
    1778                 :          3 :         if (!ios->clock || ios->clock != host->clock)
    1779                 :          3 :                 bcm2835_sdhost_set_clock(host, ios->clock);
    1780                 :          3 : }
    1781                 :            : 
    1782                 :            : static struct mmc_host_ops bcm2835_sdhost_ops = {
    1783                 :            :         .request = bcm2835_sdhost_request,
    1784                 :            :         .set_ios = bcm2835_sdhost_set_ios,
    1785                 :            :         .hw_reset = bcm2835_sdhost_reset,
    1786                 :            : };
    1787                 :            : 
    1788                 :          0 : static void bcm2835_sdhost_cmd_wait_work(struct work_struct *work)
    1789                 :            : {
    1790                 :            :         struct bcm2835_host *host;
    1791                 :            :         unsigned long flags;
    1792                 :            : 
    1793                 :          0 :         host = container_of(work, struct bcm2835_host, cmd_wait_wq);
    1794                 :            : 
    1795                 :          0 :         spin_lock_irqsave(&host->lock, flags);
    1796                 :            : 
    1797                 :          0 :         log_event("CWK<", host->cmd, host->mrq);
    1798                 :            : 
    1799                 :            :         /*
    1800                 :            :          * If this tasklet gets rescheduled while running, it will
    1801                 :            :          * be run again afterwards but without any active request.
    1802                 :            :          */
    1803                 :          0 :         if (!host->mrq) {
    1804                 :          0 :                 spin_unlock_irqrestore(&host->lock, flags);
    1805                 :          0 :                 return;
    1806                 :            :         }
    1807                 :            : 
    1808                 :          0 :         bcm2835_sdhost_finish_command(host, &flags);
    1809                 :            : 
    1810                 :          0 :         log_event("CWK>", host->cmd, 0);
    1811                 :            : 
    1812                 :          0 :         spin_unlock_irqrestore(&host->lock, flags);
    1813                 :            : }
    1814                 :            : 
    1815                 :          3 : static void bcm2835_sdhost_tasklet_finish(unsigned long param)
    1816                 :            : {
    1817                 :            :         struct bcm2835_host *host;
    1818                 :            :         unsigned long flags;
    1819                 :            :         struct mmc_request *mrq;
    1820                 :            :         struct dma_chan *terminate_chan = NULL;
    1821                 :            : 
    1822                 :          3 :         host = (struct bcm2835_host *)param;
    1823                 :            : 
    1824                 :          3 :         spin_lock_irqsave(&host->lock, flags);
    1825                 :            : 
    1826                 :          3 :         log_event("TSK<", host->mrq, 0);
    1827                 :            :         /*
    1828                 :            :          * If this tasklet gets rescheduled while running, it will
    1829                 :            :          * be run again afterwards but without any active request.
    1830                 :            :          */
    1831                 :          3 :         if (!host->mrq) {
    1832                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
    1833                 :          3 :                 return;
    1834                 :            :         }
    1835                 :            : 
    1836                 :          3 :         del_timer(&host->timer);
    1837                 :            : 
    1838                 :          3 :         mrq = host->mrq;
    1839                 :            : 
    1840                 :            :         /* Drop the overclock after any data corruption, or after any
    1841                 :            :          * error while overclocked. Ignore errors for status commands,
    1842                 :            :          * as they are likely when a card is ejected. */
    1843                 :          3 :         if (host->overclock) {
    1844                 :          0 :                 if ((mrq->cmd && mrq->cmd->error &&
    1845                 :          0 :                      (mrq->cmd->opcode != MMC_SEND_STATUS)) ||
    1846                 :          0 :                     (mrq->data && mrq->data->error) ||
    1847                 :          0 :                     (mrq->stop && mrq->stop->error) ||
    1848                 :          0 :                     (mrq->sbc && mrq->sbc->error)) {
    1849                 :          0 :                         host->overclock_50--;
    1850                 :          0 :                         pr_warn("%s: reducing overclock due to errors\n",
    1851                 :            :                                 mmc_hostname(host->mmc));
    1852                 :          0 :                         host->reset_clock = 1;
    1853                 :          0 :                         mrq->cmd->error = -ETIMEDOUT;
    1854                 :          0 :                         mrq->cmd->retries = 1;
    1855                 :            :                 }
    1856                 :            :         }
    1857                 :            : 
    1858                 :          3 :         host->mrq = NULL;
    1859                 :          3 :         host->cmd = NULL;
    1860                 :          3 :         host->data = NULL;
    1861                 :            : 
    1862                 :          3 :         host->dma_desc = NULL;
    1863                 :          3 :         terminate_chan = host->dma_chan;
    1864                 :          3 :         host->dma_chan = NULL;
    1865                 :            : 
    1866                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1867                 :            : 
    1868                 :          3 :         if (terminate_chan)
    1869                 :            :         {
    1870                 :            :                 int err = dmaengine_terminate_all(terminate_chan);
    1871                 :          0 :                 if (err)
    1872                 :          0 :                         pr_err("%s: failed to terminate DMA (%d)\n",
    1873                 :            :                                mmc_hostname(host->mmc), err);
    1874                 :            :         }
    1875                 :            : 
    1876                 :            :         /* The SDHOST block doesn't report any errors for a disconnected
    1877                 :            :            interface. All cards and SDIO devices should report some supported
    1878                 :            :            voltage range, so a zero response to SEND_OP_COND, IO_SEND_OP_COND
    1879                 :            :            or APP_SEND_OP_COND can be treated as an error. */
    1880                 :          3 :         if (((mrq->cmd->opcode == MMC_SEND_OP_COND) ||
    1881                 :          3 :              (mrq->cmd->opcode == SD_IO_SEND_OP_COND) ||
    1882                 :          3 :              (mrq->cmd->opcode == SD_APP_OP_COND)) &&
    1883                 :          3 :             (mrq->cmd->error == 0) &&
    1884                 :          3 :             (mrq->cmd->resp[0] == 0)) {
    1885                 :          0 :                 mrq->cmd->error = -ETIMEDOUT;
    1886                 :          0 :                 if (host->debug)
    1887                 :          0 :                         pr_info("%s: faking timeout due to zero OCR\n",
    1888                 :            :                                 mmc_hostname(host->mmc));
    1889                 :            :         }
    1890                 :            : 
    1891                 :          3 :         mmc_request_done(host->mmc, mrq);
    1892                 :          3 :         log_event("TSK>", mrq, 0);
    1893                 :            : }
    1894                 :            : 
    1895                 :          3 : int bcm2835_sdhost_add_host(struct bcm2835_host *host)
    1896                 :            : {
    1897                 :            :         struct mmc_host *mmc;
    1898                 :            :         struct dma_slave_config cfg;
    1899                 :            :         char pio_limit_string[20];
    1900                 :            :         int ret;
    1901                 :            : 
    1902                 :          3 :         mmc = host->mmc;
    1903                 :            : 
    1904                 :          3 :         if (!mmc->f_max || mmc->f_max > host->max_clk)
    1905                 :          3 :                 mmc->f_max = host->max_clk;
    1906                 :          3 :         mmc->f_min = host->max_clk / SDCDIV_MAX_CDIV;
    1907                 :            : 
    1908                 :          3 :         mmc->max_busy_timeout =  (~(unsigned int)0)/(mmc->f_max/1000);
    1909                 :            : 
    1910                 :            :         pr_debug("f_max %d, f_min %d, max_busy_timeout %d\n",
    1911                 :            :                  mmc->f_max, mmc->f_min, mmc->max_busy_timeout);
    1912                 :            : 
    1913                 :            :         /* host controller capabilities */
    1914                 :          3 :         mmc->caps |=
    1915                 :            :                 MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
    1916                 :            :                 MMC_CAP_NEEDS_POLL | MMC_CAP_HW_RESET | MMC_CAP_ERASE |
    1917                 :            :                 ((ALLOW_CMD23_READ|ALLOW_CMD23_WRITE) * MMC_CAP_CMD23);
    1918                 :            : 
    1919                 :          3 :         spin_lock_init(&host->lock);
    1920                 :            : 
    1921                 :          3 :         if (host->allow_dma) {
    1922                 :          3 :                 if (IS_ERR_OR_NULL(host->dma_chan_rxtx)) {
    1923                 :          0 :                         pr_err("%s: unable to initialise DMA channel. "
    1924                 :            :                                "Falling back to PIO\n",
    1925                 :            :                                mmc_hostname(mmc));
    1926                 :          0 :                         host->use_dma = false;
    1927                 :            :                 } else {
    1928                 :          3 :                         cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
    1929                 :          3 :                         cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
    1930                 :          3 :                         cfg.slave_id = 13;              /* DREQ channel */
    1931                 :            : 
    1932                 :            :                         /* Validate the slave configurations */
    1933                 :            : 
    1934                 :          3 :                         cfg.direction = DMA_MEM_TO_DEV;
    1935                 :          3 :                         cfg.src_addr = 0;
    1936                 :          3 :                         cfg.dst_addr = host->bus_addr + SDDATA;
    1937                 :            : 
    1938                 :            :                         ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
    1939                 :            : 
    1940                 :          3 :                         if (ret == 0) {
    1941                 :          3 :                                 host->dma_cfg_tx = cfg;
    1942                 :            : 
    1943                 :          3 :                                 cfg.direction = DMA_DEV_TO_MEM;
    1944                 :          3 :                                 cfg.src_addr = host->bus_addr + SDDATA;
    1945                 :          3 :                                 cfg.dst_addr = 0;
    1946                 :            : 
    1947                 :          3 :                                 ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
    1948                 :            :                         }
    1949                 :            : 
    1950                 :          3 :                         if (ret == 0) {
    1951                 :          3 :                                 host->dma_cfg_rx = cfg;
    1952                 :            : 
    1953                 :          3 :                                 host->use_dma = true;
    1954                 :            :                         } else {
    1955                 :          0 :                                 pr_err("%s: unable to configure DMA channel. "
    1956                 :            :                                        "Falling back to PIO\n",
    1957                 :            :                                        mmc_hostname(mmc));
    1958                 :          0 :                                 dma_release_channel(host->dma_chan_rxtx);
    1959                 :          0 :                                 host->dma_chan_rxtx = NULL;
    1960                 :          0 :                                 host->use_dma = false;
    1961                 :            :                         }
    1962                 :            :                 }
    1963                 :            :         } else {
    1964                 :          0 :                 host->use_dma = false;
    1965                 :            :         }
    1966                 :            : 
    1967                 :          3 :         mmc->max_segs = 128;
    1968                 :          3 :         mmc->max_req_size = 524288;
    1969                 :          3 :         mmc->max_seg_size = mmc->max_req_size;
    1970                 :          3 :         mmc->max_blk_size = 512;
    1971                 :          3 :         mmc->max_blk_count =  65535;
    1972                 :            : 
    1973                 :            :         /* report supported voltage ranges */
    1974                 :          3 :         mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
    1975                 :            : 
    1976                 :          3 :         tasklet_init(&host->finish_tasklet,
    1977                 :            :                 bcm2835_sdhost_tasklet_finish, (unsigned long)host);
    1978                 :            : 
    1979                 :          3 :         INIT_WORK(&host->cmd_wait_wq, bcm2835_sdhost_cmd_wait_work);
    1980                 :            : 
    1981                 :          3 :         timer_setup(&host->timer, bcm2835_sdhost_timeout, 0);
    1982                 :            : 
    1983                 :          3 :         bcm2835_sdhost_init(host, 0);
    1984                 :            : 
    1985                 :          3 :         ret = request_irq(host->irq, bcm2835_sdhost_irq, 0 /*IRQF_SHARED*/,
    1986                 :            :                                   mmc_hostname(mmc), host);
    1987                 :          3 :         if (ret) {
    1988                 :          0 :                 pr_err("%s: failed to request IRQ %d: %d\n",
    1989                 :            :                        mmc_hostname(mmc), host->irq, ret);
    1990                 :            :                 goto untasklet;
    1991                 :            :         }
    1992                 :            : 
    1993                 :          3 :         mmc_add_host(mmc);
    1994                 :            : 
    1995                 :          3 :         pio_limit_string[0] = '\0';
    1996                 :          3 :         if (host->use_dma && (host->pio_limit > 0))
    1997                 :          3 :                 sprintf(pio_limit_string, " (>%d)", host->pio_limit);
    1998                 :          3 :         pr_info("%s: %s loaded - DMA %s%s\n",
    1999                 :            :                 mmc_hostname(mmc), DRIVER_NAME,
    2000                 :            :                 host->use_dma ? "enabled" : "disabled",
    2001                 :            :                 pio_limit_string);
    2002                 :            : 
    2003                 :          3 :         return 0;
    2004                 :            : 
    2005                 :            : untasklet:
    2006                 :          0 :         tasklet_kill(&host->finish_tasklet);
    2007                 :            : 
    2008                 :          0 :         return ret;
    2009                 :            : }
    2010                 :            : 
    2011                 :          3 : static int bcm2835_sdhost_probe(struct platform_device *pdev)
    2012                 :            : {
    2013                 :          3 :         struct device *dev = &pdev->dev;
    2014                 :          3 :         struct device_node *node = dev->of_node;
    2015                 :            :         struct clk *clk;
    2016                 :            :         struct resource *iomem;
    2017                 :            :         struct bcm2835_host *host;
    2018                 :            :         struct mmc_host *mmc;
    2019                 :            :         const __be32 *addr;
    2020                 :            :         u32 msg[3];
    2021                 :            :         int na;
    2022                 :            :         int ret;
    2023                 :            : 
    2024                 :            :         pr_debug("bcm2835_sdhost_probe\n");
    2025                 :          3 :         mmc = mmc_alloc_host(sizeof(*host), dev);
    2026                 :          3 :         if (!mmc)
    2027                 :            :                 return -ENOMEM;
    2028                 :            : 
    2029                 :          3 :         mmc->ops = &bcm2835_sdhost_ops;
    2030                 :            :         host = mmc_priv(mmc);
    2031                 :          3 :         host->mmc = mmc;
    2032                 :          3 :         host->pio_timeout = msecs_to_jiffies(500);
    2033                 :          3 :         host->pio_limit = 1;
    2034                 :          3 :         host->max_delay = 1; /* Warn if over 1ms */
    2035                 :          3 :         host->allow_dma = 1;
    2036                 :          3 :         spin_lock_init(&host->lock);
    2037                 :            : 
    2038                 :          3 :         iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    2039                 :          3 :         host->ioaddr = devm_ioremap_resource(dev, iomem);
    2040                 :          3 :         if (IS_ERR(host->ioaddr)) {
    2041                 :            :                 ret = PTR_ERR(host->ioaddr);
    2042                 :          0 :                 goto err;
    2043                 :            :         }
    2044                 :            : 
    2045                 :          3 :         na = of_n_addr_cells(node);
    2046                 :          3 :         addr = of_get_address(node, 0, NULL, NULL);
    2047                 :          3 :         if (!addr) {
    2048                 :          0 :                 dev_err(dev, "could not get DMA-register address\n");
    2049                 :          0 :                 return -ENODEV;
    2050                 :            :         }
    2051                 :          3 :         host->bus_addr = (phys_addr_t)of_read_number(addr, na);
    2052                 :            :         pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
    2053                 :            :                  (unsigned long)host->ioaddr,
    2054                 :            :                  (unsigned long)iomem->start,
    2055                 :            :                  (unsigned long)host->bus_addr);
    2056                 :            : 
    2057                 :          3 :         if (node) {
    2058                 :            :                 /* Read any custom properties */
    2059                 :          3 :                 of_property_read_u32(node,
    2060                 :            :                                      "brcm,delay-after-stop",
    2061                 :            :                                      &host->delay_after_stop);
    2062                 :          3 :                 of_property_read_u32(node,
    2063                 :            :                                      "brcm,overclock-50",
    2064                 :            :                                      &host->user_overclock_50);
    2065                 :          3 :                 of_property_read_u32(node,
    2066                 :            :                                      "brcm,pio-limit",
    2067                 :            :                                      &host->pio_limit);
    2068                 :          3 :                 host->allow_dma =
    2069                 :          3 :                         !of_property_read_bool(node, "brcm,force-pio");
    2070                 :          3 :                 host->debug = of_property_read_bool(node, "brcm,debug");
    2071                 :            :         }
    2072                 :            : 
    2073                 :          3 :         host->dma_chan = NULL;
    2074                 :          3 :         host->dma_desc = NULL;
    2075                 :            : 
    2076                 :            :         /* Formally recognise the other way of disabling DMA */
    2077                 :          3 :         if (host->pio_limit == 0x7fffffff)
    2078                 :          0 :                 host->allow_dma = false;
    2079                 :            : 
    2080                 :          3 :         if (host->allow_dma) {
    2081                 :          3 :                 if (node) {
    2082                 :          3 :                         host->dma_chan_rxtx =
    2083                 :          3 :                                 dma_request_slave_channel(dev, "rx-tx");
    2084                 :          3 :                         if (!host->dma_chan_rxtx)
    2085                 :          0 :                                 host->dma_chan_rxtx =
    2086                 :          0 :                                         dma_request_slave_channel(dev, "tx");
    2087                 :          3 :                         if (!host->dma_chan_rxtx)
    2088                 :          0 :                                 host->dma_chan_rxtx =
    2089                 :          0 :                                         dma_request_slave_channel(dev, "rx");
    2090                 :            :                 } else {
    2091                 :            :                         dma_cap_mask_t mask;
    2092                 :            : 
    2093                 :            :                         dma_cap_zero(mask);
    2094                 :            :                         /* we don't care about the channel, any would work */
    2095                 :            :                         dma_cap_set(DMA_SLAVE, mask);
    2096                 :          0 :                         host->dma_chan_rxtx =
    2097                 :          0 :                                 dma_request_channel(mask, NULL, NULL);
    2098                 :            :                 }
    2099                 :            :         }
    2100                 :            : 
    2101                 :          3 :         clk = devm_clk_get(dev, NULL);
    2102                 :          3 :         if (IS_ERR(clk)) {
    2103                 :            :                 ret = PTR_ERR(clk);
    2104                 :          3 :                 if (ret == -EPROBE_DEFER)
    2105                 :          3 :                         dev_info(dev, "could not get clk, deferring probe\n");
    2106                 :            :                 else
    2107                 :          0 :                         dev_err(dev, "could not get clk\n");
    2108                 :            :                 goto err;
    2109                 :            :         }
    2110                 :            : 
    2111                 :          3 :         host->max_clk = clk_get_rate(clk);
    2112                 :            : 
    2113                 :          3 :         host->irq = platform_get_irq(pdev, 0);
    2114                 :          3 :         if (host->irq <= 0) {
    2115                 :          0 :                 dev_err(dev, "get IRQ failed\n");
    2116                 :            :                 ret = -EINVAL;
    2117                 :          0 :                 goto err;
    2118                 :            :         }
    2119                 :            : 
    2120                 :            :         pr_debug(" - max_clk %lx, irq %d\n",
    2121                 :            :                  (unsigned long)host->max_clk,
    2122                 :            :                  (int)host->irq);
    2123                 :            : 
    2124                 :          3 :         log_init(dev, iomem->start - host->bus_addr);
    2125                 :            : 
    2126                 :          3 :         if (node)
    2127                 :          3 :                 mmc_of_parse(mmc);
    2128                 :            :         else
    2129                 :          0 :                 mmc->caps |= MMC_CAP_4_BIT_DATA;
    2130                 :            : 
    2131                 :          3 :         msg[0] = 0;
    2132                 :          3 :         msg[1] = ~0;
    2133                 :          3 :         msg[2] = ~0;
    2134                 :            : 
    2135                 :          3 :         rpi_firmware_property(rpi_firmware_get(NULL),
    2136                 :            :                               RPI_FIRMWARE_SET_SDHOST_CLOCK,
    2137                 :            :                               &msg, sizeof(msg));
    2138                 :            : 
    2139                 :          3 :         host->firmware_sets_cdiv = (msg[1] != ~0);
    2140                 :            : 
    2141                 :          3 :         ret = bcm2835_sdhost_add_host(host);
    2142                 :          3 :         if (ret)
    2143                 :            :                 goto err;
    2144                 :            : 
    2145                 :            :         platform_set_drvdata(pdev, host);
    2146                 :            : 
    2147                 :            :         pr_debug("bcm2835_sdhost_probe -> OK\n");
    2148                 :            : 
    2149                 :          3 :         return 0;
    2150                 :            : 
    2151                 :            : err:
    2152                 :            :         pr_debug("bcm2835_sdhost_probe -> err %d\n", ret);
    2153                 :          3 :         if (host->dma_chan_rxtx)
    2154                 :          3 :                 dma_release_channel(host->dma_chan_rxtx);
    2155                 :          3 :         mmc_free_host(mmc);
    2156                 :            : 
    2157                 :          3 :         return ret;
    2158                 :            : }
    2159                 :            : 
    2160                 :          0 : static int bcm2835_sdhost_remove(struct platform_device *pdev)
    2161                 :            : {
    2162                 :            :         struct bcm2835_host *host = platform_get_drvdata(pdev);
    2163                 :            : 
    2164                 :            :         pr_debug("bcm2835_sdhost_remove\n");
    2165                 :            : 
    2166                 :          0 :         mmc_remove_host(host->mmc);
    2167                 :            : 
    2168                 :            :         bcm2835_sdhost_set_power(host, false);
    2169                 :            : 
    2170                 :          0 :         free_irq(host->irq, host);
    2171                 :            : 
    2172                 :          0 :         del_timer_sync(&host->timer);
    2173                 :            : 
    2174                 :          0 :         tasklet_kill(&host->finish_tasklet);
    2175                 :          0 :         if (host->dma_chan_rxtx)
    2176                 :          0 :                 dma_release_channel(host->dma_chan_rxtx);
    2177                 :          0 :         mmc_free_host(host->mmc);
    2178                 :            :         platform_set_drvdata(pdev, NULL);
    2179                 :            : 
    2180                 :            :         pr_debug("bcm2835_sdhost_remove - OK\n");
    2181                 :          0 :         return 0;
    2182                 :            : }
    2183                 :            : 
    2184                 :            : static const struct of_device_id bcm2835_sdhost_match[] = {
    2185                 :            :         { .compatible = "brcm,bcm2835-sdhost" },
    2186                 :            :         { }
    2187                 :            : };
    2188                 :            : MODULE_DEVICE_TABLE(of, bcm2835_sdhost_match);
    2189                 :            : 
    2190                 :            : static struct platform_driver bcm2835_sdhost_driver = {
    2191                 :            :         .probe      = bcm2835_sdhost_probe,
    2192                 :            :         .remove     = bcm2835_sdhost_remove,
    2193                 :            :         .driver     = {
    2194                 :            :                 .name           = DRIVER_NAME,
    2195                 :            :                 .owner          = THIS_MODULE,
    2196                 :            :                 .of_match_table = bcm2835_sdhost_match,
    2197                 :            :         },
    2198                 :            : };
    2199                 :          3 : module_platform_driver(bcm2835_sdhost_driver);
    2200                 :            : 
    2201                 :            : MODULE_ALIAS("platform:sdhost-bcm2835");
    2202                 :            : MODULE_DESCRIPTION("BCM2835 SDHost driver");
    2203                 :            : MODULE_LICENSE("GPL v2");
    2204                 :            : MODULE_AUTHOR("Phil Elwell");
    

Generated by: LCOV version 1.14