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