1 From 231ef69db2b015b7da9994ef893090206bf5f63f Mon Sep 17 00:00:00 2001
2 From: gellert <gellert@raspberrypi.org>
3 Date: Fri, 15 Aug 2014 16:35:06 +0100
4 Subject: [PATCH 032/304] MMC: added alternative MMC driver
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 mmc: Disable CMD23 transfers on all cards
11 Pending wire-level investigation of these types of transfers
12 and associated errors on bcm2835-mmc, disable for now. Fallback of
13 CMD18/CMD25 transfers will be used automatically by the MMC layer.
15 Reported/Tested-by: Gellert Weisz <gellert@raspberrypi.org>
17 mmc: bcm2835-mmc: enable DT support for all architectures
19 Both ARCH_BCM2835 and ARCH_BCM270x are built with OF now.
20 Enable Device Tree support for all architectures.
22 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
24 mmc: bcm2835-mmc: fix probe error handling
26 Probe error handling is broken in several places.
27 Simplify error handling by using device managed functions.
28 Replace pr_{err,info} with dev_{err,info}.
30 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
32 bcm2835-mmc: Add locks when accessing sdhost registers
34 bcm2835-mmc: Add range of debug options for slowing things down
36 bcm2835-mmc: Add option to disable some delays
38 bcm2835-mmc: Add option to disable MMC_QUIRK_BLK_NO_CMD23
40 bcm2835-mmc: Default to disabling MMC_QUIRK_BLK_NO_CMD23
42 bcm2835-mmc: Adding overclocking option
44 Allow a different clock speed to be substitued for a requested 50MHz.
45 This option is exposed using the "overclock_50" DT parameter.
46 Note that the mmc interface is restricted to EVEN integer divisions of
47 250MHz, and the highest sensible option is 63 (250/4 = 62.5), the
48 next being 125 (250/2) which is much too high.
52 bcm2835-mmc: Round up the overclock, so 62 works for 62.5Mhz
54 Also only warn once for each overclock setting.
56 mmc: bcm2835-mmc: Make available on ARCH_BCM2835
58 Make the bcm2835-mmc driver available for use on ARCH_BCM2835.
60 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
62 BCM270x_DT: add bcm2835-mmc entry
64 Add Device Tree entry for bcm2835-mmc.
65 In non-DT mode, don't add the device in the board file.
67 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
69 bcm2835-mmc: Don't overwrite MMC capabilities from DT
71 drivers/mmc/core/quirks.c | 6 +
72 drivers/mmc/host/Kconfig | 29 +
73 drivers/mmc/host/Makefile | 1 +
74 drivers/mmc/host/bcm2835-mmc.c | 1542 ++++++++++++++++++++++++++++++++++++++++
75 4 files changed, 1578 insertions(+)
76 create mode 100644 drivers/mmc/host/bcm2835-mmc.c
78 --- a/drivers/mmc/core/quirks.c
79 +++ b/drivers/mmc/core/quirks.c
80 @@ -53,6 +53,7 @@ static const struct mmc_fixup mmc_fixup_
82 void mmc_fixup_device(struct mmc_card *card, const struct mmc_fixup *table)
84 + extern unsigned mmc_debug;
85 const struct mmc_fixup *f;
86 u64 rev = cid_rev_card(card);
88 @@ -77,5 +78,10 @@ void mmc_fixup_device(struct mmc_card *c
89 f->vendor_fixup(card, f->data);
92 + /* SDHCI on BCM2708 - bug causes a certain sequence of CMD23 operations to fail.
93 + * Disable this flag for all cards (fall-back to CMD25/CMD18 multi-block transfers).
95 + if (mmc_debug & (1<<13))
96 + card->quirks |= MMC_QUIRK_BLK_NO_CMD23;
98 EXPORT_SYMBOL(mmc_fixup_device);
99 --- a/drivers/mmc/host/Kconfig
100 +++ b/drivers/mmc/host/Kconfig
103 comment "MMC/SD/SDIO Host Controller Drivers"
106 + tristate "MMC support on BCM2835"
107 + depends on MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835
109 + This selects the MMC Interface on BCM2835.
111 + If you have a controller with this interface, say Y or M here.
115 +config MMC_BCM2835_DMA
116 + bool "DMA support on BCM2835 Arasan controller"
117 + depends on MMC_BCM2835
119 + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
124 +config MMC_BCM2835_PIO_DMA_BARRIER
125 + int "Block count limit for PIO transfers"
126 + depends on MMC_BCM2835 && MMC_BCM2835_DMA
130 + The inclusive limit in bytes under which PIO will be used instead of DMA
132 + If unsure, say 2 here.
135 tristate "ARM AMBA Multimedia Card Interface support"
137 --- a/drivers/mmc/host/Makefile
138 +++ b/drivers/mmc/host/Makefile
139 @@ -18,6 +18,7 @@ obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c
140 obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
141 obj-$(CONFIG_MMC_SDHCI_F_SDH30) += sdhci_f_sdh30.o
142 obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
143 +obj-$(CONFIG_MMC_BCM2835) += bcm2835-mmc.o
144 obj-$(CONFIG_MMC_WBSD) += wbsd.o
145 obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
146 obj-$(CONFIG_MMC_MTK) += mtk-sd.o
148 +++ b/drivers/mmc/host/bcm2835-mmc.c
151 + * BCM2835 MMC host driver.
153 + * Author: Gellert Weisz <gellert@raspberrypi.org>
157 + * sdhci-bcm2708.c by Broadcom
158 + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
159 + * sdhci.c and sdhci-pci.c by Pierre Ossman
161 + * This program is free software; you can redistribute it and/or modify it
162 + * under the terms and conditions of the GNU General Public License,
163 + * version 2, as published by the Free Software Foundation.
165 + * This program is distributed in the hope it will be useful, but WITHOUT
166 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
167 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
170 + * You should have received a copy of the GNU General Public License
171 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
174 +#include <linux/delay.h>
175 +#include <linux/module.h>
176 +#include <linux/io.h>
177 +#include <linux/mmc/mmc.h>
178 +#include <linux/mmc/host.h>
179 +#include <linux/mmc/sd.h>
180 +#include <linux/scatterlist.h>
181 +#include <linux/of_address.h>
182 +#include <linux/of_irq.h>
183 +#include <linux/clk.h>
184 +#include <linux/platform_device.h>
185 +#include <linux/err.h>
186 +#include <linux/blkdev.h>
187 +#include <linux/dmaengine.h>
188 +#include <linux/dma-mapping.h>
189 +#include <linux/of_dma.h>
194 +#define DRIVER_NAME "mmc-bcm2835"
196 +#define DBG(f, x...) \
197 +pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
199 +#ifndef CONFIG_MMC_BCM2835_DMA
204 +/* the inclusive limit in bytes under which PIO will be used instead of DMA */
205 +#ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
206 +#define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
208 +#define PIO_DMA_BARRIER 00
211 +#define MIN_FREQ 400000
212 +#define TIMEOUT_VAL 0xE
213 +#define BCM2835_SDHCI_WRITE_DELAY(f) (((2 * 1000000) / f) + 1)
217 +unsigned mmc_debug2;
219 +struct bcm2835_host {
222 + void __iomem *ioaddr;
225 + struct mmc_host *mmc;
229 + int clock; /* Current clock speed */
230 + u8 pwr; /* Current voltage */
232 + unsigned int max_clk; /* Max possible freq */
233 + unsigned int timeout_clk; /* Timeout freq (KHz) */
234 + unsigned int clk_mul; /* Clock Muliplier value */
236 + struct tasklet_struct finish_tasklet; /* Tasklet structures */
238 + struct timer_list timer; /* Timer for timeouts */
240 + struct sg_mapping_iter sg_miter; /* SG state for PIO */
241 + unsigned int blocks; /* remaining PIO blocks */
243 + int irq; /* Device IRQ */
246 + u32 ier; /* cached registers */
248 + struct mmc_request *mrq; /* Current request */
249 + struct mmc_command *cmd; /* Current command */
250 + struct mmc_data *data; /* Current data request */
251 + unsigned int data_early:1; /* Data finished before cmd */
253 + wait_queue_head_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
260 + struct dma_chan *dma_chan_rx; /* DMA channel for reads */
261 + struct dma_chan *dma_chan_tx; /* DMA channel for writes */
262 + struct dma_async_tx_descriptor *tx_desc; /* descriptor */
266 + /*end of DMA part*/
268 + int max_delay; /* maximum length of time spent waiting */
270 + int flags; /* Host attributes */
271 +#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
272 +#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
273 +#define SDHCI_AUTO_CMD12 (1<<6) /* Auto CMD12 support */
274 +#define SDHCI_AUTO_CMD23 (1<<7) /* Auto CMD23 support */
275 +#define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
277 + u32 overclock_50; /* frequency to use when 50MHz is requested (in MHz) */
278 + u32 max_overclock; /* Highest reported */
282 +static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg, int from)
285 + lockdep_assert_held_once(&host->lock);
286 + writel(val, host->ioaddr + reg);
287 + udelay(BCM2835_SDHCI_WRITE_DELAY(max(host->clock, MIN_FREQ)));
289 + delay = ((mmc_debug >> 16) & 0xf) << ((mmc_debug >> 20) & 0xf);
290 + if (delay && !((1<<from) & mmc_debug2))
294 +static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
297 + lockdep_assert_held_once(&host->lock);
298 + writel(val, host->ioaddr + reg);
300 + delay = ((mmc_debug >> 24) & 0xf) << ((mmc_debug >> 28) & 0xf);
305 +static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
307 + lockdep_assert_held_once(&host->lock);
308 + return readl(host->ioaddr + reg);
311 +static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
313 + u32 oldval = (reg == SDHCI_COMMAND) ? host->shadow :
314 + bcm2835_mmc_readl(host, reg & ~3);
315 + u32 word_num = (reg >> 1) & 1;
316 + u32 word_shift = word_num * 16;
317 + u32 mask = 0xffff << word_shift;
318 + u32 newval = (oldval & ~mask) | (val << word_shift);
320 + if (reg == SDHCI_TRANSFER_MODE)
321 + host->shadow = newval;
323 + bcm2835_mmc_writel(host, newval, reg & ~3, 0);
327 +static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
329 + u32 oldval = bcm2835_mmc_readl(host, reg & ~3);
330 + u32 byte_num = reg & 3;
331 + u32 byte_shift = byte_num * 8;
332 + u32 mask = 0xff << byte_shift;
333 + u32 newval = (oldval & ~mask) | (val << byte_shift);
335 + bcm2835_mmc_writel(host, newval, reg & ~3, 1);
339 +static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
341 + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
342 + u32 word_num = (reg >> 1) & 1;
343 + u32 word_shift = word_num * 16;
344 + u32 word = (val >> word_shift) & 0xffff;
349 +static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
351 + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
352 + u32 byte_num = reg & 3;
353 + u32 byte_shift = byte_num * 8;
354 + u32 byte = (val >> byte_shift) & 0xff;
359 +static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
363 + ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
365 + /* change which requests generate IRQs - makes no difference to
366 + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
367 + bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE, 2);
371 +static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
373 + pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
374 + mmc_hostname(host->mmc));
376 + pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
377 + bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
378 + bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
379 + pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
380 + bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
381 + bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
382 + pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
383 + bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
384 + bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
385 + pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
386 + bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
387 + bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
388 + pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
389 + bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
390 + bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
391 + pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
392 + bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
393 + bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
394 + pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
395 + bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
396 + bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
397 + pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
398 + bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
399 + bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
400 + pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
401 + bcm2835_mmc_readw(host, SDHCI_ACMD12_ERR),
402 + bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
403 + pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
404 + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
405 + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
406 + pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
407 + bcm2835_mmc_readw(host, SDHCI_COMMAND),
408 + bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
409 + pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
410 + bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
412 + pr_debug(DRIVER_NAME ": ===========================================\n");
416 +static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
418 + unsigned long timeout;
419 + unsigned long flags;
421 + spin_lock_irqsave(&host->lock, flags);
422 + bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
424 + if (mask & SDHCI_RESET_ALL)
427 + /* Wait max 100 ms */
430 + /* hw clears the bit when it's done */
431 + while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
432 + if (timeout == 0) {
433 + pr_err("%s: Reset 0x%x never completed.\n",
434 + mmc_hostname(host->mmc), (int)mask);
435 + bcm2835_mmc_dumpregs(host);
439 + spin_unlock_irqrestore(&host->lock, flags);
441 + spin_lock_irqsave(&host->lock, flags);
444 + if (100-timeout > 10 && 100-timeout > host->max_delay) {
445 + host->max_delay = 100-timeout;
446 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
448 + spin_unlock_irqrestore(&host->lock, flags);
451 +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
453 +static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
455 + unsigned long flags;
457 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
459 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
461 + host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
462 + SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
463 + SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
464 + SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
465 + SDHCI_INT_RESPONSE;
467 + spin_lock_irqsave(&host->lock, flags);
468 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 3);
469 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 3);
470 + spin_unlock_irqrestore(&host->lock, flags);
473 + /* force clock reconfiguration */
475 + bcm2835_mmc_set_ios(host->mmc, &host->mmc->ios);
481 +static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
483 +static void bcm2835_mmc_dma_complete(void *param)
485 + struct bcm2835_host *host = param;
486 + struct dma_chan *dma_chan;
487 + unsigned long flags;
490 + spin_lock_irqsave(&host->lock, flags);
492 + if (host->data && !(host->data->flags & MMC_DATA_WRITE)) {
493 + /* otherwise handled in SDHCI IRQ */
494 + dma_chan = host->dma_chan_rx;
495 + dir_data = DMA_FROM_DEVICE;
497 + dma_unmap_sg(dma_chan->device->dev,
498 + host->data->sg, host->data->sg_len,
501 + bcm2835_mmc_finish_data(host);
504 + spin_unlock_irqrestore(&host->lock, flags);
507 +static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
509 + unsigned long flags;
510 + size_t blksize, len, chunk;
512 + u32 uninitialized_var(scratch);
515 + blksize = host->data->blksz;
518 + local_irq_save(flags);
521 + if (!sg_miter_next(&host->sg_miter))
524 + len = min(host->sg_miter.length, blksize);
527 + host->sg_miter.consumed = len;
529 + buf = host->sg_miter.addr;
533 + scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
537 + *buf = scratch & 0xFF;
546 + sg_miter_stop(&host->sg_miter);
548 + local_irq_restore(flags);
551 +static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
553 + unsigned long flags;
554 + size_t blksize, len, chunk;
558 + blksize = host->data->blksz;
563 + local_irq_save(flags);
566 + if (!sg_miter_next(&host->sg_miter))
569 + len = min(host->sg_miter.length, blksize);
572 + host->sg_miter.consumed = len;
574 + buf = host->sg_miter.addr;
577 + scratch |= (u32)*buf << (chunk * 8);
583 + if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
584 + mmc_raw_writel(host, scratch, SDHCI_BUFFER);
591 + sg_miter_stop(&host->sg_miter);
593 + local_irq_restore(flags);
597 +static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
601 + BUG_ON(!host->data);
603 + if (host->blocks == 0)
606 + if (host->data->flags & MMC_DATA_READ)
607 + mask = SDHCI_DATA_AVAILABLE;
609 + mask = SDHCI_SPACE_AVAILABLE;
611 + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
613 + if (host->data->flags & MMC_DATA_READ)
614 + bcm2835_bcm2835_mmc_read_block_pio(host);
616 + bcm2835_bcm2835_mmc_write_block_pio(host);
620 + /* QUIRK used in sdhci.c removes the 'if' */
621 + /* but it seems this is unnecessary */
622 + if (host->blocks == 0)
630 +static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
632 + u32 len, dir_data, dir_slave;
633 + struct dma_async_tx_descriptor *desc = NULL;
634 + struct dma_chan *dma_chan;
637 + WARN_ON(!host->data);
642 + if (host->blocks == 0)
645 + if (host->data->flags & MMC_DATA_READ) {
646 + dma_chan = host->dma_chan_rx;
647 + dir_data = DMA_FROM_DEVICE;
648 + dir_slave = DMA_DEV_TO_MEM;
650 + dma_chan = host->dma_chan_tx;
651 + dir_data = DMA_TO_DEVICE;
652 + dir_slave = DMA_MEM_TO_DEV;
655 + BUG_ON(!dma_chan->device);
656 + BUG_ON(!dma_chan->device->dev);
657 + BUG_ON(!host->data->sg);
659 + len = dma_map_sg(dma_chan->device->dev, host->data->sg,
660 + host->data->sg_len, dir_data);
662 + desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
664 + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
666 + dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
669 + unsigned long flags;
670 + spin_lock_irqsave(&host->lock, flags);
671 + bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
672 + SDHCI_INT_SPACE_AVAIL);
673 + host->tx_desc = desc;
674 + desc->callback = bcm2835_mmc_dma_complete;
675 + desc->callback_param = host;
676 + spin_unlock_irqrestore(&host->lock, flags);
677 + dmaengine_submit(desc);
678 + dma_async_issue_pending(dma_chan);
685 +static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
687 + u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
688 + u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
691 + host->ier = (host->ier & ~pio_irqs) | dma_irqs;
693 + host->ier = (host->ier & ~dma_irqs) | pio_irqs;
695 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 4);
696 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 4);
700 +static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
703 + struct mmc_data *data = cmd->data;
705 + WARN_ON(host->data);
707 + if (data || (cmd->flags & MMC_RSP_BUSY)) {
708 + count = TIMEOUT_VAL;
709 + bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
715 + /* Sanity checks */
716 + BUG_ON(data->blksz * data->blocks > 524288);
717 + BUG_ON(data->blksz > host->mmc->max_blk_size);
718 + BUG_ON(data->blocks > 65535);
721 + host->data_early = 0;
722 + host->data->bytes_xfered = 0;
725 + if (!(host->flags & SDHCI_REQ_USE_DMA)) {
728 + flags = SG_MITER_ATOMIC;
729 + if (host->data->flags & MMC_DATA_READ)
730 + flags |= SG_MITER_TO_SG;
732 + flags |= SG_MITER_FROM_SG;
733 + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
734 + host->blocks = data->blocks;
737 + host->use_dma = host->have_dma && data->blocks > PIO_DMA_BARRIER;
739 + bcm2835_mmc_set_transfer_irqs(host);
741 + /* Set the DMA boundary value and block size */
742 + bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
743 + data->blksz), SDHCI_BLOCK_SIZE);
744 + bcm2835_mmc_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
746 + BUG_ON(!host->data);
749 +static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
750 + struct mmc_command *cmd)
753 + struct mmc_data *data = cmd->data;
755 + if (data == NULL) {
756 + /* clear Auto CMD settings for no data CMDs */
757 + mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
758 + bcm2835_mmc_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
759 + SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
763 + WARN_ON(!host->data);
765 + mode = SDHCI_TRNS_BLK_CNT_EN;
767 + if ((mmc_op_multi(cmd->opcode) || data->blocks > 1)) {
768 + mode |= SDHCI_TRNS_MULTI;
771 + * If we are sending CMD23, CMD12 never gets sent
772 + * on successful completion (so no Auto-CMD12).
774 + if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
775 + mode |= SDHCI_TRNS_AUTO_CMD12;
776 + else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
777 + mode |= SDHCI_TRNS_AUTO_CMD23;
778 + bcm2835_mmc_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2, 5);
782 + if (data->flags & MMC_DATA_READ)
783 + mode |= SDHCI_TRNS_READ;
784 + if (host->flags & SDHCI_REQ_USE_DMA)
785 + mode |= SDHCI_TRNS_DMA;
787 + bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
790 +void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
794 + unsigned long timeout;
796 + WARN_ON(host->cmd);
798 + /* Wait max 10 ms */
801 + mask = SDHCI_CMD_INHIBIT;
802 + if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
803 + mask |= SDHCI_DATA_INHIBIT;
805 + /* We shouldn't wait for data inihibit for stop commands, even
806 + though they might use busy signaling */
807 + if (host->mrq->data && (cmd == host->mrq->data->stop))
808 + mask &= ~SDHCI_DATA_INHIBIT;
810 + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
811 + if (timeout == 0) {
812 + pr_err("%s: Controller never released inhibit bit(s).\n",
813 + mmc_hostname(host->mmc));
814 + bcm2835_mmc_dumpregs(host);
816 + tasklet_schedule(&host->finish_tasklet);
823 + if ((1000-timeout)/100 > 1 && (1000-timeout)/100 > host->max_delay) {
824 + host->max_delay = (1000-timeout)/100;
825 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
829 + if (!cmd->data && cmd->busy_timeout > 9000)
830 + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
832 + timeout += 10 * HZ;
833 + mod_timer(&host->timer, timeout);
837 + bcm2835_mmc_prepare_data(host, cmd);
839 + bcm2835_mmc_writel(host, cmd->arg, SDHCI_ARGUMENT, 6);
841 + bcm2835_mmc_set_transfer_mode(host, cmd);
843 + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
844 + pr_err("%s: Unsupported response type!\n",
845 + mmc_hostname(host->mmc));
846 + cmd->error = -EINVAL;
847 + tasklet_schedule(&host->finish_tasklet);
851 + if (!(cmd->flags & MMC_RSP_PRESENT))
852 + flags = SDHCI_CMD_RESP_NONE;
853 + else if (cmd->flags & MMC_RSP_136)
854 + flags = SDHCI_CMD_RESP_LONG;
855 + else if (cmd->flags & MMC_RSP_BUSY)
856 + flags = SDHCI_CMD_RESP_SHORT_BUSY;
858 + flags = SDHCI_CMD_RESP_SHORT;
860 + if (cmd->flags & MMC_RSP_CRC)
861 + flags |= SDHCI_CMD_CRC;
862 + if (cmd->flags & MMC_RSP_OPCODE)
863 + flags |= SDHCI_CMD_INDEX;
866 + flags |= SDHCI_CMD_DATA;
868 + bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
872 +static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
874 + struct mmc_data *data;
876 + BUG_ON(!host->data);
882 + data->bytes_xfered = 0;
884 + data->bytes_xfered = data->blksz * data->blocks;
887 + * Need to send CMD12 if -
888 + * a) open-ended multiblock transfer (no CMD23)
889 + * b) error in multiblock transfer
893 + !host->mrq->sbc)) {
896 + * The controller needs a reset of internal state machines
897 + * upon error conditions.
900 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
901 + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
904 + bcm2835_mmc_send_command(host, data->stop);
906 + tasklet_schedule(&host->finish_tasklet);
909 +static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
913 + BUG_ON(host->cmd == NULL);
915 + if (host->cmd->flags & MMC_RSP_PRESENT) {
916 + if (host->cmd->flags & MMC_RSP_136) {
917 + /* CRC is stripped so we need to do some shifting. */
918 + for (i = 0; i < 4; i++) {
919 + host->cmd->resp[i] = bcm2835_mmc_readl(host,
920 + SDHCI_RESPONSE + (3-i)*4) << 8;
922 + host->cmd->resp[i] |=
923 + bcm2835_mmc_readb(host,
924 + SDHCI_RESPONSE + (3-i)*4-1);
927 + host->cmd->resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
931 + host->cmd->error = 0;
933 + /* Finished CMD23, now send actual command. */
934 + if (host->cmd == host->mrq->sbc) {
936 + bcm2835_mmc_send_command(host, host->mrq->cmd);
938 + if (host->mrq->cmd->data && host->use_dma) {
939 + /* DMA transfer starts now, PIO starts after interrupt */
940 + bcm2835_mmc_transfer_dma(host);
944 + /* Processed actual command. */
945 + if (host->data && host->data_early)
946 + bcm2835_mmc_finish_data(host);
948 + if (!host->cmd->data)
949 + tasklet_schedule(&host->finish_tasklet);
956 +static void bcm2835_mmc_timeout_timer(unsigned long data)
958 + struct bcm2835_host *host;
959 + unsigned long flags;
961 + host = (struct bcm2835_host *)data;
963 + spin_lock_irqsave(&host->lock, flags);
966 + pr_err("%s: Timeout waiting for hardware interrupt.\n",
967 + mmc_hostname(host->mmc));
968 + bcm2835_mmc_dumpregs(host);
971 + host->data->error = -ETIMEDOUT;
972 + bcm2835_mmc_finish_data(host);
975 + host->cmd->error = -ETIMEDOUT;
977 + host->mrq->cmd->error = -ETIMEDOUT;
979 + tasklet_schedule(&host->finish_tasklet);
984 + spin_unlock_irqrestore(&host->lock, flags);
988 +static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
990 + if (!(host->flags & SDHCI_DEVICE_DEAD)) {
992 + host->ier |= SDHCI_INT_CARD_INT;
994 + host->ier &= ~SDHCI_INT_CARD_INT;
996 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 7);
997 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 7);
1002 +static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1004 + struct bcm2835_host *host = mmc_priv(mmc);
1005 + unsigned long flags;
1007 + spin_lock_irqsave(&host->lock, flags);
1009 + host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1011 + host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1013 + bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
1014 + spin_unlock_irqrestore(&host->lock, flags);
1017 +static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
1020 + BUG_ON(intmask == 0);
1023 + pr_err("%s: Got command interrupt 0x%08x even "
1024 + "though no command operation was in progress.\n",
1025 + mmc_hostname(host->mmc), (unsigned)intmask);
1026 + bcm2835_mmc_dumpregs(host);
1030 + if (intmask & SDHCI_INT_TIMEOUT)
1031 + host->cmd->error = -ETIMEDOUT;
1032 + else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1033 + SDHCI_INT_INDEX)) {
1034 + host->cmd->error = -EILSEQ;
1037 + if (host->cmd->error) {
1038 + tasklet_schedule(&host->finish_tasklet);
1042 + if (intmask & SDHCI_INT_RESPONSE)
1043 + bcm2835_mmc_finish_command(host);
1047 +static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
1049 + struct dma_chan *dma_chan;
1052 + BUG_ON(intmask == 0);
1054 + if (!host->data) {
1056 + * The "data complete" interrupt is also used to
1057 + * indicate that a busy state has ended. See comment
1058 + * above in sdhci_cmd_irq().
1060 + if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1061 + if (intmask & SDHCI_INT_DATA_END) {
1062 + bcm2835_mmc_finish_command(host);
1067 + pr_debug("%s: Got data interrupt 0x%08x even "
1068 + "though no data operation was in progress.\n",
1069 + mmc_hostname(host->mmc), (unsigned)intmask);
1070 + bcm2835_mmc_dumpregs(host);
1075 + if (intmask & SDHCI_INT_DATA_TIMEOUT)
1076 + host->data->error = -ETIMEDOUT;
1077 + else if (intmask & SDHCI_INT_DATA_END_BIT)
1078 + host->data->error = -EILSEQ;
1079 + else if ((intmask & SDHCI_INT_DATA_CRC) &&
1080 + SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
1081 + != MMC_BUS_TEST_R)
1082 + host->data->error = -EILSEQ;
1084 + if (host->use_dma) {
1085 + if (host->data->flags & MMC_DATA_WRITE) {
1086 + /* IRQ handled here */
1088 + dma_chan = host->dma_chan_tx;
1089 + dir_data = DMA_TO_DEVICE;
1090 + dma_unmap_sg(dma_chan->device->dev,
1091 + host->data->sg, host->data->sg_len,
1094 + bcm2835_mmc_finish_data(host);
1098 + if (host->data->error)
1099 + bcm2835_mmc_finish_data(host);
1101 + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
1102 + bcm2835_mmc_transfer_pio(host);
1104 + if (intmask & SDHCI_INT_DATA_END) {
1107 + * Data managed to finish before the
1108 + * command completed. Make sure we do
1109 + * things in the proper order.
1111 + host->data_early = 1;
1113 + bcm2835_mmc_finish_data(host);
1121 +static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
1123 + irqreturn_t result = IRQ_NONE;
1124 + struct bcm2835_host *host = dev_id;
1125 + u32 intmask, mask, unexpected = 0;
1126 + int max_loops = 16;
1128 + spin_lock(&host->lock);
1130 + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1132 + if (!intmask || intmask == 0xffffffff) {
1133 + result = IRQ_NONE;
1138 + /* Clear selected interrupts. */
1139 + mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
1140 + SDHCI_INT_BUS_POWER);
1141 + bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS, 8);
1144 + if (intmask & SDHCI_INT_CMD_MASK)
1145 + bcm2835_mmc_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
1147 + if (intmask & SDHCI_INT_DATA_MASK)
1148 + bcm2835_mmc_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
1150 + if (intmask & SDHCI_INT_BUS_POWER)
1151 + pr_err("%s: Card is consuming too much power!\n",
1152 + mmc_hostname(host->mmc));
1154 + if (intmask & SDHCI_INT_CARD_INT) {
1155 + bcm2835_mmc_enable_sdio_irq_nolock(host, false);
1156 + host->thread_isr |= SDHCI_INT_CARD_INT;
1157 + result = IRQ_WAKE_THREAD;
1160 + intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
1161 + SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
1162 + SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
1163 + SDHCI_INT_CARD_INT);
1166 + unexpected |= intmask;
1167 + bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS, 9);
1170 + if (result == IRQ_NONE)
1171 + result = IRQ_HANDLED;
1173 + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1174 + } while (intmask && --max_loops);
1176 + spin_unlock(&host->lock);
1179 + pr_err("%s: Unexpected interrupt 0x%08x.\n",
1180 + mmc_hostname(host->mmc), unexpected);
1181 + bcm2835_mmc_dumpregs(host);
1187 +static irqreturn_t bcm2835_mmc_thread_irq(int irq, void *dev_id)
1189 + struct bcm2835_host *host = dev_id;
1190 + unsigned long flags;
1193 + spin_lock_irqsave(&host->lock, flags);
1194 + isr = host->thread_isr;
1195 + host->thread_isr = 0;
1196 + spin_unlock_irqrestore(&host->lock, flags);
1198 + if (isr & SDHCI_INT_CARD_INT) {
1199 + sdio_run_irqs(host->mmc);
1201 + spin_lock_irqsave(&host->lock, flags);
1202 + if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
1203 + bcm2835_mmc_enable_sdio_irq_nolock(host, true);
1204 + spin_unlock_irqrestore(&host->lock, flags);
1207 + return isr ? IRQ_HANDLED : IRQ_NONE;
1212 +void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
1214 + int div = 0; /* Initialized for compiler warning */
1215 + int real_div = div, clk_mul = 1;
1217 + unsigned long timeout;
1218 + unsigned int input_clock = clock;
1220 + if (host->overclock_50 && (clock == 50000000))
1221 + clock = host->overclock_50 * 1000000 + 999999;
1223 + host->mmc->actual_clock = 0;
1225 + bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
1230 + /* Version 3.00 divisors must be a multiple of 2. */
1231 + if (host->max_clk <= clock)
1234 + for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1236 + if ((host->max_clk / div) <= clock)
1245 + clock = (host->max_clk * clk_mul) / real_div;
1246 + host->mmc->actual_clock = clock;
1248 + if ((clock > input_clock) && (clock > host->max_overclock)) {
1249 + pr_warn("%s: Overclocking to %dHz\n",
1250 + mmc_hostname(host->mmc), clock);
1251 + host->max_overclock = clock;
1254 + clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1255 + clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1256 + << SDHCI_DIVIDER_HI_SHIFT;
1257 + clk |= SDHCI_CLOCK_INT_EN;
1258 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1260 + /* Wait max 20 ms */
1262 + while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
1263 + & SDHCI_CLOCK_INT_STABLE)) {
1264 + if (timeout == 0) {
1265 + pr_err("%s: Internal clock never "
1266 + "stabilised.\n", mmc_hostname(host->mmc));
1267 + bcm2835_mmc_dumpregs(host);
1274 + if (20-timeout > 10 && 20-timeout > host->max_delay) {
1275 + host->max_delay = 20-timeout;
1276 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
1279 + clk |= SDHCI_CLOCK_CARD_EN;
1280 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1283 +static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
1285 + struct bcm2835_host *host;
1286 + unsigned long flags;
1288 + host = mmc_priv(mmc);
1290 + spin_lock_irqsave(&host->lock, flags);
1292 + WARN_ON(host->mrq != NULL);
1296 + if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1297 + bcm2835_mmc_send_command(host, mrq->sbc);
1299 + bcm2835_mmc_send_command(host, mrq->cmd);
1302 + spin_unlock_irqrestore(&host->lock, flags);
1304 + if (!(mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23)) && mrq->cmd->data && host->use_dma) {
1305 + /* DMA transfer starts now, PIO starts after interrupt */
1306 + bcm2835_mmc_transfer_dma(host);
1311 +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1314 + struct bcm2835_host *host = mmc_priv(mmc);
1315 + unsigned long flags;
1319 + pr_debug("bcm2835_mmc_set_ios: clock %d, pwr %d, bus_width %d, timing %d, vdd %d, drv_type %d\n",
1320 + ios->clock, ios->power_mode, ios->bus_width,
1321 + ios->timing, ios->signal_voltage, ios->drv_type);
1323 + spin_lock_irqsave(&host->lock, flags);
1325 + if (!ios->clock || ios->clock != host->clock) {
1326 + bcm2835_mmc_set_clock(host, ios->clock);
1327 + host->clock = ios->clock;
1330 + if (host->pwr != SDHCI_POWER_330) {
1331 + host->pwr = SDHCI_POWER_330;
1332 + bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1335 + ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
1337 + /* set bus width */
1338 + ctrl &= ~SDHCI_CTRL_8BITBUS;
1339 + if (ios->bus_width == MMC_BUS_WIDTH_4)
1340 + ctrl |= SDHCI_CTRL_4BITBUS;
1342 + ctrl &= ~SDHCI_CTRL_4BITBUS;
1344 + ctrl &= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
1347 + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1349 + * We only need to set Driver Strength if the
1350 + * preset value enable is not set.
1352 + ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
1353 + ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1354 + if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1355 + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1356 + else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1357 + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1359 + bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1361 + /* Reset SD Clock Enable */
1362 + clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
1363 + clk &= ~SDHCI_CLOCK_CARD_EN;
1364 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1366 + /* Re-enable SD Clock */
1367 + bcm2835_mmc_set_clock(host, host->clock);
1368 + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1372 + spin_unlock_irqrestore(&host->lock, flags);
1376 +static struct mmc_host_ops bcm2835_ops = {
1377 + .request = bcm2835_mmc_request,
1378 + .set_ios = bcm2835_mmc_set_ios,
1379 + .enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
1383 +static void bcm2835_mmc_tasklet_finish(unsigned long param)
1385 + struct bcm2835_host *host;
1386 + unsigned long flags;
1387 + struct mmc_request *mrq;
1389 + host = (struct bcm2835_host *)param;
1391 + spin_lock_irqsave(&host->lock, flags);
1394 + * If this tasklet gets rescheduled while running, it will
1395 + * be run again afterwards but without any active request.
1398 + spin_unlock_irqrestore(&host->lock, flags);
1402 + del_timer(&host->timer);
1407 + * The controller needs a reset of internal state machines
1408 + * upon error conditions.
1410 + if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1411 + ((mrq->cmd && mrq->cmd->error) ||
1412 + (mrq->data && (mrq->data->error ||
1413 + (mrq->data->stop && mrq->data->stop->error))))) {
1415 + spin_unlock_irqrestore(&host->lock, flags);
1416 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
1417 + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
1418 + spin_lock_irqsave(&host->lock, flags);
1423 + host->data = NULL;
1427 + spin_unlock_irqrestore(&host->lock, flags);
1428 + mmc_request_done(host->mmc, mrq);
1433 +static int bcm2835_mmc_add_host(struct bcm2835_host *host)
1435 + struct mmc_host *mmc = host->mmc;
1436 + struct device *dev = mmc->parent;
1438 + struct dma_slave_config cfg;
1442 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
1444 + host->clk_mul = 0;
1446 + mmc->f_max = host->max_clk;
1447 + mmc->f_max = host->max_clk;
1448 + mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
1450 + /* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
1451 + host->timeout_clk = mmc->f_max / 1000;
1452 + mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
1454 + /* host controller capabilities */
1455 + mmc->caps |= MMC_CAP_CMD23 | MMC_CAP_ERASE | MMC_CAP_NEEDS_POLL |
1456 + MMC_CAP_SDIO_IRQ | MMC_CAP_SD_HIGHSPEED |
1457 + MMC_CAP_MMC_HIGHSPEED | MMC_CAP_4_BIT_DATA;
1459 + mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
1461 + host->flags = SDHCI_AUTO_CMD23;
1463 + dev_info(dev, "mmc_debug:%x mmc_debug2:%x\n", mmc_debug, mmc_debug2);
1465 + dev_info(dev, "Forcing PIO mode\n");
1466 + host->have_dma = false;
1468 + if (IS_ERR_OR_NULL(host->dma_chan_tx) ||
1469 + IS_ERR_OR_NULL(host->dma_chan_rx)) {
1470 + dev_err(dev, "%s: Unable to initialise DMA channels. Falling back to PIO\n",
1472 + host->have_dma = false;
1474 + dev_info(dev, "DMA channels allocated");
1475 + host->have_dma = true;
1477 + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1478 + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1479 + cfg.slave_id = 11; /* DREQ channel */
1481 + cfg.direction = DMA_MEM_TO_DEV;
1483 + cfg.dst_addr = host->bus_addr + SDHCI_BUFFER;
1484 + ret = dmaengine_slave_config(host->dma_chan_tx, &cfg);
1486 + cfg.direction = DMA_DEV_TO_MEM;
1487 + cfg.src_addr = host->bus_addr + SDHCI_BUFFER;
1489 + ret = dmaengine_slave_config(host->dma_chan_rx, &cfg);
1492 + mmc->max_segs = 128;
1493 + mmc->max_req_size = 524288;
1494 + mmc->max_seg_size = mmc->max_req_size;
1495 + mmc->max_blk_size = 512;
1496 + mmc->max_blk_count = 65535;
1498 + /* report supported voltage ranges */
1499 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1501 + tasklet_init(&host->finish_tasklet,
1502 + bcm2835_mmc_tasklet_finish, (unsigned long)host);
1504 + setup_timer(&host->timer, bcm2835_mmc_timeout_timer, (unsigned long)host);
1505 + init_waitqueue_head(&host->buf_ready_int);
1507 + bcm2835_mmc_init(host, 0);
1508 + ret = devm_request_threaded_irq(dev, host->irq, bcm2835_mmc_irq,
1509 + bcm2835_mmc_thread_irq, IRQF_SHARED,
1510 + mmc_hostname(mmc), host);
1512 + dev_err(dev, "Failed to request IRQ %d: %d\n", host->irq, ret);
1517 + mmc_add_host(mmc);
1522 + tasklet_kill(&host->finish_tasklet);
1527 +static int bcm2835_mmc_probe(struct platform_device *pdev)
1529 + struct device *dev = &pdev->dev;
1530 + struct device_node *node = dev->of_node;
1532 + struct resource *iomem;
1533 + struct bcm2835_host *host;
1534 + struct mmc_host *mmc;
1535 + const __be32 *addr;
1538 + mmc = mmc_alloc_host(sizeof(*host), dev);
1542 + mmc->ops = &bcm2835_ops;
1543 + host = mmc_priv(mmc);
1545 + host->timeout = msecs_to_jiffies(1000);
1546 + spin_lock_init(&host->lock);
1548 + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1549 + host->ioaddr = devm_ioremap_resource(dev, iomem);
1550 + if (IS_ERR(host->ioaddr)) {
1551 + ret = PTR_ERR(host->ioaddr);
1555 + addr = of_get_address(node, 0, NULL, NULL);
1557 + dev_err(dev, "could not get DMA-register address\n");
1560 + host->bus_addr = be32_to_cpup(addr);
1561 + pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
1562 + (unsigned long)host->ioaddr,
1563 + (unsigned long)iomem->start,
1564 + (unsigned long)host->bus_addr);
1568 + host->dma_chan_tx = dma_request_slave_channel(dev, "tx");
1569 + host->dma_chan_rx = dma_request_slave_channel(dev, "rx");
1571 + dma_cap_mask_t mask;
1573 + dma_cap_zero(mask);
1574 + /* we don't care about the channel, any would work */
1575 + dma_cap_set(DMA_SLAVE, mask);
1576 + host->dma_chan_tx = dma_request_channel(mask, NULL, NULL);
1577 + host->dma_chan_rx = dma_request_channel(mask, NULL, NULL);
1580 + clk = devm_clk_get(dev, NULL);
1581 + if (IS_ERR(clk)) {
1582 + dev_err(dev, "could not get clk\n");
1583 + ret = PTR_ERR(clk);
1587 + host->max_clk = clk_get_rate(clk);
1589 + host->irq = platform_get_irq(pdev, 0);
1590 + if (host->irq <= 0) {
1591 + dev_err(dev, "get IRQ failed\n");
1597 + mmc_of_parse(mmc);
1599 + /* Read any custom properties */
1600 + of_property_read_u32(node,
1601 + "brcm,overclock-50",
1602 + &host->overclock_50);
1604 + mmc->caps |= MMC_CAP_4_BIT_DATA;
1607 + ret = bcm2835_mmc_add_host(host);
1611 + platform_set_drvdata(pdev, host);
1615 + mmc_free_host(mmc);
1620 +static int bcm2835_mmc_remove(struct platform_device *pdev)
1622 + struct bcm2835_host *host = platform_get_drvdata(pdev);
1623 + unsigned long flags;
1628 + scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1629 + if (scratch == (u32)-1)
1634 + spin_lock_irqsave(&host->lock, flags);
1636 + host->flags |= SDHCI_DEVICE_DEAD;
1639 + pr_err("%s: Controller removed during "
1640 + " transfer!\n", mmc_hostname(host->mmc));
1642 + host->mrq->cmd->error = -ENOMEDIUM;
1643 + tasklet_schedule(&host->finish_tasklet);
1646 + spin_unlock_irqrestore(&host->lock, flags);
1649 + mmc_remove_host(host->mmc);
1652 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
1654 + free_irq(host->irq, host);
1656 + del_timer_sync(&host->timer);
1658 + tasklet_kill(&host->finish_tasklet);
1660 + mmc_free_host(host->mmc);
1661 + platform_set_drvdata(pdev, NULL);
1667 +static const struct of_device_id bcm2835_mmc_match[] = {
1668 + { .compatible = "brcm,bcm2835-mmc" },
1671 +MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
1675 +static struct platform_driver bcm2835_mmc_driver = {
1676 + .probe = bcm2835_mmc_probe,
1677 + .remove = bcm2835_mmc_remove,
1679 + .name = DRIVER_NAME,
1680 + .owner = THIS_MODULE,
1681 + .of_match_table = bcm2835_mmc_match,
1684 +module_platform_driver(bcm2835_mmc_driver);
1686 +module_param(mmc_debug, uint, 0644);
1687 +module_param(mmc_debug2, uint, 0644);
1688 +MODULE_ALIAS("platform:mmc-bcm2835");
1689 +MODULE_DESCRIPTION("BCM2835 SDHCI driver");
1690 +MODULE_LICENSE("GPL v2");
1691 +MODULE_AUTHOR("Gellert Weisz");