1 diff -urwN linux-3.10/drivers/mmc/card/block.c linux-rpi-3.10.y/drivers/mmc/card/block.c
2 --- linux-3.10/drivers/mmc/card/block.c 2013-06-30 23:13:29.000000000 +0100
3 +++ linux-rpi-3.10.y/drivers/mmc/card/block.c 2013-07-06 15:25:50.000000000 +0100
8 - if (brq->data.blocks > 1 || do_rel_wr) {
9 + if (brq->data.blocks > 1 || do_rel_wr || card->host->caps2 & MMC_CAP2_FORCE_MULTIBLOCK) {
10 /* SPI multiblock writes terminate using a special
11 * token, not a STOP_TRANSMISSION request.
13 diff -urwN linux-3.10/drivers/mmc/core/sd.c linux-rpi-3.10.y/drivers/mmc/core/sd.c
14 --- linux-3.10/drivers/mmc/core/sd.c 2013-06-30 23:13:29.000000000 +0100
15 +++ linux-rpi-3.10.y/drivers/mmc/core/sd.c 2013-07-06 15:25:50.000000000 +0100
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/stat.h>
20 +#include <linux/jiffies.h>
21 +#include <linux/nmi.h>
23 #include <linux/mmc/host.h>
24 #include <linux/mmc/card.h>
30 +static const unsigned long retry_timeout_ms= 10*1000;
32 +// try at least 10 times, even if timeout is reached
33 +static const int retry_min_tries= 10;
35 +// delay between tries
36 +static const unsigned long retry_delay_ms= 10;
39 * Given the decoded CSD structure, decode the raw CID to our CID structure.
45 - * Fetch and process SD Status register.
46 + * Fetch and process SD Configuration Register.
48 +static int mmc_read_scr(struct mmc_card *card)
50 + unsigned long timeout_at;
53 + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
56 + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
58 + unsigned long delay_at;
61 + err = mmc_app_send_scr(card, card->raw_scr);
63 + break; // success!!!
65 + touch_nmi_watchdog(); // we are still alive!
68 + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
69 + while( time_before( jiffies, delay_at ) )
72 + touch_nmi_watchdog(); // we are still alive!
78 + pr_err("%s: failed to read SD Configuration register (SCR) after %d tries during %lu ms, error %d\n", mmc_hostname(card->host), tries, retry_timeout_ms, err );
84 + pr_info("%s: could read SD Configuration register (SCR) at the %dth attempt\n", mmc_hostname(card->host), tries );
87 + err = mmc_decode_scr(card);
95 + * Fetch and process SD Status Register.
97 static int mmc_read_ssr(struct mmc_card *card)
99 + unsigned long timeout_at;
100 unsigned int au, es, et, eo;
105 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
106 @@ -228,14 +289,40 @@
110 + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
113 + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
115 + unsigned long delay_at;
118 err = mmc_app_sd_status(card, ssr);
120 - pr_warning("%s: problem reading SD Status "
121 - "register.\n", mmc_hostname(card->host));
124 + break; // sucess!!!
126 + touch_nmi_watchdog(); // we are still alive!
129 + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
130 + while( time_before( jiffies, delay_at ) )
133 + touch_nmi_watchdog(); // we are still alive!
139 + pr_err("%s: failed to read SD Status register (SSR) after %d tries during %lu ms, error %d\n", mmc_hostname(card->host), tries, retry_timeout_ms, err );
145 + pr_info("%s: read SD Status register (SSR) after %d attempts\n", mmc_hostname(card->host), tries );
148 for (i = 0; i < 16; i++)
149 ssr[i] = be32_to_cpu(ssr[i]);
155 - * Fetch SCR from card.
156 + * Fetch and decode SD Configuration register.
158 - err = mmc_app_send_scr(card, card->raw_scr);
162 - err = mmc_decode_scr(card);
163 + err = mmc_read_scr(card);
167 diff -urwN linux-3.10/drivers/mmc/host/Kconfig linux-rpi-3.10.y/drivers/mmc/host/Kconfig
168 --- linux-3.10/drivers/mmc/host/Kconfig 2013-06-30 23:13:29.000000000 +0100
169 +++ linux-rpi-3.10.y/drivers/mmc/host/Kconfig 2013-07-06 15:25:50.000000000 +0100
174 +config MMC_SDHCI_BCM2708
175 + tristate "SDHCI support on BCM2708"
176 + depends on MMC_SDHCI && MACH_BCM2708
177 + select MMC_SDHCI_IO_ACCESSORS
179 + This selects the Secure Digital Host Controller Interface (SDHCI)
180 + often referrered to as the eMMC block.
182 + If you have a controller with this interface, say Y or M here.
186 +config MMC_SDHCI_BCM2708_DMA
187 + bool "DMA support on BCM2708 Arasan controller"
188 + depends on MMC_SDHCI_BCM2708
190 + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
195 config MMC_SDHCI_BCM2835
196 tristate "SDHCI platform support for the BCM2835 SD/MMC Controller"
197 depends on ARCH_BCM2835
198 diff -urwN linux-3.10/drivers/mmc/host/Makefile linux-rpi-3.10.y/drivers/mmc/host/Makefile
199 --- linux-3.10/drivers/mmc/host/Makefile 2013-06-30 23:13:29.000000000 +0100
200 +++ linux-rpi-3.10.y/drivers/mmc/host/Makefile 2013-07-06 15:25:50.000000000 +0100
202 obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c.o
203 obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
204 obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
205 +obj-$(CONFIG_MMC_SDHCI_BCM2708) += sdhci-bcm2708.o
206 obj-$(CONFIG_MMC_WBSD) += wbsd.o
207 obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
208 obj-$(CONFIG_MMC_OMAP) += omap.o
209 diff -urwN linux-3.10/drivers/mmc/host/sdhci-bcm2708.c linux-rpi-3.10.y/drivers/mmc/host/sdhci-bcm2708.c
210 --- linux-3.10/drivers/mmc/host/sdhci-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
211 +++ linux-rpi-3.10.y/drivers/mmc/host/sdhci-bcm2708.c 2013-07-06 15:25:50.000000000 +0100
214 + * sdhci-bcm2708.c Support for SDHCI device on BCM2708
215 + * Copyright (c) 2010 Broadcom
217 + * This program is free software; you can redistribute it and/or modify
218 + * it under the terms of the GNU General Public License version 2 as
219 + * published by the Free Software Foundation.
221 + * This program is distributed in the hope that it will be useful,
222 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
223 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
224 + * GNU General Public License for more details.
226 + * You should have received a copy of the GNU General Public License
227 + * along with this program; if not, write to the Free Software
228 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
232 + * SDHCI platform device - Arasan SD controller in BCM2708
234 + * Inspired by sdhci-pci.c, by Pierre Ossman
237 +#include <linux/delay.h>
238 +#include <linux/highmem.h>
239 +#include <linux/platform_device.h>
240 +#include <linux/module.h>
241 +#include <linux/mmc/mmc.h>
242 +#include <linux/mmc/host.h>
243 +#include <linux/mmc/sd.h>
245 +#include <linux/io.h>
246 +#include <linux/dma-mapping.h>
247 +#include <mach/dma.h>
251 +/*****************************************************************************\
255 +\*****************************************************************************/
257 +#define DRIVER_NAME "bcm2708_sdhci"
259 +/* for the time being insist on DMA mode - PIO seems not to work */
260 +#ifndef CONFIG_MMC_SDHCI_BCM2708_DMA
261 +#warning Non-DMA (PIO) version of this driver currently unavailable
263 +#undef CONFIG_MMC_SDHCI_BCM2708_DMA
264 +#define CONFIG_MMC_SDHCI_BCM2708_DMA y
266 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
267 +/* #define CHECK_DMA_USE */
269 +//#define LOG_REGISTERS
271 +#define USE_SCHED_TIME
272 +#define USE_SPACED_WRITES_2CLK 1 /* space consecutive register writes */
273 +#define USE_SOFTWARE_TIMEOUTS 1 /* not hardware timeouts */
274 +#define SOFTWARE_ERASE_TIMEOUT_SEC 30
276 +#define SDHCI_BCM_DMA_CHAN 4 /* this default is normally overriden */
277 +#define SDHCI_BCM_DMA_WAITS 0 /* delays slowing DMA transfers: 0-31 */
278 +/* We are worried that SD card DMA use may be blocking the AXI bus for others */
280 +/*! TODO: obtain these from the physical address */
281 +#define DMA_SDHCI_BASE 0x7e300000 /* EMMC register block on Videocore */
282 +#define DMA_SDHCI_BUFFER (DMA_SDHCI_BASE + SDHCI_BUFFER)
284 +#define BCM2708_SDHCI_SLEEP_TIMEOUT 1000 /* msecs */
286 +/* Mhz clock that the EMMC core is running at. Should match the platform clockman settings */
287 +#define BCM2708_EMMC_CLOCK_FREQ 50000000
289 +#define REG_EXRDFIFO_EN 0x80
290 +#define REG_EXRDFIFO_CFG 0x84
294 +/*****************************************************************************\
298 +\*****************************************************************************/
302 +#define DBG(f, x...) \
303 + pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
304 +// printk(KERN_INFO DRIVER_NAME " [%s()]: " f, __func__,## x)//GRAYG
307 +/*****************************************************************************\
309 + * High Precision Time *
311 +\*****************************************************************************/
313 +#ifdef USE_SCHED_TIME
315 +#include <mach/frc.h>
317 +typedef unsigned long hptime_t;
319 +#define FMT_HPT "lu"
321 +static inline hptime_t hptime(void)
323 + return frc_clock_ticks32();
326 +#define HPTIME_CLK_NS 1000ul
330 +typedef unsigned long hptime_t;
332 +#define FMT_HPT "lu"
334 +static inline hptime_t hptime(void)
339 +#define HPTIME_CLK_NS (1000000000ul/HZ)
343 +static inline unsigned long int since_ns(hptime_t t)
345 + return (unsigned long)((hptime() - t) * HPTIME_CLK_NS);
348 +static bool allow_highspeed = 1;
349 +static int emmc_clock_freq = BCM2708_EMMC_CLOCK_FREQ;
350 +static bool sync_after_dma = 1;
351 +static bool missing_status = 1;
352 +static bool spurious_crc_acmd51 = 0;
353 +bool enable_llm = 1;
354 +bool extra_messages = 0;
357 +static void hptime_test(void)
366 + printk(KERN_INFO DRIVER_NAME": 10ms = %"FMT_HPT" clks "
367 + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
368 + later-now, now, later,
369 + (unsigned long)(HPTIME_CLK_NS * (later - now)));
375 + printk(KERN_INFO DRIVER_NAME": 1s = %"FMT_HPT" clks "
376 + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
377 + later-now, now, later,
378 + (unsigned long)(HPTIME_CLK_NS * (later - now)));
382 +/*****************************************************************************\
384 + * SDHCI core callbacks *
386 +\*****************************************************************************/
389 +#ifdef CHECK_DMA_USE
390 +/*#define CHECK_DMA_REG_USE*/
393 +#ifdef CHECK_DMA_REG_USE
394 +/* we don't expect anything to be using these registers during a
395 + DMA (except the IRQ status) - so check */
396 +static void check_dma_reg_use(struct sdhci_host *host, int reg);
398 +#define check_dma_reg_use(host, reg)
402 +static inline u32 sdhci_bcm2708_raw_readl(struct sdhci_host *host, int reg)
404 + return readl(host->ioaddr + reg);
407 +u32 sdhci_bcm2708_readl(struct sdhci_host *host, int reg)
409 + u32 l = sdhci_bcm2708_raw_readl(host, reg);
411 +#ifdef LOG_REGISTERS
412 + printk(KERN_ERR "%s: readl from 0x%02x, value 0x%08x\n",
413 + mmc_hostname(host->mmc), reg, l);
415 + check_dma_reg_use(host, reg);
420 +u16 sdhci_bcm2708_readw(struct sdhci_host *host, int reg)
422 + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
423 + u32 w = l >> (reg << 3 & 0x18) & 0xffff;
425 +#ifdef LOG_REGISTERS
426 + printk(KERN_ERR "%s: readw from 0x%02x, value 0x%04x\n",
427 + mmc_hostname(host->mmc), reg, w);
429 + check_dma_reg_use(host, reg);
434 +u8 sdhci_bcm2708_readb(struct sdhci_host *host, int reg)
436 + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
437 + u32 b = l >> (reg << 3 & 0x18) & 0xff;
439 +#ifdef LOG_REGISTERS
440 + printk(KERN_ERR "%s: readb from 0x%02x, value 0x%02x\n",
441 + mmc_hostname(host->mmc), reg, b);
443 + check_dma_reg_use(host, reg);
449 +static void sdhci_bcm2708_raw_writel(struct sdhci_host *host, u32 val, int reg)
453 +#if USE_SPACED_WRITES_2CLK
454 + static bool timeout_disabled = false;
455 + unsigned int ns_2clk = 0;
457 + /* The Arasan has a bugette whereby it may lose the content of
458 + * successive writes to registers that are within two SD-card clock
459 + * cycles of each other (a clock domain crossing problem).
460 + * It seems, however, that the data register does not have this problem.
461 + * (Which is just as well - otherwise we'd have to nobble the DMA engine
464 + if (reg != SDHCI_BUFFER && host->clock != 0) {
465 + /* host->clock is the clock freq in Hz */
466 + static hptime_t last_write_hpt;
467 + hptime_t now = hptime();
468 + ns_2clk = cycle_delay*1000000/(host->clock/1000);
470 + if (now == last_write_hpt || now == last_write_hpt+1) {
471 + /* we can't guarantee any significant time has
472 + * passed - we'll have to wait anyway ! */
476 + /* we must have waited at least this many ns: */
477 + unsigned int ns_wait = HPTIME_CLK_NS *
478 + (last_write_hpt - now - 1);
479 + if (ns_wait < ns_2clk)
480 + ndelay(ns_2clk - ns_wait);
482 + last_write_hpt = now;
484 +#if USE_SOFTWARE_TIMEOUTS
485 + /* The Arasan is clocked for timeouts using the SD clock which is too
486 + * fast for ERASE commands and causes issues. So we disable timeouts
488 + if (host->cmd != NULL && host->cmd->opcode == MMC_ERASE &&
489 + reg == (SDHCI_COMMAND & ~3)) {
490 + mod_timer(&host->timer,
491 + jiffies + SOFTWARE_ERASE_TIMEOUT_SEC * HZ);
492 + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
493 + ier &= ~SDHCI_INT_DATA_TIMEOUT;
494 + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
495 + timeout_disabled = true;
497 + } else if (timeout_disabled) {
498 + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
499 + ier |= SDHCI_INT_DATA_TIMEOUT;
500 + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
501 + timeout_disabled = false;
505 + writel(val, host->ioaddr + reg);
507 + void __iomem * regaddr = host->ioaddr + reg;
509 + writel(val, regaddr);
511 + if (reg != SDHCI_BUFFER && reg != SDHCI_INT_STATUS && host->clock != 0)
513 + int timeout = 100000;
514 + while (val != readl(regaddr) && --timeout > 0)
518 + printk(KERN_ERR "%s: writing 0x%X to reg 0x%X "
519 + "always gives 0x%X\n",
520 + mmc_hostname(host->mmc),
521 + val, reg, readl(regaddr));
522 + BUG_ON(timeout <= 0);
528 +void sdhci_bcm2708_writel(struct sdhci_host *host, u32 val, int reg)
530 +#ifdef LOG_REGISTERS
531 + printk(KERN_ERR "%s: writel to 0x%02x, value 0x%08x\n",
532 + mmc_hostname(host->mmc), reg, val);
534 + check_dma_reg_use(host, reg);
536 + sdhci_bcm2708_raw_writel(host, val, reg);
539 +void sdhci_bcm2708_writew(struct sdhci_host *host, u16 val, int reg)
541 + static u32 shadow = 0;
543 + u32 p = reg == SDHCI_COMMAND ? shadow :
544 + sdhci_bcm2708_raw_readl(host, reg & ~3);
545 + u32 s = reg << 3 & 0x18;
547 + u32 m = 0xffff << s;
549 +#ifdef LOG_REGISTERS
550 + printk(KERN_ERR "%s: writew to 0x%02x, value 0x%04x\n",
551 + mmc_hostname(host->mmc), reg, val);
554 + if (reg == SDHCI_TRANSFER_MODE)
555 + shadow = (p & ~m) | l;
557 + check_dma_reg_use(host, reg);
558 + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
562 +void sdhci_bcm2708_writeb(struct sdhci_host *host, u8 val, int reg)
564 + u32 p = sdhci_bcm2708_raw_readl(host, reg & ~3);
565 + u32 s = reg << 3 & 0x18;
569 +#ifdef LOG_REGISTERS
570 + printk(KERN_ERR "%s: writeb to 0x%02x, value 0x%02x\n",
571 + mmc_hostname(host->mmc), reg, val);
574 + check_dma_reg_use(host, reg);
575 + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
578 +static unsigned int sdhci_bcm2708_get_max_clock(struct sdhci_host *host)
580 + return emmc_clock_freq;
583 +/*****************************************************************************\
587 +\*****************************************************************************/
589 +struct sdhci_bcm2708_priv {
592 + void __iomem *dma_chan_base;
593 + struct bcm2708_dma_cb *cb_base; /* DMA control blocks */
594 + dma_addr_t cb_handle;
595 + /* tracking scatter gather progress */
596 + unsigned sg_ix; /* scatter gather list index */
597 + unsigned sg_done; /* bytes in current sg_ix done */
598 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
599 + unsigned char dma_wanted; /* DMA transfer requested */
600 + unsigned char dma_waits; /* wait states in DMAs */
601 +#ifdef CHECK_DMA_USE
602 + unsigned char dmas_pending; /* no of unfinished DMAs */
603 + hptime_t when_started;
604 + hptime_t when_reset;
605 + hptime_t when_stopped;
608 + /* signalling the end of a transfer */
609 + void (*complete)(struct sdhci_host *);
612 +#define SDHCI_HOST_PRIV(host) \
613 + (struct sdhci_bcm2708_priv *)((struct sdhci_host *)(host)+1)
617 +#ifdef CHECK_DMA_REG_USE
618 +static void check_dma_reg_use(struct sdhci_host *host, int reg)
620 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
621 + if (host_priv->dma_wanted && reg != SDHCI_INT_STATUS) {
622 + printk(KERN_INFO"%s: accessing register 0x%x during DMA\n",
623 + mmc_hostname(host->mmc), reg);
630 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
632 +static void sdhci_clear_set_irqgen(struct sdhci_host *host, u32 clear, u32 set)
636 + ier = sdhci_bcm2708_raw_readl(host, SDHCI_SIGNAL_ENABLE);
639 + /* change which requests generate IRQs - makes no difference to
640 + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
641 + sdhci_bcm2708_raw_writel(host, ier, SDHCI_SIGNAL_ENABLE);
644 +static void sdhci_signal_irqs(struct sdhci_host *host, u32 irqs)
646 + sdhci_clear_set_irqgen(host, 0, irqs);
649 +static void sdhci_unsignal_irqs(struct sdhci_host *host, u32 irqs)
651 + sdhci_clear_set_irqgen(host, irqs, 0);
656 +static void schci_bcm2708_cb_read(struct sdhci_bcm2708_priv *host,
658 + dma_addr_t dma_addr, unsigned len,
659 + int /*bool*/ is_last)
661 + struct bcm2708_dma_cb *cb = &host->cb_base[ix];
662 + unsigned char dmawaits = host->dma_waits;
664 + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
665 + BCM2708_DMA_WAITS(dmawaits) |
666 + BCM2708_DMA_S_DREQ |
667 + BCM2708_DMA_D_WIDTH |
669 + cb->src = DMA_SDHCI_BUFFER; /* DATA register DMA address */
670 + cb->dst = dma_addr;
675 + cb->info |= BCM2708_DMA_INT_EN |
676 + BCM2708_DMA_WAIT_RESP;
679 + cb->next = host->cb_handle +
680 + (ix+1)*sizeof(struct bcm2708_dma_cb);
686 +static void schci_bcm2708_cb_write(struct sdhci_bcm2708_priv *host,
688 + dma_addr_t dma_addr, unsigned len,
689 + int /*bool*/ is_last)
691 + struct bcm2708_dma_cb *cb = &host->cb_base[ix];
692 + unsigned char dmawaits = host->dma_waits;
694 + /* We can make arbitrarily large writes as long as we specify DREQ to
695 + pace the delivery of bytes to the Arasan hardware */
696 + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
697 + BCM2708_DMA_WAITS(dmawaits) |
698 + BCM2708_DMA_D_DREQ |
699 + BCM2708_DMA_S_WIDTH |
701 + cb->src = dma_addr;
702 + cb->dst = DMA_SDHCI_BUFFER; /* DATA register DMA address */
707 + cb->info |= BCM2708_DMA_INT_EN |
708 + BCM2708_DMA_WAIT_RESP;
711 + cb->next = host->cb_handle +
712 + (ix+1)*sizeof(struct bcm2708_dma_cb);
719 +static void schci_bcm2708_dma_go(struct sdhci_host *host)
721 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
722 + void __iomem *dma_chan_base = host_priv->dma_chan_base;
724 + BUG_ON(host_priv->dma_wanted);
725 +#ifdef CHECK_DMA_USE
726 + if (host_priv->dma_wanted)
727 + printk(KERN_ERR "%s: DMA already in progress - "
728 + "now %"FMT_HPT", last started %lu "
729 + "reset %lu stopped %lu\n",
730 + mmc_hostname(host->mmc),
731 + hptime(), since_ns(host_priv->when_started),
732 + since_ns(host_priv->when_reset),
733 + since_ns(host_priv->when_stopped));
734 + else if (host_priv->dmas_pending > 0)
735 + printk(KERN_INFO "%s: note - new DMA when %d reset DMAs "
736 + "already in progress - "
737 + "now %"FMT_HPT", started %lu reset %lu stopped %lu\n",
738 + mmc_hostname(host->mmc),
739 + host_priv->dmas_pending,
740 + hptime(), since_ns(host_priv->when_started),
741 + since_ns(host_priv->when_reset),
742 + since_ns(host_priv->when_stopped));
743 + host_priv->dmas_pending += 1;
744 + host_priv->when_started = hptime();
746 + host_priv->dma_wanted = 1;
747 + DBG("PDMA go - base %p handle %08X\n", dma_chan_base,
748 + host_priv->cb_handle);
749 + bcm_dma_start(dma_chan_base, host_priv->cb_handle);
754 +sdhci_platdma_read(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
756 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
758 + DBG("PDMA to read %d bytes\n", len);
759 + host_priv->sg_done += len;
760 + schci_bcm2708_cb_read(host_priv, 0, dma_addr, len, 1/*TRUE*/);
761 + schci_bcm2708_dma_go(host);
766 +sdhci_platdma_write(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
768 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
770 + DBG("PDMA to write %d bytes\n", len);
771 + //BUG_ON(0 != (len & 0x1ff));
773 + host_priv->sg_done += len;
774 + schci_bcm2708_cb_write(host_priv, 0, dma_addr, len, 1/*TRUE*/);
775 + schci_bcm2708_dma_go(host);
778 +/*! space is avaiable to receive into or data is available to write
779 + Platform DMA exported function
782 +sdhci_bcm2708_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
783 + void(*completion_callback)(struct sdhci_host *host))
785 + struct mmc_data *data = host->data;
786 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
791 + BUG_ON(NULL == data);
792 + BUG_ON(0 == data->blksz);
794 + host_priv->complete = completion_callback;
796 + sg_ix = host_priv->sg_ix;
797 + BUG_ON(sg_ix >= data->sg_len);
799 + /* we can DMA blocks larger than blksz - it may hang the DMA
800 + channel but we are its only user */
801 + bytes = sg_dma_len(&data->sg[sg_ix]) - host_priv->sg_done;
802 + addr = sg_dma_address(&data->sg[sg_ix]) + host_priv->sg_done;
805 + /* We're going to poll for read/write available state until
809 + if (data->flags & MMC_DATA_READ) {
810 + if (*ref_intmask & SDHCI_INT_DATA_AVAIL) {
811 + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
812 + SDHCI_INT_SPACE_AVAIL);
813 + sdhci_platdma_read(host, addr, bytes);
816 + if (*ref_intmask & SDHCI_INT_SPACE_AVAIL) {
817 + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
818 + SDHCI_INT_SPACE_AVAIL);
819 + sdhci_platdma_write(host, addr, bytes);
824 + we have run out of bytes that need transferring (e.g. we may be in
825 + the middle of the last DMA transfer), or
826 + it is also possible that we've been called when another IRQ is
827 + signalled, even though we've turned off signalling of our own IRQ */
829 + *ref_intmask &= ~SDHCI_INT_DATA_END;
830 + /* don't let the main sdhci driver act on this .. we'll deal with it
831 + when we respond to the DMA - if one is currently in progress */
834 +/* is it possible to DMA the given mmc_data structure?
835 + Platform DMA exported function
838 +sdhci_bcm2708_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
840 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
841 + int ok = bcm_sg_suitable_for_dma(data->sg, data->sg_len);
844 + DBG("Reverting to PIO - bad cache alignment\n");
847 + host_priv->sg_ix = 0; /* first SG index */
848 + host_priv->sg_done = 0; /* no bytes done */
854 +#include <mach/arm_control.h> //GRAYG
855 +/*! the current SD transacton has been abandonned
856 + We need to tidy up if we were in the middle of a DMA
857 + Platform DMA exported function
860 +sdhci_bcm2708_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
862 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
863 +// unsigned long flags;
865 + BUG_ON(NULL == host);
867 +// spin_lock_irqsave(&host->lock, flags);
869 + if (host_priv->dma_wanted) {
870 + if (NULL == data) {
871 + printk(KERN_ERR "%s: ongoing DMA reset - no data!\n",
872 + mmc_hostname(host->mmc));
873 + BUG_ON(NULL == data);
875 + struct scatterlist *sg;
882 + sg_len = data->sg_len;
883 + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
885 + cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
887 + if (!(BCM2708_DMA_ACTIVE & cs))
889 + if (extra_messages)
890 + printk(KERN_INFO "%s: missed completion of "
891 + "cmd %d DMA (%d/%d [%d]/[%d]) - "
893 + mmc_hostname(host->mmc),
895 + host_priv->sg_done, sg_todo,
896 + host_priv->sg_ix+1, sg_len);
899 + printk(KERN_INFO "%s: resetting ongoing cmd %d"
900 + "DMA before %d/%d [%d]/[%d] complete\n",
901 + mmc_hostname(host->mmc),
903 + host_priv->sg_done, sg_todo,
904 + host_priv->sg_ix+1, sg_len);
905 +#ifdef CHECK_DMA_USE
906 + printk(KERN_INFO "%s: now %"FMT_HPT" started %lu "
907 + "last reset %lu last stopped %lu\n",
908 + mmc_hostname(host->mmc),
909 + hptime(), since_ns(host_priv->when_started),
910 + since_ns(host_priv->when_reset),
911 + since_ns(host_priv->when_stopped));
912 + { unsigned long info, debug;
913 + void __iomem *base;
914 + unsigned long pend0, pend1, pend2;
916 + base = host_priv->dma_chan_base;
917 + cs = readl(base + BCM2708_DMA_CS);
918 + info = readl(base + BCM2708_DMA_INFO);
919 + debug = readl(base + BCM2708_DMA_DEBUG);
920 + printk(KERN_INFO "%s: DMA%d CS=%08lX TI=%08lX "
922 + mmc_hostname(host->mmc),
923 + host_priv->dma_chan,
925 + pend0 = readl(__io_address(ARM_IRQ_PEND0));
926 + pend1 = readl(__io_address(ARM_IRQ_PEND1));
927 + pend2 = readl(__io_address(ARM_IRQ_PEND2));
929 + printk(KERN_INFO "%s: PEND0=%08lX "
930 + "PEND1=%08lX PEND2=%08lX\n",
931 + mmc_hostname(host->mmc),
932 + pend0, pend1, pend2);
934 + //gintsts = readl(__io_address(GINTSTS));
935 + //gintmsk = readl(__io_address(GINTMSK));
936 + //printk(KERN_INFO "%s: USB GINTSTS=%08lX"
937 + // "GINTMSK=%08lX\n",
938 + // mmc_hostname(host->mmc), gintsts, gintmsk);
941 + rc = bcm_dma_abort(host_priv->dma_chan_base);
944 + host_priv->dma_wanted = 0;
945 +#ifdef CHECK_DMA_USE
946 + host_priv->when_reset = hptime();
950 +// spin_unlock_irqrestore(&host->lock, flags);
954 +static void sdhci_bcm2708_dma_complete_irq(struct sdhci_host *host,
957 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
958 + struct mmc_data *data;
959 + struct scatterlist *sg;
963 +// unsigned long flags;
965 + BUG_ON(NULL == host);
967 +// spin_lock_irqsave(&host->lock, flags);
970 +#ifdef CHECK_DMA_USE
971 + if (host_priv->dmas_pending <= 0)
972 + DBG("on completion no DMA in progress - "
973 + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
974 + hptime(), since_ns(host_priv->when_started),
975 + since_ns(host_priv->when_reset),
976 + since_ns(host_priv->when_stopped));
977 + else if (host_priv->dmas_pending > 1)
978 + DBG("still %d DMA in progress after completion - "
979 + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
980 + host_priv->dmas_pending - 1,
981 + hptime(), since_ns(host_priv->when_started),
982 + since_ns(host_priv->when_reset),
983 + since_ns(host_priv->when_stopped));
984 + BUG_ON(host_priv->dmas_pending <= 0);
985 + host_priv->dmas_pending -= 1;
986 + host_priv->when_stopped = hptime();
988 + host_priv->dma_wanted = 0;
990 + if (NULL == data) {
991 + DBG("PDMA unused completion - status 0x%X\n", dma_cs);
992 +// spin_unlock_irqrestore(&host->lock, flags);
996 + sg_len = data->sg_len;
997 + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
999 + DBG("PDMA complete %d/%d [%d]/[%d]..\n",
1000 + host_priv->sg_done, sg_todo,
1001 + host_priv->sg_ix+1, sg_len);
1003 + BUG_ON(host_priv->sg_done > sg_todo);
1005 + if (host_priv->sg_done >= sg_todo) {
1006 + host_priv->sg_ix++;
1007 + host_priv->sg_done = 0;
1010 + sg_ix = host_priv->sg_ix;
1011 + if (sg_ix < sg_len) {
1013 + /* Set off next DMA if we've got the capacity */
1015 + if (data->flags & MMC_DATA_READ)
1016 + irq_mask = SDHCI_INT_DATA_AVAIL;
1018 + irq_mask = SDHCI_INT_SPACE_AVAIL;
1020 + /* We have to use the interrupt status register on the BCM2708
1021 + rather than the SDHCI_PRESENT_STATE register because latency
1022 + in the glue logic means that the information retrieved from
1023 + the latter is not always up-to-date w.r.t the DMA engine -
1024 + it may not indicate that a read or a write is ready yet */
1025 + if (sdhci_bcm2708_raw_readl(host, SDHCI_INT_STATUS) &
1027 + size_t bytes = sg_dma_len(&sg[sg_ix]) -
1028 + host_priv->sg_done;
1029 + dma_addr_t addr = sg_dma_address(&data->sg[sg_ix]) +
1030 + host_priv->sg_done;
1032 + /* acknowledge interrupt */
1033 + sdhci_bcm2708_raw_writel(host, irq_mask,
1034 + SDHCI_INT_STATUS);
1036 + BUG_ON(0 == bytes);
1038 + if (data->flags & MMC_DATA_READ)
1039 + sdhci_platdma_read(host, addr, bytes);
1041 + sdhci_platdma_write(host, addr, bytes);
1043 + DBG("PDMA - wait avail\n");
1044 + /* may generate an IRQ if already present */
1045 + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
1046 + SDHCI_INT_SPACE_AVAIL);
1049 + if (sync_after_dma) {
1050 + /* On the Arasan controller the stop command (which will be
1051 + scheduled after this completes) does not seem to work
1052 + properly if we allow it to be issued when we are
1053 + transferring data to/from the SD card.
1054 + We get CRC and DEND errors unless we wait for
1055 + the SD controller to finish reading/writing to the card. */
1057 + int timeout=30*5000;
1059 + DBG("PDMA over - sync card\n");
1060 + if (data->flags & MMC_DATA_READ)
1061 + state_mask = SDHCI_DOING_READ;
1063 + state_mask = SDHCI_DOING_WRITE;
1065 + while (0 != (sdhci_bcm2708_raw_readl(host, SDHCI_PRESENT_STATE)
1066 + & state_mask) && --timeout > 0)
1072 + printk(KERN_ERR"%s: final %s to SD card still "
1074 + mmc_hostname(host->mmc),
1075 + data->flags & MMC_DATA_READ? "read": "write");
1077 + if (host_priv->complete) {
1078 + (*host_priv->complete)(host);
1079 + DBG("PDMA %s complete\n",
1080 + data->flags & MMC_DATA_READ?"read":"write");
1081 + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
1082 + SDHCI_INT_SPACE_AVAIL);
1085 +// spin_unlock_irqrestore(&host->lock, flags);
1088 +static irqreturn_t sdhci_bcm2708_dma_irq(int irq, void *dev_id)
1090 + irqreturn_t result = IRQ_NONE;
1091 + struct sdhci_host *host = dev_id;
1092 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
1093 + u32 dma_cs; /* control and status register */
1095 + BUG_ON(NULL == dev_id);
1096 + BUG_ON(NULL == host_priv->dma_chan_base);
1098 + sdhci_spin_lock(host);
1100 + dma_cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
1102 + if (dma_cs & BCM2708_DMA_ERR) {
1103 + unsigned long debug;
1104 + debug = readl(host_priv->dma_chan_base +
1105 + BCM2708_DMA_DEBUG);
1106 + printk(KERN_ERR "%s: DMA error - CS %lX DEBUG %lX\n",
1107 + mmc_hostname(host->mmc), (unsigned long)dma_cs,
1108 + (unsigned long)debug);
1110 + writel(debug, host_priv->dma_chan_base +
1111 + BCM2708_DMA_DEBUG);
1113 + if (dma_cs & BCM2708_DMA_INT) {
1114 + /* acknowledge interrupt */
1115 + writel(BCM2708_DMA_INT,
1116 + host_priv->dma_chan_base + BCM2708_DMA_CS);
1118 + dsb(); /* ARM data synchronization (push) operation */
1120 + if (!host_priv->dma_wanted) {
1121 + /* ignore this interrupt - it was reset */
1122 + if (extra_messages)
1123 + printk(KERN_INFO "%s: DMA IRQ %X ignored - "
1124 + "results were reset\n",
1125 + mmc_hostname(host->mmc), dma_cs);
1126 +#ifdef CHECK_DMA_USE
1127 + printk(KERN_INFO "%s: now %"FMT_HPT
1128 + " started %lu reset %lu stopped %lu\n",
1129 + mmc_hostname(host->mmc), hptime(),
1130 + since_ns(host_priv->when_started),
1131 + since_ns(host_priv->when_reset),
1132 + since_ns(host_priv->when_stopped));
1133 + host_priv->dmas_pending--;
1136 + sdhci_bcm2708_dma_complete_irq(host, dma_cs);
1138 + result = IRQ_HANDLED;
1140 + sdhci_spin_unlock(host);
1144 +#endif /* CONFIG_MMC_SDHCI_BCM2708_DMA */
1147 +/***************************************************************************** \
1149 + * Device Attributes *
1151 +\*****************************************************************************/
1155 + * Show the DMA-using status
1157 +static ssize_t attr_dma_show(struct device *_dev,
1158 + struct device_attribute *attr, char *buf)
1160 + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
1163 + int use_dma = (host->flags & SDHCI_USE_PLATDMA? 1:0);
1164 + return sprintf(buf, "%d\n", use_dma);
1170 + * Set the DMA-using status
1172 +static ssize_t attr_dma_store(struct device *_dev,
1173 + struct device_attribute *attr,
1174 + const char *buf, size_t count)
1176 + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
1179 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
1180 + int on = simple_strtol(buf, NULL, 0);
1182 + host->flags |= SDHCI_USE_PLATDMA;
1183 + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
1184 + printk(KERN_INFO "%s: DMA enabled\n",
1185 + mmc_hostname(host->mmc));
1187 + host->flags &= ~(SDHCI_USE_PLATDMA | SDHCI_REQ_USE_DMA);
1188 + sdhci_bcm2708_writel(host, 0, REG_EXRDFIFO_EN);
1189 + printk(KERN_INFO "%s: DMA disabled\n",
1190 + mmc_hostname(host->mmc));
1198 +static DEVICE_ATTR(use_dma, S_IRUGO | S_IWUGO, attr_dma_show, attr_dma_store);
1202 + * Show the DMA wait states used
1204 +static ssize_t attr_dmawait_show(struct device *_dev,
1205 + struct device_attribute *attr, char *buf)
1207 + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
1210 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
1211 + int dmawait = host_priv->dma_waits;
1212 + return sprintf(buf, "%d\n", dmawait);
1218 + * Set the DMA wait state used
1220 +static ssize_t attr_dmawait_store(struct device *_dev,
1221 + struct device_attribute *attr,
1222 + const char *buf, size_t count)
1224 + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
1227 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
1228 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
1229 + int dma_waits = simple_strtol(buf, NULL, 0);
1230 + if (dma_waits >= 0 && dma_waits < 32)
1231 + host_priv->dma_waits = dma_waits;
1233 + printk(KERN_ERR "%s: illegal dma_waits value - %d",
1234 + mmc_hostname(host->mmc), dma_waits);
1241 +static DEVICE_ATTR(dma_wait, S_IRUGO | S_IWUGO,
1242 + attr_dmawait_show, attr_dmawait_store);
1246 + * Show the DMA-using status
1248 +static ssize_t attr_status_show(struct device *_dev,
1249 + struct device_attribute *attr, char *buf)
1251 + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
1254 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
1255 + return sprintf(buf,
1259 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
1260 + "dma: %s (%d waits)\n",
1262 + "dma: unconfigured\n",
1266 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
1267 + , (host->flags & SDHCI_USE_PLATDMA)? "on": "off"
1268 + , host_priv->dma_waits
1275 +static DEVICE_ATTR(status, S_IRUGO, attr_status_show, NULL);
1277 +/***************************************************************************** \
1279 + * Power Management *
1281 +\*****************************************************************************/
1285 +static int sdhci_bcm2708_suspend(struct platform_device *dev, pm_message_t state)
1287 + struct sdhci_host *host = (struct sdhci_host *)
1288 + platform_get_drvdata(dev);
1292 + ret = mmc_suspend_host(host->mmc);
1298 +static int sdhci_bcm2708_resume(struct platform_device *dev)
1300 + struct sdhci_host *host = (struct sdhci_host *)
1301 + platform_get_drvdata(dev);
1305 + ret = mmc_resume_host(host->mmc);
1313 +/*****************************************************************************\
1315 + * Device quirk functions. Implemented as local ops because the flags *
1316 + * field is out of space with newer kernels. This implementation can be *
1317 + * back ported to older kernels as well. *
1318 +\****************************************************************************/
1319 +static unsigned int sdhci_bcm2708_quirk_extra_ints(struct sdhci_host *host)
1324 +static unsigned int sdhci_bcm2708_quirk_spurious_crc_acmd51(struct sdhci_host *host)
1329 +static unsigned int sdhci_bcm2708_quirk_voltage_broken(struct sdhci_host *host)
1334 +static unsigned int sdhci_bcm2708_uhs_broken(struct sdhci_host *host)
1339 +static unsigned int sdhci_bcm2708_missing_status(struct sdhci_host *host)
1344 +/***************************************************************************** \
1348 +\*****************************************************************************/
1350 +static struct sdhci_ops sdhci_bcm2708_ops = {
1351 +#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
1352 + .read_l = sdhci_bcm2708_readl,
1353 + .read_w = sdhci_bcm2708_readw,
1354 + .read_b = sdhci_bcm2708_readb,
1355 + .write_l = sdhci_bcm2708_writel,
1356 + .write_w = sdhci_bcm2708_writew,
1357 + .write_b = sdhci_bcm2708_writeb,
1359 +#error The BCM2708 SDHCI driver needs CONFIG_MMC_SDHCI_IO_ACCESSORS to be set
1361 + .get_max_clock = sdhci_bcm2708_get_max_clock,
1363 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
1364 + // Platform DMA operations
1365 + .pdma_able = sdhci_bcm2708_platdma_dmaable,
1366 + .pdma_avail = sdhci_bcm2708_platdma_avail,
1367 + .pdma_reset = sdhci_bcm2708_platdma_reset,
1369 + .extra_ints = sdhci_bcm2708_quirk_extra_ints,
1370 + .voltage_broken = sdhci_bcm2708_quirk_voltage_broken,
1371 + .uhs_broken = sdhci_bcm2708_uhs_broken,
1374 +/*****************************************************************************\
1376 + * Device probing/removal *
1378 +\*****************************************************************************/
1380 +static int sdhci_bcm2708_probe(struct platform_device *pdev)
1382 + struct sdhci_host *host;
1383 + struct resource *iomem;
1384 + struct sdhci_bcm2708_priv *host_priv;
1387 + BUG_ON(pdev == NULL);
1389 + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1395 + if (resource_size(iomem) != 0x100)
1396 + dev_err(&pdev->dev, "Invalid iomem size. You may "
1397 + "experience problems.\n");
1399 + if (pdev->dev.parent)
1400 + host = sdhci_alloc_host(pdev->dev.parent,
1401 + sizeof(struct sdhci_bcm2708_priv));
1403 + host = sdhci_alloc_host(&pdev->dev,
1404 + sizeof(struct sdhci_bcm2708_priv));
1406 + if (IS_ERR(host)) {
1407 + ret = PTR_ERR(host);
1410 + if (missing_status) {
1411 + sdhci_bcm2708_ops.missing_status = sdhci_bcm2708_missing_status;
1414 + if( spurious_crc_acmd51 ) {
1415 + sdhci_bcm2708_ops.spurious_crc_acmd51 = sdhci_bcm2708_quirk_spurious_crc_acmd51;
1419 + printk("sdhci: %s low-latency mode\n",enable_llm?"Enable":"Disable");
1421 + host->hw_name = "BCM2708_Arasan";
1422 + host->ops = &sdhci_bcm2708_ops;
1423 + host->irq = platform_get_irq(pdev, 0);
1424 + host->second_irq = 0;
1426 + host->quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
1427 + SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
1428 + SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1429 + SDHCI_QUIRK_MISSING_CAPS |
1430 + SDHCI_QUIRK_NO_HISPD_BIT |
1431 + (sync_after_dma ? 0:SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12);
1434 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
1435 + host->flags = SDHCI_USE_PLATDMA;
1438 + if (!request_mem_region(iomem->start, resource_size(iomem),
1439 + mmc_hostname(host->mmc))) {
1440 + dev_err(&pdev->dev, "cannot request region\n");
1445 + host->ioaddr = ioremap(iomem->start, resource_size(iomem));
1446 + if (!host->ioaddr) {
1447 + dev_err(&pdev->dev, "failed to remap registers\n");
1452 + host_priv = SDHCI_HOST_PRIV(host);
1454 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
1455 + host_priv->dma_wanted = 0;
1456 +#ifdef CHECK_DMA_USE
1457 + host_priv->dmas_pending = 0;
1458 + host_priv->when_started = 0;
1459 + host_priv->when_reset = 0;
1460 + host_priv->when_stopped = 0;
1462 + host_priv->sg_ix = 0;
1463 + host_priv->sg_done = 0;
1464 + host_priv->complete = NULL;
1465 + host_priv->dma_waits = SDHCI_BCM_DMA_WAITS;
1467 + host_priv->cb_base = dma_alloc_writecombine(&pdev->dev, SZ_4K,
1468 + &host_priv->cb_handle,
1470 + if (!host_priv->cb_base) {
1471 + dev_err(&pdev->dev, "cannot allocate DMA CBs\n");
1473 + goto err_alloc_cb;
1476 + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST,
1477 + &host_priv->dma_chan_base,
1478 + &host_priv->dma_irq);
1480 + dev_err(&pdev->dev, "couldn't allocate a DMA channel\n");
1483 + host_priv->dma_chan = ret;
1485 + ret = request_irq(host_priv->dma_irq, sdhci_bcm2708_dma_irq,0,//IRQF_SHARED,
1486 + DRIVER_NAME " (dma)", host);
1488 + dev_err(&pdev->dev, "cannot set DMA IRQ\n");
1489 + goto err_add_dma_irq;
1491 + host->second_irq = host_priv->dma_irq;
1492 + DBG("DMA CBs %p handle %08X DMA%d %p DMA IRQ %d\n",
1493 + host_priv->cb_base, (unsigned)host_priv->cb_handle,
1494 + host_priv->dma_chan, host_priv->dma_chan_base,
1495 + host_priv->dma_irq);
1497 + if (allow_highspeed)
1498 + host->mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
1500 + /* single block writes cause data loss with some SD cards! */
1501 + host->mmc->caps2 |= MMC_CAP2_FORCE_MULTIBLOCK;
1504 + ret = sdhci_add_host(host);
1506 + goto err_add_host;
1508 + platform_set_drvdata(pdev, host);
1509 + ret = device_create_file(&pdev->dev, &dev_attr_use_dma);
1510 + ret = device_create_file(&pdev->dev, &dev_attr_dma_wait);
1511 + ret = device_create_file(&pdev->dev, &dev_attr_status);
1513 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
1514 + /* enable extension fifo for paced DMA transfers */
1515 + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
1516 + sdhci_bcm2708_writel(host, 4, REG_EXRDFIFO_CFG);
1519 + printk(KERN_INFO "%s: BCM2708 SDHC host at 0x%08llx DMA %d IRQ %d\n",
1520 + mmc_hostname(host->mmc), (unsigned long long)iomem->start,
1521 + host_priv->dma_chan, host_priv->dma_irq);
1526 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
1527 + free_irq(host_priv->dma_irq, host);
1529 + bcm_dma_chan_free(host_priv->dma_chan);
1531 + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
1532 + host_priv->cb_handle);
1535 + iounmap(host->ioaddr);
1537 + release_mem_region(iomem->start, resource_size(iomem));
1539 + sdhci_free_host(host);
1541 + dev_err(&pdev->dev, "probe failed, err %d\n", ret);
1545 +static int sdhci_bcm2708_remove(struct platform_device *pdev)
1547 + struct sdhci_host *host = platform_get_drvdata(pdev);
1548 + struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1549 + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
1554 + scratch = sdhci_bcm2708_readl(host, SDHCI_INT_STATUS);
1555 + if (scratch == (u32)-1)
1558 + device_remove_file(&pdev->dev, &dev_attr_status);
1559 + device_remove_file(&pdev->dev, &dev_attr_dma_wait);
1560 + device_remove_file(&pdev->dev, &dev_attr_use_dma);
1562 +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
1563 + free_irq(host_priv->dma_irq, host);
1564 + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
1565 + host_priv->cb_handle);
1567 + sdhci_remove_host(host, dead);
1568 + iounmap(host->ioaddr);
1569 + release_mem_region(iomem->start, resource_size(iomem));
1570 + sdhci_free_host(host);
1571 + platform_set_drvdata(pdev, NULL);
1576 +static struct platform_driver sdhci_bcm2708_driver = {
1578 + .name = DRIVER_NAME,
1579 + .owner = THIS_MODULE,
1581 + .probe = sdhci_bcm2708_probe,
1582 + .remove = sdhci_bcm2708_remove,
1585 + .suspend = sdhci_bcm2708_suspend,
1586 + .resume = sdhci_bcm2708_resume,
1591 +/*****************************************************************************\
1593 + * Driver init/exit *
1595 +\*****************************************************************************/
1597 +static int __init sdhci_drv_init(void)
1599 + return platform_driver_register(&sdhci_bcm2708_driver);
1602 +static void __exit sdhci_drv_exit(void)
1604 + platform_driver_unregister(&sdhci_bcm2708_driver);
1607 +module_init(sdhci_drv_init);
1608 +module_exit(sdhci_drv_exit);
1610 +module_param(allow_highspeed, bool, 0444);
1611 +module_param(emmc_clock_freq, int, 0444);
1612 +module_param(sync_after_dma, bool, 0444);
1613 +module_param(missing_status, bool, 0444);
1614 +module_param(spurious_crc_acmd51, bool, 0444);
1615 +module_param(enable_llm, bool, 0444);
1616 +module_param(cycle_delay, int, 0444);
1617 +module_param(extra_messages, bool, 0444);
1619 +MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
1620 +MODULE_AUTHOR("Broadcom <info@broadcom.com>");
1621 +MODULE_LICENSE("GPL v2");
1622 +MODULE_ALIAS("platform:"DRIVER_NAME);
1624 +MODULE_PARM_DESC(allow_highspeed, "Allow high speed transfers modes");
1625 +MODULE_PARM_DESC(emmc_clock_freq, "Specify the speed of emmc clock");
1626 +MODULE_PARM_DESC(sync_after_dma, "Block in driver until dma complete");
1627 +MODULE_PARM_DESC(missing_status, "Use the missing status quirk");
1628 +MODULE_PARM_DESC(spurious_crc_acmd51, "Use the spurious crc quirk for reading SCR (ACMD51)");
1629 +MODULE_PARM_DESC(enable_llm, "Enable low-latency mode");
1630 +MODULE_PARM_DESC(extra_messages, "Enable more sdcard warning messages");
1633 diff -urwN linux-3.10/drivers/mmc/host/sdhci.c linux-rpi-3.10.y/drivers/mmc/host/sdhci.c
1634 --- linux-3.10/drivers/mmc/host/sdhci.c 2013-06-30 23:13:29.000000000 +0100
1635 +++ linux-rpi-3.10.y/drivers/mmc/host/sdhci.c 2013-07-06 15:25:50.000000000 +0100
1637 #include <linux/mmc/mmc.h>
1638 #include <linux/mmc/host.h>
1639 #include <linux/mmc/card.h>
1640 +#include <linux/mmc/sd.h>
1641 #include <linux/mmc/slot-gpio.h>
1644 @@ -123,6 +124,91 @@
1645 * Low level functions *
1647 \*****************************************************************************/
1648 +extern bool enable_llm;
1649 +static int sdhci_locked=0;
1650 +void sdhci_spin_lock(struct sdhci_host *host)
1652 + spin_lock(&host->lock);
1653 +#ifdef CONFIG_PREEMPT
1656 + disable_irq_nosync(host->irq);
1657 + if(host->second_irq)
1658 + disable_irq_nosync(host->second_irq);
1659 + local_irq_enable();
1664 +void sdhci_spin_unlock(struct sdhci_host *host)
1666 +#ifdef CONFIG_PREEMPT
1669 + local_irq_disable();
1670 + if(host->second_irq)
1671 + enable_irq(host->second_irq);
1672 + enable_irq(host->irq);
1675 + spin_unlock(&host->lock);
1678 +void sdhci_spin_lock_irqsave(struct sdhci_host *host,unsigned long *flags)
1680 +#ifdef CONFIG_PREEMPT
1683 + while(sdhci_locked)
1685 + preempt_schedule();
1687 + spin_lock_irqsave(&host->lock,*flags);
1688 + disable_irq(host->irq);
1689 + if(host->second_irq)
1690 + disable_irq(host->second_irq);
1691 + local_irq_enable();
1695 + spin_lock_irqsave(&host->lock,*flags);
1698 +void sdhci_spin_unlock_irqrestore(struct sdhci_host *host,unsigned long flags)
1700 +#ifdef CONFIG_PREEMPT
1703 + local_irq_disable();
1704 + if(host->second_irq)
1705 + enable_irq(host->second_irq);
1706 + enable_irq(host->irq);
1709 + spin_unlock_irqrestore(&host->lock,flags);
1712 +static void sdhci_spin_enable_schedule(struct sdhci_host *host)
1714 +#ifdef CONFIG_PREEMPT
1723 +static void sdhci_spin_disable_schedule(struct sdhci_host *host)
1725 +#ifdef CONFIG_PREEMPT
1728 + preempt_disable();
1734 static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
1737 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
1738 unsigned long flags;
1740 - spin_lock_irqsave(&host->lock, flags);
1741 + sdhci_spin_lock_irqsave(host, &flags);
1743 if (host->runtime_suspended)
1747 sdhci_activate_led(host);
1749 - spin_unlock_irqrestore(&host->lock, flags);
1750 + sdhci_spin_unlock_irqrestore(host, flags);
1755 u32 uninitialized_var(scratch);
1758 - DBG("PIO reading\n");
1759 + DBG("PIO reading %db\n", host->data->blksz);
1761 blksize = host->data->blksz;
1767 - DBG("PIO writing\n");
1768 + DBG("PIO writing %db\n", host->data->blksz);
1770 blksize = host->data->blksz;
1772 @@ -399,19 +485,28 @@
1773 local_irq_restore(flags);
1776 -static void sdhci_transfer_pio(struct sdhci_host *host)
1777 +static void sdhci_transfer_pio(struct sdhci_host *host, u32 intstate)
1784 BUG_ON(!host->data);
1786 if (host->blocks == 0)
1789 - if (host->data->flags & MMC_DATA_READ)
1790 + if (host->data->flags & MMC_DATA_READ) {
1791 mask = SDHCI_DATA_AVAILABLE;
1793 + intmask = SDHCI_INT_DATA_AVAIL;
1795 mask = SDHCI_SPACE_AVAILABLE;
1796 + intmask = SDHCI_INT_SPACE_AVAIL;
1799 + /* initially we can see whether we can procede using intstate */
1800 + available = (intstate & intmask);
1803 * Some controllers (JMicron JMB38x) mess up the buffer bits
1805 (host->data->blocks == 1))
1808 - while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
1809 + while (available) {
1810 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
1813 @@ -434,9 +529,11 @@
1815 if (host->blocks == 0)
1817 + state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1818 + available = state & mask;
1821 - DBG("PIO transfer complete.\n");
1822 + DBG("PIO transfer complete - %d blocks left.\n", host->blocks);
1825 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
1827 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
1828 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
1830 - if (host->flags & SDHCI_REQ_USE_DMA)
1831 + /* platform DMA will begin on receipt of PIO irqs */
1832 + if ((host->flags & SDHCI_REQ_USE_DMA) &&
1833 + !(host->flags & SDHCI_USE_PLATDMA))
1834 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
1836 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
1837 @@ -741,44 +840,25 @@
1838 host->data_early = 0;
1839 host->data->bytes_xfered = 0;
1841 - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
1842 + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA | SDHCI_USE_PLATDMA))
1843 host->flags |= SDHCI_REQ_USE_DMA;
1846 * FIXME: This doesn't account for merging when mapping the
1849 - if (host->flags & SDHCI_REQ_USE_DMA) {
1851 - struct scatterlist *sg;
1854 - if (host->flags & SDHCI_USE_ADMA) {
1855 - if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
1858 - if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
1862 - if (unlikely(broken)) {
1863 - for_each_sg(data->sg, sg, data->sg_len, i) {
1864 - if (sg->length & 0x3) {
1865 - DBG("Reverting to PIO because of "
1866 - "transfer size (%d)\n",
1868 - host->flags &= ~SDHCI_REQ_USE_DMA;
1876 * The assumption here being that alignment is the same after
1877 * translation to device address space.
1879 - if (host->flags & SDHCI_REQ_USE_DMA) {
1880 + if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) ==
1881 + (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) {
1883 + if (! sdhci_platdma_dmaable(host, data))
1884 + host->flags &= ~SDHCI_REQ_USE_DMA;
1886 + } else if (host->flags & SDHCI_REQ_USE_DMA) {
1888 struct scatterlist *sg;
1893 host->flags &= ~SDHCI_REQ_USE_DMA;
1896 + if (!(host->flags & SDHCI_USE_PLATDMA)) {
1897 WARN_ON(sg_cnt != 1);
1898 sdhci_writel(host, sg_dma_address(data->sg),
1900 @@ -853,11 +934,13 @@
1901 if (host->version >= SDHCI_SPEC_200) {
1902 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1903 ctrl &= ~SDHCI_CTRL_DMA_MASK;
1904 + if (! (host->flags & SDHCI_USE_PLATDMA)) {
1905 if ((host->flags & SDHCI_REQ_USE_DMA) &&
1906 (host->flags & SDHCI_USE_ADMA))
1907 ctrl |= SDHCI_CTRL_ADMA32;
1909 ctrl |= SDHCI_CTRL_SDMA;
1911 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1916 if (data->flags & MMC_DATA_READ)
1917 mode |= SDHCI_TRNS_READ;
1918 - if (host->flags & SDHCI_REQ_USE_DMA)
1919 + if ((host->flags & SDHCI_REQ_USE_DMA) &&
1920 + !(host->flags & SDHCI_USE_PLATDMA))
1921 mode |= SDHCI_TRNS_DMA;
1923 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
1924 @@ -925,13 +1009,16 @@
1927 if (host->flags & SDHCI_REQ_USE_DMA) {
1928 - if (host->flags & SDHCI_USE_ADMA)
1929 - sdhci_adma_table_post(host, data);
1931 + /* we may have to abandon an ongoing platform DMA */
1932 + if (host->flags & SDHCI_USE_PLATDMA)
1933 + sdhci_platdma_reset(host, data);
1935 + if (host->flags & (SDHCI_USE_PLATDMA | SDHCI_USE_SDMA)) {
1936 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
1937 data->sg_len, (data->flags & MMC_DATA_READ) ?
1938 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1940 + } else if (host->flags & SDHCI_USE_ADMA)
1941 + sdhci_adma_table_post(host, data);
1945 @@ -984,6 +1071,12 @@
1946 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1947 mask |= SDHCI_DATA_INHIBIT;
1949 + if(host->ops->missing_status && (cmd->opcode == MMC_SEND_STATUS)) {
1950 + timeout = 5000; // Really obscenely large delay to send the status, due to bug in controller
1951 + // which might cause the STATUS command to get stuck when a data operation is in flow
1952 + mask |= SDHCI_DATA_INHIBIT;
1955 /* We shouldn't wait for data inihibit for stop commands, even
1956 though they might use busy signaling */
1957 if (host->mrq->data && (cmd == host->mrq->data->stop))
1958 @@ -999,12 +1092,20 @@
1962 + sdhci_spin_enable_schedule(host);
1964 + sdhci_spin_disable_schedule(host);
1966 + DBG("send cmd %d - wait 0x%X irq 0x%x\n", cmd->opcode, mask,
1967 + sdhci_readl(host, SDHCI_INT_STATUS));
1969 mod_timer(&host->timer, jiffies + 10 * HZ);
1972 + if (host->last_cmdop == MMC_APP_CMD)
1973 + host->last_cmdop = -cmd->opcode;
1975 + host->last_cmdop = cmd->opcode;
1977 sdhci_prepare_data(host, cmd);
1979 @@ -1220,7 +1321,9 @@
1983 + sdhci_spin_enable_schedule(host);
1985 + sdhci_spin_disable_schedule(host);
1988 clk |= SDHCI_CLOCK_CARD_EN;
1989 @@ -1316,7 +1419,7 @@
1991 sdhci_runtime_pm_get(host);
1993 - spin_lock_irqsave(&host->lock, flags);
1994 + sdhci_spin_lock_irqsave(host, &flags);
1996 WARN_ON(host->mrq != NULL);
1998 @@ -1374,9 +1477,9 @@
1999 mmc->card->type == MMC_TYPE_MMC ?
2000 MMC_SEND_TUNING_BLOCK_HS200 :
2001 MMC_SEND_TUNING_BLOCK;
2002 - spin_unlock_irqrestore(&host->lock, flags);
2003 + sdhci_spin_unlock_irqrestore(host, flags);
2004 sdhci_execute_tuning(mmc, tuning_opcode);
2005 - spin_lock_irqsave(&host->lock, flags);
2006 + sdhci_spin_lock_irqsave(host, &flags);
2008 /* Restore original mmc_request structure */
2010 @@ -1390,7 +1493,7 @@
2014 - spin_unlock_irqrestore(&host->lock, flags);
2015 + sdhci_spin_unlock_irqrestore(host, flags);
2018 static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
2019 @@ -1399,10 +1502,10 @@
2023 - spin_lock_irqsave(&host->lock, flags);
2024 + sdhci_spin_lock_irqsave(host, &flags);
2026 if (host->flags & SDHCI_DEVICE_DEAD) {
2027 - spin_unlock_irqrestore(&host->lock, flags);
2028 + sdhci_spin_unlock_irqrestore(host, flags);
2029 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
2030 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
2032 @@ -1429,9 +1532,9 @@
2033 vdd_bit = sdhci_set_power(host, ios->vdd);
2035 if (host->vmmc && vdd_bit != -1) {
2036 - spin_unlock_irqrestore(&host->lock, flags);
2037 + sdhci_spin_unlock_irqrestore(host, flags);
2038 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
2039 - spin_lock_irqsave(&host->lock, flags);
2040 + sdhci_spin_lock_irqsave(host, &flags);
2043 if (host->ops->platform_send_init_74_clocks)
2044 @@ -1470,7 +1573,7 @@
2046 ctrl &= ~SDHCI_CTRL_HISPD;
2048 - if (host->version >= SDHCI_SPEC_300) {
2049 + if (host->version >= SDHCI_SPEC_300 && !(host->ops->uhs_broken)) {
2052 /* In case of UHS-I modes, set High Speed Enable */
2053 @@ -1569,7 +1672,7 @@
2054 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2057 - spin_unlock_irqrestore(&host->lock, flags);
2058 + sdhci_spin_unlock_irqrestore(host, flags);
2061 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
2062 @@ -1617,7 +1720,7 @@
2063 unsigned long flags;
2066 - spin_lock_irqsave(&host->lock, flags);
2067 + sdhci_spin_lock_irqsave(host, &flags);
2069 if (host->flags & SDHCI_DEVICE_DEAD)
2071 @@ -1627,7 +1730,7 @@
2072 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
2073 & SDHCI_WRITE_PROTECT);
2075 - spin_unlock_irqrestore(&host->lock, flags);
2076 + sdhci_spin_unlock_irqrestore(host, flags);
2078 /* This quirk needs to be replaced by a callback-function later */
2079 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
2080 @@ -1700,9 +1803,9 @@
2081 struct sdhci_host *host = mmc_priv(mmc);
2082 unsigned long flags;
2084 - spin_lock_irqsave(&host->lock, flags);
2085 + sdhci_spin_lock_irqsave(host, &flags);
2086 sdhci_enable_sdio_irq_nolock(host, enable);
2087 - spin_unlock_irqrestore(&host->lock, flags);
2088 + sdhci_spin_unlock_irqrestore(host, flags);
2091 static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
2092 @@ -2046,7 +2149,7 @@
2093 struct sdhci_host *host = mmc_priv(mmc);
2094 unsigned long flags;
2096 - spin_lock_irqsave(&host->lock, flags);
2097 + sdhci_spin_lock_irqsave(host, &flags);
2099 /* Check host->mrq first in case we are runtime suspended */
2101 @@ -2063,7 +2166,7 @@
2102 tasklet_schedule(&host->finish_tasklet);
2105 - spin_unlock_irqrestore(&host->lock, flags);
2106 + sdhci_spin_unlock_irqrestore(host, flags);
2109 static const struct mmc_host_ops sdhci_ops = {
2110 @@ -2102,14 +2205,14 @@
2112 host = (struct sdhci_host*)param;
2114 - spin_lock_irqsave(&host->lock, flags);
2115 + sdhci_spin_lock_irqsave(host, &flags);
2118 * If this tasklet gets rescheduled while running, it will
2119 * be run again afterwards but without any active request.
2122 - spin_unlock_irqrestore(&host->lock, flags);
2123 + sdhci_spin_unlock_irqrestore(host, flags);
2127 @@ -2147,7 +2250,7 @@
2131 - spin_unlock_irqrestore(&host->lock, flags);
2132 + sdhci_spin_unlock_irqrestore(host, flags);
2134 mmc_request_done(host->mmc, mrq);
2135 sdhci_runtime_pm_put(host);
2136 @@ -2160,11 +2263,11 @@
2138 host = (struct sdhci_host*)data;
2140 - spin_lock_irqsave(&host->lock, flags);
2141 + sdhci_spin_lock_irqsave(host, &flags);
2144 pr_err("%s: Timeout waiting for hardware "
2145 - "interrupt.\n", mmc_hostname(host->mmc));
2146 + "interrupt - cmd%d.\n", mmc_hostname(host->mmc), host->last_cmdop);
2147 sdhci_dumpregs(host);
2150 @@ -2181,7 +2284,7 @@
2154 - spin_unlock_irqrestore(&host->lock, flags);
2155 + sdhci_spin_unlock_irqrestore(host, flags);
2158 static void sdhci_tuning_timer(unsigned long data)
2159 @@ -2191,11 +2294,11 @@
2161 host = (struct sdhci_host *)data;
2163 - spin_lock_irqsave(&host->lock, flags);
2164 + sdhci_spin_lock_irqsave(host, &flags);
2166 host->flags |= SDHCI_NEEDS_RETUNING;
2168 - spin_unlock_irqrestore(&host->lock, flags);
2169 + sdhci_spin_unlock_irqrestore(host, flags);
2172 /*****************************************************************************\
2173 @@ -2209,10 +2312,13 @@
2174 BUG_ON(intmask == 0);
2177 + if (!(host->ops->extra_ints)) {
2178 pr_err("%s: Got command interrupt 0x%08x even "
2179 "though no command operation was in progress.\n",
2180 mmc_hostname(host->mmc), (unsigned)intmask);
2181 sdhci_dumpregs(host);
2183 + DBG("cmd irq 0x%08x cmd complete\n", (unsigned)intmask);
2187 @@ -2282,6 +2388,19 @@
2188 static void sdhci_show_adma_error(struct sdhci_host *host) { }
2191 +static void sdhci_data_end(struct sdhci_host *host)
2195 + * Data managed to finish before the
2196 + * command completed. Make sure we do
2197 + * things in the proper order.
2199 + host->data_early = 1;
2201 + sdhci_finish_data(host);
2204 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2207 @@ -2311,23 +2430,39 @@
2211 + if (!(host->ops->extra_ints)) {
2212 pr_err("%s: Got data interrupt 0x%08x even "
2213 "though no data operation was in progress.\n",
2214 mmc_hostname(host->mmc), (unsigned)intmask);
2215 sdhci_dumpregs(host);
2217 + DBG("data irq 0x%08x but no data\n", (unsigned)intmask);
2222 if (intmask & SDHCI_INT_DATA_TIMEOUT)
2223 host->data->error = -ETIMEDOUT;
2224 - else if (intmask & SDHCI_INT_DATA_END_BIT)
2225 + else if (intmask & SDHCI_INT_DATA_END_BIT) {
2226 + DBG("end error in cmd %d\n", host->last_cmdop);
2227 + if (host->ops->spurious_crc_acmd51 &&
2228 + host->last_cmdop == -SD_APP_SEND_SCR) {
2229 + DBG("ignoring spurious data_end_bit error\n");
2230 + intmask = SDHCI_INT_DATA_AVAIL|SDHCI_INT_DATA_END;
2232 host->data->error = -EILSEQ;
2233 - else if ((intmask & SDHCI_INT_DATA_CRC) &&
2234 + } else if ((intmask & SDHCI_INT_DATA_CRC) &&
2235 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2236 - != MMC_BUS_TEST_R)
2237 + != MMC_BUS_TEST_R) {
2238 + DBG("crc error in cmd %d\n", host->last_cmdop);
2239 + if (host->ops->spurious_crc_acmd51 &&
2240 + host->last_cmdop == -SD_APP_SEND_SCR) {
2241 + DBG("ignoring spurious data_crc_bit error\n");
2242 + intmask = SDHCI_INT_DATA_AVAIL|SDHCI_INT_DATA_END;
2244 host->data->error = -EILSEQ;
2245 - else if (intmask & SDHCI_INT_ADMA_ERROR) {
2247 + } else if (intmask & SDHCI_INT_ADMA_ERROR) {
2248 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
2249 sdhci_show_adma_error(host);
2250 host->data->error = -EIO;
2251 @@ -2335,11 +2470,18 @@
2252 host->ops->adma_workaround(host, intmask);
2255 - if (host->data->error)
2256 + if (host->data->error) {
2257 + DBG("finish request early on error %d\n", host->data->error);
2258 sdhci_finish_data(host);
2260 - if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
2261 - sdhci_transfer_pio(host);
2263 + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
2264 + if (host->flags & SDHCI_REQ_USE_DMA) {
2265 + /* possible only in PLATDMA mode */
2266 + sdhci_platdma_avail(host, &intmask,
2269 + sdhci_transfer_pio(host, intmask);
2273 * We currently don't do anything fancy with DMA
2274 @@ -2368,18 +2510,8 @@
2275 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2278 - if (intmask & SDHCI_INT_DATA_END) {
2281 - * Data managed to finish before the
2282 - * command completed. Make sure we do
2283 - * things in the proper order.
2285 - host->data_early = 1;
2287 - sdhci_finish_data(host);
2290 + if (intmask & SDHCI_INT_DATA_END)
2291 + sdhci_data_end(host);
2295 @@ -2390,10 +2522,10 @@
2296 u32 intmask, unexpected = 0;
2297 int cardint = 0, max_loops = 16;
2299 - spin_lock(&host->lock);
2300 + sdhci_spin_lock(host);
2302 if (host->runtime_suspended) {
2303 - spin_unlock(&host->lock);
2304 + sdhci_spin_unlock(host);
2305 pr_warning("%s: got irq while runtime suspended\n",
2306 mmc_hostname(host->mmc));
2308 @@ -2435,6 +2567,22 @@
2309 tasklet_schedule(&host->card_tasklet);
2312 + if (intmask & SDHCI_INT_ERROR_MASK & ~SDHCI_INT_ERROR)
2313 + DBG("controller reports error 0x%x -"
2314 + "%s%s%s%s%s%s%s%s%s%s",
2316 + intmask & SDHCI_INT_TIMEOUT? " timeout": "",
2317 + intmask & SDHCI_INT_CRC ? " crc": "",
2318 + intmask & SDHCI_INT_END_BIT? " endbit": "",
2319 + intmask & SDHCI_INT_INDEX? " index": "",
2320 + intmask & SDHCI_INT_DATA_TIMEOUT? " data_timeout": "",
2321 + intmask & SDHCI_INT_DATA_CRC? " data_crc": "",
2322 + intmask & SDHCI_INT_DATA_END_BIT? " data_endbit": "",
2323 + intmask & SDHCI_INT_BUS_POWER? " buspower": "",
2324 + intmask & SDHCI_INT_ACMD12ERR? " acmd12": "",
2325 + intmask & SDHCI_INT_ADMA_ERROR? " adma": ""
2328 if (intmask & SDHCI_INT_CMD_MASK) {
2329 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2331 @@ -2449,7 +2597,13 @@
2333 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2335 - intmask &= ~SDHCI_INT_ERROR;
2336 + if (intmask & SDHCI_INT_ERROR_MASK) {
2337 + /* collect any uncovered errors */
2338 + sdhci_writel(host, intmask & SDHCI_INT_ERROR_MASK,
2339 + SDHCI_INT_STATUS);
2342 + intmask &= ~SDHCI_INT_ERROR_MASK;
2344 if (intmask & SDHCI_INT_BUS_POWER) {
2345 pr_err("%s: Card is consuming too much power!\n",
2346 @@ -2475,7 +2629,7 @@
2347 if (intmask && --max_loops)
2350 - spin_unlock(&host->lock);
2351 + sdhci_spin_unlock(host);
2354 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2355 @@ -2569,7 +2723,8 @@
2359 - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2360 + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
2361 + SDHCI_USE_PLATDMA)) {
2362 if (host->ops->enable_dma)
2363 host->ops->enable_dma(host);
2365 @@ -2636,15 +2791,15 @@
2366 host->flags &= ~SDHCI_NEEDS_RETUNING;
2369 - spin_lock_irqsave(&host->lock, flags);
2370 + sdhci_spin_lock_irqsave(host, &flags);
2371 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2372 - spin_unlock_irqrestore(&host->lock, flags);
2373 + sdhci_spin_unlock_irqrestore(host, flags);
2375 synchronize_irq(host->irq);
2377 - spin_lock_irqsave(&host->lock, flags);
2378 + sdhci_spin_lock_irqsave(host, &flags);
2379 host->runtime_suspended = true;
2380 - spin_unlock_irqrestore(&host->lock, flags);
2381 + sdhci_spin_unlock_irqrestore(host, flags);
2385 @@ -2670,16 +2825,16 @@
2386 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2387 if ((host_flags & SDHCI_PV_ENABLED) &&
2388 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2389 - spin_lock_irqsave(&host->lock, flags);
2390 + sdhci_spin_lock_irqsave(host, &flags);
2391 sdhci_enable_preset_value(host, true);
2392 - spin_unlock_irqrestore(&host->lock, flags);
2393 + sdhci_spin_unlock_irqrestore(host, flags);
2396 /* Set the re-tuning expiration flag */
2397 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2398 host->flags |= SDHCI_NEEDS_RETUNING;
2400 - spin_lock_irqsave(&host->lock, flags);
2401 + sdhci_spin_lock_irqsave(host, &flags);
2403 host->runtime_suspended = false;
2405 @@ -2690,7 +2845,7 @@
2406 /* Enable Card Detection */
2407 sdhci_enable_card_detection(host);
2409 - spin_unlock_irqrestore(&host->lock, flags);
2410 + sdhci_spin_unlock_irqrestore(host, flags);
2414 @@ -2785,14 +2940,16 @@
2415 host->flags &= ~SDHCI_USE_ADMA;
2418 - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2419 + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
2420 + SDHCI_USE_PLATDMA)) {
2421 if (host->ops->enable_dma) {
2422 if (host->ops->enable_dma(host)) {
2423 pr_warning("%s: No suitable DMA "
2424 "available. Falling back to PIO.\n",
2427 - ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2428 + ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA |
2429 + SDHCI_USE_PLATDMA);
2433 @@ -3080,6 +3237,12 @@
2434 SDHCI_MAX_CURRENT_MULTIPLIER;
2437 + if(host->ops->voltage_broken) {
2438 + ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
2439 + // Cannot support UHS modes if we are stuck at 3.3V;
2440 + mmc->caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50);
2443 mmc->ocr_avail = ocr_avail;
2444 mmc->ocr_avail_sdio = ocr_avail;
2445 if (host->ocr_avail_sdio)
2446 @@ -3174,7 +3337,7 @@
2447 host->tuning_timer.function = sdhci_tuning_timer;
2450 - ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2451 + ret = request_irq(host->irq, sdhci_irq, 0,//IRQF_SHARED,
2452 mmc_hostname(mmc), host);
2454 pr_err("%s: Failed to request IRQ %d: %d\n",
2455 @@ -3210,6 +3373,7 @@
2457 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
2458 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
2459 + (host->flags & SDHCI_USE_PLATDMA) ? "platform's DMA" :
2460 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2461 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
2463 @@ -3237,7 +3401,7 @@
2464 unsigned long flags;
2467 - spin_lock_irqsave(&host->lock, flags);
2468 + sdhci_spin_lock_irqsave(host, &flags);
2470 host->flags |= SDHCI_DEVICE_DEAD;
2472 @@ -3249,7 +3413,7 @@
2473 tasklet_schedule(&host->finish_tasklet);
2476 - spin_unlock_irqrestore(&host->lock, flags);
2477 + sdhci_spin_unlock_irqrestore(host, flags);
2480 sdhci_disable_card_detection(host);
2481 diff -urwN linux-3.10/drivers/mmc/host/sdhci.h linux-rpi-3.10.y/drivers/mmc/host/sdhci.h
2482 --- linux-3.10/drivers/mmc/host/sdhci.h 2013-06-30 23:13:29.000000000 +0100
2483 +++ linux-rpi-3.10.y/drivers/mmc/host/sdhci.h 2013-07-06 15:25:50.000000000 +0100
2484 @@ -289,6 +289,20 @@
2485 void (*platform_reset_enter)(struct sdhci_host *host, u8 mask);
2486 void (*platform_reset_exit)(struct sdhci_host *host, u8 mask);
2487 int (*set_uhs_signaling)(struct sdhci_host *host, unsigned int uhs);
2489 + int (*pdma_able)(struct sdhci_host *host,
2490 + struct mmc_data *data);
2491 + void (*pdma_avail)(struct sdhci_host *host,
2492 + unsigned int *ref_intmask,
2493 + void(*complete)(struct sdhci_host *));
2494 + void (*pdma_reset)(struct sdhci_host *host,
2495 + struct mmc_data *data);
2496 + unsigned int (*extra_ints)(struct sdhci_host *host);
2497 + unsigned int (*spurious_crc_acmd51)(struct sdhci_host *host);
2498 + unsigned int (*voltage_broken)(struct sdhci_host *host);
2499 + unsigned int (*uhs_broken)(struct sdhci_host *host);
2500 + unsigned int (*missing_status)(struct sdhci_host *host);
2502 void (*hw_reset)(struct sdhci_host *host);
2503 void (*platform_suspend)(struct sdhci_host *host);
2504 void (*platform_resume)(struct sdhci_host *host);
2505 @@ -399,9 +413,38 @@
2506 extern void sdhci_enable_irq_wakeups(struct sdhci_host *host);
2509 +static inline int /*bool*/
2510 +sdhci_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
2512 + if (host->ops->pdma_able)
2513 + return host->ops->pdma_able(host, data);
2518 +sdhci_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
2519 + void(*completion_callback)(struct sdhci_host *))
2521 + if (host->ops->pdma_avail)
2522 + host->ops->pdma_avail(host, ref_intmask, completion_callback);
2526 +sdhci_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
2528 + if (host->ops->pdma_reset)
2529 + host->ops->pdma_reset(host, data);
2532 #ifdef CONFIG_PM_RUNTIME
2533 extern int sdhci_runtime_suspend_host(struct sdhci_host *host);
2534 extern int sdhci_runtime_resume_host(struct sdhci_host *host);
2537 +extern void sdhci_spin_lock_irqsave(struct sdhci_host *host,unsigned long *flags);
2538 +extern void sdhci_spin_unlock_irqrestore(struct sdhci_host *host,unsigned long flags);
2539 +extern void sdhci_spin_lock(struct sdhci_host *host);
2540 +extern void sdhci_spin_unlock(struct sdhci_host *host);
2543 #endif /* __SDHCI_HW_H */
2544 diff -urN linux-3.10/include/linux/mmc/host.h linux-rpi-3.10.y/include/linux/mmc/host.h
2545 --- linux-3.10/include/linux/mmc/host.h 2013-06-30 23:13:29.000000000 +0100
2546 +++ linux-rpi-3.10.y/include/linux/mmc/host.h 2013-07-06 15:25:50.000000000 +0100
2548 #define MMC_CAP2_PACKED_CMD (MMC_CAP2_PACKED_RD | \
2550 #define MMC_CAP2_NO_PRESCAN_POWERUP (1 << 14) /* Don't power up before scan */
2551 +#define MMC_CAP2_FORCE_MULTIBLOCK (1 << 31) /* Always use multiblock transfers */
2553 mmc_pm_flag_t pm_caps; /* supported pm features */
2555 diff -urN linux-3.10/include/linux/mmc/sdhci.h linux-rpi-3.10.y/include/linux/mmc/sdhci.h
2556 --- linux-3.10/include/linux/mmc/sdhci.h 2013-06-30 23:13:29.000000000 +0100
2557 +++ linux-rpi-3.10.y/include/linux/mmc/sdhci.h 2013-07-06 15:25:50.000000000 +0100
2559 #define SDHCI_QUIRK2_PRESET_VALUE_BROKEN (1<<3)
2561 int irq; /* Device IRQ */
2562 + int second_irq; /* Additional IRQ to disable/enable in low-latency mode */
2563 void __iomem *ioaddr; /* Mapped address */
2565 const struct sdhci_ops *ops; /* Low level hw interface */
2567 #define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
2568 #define SDHCI_HS200_NEEDS_TUNING (1<<10) /* HS200 needs tuning */
2569 #define SDHCI_USING_RETUNING_TIMER (1<<11) /* Host is using a retuning timer for the card */
2570 +#define SDHCI_USE_PLATDMA (1<<12) /* Host uses 3rd party DMA */
2572 unsigned int version; /* SDHCI spec. version */
2576 struct mmc_request *mrq; /* Current request */
2577 struct mmc_command *cmd; /* Current command */
2578 + int last_cmdop; /* Opcode of last cmd sent */
2579 struct mmc_data *data; /* Current data request */
2580 unsigned int data_early:1; /* Data finished before cmd */