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