<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From e15eff977effee05b5b47b20762e11ca345315c6 Mon Sep 17 00:00:00 2001
From: gellert &lt;gellert@raspberrypi.org&gt;
Date: Fri, 15 Aug 2014 16:35:06 +0100
Subject: [PATCH] MMC: added alternative MMC driver
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

mmc: Disable CMD23 transfers on all cards

Pending wire-level investigation of these types of transfers
and associated errors on bcm2835-mmc, disable for now. Fallback of
CMD18/CMD25 transfers will be used automatically by the MMC layer.

Reported/Tested-by: Gellert Weisz &lt;gellert@raspberrypi.org&gt;

mmc: bcm2835-mmc: enable DT support for all architectures

Both ARCH_BCM2835 and ARCH_BCM270x are built with OF now.
Enable Device Tree support for all architectures.

Signed-off-by: Noralf TrÃ¸nnes &lt;noralf@tronnes.org&gt;

mmc: bcm2835-mmc: fix probe error handling

Probe error handling is broken in several places.
Simplify error handling by using device managed functions.
Replace pr_{err,info} with dev_{err,info}.

Signed-off-by: Noralf TrÃ¸nnes &lt;noralf@tronnes.org&gt;

bcm2835-mmc: Add locks when accessing sdhost registers

bcm2835-mmc: Add range of debug options for slowing things down

bcm2835-mmc: Add option to disable some delays

bcm2835-mmc: Add option to disable MMC_QUIRK_BLK_NO_CMD23

bcm2835-mmc: Default to disabling MMC_QUIRK_BLK_NO_CMD23

bcm2835-mmc: Adding overclocking option

Allow a different clock speed to be substitued for a requested 50MHz.
This option is exposed using the "overclock_50" DT parameter.
Note that the mmc interface is restricted to EVEN integer divisions of
250MHz, and the highest sensible option is 63 (250/4 = 62.5), the
next being 125 (250/2) which is much too high.

Use at your own risk.

bcm2835-mmc: Round up the overclock, so 62 works for 62.5Mhz

Also only warn once for each overclock setting.

mmc: bcm2835-mmc: Make available on ARCH_BCM2835

Make the bcm2835-mmc driver available for use on ARCH_BCM2835.

Signed-off-by: Noralf TrÃ¸nnes &lt;noralf@tronnes.org&gt;

BCM270x_DT: add bcm2835-mmc entry

Add Device Tree entry for bcm2835-mmc.
In non-DT mode, don't add the device in the board file.

Signed-off-by: Noralf TrÃ¸nnes &lt;noralf@tronnes.org&gt;

bcm2835-mmc: Don't overwrite MMC capabilities from DT

bcm2835-mmc: Don't override bus width capabilities from devicetree

Take out the force setting of the MMC_CAP_4_BIT_DATA host capability
so that the result read from devicetree via mmc_of_parse() is
preserved.

bcm2835-mmc: Only claim one DMA channel

With both MMC controllers enabled there are few DMA channels left. The
bcm2835-mmc driver only uses DMA in one direction at a time, so it
doesn't need to claim two channels.

See: https://github.com/raspberrypi/linux/issues/1327

Signed-off-by: Phil Elwell &lt;phil@raspberrypi.org&gt;

bcm2835-mmc: New timer API

mmc: bcm2835-mmc: Support underclocking

Support underclocking of the SD bus using the max-frequency DT property
(which currently has no DT parameter). The sd_overclock parameter
already provides another way to achieve the same thing which should be
equivalent in end result, but it is a bug not to support max-frequency
as well.

See: https://github.com/raspberrypi/linux/issues/2350

Signed-off-by: Phil Elwell &lt;phil@raspberrypi.org&gt;

mmc/bcm2835: Recover from MMC_SEND_EXT_CSD

If the user issues an "mmc extcsd read", the SD controller receives
what it thinks is a SEND_IF_COND command with an unexpected data block.
The resulting operations leave the FSM stuck in READWAIT, a state which
persists until the MMC framework resets the controller, by which point
the root filesystem is likely to have been unmounted.

A less heavyweight solution is to detect the condition and nudge the
FSM by asserting the (self-clearing) FORCE_DATA_MODE bit.

N.B. This workaround was essentially discovered by accident and without
a full understanding the inner workings of the controller, so it is
fortunate that the "fix" only modifies error paths.

See: https://github.com/raspberrypi/linux/issues/2728

Signed-off-by: Phil Elwell &lt;phil@raspberrypi.org&gt;

bcm2835-mmc: Fix DMA channel leak

The BCM2835 MMC host driver requests a DMA channel on probe but neglects
to release the channel in the probe error path and on driver unbind.

I'm seeing this happen on every boot of the Compute Module 3: On first
driver probe, DMA channel 2 is allocated and then leaked with a "could
not get clk, deferring probe" message. On second driver probe, channel 4
is allocated.

Fix it.

Signed-off-by: Lukas Wunner &lt;lukas@wunner.de&gt;
Cc: Frank Pavlic &lt;f.pavlic@kunbus.de&gt;

bcm2835-mmc: Fix struct mmc_host leak on probe

The BCM2835 MMC host driver requests the bus address of the host's
register map on probe.  If that fails, the driver leaks the struct
mmc_host allocated earlier.

Fix it.

Signed-off-by: Lukas Wunner &lt;lukas@wunner.de&gt;
Cc: Frank Pavlic &lt;f.pavlic@kunbus.de&gt;

bcm2835-mmc: Fix duplicate free_irq() on remove

The BCM2835 MMC host driver requests its interrupt as a device-managed
resource, so the interrupt is automatically freed after the driver is
unbound.

However on driver unbind, bcm2835_mmc_remove() frees the interrupt
explicitly to avoid invocation of the interrupt handler after driver
structures have been torn down.

The interrupt is thus freed twice, leading to a WARN splat in
__free_irq().  Fix by not requesting the interrupt as a device-managed
resource.

Signed-off-by: Lukas Wunner &lt;lukas@wunner.de&gt;
Cc: Frank Pavlic &lt;f.pavlic@kunbus.de&gt;

bcm2835-mmc: Handle mmc_add_host() errors

The BCM2835 MMC host driver calls mmc_add_host() but doesn't check its
return value.  Errors occurring in that function are therefore not
handled.  Fix it.

Signed-off-by: Lukas Wunner &lt;lukas@wunner.de&gt;
Cc: Frank Pavlic &lt;f.pavlic@kunbus.de&gt;

bcm2835-mmc: Deduplicate reset of driver data on remove

The BCM2835 MMC host driver sets the device's driver data pointer to
NULL on -&gt;remove() even though the driver core subsequently does the
same in __device_release_driver().  Drop the duplicate assignment.

Signed-off-by: Lukas Wunner &lt;lukas@wunner.de&gt;
Cc: Frank Pavlic &lt;f.pavlic@kunbus.de&gt;

bcm2835_mmc: Remove vestigial threaded IRQ

With SDIO processing now managed by the MMC framework with a
workqueue, the bcm2835_mmc driver no longer needs a threaded
IRQ.

Signed-off-by: Phil Elwell &lt;phil@raspberrypi.org&gt;

Add missing dma_unmap_sg calls to free relevant swiotlb bounce buffers.
This prevents DMA leaks.

Signed-off-by: Yaroslav Rosomakho &lt;yaroslavros@gmail.com&gt;

Limit max_req_size under arm64 (or any other platform that uses swiotlb) to prevent potential buffer overflow due to bouncing.

Signed-off-by: Yaroslav Rosomakho &lt;yaroslavros@gmail.com&gt;
---
 drivers/mmc/core/block.c       |   28 +-
 drivers/mmc/core/core.c        |    3 +-
 drivers/mmc/core/host.c        |   17 +-
 drivers/mmc/core/quirks.h      |    8 +
 drivers/mmc/host/Kconfig       |   29 +
 drivers/mmc/host/Makefile      |    1 +
 drivers/mmc/host/bcm2835-mmc.c | 1576 ++++++++++++++++++++++++++++++++
 include/linux/mmc/card.h       |    2 +
 8 files changed, 1659 insertions(+), 5 deletions(-)
 create mode 100644 drivers/mmc/host/bcm2835-mmc.c

--- a/drivers/mmc/core/block.c
+++ b/drivers/mmc/core/block.c
@@ -166,6 +166,13 @@ static DEFINE_MUTEX(open_lock);
 module_param(perdev_minors, int, 0444);
 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
 
+/*
+ * Allow quirks to be overridden for the current card
+ */
+static char *card_quirks;
+module_param(card_quirks, charp, 0644);
+MODULE_PARM_DESC(card_quirks, "Force the use of the indicated quirks (a bitfield)");
+
 static inline int mmc_blk_part_switch(struct mmc_card *card,
 				      unsigned int part_type);
 
@@ -2890,6 +2897,7 @@ static int mmc_blk_probe(struct mmc_card
 {
 	struct mmc_blk_data *md, *part_md;
 	char cap_str[10];
+	char quirk_str[24];
 
 	/*
 	 * Check that the card supports the command class(es) we need.
@@ -2897,7 +2905,16 @@ static int mmc_blk_probe(struct mmc_card
 	if (!(card-&gt;csd.cmdclass &amp; CCC_BLOCK_READ))
 		return -ENODEV;
 
-	mmc_fixup_device(card, mmc_blk_fixups);
+	if (card_quirks) {
+		unsigned long quirks;
+		if (kstrtoul(card_quirks, 0, &amp;quirks) == 0)
+			card-&gt;quirks = (unsigned int)quirks;
+		else
+			pr_err("mmc_block: Invalid card_quirks parameter '%s'\n",
+			       card_quirks);
+	}
+	else
+		mmc_fixup_device(card, mmc_blk_fixups);
 
 	card-&gt;complete_wq = alloc_workqueue("mmc_complete",
 					WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
@@ -2912,9 +2929,14 @@ static int mmc_blk_probe(struct mmc_card
 
 	string_get_size((u64)get_capacity(md-&gt;disk), 512, STRING_UNITS_2,
 			cap_str, sizeof(cap_str));
-	pr_info("%s: %s %s %s %s\n",
+	if (card-&gt;quirks)
+		snprintf(quirk_str, sizeof(quirk_str),
+			 " (quirks 0x%08x)", card-&gt;quirks);
+	else
+		quirk_str[0] = '\0';
+	pr_info("%s: %s %s %s%s%s\n",
 		md-&gt;disk-&gt;disk_name, mmc_card_id(card), mmc_card_name(card),
-		cap_str, md-&gt;read_only ? "(ro)" : "");
+		cap_str, md-&gt;read_only ? " (ro)" : "", quirk_str);
 
 	if (mmc_blk_alloc_parts(card, md))
 		goto out;
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -1936,7 +1936,8 @@ EXPORT_SYMBOL(mmc_erase);
 int mmc_can_erase(struct mmc_card *card)
 {
 	if ((card-&gt;host-&gt;caps &amp; MMC_CAP_ERASE) &amp;&amp;
-	    (card-&gt;csd.cmdclass &amp; CCC_ERASE) &amp;&amp; card-&gt;erase_size)
+	    (card-&gt;csd.cmdclass &amp; CCC_ERASE) &amp;&amp; card-&gt;erase_size &amp;&amp;
+	    !(card-&gt;quirks &amp; MMC_QUIRK_ERASE_BROKEN))
 		return 1;
 	return 0;
 }
--- a/drivers/mmc/core/host.c
+++ b/drivers/mmc/core/host.c
@@ -443,15 +443,30 @@ struct mmc_host *mmc_alloc_host(int extr
 {
 	int err;
 	struct mmc_host *host;
+	int id;
 
 	host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
 	if (!host)
 		return NULL;
 
+	/* If OF aliases exist, start dynamic assignment after highest */
+	id = of_alias_get_highest_id("mmc");
+	id = (id &lt; 0) ? 0 : id + 1;
+
+	/* If this devices has OF node, maybe it has an alias */
+	if (dev-&gt;of_node) {
+		int of_id = of_alias_get_id(dev-&gt;of_node, "mmc");
+
+		if (of_id &lt; 0)
+			dev_warn(dev, "/aliases ID not available\n");
+		else
+			id = of_id;
+	}
+
 	/* scanning will be enabled when we're ready */
 	host-&gt;rescan_disable = 1;
 
-	err = ida_simple_get(&amp;mmc_host_ida, 0, 0, GFP_KERNEL);
+	err = ida_simple_get(&amp;mmc_host_ida, id, 0, GFP_KERNEL);
 	if (err &lt; 0) {
 		kfree(host);
 		return NULL;
--- a/drivers/mmc/core/quirks.h
+++ b/drivers/mmc/core/quirks.h
@@ -99,6 +99,14 @@ static const struct mmc_fixup mmc_blk_fi
 	MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
 		  MMC_QUIRK_TRIM_BROKEN),
 
+	/*
+	 *  On some Kingston SD cards, multiple erases of less than 64
+	 *  sectors can cause corruption.
+	 */
+	MMC_FIXUP("SD16G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
+	MMC_FIXUP("SD32G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
+	MMC_FIXUP("SD64G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
+
 	END_FIXUP
 };
 
--- a/drivers/mmc/host/Kconfig
+++ b/drivers/mmc/host/Kconfig
@@ -5,6 +5,35 @@
 
 comment "MMC/SD/SDIO Host Controller Drivers"
 
+config MMC_BCM2835_MMC
+	tristate "MMC support on BCM2835"
+	depends on MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835
+	help
+	  This selects the MMC Interface on BCM2835.
+
+	  If you have a controller with this interface, say Y or M here.
+
+	  If unsure, say N.
+
+config MMC_BCM2835_DMA
+	bool "DMA support on BCM2835 Arasan controller"
+	depends on MMC_BCM2835_MMC
+	help
+	  Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
+	  based chips.
+
+	  If unsure, say N.
+
+config MMC_BCM2835_PIO_DMA_BARRIER
+	int "Block count limit for PIO transfers"
+	depends on MMC_BCM2835_MMC &amp;&amp; MMC_BCM2835_DMA
+	range 0 256
+	default 2
+	help
+	  The inclusive limit in bytes under which PIO will be used instead of DMA
+
+	  If unsure, say 2 here.
+
 config MMC_DEBUG
 	bool "MMC host drivers debugging"
 	depends on MMC != n
--- a/drivers/mmc/host/Makefile
+++ b/drivers/mmc/host/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_MMC_SDHCI_SIRF)   	+= sdhci
 obj-$(CONFIG_MMC_SDHCI_F_SDH30)	+= sdhci_f_sdh30.o
 obj-$(CONFIG_MMC_SDHCI_SPEAR)	+= sdhci-spear.o
 obj-$(CONFIG_MMC_SDHCI_AM654)	+= sdhci_am654.o
+obj-$(CONFIG_MMC_BCM2835_MMC)	+= bcm2835-mmc.o
 obj-$(CONFIG_MMC_WBSD)		+= wbsd.o
 obj-$(CONFIG_MMC_AU1X)		+= au1xmmc.o
 obj-$(CONFIG_MMC_ALCOR)	+= alcor.o
--- /dev/null
+++ b/drivers/mmc/host/bcm2835-mmc.c
@@ -0,0 +1,1576 @@
+/*
+ * BCM2835 MMC host driver.
+ *
+ * Author:      Gellert Weisz &lt;gellert@raspberrypi.org&gt;
+ *              Copyright 2014
+ *
+ * Based on
+ *  sdhci-bcm2708.c by Broadcom
+ *  sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
+ *  sdhci.c and sdhci-pci.c by Pierre Ossman
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
+ */
+
+#include &lt;linux/delay.h&gt;
+#include &lt;linux/module.h&gt;
+#include &lt;linux/io.h&gt;
+#include &lt;linux/mmc/mmc.h&gt;
+#include &lt;linux/mmc/host.h&gt;
+#include &lt;linux/mmc/sd.h&gt;
+#include &lt;linux/scatterlist.h&gt;
+#include &lt;linux/of_address.h&gt;
+#include &lt;linux/of_irq.h&gt;
+#include &lt;linux/clk.h&gt;
+#include &lt;linux/platform_device.h&gt;
+#include &lt;linux/err.h&gt;
+#include &lt;linux/blkdev.h&gt;
+#include &lt;linux/dmaengine.h&gt;
+#include &lt;linux/dma-mapping.h&gt;
+#include &lt;linux/of_dma.h&gt;
+#include &lt;linux/swiotlb.h&gt;
+
+#include "sdhci.h"
+
+
+#define DRIVER_NAME "mmc-bcm2835"
+
+#define DBG(f, x...) \
+pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
+
+#ifndef CONFIG_MMC_BCM2835_DMA
+ #define FORCE_PIO
+#endif
+
+
+/* the inclusive limit in bytes under which PIO will be used instead of DMA */
+#ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
+#define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
+#else
+#define PIO_DMA_BARRIER 00
+#endif
+
+#define MIN_FREQ 400000
+#define TIMEOUT_VAL 0xE
+#define BCM2835_SDHCI_WRITE_DELAY(f)	(((2 * 1000000) / f) + 1)
+
+
+unsigned mmc_debug;
+unsigned mmc_debug2;
+
+struct bcm2835_host {
+	spinlock_t				lock;
+
+	void __iomem			*ioaddr;
+	u32						bus_addr;
+
+	struct mmc_host			*mmc;
+
+	u32						timeout;
+
+	int						clock;	/* Current clock speed */
+	u8						pwr;	/* Current voltage */
+
+	unsigned int			max_clk;		/* Max possible freq */
+	unsigned int			timeout_clk;	/* Timeout freq (KHz) */
+	unsigned int			clk_mul;		/* Clock Muliplier value */
+
+	struct tasklet_struct	finish_tasklet;		/* Tasklet structures */
+
+	struct timer_list		timer;			/* Timer for timeouts */
+
+	struct sg_mapping_iter	sg_miter;		/* SG state for PIO */
+	unsigned int			blocks;			/* remaining PIO blocks */
+
+	int						irq;			/* Device IRQ */
+
+
+	u32						ier;			/* cached registers */
+
+	struct mmc_request		*mrq;			/* Current request */
+	struct mmc_command		*cmd;			/* Current command */
+	struct mmc_data			*data;			/* Current data request */
+	unsigned int			data_early:1;		/* Data finished before cmd */
+
+	wait_queue_head_t		buf_ready_int;		/* Waitqueue for Buffer Read Ready interrupt */
+
+	u32						shadow;
+
+	/*DMA part*/
+	struct dma_chan			*dma_chan_rxtx;		/* DMA channel for reads and writes */
+	struct dma_slave_config		dma_cfg_rx;
+	struct dma_slave_config		dma_cfg_tx;
+	struct dma_async_tx_descriptor	*tx_desc;	/* descriptor */
+
+	bool					have_dma;
+	bool					use_dma;
+	bool					wait_for_dma;
+	/*end of DMA part*/
+
+	int						max_delay;	/* maximum length of time spent waiting */
+
+	int						flags;				/* Host attributes */
+#define SDHCI_REQ_USE_DMA	(1&lt;&lt;2)	/* Use DMA for this req. */
+#define SDHCI_DEVICE_DEAD	(1&lt;&lt;3)	/* Device unresponsive */
+#define SDHCI_AUTO_CMD12	(1&lt;&lt;6)	/* Auto CMD12 support */
+#define SDHCI_AUTO_CMD23	(1&lt;&lt;7)	/* Auto CMD23 support */
+#define SDHCI_SDIO_IRQ_ENABLED	(1&lt;&lt;9)	/* SDIO irq enabled */
+
+	u32				overclock_50;	/* frequency to use when 50MHz is requested (in MHz) */
+	u32				max_overclock;	/* Highest reported */
+};
+
+
+static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg, int from)
+{
+	unsigned delay;
+	lockdep_assert_held_once(&amp;host-&gt;lock);
+	writel(val, host-&gt;ioaddr + reg);
+	udelay(BCM2835_SDHCI_WRITE_DELAY(max(host-&gt;clock, MIN_FREQ)));
+
+	delay = ((mmc_debug &gt;&gt; 16) &amp; 0xf) &lt;&lt; ((mmc_debug &gt;&gt; 20) &amp; 0xf);
+	if (delay &amp;&amp; !((1&lt;&lt;from) &amp; mmc_debug2))
+		udelay(delay);
+}
+
+static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
+{
+	unsigned delay;
+	lockdep_assert_held_once(&amp;host-&gt;lock);
+	writel(val, host-&gt;ioaddr + reg);
+
+	delay = ((mmc_debug &gt;&gt; 24) &amp; 0xf) &lt;&lt; ((mmc_debug &gt;&gt; 28) &amp; 0xf);
+	if (delay)
+		udelay(delay);
+}
+
+static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
+{
+	lockdep_assert_held_once(&amp;host-&gt;lock);
+	return readl(host-&gt;ioaddr + reg);
+}
+
+static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
+{
+	u32 oldval = (reg == SDHCI_COMMAND) ? host-&gt;shadow :
+		bcm2835_mmc_readl(host, reg &amp; ~3);
+	u32 word_num = (reg &gt;&gt; 1) &amp; 1;
+	u32 word_shift = word_num * 16;
+	u32 mask = 0xffff &lt;&lt; word_shift;
+	u32 newval = (oldval &amp; ~mask) | (val &lt;&lt; word_shift);
+
+	if (reg == SDHCI_TRANSFER_MODE)
+		host-&gt;shadow = newval;
+	else
+		bcm2835_mmc_writel(host, newval, reg &amp; ~3, 0);
+
+}
+
+static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
+{
+	u32 oldval = bcm2835_mmc_readl(host, reg &amp; ~3);
+	u32 byte_num = reg &amp; 3;
+	u32 byte_shift = byte_num * 8;
+	u32 mask = 0xff &lt;&lt; byte_shift;
+	u32 newval = (oldval &amp; ~mask) | (val &lt;&lt; byte_shift);
+
+	bcm2835_mmc_writel(host, newval, reg &amp; ~3, 1);
+}
+
+
+static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
+{
+	u32 val = bcm2835_mmc_readl(host, (reg &amp; ~3));
+	u32 word_num = (reg &gt;&gt; 1) &amp; 1;
+	u32 word_shift = word_num * 16;
+	u32 word = (val &gt;&gt; word_shift) &amp; 0xffff;
+
+	return word;
+}
+
+static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
+{
+	u32 val = bcm2835_mmc_readl(host, (reg &amp; ~3));
+	u32 byte_num = reg &amp; 3;
+	u32 byte_shift = byte_num * 8;
+	u32 byte = (val &gt;&gt; byte_shift) &amp; 0xff;
+
+	return byte;
+}
+
+static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
+{
+	u32 ier;
+
+	ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
+	ier &amp;= ~clear;
+	/* change which requests generate IRQs - makes no difference to
+	   the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
+	bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE, 2);
+}
+
+
+static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
+{
+	pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
+		mmc_hostname(host-&gt;mmc));
+
+	pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version:  0x%08x\n",
+		bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
+		bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
+	pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt:  0x%08x\n",
+		bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
+		bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
+	pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
+		bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
+		bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
+	pr_debug(DRIVER_NAME ": Present:  0x%08x | Host ctl: 0x%08x\n",
+		bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
+		bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
+	pr_debug(DRIVER_NAME ": Power:    0x%08x | Blk gap:  0x%08x\n",
+		bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
+		bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
+	pr_debug(DRIVER_NAME ": Wake-up:  0x%08x | Clock:    0x%08x\n",
+		bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
+		bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
+	pr_debug(DRIVER_NAME ": Timeout:  0x%08x | Int stat: 0x%08x\n",
+		bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
+		bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
+	pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
+		bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
+		bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
+	pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
+		bcm2835_mmc_readw(host, SDHCI_AUTO_CMD_STATUS),
+		bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
+	pr_debug(DRIVER_NAME ": Caps:     0x%08x | Caps_1:   0x%08x\n",
+		bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
+		bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
+	pr_debug(DRIVER_NAME ": Cmd:      0x%08x | Max curr: 0x%08x\n",
+		bcm2835_mmc_readw(host, SDHCI_COMMAND),
+		bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
+	pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
+		bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
+
+	pr_debug(DRIVER_NAME ": ===========================================\n");
+}
+
+
+static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
+{
+	unsigned long timeout;
+	unsigned long flags;
+
+	spin_lock_irqsave(&amp;host-&gt;lock, flags);
+	bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
+
+	if (mask &amp; SDHCI_RESET_ALL)
+		host-&gt;clock = 0;
+
+	/* Wait max 100 ms */
+	timeout = 100;
+
+	/* hw clears the bit when it's done */
+	while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) &amp; mask) {
+		if (timeout == 0) {
+			pr_err("%s: Reset 0x%x never completed.\n",
+				mmc_hostname(host-&gt;mmc), (int)mask);
+			bcm2835_mmc_dumpregs(host);
+			return;
+		}
+		timeout--;
+		spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+		mdelay(1);
+		spin_lock_irqsave(&amp;host-&gt;lock, flags);
+	}
+
+	if (100-timeout &gt; 10 &amp;&amp; 100-timeout &gt; host-&gt;max_delay) {
+		host-&gt;max_delay = 100-timeout;
+		pr_warning("Warning: MMC controller hung for %d ms\n", host-&gt;max_delay);
+	}
+	spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+}
+
+static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
+
+static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
+{
+	unsigned long flags;
+	if (soft)
+		bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
+	else
+		bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
+
+	host-&gt;ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
+		    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
+		    SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
+		    SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
+		    SDHCI_INT_RESPONSE;
+
+	spin_lock_irqsave(&amp;host-&gt;lock, flags);
+	bcm2835_mmc_writel(host, host-&gt;ier, SDHCI_INT_ENABLE, 3);
+	bcm2835_mmc_writel(host, host-&gt;ier, SDHCI_SIGNAL_ENABLE, 3);
+	spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+
+	if (soft) {
+		/* force clock reconfiguration */
+		host-&gt;clock = 0;
+		bcm2835_mmc_set_ios(host-&gt;mmc, &amp;host-&gt;mmc-&gt;ios);
+	}
+}
+
+
+
+static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
+
+static void bcm2835_mmc_dma_complete(void *param)
+{
+	struct bcm2835_host *host = param;
+	struct dma_chan *dma_chan;
+	unsigned long flags;
+	u32 dir_data;
+
+	spin_lock_irqsave(&amp;host-&gt;lock, flags);
+
+	host-&gt;use_dma = false;
+
+	if (host-&gt;data) {
+		dma_chan = host-&gt;dma_chan_rxtx;
+		if (host-&gt;data-&gt;flags &amp; MMC_DATA_WRITE)
+			dir_data = DMA_TO_DEVICE;
+		else
+			dir_data = DMA_FROM_DEVICE;
+		dma_unmap_sg(dma_chan-&gt;device-&gt;dev,
+		     host-&gt;data-&gt;sg, host-&gt;data-&gt;sg_len,
+		     dir_data);
+		if (! (host-&gt;data-&gt;flags &amp; MMC_DATA_WRITE))
+			bcm2835_mmc_finish_data(host);
+	} else if (host-&gt;wait_for_dma) {
+		host-&gt;wait_for_dma = false;
+		tasklet_schedule(&amp;host-&gt;finish_tasklet);
+	}
+
+	spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+}
+
+static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
+{
+	unsigned long flags;
+	size_t blksize, len, chunk;
+
+	u32 uninitialized_var(scratch);
+	u8 *buf;
+
+	blksize = host-&gt;data-&gt;blksz;
+	chunk = 0;
+
+	local_irq_save(flags);
+
+	while (blksize) {
+		if (!sg_miter_next(&amp;host-&gt;sg_miter))
+			BUG();
+
+		len = min(host-&gt;sg_miter.length, blksize);
+
+		blksize -= len;
+		host-&gt;sg_miter.consumed = len;
+
+		buf = host-&gt;sg_miter.addr;
+
+		while (len) {
+			if (chunk == 0) {
+				scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
+				chunk = 4;
+			}
+
+			*buf = scratch &amp; 0xFF;
+
+			buf++;
+			scratch &gt;&gt;= 8;
+			chunk--;
+			len--;
+		}
+	}
+
+	sg_miter_stop(&amp;host-&gt;sg_miter);
+
+	local_irq_restore(flags);
+}
+
+static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
+{
+	unsigned long flags;
+	size_t blksize, len, chunk;
+	u32 scratch;
+	u8 *buf;
+
+	blksize = host-&gt;data-&gt;blksz;
+	chunk = 0;
+	chunk = 0;
+	scratch = 0;
+
+	local_irq_save(flags);
+
+	while (blksize) {
+		if (!sg_miter_next(&amp;host-&gt;sg_miter))
+			BUG();
+
+		len = min(host-&gt;sg_miter.length, blksize);
+
+		blksize -= len;
+		host-&gt;sg_miter.consumed = len;
+
+		buf = host-&gt;sg_miter.addr;
+
+		while (len) {
+			scratch |= (u32)*buf &lt;&lt; (chunk * 8);
+
+			buf++;
+			chunk++;
+			len--;
+
+			if ((chunk == 4) || ((len == 0) &amp;&amp; (blksize == 0))) {
+				mmc_raw_writel(host, scratch, SDHCI_BUFFER);
+				chunk = 0;
+				scratch = 0;
+			}
+		}
+	}
+
+	sg_miter_stop(&amp;host-&gt;sg_miter);
+
+	local_irq_restore(flags);
+}
+
+
+static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
+{
+	u32 mask;
+
+	BUG_ON(!host-&gt;data);
+
+	if (host-&gt;blocks == 0)
+		return;
+
+	if (host-&gt;data-&gt;flags &amp; MMC_DATA_READ)
+		mask = SDHCI_DATA_AVAILABLE;
+	else
+		mask = SDHCI_SPACE_AVAILABLE;
+
+	while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) &amp; mask) {
+
+		if (host-&gt;data-&gt;flags &amp; MMC_DATA_READ)
+			bcm2835_bcm2835_mmc_read_block_pio(host);
+		else
+			bcm2835_bcm2835_mmc_write_block_pio(host);
+
+		host-&gt;blocks--;
+
+		/* QUIRK used in sdhci.c removes the 'if' */
+		/* but it seems this is unnecessary */
+		if (host-&gt;blocks == 0)
+			break;
+
+
+	}
+}
+
+
+static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
+{
+	u32 len, dir_data, dir_slave;
+	struct dma_async_tx_descriptor *desc = NULL;
+	struct dma_chan *dma_chan;
+
+
+	WARN_ON(!host-&gt;data);
+
+	if (!host-&gt;data)
+		return;
+
+	if (host-&gt;blocks == 0)
+		return;
+
+	dma_chan = host-&gt;dma_chan_rxtx;
+	if (host-&gt;data-&gt;flags &amp; MMC_DATA_READ) {
+		dir_data = DMA_FROM_DEVICE;
+		dir_slave = DMA_DEV_TO_MEM;
+	} else {
+		dir_data = DMA_TO_DEVICE;
+		dir_slave = DMA_MEM_TO_DEV;
+	}
+
+	/* The parameters have already been validated, so this will not fail */
+	(void)dmaengine_slave_config(dma_chan,
+				     (dir_data == DMA_FROM_DEVICE) ?
+				     &amp;host-&gt;dma_cfg_rx :
+				     &amp;host-&gt;dma_cfg_tx);
+
+	BUG_ON(!dma_chan-&gt;device);
+	BUG_ON(!dma_chan-&gt;device-&gt;dev);
+	BUG_ON(!host-&gt;data-&gt;sg);
+
+	len = dma_map_sg(dma_chan-&gt;device-&gt;dev, host-&gt;data-&gt;sg,
+			 host-&gt;data-&gt;sg_len, dir_data);
+	if (len &gt; 0) {
+		desc = dmaengine_prep_slave_sg(dma_chan, host-&gt;data-&gt;sg,
+					       len, dir_slave,
+					       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
+	} else {
+		dev_err(mmc_dev(host-&gt;mmc), "dma_map_sg returned zero length\n");
+	}
+	if (desc) {
+		unsigned long flags;
+		spin_lock_irqsave(&amp;host-&gt;lock, flags);
+		bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
+						    SDHCI_INT_SPACE_AVAIL);
+		host-&gt;tx_desc = desc;
+		desc-&gt;callback = bcm2835_mmc_dma_complete;
+		desc-&gt;callback_param = host;
+		spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+		dmaengine_submit(desc);
+		dma_async_issue_pending(dma_chan);
+	} else {
+		dma_unmap_sg(dma_chan-&gt;device-&gt;dev, host-&gt;data-&gt;sg, len, dir_data);
+	}
+
+}
+
+
+
+static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
+{
+	u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
+	u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
+
+	if (host-&gt;use_dma)
+		host-&gt;ier = (host-&gt;ier &amp; ~pio_irqs) | dma_irqs;
+	else
+		host-&gt;ier = (host-&gt;ier &amp; ~dma_irqs) | pio_irqs;
+
+	bcm2835_mmc_writel(host, host-&gt;ier, SDHCI_INT_ENABLE, 4);
+	bcm2835_mmc_writel(host, host-&gt;ier, SDHCI_SIGNAL_ENABLE, 4);
+}
+
+
+static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
+{
+	u8 count;
+	struct mmc_data *data = cmd-&gt;data;
+
+	WARN_ON(host-&gt;data);
+
+	if (data || (cmd-&gt;flags &amp; MMC_RSP_BUSY)) {
+		count = TIMEOUT_VAL;
+		bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
+	}
+
+	if (!data)
+		return;
+
+	/* Sanity checks */
+	BUG_ON(data-&gt;blksz * data-&gt;blocks &gt; 524288);
+	BUG_ON(data-&gt;blksz &gt; host-&gt;mmc-&gt;max_blk_size);
+	BUG_ON(data-&gt;blocks &gt; 65535);
+
+	host-&gt;data = data;
+	host-&gt;data_early = 0;
+	host-&gt;data-&gt;bytes_xfered = 0;
+
+
+	if (!(host-&gt;flags &amp; SDHCI_REQ_USE_DMA)) {
+		int flags;
+
+		flags = SG_MITER_ATOMIC;
+		if (host-&gt;data-&gt;flags &amp; MMC_DATA_READ)
+			flags |= SG_MITER_TO_SG;
+		else
+			flags |= SG_MITER_FROM_SG;
+		sg_miter_start(&amp;host-&gt;sg_miter, data-&gt;sg, data-&gt;sg_len, flags);
+		host-&gt;blocks = data-&gt;blocks;
+	}
+
+	host-&gt;use_dma = host-&gt;have_dma &amp;&amp; data-&gt;blocks &gt; PIO_DMA_BARRIER;
+
+	bcm2835_mmc_set_transfer_irqs(host);
+
+	/* Set the DMA boundary value and block size */
+	bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
+		data-&gt;blksz), SDHCI_BLOCK_SIZE);
+	bcm2835_mmc_writew(host, data-&gt;blocks, SDHCI_BLOCK_COUNT);
+
+	BUG_ON(!host-&gt;data);
+}
+
+static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
+	struct mmc_command *cmd)
+{
+	u16 mode;
+	struct mmc_data *data = cmd-&gt;data;
+
+	if (data == NULL) {
+		/* clear Auto CMD settings for no data CMDs */
+		mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
+		bcm2835_mmc_writew(host, mode &amp; ~(SDHCI_TRNS_AUTO_CMD12 |
+				SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
+		return;
+	}
+
+	WARN_ON(!host-&gt;data);
+
+	mode = SDHCI_TRNS_BLK_CNT_EN;
+
+	if ((mmc_op_multi(cmd-&gt;opcode) || data-&gt;blocks &gt; 1)) {
+		mode |= SDHCI_TRNS_MULTI;
+
+		/*
+		 * If we are sending CMD23, CMD12 never gets sent
+		 * on successful completion (so no Auto-CMD12).
+		 */
+		if (!host-&gt;mrq-&gt;sbc &amp;&amp; (host-&gt;flags &amp; SDHCI_AUTO_CMD12))
+			mode |= SDHCI_TRNS_AUTO_CMD12;
+		else if (host-&gt;mrq-&gt;sbc &amp;&amp; (host-&gt;flags &amp; SDHCI_AUTO_CMD23)) {
+			mode |= SDHCI_TRNS_AUTO_CMD23;
+			bcm2835_mmc_writel(host, host-&gt;mrq-&gt;sbc-&gt;arg, SDHCI_ARGUMENT2, 5);
+		}
+	}
+
+	if (data-&gt;flags &amp; MMC_DATA_READ)
+		mode |= SDHCI_TRNS_READ;
+	if (host-&gt;flags &amp; SDHCI_REQ_USE_DMA)
+		mode |= SDHCI_TRNS_DMA;
+
+	bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
+}
+
+void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
+{
+	int flags;
+	u32 mask;
+	unsigned long timeout;
+
+	WARN_ON(host-&gt;cmd);
+
+	/* Wait max 10 ms */
+	timeout = 1000;
+
+	mask = SDHCI_CMD_INHIBIT;
+	if ((cmd-&gt;data != NULL) || (cmd-&gt;flags &amp; MMC_RSP_BUSY))
+		mask |= SDHCI_DATA_INHIBIT;
+
+	/* We shouldn't wait for data inihibit for stop commands, even
+	   though they might use busy signaling */
+	if (host-&gt;mrq-&gt;data &amp;&amp; (cmd == host-&gt;mrq-&gt;data-&gt;stop))
+		mask &amp;= ~SDHCI_DATA_INHIBIT;
+
+	while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) &amp; mask) {
+		if (timeout == 0) {
+			pr_err("%s: Controller never released inhibit bit(s).\n",
+				mmc_hostname(host-&gt;mmc));
+			bcm2835_mmc_dumpregs(host);
+			cmd-&gt;error = -EIO;
+			tasklet_schedule(&amp;host-&gt;finish_tasklet);
+			return;
+		}
+		timeout--;
+		udelay(10);
+	}
+
+	if ((1000-timeout)/100 &gt; 1 &amp;&amp; (1000-timeout)/100 &gt; host-&gt;max_delay) {
+		host-&gt;max_delay = (1000-timeout)/100;
+		pr_warning("Warning: MMC controller hung for %d ms\n", host-&gt;max_delay);
+	}
+
+	timeout = jiffies;
+	if (!cmd-&gt;data &amp;&amp; cmd-&gt;busy_timeout &gt; 9000)
+		timeout += DIV_ROUND_UP(cmd-&gt;busy_timeout, 1000) * HZ + HZ;
+	else
+		timeout += 10 * HZ;
+	mod_timer(&amp;host-&gt;timer, timeout);
+
+	host-&gt;cmd = cmd;
+	host-&gt;use_dma = false;
+
+	bcm2835_mmc_prepare_data(host, cmd);
+
+	bcm2835_mmc_writel(host, cmd-&gt;arg, SDHCI_ARGUMENT, 6);
+
+	bcm2835_mmc_set_transfer_mode(host, cmd);
+
+	if ((cmd-&gt;flags &amp; MMC_RSP_136) &amp;&amp; (cmd-&gt;flags &amp; MMC_RSP_BUSY)) {
+		pr_err("%s: Unsupported response type!\n",
+			mmc_hostname(host-&gt;mmc));
+		cmd-&gt;error = -EINVAL;
+		tasklet_schedule(&amp;host-&gt;finish_tasklet);
+		return;
+	}
+
+	if (!(cmd-&gt;flags &amp; MMC_RSP_PRESENT))
+		flags = SDHCI_CMD_RESP_NONE;
+	else if (cmd-&gt;flags &amp; MMC_RSP_136)
+		flags = SDHCI_CMD_RESP_LONG;
+	else if (cmd-&gt;flags &amp; MMC_RSP_BUSY)
+		flags = SDHCI_CMD_RESP_SHORT_BUSY;
+	else
+		flags = SDHCI_CMD_RESP_SHORT;
+
+	if (cmd-&gt;flags &amp; MMC_RSP_CRC)
+		flags |= SDHCI_CMD_CRC;
+	if (cmd-&gt;flags &amp; MMC_RSP_OPCODE)
+		flags |= SDHCI_CMD_INDEX;
+
+	if (cmd-&gt;data)
+		flags |= SDHCI_CMD_DATA;
+
+	bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd-&gt;opcode, flags), SDHCI_COMMAND);
+}
+
+
+static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
+{
+	struct mmc_data *data;
+
+	BUG_ON(!host-&gt;data);
+
+	data = host-&gt;data;
+	host-&gt;data = NULL;
+
+	if (data-&gt;error)
+		data-&gt;bytes_xfered = 0;
+	else
+		data-&gt;bytes_xfered = data-&gt;blksz * data-&gt;blocks;
+
+	/*
+	 * Need to send CMD12 if -
+	 * a) open-ended multiblock transfer (no CMD23)
+	 * b) error in multiblock transfer
+	 */
+	if (data-&gt;stop &amp;&amp;
+	    (data-&gt;error ||
+	     !host-&gt;mrq-&gt;sbc)) {
+
+		/*
+		 * The controller needs a reset of internal state machines
+		 * upon error conditions.
+		 */
+		if (data-&gt;error) {
+			bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
+			bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
+		}
+
+		bcm2835_mmc_send_command(host, data-&gt;stop);
+	} else if (host-&gt;use_dma) {
+		host-&gt;wait_for_dma = true;
+	} else {
+		tasklet_schedule(&amp;host-&gt;finish_tasklet);
+	}
+}
+
+static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
+{
+	int i;
+
+	BUG_ON(host-&gt;cmd == NULL);
+
+	if (host-&gt;cmd-&gt;flags &amp; MMC_RSP_PRESENT) {
+		if (host-&gt;cmd-&gt;flags &amp; MMC_RSP_136) {
+			/* CRC is stripped so we need to do some shifting. */
+			for (i = 0; i &lt; 4; i++) {
+				host-&gt;cmd-&gt;resp[i] = bcm2835_mmc_readl(host,
+					SDHCI_RESPONSE + (3-i)*4) &lt;&lt; 8;
+				if (i != 3)
+					host-&gt;cmd-&gt;resp[i] |=
+						bcm2835_mmc_readb(host,
+						SDHCI_RESPONSE + (3-i)*4-1);
+			}
+		} else {
+			host-&gt;cmd-&gt;resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
+		}
+	}
+
+	host-&gt;cmd-&gt;error = 0;
+
+	/* Finished CMD23, now send actual command. */
+	if (host-&gt;cmd == host-&gt;mrq-&gt;sbc) {
+		host-&gt;cmd = NULL;
+		bcm2835_mmc_send_command(host, host-&gt;mrq-&gt;cmd);
+
+		if (host-&gt;mrq-&gt;cmd-&gt;data &amp;&amp; host-&gt;use_dma) {
+			/* DMA transfer starts now, PIO starts after interrupt */
+			bcm2835_mmc_transfer_dma(host);
+		}
+	} else {
+
+		/* Processed actual command. */
+		if (host-&gt;data &amp;&amp; host-&gt;data_early)
+			bcm2835_mmc_finish_data(host);
+
+		if (!host-&gt;cmd-&gt;data)
+			tasklet_schedule(&amp;host-&gt;finish_tasklet);
+
+		host-&gt;cmd = NULL;
+	}
+}
+
+
+static void bcm2835_mmc_timeout_timer(struct timer_list *t)
+{
+	struct bcm2835_host *host = from_timer(host, t, timer);
+	unsigned long flags;
+
+	spin_lock_irqsave(&amp;host-&gt;lock, flags);
+
+	if (host-&gt;mrq) {
+		pr_err("%s: Timeout waiting for hardware interrupt.\n",
+			mmc_hostname(host-&gt;mmc));
+		bcm2835_mmc_dumpregs(host);
+
+		if (host-&gt;data) {
+			host-&gt;data-&gt;error = -ETIMEDOUT;
+			bcm2835_mmc_finish_data(host);
+		} else {
+			if (host-&gt;cmd)
+				host-&gt;cmd-&gt;error = -ETIMEDOUT;
+			else
+				host-&gt;mrq-&gt;cmd-&gt;error = -ETIMEDOUT;
+
+			tasklet_schedule(&amp;host-&gt;finish_tasklet);
+		}
+	}
+
+	spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+}
+
+
+static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
+{
+	if (!(host-&gt;flags &amp; SDHCI_DEVICE_DEAD)) {
+		if (enable)
+			host-&gt;ier |= SDHCI_INT_CARD_INT;
+		else
+			host-&gt;ier &amp;= ~SDHCI_INT_CARD_INT;
+
+		bcm2835_mmc_writel(host, host-&gt;ier, SDHCI_INT_ENABLE, 7);
+		bcm2835_mmc_writel(host, host-&gt;ier, SDHCI_SIGNAL_ENABLE, 7);
+	}
+}
+
+static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
+{
+	struct bcm2835_host *host = mmc_priv(mmc);
+	unsigned long flags;
+
+	spin_lock_irqsave(&amp;host-&gt;lock, flags);
+	if (enable)
+		host-&gt;flags |= SDHCI_SDIO_IRQ_ENABLED;
+	else
+		host-&gt;flags &amp;= ~SDHCI_SDIO_IRQ_ENABLED;
+
+	bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
+	spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+}
+
+static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
+{
+
+	BUG_ON(intmask == 0);
+
+	if (!host-&gt;cmd) {
+		pr_err("%s: Got command interrupt 0x%08x even "
+			"though no command operation was in progress.\n",
+			mmc_hostname(host-&gt;mmc), (unsigned)intmask);
+		bcm2835_mmc_dumpregs(host);
+		return;
+	}
+
+	if (intmask &amp; SDHCI_INT_TIMEOUT)
+		host-&gt;cmd-&gt;error = -ETIMEDOUT;
+	else if (intmask &amp; (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
+			SDHCI_INT_INDEX)) {
+			host-&gt;cmd-&gt;error = -EILSEQ;
+	}
+
+	if (host-&gt;cmd-&gt;error) {
+		tasklet_schedule(&amp;host-&gt;finish_tasklet);
+		return;
+	}
+
+	if (intmask &amp; SDHCI_INT_RESPONSE)
+		bcm2835_mmc_finish_command(host);
+
+}
+
+static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
+{
+	struct dma_chan *dma_chan;
+	u32 dir_data;
+
+	BUG_ON(intmask == 0);
+
+	if (!host-&gt;data) {
+		/*
+		 * The "data complete" interrupt is also used to
+		 * indicate that a busy state has ended. See comment
+		 * above in sdhci_cmd_irq().
+		 */
+		if (host-&gt;cmd &amp;&amp; (host-&gt;cmd-&gt;flags &amp; MMC_RSP_BUSY)) {
+			if (intmask &amp; SDHCI_INT_DATA_END) {
+				bcm2835_mmc_finish_command(host);
+				return;
+			}
+		}
+
+		pr_debug("%s: Got data interrupt 0x%08x even "
+			"though no data operation was in progress.\n",
+			mmc_hostname(host-&gt;mmc), (unsigned)intmask);
+		bcm2835_mmc_dumpregs(host);
+
+		return;
+	}
+
+	if (intmask &amp; SDHCI_INT_DATA_TIMEOUT)
+		host-&gt;data-&gt;error = -ETIMEDOUT;
+	else if (intmask &amp; SDHCI_INT_DATA_END_BIT)
+		host-&gt;data-&gt;error = -EILSEQ;
+	else if ((intmask &amp; SDHCI_INT_DATA_CRC) &amp;&amp;
+		SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
+			!= MMC_BUS_TEST_R)
+		host-&gt;data-&gt;error = -EILSEQ;
+
+	if (host-&gt;use_dma) {
+		if  (host-&gt;data-&gt;flags &amp; MMC_DATA_WRITE) {
+			/* IRQ handled here */
+
+			dma_chan = host-&gt;dma_chan_rxtx;
+			dir_data = DMA_TO_DEVICE;
+			dma_unmap_sg(dma_chan-&gt;device-&gt;dev,
+				 host-&gt;data-&gt;sg, host-&gt;data-&gt;sg_len,
+				 dir_data);
+
+			bcm2835_mmc_finish_data(host);
+		}
+
+	} else {
+		if (host-&gt;data-&gt;error)
+			bcm2835_mmc_finish_data(host);
+		else {
+			if (intmask &amp; (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
+				bcm2835_mmc_transfer_pio(host);
+
+			if (intmask &amp; SDHCI_INT_DATA_END) {
+				if (host-&gt;cmd) {
+					/*
+					 * Data managed to finish before the
+					 * command completed. Make sure we do
+					 * things in the proper order.
+					 */
+					host-&gt;data_early = 1;
+				} else {
+					bcm2835_mmc_finish_data(host);
+				}
+			}
+		}
+	}
+}
+
+
+static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
+{
+	irqreturn_t result = IRQ_NONE;
+	struct bcm2835_host *host = dev_id;
+	u32 intmask, mask, unexpected = 0;
+	int max_loops = 16;
+
+	spin_lock(&amp;host-&gt;lock);
+
+	intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
+
+	if (!intmask || intmask == 0xffffffff) {
+		result = IRQ_NONE;
+		goto out;
+	}
+
+	do {
+		/* Clear selected interrupts. */
+		mask = intmask &amp; (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
+				  SDHCI_INT_BUS_POWER);
+		bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS, 8);
+
+
+		if (intmask &amp; SDHCI_INT_CMD_MASK)
+			bcm2835_mmc_cmd_irq(host, intmask &amp; SDHCI_INT_CMD_MASK);
+
+		if (intmask &amp; SDHCI_INT_DATA_MASK)
+			bcm2835_mmc_data_irq(host, intmask &amp; SDHCI_INT_DATA_MASK);
+
+		if (intmask &amp; SDHCI_INT_BUS_POWER)
+			pr_err("%s: Card is consuming too much power!\n",
+				mmc_hostname(host-&gt;mmc));
+
+		if (intmask &amp; SDHCI_INT_CARD_INT) {
+			bcm2835_mmc_enable_sdio_irq_nolock(host, false);
+			sdio_signal_irq(host-&gt;mmc);
+		}
+
+		intmask &amp;= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
+			     SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
+			     SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
+			     SDHCI_INT_CARD_INT);
+
+		if (intmask) {
+			unexpected |= intmask;
+			bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS, 9);
+		}
+
+		if (result == IRQ_NONE)
+			result = IRQ_HANDLED;
+
+		intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
+	} while (intmask &amp;&amp; --max_loops);
+out:
+	spin_unlock(&amp;host-&gt;lock);
+
+	if (unexpected) {
+		pr_err("%s: Unexpected interrupt 0x%08x.\n",
+			   mmc_hostname(host-&gt;mmc), unexpected);
+		bcm2835_mmc_dumpregs(host);
+	}
+
+	return result;
+}
+
+
+static void bcm2835_mmc_ack_sdio_irq(struct mmc_host *mmc)
+{
+	struct bcm2835_host *host = mmc_priv(mmc);
+	unsigned long flags;
+
+	spin_lock_irqsave(&amp;host-&gt;lock, flags);
+	if (host-&gt;flags &amp; SDHCI_SDIO_IRQ_ENABLED)
+		bcm2835_mmc_enable_sdio_irq_nolock(host, true);
+	spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+}
+
+void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
+{
+	int div = 0; /* Initialized for compiler warning */
+	int real_div = div, clk_mul = 1;
+	u16 clk = 0;
+	unsigned long timeout;
+	unsigned int input_clock = clock;
+
+	if (host-&gt;overclock_50 &amp;&amp; (clock == 50000000))
+		clock = host-&gt;overclock_50 * 1000000 + 999999;
+
+	host-&gt;mmc-&gt;actual_clock = 0;
+
+	bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
+
+	if (clock == 0)
+		return;
+
+	/* Version 3.00 divisors must be a multiple of 2. */
+	if (host-&gt;max_clk &lt;= clock)
+		div = 1;
+	else {
+		for (div = 2; div &lt; SDHCI_MAX_DIV_SPEC_300;
+			 div += 2) {
+			if ((host-&gt;max_clk / div) &lt;= clock)
+				break;
+		}
+	}
+
+	real_div = div;
+	div &gt;&gt;= 1;
+
+	if (real_div)
+		clock = (host-&gt;max_clk * clk_mul) / real_div;
+	host-&gt;mmc-&gt;actual_clock = clock;
+
+	if ((clock &gt; input_clock) &amp;&amp; (clock &gt; host-&gt;max_overclock)) {
+		pr_warn("%s: Overclocking to %dHz\n",
+			mmc_hostname(host-&gt;mmc), clock);
+		host-&gt;max_overclock = clock;
+	}
+
+	clk |= (div &amp; SDHCI_DIV_MASK) &lt;&lt; SDHCI_DIVIDER_SHIFT;
+	clk |= ((div &amp; SDHCI_DIV_HI_MASK) &gt;&gt; SDHCI_DIV_MASK_LEN)
+		&lt;&lt; SDHCI_DIVIDER_HI_SHIFT;
+	clk |= SDHCI_CLOCK_INT_EN;
+	bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
+
+	/* Wait max 20 ms */
+	timeout = 20;
+	while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
+		&amp; SDHCI_CLOCK_INT_STABLE)) {
+		if (timeout == 0) {
+			pr_err("%s: Internal clock never "
+				"stabilised.\n", mmc_hostname(host-&gt;mmc));
+			bcm2835_mmc_dumpregs(host);
+			return;
+		}
+		timeout--;
+		mdelay(1);
+	}
+
+	if (20-timeout &gt; 10 &amp;&amp; 20-timeout &gt; host-&gt;max_delay) {
+		host-&gt;max_delay = 20-timeout;
+		pr_warning("Warning: MMC controller hung for %d ms\n", host-&gt;max_delay);
+	}
+
+	clk |= SDHCI_CLOCK_CARD_EN;
+	bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
+}
+
+static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
+{
+	struct bcm2835_host *host;
+	unsigned long flags;
+
+	host = mmc_priv(mmc);
+
+	spin_lock_irqsave(&amp;host-&gt;lock, flags);
+
+	WARN_ON(host-&gt;mrq != NULL);
+
+	host-&gt;mrq = mrq;
+
+	if (mrq-&gt;sbc &amp;&amp; !(host-&gt;flags &amp; SDHCI_AUTO_CMD23))
+		bcm2835_mmc_send_command(host, mrq-&gt;sbc);
+	else
+		bcm2835_mmc_send_command(host, mrq-&gt;cmd);
+
+	spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+
+	if (!(mrq-&gt;sbc &amp;&amp; !(host-&gt;flags &amp; SDHCI_AUTO_CMD23)) &amp;&amp; mrq-&gt;cmd-&gt;data &amp;&amp; host-&gt;use_dma) {
+		/* DMA transfer starts now, PIO starts after interrupt */
+		bcm2835_mmc_transfer_dma(host);
+	}
+}
+
+
+static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
+{
+
+	struct bcm2835_host *host = mmc_priv(mmc);
+	unsigned long flags;
+	u8 ctrl;
+	u16 clk, ctrl_2;
+
+	pr_debug("bcm2835_mmc_set_ios: clock %d, pwr %d, bus_width %d, timing %d, vdd %d, drv_type %d\n",
+		 ios-&gt;clock, ios-&gt;power_mode, ios-&gt;bus_width,
+		 ios-&gt;timing, ios-&gt;signal_voltage, ios-&gt;drv_type);
+
+	spin_lock_irqsave(&amp;host-&gt;lock, flags);
+
+	if (!ios-&gt;clock || ios-&gt;clock != host-&gt;clock) {
+		bcm2835_mmc_set_clock(host, ios-&gt;clock);
+		host-&gt;clock = ios-&gt;clock;
+	}
+
+	if (host-&gt;pwr != SDHCI_POWER_330) {
+		host-&gt;pwr = SDHCI_POWER_330;
+		bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
+	}
+
+	ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
+
+	/* set bus width */
+	ctrl &amp;= ~SDHCI_CTRL_8BITBUS;
+	if (ios-&gt;bus_width == MMC_BUS_WIDTH_4)
+		ctrl |= SDHCI_CTRL_4BITBUS;
+	else
+		ctrl &amp;= ~SDHCI_CTRL_4BITBUS;
+
+	ctrl &amp;= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
+
+
+	bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
+	/*
+	 * We only need to set Driver Strength if the
+	 * preset value enable is not set.
+	 */
+	ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
+	ctrl_2 &amp;= ~SDHCI_CTRL_DRV_TYPE_MASK;
+	if (ios-&gt;drv_type == MMC_SET_DRIVER_TYPE_A)
+		ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
+	else if (ios-&gt;drv_type == MMC_SET_DRIVER_TYPE_C)
+		ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
+
+	bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
+
+	/* Reset SD Clock Enable */
+	clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
+	clk &amp;= ~SDHCI_CLOCK_CARD_EN;
+	bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
+
+	/* Re-enable SD Clock */
+	bcm2835_mmc_set_clock(host, host-&gt;clock);
+	bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
+
+	spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+}
+
+
+static struct mmc_host_ops bcm2835_ops = {
+	.request = bcm2835_mmc_request,
+	.set_ios = bcm2835_mmc_set_ios,
+	.enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
+	.ack_sdio_irq = bcm2835_mmc_ack_sdio_irq,
+};
+
+
+static void bcm2835_mmc_tasklet_finish(unsigned long param)
+{
+	struct bcm2835_host *host;
+	unsigned long flags;
+	struct mmc_request *mrq;
+
+	host = (struct bcm2835_host *)param;
+
+	spin_lock_irqsave(&amp;host-&gt;lock, flags);
+
+	/*
+	 * If this tasklet gets rescheduled while running, it will
+	 * be run again afterwards but without any active request.
+	 */
+	if (!host-&gt;mrq) {
+		spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+		return;
+	}
+
+	del_timer(&amp;host-&gt;timer);
+
+	mrq = host-&gt;mrq;
+
+	/*
+	 * The controller needs a reset of internal state machines
+	 * upon error conditions.
+	 */
+	if (!(host-&gt;flags &amp; SDHCI_DEVICE_DEAD) &amp;&amp;
+	    ((mrq-&gt;cmd &amp;&amp; mrq-&gt;cmd-&gt;error) ||
+		 (mrq-&gt;data &amp;&amp; (mrq-&gt;data-&gt;error ||
+		  (mrq-&gt;data-&gt;stop &amp;&amp; mrq-&gt;data-&gt;stop-&gt;error))))) {
+
+		spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+		bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
+		bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
+		spin_lock_irqsave(&amp;host-&gt;lock, flags);
+	}
+
+	host-&gt;mrq = NULL;
+	host-&gt;cmd = NULL;
+	host-&gt;data = NULL;
+
+	spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+	mmc_request_done(host-&gt;mmc, mrq);
+}
+
+
+
+static int bcm2835_mmc_add_host(struct bcm2835_host *host)
+{
+	struct mmc_host *mmc = host-&gt;mmc;
+	struct device *dev = mmc-&gt;parent;
+#ifndef FORCE_PIO
+	struct dma_slave_config cfg;
+#endif
+	int ret;
+
+	bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
+
+	host-&gt;clk_mul = 0;
+
+	if (!mmc-&gt;f_max || mmc-&gt;f_max &gt; host-&gt;max_clk)
+		mmc-&gt;f_max = host-&gt;max_clk;
+	mmc-&gt;f_min = host-&gt;max_clk / SDHCI_MAX_DIV_SPEC_300;
+
+	/* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
+	host-&gt;timeout_clk = mmc-&gt;f_max / 1000;
+	mmc-&gt;max_busy_timeout = (1 &lt;&lt; 27) / host-&gt;timeout_clk;
+
+	/* host controller capabilities */
+	mmc-&gt;caps |= MMC_CAP_CMD23 | MMC_CAP_ERASE | MMC_CAP_NEEDS_POLL |
+		MMC_CAP_SDIO_IRQ | MMC_CAP_SD_HIGHSPEED |
+		MMC_CAP_MMC_HIGHSPEED;
+
+	mmc-&gt;caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
+
+	host-&gt;flags = SDHCI_AUTO_CMD23;
+
+	dev_info(dev, "mmc_debug:%x mmc_debug2:%x\n", mmc_debug, mmc_debug2);
+#ifdef FORCE_PIO
+	dev_info(dev, "Forcing PIO mode\n");
+	host-&gt;have_dma = false;
+#else
+	if (IS_ERR_OR_NULL(host-&gt;dma_chan_rxtx)) {
+		dev_err(dev, "%s: Unable to initialise DMA channel. Falling back to PIO\n",
+			DRIVER_NAME);
+		host-&gt;have_dma = false;
+	} else {
+		dev_info(dev, "DMA channel allocated");
+
+		cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
+		cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
+		cfg.slave_id = 11;		/* DREQ channel */
+
+		/* Validate the slave configurations */
+
+		cfg.direction = DMA_MEM_TO_DEV;
+		cfg.src_addr = 0;
+		cfg.dst_addr = host-&gt;bus_addr + SDHCI_BUFFER;
+
+		ret = dmaengine_slave_config(host-&gt;dma_chan_rxtx, &amp;cfg);
+
+		if (ret == 0) {
+			host-&gt;dma_cfg_tx = cfg;
+
+			cfg.direction = DMA_DEV_TO_MEM;
+			cfg.src_addr = host-&gt;bus_addr + SDHCI_BUFFER;
+			cfg.dst_addr = 0;
+
+			ret = dmaengine_slave_config(host-&gt;dma_chan_rxtx, &amp;cfg);
+		}
+
+		if (ret == 0) {
+			host-&gt;dma_cfg_rx = cfg;
+
+			host-&gt;have_dma = true;
+		} else {
+			pr_err("%s: unable to configure DMA channel. "
+			       "Falling back to PIO\n",
+			       mmc_hostname(mmc));
+			dma_release_channel(host-&gt;dma_chan_rxtx);
+			host-&gt;dma_chan_rxtx = NULL;
+			host-&gt;have_dma = false;
+		}
+	}
+#endif
+	mmc-&gt;max_segs = 128;
+	if (swiotlb_max_segment())
+		mmc-&gt;max_req_size = (1 &lt;&lt; IO_TLB_SHIFT) * IO_TLB_SEGSIZE;
+	else
+		mmc-&gt;max_req_size = 524288;
+	mmc-&gt;max_seg_size = mmc-&gt;max_req_size;
+	mmc-&gt;max_blk_size = 512;
+	mmc-&gt;max_blk_count =  65535;
+
+	/* report supported voltage ranges */
+	mmc-&gt;ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
+
+	tasklet_init(&amp;host-&gt;finish_tasklet,
+		bcm2835_mmc_tasklet_finish, (unsigned long)host);
+
+	timer_setup(&amp;host-&gt;timer, bcm2835_mmc_timeout_timer, 0);
+	init_waitqueue_head(&amp;host-&gt;buf_ready_int);
+
+	bcm2835_mmc_init(host, 0);
+	ret = request_irq(host-&gt;irq, bcm2835_mmc_irq, IRQF_SHARED,
+				   mmc_hostname(mmc), host);
+	if (ret) {
+		dev_err(dev, "Failed to request IRQ %d: %d\n", host-&gt;irq, ret);
+		goto untasklet;
+	}
+
+	ret = mmc_add_host(mmc);
+	if (ret) {
+		dev_err(dev, "could not add MMC host\n");
+		goto free_irq;
+	}
+
+	return 0;
+
+free_irq:
+	free_irq(host-&gt;irq, host);
+untasklet:
+	tasklet_kill(&amp;host-&gt;finish_tasklet);
+
+	return ret;
+}
+
+static int bcm2835_mmc_probe(struct platform_device *pdev)
+{
+	struct device *dev = &amp;pdev-&gt;dev;
+	struct device_node *node = dev-&gt;of_node;
+	struct clk *clk;
+	struct resource *iomem;
+	struct bcm2835_host *host;
+	struct mmc_host *mmc;
+	const __be32 *addr;
+	int ret;
+
+	mmc = mmc_alloc_host(sizeof(*host), dev);
+	if (!mmc)
+		return -ENOMEM;
+
+	mmc-&gt;ops = &amp;bcm2835_ops;
+	host = mmc_priv(mmc);
+	host-&gt;mmc = mmc;
+	host-&gt;timeout = msecs_to_jiffies(1000);
+	spin_lock_init(&amp;host-&gt;lock);
+
+	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	host-&gt;ioaddr = devm_ioremap_resource(dev, iomem);
+	if (IS_ERR(host-&gt;ioaddr)) {
+		ret = PTR_ERR(host-&gt;ioaddr);
+		goto err;
+	}
+
+	addr = of_get_address(node, 0, NULL, NULL);
+	if (!addr) {
+		dev_err(dev, "could not get DMA-register address\n");
+		ret = -ENODEV;
+		goto err;
+	}
+	host-&gt;bus_addr = be32_to_cpup(addr);
+	pr_debug(" - ioaddr %lx, iomem-&gt;start %lx, bus_addr %lx\n",
+		 (unsigned long)host-&gt;ioaddr,
+		 (unsigned long)iomem-&gt;start,
+		 (unsigned long)host-&gt;bus_addr);
+
+#ifndef FORCE_PIO
+	if (node) {
+		host-&gt;dma_chan_rxtx = dma_request_slave_channel(dev, "rx-tx");
+		if (!host-&gt;dma_chan_rxtx)
+			host-&gt;dma_chan_rxtx =
+				dma_request_slave_channel(dev, "tx");
+		if (!host-&gt;dma_chan_rxtx)
+			host-&gt;dma_chan_rxtx =
+				dma_request_slave_channel(dev, "rx");
+	} else {
+		dma_cap_mask_t mask;
+
+		dma_cap_zero(mask);
+		/* we don't care about the channel, any would work */
+		dma_cap_set(DMA_SLAVE, mask);
+		host-&gt;dma_chan_rxtx = dma_request_channel(mask, NULL, NULL);
+	}
+#endif
+	clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(clk)) {
+		ret = PTR_ERR(clk);
+		if (ret == -EPROBE_DEFER)
+			dev_info(dev, "could not get clk, deferring probe\n");
+		else
+			dev_err(dev, "could not get clk\n");
+		goto err;
+	}
+
+	host-&gt;max_clk = clk_get_rate(clk);
+
+	host-&gt;irq = platform_get_irq(pdev, 0);
+	if (host-&gt;irq &lt;= 0) {
+		dev_err(dev, "get IRQ failed\n");
+		ret = -EINVAL;
+		goto err;
+	}
+
+	if (node) {
+		mmc_of_parse(mmc);
+
+		/* Read any custom properties */
+		of_property_read_u32(node,
+				     "brcm,overclock-50",
+				     &amp;host-&gt;overclock_50);
+	} else {
+		mmc-&gt;caps |= MMC_CAP_4_BIT_DATA;
+	}
+
+	ret = bcm2835_mmc_add_host(host);
+	if (ret)
+		goto err;
+
+	platform_set_drvdata(pdev, host);
+
+	return 0;
+err:
+	if (host-&gt;dma_chan_rxtx)
+		dma_release_channel(host-&gt;dma_chan_rxtx);
+	mmc_free_host(mmc);
+
+	return ret;
+}
+
+static int bcm2835_mmc_remove(struct platform_device *pdev)
+{
+	struct bcm2835_host *host = platform_get_drvdata(pdev);
+	unsigned long flags;
+	int dead;
+	u32 scratch;
+
+	dead = 0;
+	scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
+	if (scratch == (u32)-1)
+		dead = 1;
+
+
+	if (dead) {
+		spin_lock_irqsave(&amp;host-&gt;lock, flags);
+
+		host-&gt;flags |= SDHCI_DEVICE_DEAD;
+
+		if (host-&gt;mrq) {
+			pr_err("%s: Controller removed during "
+				" transfer!\n", mmc_hostname(host-&gt;mmc));
+
+			host-&gt;mrq-&gt;cmd-&gt;error = -ENOMEDIUM;
+			tasklet_schedule(&amp;host-&gt;finish_tasklet);
+		}
+
+		spin_unlock_irqrestore(&amp;host-&gt;lock, flags);
+	}
+
+	mmc_remove_host(host-&gt;mmc);
+
+	if (!dead)
+		bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
+
+	free_irq(host-&gt;irq, host);
+
+	del_timer_sync(&amp;host-&gt;timer);
+
+	tasklet_kill(&amp;host-&gt;finish_tasklet);
+
+	if (host-&gt;dma_chan_rxtx)
+		dma_release_channel(host-&gt;dma_chan_rxtx);
+
+	mmc_free_host(host-&gt;mmc);
+
+	return 0;
+}
+
+
+static const struct of_device_id bcm2835_mmc_match[] = {
+	{ .compatible = "brcm,bcm2835-mmc" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
+
+
+
+static struct platform_driver bcm2835_mmc_driver = {
+	.probe      = bcm2835_mmc_probe,
+	.remove     = bcm2835_mmc_remove,
+	.driver     = {
+		.name		= DRIVER_NAME,
+		.owner		= THIS_MODULE,
+		.of_match_table	= bcm2835_mmc_match,
+	},
+};
+module_platform_driver(bcm2835_mmc_driver);
+
+module_param(mmc_debug, uint, 0644);
+module_param(mmc_debug2, uint, 0644);
+MODULE_ALIAS("platform:mmc-bcm2835");
+MODULE_DESCRIPTION("BCM2835 SDHCI driver");
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Gellert Weisz");
--- a/include/linux/mmc/card.h
+++ b/include/linux/mmc/card.h
@@ -270,6 +270,8 @@ struct mmc_card {
 #define MMC_QUIRK_TRIM_BROKEN	(1&lt;&lt;12)		/* Skip trim */
 #define MMC_QUIRK_BROKEN_HPI	(1&lt;&lt;13)		/* Disable broken HPI support */
 
+#define MMC_QUIRK_ERASE_BROKEN	(1&lt;&lt;31)		/* Skip erase */
+
 	bool			reenable_cmdq;	/* Re-enable Command Queue */
 
 	unsigned int		erase_size;	/* erase size in sectors */
</pre></body></html>