1 From 0464f674cf3e441b8651c4a5ea0131fa24a3adc4 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 008/121] 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 arch/arm/mach-bcm2708/bcm2708.c | 31 +
70 arch/arm/mach-bcm2709/bcm2709.c | 35 +-
71 drivers/mmc/core/quirks.c | 6 +
72 drivers/mmc/host/Kconfig | 29 +
73 drivers/mmc/host/Makefile | 1 +
74 drivers/mmc/host/bcm2835-mmc.c | 1558 +++++++++++++++++++++++++++++++++++++++
75 6 files changed, 1658 insertions(+), 2 deletions(-)
76 create mode 100644 drivers/mmc/host/bcm2835-mmc.c
78 --- a/arch/arm/mach-bcm2708/bcm2708.c
79 +++ b/arch/arm/mach-bcm2708/bcm2708.c
80 @@ -401,6 +401,34 @@ static struct platform_device bcm2708_gp
84 +#ifdef CONFIG_MMC_BCM2835 /* Arasan emmc SD (new) */
85 +static struct resource bcm2835_emmc_resources[] = {
88 + .end = EMMC_BASE + SZ_256 - 1, /* we only need this area */
89 + /* the memory map actually makes SZ_4K available */
90 + .flags = IORESOURCE_MEM,
93 + .start = IRQ_ARASANSDIO,
94 + .end = IRQ_ARASANSDIO,
95 + .flags = IORESOURCE_IRQ,
99 +static u64 bcm2835_emmc_dmamask = 0xffffffffUL;
101 +struct platform_device bcm2835_emmc_device = {
102 + .name = "mmc-bcm2835",
104 + .num_resources = ARRAY_SIZE(bcm2835_emmc_resources),
105 + .resource = bcm2835_emmc_resources,
107 + .dma_mask = &bcm2835_emmc_dmamask,
108 + .coherent_dma_mask = 0xffffffffUL},
110 +#endif /* CONFIG_MMC_BCM2835 */
112 int __init bcm_register_device(struct platform_device *pdev)
115 @@ -538,6 +566,9 @@ void __init bcm2708_init(void)
116 bcm_register_device_dt(&bcm2708_fb_device);
117 bcm_register_device_dt(&bcm2708_usb_device);
119 +#ifdef CONFIG_MMC_BCM2835
120 + bcm_register_device_dt(&bcm2835_emmc_device);
123 bcm2708_init_uart1();
125 --- a/arch/arm/mach-bcm2709/bcm2709.c
126 +++ b/arch/arm/mach-bcm2709/bcm2709.c
127 @@ -421,6 +421,34 @@ static struct platform_device bcm2708_gp
131 +#ifdef CONFIG_MMC_BCM2835 /* Arasan emmc SD (new) */
132 +static struct resource bcm2835_emmc_resources[] = {
134 + .start = EMMC_BASE,
135 + .end = EMMC_BASE + SZ_256 - 1, /* we only need this area */
136 + /* the memory map actually makes SZ_4K available */
137 + .flags = IORESOURCE_MEM,
140 + .start = IRQ_ARASANSDIO,
141 + .end = IRQ_ARASANSDIO,
142 + .flags = IORESOURCE_IRQ,
146 +static u64 bcm2835_emmc_dmamask = 0xffffffffUL;
148 +struct platform_device bcm2835_emmc_device = {
149 + .name = "mmc-bcm2835",
151 + .num_resources = ARRAY_SIZE(bcm2835_emmc_resources),
152 + .resource = bcm2835_emmc_resources,
154 + .dma_mask = &bcm2835_emmc_dmamask,
155 + .coherent_dma_mask = 0xffffffffUL},
157 +#endif /* CONFIG_MMC_BCM2835 */
159 int __init bcm_register_device(struct platform_device *pdev)
162 @@ -558,8 +586,11 @@ void __init bcm2709_init(void)
163 bcm_register_device_dt(&bcm2708_fb_device);
164 bcm_register_device_dt(&bcm2708_usb_device);
166 - bcm2708_init_led();
167 - bcm2708_init_uart1();
168 +#ifdef CONFIG_MMC_BCM2835
169 + bcm_register_device_dt(&bcm2835_emmc_device);
171 + bcm2709_init_led();
172 + bcm2709_init_uart1();
175 for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
176 --- a/drivers/mmc/core/quirks.c
177 +++ b/drivers/mmc/core/quirks.c
178 @@ -71,6 +71,7 @@ static const struct mmc_fixup mmc_fixup_
180 void mmc_fixup_device(struct mmc_card *card, const struct mmc_fixup *table)
182 + extern unsigned mmc_debug;
183 const struct mmc_fixup *f;
184 u64 rev = cid_rev_card(card);
186 @@ -95,5 +96,10 @@ void mmc_fixup_device(struct mmc_card *c
187 f->vendor_fixup(card, f->data);
190 + /* SDHCI on BCM2708 - bug causes a certain sequence of CMD23 operations to fail.
191 + * Disable this flag for all cards (fall-back to CMD25/CMD18 multi-block transfers).
193 + if (mmc_debug & (1<<13))
194 + card->quirks |= MMC_QUIRK_BLK_NO_CMD23;
196 EXPORT_SYMBOL(mmc_fixup_device);
197 --- a/drivers/mmc/host/Kconfig
198 +++ b/drivers/mmc/host/Kconfig
201 comment "MMC/SD/SDIO Host Controller Drivers"
204 + tristate "MMC support on BCM2835"
205 + depends on MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835
207 + This selects the MMC Interface on BCM2835.
209 + If you have a controller with this interface, say Y or M here.
213 +config MMC_BCM2835_DMA
214 + bool "DMA support on BCM2835 Arasan controller"
215 + depends on MMC_BCM2835
217 + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
222 +config MMC_BCM2835_PIO_DMA_BARRIER
223 + int "Block count limit for PIO transfers"
224 + depends on MMC_BCM2835 && MMC_BCM2835_DMA
228 + The inclusive limit in bytes under which PIO will be used instead of DMA
230 + If unsure, say 2 here.
233 tristate "ARM AMBA Multimedia Card Interface support"
235 --- a/drivers/mmc/host/Makefile
236 +++ b/drivers/mmc/host/Makefile
237 @@ -18,6 +18,7 @@ obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c
238 obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
239 obj-$(CONFIG_MMC_SDHCI_F_SDH30) += sdhci_f_sdh30.o
240 obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
241 +obj-$(CONFIG_MMC_BCM2835) += bcm2835-mmc.o
242 obj-$(CONFIG_MMC_WBSD) += wbsd.o
243 obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
244 obj-$(CONFIG_MMC_OMAP) += omap.o
246 +++ b/drivers/mmc/host/bcm2835-mmc.c
249 + * BCM2835 MMC host driver.
251 + * Author: Gellert Weisz <gellert@raspberrypi.org>
255 + * sdhci-bcm2708.c by Broadcom
256 + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
257 + * sdhci.c and sdhci-pci.c by Pierre Ossman
259 + * This program is free software; you can redistribute it and/or modify it
260 + * under the terms and conditions of the GNU General Public License,
261 + * version 2, as published by the Free Software Foundation.
263 + * This program is distributed in the hope it will be useful, but WITHOUT
264 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
265 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
268 + * You should have received a copy of the GNU General Public License
269 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
272 +#include <linux/delay.h>
273 +#include <linux/module.h>
274 +#include <linux/io.h>
275 +#include <linux/mmc/mmc.h>
276 +#include <linux/mmc/host.h>
277 +#include <linux/mmc/sd.h>
278 +#include <linux/scatterlist.h>
279 +#include <linux/of_address.h>
280 +#include <linux/of_irq.h>
281 +#include <linux/clk.h>
282 +#include <linux/platform_device.h>
283 +#include <linux/err.h>
284 +#include <linux/blkdev.h>
285 +#include <linux/dmaengine.h>
286 +#include <linux/dma-mapping.h>
287 +#include <linux/of_dma.h>
292 +#define DRIVER_NAME "mmc-bcm2835"
294 +#define DBG(f, x...) \
295 +pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
297 +#ifndef CONFIG_MMC_BCM2835_DMA
302 +/* the inclusive limit in bytes under which PIO will be used instead of DMA */
303 +#ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
304 +#define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
306 +#define PIO_DMA_BARRIER 00
309 +#define MIN_FREQ 400000
310 +#define TIMEOUT_VAL 0xE
311 +#define BCM2835_SDHCI_WRITE_DELAY(f) (((2 * 1000000) / f) + 1)
313 +#ifndef BCM2708_PERI_BASE
314 + #define BCM2708_PERI_BASE 0x20000000
317 +/* FIXME: Needs IOMMU support */
318 +#define BCM2835_VCMMU_SHIFT (0x7E000000 - BCM2708_PERI_BASE)
322 +unsigned mmc_debug2;
324 +struct bcm2835_host {
327 + void __iomem *ioaddr;
330 + struct mmc_host *mmc;
334 + int clock; /* Current clock speed */
335 + u8 pwr; /* Current voltage */
337 + unsigned int max_clk; /* Max possible freq */
338 + unsigned int timeout_clk; /* Timeout freq (KHz) */
339 + unsigned int clk_mul; /* Clock Muliplier value */
341 + struct tasklet_struct finish_tasklet; /* Tasklet structures */
343 + struct timer_list timer; /* Timer for timeouts */
345 + struct sg_mapping_iter sg_miter; /* SG state for PIO */
346 + unsigned int blocks; /* remaining PIO blocks */
348 + int irq; /* Device IRQ */
351 + u32 ier; /* cached registers */
353 + struct mmc_request *mrq; /* Current request */
354 + struct mmc_command *cmd; /* Current command */
355 + struct mmc_data *data; /* Current data request */
356 + unsigned int data_early:1; /* Data finished before cmd */
358 + wait_queue_head_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
365 + struct dma_chan *dma_chan_rx; /* DMA channel for reads */
366 + struct dma_chan *dma_chan_tx; /* DMA channel for writes */
367 + struct dma_async_tx_descriptor *tx_desc; /* descriptor */
371 + /*end of DMA part*/
373 + int max_delay; /* maximum length of time spent waiting */
375 + int flags; /* Host attributes */
376 +#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
377 +#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
378 +#define SDHCI_AUTO_CMD12 (1<<6) /* Auto CMD12 support */
379 +#define SDHCI_AUTO_CMD23 (1<<7) /* Auto CMD23 support */
380 +#define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
382 + u32 overclock_50; /* frequency to use when 50MHz is requested (in MHz) */
383 + u32 max_overclock; /* Highest reported */
387 +static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg, int from)
390 + lockdep_assert_held_once(&host->lock);
391 + writel(val, host->ioaddr + reg);
392 + udelay(BCM2835_SDHCI_WRITE_DELAY(max(host->clock, MIN_FREQ)));
394 + delay = ((mmc_debug >> 16) & 0xf) << ((mmc_debug >> 20) & 0xf);
395 + if (delay && !((1<<from) & mmc_debug2))
399 +static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
402 + lockdep_assert_held_once(&host->lock);
403 + writel(val, host->ioaddr + reg);
405 + delay = ((mmc_debug >> 24) & 0xf) << ((mmc_debug >> 28) & 0xf);
410 +static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
412 + lockdep_assert_held_once(&host->lock);
413 + return readl(host->ioaddr + reg);
416 +static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
418 + u32 oldval = (reg == SDHCI_COMMAND) ? host->shadow :
419 + bcm2835_mmc_readl(host, reg & ~3);
420 + u32 word_num = (reg >> 1) & 1;
421 + u32 word_shift = word_num * 16;
422 + u32 mask = 0xffff << word_shift;
423 + u32 newval = (oldval & ~mask) | (val << word_shift);
425 + if (reg == SDHCI_TRANSFER_MODE)
426 + host->shadow = newval;
428 + bcm2835_mmc_writel(host, newval, reg & ~3, 0);
432 +static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
434 + u32 oldval = bcm2835_mmc_readl(host, reg & ~3);
435 + u32 byte_num = reg & 3;
436 + u32 byte_shift = byte_num * 8;
437 + u32 mask = 0xff << byte_shift;
438 + u32 newval = (oldval & ~mask) | (val << byte_shift);
440 + bcm2835_mmc_writel(host, newval, reg & ~3, 1);
444 +static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
446 + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
447 + u32 word_num = (reg >> 1) & 1;
448 + u32 word_shift = word_num * 16;
449 + u32 word = (val >> word_shift) & 0xffff;
454 +static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
456 + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
457 + u32 byte_num = reg & 3;
458 + u32 byte_shift = byte_num * 8;
459 + u32 byte = (val >> byte_shift) & 0xff;
464 +static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
468 + ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
470 + /* change which requests generate IRQs - makes no difference to
471 + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
472 + bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE, 2);
476 +static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
478 + pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
479 + mmc_hostname(host->mmc));
481 + pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
482 + bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
483 + bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
484 + pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
485 + bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
486 + bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
487 + pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
488 + bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
489 + bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
490 + pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
491 + bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
492 + bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
493 + pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
494 + bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
495 + bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
496 + pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
497 + bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
498 + bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
499 + pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
500 + bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
501 + bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
502 + pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
503 + bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
504 + bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
505 + pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
506 + bcm2835_mmc_readw(host, SDHCI_ACMD12_ERR),
507 + bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
508 + pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
509 + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
510 + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
511 + pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
512 + bcm2835_mmc_readw(host, SDHCI_COMMAND),
513 + bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
514 + pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
515 + bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
517 + pr_debug(DRIVER_NAME ": ===========================================\n");
521 +static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
523 + unsigned long timeout;
524 + unsigned long flags;
526 + spin_lock_irqsave(&host->lock, flags);
527 + bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
529 + if (mask & SDHCI_RESET_ALL)
532 + /* Wait max 100 ms */
535 + /* hw clears the bit when it's done */
536 + while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
537 + if (timeout == 0) {
538 + pr_err("%s: Reset 0x%x never completed.\n",
539 + mmc_hostname(host->mmc), (int)mask);
540 + bcm2835_mmc_dumpregs(host);
544 + spin_unlock_irqrestore(&host->lock, flags);
546 + spin_lock_irqsave(&host->lock, flags);
549 + if (100-timeout > 10 && 100-timeout > host->max_delay) {
550 + host->max_delay = 100-timeout;
551 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
553 + spin_unlock_irqrestore(&host->lock, flags);
556 +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
558 +static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
560 + unsigned long flags;
562 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
564 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
566 + host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
567 + SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
568 + SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
569 + SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
570 + SDHCI_INT_RESPONSE;
572 + spin_lock_irqsave(&host->lock, flags);
573 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 3);
574 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 3);
575 + spin_unlock_irqrestore(&host->lock, flags);
578 + /* force clock reconfiguration */
580 + bcm2835_mmc_set_ios(host->mmc, &host->mmc->ios);
586 +static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
588 +static void bcm2835_mmc_dma_complete(void *param)
590 + struct bcm2835_host *host = param;
591 + struct dma_chan *dma_chan;
592 + unsigned long flags;
595 + spin_lock_irqsave(&host->lock, flags);
597 + if (host->data && !(host->data->flags & MMC_DATA_WRITE)) {
598 + /* otherwise handled in SDHCI IRQ */
599 + dma_chan = host->dma_chan_rx;
600 + dir_data = DMA_FROM_DEVICE;
602 + dma_unmap_sg(dma_chan->device->dev,
603 + host->data->sg, host->data->sg_len,
606 + bcm2835_mmc_finish_data(host);
609 + spin_unlock_irqrestore(&host->lock, flags);
612 +static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
614 + unsigned long flags;
615 + size_t blksize, len, chunk;
617 + u32 uninitialized_var(scratch);
620 + blksize = host->data->blksz;
623 + local_irq_save(flags);
626 + if (!sg_miter_next(&host->sg_miter))
629 + len = min(host->sg_miter.length, blksize);
632 + host->sg_miter.consumed = len;
634 + buf = host->sg_miter.addr;
638 + scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
642 + *buf = scratch & 0xFF;
651 + sg_miter_stop(&host->sg_miter);
653 + local_irq_restore(flags);
656 +static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
658 + unsigned long flags;
659 + size_t blksize, len, chunk;
663 + blksize = host->data->blksz;
668 + local_irq_save(flags);
671 + if (!sg_miter_next(&host->sg_miter))
674 + len = min(host->sg_miter.length, blksize);
677 + host->sg_miter.consumed = len;
679 + buf = host->sg_miter.addr;
682 + scratch |= (u32)*buf << (chunk * 8);
688 + if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
689 + mmc_raw_writel(host, scratch, SDHCI_BUFFER);
696 + sg_miter_stop(&host->sg_miter);
698 + local_irq_restore(flags);
702 +static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
706 + BUG_ON(!host->data);
708 + if (host->blocks == 0)
711 + if (host->data->flags & MMC_DATA_READ)
712 + mask = SDHCI_DATA_AVAILABLE;
714 + mask = SDHCI_SPACE_AVAILABLE;
716 + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
718 + if (host->data->flags & MMC_DATA_READ)
719 + bcm2835_bcm2835_mmc_read_block_pio(host);
721 + bcm2835_bcm2835_mmc_write_block_pio(host);
725 + /* QUIRK used in sdhci.c removes the 'if' */
726 + /* but it seems this is unnecessary */
727 + if (host->blocks == 0)
735 +static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
737 + u32 len, dir_data, dir_slave;
738 + struct dma_async_tx_descriptor *desc = NULL;
739 + struct dma_chan *dma_chan;
742 + WARN_ON(!host->data);
747 + if (host->blocks == 0)
750 + if (host->data->flags & MMC_DATA_READ) {
751 + dma_chan = host->dma_chan_rx;
752 + dir_data = DMA_FROM_DEVICE;
753 + dir_slave = DMA_DEV_TO_MEM;
755 + dma_chan = host->dma_chan_tx;
756 + dir_data = DMA_TO_DEVICE;
757 + dir_slave = DMA_MEM_TO_DEV;
760 + BUG_ON(!dma_chan->device);
761 + BUG_ON(!dma_chan->device->dev);
762 + BUG_ON(!host->data->sg);
764 + len = dma_map_sg(dma_chan->device->dev, host->data->sg,
765 + host->data->sg_len, dir_data);
767 + desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
769 + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
771 + dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
774 + unsigned long flags;
775 + spin_lock_irqsave(&host->lock, flags);
776 + bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
777 + SDHCI_INT_SPACE_AVAIL);
778 + host->tx_desc = desc;
779 + desc->callback = bcm2835_mmc_dma_complete;
780 + desc->callback_param = host;
781 + spin_unlock_irqrestore(&host->lock, flags);
782 + dmaengine_submit(desc);
783 + dma_async_issue_pending(dma_chan);
790 +static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
792 + u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
793 + u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
796 + host->ier = (host->ier & ~pio_irqs) | dma_irqs;
798 + host->ier = (host->ier & ~dma_irqs) | pio_irqs;
800 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 4);
801 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 4);
805 +static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
808 + struct mmc_data *data = cmd->data;
810 + WARN_ON(host->data);
812 + if (data || (cmd->flags & MMC_RSP_BUSY)) {
813 + count = TIMEOUT_VAL;
814 + bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
820 + /* Sanity checks */
821 + BUG_ON(data->blksz * data->blocks > 524288);
822 + BUG_ON(data->blksz > host->mmc->max_blk_size);
823 + BUG_ON(data->blocks > 65535);
826 + host->data_early = 0;
827 + host->data->bytes_xfered = 0;
830 + if (!(host->flags & SDHCI_REQ_USE_DMA)) {
833 + flags = SG_MITER_ATOMIC;
834 + if (host->data->flags & MMC_DATA_READ)
835 + flags |= SG_MITER_TO_SG;
837 + flags |= SG_MITER_FROM_SG;
838 + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
839 + host->blocks = data->blocks;
842 + host->use_dma = host->have_dma && data->blocks > PIO_DMA_BARRIER;
844 + bcm2835_mmc_set_transfer_irqs(host);
846 + /* Set the DMA boundary value and block size */
847 + bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
848 + data->blksz), SDHCI_BLOCK_SIZE);
849 + bcm2835_mmc_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
851 + BUG_ON(!host->data);
854 +static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
855 + struct mmc_command *cmd)
858 + struct mmc_data *data = cmd->data;
860 + if (data == NULL) {
861 + /* clear Auto CMD settings for no data CMDs */
862 + mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
863 + bcm2835_mmc_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
864 + SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
868 + WARN_ON(!host->data);
870 + mode = SDHCI_TRNS_BLK_CNT_EN;
872 + if ((mmc_op_multi(cmd->opcode) || data->blocks > 1)) {
873 + mode |= SDHCI_TRNS_MULTI;
876 + * If we are sending CMD23, CMD12 never gets sent
877 + * on successful completion (so no Auto-CMD12).
879 + if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
880 + mode |= SDHCI_TRNS_AUTO_CMD12;
881 + else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
882 + mode |= SDHCI_TRNS_AUTO_CMD23;
883 + bcm2835_mmc_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2, 5);
887 + if (data->flags & MMC_DATA_READ)
888 + mode |= SDHCI_TRNS_READ;
889 + if (host->flags & SDHCI_REQ_USE_DMA)
890 + mode |= SDHCI_TRNS_DMA;
892 + bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
895 +void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
899 + unsigned long timeout;
901 + WARN_ON(host->cmd);
903 + /* Wait max 10 ms */
906 + mask = SDHCI_CMD_INHIBIT;
907 + if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
908 + mask |= SDHCI_DATA_INHIBIT;
910 + /* We shouldn't wait for data inihibit for stop commands, even
911 + though they might use busy signaling */
912 + if (host->mrq->data && (cmd == host->mrq->data->stop))
913 + mask &= ~SDHCI_DATA_INHIBIT;
915 + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
916 + if (timeout == 0) {
917 + pr_err("%s: Controller never released inhibit bit(s).\n",
918 + mmc_hostname(host->mmc));
919 + bcm2835_mmc_dumpregs(host);
921 + tasklet_schedule(&host->finish_tasklet);
928 + if ((1000-timeout)/100 > 1 && (1000-timeout)/100 > host->max_delay) {
929 + host->max_delay = (1000-timeout)/100;
930 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
934 +#ifdef CONFIG_ARCH_BCM2835
935 + if (!cmd->data && cmd->busy_timeout > 9000)
936 + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
939 + timeout += 10 * HZ;
940 + mod_timer(&host->timer, timeout);
944 + bcm2835_mmc_prepare_data(host, cmd);
946 + bcm2835_mmc_writel(host, cmd->arg, SDHCI_ARGUMENT, 6);
948 + bcm2835_mmc_set_transfer_mode(host, cmd);
950 + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
951 + pr_err("%s: Unsupported response type!\n",
952 + mmc_hostname(host->mmc));
953 + cmd->error = -EINVAL;
954 + tasklet_schedule(&host->finish_tasklet);
958 + if (!(cmd->flags & MMC_RSP_PRESENT))
959 + flags = SDHCI_CMD_RESP_NONE;
960 + else if (cmd->flags & MMC_RSP_136)
961 + flags = SDHCI_CMD_RESP_LONG;
962 + else if (cmd->flags & MMC_RSP_BUSY)
963 + flags = SDHCI_CMD_RESP_SHORT_BUSY;
965 + flags = SDHCI_CMD_RESP_SHORT;
967 + if (cmd->flags & MMC_RSP_CRC)
968 + flags |= SDHCI_CMD_CRC;
969 + if (cmd->flags & MMC_RSP_OPCODE)
970 + flags |= SDHCI_CMD_INDEX;
973 + flags |= SDHCI_CMD_DATA;
975 + bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
979 +static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
981 + struct mmc_data *data;
983 + BUG_ON(!host->data);
989 + data->bytes_xfered = 0;
991 + data->bytes_xfered = data->blksz * data->blocks;
994 + * Need to send CMD12 if -
995 + * a) open-ended multiblock transfer (no CMD23)
996 + * b) error in multiblock transfer
1000 + !host->mrq->sbc)) {
1003 + * The controller needs a reset of internal state machines
1004 + * upon error conditions.
1006 + if (data->error) {
1007 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
1008 + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
1011 + bcm2835_mmc_send_command(host, data->stop);
1013 + tasklet_schedule(&host->finish_tasklet);
1016 +static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
1020 + BUG_ON(host->cmd == NULL);
1022 + if (host->cmd->flags & MMC_RSP_PRESENT) {
1023 + if (host->cmd->flags & MMC_RSP_136) {
1024 + /* CRC is stripped so we need to do some shifting. */
1025 + for (i = 0; i < 4; i++) {
1026 + host->cmd->resp[i] = bcm2835_mmc_readl(host,
1027 + SDHCI_RESPONSE + (3-i)*4) << 8;
1029 + host->cmd->resp[i] |=
1030 + bcm2835_mmc_readb(host,
1031 + SDHCI_RESPONSE + (3-i)*4-1);
1034 + host->cmd->resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
1038 + host->cmd->error = 0;
1040 + /* Finished CMD23, now send actual command. */
1041 + if (host->cmd == host->mrq->sbc) {
1043 + bcm2835_mmc_send_command(host, host->mrq->cmd);
1045 + if (host->mrq->cmd->data && host->use_dma) {
1046 + /* DMA transfer starts now, PIO starts after interrupt */
1047 + bcm2835_mmc_transfer_dma(host);
1051 + /* Processed actual command. */
1052 + if (host->data && host->data_early)
1053 + bcm2835_mmc_finish_data(host);
1055 + if (!host->cmd->data)
1056 + tasklet_schedule(&host->finish_tasklet);
1063 +static void bcm2835_mmc_timeout_timer(unsigned long data)
1065 + struct bcm2835_host *host;
1066 + unsigned long flags;
1068 + host = (struct bcm2835_host *)data;
1070 + spin_lock_irqsave(&host->lock, flags);
1073 + pr_err("%s: Timeout waiting for hardware interrupt.\n",
1074 + mmc_hostname(host->mmc));
1075 + bcm2835_mmc_dumpregs(host);
1078 + host->data->error = -ETIMEDOUT;
1079 + bcm2835_mmc_finish_data(host);
1082 + host->cmd->error = -ETIMEDOUT;
1084 + host->mrq->cmd->error = -ETIMEDOUT;
1086 + tasklet_schedule(&host->finish_tasklet);
1091 + spin_unlock_irqrestore(&host->lock, flags);
1095 +static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
1097 + if (!(host->flags & SDHCI_DEVICE_DEAD)) {
1099 + host->ier |= SDHCI_INT_CARD_INT;
1101 + host->ier &= ~SDHCI_INT_CARD_INT;
1103 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 7);
1104 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 7);
1109 +static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1111 + struct bcm2835_host *host = mmc_priv(mmc);
1112 + unsigned long flags;
1114 + spin_lock_irqsave(&host->lock, flags);
1116 + host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1118 + host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1120 + bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
1121 + spin_unlock_irqrestore(&host->lock, flags);
1124 +static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
1127 + BUG_ON(intmask == 0);
1130 + pr_err("%s: Got command interrupt 0x%08x even "
1131 + "though no command operation was in progress.\n",
1132 + mmc_hostname(host->mmc), (unsigned)intmask);
1133 + bcm2835_mmc_dumpregs(host);
1137 + if (intmask & SDHCI_INT_TIMEOUT)
1138 + host->cmd->error = -ETIMEDOUT;
1139 + else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1140 + SDHCI_INT_INDEX)) {
1141 + host->cmd->error = -EILSEQ;
1144 + if (host->cmd->error) {
1145 + tasklet_schedule(&host->finish_tasklet);
1149 + if (intmask & SDHCI_INT_RESPONSE)
1150 + bcm2835_mmc_finish_command(host);
1154 +static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
1156 + struct dma_chan *dma_chan;
1159 + BUG_ON(intmask == 0);
1161 + if (!host->data) {
1163 + * The "data complete" interrupt is also used to
1164 + * indicate that a busy state has ended. See comment
1165 + * above in sdhci_cmd_irq().
1167 + if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1168 + if (intmask & SDHCI_INT_DATA_END) {
1169 + bcm2835_mmc_finish_command(host);
1174 + pr_debug("%s: Got data interrupt 0x%08x even "
1175 + "though no data operation was in progress.\n",
1176 + mmc_hostname(host->mmc), (unsigned)intmask);
1177 + bcm2835_mmc_dumpregs(host);
1182 + if (intmask & SDHCI_INT_DATA_TIMEOUT)
1183 + host->data->error = -ETIMEDOUT;
1184 + else if (intmask & SDHCI_INT_DATA_END_BIT)
1185 + host->data->error = -EILSEQ;
1186 + else if ((intmask & SDHCI_INT_DATA_CRC) &&
1187 + SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
1188 + != MMC_BUS_TEST_R)
1189 + host->data->error = -EILSEQ;
1191 + if (host->use_dma) {
1192 + if (host->data->flags & MMC_DATA_WRITE) {
1193 + /* IRQ handled here */
1195 + dma_chan = host->dma_chan_tx;
1196 + dir_data = DMA_TO_DEVICE;
1197 + dma_unmap_sg(dma_chan->device->dev,
1198 + host->data->sg, host->data->sg_len,
1201 + bcm2835_mmc_finish_data(host);
1205 + if (host->data->error)
1206 + bcm2835_mmc_finish_data(host);
1208 + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
1209 + bcm2835_mmc_transfer_pio(host);
1211 + if (intmask & SDHCI_INT_DATA_END) {
1214 + * Data managed to finish before the
1215 + * command completed. Make sure we do
1216 + * things in the proper order.
1218 + host->data_early = 1;
1220 + bcm2835_mmc_finish_data(host);
1228 +static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
1230 + irqreturn_t result = IRQ_NONE;
1231 + struct bcm2835_host *host = dev_id;
1232 + u32 intmask, mask, unexpected = 0;
1233 + int max_loops = 16;
1234 +#ifndef CONFIG_ARCH_BCM2835
1238 + spin_lock(&host->lock);
1240 + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1242 + if (!intmask || intmask == 0xffffffff) {
1243 + result = IRQ_NONE;
1248 + /* Clear selected interrupts. */
1249 + mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
1250 + SDHCI_INT_BUS_POWER);
1251 + bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS, 8);
1254 + if (intmask & SDHCI_INT_CMD_MASK)
1255 + bcm2835_mmc_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
1257 + if (intmask & SDHCI_INT_DATA_MASK)
1258 + bcm2835_mmc_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
1260 + if (intmask & SDHCI_INT_BUS_POWER)
1261 + pr_err("%s: Card is consuming too much power!\n",
1262 + mmc_hostname(host->mmc));
1264 + if (intmask & SDHCI_INT_CARD_INT) {
1265 +#ifndef CONFIG_ARCH_BCM2835
1268 + bcm2835_mmc_enable_sdio_irq_nolock(host, false);
1269 + host->thread_isr |= SDHCI_INT_CARD_INT;
1270 + result = IRQ_WAKE_THREAD;
1274 + intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
1275 + SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
1276 + SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
1277 + SDHCI_INT_CARD_INT);
1280 + unexpected |= intmask;
1281 + bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS, 9);
1284 + if (result == IRQ_NONE)
1285 + result = IRQ_HANDLED;
1287 + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1288 + } while (intmask && --max_loops);
1290 + spin_unlock(&host->lock);
1293 + pr_err("%s: Unexpected interrupt 0x%08x.\n",
1294 + mmc_hostname(host->mmc), unexpected);
1295 + bcm2835_mmc_dumpregs(host);
1298 +#ifndef CONFIG_ARCH_BCM2835
1300 + mmc_signal_sdio_irq(host->mmc);
1306 +#ifdef CONFIG_ARCH_BCM2835
1307 +static irqreturn_t bcm2835_mmc_thread_irq(int irq, void *dev_id)
1309 + struct bcm2835_host *host = dev_id;
1310 + unsigned long flags;
1313 + spin_lock_irqsave(&host->lock, flags);
1314 + isr = host->thread_isr;
1315 + host->thread_isr = 0;
1316 + spin_unlock_irqrestore(&host->lock, flags);
1318 + if (isr & SDHCI_INT_CARD_INT) {
1319 + sdio_run_irqs(host->mmc);
1321 + spin_lock_irqsave(&host->lock, flags);
1322 + if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
1323 + bcm2835_mmc_enable_sdio_irq_nolock(host, true);
1324 + spin_unlock_irqrestore(&host->lock, flags);
1327 + return isr ? IRQ_HANDLED : IRQ_NONE;
1333 +void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
1335 + int div = 0; /* Initialized for compiler warning */
1336 + int real_div = div, clk_mul = 1;
1338 + unsigned long timeout;
1339 + unsigned int input_clock = clock;
1341 + if (host->overclock_50 && (clock == 50000000))
1342 + clock = host->overclock_50 * 1000000 + 999999;
1344 + host->mmc->actual_clock = 0;
1346 + bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
1351 + /* Version 3.00 divisors must be a multiple of 2. */
1352 + if (host->max_clk <= clock)
1355 + for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1357 + if ((host->max_clk / div) <= clock)
1366 + clock = (host->max_clk * clk_mul) / real_div;
1367 + host->mmc->actual_clock = clock;
1369 + if ((clock > input_clock) && (clock > host->max_overclock)) {
1370 + pr_warn("%s: Overclocking to %dHz\n",
1371 + mmc_hostname(host->mmc), clock);
1372 + host->max_overclock = clock;
1375 + clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1376 + clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1377 + << SDHCI_DIVIDER_HI_SHIFT;
1378 + clk |= SDHCI_CLOCK_INT_EN;
1379 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1381 + /* Wait max 20 ms */
1383 + while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
1384 + & SDHCI_CLOCK_INT_STABLE)) {
1385 + if (timeout == 0) {
1386 + pr_err("%s: Internal clock never "
1387 + "stabilised.\n", mmc_hostname(host->mmc));
1388 + bcm2835_mmc_dumpregs(host);
1395 + if (20-timeout > 10 && 20-timeout > host->max_delay) {
1396 + host->max_delay = 20-timeout;
1397 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
1400 + clk |= SDHCI_CLOCK_CARD_EN;
1401 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1404 +static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
1406 + struct bcm2835_host *host;
1407 + unsigned long flags;
1409 + host = mmc_priv(mmc);
1411 + spin_lock_irqsave(&host->lock, flags);
1413 + WARN_ON(host->mrq != NULL);
1417 + if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1418 + bcm2835_mmc_send_command(host, mrq->sbc);
1420 + bcm2835_mmc_send_command(host, mrq->cmd);
1423 + spin_unlock_irqrestore(&host->lock, flags);
1425 + if (!(mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23)) && mrq->cmd->data && host->use_dma) {
1426 + /* DMA transfer starts now, PIO starts after interrupt */
1427 + bcm2835_mmc_transfer_dma(host);
1432 +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1435 + struct bcm2835_host *host = mmc_priv(mmc);
1436 + unsigned long flags;
1440 + pr_debug("bcm2835_mmc_set_ios: clock %d, pwr %d, bus_width %d, timing %d, vdd %d, drv_type %d\n",
1441 + ios->clock, ios->power_mode, ios->bus_width,
1442 + ios->timing, ios->signal_voltage, ios->drv_type);
1444 + spin_lock_irqsave(&host->lock, flags);
1446 + if (!ios->clock || ios->clock != host->clock) {
1447 + bcm2835_mmc_set_clock(host, ios->clock);
1448 + host->clock = ios->clock;
1451 + if (host->pwr != SDHCI_POWER_330) {
1452 + host->pwr = SDHCI_POWER_330;
1453 + bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1456 + ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
1458 + /* set bus width */
1459 + ctrl &= ~SDHCI_CTRL_8BITBUS;
1460 + if (ios->bus_width == MMC_BUS_WIDTH_4)
1461 + ctrl |= SDHCI_CTRL_4BITBUS;
1463 + ctrl &= ~SDHCI_CTRL_4BITBUS;
1465 + ctrl &= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
1468 + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1470 + * We only need to set Driver Strength if the
1471 + * preset value enable is not set.
1473 + ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
1474 + ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1475 + if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1476 + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1477 + else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1478 + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1480 + bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1482 + /* Reset SD Clock Enable */
1483 + clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
1484 + clk &= ~SDHCI_CLOCK_CARD_EN;
1485 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1487 + /* Re-enable SD Clock */
1488 + bcm2835_mmc_set_clock(host, host->clock);
1489 + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1493 + spin_unlock_irqrestore(&host->lock, flags);
1497 +static struct mmc_host_ops bcm2835_ops = {
1498 + .request = bcm2835_mmc_request,
1499 + .set_ios = bcm2835_mmc_set_ios,
1500 + .enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
1504 +static void bcm2835_mmc_tasklet_finish(unsigned long param)
1506 + struct bcm2835_host *host;
1507 + unsigned long flags;
1508 + struct mmc_request *mrq;
1510 + host = (struct bcm2835_host *)param;
1512 + spin_lock_irqsave(&host->lock, flags);
1515 + * If this tasklet gets rescheduled while running, it will
1516 + * be run again afterwards but without any active request.
1519 + spin_unlock_irqrestore(&host->lock, flags);
1523 + del_timer(&host->timer);
1528 + * The controller needs a reset of internal state machines
1529 + * upon error conditions.
1531 + if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1532 + ((mrq->cmd && mrq->cmd->error) ||
1533 + (mrq->data && (mrq->data->error ||
1534 + (mrq->data->stop && mrq->data->stop->error))))) {
1536 + spin_unlock_irqrestore(&host->lock, flags);
1537 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
1538 + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
1539 + spin_lock_irqsave(&host->lock, flags);
1544 + host->data = NULL;
1548 + spin_unlock_irqrestore(&host->lock, flags);
1549 + mmc_request_done(host->mmc, mrq);
1554 +static int bcm2835_mmc_add_host(struct bcm2835_host *host)
1556 + struct mmc_host *mmc = host->mmc;
1557 + struct device *dev = mmc->parent;
1559 + struct dma_slave_config cfg;
1563 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
1565 + host->clk_mul = 0;
1567 + mmc->f_max = host->max_clk;
1568 + mmc->f_max = host->max_clk;
1569 + mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
1571 + /* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
1572 + host->timeout_clk = mmc->f_max / 1000;
1573 +#ifdef CONFIG_ARCH_BCM2835
1574 + mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
1576 + /* host controller capabilities */
1577 + mmc->caps = MMC_CAP_CMD23 | MMC_CAP_ERASE | MMC_CAP_NEEDS_POLL | MMC_CAP_SDIO_IRQ |
1578 + MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED | MMC_CAP_4_BIT_DATA;
1580 + host->flags = SDHCI_AUTO_CMD23;
1582 + dev_info(dev, "mmc_debug:%x mmc_debug2:%x\n", mmc_debug, mmc_debug2);
1584 + dev_info(dev, "Forcing PIO mode\n");
1585 + host->have_dma = false;
1587 + if (IS_ERR_OR_NULL(host->dma_chan_tx) ||
1588 + IS_ERR_OR_NULL(host->dma_chan_rx)) {
1589 + dev_err(dev, "%s: Unable to initialise DMA channels. Falling back to PIO\n",
1591 + host->have_dma = false;
1593 + dev_info(dev, "DMA channels allocated");
1594 + host->have_dma = true;
1596 + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1597 + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1598 + cfg.slave_id = 11; /* DREQ channel */
1600 + cfg.direction = DMA_MEM_TO_DEV;
1602 + cfg.dst_addr = host->phys_addr + SDHCI_BUFFER;
1603 + ret = dmaengine_slave_config(host->dma_chan_tx, &cfg);
1605 + cfg.direction = DMA_DEV_TO_MEM;
1606 + cfg.src_addr = host->phys_addr + SDHCI_BUFFER;
1608 + ret = dmaengine_slave_config(host->dma_chan_rx, &cfg);
1611 + mmc->max_segs = 128;
1612 + mmc->max_req_size = 524288;
1613 + mmc->max_seg_size = mmc->max_req_size;
1614 + mmc->max_blk_size = 512;
1615 + mmc->max_blk_count = 65535;
1617 + /* report supported voltage ranges */
1618 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1620 + tasklet_init(&host->finish_tasklet,
1621 + bcm2835_mmc_tasklet_finish, (unsigned long)host);
1623 + setup_timer(&host->timer, bcm2835_mmc_timeout_timer, (unsigned long)host);
1624 + init_waitqueue_head(&host->buf_ready_int);
1626 + bcm2835_mmc_init(host, 0);
1627 +#ifndef CONFIG_ARCH_BCM2835
1628 + ret = devm_request_irq(dev, host->irq, bcm2835_mmc_irq, 0,
1629 + mmc_hostname(mmc), host);
1631 + ret = devm_request_threaded_irq(dev, host->irq, bcm2835_mmc_irq,
1632 + bcm2835_mmc_thread_irq, IRQF_SHARED,
1633 + mmc_hostname(mmc), host);
1636 + dev_err(dev, "Failed to request IRQ %d: %d\n", host->irq, ret);
1641 + mmc_add_host(mmc);
1646 + tasklet_kill(&host->finish_tasklet);
1651 +static int bcm2835_mmc_probe(struct platform_device *pdev)
1653 + struct device *dev = &pdev->dev;
1654 + struct device_node *node = dev->of_node;
1656 + struct resource *iomem;
1657 + struct bcm2835_host *host;
1658 + struct mmc_host *mmc;
1661 + mmc = mmc_alloc_host(sizeof(*host), dev);
1665 + mmc->ops = &bcm2835_ops;
1666 + host = mmc_priv(mmc);
1668 + host->timeout = msecs_to_jiffies(1000);
1669 + spin_lock_init(&host->lock);
1671 + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1672 + host->ioaddr = devm_ioremap_resource(dev, iomem);
1673 + if (IS_ERR(host->ioaddr)) {
1674 + ret = PTR_ERR(host->ioaddr);
1678 + host->phys_addr = iomem->start + BCM2835_VCMMU_SHIFT;
1682 + host->dma_chan_tx = dma_request_slave_channel(dev, "tx");
1683 + host->dma_chan_rx = dma_request_slave_channel(dev, "rx");
1685 + dma_cap_mask_t mask;
1687 + dma_cap_zero(mask);
1688 + /* we don't care about the channel, any would work */
1689 + dma_cap_set(DMA_SLAVE, mask);
1690 + host->dma_chan_tx = dma_request_channel(mask, NULL, NULL);
1691 + host->dma_chan_rx = dma_request_channel(mask, NULL, NULL);
1694 + clk = devm_clk_get(dev, NULL);
1695 + if (IS_ERR(clk)) {
1696 + dev_err(dev, "could not get clk\n");
1697 + ret = PTR_ERR(clk);
1701 + host->max_clk = clk_get_rate(clk);
1703 + host->irq = platform_get_irq(pdev, 0);
1704 + if (host->irq <= 0) {
1705 + dev_err(dev, "get IRQ failed\n");
1711 + mmc_of_parse(mmc);
1713 + /* Read any custom properties */
1714 + of_property_read_u32(node,
1715 + "brcm,overclock-50",
1716 + &host->overclock_50);
1718 + mmc->caps |= MMC_CAP_4_BIT_DATA;
1721 + ret = bcm2835_mmc_add_host(host);
1725 + platform_set_drvdata(pdev, host);
1729 + mmc_free_host(mmc);
1734 +static int bcm2835_mmc_remove(struct platform_device *pdev)
1736 + struct bcm2835_host *host = platform_get_drvdata(pdev);
1737 + unsigned long flags;
1742 + scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1743 + if (scratch == (u32)-1)
1748 + spin_lock_irqsave(&host->lock, flags);
1750 + host->flags |= SDHCI_DEVICE_DEAD;
1753 + pr_err("%s: Controller removed during "
1754 + " transfer!\n", mmc_hostname(host->mmc));
1756 + host->mrq->cmd->error = -ENOMEDIUM;
1757 + tasklet_schedule(&host->finish_tasklet);
1760 + spin_unlock_irqrestore(&host->lock, flags);
1763 + mmc_remove_host(host->mmc);
1766 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
1768 + free_irq(host->irq, host);
1770 + del_timer_sync(&host->timer);
1772 + tasklet_kill(&host->finish_tasklet);
1774 + mmc_free_host(host->mmc);
1775 + platform_set_drvdata(pdev, NULL);
1781 +static const struct of_device_id bcm2835_mmc_match[] = {
1782 + { .compatible = "brcm,bcm2835-mmc" },
1785 +MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
1789 +static struct platform_driver bcm2835_mmc_driver = {
1790 + .probe = bcm2835_mmc_probe,
1791 + .remove = bcm2835_mmc_remove,
1793 + .name = DRIVER_NAME,
1794 + .owner = THIS_MODULE,
1795 + .of_match_table = bcm2835_mmc_match,
1798 +module_platform_driver(bcm2835_mmc_driver);
1800 +module_param(mmc_debug, uint, 0644);
1801 +module_param(mmc_debug2, uint, 0644);
1802 +MODULE_ALIAS("platform:mmc-bcm2835");
1803 +MODULE_DESCRIPTION("BCM2835 SDHCI driver");
1804 +MODULE_LICENSE("GPL v2");
1805 +MODULE_AUTHOR("Gellert Weisz");