1 From f6ef8b1ebd154b65626191b38ca2cde8274d3ecd Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.org>
3 Date: Wed, 25 Mar 2015 17:49:47 +0000
4 Subject: [PATCH 041/454] Adding bcm2835-sdhost driver, and an overlay to
7 BCM2835 has two SD card interfaces. This driver uses the other one.
9 bcm2835-sdhost: Error handling fix, and code clarification
11 bcm2835-sdhost: Adding overclocking option
13 Allow a different clock speed to be substitued for a requested 50MHz.
14 This option is exposed using the "overclock_50" DT parameter.
15 Note that the sdhost interface is restricted to integer divisions of
16 core_freq, and the highest sensible option for a core_freq of 250MHz
17 is 84 (250/3 = 83.3MHz), the next being 125 (250/2) which is much too
22 bcm2835-sdhost: Round up the overclock, so 62 works for 62.5Mhz
24 Also only warn once for each overclock setting.
26 bcm2835-sdhost: Improve error handling and recovery
28 1) Expose the hw_reset method to the MMC framework, removing many
29 internal calls by the driver.
31 2) Reduce overclock setting on error.
33 3) Increase timeout to cope with high capacity cards.
35 4) Add properties and parameters to control pio_limit and debug.
37 5) Reduce messages at probe time.
39 bcm2835-sdhost: Further improve overclock back-off
41 bcm2835-sdhost: Clear HBLC for PIO mode
43 Also update pio_limit default in overlay README.
45 bcm2835-sdhost: Add the ERASE capability
47 See: https://github.com/raspberrypi/linux/issues/1076
49 bcm2835-sdhost: Ignore CRC7 for MMC CMD1
51 It seems that the sdhost interface returns CRC7 errors for CMD1,
52 which is the MMC-specific SEND_OP_COND. Returning these errors to
53 the MMC layer causes a downward spiral, but ignoring them seems
56 bcm2835-mmc/sdhost: Remove ARCH_BCM2835 differences
58 The bcm2835-mmc driver (and -sdhost driver that copied from it)
59 contains code to handle SDIO interrupts in a threaded interrupt
60 handler rather than waking the MMC framework thread. The change
61 follows a patch from Russell King that adds the facility as the
62 preferred way of working.
64 However, the new code path is only present in ARCH_BCM2835
65 builds, which I have taken to be a way of testing the waters
66 rather than making the change across the board; I can't see
67 any technical reason why it wouldn't be enabled for MACH_BCM270X
68 builds. So this patch standardises on the ARCH_BCM2835 code,
69 removing the old code paths.
71 bcm2835-sdhost: Don't log timeout errors unless debug=1
73 The MMC card-discovery process generates timeouts. This is
74 expected behaviour, so reporting it to the user serves no purpose.
75 Suppress the reporting of timeout errors unless the debug flag
78 bcm2835-sdhost: Add workaround for odd behaviour on some cards
80 For reasons not understood, the sdhost driver fails when reading
81 sectors very near the end of some SD cards. The problem could
82 be related to the similar issue that reading the final sector
83 of any card as part of a multiple read never completes, and the
84 workaround is an extension of the mechanism introduced to solve
85 that problem which ensures those sectors are always read singly.
87 bcm2835-sdhost: Major revision
89 This is a significant revision of the bcm2835-sdhost driver. It
90 improves on the original in a number of ways:
92 1) Through the use of CMD23 for reads it appears to avoid problems
93 reading some sectors on certain high speed cards.
94 2) Better atomicity to prevent crashes.
95 3) Higher performance.
96 4) Activity logging included, for easier diagnosis in the event
99 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
101 bcm2835-sdhost: Restore ATOMIC flag to PIO sg mapping
103 Allocation problems have been seen in a wireless driver, and
104 this is the only change which might have been responsible.
106 SQUASH: bcm2835-sdhost: Only claim one DMA channel
108 With both MMC controllers enabled there are few DMA channels left. The
109 bcm2835-sdhost driver only uses DMA in one direction at a time, so it
110 doesn't need to claim two channels.
112 See: https://github.com/raspberrypi/linux/issues/1327
114 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
116 bcm2835-sdhost: Workaround for "slow" sectors
118 Some cards have been seen to cause timeouts after certain sectors are
119 read. This workaround enforces a minimum delay between the stop after
120 reading one of those sectors and a subsequent data command.
122 Using CMD23 (SET_BLOCK_COUNT) avoids this problem, so good cards will
123 not be penalised by this workaround.
125 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
127 bcm2835-sdhost: Firmware manages the clock divisor
129 The bcm2835-sdhost driver hands control of the CDIV clock divisor
130 register to matching firmware, allowing it to adjust to a changing
131 core clock. This removes the need to use the performance governor or
132 to enable io_is_busy on the on-demand governor in order to get the
135 N.B. As SD clocks must be an integer divisor of the core clock, it is
136 possible that the SD clock for "turbo" mode can be different (even
137 lower) than "normal" mode.
139 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
141 bcm2835-sdhost: Reset the clock in task context
143 Since reprogramming the clock can now involve a round-trip to the
144 firmware it must not be done at atomic context, and a tasklet
147 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
149 bcm2835-sdhost: Don't exit cmd wait loop on error
151 The FAIL flag can be set in the CMD register before command processing
152 is complete, leading to spurious "failed to complete" errors. This has
153 the effect of promoting harmless CRC7 errors during CMD1 processing
154 into errors that can delay and even prevent booting.
157 1) Convert the last KERN_ERROR message in the register dumping to
159 2) Remove an unnecessary reset call from bcm2835_sdhost_add_host.
161 See: https://github.com/raspberrypi/linux/pull/1492
163 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
165 bcm2835-sdhost: mmc_card_blockaddr fix
167 Get the definition of mmc_card_blockaddr from drivers/mmc/core/card.h.
169 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
171 drivers/mmc/host/Kconfig | 10 +
172 drivers/mmc/host/Makefile | 1 +
173 drivers/mmc/host/bcm2835-sdhost.c | 2193 +++++++++++++++++++++++++++++
174 3 files changed, 2204 insertions(+)
175 create mode 100644 drivers/mmc/host/bcm2835-sdhost.c
177 --- a/drivers/mmc/host/Kconfig
178 +++ b/drivers/mmc/host/Kconfig
179 @@ -33,6 +33,16 @@ config MMC_BCM2835_PIO_DMA_BARRIER
181 If unsure, say 2 here.
183 +config MMC_BCM2835_SDHOST
184 + tristate "Support for the SDHost controller on BCM2708/9"
185 + depends on ARCH_BCM2835
187 + This selects the SDHost controller on BCM2835/6.
189 + If you have a controller with this interface, say Y or M here.
194 bool "MMC host drivers debugging"
196 --- a/drivers/mmc/host/Makefile
197 +++ b/drivers/mmc/host/Makefile
198 @@ -21,6 +21,7 @@ obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci
199 obj-$(CONFIG_MMC_SDHCI_F_SDH30) += sdhci_f_sdh30.o
200 obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
201 obj-$(CONFIG_MMC_BCM2835_MMC) += bcm2835-mmc.o
202 +obj-$(CONFIG_MMC_BCM2835_SDHOST) += bcm2835-sdhost.o
203 obj-$(CONFIG_MMC_WBSD) += wbsd.o
204 obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
205 obj-$(CONFIG_MMC_MTK) += mtk-sd.o
207 +++ b/drivers/mmc/host/bcm2835-sdhost.c
210 + * BCM2835 SD host driver.
212 + * Author: Phil Elwell <phil@raspberrypi.org>
213 + * Copyright (C) 2015-2016 Raspberry Pi (Trading) Ltd.
216 + * mmc-bcm2835.c by Gellert Weisz
217 + * which is, in turn, based on
218 + * sdhci-bcm2708.c by Broadcom
219 + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
220 + * sdhci.c and sdhci-pci.c by Pierre Ossman
222 + * This program is free software; you can redistribute it and/or modify it
223 + * under the terms and conditions of the GNU General Public License,
224 + * version 2, as published by the Free Software Foundation.
226 + * This program is distributed in the hope it will be useful, but WITHOUT
227 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
228 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
231 + * You should have received a copy of the GNU General Public License
232 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
235 +#define FIFO_READ_THRESHOLD 4
236 +#define FIFO_WRITE_THRESHOLD 4
237 +#define ALLOW_CMD23_READ 1
238 +#define ALLOW_CMD23_WRITE 0
239 +#define ENABLE_LOG 1
240 +#define SDDATA_FIFO_PIO_BURST 8
241 +#define CMD_DALLY_US 1
243 +#include <linux/delay.h>
244 +#include <linux/module.h>
245 +#include <linux/io.h>
246 +#include <linux/mmc/mmc.h>
247 +#include <linux/mmc/host.h>
248 +#include <linux/mmc/sd.h>
249 +#include <linux/mmc/sdio.h>
250 +#include <linux/scatterlist.h>
251 +#include <linux/of_address.h>
252 +#include <linux/of_irq.h>
253 +#include <linux/clk.h>
254 +#include <linux/platform_device.h>
255 +#include <linux/err.h>
256 +#include <linux/blkdev.h>
257 +#include <linux/dmaengine.h>
258 +#include <linux/dma-mapping.h>
259 +#include <linux/of_dma.h>
260 +#include <linux/time.h>
261 +#include <linux/workqueue.h>
262 +#include <linux/interrupt.h>
263 +#include <soc/bcm2835/raspberrypi-firmware.h>
265 +/* For mmc_card_blockaddr */
266 +#include "../core/card.h"
268 +#define DRIVER_NAME "sdhost-bcm2835"
270 +#define SDCMD 0x00 /* Command to SD card - 16 R/W */
271 +#define SDARG 0x04 /* Argument to SD card - 32 R/W */
272 +#define SDTOUT 0x08 /* Start value for timeout counter - 32 R/W */
273 +#define SDCDIV 0x0c /* Start value for clock divider - 11 R/W */
274 +#define SDRSP0 0x10 /* SD card response (31:0) - 32 R */
275 +#define SDRSP1 0x14 /* SD card response (63:32) - 32 R */
276 +#define SDRSP2 0x18 /* SD card response (95:64) - 32 R */
277 +#define SDRSP3 0x1c /* SD card response (127:96) - 32 R */
278 +#define SDHSTS 0x20 /* SD host status - 11 R */
279 +#define SDVDD 0x30 /* SD card power control - 1 R/W */
280 +#define SDEDM 0x34 /* Emergency Debug Mode - 13 R/W */
281 +#define SDHCFG 0x38 /* Host configuration - 2 R/W */
282 +#define SDHBCT 0x3c /* Host byte count (debug) - 32 R/W */
283 +#define SDDATA 0x40 /* Data to/from SD card - 32 R/W */
284 +#define SDHBLC 0x50 /* Host block count (SDIO/SDHC) - 9 R/W */
286 +#define SDCMD_NEW_FLAG 0x8000
287 +#define SDCMD_FAIL_FLAG 0x4000
288 +#define SDCMD_BUSYWAIT 0x800
289 +#define SDCMD_NO_RESPONSE 0x400
290 +#define SDCMD_LONG_RESPONSE 0x200
291 +#define SDCMD_WRITE_CMD 0x80
292 +#define SDCMD_READ_CMD 0x40
293 +#define SDCMD_CMD_MASK 0x3f
295 +#define SDCDIV_MAX_CDIV 0x7ff
297 +#define SDHSTS_BUSY_IRPT 0x400
298 +#define SDHSTS_BLOCK_IRPT 0x200
299 +#define SDHSTS_SDIO_IRPT 0x100
300 +#define SDHSTS_REW_TIME_OUT 0x80
301 +#define SDHSTS_CMD_TIME_OUT 0x40
302 +#define SDHSTS_CRC16_ERROR 0x20
303 +#define SDHSTS_CRC7_ERROR 0x10
304 +#define SDHSTS_FIFO_ERROR 0x08
307 +#define SDHSTS_DATA_FLAG 0x01
309 +#define SDHSTS_TRANSFER_ERROR_MASK (SDHSTS_CRC7_ERROR|SDHSTS_CRC16_ERROR|SDHSTS_REW_TIME_OUT|SDHSTS_FIFO_ERROR)
310 +#define SDHSTS_ERROR_MASK (SDHSTS_CMD_TIME_OUT|SDHSTS_TRANSFER_ERROR_MASK)
312 +#define SDHCFG_BUSY_IRPT_EN (1<<10)
313 +#define SDHCFG_BLOCK_IRPT_EN (1<<8)
314 +#define SDHCFG_SDIO_IRPT_EN (1<<5)
315 +#define SDHCFG_DATA_IRPT_EN (1<<4)
316 +#define SDHCFG_SLOW_CARD (1<<3)
317 +#define SDHCFG_WIDE_EXT_BUS (1<<2)
318 +#define SDHCFG_WIDE_INT_BUS (1<<1)
319 +#define SDHCFG_REL_CMD_LINE (1<<0)
321 +#define SDEDM_FORCE_DATA_MODE (1<<19)
322 +#define SDEDM_CLOCK_PULSE (1<<20)
323 +#define SDEDM_BYPASS (1<<21)
325 +#define SDEDM_WRITE_THRESHOLD_SHIFT 9
326 +#define SDEDM_READ_THRESHOLD_SHIFT 14
327 +#define SDEDM_THRESHOLD_MASK 0x1f
329 +#define SDEDM_FSM_MASK 0xf
330 +#define SDEDM_FSM_IDENTMODE 0x0
331 +#define SDEDM_FSM_DATAMODE 0x1
332 +#define SDEDM_FSM_READDATA 0x2
333 +#define SDEDM_FSM_WRITEDATA 0x3
334 +#define SDEDM_FSM_READWAIT 0x4
335 +#define SDEDM_FSM_READCRC 0x5
336 +#define SDEDM_FSM_WRITECRC 0x6
337 +#define SDEDM_FSM_WRITEWAIT1 0x7
338 +#define SDEDM_FSM_POWERDOWN 0x8
339 +#define SDEDM_FSM_POWERUP 0x9
340 +#define SDEDM_FSM_WRITESTART1 0xa
341 +#define SDEDM_FSM_WRITESTART2 0xb
342 +#define SDEDM_FSM_GENPULSES 0xc
343 +#define SDEDM_FSM_WRITEWAIT2 0xd
344 +#define SDEDM_FSM_STARTPOWDOWN 0xf
346 +#define SDDATA_FIFO_WORDS 16
348 +#define USE_CMD23_FLAGS ((ALLOW_CMD23_READ * MMC_DATA_READ) | \
349 + (ALLOW_CMD23_WRITE * MMC_DATA_WRITE))
354 +struct bcm2835_host {
357 + void __iomem *ioaddr;
360 + struct mmc_host *mmc;
362 + u32 pio_timeout; /* In jiffies */
364 + int clock; /* Current clock speed */
366 + bool slow_card; /* Force 11-bit divisor */
368 + unsigned int max_clk; /* Max possible freq */
370 + struct tasklet_struct finish_tasklet; /* Tasklet structures */
372 + struct work_struct cmd_wait_wq; /* Workqueue function */
374 + struct timer_list timer; /* Timer for timeouts */
376 + struct sg_mapping_iter sg_miter; /* SG state for PIO */
377 + unsigned int blocks; /* remaining PIO blocks */
379 + int irq; /* Device IRQ */
381 + u32 cmd_quick_poll_retries;
382 + u32 ns_per_fifo_word;
384 + /* cached registers */
388 + struct mmc_request *mrq; /* Current request */
389 + struct mmc_command *cmd; /* Current command */
390 + struct mmc_data *data; /* Current data request */
391 + unsigned int data_complete:1; /* Data finished before cmd */
393 + unsigned int flush_fifo:1; /* Drain the fifo when finishing */
395 + unsigned int use_busy:1; /* Wait for busy interrupt */
397 + unsigned int use_sbc:1; /* Send CMD23 */
399 + unsigned int debug:1; /* Enable debug output */
400 + unsigned int firmware_sets_cdiv:1; /* Let the firmware manage the clock */
401 + unsigned int reset_clock:1; /* Reset the clock fore the next request */
404 + struct dma_chan *dma_chan_rxtx; /* DMA channel for reads and writes */
405 + struct dma_chan *dma_chan; /* Channel in use */
406 + struct dma_slave_config dma_cfg_rx;
407 + struct dma_slave_config dma_cfg_tx;
408 + struct dma_async_tx_descriptor *dma_desc;
411 + struct page *drain_page;
416 + /*end of DMA part*/
418 + int max_delay; /* maximum length of time spent waiting */
419 + struct timeval stop_time; /* when the last stop was issued */
420 + u32 delay_after_stop; /* minimum time between stop and subsequent data transfer */
421 + u32 delay_after_this_stop; /* minimum time between this stop and subsequent data transfer */
422 + u32 user_overclock_50; /* User's preferred frequency to use when 50MHz is requested (in MHz) */
423 + u32 overclock_50; /* frequency to use when 50MHz is requested (in MHz) */
424 + u32 overclock; /* Current frequency if overclocked, else zero */
425 + u32 pio_limit; /* Maximum block count for PIO (0 = always DMA) */
427 + u32 sectors; /* Cached card size in sectors */
432 +struct log_entry_struct {
439 +typedef struct log_entry_struct LOG_ENTRY_T;
441 +LOG_ENTRY_T *sdhost_log_buf;
442 +dma_addr_t sdhost_log_addr;
443 +static u32 sdhost_log_idx;
444 +static spinlock_t log_lock;
445 +static void __iomem *timer_base;
447 +#define LOG_ENTRIES (256*1)
448 +#define LOG_SIZE (sizeof(LOG_ENTRY_T)*LOG_ENTRIES)
450 +static void log_init(struct device *dev, u32 bus_to_phys)
452 + spin_lock_init(&log_lock);
453 + sdhost_log_buf = dma_zalloc_coherent(dev, LOG_SIZE, &sdhost_log_addr,
455 + if (sdhost_log_buf) {
456 + pr_info("sdhost: log_buf @ %p (%x)\n",
457 + sdhost_log_buf, sdhost_log_addr);
458 + timer_base = ioremap_nocache(bus_to_phys + 0x7e003000, SZ_4K);
460 + pr_err("sdhost: failed to remap timer\n");
463 + pr_err("sdhost: failed to allocate log buf\n");
466 +static void log_event_impl(const char *event, u32 param1, u32 param2)
468 + if (sdhost_log_buf) {
469 + LOG_ENTRY_T *entry;
470 + unsigned long flags;
472 + spin_lock_irqsave(&log_lock, flags);
474 + entry = sdhost_log_buf + sdhost_log_idx;
475 + memcpy(entry->event, event, 4);
476 + entry->timestamp = (readl(timer_base + 4) & 0x3fffffff) +
477 + (smp_processor_id()<<30);
478 + entry->param1 = param1;
479 + entry->param2 = param2;
480 + sdhost_log_idx = (sdhost_log_idx + 1) % LOG_ENTRIES;
482 + spin_unlock_irqrestore(&log_lock, flags);
486 +static void log_dump(void)
488 + if (sdhost_log_buf) {
489 + LOG_ENTRY_T *entry;
490 + unsigned long flags;
493 + spin_lock_irqsave(&log_lock, flags);
495 + idx = sdhost_log_idx;
497 + entry = sdhost_log_buf + idx;
498 + if (entry->event[0] != '\0')
499 + pr_info("[%08x] %.4s %x %x\n",
504 + idx = (idx + 1) % LOG_ENTRIES;
505 + } while (idx != sdhost_log_idx);
507 + spin_unlock_irqrestore(&log_lock, flags);
511 +#define log_event(event, param1, param2) log_event_impl(event, param1, param2)
515 +#define log_init(x) (void)0
516 +#define log_event(event, param1, param2) (void)0
517 +#define log_dump() (void)0
521 +static inline void bcm2835_sdhost_write(struct bcm2835_host *host, u32 val, int reg)
523 + writel(val, host->ioaddr + reg);
526 +static inline u32 bcm2835_sdhost_read(struct bcm2835_host *host, int reg)
528 + return readl(host->ioaddr + reg);
531 +static inline u32 bcm2835_sdhost_read_relaxed(struct bcm2835_host *host, int reg)
533 + return readl_relaxed(host->ioaddr + reg);
536 +static void bcm2835_sdhost_dumpcmd(struct bcm2835_host *host,
537 + struct mmc_command *cmd,
541 + pr_info("%s:%c%s op %d arg 0x%x flags 0x%x - resp %08x %08x %08x %08x, err %d\n",
542 + mmc_hostname(host->mmc),
543 + (cmd == host->cmd) ? '>' : ' ',
544 + label, cmd->opcode, cmd->arg, cmd->flags,
545 + cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3],
549 +static void bcm2835_sdhost_dumpregs(struct bcm2835_host *host)
553 + bcm2835_sdhost_dumpcmd(host, host->mrq->sbc, "sbc");
554 + bcm2835_sdhost_dumpcmd(host, host->mrq->cmd, "cmd");
555 + if (host->mrq->data)
556 + pr_info("%s: data blocks %x blksz %x - err %d\n",
557 + mmc_hostname(host->mmc),
558 + host->mrq->data->blocks,
559 + host->mrq->data->blksz,
560 + host->mrq->data->error);
561 + bcm2835_sdhost_dumpcmd(host, host->mrq->stop, "stop");
564 + pr_info("%s: =========== REGISTER DUMP ===========\n",
565 + mmc_hostname(host->mmc));
567 + pr_info("%s: SDCMD 0x%08x\n",
568 + mmc_hostname(host->mmc),
569 + bcm2835_sdhost_read(host, SDCMD));
570 + pr_info("%s: SDARG 0x%08x\n",
571 + mmc_hostname(host->mmc),
572 + bcm2835_sdhost_read(host, SDARG));
573 + pr_info("%s: SDTOUT 0x%08x\n",
574 + mmc_hostname(host->mmc),
575 + bcm2835_sdhost_read(host, SDTOUT));
576 + pr_info("%s: SDCDIV 0x%08x\n",
577 + mmc_hostname(host->mmc),
578 + bcm2835_sdhost_read(host, SDCDIV));
579 + pr_info("%s: SDRSP0 0x%08x\n",
580 + mmc_hostname(host->mmc),
581 + bcm2835_sdhost_read(host, SDRSP0));
582 + pr_info("%s: SDRSP1 0x%08x\n",
583 + mmc_hostname(host->mmc),
584 + bcm2835_sdhost_read(host, SDRSP1));
585 + pr_info("%s: SDRSP2 0x%08x\n",
586 + mmc_hostname(host->mmc),
587 + bcm2835_sdhost_read(host, SDRSP2));
588 + pr_info("%s: SDRSP3 0x%08x\n",
589 + mmc_hostname(host->mmc),
590 + bcm2835_sdhost_read(host, SDRSP3));
591 + pr_info("%s: SDHSTS 0x%08x\n",
592 + mmc_hostname(host->mmc),
593 + bcm2835_sdhost_read(host, SDHSTS));
594 + pr_info("%s: SDVDD 0x%08x\n",
595 + mmc_hostname(host->mmc),
596 + bcm2835_sdhost_read(host, SDVDD));
597 + pr_info("%s: SDEDM 0x%08x\n",
598 + mmc_hostname(host->mmc),
599 + bcm2835_sdhost_read(host, SDEDM));
600 + pr_info("%s: SDHCFG 0x%08x\n",
601 + mmc_hostname(host->mmc),
602 + bcm2835_sdhost_read(host, SDHCFG));
603 + pr_info("%s: SDHBCT 0x%08x\n",
604 + mmc_hostname(host->mmc),
605 + bcm2835_sdhost_read(host, SDHBCT));
606 + pr_info("%s: SDHBLC 0x%08x\n",
607 + mmc_hostname(host->mmc),
608 + bcm2835_sdhost_read(host, SDHBLC));
610 + pr_info("%s: ===========================================\n",
611 + mmc_hostname(host->mmc));
614 +static void bcm2835_sdhost_set_power(struct bcm2835_host *host, bool on)
616 + bcm2835_sdhost_write(host, on ? 1 : 0, SDVDD);
619 +static void bcm2835_sdhost_reset_internal(struct bcm2835_host *host)
624 + pr_info("%s: reset\n", mmc_hostname(host->mmc));
626 + bcm2835_sdhost_set_power(host, false);
628 + bcm2835_sdhost_write(host, 0, SDCMD);
629 + bcm2835_sdhost_write(host, 0, SDARG);
630 + bcm2835_sdhost_write(host, 0xf00000, SDTOUT);
631 + bcm2835_sdhost_write(host, 0, SDCDIV);
632 + bcm2835_sdhost_write(host, 0x7f8, SDHSTS); /* Write 1s to clear */
633 + bcm2835_sdhost_write(host, 0, SDHCFG);
634 + bcm2835_sdhost_write(host, 0, SDHBCT);
635 + bcm2835_sdhost_write(host, 0, SDHBLC);
637 + /* Limit fifo usage due to silicon bug */
638 + temp = bcm2835_sdhost_read(host, SDEDM);
639 + temp &= ~((SDEDM_THRESHOLD_MASK<<SDEDM_READ_THRESHOLD_SHIFT) |
640 + (SDEDM_THRESHOLD_MASK<<SDEDM_WRITE_THRESHOLD_SHIFT));
641 + temp |= (FIFO_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |
642 + (FIFO_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);
643 + bcm2835_sdhost_write(host, temp, SDEDM);
645 + bcm2835_sdhost_set_power(host, true);
649 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
650 + bcm2835_sdhost_write(host, SDCDIV_MAX_CDIV, SDCDIV);
654 +static void bcm2835_sdhost_reset(struct mmc_host *mmc)
656 + struct bcm2835_host *host = mmc_priv(mmc);
657 + unsigned long flags;
658 + spin_lock_irqsave(&host->lock, flags);
659 + log_event("RST<", 0, 0);
661 + bcm2835_sdhost_reset_internal(host);
663 + spin_unlock_irqrestore(&host->lock, flags);
666 +static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
668 +static void bcm2835_sdhost_init(struct bcm2835_host *host, int soft)
670 + pr_debug("bcm2835_sdhost_init(%d)\n", soft);
672 + /* Set interrupt enables */
673 + host->hcfg = SDHCFG_BUSY_IRPT_EN;
675 + bcm2835_sdhost_reset_internal(host);
678 + /* force clock reconfiguration */
680 + bcm2835_sdhost_set_ios(host->mmc, &host->mmc->ios);
684 +static void bcm2835_sdhost_wait_transfer_complete(struct bcm2835_host *host)
687 + u32 alternate_idle;
690 + alternate_idle = (host->mrq->data->flags & MMC_DATA_READ) ?
691 + SDEDM_FSM_READWAIT : SDEDM_FSM_WRITESTART1;
693 + edm = bcm2835_sdhost_read(host, SDEDM);
695 + log_event("WTC<", edm, 0);
700 + u32 fsm = edm & SDEDM_FSM_MASK;
701 + if ((fsm == SDEDM_FSM_IDENTMODE) ||
702 + (fsm == SDEDM_FSM_DATAMODE))
704 + if (fsm == alternate_idle) {
705 + bcm2835_sdhost_write(host,
706 + edm | SDEDM_FORCE_DATA_MODE,
712 + if (timediff == 100000) {
713 + pr_err("%s: wait_transfer_complete - still waiting after %d retries\n",
714 + mmc_hostname(host->mmc),
717 + bcm2835_sdhost_dumpregs(host);
718 + host->mrq->data->error = -ETIMEDOUT;
719 + log_event("WTC!", edm, 0);
723 + edm = bcm2835_sdhost_read(host, SDEDM);
725 + log_event("WTC>", edm, 0);
728 +static void bcm2835_sdhost_finish_data(struct bcm2835_host *host);
730 +static void bcm2835_sdhost_dma_complete(void *param)
732 + struct bcm2835_host *host = param;
733 + struct mmc_data *data = host->data;
734 + unsigned long flags;
736 + spin_lock_irqsave(&host->lock, flags);
737 + log_event("DMA<", (u32)host->data, bcm2835_sdhost_read(host, SDHSTS));
738 + log_event("DMA ", bcm2835_sdhost_read(host, SDCMD),
739 + bcm2835_sdhost_read(host, SDEDM));
741 + if (host->dma_chan) {
742 + dma_unmap_sg(host->dma_chan->device->dev,
743 + data->sg, data->sg_len,
746 + host->dma_chan = NULL;
749 + if (host->drain_words) {
753 + page = kmap_atomic(host->drain_page);
754 + buf = page + host->drain_offset;
756 + while (host->drain_words) {
757 + u32 edm = bcm2835_sdhost_read(host, SDEDM);
758 + if ((edm >> 4) & 0x1f)
759 + *(buf++) = bcm2835_sdhost_read(host,
761 + host->drain_words--;
764 + kunmap_atomic(page);
767 + bcm2835_sdhost_finish_data(host);
769 + log_event("DMA>", (u32)host->data, 0);
770 + spin_unlock_irqrestore(&host->lock, flags);
773 +static void bcm2835_sdhost_read_block_pio(struct bcm2835_host *host)
775 + unsigned long flags;
776 + size_t blksize, len;
778 + unsigned long wait_max;
780 + blksize = host->data->blksz;
782 + wait_max = jiffies + msecs_to_jiffies(host->pio_timeout);
784 + local_irq_save(flags);
790 + if (!sg_miter_next(&host->sg_miter)) {
791 + host->data->error = -EINVAL;
795 + len = min(host->sg_miter.length, blksize);
797 + host->data->error = -EINVAL;
802 + host->sg_miter.consumed = len;
804 + buf = (u32 *)host->sg_miter.addr;
806 + copy_words = len/4;
808 + while (copy_words) {
809 + int burst_words, words;
812 + burst_words = SDDATA_FIFO_PIO_BURST;
813 + if (burst_words > copy_words)
814 + burst_words = copy_words;
815 + edm = bcm2835_sdhost_read(host, SDEDM);
816 + words = ((edm >> 4) & 0x1f);
818 + if (words < burst_words) {
819 + int fsm_state = (edm & SDEDM_FSM_MASK);
820 + if ((fsm_state != SDEDM_FSM_READDATA) &&
821 + (fsm_state != SDEDM_FSM_READWAIT) &&
822 + (fsm_state != SDEDM_FSM_READCRC)) {
823 + hsts = bcm2835_sdhost_read(host,
825 + pr_info("%s: fsm %x, hsts %x\n",
826 + mmc_hostname(host->mmc),
828 + if (hsts & SDHSTS_ERROR_MASK)
832 + if (time_after(jiffies, wait_max)) {
833 + pr_err("%s: PIO read timeout - EDM %x\n",
834 + mmc_hostname(host->mmc),
836 + hsts = SDHSTS_REW_TIME_OUT;
839 + ndelay((burst_words - words) *
840 + host->ns_per_fifo_word);
842 + } else if (words > copy_words) {
843 + words = copy_words;
846 + copy_words -= words;
849 + *(buf++) = bcm2835_sdhost_read(host, SDDATA);
854 + if (hsts & SDHSTS_ERROR_MASK)
858 + sg_miter_stop(&host->sg_miter);
860 + local_irq_restore(flags);
863 +static void bcm2835_sdhost_write_block_pio(struct bcm2835_host *host)
865 + unsigned long flags;
866 + size_t blksize, len;
868 + unsigned long wait_max;
870 + blksize = host->data->blksz;
872 + wait_max = jiffies + msecs_to_jiffies(host->pio_timeout);
874 + local_irq_save(flags);
880 + if (!sg_miter_next(&host->sg_miter)) {
881 + host->data->error = -EINVAL;
885 + len = min(host->sg_miter.length, blksize);
887 + host->data->error = -EINVAL;
892 + host->sg_miter.consumed = len;
894 + buf = (u32 *)host->sg_miter.addr;
896 + copy_words = len/4;
898 + while (copy_words) {
899 + int burst_words, words;
902 + burst_words = SDDATA_FIFO_PIO_BURST;
903 + if (burst_words > copy_words)
904 + burst_words = copy_words;
905 + edm = bcm2835_sdhost_read(host, SDEDM);
906 + words = SDDATA_FIFO_WORDS - ((edm >> 4) & 0x1f);
908 + if (words < burst_words) {
909 + int fsm_state = (edm & SDEDM_FSM_MASK);
910 + if ((fsm_state != SDEDM_FSM_WRITEDATA) &&
911 + (fsm_state != SDEDM_FSM_WRITESTART1) &&
912 + (fsm_state != SDEDM_FSM_WRITESTART2)) {
913 + hsts = bcm2835_sdhost_read(host,
915 + pr_info("%s: fsm %x, hsts %x\n",
916 + mmc_hostname(host->mmc),
918 + if (hsts & SDHSTS_ERROR_MASK)
922 + if (time_after(jiffies, wait_max)) {
923 + pr_err("%s: PIO write timeout - EDM %x\n",
924 + mmc_hostname(host->mmc),
926 + hsts = SDHSTS_REW_TIME_OUT;
929 + ndelay((burst_words - words) *
930 + host->ns_per_fifo_word);
932 + } else if (words > copy_words) {
933 + words = copy_words;
936 + copy_words -= words;
939 + bcm2835_sdhost_write(host, *(buf++), SDDATA);
944 + if (hsts & SDHSTS_ERROR_MASK)
948 + sg_miter_stop(&host->sg_miter);
950 + local_irq_restore(flags);
953 +static void bcm2835_sdhost_transfer_pio(struct bcm2835_host *host)
957 + BUG_ON(!host->data);
958 + log_event("XFP<", (u32)host->data, host->blocks);
960 + is_read = (host->data->flags & MMC_DATA_READ) != 0;
962 + bcm2835_sdhost_read_block_pio(host);
964 + bcm2835_sdhost_write_block_pio(host);
966 + sdhsts = bcm2835_sdhost_read(host, SDHSTS);
967 + if (sdhsts & (SDHSTS_CRC16_ERROR |
968 + SDHSTS_CRC7_ERROR |
969 + SDHSTS_FIFO_ERROR)) {
970 + pr_err("%s: %s transfer error - HSTS %x\n",
971 + mmc_hostname(host->mmc),
972 + is_read ? "read" : "write",
974 + host->data->error = -EILSEQ;
975 + } else if ((sdhsts & (SDHSTS_CMD_TIME_OUT |
976 + SDHSTS_REW_TIME_OUT))) {
977 + pr_err("%s: %s timeout error - HSTS %x\n",
978 + mmc_hostname(host->mmc),
979 + is_read ? "read" : "write",
981 + host->data->error = -ETIMEDOUT;
983 + log_event("XFP>", (u32)host->data, host->blocks);
986 +static void bcm2835_sdhost_prepare_dma(struct bcm2835_host *host,
987 + struct mmc_data *data)
989 + int len, dir_data, dir_slave;
990 + struct dma_async_tx_descriptor *desc = NULL;
991 + struct dma_chan *dma_chan;
993 + log_event("PRD<", (u32)data, 0);
994 + pr_debug("bcm2835_sdhost_prepare_dma()\n");
996 + dma_chan = host->dma_chan_rxtx;
997 + if (data->flags & MMC_DATA_READ) {
998 + dir_data = DMA_FROM_DEVICE;
999 + dir_slave = DMA_DEV_TO_MEM;
1001 + dir_data = DMA_TO_DEVICE;
1002 + dir_slave = DMA_MEM_TO_DEV;
1004 + log_event("PRD1", (u32)dma_chan, 0);
1006 + BUG_ON(!dma_chan->device);
1007 + BUG_ON(!dma_chan->device->dev);
1008 + BUG_ON(!data->sg);
1010 + /* The block doesn't manage the FIFO DREQs properly for multi-block
1011 + transfers, so don't attempt to DMA the final few words.
1012 + Unfortunately this requires the final sg entry to be trimmed.
1013 + N.B. This code demands that the overspill is contained in
1014 + a single sg entry.
1017 + host->drain_words = 0;
1018 + if ((data->blocks > 1) && (dir_data == DMA_FROM_DEVICE)) {
1019 + struct scatterlist *sg;
1023 + len = min((u32)(FIFO_READ_THRESHOLD - 1) * 4,
1024 + (u32)data->blocks * data->blksz);
1026 + for_each_sg(data->sg, sg, data->sg_len, i) {
1027 + if (sg_is_last(sg)) {
1028 + BUG_ON(sg->length < len);
1029 + sg->length -= len;
1030 + host->drain_page = sg_page(sg);
1031 + host->drain_offset = sg->offset + sg->length;
1034 + host->drain_words = len/4;
1037 + /* The parameters have already been validated, so this will not fail */
1038 + (void)dmaengine_slave_config(dma_chan,
1039 + (dir_data == DMA_FROM_DEVICE) ?
1040 + &host->dma_cfg_rx :
1041 + &host->dma_cfg_tx);
1043 + len = dma_map_sg(dma_chan->device->dev, data->sg, data->sg_len,
1046 + log_event("PRD2", len, 0);
1048 + desc = dmaengine_prep_slave_sg(dma_chan, data->sg,
1050 + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1051 + log_event("PRD3", (u32)desc, 0);
1054 + desc->callback = bcm2835_sdhost_dma_complete;
1055 + desc->callback_param = host;
1056 + host->dma_desc = desc;
1057 + host->dma_chan = dma_chan;
1058 + host->dma_dir = dir_data;
1060 + log_event("PDM>", (u32)data, 0);
1063 +static void bcm2835_sdhost_start_dma(struct bcm2835_host *host)
1065 + log_event("SDMA", (u32)host->data, (u32)host->dma_chan);
1066 + dmaengine_submit(host->dma_desc);
1067 + dma_async_issue_pending(host->dma_chan);
1070 +static void bcm2835_sdhost_set_transfer_irqs(struct bcm2835_host *host)
1072 + u32 all_irqs = SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN |
1073 + SDHCFG_BUSY_IRPT_EN;
1074 + if (host->dma_desc)
1075 + host->hcfg = (host->hcfg & ~all_irqs) |
1076 + SDHCFG_BUSY_IRPT_EN;
1078 + host->hcfg = (host->hcfg & ~all_irqs) |
1079 + SDHCFG_DATA_IRPT_EN |
1080 + SDHCFG_BUSY_IRPT_EN;
1082 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1085 +static void bcm2835_sdhost_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
1087 + struct mmc_data *data = cmd->data;
1089 + WARN_ON(host->data);
1091 + host->data = data;
1095 + /* Sanity checks */
1096 + BUG_ON(data->blksz * data->blocks > 524288);
1097 + BUG_ON(data->blksz > host->mmc->max_blk_size);
1098 + BUG_ON(data->blocks > 65535);
1100 + host->data_complete = 0;
1101 + host->flush_fifo = 0;
1102 + host->data->bytes_xfered = 0;
1104 + if (!host->sectors && host->mmc->card) {
1105 + struct mmc_card *card = host->mmc->card;
1106 + if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1108 + * The EXT_CSD sector count is in number of 512 byte
1111 + host->sectors = card->ext_csd.sectors;
1114 + * The CSD capacity field is in units of read_blkbits.
1115 + * set_capacity takes units of 512 bytes.
1117 + host->sectors = card->csd.capacity <<
1118 + (card->csd.read_blkbits - 9);
1122 + if (!host->dma_desc) {
1124 + int flags = SG_MITER_ATOMIC;
1126 + if (data->flags & MMC_DATA_READ)
1127 + flags |= SG_MITER_TO_SG;
1129 + flags |= SG_MITER_FROM_SG;
1130 + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
1131 + host->blocks = data->blocks;
1134 + bcm2835_sdhost_set_transfer_irqs(host);
1136 + bcm2835_sdhost_write(host, data->blksz, SDHBCT);
1137 + bcm2835_sdhost_write(host, data->blocks, SDHBLC);
1139 + BUG_ON(!host->data);
1142 +bool bcm2835_sdhost_send_command(struct bcm2835_host *host,
1143 + struct mmc_command *cmd)
1145 + u32 sdcmd, sdhsts;
1146 + unsigned long timeout;
1149 + WARN_ON(host->cmd);
1150 + log_event("CMD<", cmd->opcode, cmd->arg);
1153 + pr_debug("%s: send_command %d 0x%x "
1154 + "(flags 0x%x) - %s %d*%d\n",
1155 + mmc_hostname(host->mmc),
1156 + cmd->opcode, cmd->arg, cmd->flags,
1157 + (cmd->data->flags & MMC_DATA_READ) ?
1158 + "read" : "write", cmd->data->blocks,
1159 + cmd->data->blksz);
1161 + pr_debug("%s: send_command %d 0x%x (flags 0x%x)\n",
1162 + mmc_hostname(host->mmc),
1163 + cmd->opcode, cmd->arg, cmd->flags);
1165 + /* Wait max 100 ms */
1168 + while (bcm2835_sdhost_read(host, SDCMD) & SDCMD_NEW_FLAG) {
1169 + if (timeout == 0) {
1170 + pr_warn("%s: previous command never completed.\n",
1171 + mmc_hostname(host->mmc));
1173 + bcm2835_sdhost_dumpregs(host);
1174 + cmd->error = -EILSEQ;
1175 + tasklet_schedule(&host->finish_tasklet);
1182 + delay = (10000 - timeout)/100;
1183 + if (delay > host->max_delay) {
1184 + host->max_delay = delay;
1185 + pr_warning("%s: controller hung for %d ms\n",
1186 + mmc_hostname(host->mmc),
1190 + timeout = jiffies;
1191 + if (!cmd->data && cmd->busy_timeout > 9000)
1192 + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1194 + timeout += 10 * HZ;
1195 + mod_timer(&host->timer, timeout);
1199 + /* Clear any error flags */
1200 + sdhsts = bcm2835_sdhost_read(host, SDHSTS);
1201 + if (sdhsts & SDHSTS_ERROR_MASK)
1202 + bcm2835_sdhost_write(host, sdhsts, SDHSTS);
1204 + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1205 + pr_err("%s: unsupported response type!\n",
1206 + mmc_hostname(host->mmc));
1207 + cmd->error = -EINVAL;
1208 + tasklet_schedule(&host->finish_tasklet);
1212 + bcm2835_sdhost_prepare_data(host, cmd);
1214 + bcm2835_sdhost_write(host, cmd->arg, SDARG);
1216 + sdcmd = cmd->opcode & SDCMD_CMD_MASK;
1218 + host->use_busy = 0;
1219 + if (!(cmd->flags & MMC_RSP_PRESENT)) {
1220 + sdcmd |= SDCMD_NO_RESPONSE;
1222 + if (cmd->flags & MMC_RSP_136)
1223 + sdcmd |= SDCMD_LONG_RESPONSE;
1224 + if (cmd->flags & MMC_RSP_BUSY) {
1225 + sdcmd |= SDCMD_BUSYWAIT;
1226 + host->use_busy = 1;
1231 + log_event("CMDD", cmd->data->blocks, cmd->data->blksz);
1232 + if (host->delay_after_this_stop) {
1233 + struct timeval now;
1234 + int time_since_stop;
1235 + do_gettimeofday(&now);
1236 + time_since_stop = (now.tv_sec - host->stop_time.tv_sec);
1237 + if (time_since_stop < 2) {
1238 + /* Possibly less than one second */
1239 + time_since_stop = time_since_stop * 1000000 +
1240 + (now.tv_usec - host->stop_time.tv_usec);
1241 + if (time_since_stop <
1242 + host->delay_after_this_stop)
1243 + udelay(host->delay_after_this_stop -
1248 + host->delay_after_this_stop = host->delay_after_stop;
1249 + if ((cmd->data->flags & MMC_DATA_READ) && !host->use_sbc) {
1250 + /* See if read crosses one of the hazardous sectors */
1251 + u32 first_blk, last_blk;
1253 + /* Intentionally include the following sector because
1254 + without CMD23/SBC the read may run on. */
1255 + first_blk = host->mrq->cmd->arg;
1256 + last_blk = first_blk + cmd->data->blocks;
1258 + if (((last_blk >= (host->sectors - 64)) &&
1259 + (first_blk <= (host->sectors - 64))) ||
1260 + ((last_blk >= (host->sectors - 32)) &&
1261 + (first_blk <= (host->sectors - 32)))) {
1262 + host->delay_after_this_stop =
1263 + max(250u, host->delay_after_stop);
1267 + if (cmd->data->flags & MMC_DATA_WRITE)
1268 + sdcmd |= SDCMD_WRITE_CMD;
1269 + if (cmd->data->flags & MMC_DATA_READ)
1270 + sdcmd |= SDCMD_READ_CMD;
1273 + bcm2835_sdhost_write(host, sdcmd | SDCMD_NEW_FLAG, SDCMD);
1278 +static void bcm2835_sdhost_finish_command(struct bcm2835_host *host,
1279 + unsigned long *irq_flags);
1280 +static void bcm2835_sdhost_transfer_complete(struct bcm2835_host *host);
1282 +static void bcm2835_sdhost_finish_data(struct bcm2835_host *host)
1284 + struct mmc_data *data;
1286 + data = host->data;
1289 + log_event("FDA<", (u32)host->mrq, (u32)host->cmd);
1290 + pr_debug("finish_data(error %d, stop %d, sbc %d)\n",
1291 + data->error, data->stop ? 1 : 0,
1292 + host->mrq->sbc ? 1 : 0);
1294 + host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
1295 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1297 + data->bytes_xfered = data->error ? 0 : (data->blksz * data->blocks);
1299 + host->data_complete = 1;
1303 + * Data managed to finish before the
1304 + * command completed. Make sure we do
1305 + * things in the proper order.
1307 + pr_debug("Finished early - HSTS %x\n",
1308 + bcm2835_sdhost_read(host, SDHSTS));
1311 + bcm2835_sdhost_transfer_complete(host);
1312 + log_event("FDA>", (u32)host->mrq, (u32)host->cmd);
1315 +static void bcm2835_sdhost_transfer_complete(struct bcm2835_host *host)
1317 + struct mmc_data *data;
1319 + BUG_ON(host->cmd);
1320 + BUG_ON(!host->data);
1321 + BUG_ON(!host->data_complete);
1323 + data = host->data;
1324 + host->data = NULL;
1326 + log_event("TCM<", (u32)data, data->error);
1327 + pr_debug("transfer_complete(error %d, stop %d)\n",
1328 + data->error, data->stop ? 1 : 0);
1331 + * Need to send CMD12 if -
1332 + * a) open-ended multiblock transfer (no CMD23)
1333 + * b) error in multiblock transfer
1335 + if (host->mrq->stop && (data->error || !host->use_sbc)) {
1336 + if (bcm2835_sdhost_send_command(host, host->mrq->stop)) {
1337 + /* No busy, so poll for completion */
1338 + if (!host->use_busy)
1339 + bcm2835_sdhost_finish_command(host, NULL);
1341 + if (host->delay_after_this_stop)
1342 + do_gettimeofday(&host->stop_time);
1345 + bcm2835_sdhost_wait_transfer_complete(host);
1346 + tasklet_schedule(&host->finish_tasklet);
1348 + log_event("TCM>", (u32)data, 0);
1351 +/* If irq_flags is valid, the caller is in a thread context and is allowed
1353 +static void bcm2835_sdhost_finish_command(struct bcm2835_host *host,
1354 + unsigned long *irq_flags)
1359 + struct timeval before, after;
1363 + log_event("FCM<", (u32)host->mrq, (u32)host->cmd);
1364 + pr_debug("finish_command(%x)\n", bcm2835_sdhost_read(host, SDCMD));
1366 + BUG_ON(!host->cmd || !host->mrq);
1368 + /* Poll quickly at first */
1370 + retries = host->cmd_quick_poll_retries;
1372 + /* Work out how many polls take 1us by timing 10us */
1373 + struct timeval start, now;
1382 + do_gettimeofday(&start);
1384 + for (i = 0; i < retries; i++) {
1386 + sdcmd = bcm2835_sdhost_read(host, SDCMD);
1389 + do_gettimeofday(&now);
1390 + us_diff = (now.tv_sec - start.tv_sec) * 1000000 +
1391 + (now.tv_usec - start.tv_usec);
1392 + } while (us_diff < 10);
1394 + host->cmd_quick_poll_retries = ((retries * us_diff + 9)*CMD_DALLY_US)/10 + 1;
1395 + retries = 1; // We've already waited long enough this time
1398 + for (sdcmd = bcm2835_sdhost_read(host, SDCMD);
1399 + (sdcmd & SDCMD_NEW_FLAG) && retries;
1402 + sdcmd = bcm2835_sdhost_read(host, SDCMD);
1406 + unsigned long wait_max;
1409 + /* Schedule the work */
1410 + log_event("CWWQ", 0, 0);
1411 + schedule_work(&host->cmd_wait_wq);
1415 + /* Wait max 100 ms */
1416 + wait_max = jiffies + msecs_to_jiffies(100);
1417 + while (time_before(jiffies, wait_max)) {
1418 + spin_unlock_irqrestore(&host->lock, *irq_flags);
1419 + usleep_range(1, 10);
1420 + spin_lock_irqsave(&host->lock, *irq_flags);
1421 + sdcmd = bcm2835_sdhost_read(host, SDCMD);
1422 + if (!(sdcmd & SDCMD_NEW_FLAG))
1427 + /* Check for errors */
1428 + if (sdcmd & SDCMD_NEW_FLAG) {
1429 + if (host->debug) {
1430 + pr_err("%s: command %d never completed.\n",
1431 + mmc_hostname(host->mmc), host->cmd->opcode);
1432 + bcm2835_sdhost_dumpregs(host);
1434 + host->cmd->error = -EILSEQ;
1435 + tasklet_schedule(&host->finish_tasklet);
1437 + } else if (sdcmd & SDCMD_FAIL_FLAG) {
1438 + u32 sdhsts = bcm2835_sdhost_read(host, SDHSTS);
1440 + /* Clear the errors */
1441 + bcm2835_sdhost_write(host, SDHSTS_ERROR_MASK, SDHSTS);
1444 + pr_info("%s: error detected - CMD %x, HSTS %03x, EDM %x\n",
1445 + mmc_hostname(host->mmc), sdcmd, sdhsts,
1446 + bcm2835_sdhost_read(host, SDEDM));
1448 + if ((sdhsts & SDHSTS_CRC7_ERROR) &&
1449 + (host->cmd->opcode == 1)) {
1451 + pr_info("%s: ignoring CRC7 error for CMD1\n",
1452 + mmc_hostname(host->mmc));
1454 + if (sdhsts & SDHSTS_CMD_TIME_OUT) {
1456 + pr_warn("%s: command %d timeout\n",
1457 + mmc_hostname(host->mmc),
1458 + host->cmd->opcode);
1459 + host->cmd->error = -ETIMEDOUT;
1461 + pr_warn("%s: unexpected command %d error\n",
1462 + mmc_hostname(host->mmc),
1463 + host->cmd->opcode);
1464 + host->cmd->error = -EILSEQ;
1466 + tasklet_schedule(&host->finish_tasklet);
1471 + if (host->cmd->flags & MMC_RSP_PRESENT) {
1472 + if (host->cmd->flags & MMC_RSP_136) {
1474 + for (i = 0; i < 4; i++)
1475 + host->cmd->resp[3 - i] = bcm2835_sdhost_read(host, SDRSP0 + i*4);
1476 + pr_debug("%s: finish_command %08x %08x %08x %08x\n",
1477 + mmc_hostname(host->mmc),
1478 + host->cmd->resp[0], host->cmd->resp[1], host->cmd->resp[2], host->cmd->resp[3]);
1479 + log_event("RSP ", host->cmd->resp[0], host->cmd->resp[1]);
1481 + host->cmd->resp[0] = bcm2835_sdhost_read(host, SDRSP0);
1482 + pr_debug("%s: finish_command %08x\n",
1483 + mmc_hostname(host->mmc),
1484 + host->cmd->resp[0]);
1485 + log_event("RSP ", host->cmd->resp[0], 0);
1489 + if (host->cmd == host->mrq->sbc) {
1490 + /* Finished CMD23, now send actual command. */
1492 + if (bcm2835_sdhost_send_command(host, host->mrq->cmd)) {
1493 + if (host->data && host->dma_desc)
1494 + /* DMA transfer starts now, PIO starts after irq */
1495 + bcm2835_sdhost_start_dma(host);
1497 + if (!host->use_busy)
1498 + bcm2835_sdhost_finish_command(host, NULL);
1500 + } else if (host->cmd == host->mrq->stop) {
1501 + /* Finished CMD12 */
1502 + tasklet_schedule(&host->finish_tasklet);
1504 + /* Processed actual command. */
1507 + tasklet_schedule(&host->finish_tasklet);
1508 + else if (host->data_complete)
1509 + bcm2835_sdhost_transfer_complete(host);
1511 + log_event("FCM>", (u32)host->mrq, (u32)host->cmd);
1514 +static void bcm2835_sdhost_timeout(unsigned long data)
1516 + struct bcm2835_host *host;
1517 + unsigned long flags;
1519 + host = (struct bcm2835_host *)data;
1521 + spin_lock_irqsave(&host->lock, flags);
1522 + log_event("TIM<", 0, 0);
1525 + pr_err("%s: timeout waiting for hardware interrupt.\n",
1526 + mmc_hostname(host->mmc));
1528 + bcm2835_sdhost_dumpregs(host);
1531 + host->data->error = -ETIMEDOUT;
1532 + bcm2835_sdhost_finish_data(host);
1535 + host->cmd->error = -ETIMEDOUT;
1537 + host->mrq->cmd->error = -ETIMEDOUT;
1539 + pr_debug("timeout_timer tasklet_schedule\n");
1540 + tasklet_schedule(&host->finish_tasklet);
1545 + spin_unlock_irqrestore(&host->lock, flags);
1548 +static void bcm2835_sdhost_busy_irq(struct bcm2835_host *host, u32 intmask)
1550 + log_event("IRQB", (u32)host->cmd, intmask);
1552 + pr_err("%s: got command busy interrupt 0x%08x even "
1553 + "though no command operation was in progress.\n",
1554 + mmc_hostname(host->mmc), (unsigned)intmask);
1555 + bcm2835_sdhost_dumpregs(host);
1559 + if (!host->use_busy) {
1560 + pr_err("%s: got command busy interrupt 0x%08x even "
1561 + "though not expecting one.\n",
1562 + mmc_hostname(host->mmc), (unsigned)intmask);
1563 + bcm2835_sdhost_dumpregs(host);
1566 + host->use_busy = 0;
1568 + if (intmask & SDHSTS_ERROR_MASK)
1570 + pr_err("sdhost_busy_irq: intmask %x, data %p\n", intmask, host->mrq->data);
1571 + if (intmask & SDHSTS_CRC7_ERROR)
1572 + host->cmd->error = -EILSEQ;
1573 + else if (intmask & (SDHSTS_CRC16_ERROR |
1574 + SDHSTS_FIFO_ERROR)) {
1575 + if (host->mrq->data)
1576 + host->mrq->data->error = -EILSEQ;
1578 + host->cmd->error = -EILSEQ;
1579 + } else if (intmask & SDHSTS_REW_TIME_OUT) {
1580 + if (host->mrq->data)
1581 + host->mrq->data->error = -ETIMEDOUT;
1583 + host->cmd->error = -ETIMEDOUT;
1584 + } else if (intmask & SDHSTS_CMD_TIME_OUT)
1585 + host->cmd->error = -ETIMEDOUT;
1587 + if (host->debug) {
1589 + bcm2835_sdhost_dumpregs(host);
1593 + bcm2835_sdhost_finish_command(host, NULL);
1596 +static void bcm2835_sdhost_data_irq(struct bcm2835_host *host, u32 intmask)
1598 + /* There are no dedicated data/space available interrupt
1599 + status bits, so it is necessary to use the single shared
1600 + data/space available FIFO status bits. It is therefore not
1601 + an error to get here when there is no data transfer in
1603 + log_event("IRQD", (u32)host->data, intmask);
1607 + if (intmask & (SDHSTS_CRC16_ERROR |
1608 + SDHSTS_FIFO_ERROR |
1609 + SDHSTS_REW_TIME_OUT)) {
1610 + if (intmask & (SDHSTS_CRC16_ERROR |
1611 + SDHSTS_FIFO_ERROR))
1612 + host->data->error = -EILSEQ;
1614 + host->data->error = -ETIMEDOUT;
1616 + if (host->debug) {
1618 + bcm2835_sdhost_dumpregs(host);
1622 + if (host->data->error) {
1623 + bcm2835_sdhost_finish_data(host);
1624 + } else if (host->data->flags & MMC_DATA_WRITE) {
1625 + /* Use the block interrupt for writes after the first block */
1626 + host->hcfg &= ~(SDHCFG_DATA_IRPT_EN);
1627 + host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
1628 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1629 + bcm2835_sdhost_transfer_pio(host);
1631 + bcm2835_sdhost_transfer_pio(host);
1633 + if ((host->blocks == 0) || host->data->error)
1634 + bcm2835_sdhost_finish_data(host);
1638 +static void bcm2835_sdhost_block_irq(struct bcm2835_host *host, u32 intmask)
1640 + log_event("IRQK", (u32)host->data, intmask);
1641 + if (!host->data) {
1642 + pr_err("%s: got block interrupt 0x%08x even "
1643 + "though no data operation was in progress.\n",
1644 + mmc_hostname(host->mmc), (unsigned)intmask);
1645 + bcm2835_sdhost_dumpregs(host);
1649 + if (intmask & (SDHSTS_CRC16_ERROR |
1650 + SDHSTS_FIFO_ERROR |
1651 + SDHSTS_REW_TIME_OUT)) {
1652 + if (intmask & (SDHSTS_CRC16_ERROR |
1653 + SDHSTS_FIFO_ERROR))
1654 + host->data->error = -EILSEQ;
1656 + host->data->error = -ETIMEDOUT;
1658 + if (host->debug) {
1660 + bcm2835_sdhost_dumpregs(host);
1664 + if (!host->dma_desc) {
1665 + BUG_ON(!host->blocks);
1666 + if (host->data->error || (--host->blocks == 0)) {
1667 + bcm2835_sdhost_finish_data(host);
1669 + bcm2835_sdhost_transfer_pio(host);
1671 + } else if (host->data->flags & MMC_DATA_WRITE) {
1672 + bcm2835_sdhost_finish_data(host);
1676 +static irqreturn_t bcm2835_sdhost_irq(int irq, void *dev_id)
1678 + irqreturn_t result = IRQ_NONE;
1679 + struct bcm2835_host *host = dev_id;
1682 + spin_lock(&host->lock);
1684 + intmask = bcm2835_sdhost_read(host, SDHSTS);
1685 + log_event("IRQ<", intmask, 0);
1687 + bcm2835_sdhost_write(host,
1688 + SDHSTS_BUSY_IRPT |
1689 + SDHSTS_BLOCK_IRPT |
1690 + SDHSTS_SDIO_IRPT |
1694 + if (intmask & SDHSTS_BLOCK_IRPT) {
1695 + bcm2835_sdhost_block_irq(host, intmask);
1696 + result = IRQ_HANDLED;
1699 + if (intmask & SDHSTS_BUSY_IRPT) {
1700 + bcm2835_sdhost_busy_irq(host, intmask);
1701 + result = IRQ_HANDLED;
1704 + /* There is no true data interrupt status bit, so it is
1705 + necessary to qualify the data flag with the interrupt
1707 + if ((intmask & SDHSTS_DATA_FLAG) &&
1708 + (host->hcfg & SDHCFG_DATA_IRPT_EN)) {
1709 + bcm2835_sdhost_data_irq(host, intmask);
1710 + result = IRQ_HANDLED;
1715 + log_event("IRQ>", bcm2835_sdhost_read(host, SDHSTS), 0);
1716 + spin_unlock(&host->lock);
1721 +void bcm2835_sdhost_set_clock(struct bcm2835_host *host, unsigned int clock)
1723 + int div = 0; /* Initialized for compiler warning */
1724 + unsigned int input_clock = clock;
1725 + unsigned long flags;
1728 + pr_info("%s: set_clock(%d)\n", mmc_hostname(host->mmc), clock);
1730 + if ((host->overclock_50 > 50) &&
1731 + (clock == 50*MHZ))
1732 + clock = host->overclock_50 * MHZ + (MHZ - 1);
1734 + /* The SDCDIV register has 11 bits, and holds (div - 2).
1735 + But in data mode the max is 50MHz wihout a minimum, and only the
1736 + bottom 3 bits are used. Since the switch over is automatic (unless
1737 + we have marked the card as slow...), chosen values have to make
1738 + sense in both modes.
1739 + Ident mode must be 100-400KHz, so can range check the requested
1740 + clock. CMD15 must be used to return to data mode, so this can be
1743 + clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz
1744 + 4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz
1746 + 623->400KHz/27.8MHz
1747 + reset value (507)->491159/50MHz
1749 + BUT, the 3-bit clock divisor in data mode is too small if the
1750 + core clock is higher than 250MHz, so instead use the SLOW_CARD
1751 + configuration bit to force the use of the ident clock divisor
1755 + host->mmc->actual_clock = 0;
1757 + if (host->firmware_sets_cdiv) {
1758 + u32 msg[3] = { clock, 0, 0 };
1760 + rpi_firmware_property(rpi_firmware_get(NULL),
1761 + RPI_FIRMWARE_SET_SDHOST_CLOCK,
1762 + &msg, sizeof(msg));
1764 + clock = max(msg[1], msg[2]);
1765 + spin_lock_irqsave(&host->lock, flags);
1767 + spin_lock_irqsave(&host->lock, flags);
1768 + if (clock < 100000) {
1769 + /* Can't stop the clock, but make it as slow as
1770 + * possible to show willing
1772 + host->cdiv = SDCDIV_MAX_CDIV;
1773 + bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
1775 + spin_unlock_irqrestore(&host->lock, flags);
1779 + div = host->max_clk / clock;
1782 + if ((host->max_clk / div) > clock)
1786 + if (div > SDCDIV_MAX_CDIV)
1787 + div = SDCDIV_MAX_CDIV;
1789 + clock = host->max_clk / (div + 2);
1792 + bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
1795 + pr_info("%s: clock=%d -> max_clk=%d, cdiv=%x "
1796 + "(actual clock %d)\n",
1797 + mmc_hostname(host->mmc), input_clock,
1798 + host->max_clk, host->cdiv,
1802 + /* Calibrate some delays */
1804 + host->ns_per_fifo_word = (1000000000/clock) *
1805 + ((host->mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
1807 + if (input_clock == 50 * MHZ) {
1808 + if (clock > input_clock) {
1809 + /* Save the closest value, to make it easier
1810 + to reduce in the event of error */
1811 + host->overclock_50 = (clock/MHZ);
1813 + if (clock != host->overclock) {
1814 + pr_info("%s: overclocking to %dHz\n",
1815 + mmc_hostname(host->mmc), clock);
1816 + host->overclock = clock;
1818 + } else if (host->overclock) {
1819 + host->overclock = 0;
1820 + if (clock == 50 * MHZ)
1821 + pr_warn("%s: cancelling overclock\n",
1822 + mmc_hostname(host->mmc));
1824 + } else if (input_clock == 0) {
1825 + /* Reset the preferred overclock when the clock is stopped.
1826 + * This always happens during initialisation. */
1827 + host->overclock_50 = host->user_overclock_50;
1828 + host->overclock = 0;
1831 + /* Set the timeout to 500ms */
1832 + bcm2835_sdhost_write(host, clock/2, SDTOUT);
1834 + host->mmc->actual_clock = clock;
1835 + host->clock = input_clock;
1836 + host->reset_clock = 0;
1839 + spin_unlock_irqrestore(&host->lock, flags);
1842 +static void bcm2835_sdhost_request(struct mmc_host *mmc, struct mmc_request *mrq)
1844 + struct bcm2835_host *host;
1845 + unsigned long flags;
1848 + host = mmc_priv(mmc);
1850 + if (host->debug) {
1851 + struct mmc_command *cmd = mrq->cmd;
1854 + pr_info("%s: cmd %d 0x%x (flags 0x%x) - %s %d*%d\n",
1855 + mmc_hostname(mmc),
1856 + cmd->opcode, cmd->arg, cmd->flags,
1857 + (cmd->data->flags & MMC_DATA_READ) ?
1858 + "read" : "write", cmd->data->blocks,
1859 + cmd->data->blksz);
1861 + pr_info("%s: cmd %d 0x%x (flags 0x%x)\n",
1862 + mmc_hostname(mmc),
1863 + cmd->opcode, cmd->arg, cmd->flags);
1866 + /* Reset the error statuses in case this is a retry */
1868 + mrq->sbc->error = 0;
1870 + mrq->cmd->error = 0;
1872 + mrq->data->error = 0;
1874 + mrq->stop->error = 0;
1876 + if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
1877 + pr_err("%s: unsupported block size (%d bytes)\n",
1878 + mmc_hostname(mmc), mrq->data->blksz);
1879 + mrq->cmd->error = -EINVAL;
1880 + mmc_request_done(mmc, mrq);
1884 + if (host->use_dma && mrq->data &&
1885 + (mrq->data->blocks > host->pio_limit))
1886 + bcm2835_sdhost_prepare_dma(host, mrq->data);
1888 + if (host->reset_clock)
1889 + bcm2835_sdhost_set_clock(host, host->clock);
1891 + spin_lock_irqsave(&host->lock, flags);
1893 + WARN_ON(host->mrq != NULL);
1896 + edm = bcm2835_sdhost_read(host, SDEDM);
1897 + fsm = edm & SDEDM_FSM_MASK;
1899 + log_event("REQ<", (u32)mrq, edm);
1900 + if ((fsm != SDEDM_FSM_IDENTMODE) &&
1901 + (fsm != SDEDM_FSM_DATAMODE)) {
1902 + log_event("REQ!", (u32)mrq, edm);
1903 + if (host->debug) {
1904 + pr_warn("%s: previous command (%d) not complete (EDM %x)\n",
1905 + mmc_hostname(host->mmc),
1906 + bcm2835_sdhost_read(host, SDCMD) & SDCMD_CMD_MASK,
1909 + bcm2835_sdhost_dumpregs(host);
1911 + mrq->cmd->error = -EILSEQ;
1912 + tasklet_schedule(&host->finish_tasklet);
1914 + spin_unlock_irqrestore(&host->lock, flags);
1918 + host->use_sbc = !!mrq->sbc &&
1919 + (host->mrq->data->flags & USE_CMD23_FLAGS);
1920 + if (host->use_sbc) {
1921 + if (bcm2835_sdhost_send_command(host, mrq->sbc)) {
1922 + if (!host->use_busy)
1923 + bcm2835_sdhost_finish_command(host, &flags);
1925 + } else if (bcm2835_sdhost_send_command(host, mrq->cmd)) {
1926 + if (host->data && host->dma_desc)
1927 + /* DMA transfer starts now, PIO starts after irq */
1928 + bcm2835_sdhost_start_dma(host);
1930 + if (!host->use_busy)
1931 + bcm2835_sdhost_finish_command(host, &flags);
1934 + log_event("CMD ", (u32)mrq->cmd->opcode,
1935 + mrq->data ? (u32)mrq->data->blksz : 0);
1938 + log_event("REQ>", (u32)mrq, 0);
1939 + spin_unlock_irqrestore(&host->lock, flags);
1942 +static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1945 + struct bcm2835_host *host = mmc_priv(mmc);
1946 + unsigned long flags;
1949 + pr_info("%s: ios clock %d, pwr %d, bus_width %d, "
1950 + "timing %d, vdd %d, drv_type %d\n",
1951 + mmc_hostname(mmc),
1952 + ios->clock, ios->power_mode, ios->bus_width,
1953 + ios->timing, ios->signal_voltage, ios->drv_type);
1955 + spin_lock_irqsave(&host->lock, flags);
1957 + log_event("IOS<", ios->clock, 0);
1959 + /* set bus width */
1960 + host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
1961 + if (ios->bus_width == MMC_BUS_WIDTH_4)
1962 + host->hcfg |= SDHCFG_WIDE_EXT_BUS;
1964 + host->hcfg |= SDHCFG_WIDE_INT_BUS;
1966 + /* Disable clever clock switching, to cope with fast core clocks */
1967 + host->hcfg |= SDHCFG_SLOW_CARD;
1969 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1973 + spin_unlock_irqrestore(&host->lock, flags);
1975 + if (!ios->clock || ios->clock != host->clock)
1976 + bcm2835_sdhost_set_clock(host, ios->clock);
1979 +static struct mmc_host_ops bcm2835_sdhost_ops = {
1980 + .request = bcm2835_sdhost_request,
1981 + .set_ios = bcm2835_sdhost_set_ios,
1982 + .hw_reset = bcm2835_sdhost_reset,
1985 +static void bcm2835_sdhost_cmd_wait_work(struct work_struct *work)
1987 + struct bcm2835_host *host;
1988 + unsigned long flags;
1990 + host = container_of(work, struct bcm2835_host, cmd_wait_wq);
1992 + spin_lock_irqsave(&host->lock, flags);
1994 + log_event("CWK<", (u32)host->cmd, (u32)host->mrq);
1997 + * If this tasklet gets rescheduled while running, it will
1998 + * be run again afterwards but without any active request.
2001 + spin_unlock_irqrestore(&host->lock, flags);
2005 + bcm2835_sdhost_finish_command(host, &flags);
2009 + log_event("CWK>", (u32)host->cmd, 0);
2011 + spin_unlock_irqrestore(&host->lock, flags);
2014 +static void bcm2835_sdhost_tasklet_finish(unsigned long param)
2016 + struct bcm2835_host *host;
2017 + unsigned long flags;
2018 + struct mmc_request *mrq;
2019 + struct dma_chan *terminate_chan = NULL;
2021 + host = (struct bcm2835_host *)param;
2023 + spin_lock_irqsave(&host->lock, flags);
2025 + log_event("TSK<", (u32)host->mrq, 0);
2027 + * If this tasklet gets rescheduled while running, it will
2028 + * be run again afterwards but without any active request.
2031 + spin_unlock_irqrestore(&host->lock, flags);
2035 + del_timer(&host->timer);
2039 + /* Drop the overclock after any data corruption, or after any
2040 + * error while overclocked. Ignore errors for status commands,
2041 + * as they are likely when a card is ejected. */
2042 + if (host->overclock) {
2043 + if ((mrq->cmd && mrq->cmd->error &&
2044 + (mrq->cmd->opcode != MMC_SEND_STATUS)) ||
2045 + (mrq->data && mrq->data->error) ||
2046 + (mrq->stop && mrq->stop->error) ||
2047 + (mrq->sbc && mrq->sbc->error)) {
2048 + host->overclock_50--;
2049 + pr_warn("%s: reducing overclock due to errors\n",
2050 + mmc_hostname(host->mmc));
2051 + host->reset_clock = 1;
2052 + mrq->cmd->error = -ETIMEDOUT;
2053 + mrq->cmd->retries = 1;
2059 + host->data = NULL;
2063 + host->dma_desc = NULL;
2064 + terminate_chan = host->dma_chan;
2065 + host->dma_chan = NULL;
2067 + spin_unlock_irqrestore(&host->lock, flags);
2069 + if (terminate_chan)
2071 + int err = dmaengine_terminate_all(terminate_chan);
2073 + pr_err("%s: failed to terminate DMA (%d)\n",
2074 + mmc_hostname(host->mmc), err);
2077 + /* The SDHOST block doesn't report any errors for a disconnected
2078 + interface. All cards and SDIO devices should report some supported
2079 + voltage range, so a zero response to SEND_OP_COND, IO_SEND_OP_COND
2080 + or APP_SEND_OP_COND can be treated as an error. */
2081 + if (((mrq->cmd->opcode == MMC_SEND_OP_COND) ||
2082 + (mrq->cmd->opcode == SD_IO_SEND_OP_COND) ||
2083 + (mrq->cmd->opcode == SD_APP_OP_COND)) &&
2084 + (mrq->cmd->error == 0) &&
2085 + (mrq->cmd->resp[0] == 0)) {
2086 + mrq->cmd->error = -ETIMEDOUT;
2088 + pr_info("%s: faking timeout due to zero OCR\n",
2089 + mmc_hostname(host->mmc));
2092 + mmc_request_done(host->mmc, mrq);
2093 + log_event("TSK>", (u32)mrq, 0);
2096 +int bcm2835_sdhost_add_host(struct bcm2835_host *host)
2098 + struct mmc_host *mmc;
2099 + struct dma_slave_config cfg;
2100 + char pio_limit_string[20];
2105 + mmc->f_max = host->max_clk;
2106 + mmc->f_min = host->max_clk / SDCDIV_MAX_CDIV;
2108 + mmc->max_busy_timeout = (~(unsigned int)0)/(mmc->f_max/1000);
2110 + pr_debug("f_max %d, f_min %d, max_busy_timeout %d\n",
2111 + mmc->f_max, mmc->f_min, mmc->max_busy_timeout);
2113 + /* host controller capabilities */
2115 + MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
2116 + MMC_CAP_NEEDS_POLL | MMC_CAP_HW_RESET | MMC_CAP_ERASE |
2117 + ((ALLOW_CMD23_READ|ALLOW_CMD23_WRITE) * MMC_CAP_CMD23);
2119 + spin_lock_init(&host->lock);
2121 + if (host->allow_dma) {
2122 + if (IS_ERR_OR_NULL(host->dma_chan_rxtx)) {
2123 + pr_err("%s: unable to initialise DMA channel. "
2124 + "Falling back to PIO\n",
2125 + mmc_hostname(mmc));
2126 + host->use_dma = false;
2128 + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2129 + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2130 + cfg.slave_id = 13; /* DREQ channel */
2132 + /* Validate the slave configurations */
2134 + cfg.direction = DMA_MEM_TO_DEV;
2136 + cfg.dst_addr = host->bus_addr + SDDATA;
2138 + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
2141 + host->dma_cfg_tx = cfg;
2143 + cfg.direction = DMA_DEV_TO_MEM;
2144 + cfg.src_addr = host->bus_addr + SDDATA;
2147 + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
2151 + host->dma_cfg_rx = cfg;
2153 + host->use_dma = true;
2155 + pr_err("%s: unable to configure DMA channel. "
2156 + "Falling back to PIO\n",
2157 + mmc_hostname(mmc));
2158 + dma_release_channel(host->dma_chan_rxtx);
2159 + host->dma_chan_rxtx = NULL;
2160 + host->use_dma = false;
2164 + host->use_dma = false;
2167 + mmc->max_segs = 128;
2168 + mmc->max_req_size = 524288;
2169 + mmc->max_seg_size = mmc->max_req_size;
2170 + mmc->max_blk_size = 512;
2171 + mmc->max_blk_count = 65535;
2173 + /* report supported voltage ranges */
2174 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2176 + tasklet_init(&host->finish_tasklet,
2177 + bcm2835_sdhost_tasklet_finish, (unsigned long)host);
2179 + INIT_WORK(&host->cmd_wait_wq, bcm2835_sdhost_cmd_wait_work);
2181 + setup_timer(&host->timer, bcm2835_sdhost_timeout,
2182 + (unsigned long)host);
2184 + bcm2835_sdhost_init(host, 0);
2186 + ret = request_irq(host->irq, bcm2835_sdhost_irq, 0 /*IRQF_SHARED*/,
2187 + mmc_hostname(mmc), host);
2189 + pr_err("%s: failed to request IRQ %d: %d\n",
2190 + mmc_hostname(mmc), host->irq, ret);
2195 + mmc_add_host(mmc);
2197 + pio_limit_string[0] = '\0';
2198 + if (host->use_dma && (host->pio_limit > 0))
2199 + sprintf(pio_limit_string, " (>%d)", host->pio_limit);
2200 + pr_info("%s: %s loaded - DMA %s%s\n",
2201 + mmc_hostname(mmc), DRIVER_NAME,
2202 + host->use_dma ? "enabled" : "disabled",
2203 + pio_limit_string);
2208 + tasklet_kill(&host->finish_tasklet);
2213 +static int bcm2835_sdhost_probe(struct platform_device *pdev)
2215 + struct device *dev = &pdev->dev;
2216 + struct device_node *node = dev->of_node;
2218 + struct resource *iomem;
2219 + struct bcm2835_host *host;
2220 + struct mmc_host *mmc;
2221 + const __be32 *addr;
2225 + pr_debug("bcm2835_sdhost_probe\n");
2226 + mmc = mmc_alloc_host(sizeof(*host), dev);
2230 + mmc->ops = &bcm2835_sdhost_ops;
2231 + host = mmc_priv(mmc);
2233 + host->pio_timeout = msecs_to_jiffies(500);
2234 + host->pio_limit = 1;
2235 + host->max_delay = 1; /* Warn if over 1ms */
2236 + host->allow_dma = 1;
2237 + spin_lock_init(&host->lock);
2239 + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2240 + host->ioaddr = devm_ioremap_resource(dev, iomem);
2241 + if (IS_ERR(host->ioaddr)) {
2242 + ret = PTR_ERR(host->ioaddr);
2246 + addr = of_get_address(node, 0, NULL, NULL);
2248 + dev_err(dev, "could not get DMA-register address\n");
2251 + host->bus_addr = be32_to_cpup(addr);
2252 + pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
2253 + (unsigned long)host->ioaddr,
2254 + (unsigned long)iomem->start,
2255 + (unsigned long)host->bus_addr);
2258 + /* Read any custom properties */
2259 + of_property_read_u32(node,
2260 + "brcm,delay-after-stop",
2261 + &host->delay_after_stop);
2262 + of_property_read_u32(node,
2263 + "brcm,overclock-50",
2264 + &host->user_overclock_50);
2265 + of_property_read_u32(node,
2267 + &host->pio_limit);
2269 + !of_property_read_bool(node, "brcm,force-pio");
2270 + host->debug = of_property_read_bool(node, "brcm,debug");
2273 + host->dma_chan = NULL;
2274 + host->dma_desc = NULL;
2276 + /* Formally recognise the other way of disabling DMA */
2277 + if (host->pio_limit == 0x7fffffff)
2278 + host->allow_dma = false;
2280 + if (host->allow_dma) {
2282 + host->dma_chan_rxtx =
2283 + dma_request_slave_channel(dev, "rx-tx");
2284 + if (!host->dma_chan_rxtx)
2285 + host->dma_chan_rxtx =
2286 + dma_request_slave_channel(dev, "tx");
2287 + if (!host->dma_chan_rxtx)
2288 + host->dma_chan_rxtx =
2289 + dma_request_slave_channel(dev, "rx");
2291 + dma_cap_mask_t mask;
2293 + dma_cap_zero(mask);
2294 + /* we don't care about the channel, any would work */
2295 + dma_cap_set(DMA_SLAVE, mask);
2296 + host->dma_chan_rxtx =
2297 + dma_request_channel(mask, NULL, NULL);
2301 + clk = devm_clk_get(dev, NULL);
2302 + if (IS_ERR(clk)) {
2303 + ret = PTR_ERR(clk);
2304 + if (ret == -EPROBE_DEFER)
2305 + dev_info(dev, "could not get clk, deferring probe\n");
2307 + dev_err(dev, "could not get clk\n");
2311 + host->max_clk = clk_get_rate(clk);
2313 + host->irq = platform_get_irq(pdev, 0);
2314 + if (host->irq <= 0) {
2315 + dev_err(dev, "get IRQ failed\n");
2320 + pr_debug(" - max_clk %lx, irq %d\n",
2321 + (unsigned long)host->max_clk,
2324 + log_init(dev, iomem->start - host->bus_addr);
2327 + mmc_of_parse(mmc);
2329 + mmc->caps |= MMC_CAP_4_BIT_DATA;
2335 + rpi_firmware_property(rpi_firmware_get(NULL),
2336 + RPI_FIRMWARE_SET_SDHOST_CLOCK,
2337 + &msg, sizeof(msg));
2339 + host->firmware_sets_cdiv = (msg[1] != ~0);
2341 + ret = bcm2835_sdhost_add_host(host);
2345 + platform_set_drvdata(pdev, host);
2347 + pr_debug("bcm2835_sdhost_probe -> OK\n");
2352 + pr_debug("bcm2835_sdhost_probe -> err %d\n", ret);
2353 + mmc_free_host(mmc);
2358 +static int bcm2835_sdhost_remove(struct platform_device *pdev)
2360 + struct bcm2835_host *host = platform_get_drvdata(pdev);
2362 + pr_debug("bcm2835_sdhost_remove\n");
2364 + mmc_remove_host(host->mmc);
2366 + bcm2835_sdhost_set_power(host, false);
2368 + free_irq(host->irq, host);
2370 + del_timer_sync(&host->timer);
2372 + tasklet_kill(&host->finish_tasklet);
2374 + mmc_free_host(host->mmc);
2375 + platform_set_drvdata(pdev, NULL);
2377 + pr_debug("bcm2835_sdhost_remove - OK\n");
2381 +static const struct of_device_id bcm2835_sdhost_match[] = {
2382 + { .compatible = "brcm,bcm2835-sdhost" },
2385 +MODULE_DEVICE_TABLE(of, bcm2835_sdhost_match);
2387 +static struct platform_driver bcm2835_sdhost_driver = {
2388 + .probe = bcm2835_sdhost_probe,
2389 + .remove = bcm2835_sdhost_remove,
2391 + .name = DRIVER_NAME,
2392 + .owner = THIS_MODULE,
2393 + .of_match_table = bcm2835_sdhost_match,
2396 +module_platform_driver(bcm2835_sdhost_driver);
2398 +MODULE_ALIAS("platform:sdhost-bcm2835");
2399 +MODULE_DESCRIPTION("BCM2835 SDHost driver");
2400 +MODULE_LICENSE("GPL v2");
2401 +MODULE_AUTHOR("Phil Elwell");