1 From e15eff977effee05b5b47b20762e11ca345315c6 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] 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 bcm2835-mmc: Don't override bus width capabilities from devicetree
73 Take out the force setting of the MMC_CAP_4_BIT_DATA host capability
74 so that the result read from devicetree via mmc_of_parse() is
77 bcm2835-mmc: Only claim one DMA channel
79 With both MMC controllers enabled there are few DMA channels left. The
80 bcm2835-mmc driver only uses DMA in one direction at a time, so it
81 doesn't need to claim two channels.
83 See: https://github.com/raspberrypi/linux/issues/1327
85 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
87 bcm2835-mmc: New timer API
89 mmc: bcm2835-mmc: Support underclocking
91 Support underclocking of the SD bus using the max-frequency DT property
92 (which currently has no DT parameter). The sd_overclock parameter
93 already provides another way to achieve the same thing which should be
94 equivalent in end result, but it is a bug not to support max-frequency
97 See: https://github.com/raspberrypi/linux/issues/2350
99 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
101 mmc/bcm2835: Recover from MMC_SEND_EXT_CSD
103 If the user issues an "mmc extcsd read", the SD controller receives
104 what it thinks is a SEND_IF_COND command with an unexpected data block.
105 The resulting operations leave the FSM stuck in READWAIT, a state which
106 persists until the MMC framework resets the controller, by which point
107 the root filesystem is likely to have been unmounted.
109 A less heavyweight solution is to detect the condition and nudge the
110 FSM by asserting the (self-clearing) FORCE_DATA_MODE bit.
112 N.B. This workaround was essentially discovered by accident and without
113 a full understanding the inner workings of the controller, so it is
114 fortunate that the "fix" only modifies error paths.
116 See: https://github.com/raspberrypi/linux/issues/2728
118 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
120 bcm2835-mmc: Fix DMA channel leak
122 The BCM2835 MMC host driver requests a DMA channel on probe but neglects
123 to release the channel in the probe error path and on driver unbind.
125 I'm seeing this happen on every boot of the Compute Module 3: On first
126 driver probe, DMA channel 2 is allocated and then leaked with a "could
127 not get clk, deferring probe" message. On second driver probe, channel 4
132 Signed-off-by: Lukas Wunner <lukas@wunner.de>
133 Cc: Frank Pavlic <f.pavlic@kunbus.de>
135 bcm2835-mmc: Fix struct mmc_host leak on probe
137 The BCM2835 MMC host driver requests the bus address of the host's
138 register map on probe. If that fails, the driver leaks the struct
139 mmc_host allocated earlier.
143 Signed-off-by: Lukas Wunner <lukas@wunner.de>
144 Cc: Frank Pavlic <f.pavlic@kunbus.de>
146 bcm2835-mmc: Fix duplicate free_irq() on remove
148 The BCM2835 MMC host driver requests its interrupt as a device-managed
149 resource, so the interrupt is automatically freed after the driver is
152 However on driver unbind, bcm2835_mmc_remove() frees the interrupt
153 explicitly to avoid invocation of the interrupt handler after driver
154 structures have been torn down.
156 The interrupt is thus freed twice, leading to a WARN splat in
157 __free_irq(). Fix by not requesting the interrupt as a device-managed
160 Signed-off-by: Lukas Wunner <lukas@wunner.de>
161 Cc: Frank Pavlic <f.pavlic@kunbus.de>
163 bcm2835-mmc: Handle mmc_add_host() errors
165 The BCM2835 MMC host driver calls mmc_add_host() but doesn't check its
166 return value. Errors occurring in that function are therefore not
169 Signed-off-by: Lukas Wunner <lukas@wunner.de>
170 Cc: Frank Pavlic <f.pavlic@kunbus.de>
172 bcm2835-mmc: Deduplicate reset of driver data on remove
174 The BCM2835 MMC host driver sets the device's driver data pointer to
175 NULL on ->remove() even though the driver core subsequently does the
176 same in __device_release_driver(). Drop the duplicate assignment.
178 Signed-off-by: Lukas Wunner <lukas@wunner.de>
179 Cc: Frank Pavlic <f.pavlic@kunbus.de>
181 bcm2835_mmc: Remove vestigial threaded IRQ
183 With SDIO processing now managed by the MMC framework with a
184 workqueue, the bcm2835_mmc driver no longer needs a threaded
187 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
189 Add missing dma_unmap_sg calls to free relevant swiotlb bounce buffers.
190 This prevents DMA leaks.
192 Signed-off-by: Yaroslav Rosomakho <yaroslavros@gmail.com>
194 Limit max_req_size under arm64 (or any other platform that uses swiotlb) to prevent potential buffer overflow due to bouncing.
196 Signed-off-by: Yaroslav Rosomakho <yaroslavros@gmail.com>
198 drivers/mmc/core/block.c | 28 +-
199 drivers/mmc/core/core.c | 3 +-
200 drivers/mmc/core/host.c | 17 +-
201 drivers/mmc/core/quirks.h | 8 +
202 drivers/mmc/host/Kconfig | 29 +
203 drivers/mmc/host/Makefile | 1 +
204 drivers/mmc/host/bcm2835-mmc.c | 1576 ++++++++++++++++++++++++++++++++
205 include/linux/mmc/card.h | 2 +
206 8 files changed, 1659 insertions(+), 5 deletions(-)
207 create mode 100644 drivers/mmc/host/bcm2835-mmc.c
209 --- a/drivers/mmc/core/block.c
210 +++ b/drivers/mmc/core/block.c
211 @@ -166,6 +166,13 @@ static DEFINE_MUTEX(open_lock);
212 module_param(perdev_minors, int, 0444);
213 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
216 + * Allow quirks to be overridden for the current card
218 +static char *card_quirks;
219 +module_param(card_quirks, charp, 0644);
220 +MODULE_PARM_DESC(card_quirks, "Force the use of the indicated quirks (a bitfield)");
222 static inline int mmc_blk_part_switch(struct mmc_card *card,
223 unsigned int part_type);
225 @@ -2891,6 +2898,7 @@ static int mmc_blk_probe(struct mmc_card
227 struct mmc_blk_data *md, *part_md;
229 + char quirk_str[24];
232 * Check that the card supports the command class(es) we need.
233 @@ -2898,7 +2906,16 @@ static int mmc_blk_probe(struct mmc_card
234 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
237 - mmc_fixup_device(card, mmc_blk_fixups);
239 + unsigned long quirks;
240 + if (kstrtoul(card_quirks, 0, &quirks) == 0)
241 + card->quirks = (unsigned int)quirks;
243 + pr_err("mmc_block: Invalid card_quirks parameter '%s'\n",
247 + mmc_fixup_device(card, mmc_blk_fixups);
249 card->complete_wq = alloc_workqueue("mmc_complete",
250 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
251 @@ -2913,9 +2930,14 @@ static int mmc_blk_probe(struct mmc_card
253 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
254 cap_str, sizeof(cap_str));
255 - pr_info("%s: %s %s %s %s\n",
257 + snprintf(quirk_str, sizeof(quirk_str),
258 + " (quirks 0x%08x)", card->quirks);
260 + quirk_str[0] = '\0';
261 + pr_info("%s: %s %s %s%s%s\n",
262 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
263 - cap_str, md->read_only ? "(ro)" : "");
264 + cap_str, md->read_only ? " (ro)" : "", quirk_str);
266 if (mmc_blk_alloc_parts(card, md))
268 --- a/drivers/mmc/core/core.c
269 +++ b/drivers/mmc/core/core.c
270 @@ -1925,7 +1925,8 @@ EXPORT_SYMBOL(mmc_erase);
271 int mmc_can_erase(struct mmc_card *card)
273 if ((card->host->caps & MMC_CAP_ERASE) &&
274 - (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
275 + (card->csd.cmdclass & CCC_ERASE) && card->erase_size &&
276 + !(card->quirks & MMC_QUIRK_ERASE_BROKEN))
280 --- a/drivers/mmc/core/host.c
281 +++ b/drivers/mmc/core/host.c
282 @@ -434,15 +434,30 @@ struct mmc_host *mmc_alloc_host(int extr
285 struct mmc_host *host;
288 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
292 + /* If OF aliases exist, start dynamic assignment after highest */
293 + id = of_alias_get_highest_id("mmc");
294 + id = (id < 0) ? 0 : id + 1;
296 + /* If this devices has OF node, maybe it has an alias */
297 + if (dev->of_node) {
298 + int of_id = of_alias_get_id(dev->of_node, "mmc");
301 + dev_warn(dev, "/aliases ID not available\n");
306 /* scanning will be enabled when we're ready */
307 host->rescan_disable = 1;
309 - err = ida_simple_get(&mmc_host_ida, 0, 0, GFP_KERNEL);
310 + err = ida_simple_get(&mmc_host_ida, id, 0, GFP_KERNEL);
314 --- a/drivers/mmc/core/quirks.h
315 +++ b/drivers/mmc/core/quirks.h
316 @@ -99,6 +99,14 @@ static const struct mmc_fixup mmc_blk_fi
317 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
318 MMC_QUIRK_TRIM_BROKEN),
321 + * On some Kingston SD cards, multiple erases of less than 64
322 + * sectors can cause corruption.
324 + MMC_FIXUP("SD16G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
325 + MMC_FIXUP("SD32G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
326 + MMC_FIXUP("SD64G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
331 --- a/drivers/mmc/host/Kconfig
332 +++ b/drivers/mmc/host/Kconfig
335 comment "MMC/SD/SDIO Host Controller Drivers"
337 +config MMC_BCM2835_MMC
338 + tristate "MMC support on BCM2835"
339 + depends on MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835
341 + This selects the MMC Interface on BCM2835.
343 + If you have a controller with this interface, say Y or M here.
347 +config MMC_BCM2835_DMA
348 + bool "DMA support on BCM2835 Arasan controller"
349 + depends on MMC_BCM2835_MMC
351 + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
356 +config MMC_BCM2835_PIO_DMA_BARRIER
357 + int "Block count limit for PIO transfers"
358 + depends on MMC_BCM2835_MMC && MMC_BCM2835_DMA
362 + The inclusive limit in bytes under which PIO will be used instead of DMA
364 + If unsure, say 2 here.
367 bool "MMC host drivers debugging"
369 --- a/drivers/mmc/host/Makefile
370 +++ b/drivers/mmc/host/Makefile
371 @@ -23,6 +23,7 @@ obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci
372 obj-$(CONFIG_MMC_SDHCI_F_SDH30) += sdhci_f_sdh30.o
373 obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
374 obj-$(CONFIG_MMC_SDHCI_AM654) += sdhci_am654.o
375 +obj-$(CONFIG_MMC_BCM2835_MMC) += bcm2835-mmc.o
376 obj-$(CONFIG_MMC_WBSD) += wbsd.o
377 obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
378 obj-$(CONFIG_MMC_ALCOR) += alcor.o
380 +++ b/drivers/mmc/host/bcm2835-mmc.c
383 + * BCM2835 MMC host driver.
385 + * Author: Gellert Weisz <gellert@raspberrypi.org>
389 + * sdhci-bcm2708.c by Broadcom
390 + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
391 + * sdhci.c and sdhci-pci.c by Pierre Ossman
393 + * This program is free software; you can redistribute it and/or modify it
394 + * under the terms and conditions of the GNU General Public License,
395 + * version 2, as published by the Free Software Foundation.
397 + * This program is distributed in the hope it will be useful, but WITHOUT
398 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
399 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
402 + * You should have received a copy of the GNU General Public License
403 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
406 +#include <linux/delay.h>
407 +#include <linux/module.h>
408 +#include <linux/io.h>
409 +#include <linux/mmc/mmc.h>
410 +#include <linux/mmc/host.h>
411 +#include <linux/mmc/sd.h>
412 +#include <linux/scatterlist.h>
413 +#include <linux/of_address.h>
414 +#include <linux/of_irq.h>
415 +#include <linux/clk.h>
416 +#include <linux/platform_device.h>
417 +#include <linux/err.h>
418 +#include <linux/blkdev.h>
419 +#include <linux/dmaengine.h>
420 +#include <linux/dma-mapping.h>
421 +#include <linux/of_dma.h>
422 +#include <linux/swiotlb.h>
427 +#define DRIVER_NAME "mmc-bcm2835"
429 +#define DBG(f, x...) \
430 +pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
432 +#ifndef CONFIG_MMC_BCM2835_DMA
437 +/* the inclusive limit in bytes under which PIO will be used instead of DMA */
438 +#ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
439 +#define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
441 +#define PIO_DMA_BARRIER 00
444 +#define MIN_FREQ 400000
445 +#define TIMEOUT_VAL 0xE
446 +#define BCM2835_SDHCI_WRITE_DELAY(f) (((2 * 1000000) / f) + 1)
450 +unsigned mmc_debug2;
452 +struct bcm2835_host {
455 + void __iomem *ioaddr;
458 + struct mmc_host *mmc;
462 + int clock; /* Current clock speed */
463 + u8 pwr; /* Current voltage */
465 + unsigned int max_clk; /* Max possible freq */
466 + unsigned int timeout_clk; /* Timeout freq (KHz) */
467 + unsigned int clk_mul; /* Clock Muliplier value */
469 + struct tasklet_struct finish_tasklet; /* Tasklet structures */
471 + struct timer_list timer; /* Timer for timeouts */
473 + struct sg_mapping_iter sg_miter; /* SG state for PIO */
474 + unsigned int blocks; /* remaining PIO blocks */
476 + int irq; /* Device IRQ */
479 + u32 ier; /* cached registers */
481 + struct mmc_request *mrq; /* Current request */
482 + struct mmc_command *cmd; /* Current command */
483 + struct mmc_data *data; /* Current data request */
484 + unsigned int data_early:1; /* Data finished before cmd */
486 + wait_queue_head_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
491 + struct dma_chan *dma_chan_rxtx; /* DMA channel for reads and writes */
492 + struct dma_slave_config dma_cfg_rx;
493 + struct dma_slave_config dma_cfg_tx;
494 + struct dma_async_tx_descriptor *tx_desc; /* descriptor */
499 + /*end of DMA part*/
501 + int max_delay; /* maximum length of time spent waiting */
503 + int flags; /* Host attributes */
504 +#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
505 +#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
506 +#define SDHCI_AUTO_CMD12 (1<<6) /* Auto CMD12 support */
507 +#define SDHCI_AUTO_CMD23 (1<<7) /* Auto CMD23 support */
508 +#define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
510 + u32 overclock_50; /* frequency to use when 50MHz is requested (in MHz) */
511 + u32 max_overclock; /* Highest reported */
515 +static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg, int from)
518 + lockdep_assert_held_once(&host->lock);
519 + writel(val, host->ioaddr + reg);
520 + udelay(BCM2835_SDHCI_WRITE_DELAY(max(host->clock, MIN_FREQ)));
522 + delay = ((mmc_debug >> 16) & 0xf) << ((mmc_debug >> 20) & 0xf);
523 + if (delay && !((1<<from) & mmc_debug2))
527 +static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
530 + lockdep_assert_held_once(&host->lock);
531 + writel(val, host->ioaddr + reg);
533 + delay = ((mmc_debug >> 24) & 0xf) << ((mmc_debug >> 28) & 0xf);
538 +static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
540 + lockdep_assert_held_once(&host->lock);
541 + return readl(host->ioaddr + reg);
544 +static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
546 + u32 oldval = (reg == SDHCI_COMMAND) ? host->shadow :
547 + bcm2835_mmc_readl(host, reg & ~3);
548 + u32 word_num = (reg >> 1) & 1;
549 + u32 word_shift = word_num * 16;
550 + u32 mask = 0xffff << word_shift;
551 + u32 newval = (oldval & ~mask) | (val << word_shift);
553 + if (reg == SDHCI_TRANSFER_MODE)
554 + host->shadow = newval;
556 + bcm2835_mmc_writel(host, newval, reg & ~3, 0);
560 +static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
562 + u32 oldval = bcm2835_mmc_readl(host, reg & ~3);
563 + u32 byte_num = reg & 3;
564 + u32 byte_shift = byte_num * 8;
565 + u32 mask = 0xff << byte_shift;
566 + u32 newval = (oldval & ~mask) | (val << byte_shift);
568 + bcm2835_mmc_writel(host, newval, reg & ~3, 1);
572 +static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
574 + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
575 + u32 word_num = (reg >> 1) & 1;
576 + u32 word_shift = word_num * 16;
577 + u32 word = (val >> word_shift) & 0xffff;
582 +static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
584 + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
585 + u32 byte_num = reg & 3;
586 + u32 byte_shift = byte_num * 8;
587 + u32 byte = (val >> byte_shift) & 0xff;
592 +static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
596 + ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
598 + /* change which requests generate IRQs - makes no difference to
599 + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
600 + bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE, 2);
604 +static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
606 + pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
607 + mmc_hostname(host->mmc));
609 + pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
610 + bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
611 + bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
612 + pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
613 + bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
614 + bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
615 + pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
616 + bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
617 + bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
618 + pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
619 + bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
620 + bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
621 + pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
622 + bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
623 + bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
624 + pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
625 + bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
626 + bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
627 + pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
628 + bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
629 + bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
630 + pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
631 + bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
632 + bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
633 + pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
634 + bcm2835_mmc_readw(host, SDHCI_AUTO_CMD_STATUS),
635 + bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
636 + pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
637 + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
638 + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
639 + pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
640 + bcm2835_mmc_readw(host, SDHCI_COMMAND),
641 + bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
642 + pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
643 + bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
645 + pr_debug(DRIVER_NAME ": ===========================================\n");
649 +static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
651 + unsigned long timeout;
652 + unsigned long flags;
654 + spin_lock_irqsave(&host->lock, flags);
655 + bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
657 + if (mask & SDHCI_RESET_ALL)
660 + /* Wait max 100 ms */
663 + /* hw clears the bit when it's done */
664 + while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
665 + if (timeout == 0) {
666 + pr_err("%s: Reset 0x%x never completed.\n",
667 + mmc_hostname(host->mmc), (int)mask);
668 + bcm2835_mmc_dumpregs(host);
672 + spin_unlock_irqrestore(&host->lock, flags);
674 + spin_lock_irqsave(&host->lock, flags);
677 + if (100-timeout > 10 && 100-timeout > host->max_delay) {
678 + host->max_delay = 100-timeout;
679 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
681 + spin_unlock_irqrestore(&host->lock, flags);
684 +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
686 +static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
688 + unsigned long flags;
690 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
692 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
694 + host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
695 + SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
696 + SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
697 + SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
698 + SDHCI_INT_RESPONSE;
700 + spin_lock_irqsave(&host->lock, flags);
701 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 3);
702 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 3);
703 + spin_unlock_irqrestore(&host->lock, flags);
706 + /* force clock reconfiguration */
708 + bcm2835_mmc_set_ios(host->mmc, &host->mmc->ios);
714 +static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
716 +static void bcm2835_mmc_dma_complete(void *param)
718 + struct bcm2835_host *host = param;
719 + struct dma_chan *dma_chan;
720 + unsigned long flags;
723 + spin_lock_irqsave(&host->lock, flags);
725 + host->use_dma = false;
728 + dma_chan = host->dma_chan_rxtx;
729 + if (host->data->flags & MMC_DATA_WRITE)
730 + dir_data = DMA_TO_DEVICE;
732 + dir_data = DMA_FROM_DEVICE;
733 + dma_unmap_sg(dma_chan->device->dev,
734 + host->data->sg, host->data->sg_len,
736 + if (! (host->data->flags & MMC_DATA_WRITE))
737 + bcm2835_mmc_finish_data(host);
738 + } else if (host->wait_for_dma) {
739 + host->wait_for_dma = false;
740 + tasklet_schedule(&host->finish_tasklet);
743 + spin_unlock_irqrestore(&host->lock, flags);
746 +static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
748 + unsigned long flags;
749 + size_t blksize, len, chunk;
751 + u32 uninitialized_var(scratch);
754 + blksize = host->data->blksz;
757 + local_irq_save(flags);
760 + if (!sg_miter_next(&host->sg_miter))
763 + len = min(host->sg_miter.length, blksize);
766 + host->sg_miter.consumed = len;
768 + buf = host->sg_miter.addr;
772 + scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
776 + *buf = scratch & 0xFF;
785 + sg_miter_stop(&host->sg_miter);
787 + local_irq_restore(flags);
790 +static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
792 + unsigned long flags;
793 + size_t blksize, len, chunk;
797 + blksize = host->data->blksz;
802 + local_irq_save(flags);
805 + if (!sg_miter_next(&host->sg_miter))
808 + len = min(host->sg_miter.length, blksize);
811 + host->sg_miter.consumed = len;
813 + buf = host->sg_miter.addr;
816 + scratch |= (u32)*buf << (chunk * 8);
822 + if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
823 + mmc_raw_writel(host, scratch, SDHCI_BUFFER);
830 + sg_miter_stop(&host->sg_miter);
832 + local_irq_restore(flags);
836 +static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
840 + BUG_ON(!host->data);
842 + if (host->blocks == 0)
845 + if (host->data->flags & MMC_DATA_READ)
846 + mask = SDHCI_DATA_AVAILABLE;
848 + mask = SDHCI_SPACE_AVAILABLE;
850 + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
852 + if (host->data->flags & MMC_DATA_READ)
853 + bcm2835_bcm2835_mmc_read_block_pio(host);
855 + bcm2835_bcm2835_mmc_write_block_pio(host);
859 + /* QUIRK used in sdhci.c removes the 'if' */
860 + /* but it seems this is unnecessary */
861 + if (host->blocks == 0)
869 +static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
871 + u32 len, dir_data, dir_slave;
872 + struct dma_async_tx_descriptor *desc = NULL;
873 + struct dma_chan *dma_chan;
876 + WARN_ON(!host->data);
881 + if (host->blocks == 0)
884 + dma_chan = host->dma_chan_rxtx;
885 + if (host->data->flags & MMC_DATA_READ) {
886 + dir_data = DMA_FROM_DEVICE;
887 + dir_slave = DMA_DEV_TO_MEM;
889 + dir_data = DMA_TO_DEVICE;
890 + dir_slave = DMA_MEM_TO_DEV;
893 + /* The parameters have already been validated, so this will not fail */
894 + (void)dmaengine_slave_config(dma_chan,
895 + (dir_data == DMA_FROM_DEVICE) ?
896 + &host->dma_cfg_rx :
897 + &host->dma_cfg_tx);
899 + BUG_ON(!dma_chan->device);
900 + BUG_ON(!dma_chan->device->dev);
901 + BUG_ON(!host->data->sg);
903 + len = dma_map_sg(dma_chan->device->dev, host->data->sg,
904 + host->data->sg_len, dir_data);
906 + desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
908 + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
910 + dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
913 + unsigned long flags;
914 + spin_lock_irqsave(&host->lock, flags);
915 + bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
916 + SDHCI_INT_SPACE_AVAIL);
917 + host->tx_desc = desc;
918 + desc->callback = bcm2835_mmc_dma_complete;
919 + desc->callback_param = host;
920 + spin_unlock_irqrestore(&host->lock, flags);
921 + dmaengine_submit(desc);
922 + dma_async_issue_pending(dma_chan);
924 + dma_unmap_sg(dma_chan->device->dev, host->data->sg, len, dir_data);
931 +static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
933 + u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
934 + u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
937 + host->ier = (host->ier & ~pio_irqs) | dma_irqs;
939 + host->ier = (host->ier & ~dma_irqs) | pio_irqs;
941 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 4);
942 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 4);
946 +static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
949 + struct mmc_data *data = cmd->data;
951 + WARN_ON(host->data);
953 + if (data || (cmd->flags & MMC_RSP_BUSY)) {
954 + count = TIMEOUT_VAL;
955 + bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
961 + /* Sanity checks */
962 + BUG_ON(data->blksz * data->blocks > 524288);
963 + BUG_ON(data->blksz > host->mmc->max_blk_size);
964 + BUG_ON(data->blocks > 65535);
967 + host->data_early = 0;
968 + host->data->bytes_xfered = 0;
971 + if (!(host->flags & SDHCI_REQ_USE_DMA)) {
974 + flags = SG_MITER_ATOMIC;
975 + if (host->data->flags & MMC_DATA_READ)
976 + flags |= SG_MITER_TO_SG;
978 + flags |= SG_MITER_FROM_SG;
979 + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
980 + host->blocks = data->blocks;
983 + host->use_dma = host->have_dma && data->blocks > PIO_DMA_BARRIER;
985 + bcm2835_mmc_set_transfer_irqs(host);
987 + /* Set the DMA boundary value and block size */
988 + bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
989 + data->blksz), SDHCI_BLOCK_SIZE);
990 + bcm2835_mmc_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
992 + BUG_ON(!host->data);
995 +static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
996 + struct mmc_command *cmd)
999 + struct mmc_data *data = cmd->data;
1001 + if (data == NULL) {
1002 + /* clear Auto CMD settings for no data CMDs */
1003 + mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
1004 + bcm2835_mmc_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
1005 + SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
1009 + WARN_ON(!host->data);
1011 + mode = SDHCI_TRNS_BLK_CNT_EN;
1013 + if ((mmc_op_multi(cmd->opcode) || data->blocks > 1)) {
1014 + mode |= SDHCI_TRNS_MULTI;
1017 + * If we are sending CMD23, CMD12 never gets sent
1018 + * on successful completion (so no Auto-CMD12).
1020 + if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
1021 + mode |= SDHCI_TRNS_AUTO_CMD12;
1022 + else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
1023 + mode |= SDHCI_TRNS_AUTO_CMD23;
1024 + bcm2835_mmc_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2, 5);
1028 + if (data->flags & MMC_DATA_READ)
1029 + mode |= SDHCI_TRNS_READ;
1030 + if (host->flags & SDHCI_REQ_USE_DMA)
1031 + mode |= SDHCI_TRNS_DMA;
1033 + bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
1036 +void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
1040 + unsigned long timeout;
1042 + WARN_ON(host->cmd);
1044 + /* Wait max 10 ms */
1047 + mask = SDHCI_CMD_INHIBIT;
1048 + if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1049 + mask |= SDHCI_DATA_INHIBIT;
1051 + /* We shouldn't wait for data inihibit for stop commands, even
1052 + though they might use busy signaling */
1053 + if (host->mrq->data && (cmd == host->mrq->data->stop))
1054 + mask &= ~SDHCI_DATA_INHIBIT;
1056 + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
1057 + if (timeout == 0) {
1058 + pr_err("%s: Controller never released inhibit bit(s).\n",
1059 + mmc_hostname(host->mmc));
1060 + bcm2835_mmc_dumpregs(host);
1061 + cmd->error = -EIO;
1062 + tasklet_schedule(&host->finish_tasklet);
1069 + if ((1000-timeout)/100 > 1 && (1000-timeout)/100 > host->max_delay) {
1070 + host->max_delay = (1000-timeout)/100;
1071 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
1074 + timeout = jiffies;
1075 + if (!cmd->data && cmd->busy_timeout > 9000)
1076 + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1078 + timeout += 10 * HZ;
1079 + mod_timer(&host->timer, timeout);
1082 + host->use_dma = false;
1084 + bcm2835_mmc_prepare_data(host, cmd);
1086 + bcm2835_mmc_writel(host, cmd->arg, SDHCI_ARGUMENT, 6);
1088 + bcm2835_mmc_set_transfer_mode(host, cmd);
1090 + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1091 + pr_err("%s: Unsupported response type!\n",
1092 + mmc_hostname(host->mmc));
1093 + cmd->error = -EINVAL;
1094 + tasklet_schedule(&host->finish_tasklet);
1098 + if (!(cmd->flags & MMC_RSP_PRESENT))
1099 + flags = SDHCI_CMD_RESP_NONE;
1100 + else if (cmd->flags & MMC_RSP_136)
1101 + flags = SDHCI_CMD_RESP_LONG;
1102 + else if (cmd->flags & MMC_RSP_BUSY)
1103 + flags = SDHCI_CMD_RESP_SHORT_BUSY;
1105 + flags = SDHCI_CMD_RESP_SHORT;
1107 + if (cmd->flags & MMC_RSP_CRC)
1108 + flags |= SDHCI_CMD_CRC;
1109 + if (cmd->flags & MMC_RSP_OPCODE)
1110 + flags |= SDHCI_CMD_INDEX;
1113 + flags |= SDHCI_CMD_DATA;
1115 + bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1119 +static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
1121 + struct mmc_data *data;
1123 + BUG_ON(!host->data);
1125 + data = host->data;
1126 + host->data = NULL;
1129 + data->bytes_xfered = 0;
1131 + data->bytes_xfered = data->blksz * data->blocks;
1134 + * Need to send CMD12 if -
1135 + * a) open-ended multiblock transfer (no CMD23)
1136 + * b) error in multiblock transfer
1140 + !host->mrq->sbc)) {
1143 + * The controller needs a reset of internal state machines
1144 + * upon error conditions.
1146 + if (data->error) {
1147 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
1148 + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
1151 + bcm2835_mmc_send_command(host, data->stop);
1152 + } else if (host->use_dma) {
1153 + host->wait_for_dma = true;
1155 + tasklet_schedule(&host->finish_tasklet);
1159 +static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
1163 + BUG_ON(host->cmd == NULL);
1165 + if (host->cmd->flags & MMC_RSP_PRESENT) {
1166 + if (host->cmd->flags & MMC_RSP_136) {
1167 + /* CRC is stripped so we need to do some shifting. */
1168 + for (i = 0; i < 4; i++) {
1169 + host->cmd->resp[i] = bcm2835_mmc_readl(host,
1170 + SDHCI_RESPONSE + (3-i)*4) << 8;
1172 + host->cmd->resp[i] |=
1173 + bcm2835_mmc_readb(host,
1174 + SDHCI_RESPONSE + (3-i)*4-1);
1177 + host->cmd->resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
1181 + host->cmd->error = 0;
1183 + /* Finished CMD23, now send actual command. */
1184 + if (host->cmd == host->mrq->sbc) {
1186 + bcm2835_mmc_send_command(host, host->mrq->cmd);
1188 + if (host->mrq->cmd->data && host->use_dma) {
1189 + /* DMA transfer starts now, PIO starts after interrupt */
1190 + bcm2835_mmc_transfer_dma(host);
1194 + /* Processed actual command. */
1195 + if (host->data && host->data_early)
1196 + bcm2835_mmc_finish_data(host);
1198 + if (!host->cmd->data)
1199 + tasklet_schedule(&host->finish_tasklet);
1206 +static void bcm2835_mmc_timeout_timer(struct timer_list *t)
1208 + struct bcm2835_host *host = from_timer(host, t, timer);
1209 + unsigned long flags;
1211 + spin_lock_irqsave(&host->lock, flags);
1214 + pr_err("%s: Timeout waiting for hardware interrupt.\n",
1215 + mmc_hostname(host->mmc));
1216 + bcm2835_mmc_dumpregs(host);
1219 + host->data->error = -ETIMEDOUT;
1220 + bcm2835_mmc_finish_data(host);
1223 + host->cmd->error = -ETIMEDOUT;
1225 + host->mrq->cmd->error = -ETIMEDOUT;
1227 + tasklet_schedule(&host->finish_tasklet);
1231 + spin_unlock_irqrestore(&host->lock, flags);
1235 +static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
1237 + if (!(host->flags & SDHCI_DEVICE_DEAD)) {
1239 + host->ier |= SDHCI_INT_CARD_INT;
1241 + host->ier &= ~SDHCI_INT_CARD_INT;
1243 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 7);
1244 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 7);
1248 +static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1250 + struct bcm2835_host *host = mmc_priv(mmc);
1251 + unsigned long flags;
1253 + spin_lock_irqsave(&host->lock, flags);
1255 + host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1257 + host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1259 + bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
1260 + spin_unlock_irqrestore(&host->lock, flags);
1263 +static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
1266 + BUG_ON(intmask == 0);
1269 + pr_err("%s: Got command interrupt 0x%08x even "
1270 + "though no command operation was in progress.\n",
1271 + mmc_hostname(host->mmc), (unsigned)intmask);
1272 + bcm2835_mmc_dumpregs(host);
1276 + if (intmask & SDHCI_INT_TIMEOUT)
1277 + host->cmd->error = -ETIMEDOUT;
1278 + else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1279 + SDHCI_INT_INDEX)) {
1280 + host->cmd->error = -EILSEQ;
1283 + if (host->cmd->error) {
1284 + tasklet_schedule(&host->finish_tasklet);
1288 + if (intmask & SDHCI_INT_RESPONSE)
1289 + bcm2835_mmc_finish_command(host);
1293 +static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
1295 + struct dma_chan *dma_chan;
1298 + BUG_ON(intmask == 0);
1300 + if (!host->data) {
1302 + * The "data complete" interrupt is also used to
1303 + * indicate that a busy state has ended. See comment
1304 + * above in sdhci_cmd_irq().
1306 + if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1307 + if (intmask & SDHCI_INT_DATA_END) {
1308 + bcm2835_mmc_finish_command(host);
1313 + pr_debug("%s: Got data interrupt 0x%08x even "
1314 + "though no data operation was in progress.\n",
1315 + mmc_hostname(host->mmc), (unsigned)intmask);
1316 + bcm2835_mmc_dumpregs(host);
1321 + if (intmask & SDHCI_INT_DATA_TIMEOUT)
1322 + host->data->error = -ETIMEDOUT;
1323 + else if (intmask & SDHCI_INT_DATA_END_BIT)
1324 + host->data->error = -EILSEQ;
1325 + else if ((intmask & SDHCI_INT_DATA_CRC) &&
1326 + SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
1327 + != MMC_BUS_TEST_R)
1328 + host->data->error = -EILSEQ;
1330 + if (host->use_dma) {
1331 + if (host->data->flags & MMC_DATA_WRITE) {
1332 + /* IRQ handled here */
1334 + dma_chan = host->dma_chan_rxtx;
1335 + dir_data = DMA_TO_DEVICE;
1336 + dma_unmap_sg(dma_chan->device->dev,
1337 + host->data->sg, host->data->sg_len,
1340 + bcm2835_mmc_finish_data(host);
1344 + if (host->data->error)
1345 + bcm2835_mmc_finish_data(host);
1347 + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
1348 + bcm2835_mmc_transfer_pio(host);
1350 + if (intmask & SDHCI_INT_DATA_END) {
1353 + * Data managed to finish before the
1354 + * command completed. Make sure we do
1355 + * things in the proper order.
1357 + host->data_early = 1;
1359 + bcm2835_mmc_finish_data(host);
1367 +static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
1369 + irqreturn_t result = IRQ_NONE;
1370 + struct bcm2835_host *host = dev_id;
1371 + u32 intmask, mask, unexpected = 0;
1372 + int max_loops = 16;
1374 + spin_lock(&host->lock);
1376 + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1378 + if (!intmask || intmask == 0xffffffff) {
1379 + result = IRQ_NONE;
1384 + /* Clear selected interrupts. */
1385 + mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
1386 + SDHCI_INT_BUS_POWER);
1387 + bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS, 8);
1390 + if (intmask & SDHCI_INT_CMD_MASK)
1391 + bcm2835_mmc_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
1393 + if (intmask & SDHCI_INT_DATA_MASK)
1394 + bcm2835_mmc_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
1396 + if (intmask & SDHCI_INT_BUS_POWER)
1397 + pr_err("%s: Card is consuming too much power!\n",
1398 + mmc_hostname(host->mmc));
1400 + if (intmask & SDHCI_INT_CARD_INT) {
1401 + bcm2835_mmc_enable_sdio_irq_nolock(host, false);
1402 + sdio_signal_irq(host->mmc);
1405 + intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
1406 + SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
1407 + SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
1408 + SDHCI_INT_CARD_INT);
1411 + unexpected |= intmask;
1412 + bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS, 9);
1415 + if (result == IRQ_NONE)
1416 + result = IRQ_HANDLED;
1418 + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1419 + } while (intmask && --max_loops);
1421 + spin_unlock(&host->lock);
1424 + pr_err("%s: Unexpected interrupt 0x%08x.\n",
1425 + mmc_hostname(host->mmc), unexpected);
1426 + bcm2835_mmc_dumpregs(host);
1433 +static void bcm2835_mmc_ack_sdio_irq(struct mmc_host *mmc)
1435 + struct bcm2835_host *host = mmc_priv(mmc);
1436 + unsigned long flags;
1438 + spin_lock_irqsave(&host->lock, flags);
1439 + if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
1440 + bcm2835_mmc_enable_sdio_irq_nolock(host, true);
1441 + spin_unlock_irqrestore(&host->lock, flags);
1444 +void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
1446 + int div = 0; /* Initialized for compiler warning */
1447 + int real_div = div, clk_mul = 1;
1449 + unsigned long timeout;
1450 + unsigned int input_clock = clock;
1452 + if (host->overclock_50 && (clock == 50000000))
1453 + clock = host->overclock_50 * 1000000 + 999999;
1455 + host->mmc->actual_clock = 0;
1457 + bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
1462 + /* Version 3.00 divisors must be a multiple of 2. */
1463 + if (host->max_clk <= clock)
1466 + for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1468 + if ((host->max_clk / div) <= clock)
1477 + clock = (host->max_clk * clk_mul) / real_div;
1478 + host->mmc->actual_clock = clock;
1480 + if ((clock > input_clock) && (clock > host->max_overclock)) {
1481 + pr_warn("%s: Overclocking to %dHz\n",
1482 + mmc_hostname(host->mmc), clock);
1483 + host->max_overclock = clock;
1486 + clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1487 + clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1488 + << SDHCI_DIVIDER_HI_SHIFT;
1489 + clk |= SDHCI_CLOCK_INT_EN;
1490 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1492 + /* Wait max 20 ms */
1494 + while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
1495 + & SDHCI_CLOCK_INT_STABLE)) {
1496 + if (timeout == 0) {
1497 + pr_err("%s: Internal clock never "
1498 + "stabilised.\n", mmc_hostname(host->mmc));
1499 + bcm2835_mmc_dumpregs(host);
1506 + if (20-timeout > 10 && 20-timeout > host->max_delay) {
1507 + host->max_delay = 20-timeout;
1508 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
1511 + clk |= SDHCI_CLOCK_CARD_EN;
1512 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1515 +static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
1517 + struct bcm2835_host *host;
1518 + unsigned long flags;
1520 + host = mmc_priv(mmc);
1522 + spin_lock_irqsave(&host->lock, flags);
1524 + WARN_ON(host->mrq != NULL);
1528 + if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1529 + bcm2835_mmc_send_command(host, mrq->sbc);
1531 + bcm2835_mmc_send_command(host, mrq->cmd);
1533 + spin_unlock_irqrestore(&host->lock, flags);
1535 + if (!(mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23)) && mrq->cmd->data && host->use_dma) {
1536 + /* DMA transfer starts now, PIO starts after interrupt */
1537 + bcm2835_mmc_transfer_dma(host);
1542 +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1545 + struct bcm2835_host *host = mmc_priv(mmc);
1546 + unsigned long flags;
1550 + pr_debug("bcm2835_mmc_set_ios: clock %d, pwr %d, bus_width %d, timing %d, vdd %d, drv_type %d\n",
1551 + ios->clock, ios->power_mode, ios->bus_width,
1552 + ios->timing, ios->signal_voltage, ios->drv_type);
1554 + spin_lock_irqsave(&host->lock, flags);
1556 + if (!ios->clock || ios->clock != host->clock) {
1557 + bcm2835_mmc_set_clock(host, ios->clock);
1558 + host->clock = ios->clock;
1561 + if (host->pwr != SDHCI_POWER_330) {
1562 + host->pwr = SDHCI_POWER_330;
1563 + bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1566 + ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
1568 + /* set bus width */
1569 + ctrl &= ~SDHCI_CTRL_8BITBUS;
1570 + if (ios->bus_width == MMC_BUS_WIDTH_4)
1571 + ctrl |= SDHCI_CTRL_4BITBUS;
1573 + ctrl &= ~SDHCI_CTRL_4BITBUS;
1575 + ctrl &= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
1578 + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1580 + * We only need to set Driver Strength if the
1581 + * preset value enable is not set.
1583 + ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
1584 + ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1585 + if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1586 + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1587 + else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1588 + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1590 + bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1592 + /* Reset SD Clock Enable */
1593 + clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
1594 + clk &= ~SDHCI_CLOCK_CARD_EN;
1595 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1597 + /* Re-enable SD Clock */
1598 + bcm2835_mmc_set_clock(host, host->clock);
1599 + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1601 + spin_unlock_irqrestore(&host->lock, flags);
1605 +static struct mmc_host_ops bcm2835_ops = {
1606 + .request = bcm2835_mmc_request,
1607 + .set_ios = bcm2835_mmc_set_ios,
1608 + .enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
1609 + .ack_sdio_irq = bcm2835_mmc_ack_sdio_irq,
1613 +static void bcm2835_mmc_tasklet_finish(unsigned long param)
1615 + struct bcm2835_host *host;
1616 + unsigned long flags;
1617 + struct mmc_request *mrq;
1619 + host = (struct bcm2835_host *)param;
1621 + spin_lock_irqsave(&host->lock, flags);
1624 + * If this tasklet gets rescheduled while running, it will
1625 + * be run again afterwards but without any active request.
1628 + spin_unlock_irqrestore(&host->lock, flags);
1632 + del_timer(&host->timer);
1637 + * The controller needs a reset of internal state machines
1638 + * upon error conditions.
1640 + if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1641 + ((mrq->cmd && mrq->cmd->error) ||
1642 + (mrq->data && (mrq->data->error ||
1643 + (mrq->data->stop && mrq->data->stop->error))))) {
1645 + spin_unlock_irqrestore(&host->lock, flags);
1646 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
1647 + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
1648 + spin_lock_irqsave(&host->lock, flags);
1653 + host->data = NULL;
1655 + spin_unlock_irqrestore(&host->lock, flags);
1656 + mmc_request_done(host->mmc, mrq);
1661 +static int bcm2835_mmc_add_host(struct bcm2835_host *host)
1663 + struct mmc_host *mmc = host->mmc;
1664 + struct device *dev = mmc->parent;
1666 + struct dma_slave_config cfg;
1670 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
1672 + host->clk_mul = 0;
1674 + if (!mmc->f_max || mmc->f_max > host->max_clk)
1675 + mmc->f_max = host->max_clk;
1676 + mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
1678 + /* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
1679 + host->timeout_clk = mmc->f_max / 1000;
1680 + mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
1682 + /* host controller capabilities */
1683 + mmc->caps |= MMC_CAP_CMD23 | MMC_CAP_ERASE | MMC_CAP_NEEDS_POLL |
1684 + MMC_CAP_SDIO_IRQ | MMC_CAP_SD_HIGHSPEED |
1685 + MMC_CAP_MMC_HIGHSPEED;
1687 + mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
1689 + host->flags = SDHCI_AUTO_CMD23;
1691 + dev_info(dev, "mmc_debug:%x mmc_debug2:%x\n", mmc_debug, mmc_debug2);
1693 + dev_info(dev, "Forcing PIO mode\n");
1694 + host->have_dma = false;
1696 + if (IS_ERR_OR_NULL(host->dma_chan_rxtx)) {
1697 + dev_err(dev, "%s: Unable to initialise DMA channel. Falling back to PIO\n",
1699 + host->have_dma = false;
1701 + dev_info(dev, "DMA channel allocated");
1703 + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1704 + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1705 + cfg.slave_id = 11; /* DREQ channel */
1707 + /* Validate the slave configurations */
1709 + cfg.direction = DMA_MEM_TO_DEV;
1711 + cfg.dst_addr = host->bus_addr + SDHCI_BUFFER;
1713 + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
1716 + host->dma_cfg_tx = cfg;
1718 + cfg.direction = DMA_DEV_TO_MEM;
1719 + cfg.src_addr = host->bus_addr + SDHCI_BUFFER;
1722 + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
1726 + host->dma_cfg_rx = cfg;
1728 + host->have_dma = true;
1730 + pr_err("%s: unable to configure DMA channel. "
1731 + "Falling back to PIO\n",
1732 + mmc_hostname(mmc));
1733 + dma_release_channel(host->dma_chan_rxtx);
1734 + host->dma_chan_rxtx = NULL;
1735 + host->have_dma = false;
1739 + mmc->max_segs = 128;
1740 + if (swiotlb_max_segment())
1741 + mmc->max_req_size = (1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE;
1743 + mmc->max_req_size = 524288;
1744 + mmc->max_seg_size = mmc->max_req_size;
1745 + mmc->max_blk_size = 512;
1746 + mmc->max_blk_count = 65535;
1748 + /* report supported voltage ranges */
1749 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1751 + tasklet_init(&host->finish_tasklet,
1752 + bcm2835_mmc_tasklet_finish, (unsigned long)host);
1754 + timer_setup(&host->timer, bcm2835_mmc_timeout_timer, 0);
1755 + init_waitqueue_head(&host->buf_ready_int);
1757 + bcm2835_mmc_init(host, 0);
1758 + ret = request_irq(host->irq, bcm2835_mmc_irq, IRQF_SHARED,
1759 + mmc_hostname(mmc), host);
1761 + dev_err(dev, "Failed to request IRQ %d: %d\n", host->irq, ret);
1765 + ret = mmc_add_host(mmc);
1767 + dev_err(dev, "could not add MMC host\n");
1774 + free_irq(host->irq, host);
1776 + tasklet_kill(&host->finish_tasklet);
1781 +static int bcm2835_mmc_probe(struct platform_device *pdev)
1783 + struct device *dev = &pdev->dev;
1784 + struct device_node *node = dev->of_node;
1786 + struct resource *iomem;
1787 + struct bcm2835_host *host;
1788 + struct mmc_host *mmc;
1789 + const __be32 *addr;
1792 + mmc = mmc_alloc_host(sizeof(*host), dev);
1796 + mmc->ops = &bcm2835_ops;
1797 + host = mmc_priv(mmc);
1799 + host->timeout = msecs_to_jiffies(1000);
1800 + spin_lock_init(&host->lock);
1802 + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1803 + host->ioaddr = devm_ioremap_resource(dev, iomem);
1804 + if (IS_ERR(host->ioaddr)) {
1805 + ret = PTR_ERR(host->ioaddr);
1809 + addr = of_get_address(node, 0, NULL, NULL);
1811 + dev_err(dev, "could not get DMA-register address\n");
1815 + host->bus_addr = be32_to_cpup(addr);
1816 + pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
1817 + (unsigned long)host->ioaddr,
1818 + (unsigned long)iomem->start,
1819 + (unsigned long)host->bus_addr);
1823 + host->dma_chan_rxtx = dma_request_slave_channel(dev, "rx-tx");
1824 + if (!host->dma_chan_rxtx)
1825 + host->dma_chan_rxtx =
1826 + dma_request_slave_channel(dev, "tx");
1827 + if (!host->dma_chan_rxtx)
1828 + host->dma_chan_rxtx =
1829 + dma_request_slave_channel(dev, "rx");
1831 + dma_cap_mask_t mask;
1833 + dma_cap_zero(mask);
1834 + /* we don't care about the channel, any would work */
1835 + dma_cap_set(DMA_SLAVE, mask);
1836 + host->dma_chan_rxtx = dma_request_channel(mask, NULL, NULL);
1839 + clk = devm_clk_get(dev, NULL);
1840 + if (IS_ERR(clk)) {
1841 + ret = PTR_ERR(clk);
1842 + if (ret == -EPROBE_DEFER)
1843 + dev_info(dev, "could not get clk, deferring probe\n");
1845 + dev_err(dev, "could not get clk\n");
1849 + host->max_clk = clk_get_rate(clk);
1851 + host->irq = platform_get_irq(pdev, 0);
1852 + if (host->irq <= 0) {
1853 + dev_err(dev, "get IRQ failed\n");
1859 + mmc_of_parse(mmc);
1861 + /* Read any custom properties */
1862 + of_property_read_u32(node,
1863 + "brcm,overclock-50",
1864 + &host->overclock_50);
1866 + mmc->caps |= MMC_CAP_4_BIT_DATA;
1869 + ret = bcm2835_mmc_add_host(host);
1873 + platform_set_drvdata(pdev, host);
1877 + if (host->dma_chan_rxtx)
1878 + dma_release_channel(host->dma_chan_rxtx);
1879 + mmc_free_host(mmc);
1884 +static int bcm2835_mmc_remove(struct platform_device *pdev)
1886 + struct bcm2835_host *host = platform_get_drvdata(pdev);
1887 + unsigned long flags;
1892 + scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1893 + if (scratch == (u32)-1)
1898 + spin_lock_irqsave(&host->lock, flags);
1900 + host->flags |= SDHCI_DEVICE_DEAD;
1903 + pr_err("%s: Controller removed during "
1904 + " transfer!\n", mmc_hostname(host->mmc));
1906 + host->mrq->cmd->error = -ENOMEDIUM;
1907 + tasklet_schedule(&host->finish_tasklet);
1910 + spin_unlock_irqrestore(&host->lock, flags);
1913 + mmc_remove_host(host->mmc);
1916 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
1918 + free_irq(host->irq, host);
1920 + del_timer_sync(&host->timer);
1922 + tasklet_kill(&host->finish_tasklet);
1924 + if (host->dma_chan_rxtx)
1925 + dma_release_channel(host->dma_chan_rxtx);
1927 + mmc_free_host(host->mmc);
1933 +static const struct of_device_id bcm2835_mmc_match[] = {
1934 + { .compatible = "brcm,bcm2835-mmc" },
1937 +MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
1941 +static struct platform_driver bcm2835_mmc_driver = {
1942 + .probe = bcm2835_mmc_probe,
1943 + .remove = bcm2835_mmc_remove,
1945 + .name = DRIVER_NAME,
1946 + .owner = THIS_MODULE,
1947 + .of_match_table = bcm2835_mmc_match,
1950 +module_platform_driver(bcm2835_mmc_driver);
1952 +module_param(mmc_debug, uint, 0644);
1953 +module_param(mmc_debug2, uint, 0644);
1954 +MODULE_ALIAS("platform:mmc-bcm2835");
1955 +MODULE_DESCRIPTION("BCM2835 SDHCI driver");
1956 +MODULE_LICENSE("GPL v2");
1957 +MODULE_AUTHOR("Gellert Weisz");
1958 --- a/include/linux/mmc/card.h
1959 +++ b/include/linux/mmc/card.h
1960 @@ -270,6 +270,8 @@ struct mmc_card {
1961 #define MMC_QUIRK_TRIM_BROKEN (1<<12) /* Skip trim */
1962 #define MMC_QUIRK_BROKEN_HPI (1<<13) /* Disable broken HPI support */
1964 +#define MMC_QUIRK_ERASE_BROKEN (1<<31) /* Skip erase */
1966 bool reenable_cmdq; /* Re-enable Command Queue */
1968 unsigned int erase_size; /* erase size in sectors */